-
Notifications
You must be signed in to change notification settings - Fork 0
/
riskparity.R
551 lines (321 loc) · 16.2 KB
/
riskparity.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#risk parity portfolio preliminaries
source("functions.R")
source("Previous functions/projectfunctions.R")
library(riskParityPortfolio)
library(matlib)
library(xts)
library(highfrequency)
library(matlib)
library(MCS)
library(PerformanceAnalytics)
#####################################################################################################
#
#
# GETTING CLOSE-TO-CLOSE RETURNS
#
#
#####################################################################################################
library(alphavantager)
library(ggplot2)
#source("functions.R")
source("APIKEY.R")
av_api_key(apikey)
TLT <- as.data.frame(av_get(symbol = "TLT", av_fun = "TIME_SERIES_DAILY", outputsize = "full"))
rownames(TLT) <- TLT$timestamp
SPY <- as.data.frame(av_get(symbol = "SPY", av_fun = "TIME_SERIES_DAILY", outputsize = "full"))
rownames(SPY) <- SPY$timestamp
returns_TLT <- as.xts(diff(log(TLT[,4])), order.by = as.Date(TLT[,1], format='%d/%m/%Y')[-1])
returns_SPY <- as.xts(diff(log(SPY[,4])), order.by = as.Date(SPY[,1])[-1])
#####################################################################################################
#
#
# Calculating the risk-free rate from a 3-month T-bill:
#
#
#####################################################################################################
#Calculating the risk-free rate from a 3-month T-bill:
#3month T-bill. Has no coupons
dtb3 <- read.csv("DTB3.csv", header = T)
dtb3[,2] <- as.numeric(levels(dtb3[,2]))[dtb3[,2]]
dtb3[,2] <- na.approx(dtb3[,2]) #dtb3[!is.na(dtb3[,2]), ]
#this is how you should do it:
logriskfreerate <- log(1 + dtb3[,2]/(100*365))
logriskfreerate <- xts(logriskfreerate, order.by = as.Date(dtb3[,1]))
test <- (1+dtb3[,2]/100)^(1/(365))-1
ggplot() + geom_line(aes(1:length(test), test, col = "test")) + geom_line(aes(1:length(test), logriskfreerate, col="RF"))
ts.plot(logriskfreerate*100, col = "red")
intersectMulti <- function(x=list()){
for(i in 2:length(x)){
if(i==2) foo <- x[[i-1]]
foo <- intersect(foo,x[[i]]) #find intersection between ith and previous
}
return(x[[1]][match(foo, x[[1]])]) #get original to retain format
}
indexes <- intersectMulti(list(index(logriskfreerate), index(returns_TLT)))
logriskfreerate <- logriskfreerate[indexes]
#These are now excess returns.
returns_TLT <- returns_TLT[seq(from= as.Date('2010-01-02'), to = as.Date('2019-12-31'), by=1), ] - logriskfreerate
returns_SPY <- returns_SPY[seq(from= as.Date('2010-01-02'), to = as.Date('2019-12-31'), by=1), ] - logriskfreerate
merged_ret <- cbind(returns_TLT, returns_SPY)
#####################################################################################################
#
#
# PORTFOLIO ANALYSIS:
#
#
#####################################################################################################
library(CVXR)
portolioMaxSharpeRatio <- function(mu, Sigma) {
w_ <- Variable(nrow(Sigma))
prob <- Problem(Minimize(quad_form(w_, Sigma)),
constraints = list(w_ >= 0, t(mu) %*% w_ == 1))
result <- solve(prob)
return(as.vector(result$getValue(w_)/sum(result$getValue(w_))))
}
riskparity_2dim <- function(matrix, risktarget, rt = F){
#
#
#
#NOTE TO YOURSELF: Palomar uses non-sqrt portrisk, which gives
#reasonable risk for the unlevered risk-parity portfolio. Using sqrt
#
#REMEMBER TO USE COV(),SD() etc. FOR CLOSE-TO-CLOSE RETURNS, SINCE REALCOV OVERESTIMATES
#THE COVARIANCE FOR CLOSE-TO-CLOSE RETURNS
bonds <- matrix[1,1]
stocks <- matrix[2,2]
w_1 <- sqrt(bonds)^-1 / (sqrt(stocks)^-1 + sqrt(bonds)^-1)
w_2 <- sqrt(stocks)^-1 / (sqrt(stocks)^-1 + sqrt(bonds)^-1)
w <- matrix(c(w_1, w_2), ncol=1, nrow=2) #dimnames = list(c(), c("Bond", "Stock"))
#Palomar uses portfolio variance as portfolio risk, thus no sqrt.
portrisk <- as.numeric(sqrt(t(w) %*% (matrix) %*% w))
riskcont <- w * (matrix %*% w)/portrisk
relativeriskcont <- (w * (matrix %*% w)) / portrisk^2
if(rt){
alpha <- risktarget / portrisk
w_new <- w %*% alpha
w_new <- matrix(c(w_new[1], w_new[2]), ncol=1, nrow=2)
#here is sqrt, while palomar uses variance, no sqrt.
portrisk <- as.numeric(sqrt((t(w_new) %*% matrix %*% w_new))) #gives marginal risk for each asset.
riskcont <- (w_new * (matrix %*% w_new)) / portrisk
w_riskfree <- uniroot(function(x) colSums(w_new)+x-1, interval = c(-100,100))$root
w_new <- matrix(c(w_new, w_riskfree), ncol=1, nrow=3)
rownames(w_new) <- c("TLT", "SPY", "riskfree")
lout <- list(w_new, portrisk, riskcont)
names(lout) <- c("w", "portrisk", "riskcont")
return(lout)
}
lout <- list(w, portrisk, riskcont, relativeriskcont)
names(lout) <- c("w", "portrisk", "riskcont", "relativeriskcont")
return(lout)
}
calccov <- readRDS("calculatedcovariances.rds")
mergedfrequencies <- readRDS("mergedfrequencies.rds")
riskparity_2dim(cov(merged_ret))
riskparity_2dim(calccov[[5]][[6]][,,1])$portrisk*sqrt(252)*100
#-------------------------------portfolio volatility's sensitivity to correlation--------------------------
#getting average vol for tlt and spy.
meanvolTLT <- mean(sqrt(calccov[[1]][[7]][1,1,]*252))
meanvolSPY <- mean(sqrt(calccov[[1]][[7]][2,2,]*252))
correlations <- seq(-0.9,0.9,0.01)
sensitivity <- numeric()
sensitivity2 <- numeric()
for(i in 1:length(correlations)){
newcovariance <- matrix(c(meanvolTLT^2, meanvolTLT*meanvolSPY*correlations[i],
meanvolTLT*meanvolSPY*correlations[i], meanvolSPY^2), ncol = 2, nrow = 2)
w2 <- riskparity_2dim(newcovariance,0,F)$w
portrisk2 <- (riskparity_2dim(newcovariance,0,F)$portrisk)
sensitivity2[i] <- (w2[1]*w2[2]*meanvolTLT*meanvolSPY) / (portrisk2)
}
library(ggplot2)
#Shows the volatility's sensitivity to correlation for the unlevered risk-parity portfolio. In essence,
#upscaling and downscaling the portfolios using a leverage parameter only shifts the graph.
p1 <- ggplot() + geom_line(aes(correlations, sensitivity2*100), col = "red", lwd = 1) +
scale_x_continuous(breaks = round(seq(-0.9,0.9, by = 0.1),1)) + ylab("portfolio volatility (%)") + xlab("Correlation")
#-------------------------------weight distribution dependent on excess returns---------------------------------------
#done for returns until you have control over a risk-free asset.
#source("Previous functions/projectfunctions.R")
stockvol <- seq(0,0.15,0.001)*1e-5
bondvol <- rep(0.03, length(stockvol))*1e-5
covs <- array(0L, c(2,2,length(stockvol)))
for(i in 1:length(stockvol)){
covs[,,i] <- matrix(c(bondvol[i]^2, 0, 0, stockvol[i]^2), ncol=2, nrow=2)
}
weightsfordistribution <- matrix(0L, ncol=2, nrow=length(stockvol))
for(i in 1:length(stockvol)){
weightsfordistribution[i, ] <- riskparity_2dim(covs[,,i])$w[1:2]
}
library(PerformanceAnalytics)
rownames(weightsfordistribution) <- stockvol * 1e5
ggplot() + geom_line(aes(stockvol*1e5, weightsfordistribution[,2]))
#------------------------------------------trying effcient frontier and "risk-parity line"--------------------------
#starts at minvar portfolio and then goes to 100% stocks.
#
#
# Be aware that TLT did a better job than SPY therefore you can only construct this where you go
# 100% into TLT instead of SPY.
#data get and preparation:
#
#
#
#USING CLOSE-TO-CLOSE RETURNS --> COV(), SD() AND NOT INTRADAY MEASURES (since they dont have proper scaling).
#
#
#
#
###################################################
ggplot() + geom_line(aes(index(returns_TLT), 1+cumsum(returns_TLT), col="TLT")) +
geom_line(aes(index(returns_TLT), 1+cumsum(returns_SPY), col="SPY"))
covlol <- cov(merged_ret)
colnames(covlol) <- c("TLT", "SPY")
minvarweights <- minvar(covlol)
minvarret <- merged_ret %*% minvarweights
#SPY
w2 <- seq(minvarweights[2],1,0.001)
#SPY
w1 <- 1-w2
w_synthetic <- matrix(cbind(w1,w2), ncol = 2, nrow = length(w1))
portfolios <- matrix(0L, ncol = length(w1), nrow = length(merged_ret[,2]))
for(i in 1:length(w1)){
portfolios[,i] <- (merged_ret) %*% w_synthetic[i, ]
}
expectedreturns <- colMeans(portfolios)*252*100
portdev <- apply(portfolios, MARGIN = c(2), FUN = function(x) sd(x))* sqrt(252)*100
#expectedreturns <- sort(expectedreturns[1:58], decreasing = T)
#portdev <- sort(portdev[1:58], decreasing = T)
#unlevered risk-parity:
rpunlevered <- riskparity_2dim(covlol)$w
retrpunlevered <- merged_ret %*% rpunlevered
meanrpunlevered <- mean(retrpunlevered) * 252 *100
sdrpunlevered <- sd(retrpunlevered) * sqrt(252) * 100
#constructing risk-parity leverage line. For alpha = 0, then it will obviously be in origo.
#it is not the capital market line.
#0.95
alpha <- seq(0.95,1.7,0.01)
leverageline <- rpunlevered %*% alpha
riskfreeassetcont <- numeric()
for(i in 1:length(alpha)){
riskfreeassetcont[i] <- uniroot(function(x) rowSums(t(leverageline))[i] + x - 1, interval = c(-100,100))$root
}
leverageline <- cbind(t(leverageline), riskfreeassetcont)
leverageline <- t(leverageline)
leveragelineret <- cbind(merged_ret, logriskfreerate) %*% leverageline
leveragelinemeans <- colMeans(leveragelineret) * 252 * 100
leveragelinestds <- apply(leveragelineret, MARGIN = c(2), FUN = function(x) sd(x))* sqrt(252)*100
rplevered <- riskparity_2dim(covlol, 0.0846, T)$w[1:2]
retrplevered <- merged_ret %*% rplevered
meanrplevered <- mean(retrplevered) * 252 *100
sdrplevered <- sd(retrplevered) * sqrt(252) * 100
#Finding the levered risk parity portfolio with same standard deviation as 80/20 portfolio.
root <- uniroot(function(x) sd(merged_ret %*% (rpunlevered %*% x))*sqrt(252)*100 - portdev[366], interval = c(0,100))$root
rplevered <- rpunlevered %*% root
retrplevered <- cbind(merged_ret,logriskfreerate) %*% t(cbind(t(rplevered), -0.4647607))
meanrplevered <- mean(retrplevered) * 252 *100
sdrplevered <- sd(retrplevered) * sqrt(252) * 100
#CML:
tangent <- portolioMaxSharpeRatio(colMeans(merged_ret*100)*252,covlol*10000)
tangentret <- merged_ret %*% tangent
tangentexpectedret <- mean(tangentret) * 100 * 252
tangentdeviation <- sd(tangentret) * 100 * sqrt(252)
sharpetangent <- (tangentexpectedret - mean(logriskfreerate) * 100 * 252)/tangentdeviation
#leverageline computes the weights of the risky-assets and riskfree.
library(PerformanceAnalytics)
CML <- mean(logriskfreerate) * 100 * 252 + sharpetangent * leveragelinestds
p2 <- ggplot() + geom_line(aes(portdev, expectedreturns, col = "Efficient frontier"), lwd=1) +
geom_line(aes(leveragelinestds, leveragelinemeans, col ="Leverage line"), lwd=1) +
geom_point(aes(sd(minvarret)*sqrt(252)*100,colMeans(minvarret)*252*100)) +
geom_line(aes(leveragelinestds, CML, col ="CML"), lwd=1) +
geom_point(aes(portdev[166],expectedreturns[166])) +
geom_point(aes(portdev[366],expectedreturns[366])) +
geom_point(aes(tangentdeviation,tangentexpectedret)) +
geom_point(aes(sdrpunlevered,meanrpunlevered)) +
geom_text(aes(sd(minvarret)*sqrt(252)*100,colMeans(minvarret)*252*100,
label="Minimum variance portfolio"),hjust=-.05, vjust=0) +
geom_text(aes(portdev[166],expectedreturns[166],
label="60/40 equity/bond"),hjust=-0.05, vjust=0.5) +
geom_text(aes(portdev[366],expectedreturns[366],
label="80/20 equity/bond"),hjust=-0.05, vjust=0.5) +
geom_text(aes(sdrpunlevered,meanrpunlevered,
label="Risk-parity unlevered"),hjust=-0.09, vjust=-0.5) +
geom_point(aes(sdrplevered,meanrplevered)) +
geom_text(aes(sdrplevered,meanrplevered,
label="Risk-parity levered"),hjust=-0.05, vjust=0.5) + ylab("Annualized expected returns (%)")+
xlab("Annualized risk (%)") +
theme(legend.title = NULL,legend.position = c(0.70, 0.23), legend.background = element_rect(fill="lightblue", size=0.5,
linetype="solid"),
plot.title = element_text(hjust = 0.5, face = "bold"), axis.title=element_text(size=12))
library(gridExtra)
p3 <- grid.arrange(p1, p2, ncol=2)
ggsave(p3, file="portfoliosensandefficientfront.eps", device = "eps")
#------------------------------------CALCULATING VOL-SCALED PORTFOLIOS-------------------------------------
TLT <- as.data.frame(av_get(symbol = "TLT", av_fun = "TIME_SERIES_DAILY", outputsize = "full"))
rownames(TLT) <- TLT$timestamp
SPY <- as.data.frame(av_get(symbol = "SPY", av_fun = "TIME_SERIES_DAILY", outputsize = "full"))
rownames(SPY) <- SPY$timestamp
returns_TLT <- as.xts(diff(log(TLT[,4])), order.by = as.Date(TLT[,1], format='%d/%m/%Y')[-1])
returns_SPY <- as.xts(diff(log(SPY[,4])), order.by = as.Date(SPY[,1])[-1])
returns_TLT <- returns_TLT[seq(from= as.Date('2010-01-04'), to = as.Date('2019-12-31'), by=1), ] - logriskfreerate
returns_SPY <- returns_SPY[seq(from= as.Date('2010-01-04'), to = as.Date('2019-12-31'), by=1), ] - logriskfreerate
merged_ret <- cbind(returns_TLT, returns_SPY)
#KEY NOTE: IT IS IMPORTANT THAT BOTH ROLLING FORECASTS START WITH SAME FIXED WINDOW TO ACHIEVE CONSISTENCY.
#OTHERWISE ROLLING VARS WILL BE UNDER/OVER ESTIMATED.
varsmerged <- ewma.filter(merged_ret, 30, F, T)
stdTLT <- sqrt(varsmerged[1,1,]) #* sqrt(252)
stdSPY <- sqrt(varsmerged[2,2,]) #* sqrt(252)
#target is in terms of var. That implies that you need to think: For what variance do I get 10% vol.
#Ie. sqrt(0.01)=0.1.NOPE
#decimalnumbers
#finds when first rolling sd.dev is calculated
start <- length(returns_TLT) - length(varsmerged[1,1,])
target <- 0.1
scaledTLT <- (target/stdTLT) * returns_TLT #* 252
scaledSPY <- (target/stdSPY) * returns_SPY #* 252
riskfreealloc <- (target/stdTLT) + (target/stdSPY)
#60/40 portfolio:
portret6040 <- 0.6*scaledSPY + 0.4*scaledTLT
rollingdevportret6040 <- ewma.filter(portret6040, 30, F, T)
rollingdevportret6040 <- xts(sqrt(rollingdevportret6040[1,1,]), order.by = index(returns_TLT))
ggplot() + geom_line(aes(1:length(rollingdevportret6040), rollingdevportret6040)) + geom_hline(yintercept = target)
#leverage returns it to the risk-target
levparam <- 0.1 / rollingdevportret6040
portret6040lev <- levparam * 0.6 * scaledSPY + levparam * 0.4 * scaledTLT
rollingdevportret6040levered <- ewma.filter(portret6040lev, 30, F, T)
rollingdevportret6040levered <- xts(sqrt(rollingdevportret6040levered[1,1,]), order.by = index(returns_TLT))
ggplot() + geom_line(aes(index(rollingdevportret6040levered), rollingdevportret6040levered)) + geom_hline(yintercept = target)
#Scaled vs unscaled returns across assets: NOPE
unscaledacross <- rowMeans(merged_ret)
scaledacross <- rowMeans(cbind(scaledTLT, scaledSPY))
#cumulative frequency.
ggplot() + geom_line(aes(index(merged_ret), 1+cumsum(unscaledacross), col = "Unscaled")) +
geom_line(aes(index(merged_ret), 1+cumsum(scaledacross)*(1/10), col = "Scaled"))
#1 year rolling returns: NOPE
unscaledrolling <- na.omit(rollapply(unscaledacross, 252, function(x) mean(x), by.column = F,
align = 'right'))
scaledrolling <- na.omit(rollapply(scaledacross, 252, function(x) mean(x), by.column = F,
align = 'right'))
ggplot() + geom_line(aes(index(merged_ret)[252:2516], unscaledrolling, col = "Unscaled")) +
geom_line(aes(index(merged_ret)[252:2516], scaledrolling, col = "Scaled"), alpha = 0.4)
#leverage graph where you see for each weight, how much it undertargets the original risk target
weightSPY <- seq(0,1,0.01)
weightTLT <- sort(weightSPY, T)
portfolioreturnsfrontier <- matrix(0L, ncol = length(weightTLT), nrow= 2516)
for(i in 1:length(weightSPY)){
portfolioreturnsfrontier[, i] <- weightSPY[i]*scaledSPY + weightTLT[i]*scaledTLT
}
rollingstd <- matrix(0L, ncol = length(weightTLT), nrow= 2516)
for(i in 1:length(weightSPY)){
rollingstd[,i] <- ewma.filter(xts(portfolioreturnsfrontier[,i], order.by = as.Date(1:2516)), 30, F, T)
}
rollingstd2 <- apply(rollingstd, MARGIN = c(2), FUN = function(x) sqrt(x))
#average absolute deviation away from risk-target:
avgabsdev <- apply(rollingstd2, MARGIN = c(2), FUN = function(x) (0.1-mean(x)))
#leverage parameter:
leverageparams <- apply(rollingstd2, MARGIN = c(2), FUN = function(x) mean(0.1/x))
p4 <- ggplot() + geom_line(aes(weightTLT, avgabsdev, col = "Avg. dev from risk-target"), lwd = 1) +
geom_line(aes(weightTLT, (leverageparams-1)/40, col = "Avg. leverage"), lwd = 1) +
scale_y_continuous(sec.axis = sec_axis(~.*40+1, name = "Leverage")) +
theme(legend.justification=c(0,1), legend.position=c(0.67,0.97),
legend.background = element_rect(fill="lightblue",
size=0.5, linetype="solid",
colour ="darkblue"),
legend.text = element_text(colour="black", size=8, face="bold")) + xlab("Weight") + ylab("Deviation")
ggsave(p4, file="weightandlev.eps", device = "eps")