Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/paper_analysis/R_interpretation/chartCVaRRelPerf.R
1433 views
1
#################################################################################
2
# Create CVaR + RelPerf plot
3
#################################################################################
4
5
# ! Set your working directory (folder containing the subfolders R_allocation, R_interpretation, data, weights, etc)
6
7
setwd("c:/Documents and Settings/Administrator/Desktop/risk budget programs")
8
9
10
# Options:
11
################
12
13
# specify the number of years used for the estimation
14
estyears = 5;
15
Tstart = 1+(8-estyears)*4
16
CC = T
17
# Load programs
18
19
source("R_interpretation/chart.StackedBar.R");
20
library(zoo); library(PerformanceAnalytics)
21
22
# number of risky assets
23
firstyear = 1976 ; firstquarter = 1; lastyear = 2010; lastquarter = 2;
24
cAssets = 4
25
26
# "MinRisk_ReturnTarget" "EqualRisk"
27
# "MinRiskConc_ReturnTarget"
28
29
names = c( "EqualWeight" , "MinRisk" , "MinRisk_PositionLimit" , "MinRisk_RiskLimit" ,
30
"MinRiskConc" , "MinRiskConc_PositionLimit", "EqualRisk" ,
31
"MinRisk_ReturnTarget", "MinRiskConc_ReturnTarget" )
32
if(CC){ names[1:length(names)] = paste( names[1:length(names)], "_CC", sep="") }
33
34
namelabels = c( "Equal-Weight" , "Min CVaR" , "Min CVaR + 40% Position Limit" , "Min CVaR + 40% CVaR Alloc Limit" ,
35
"Min CVaR Concentration" , "Min CVaR Concentration + 40% Position Limit", "Min CVaR + ERC constraint" , "Min CVaR + Return Target" , "Min CVaR Conc + Return Target" )
36
37
riskcrit = "mES"
38
39
if(CC){
40
load( file="mCVaR_CC.Rdata")
41
load( file="mMU_CC.Rdata")
42
}else{
43
load( file="mCVaR.Rdata")
44
load( file="mMU.Rdata")
45
}
46
47
mMU = window(mMU , start=as.Date("1984-01-01") , end=tail(time(mMU),1) )
48
mCVaR = window(mCVaR , start=as.Date("1984-01-01") , end=tail(time(mCVaR),1) )
49
50
# do not plot the last month such that it is fully comparable with out-of-sample plots
51
52
sel = c( 1 : (nrow(mCVaR)-1) );
53
54
55
# Load the data
56
57
nominalreturns = T;
58
if(nominalreturns){
59
load(file=paste(getwd(),"/data/monthlyR.RData",sep="")) }else{ load(file=paste(getwd(),"/data/monthlyR_real.RData")) }
60
R = monthlyR
61
cAssets = ncol(monthlyR)
62
63
# Define rebalancing periods:
64
65
ep = endpoints(monthlyR,on='quarters')
66
# select those for estimation period
67
ep.start = ep[1:(length(ep)-estyears*4)]+1
68
from = time(monthlyR)[ep.start]; from = seq( as.Date(paste(firstyear,"-01-01",sep="")), as.Date(paste(lastyear-estyears,"-07-01",sep="")), by="3 month")
69
ep.end = ep[(1+estyears*4):length(ep)]
70
to = time(monthlyR)[ep.end]
71
cPeriods = length(from);
72
73
74
# Bear periods
75
sp500 = window (monthlyR , start = from[1] , end = to[ length(to) ] )[,2]
76
bear = c(1:length(sp500))[sp500<mean(sp500)]
77
bear = c(1:length(sp500))[sp500<(-0.12)]
78
m.bear.dates = list();
79
i=1;
80
for( b in bear){
81
m.bear.dates[[i]] = c( b-0.5, b+0.5)
82
i = i + 1;
83
}
84
85
out = table.Drawdowns(sp500,top=10)
86
start.bear = out$From[out$Depth<(-0.12)]
87
end.bear = out$Trough[out$Depth<(-0.12)]
88
start.bear.index = c(1:length(sp500))[ time(sp500) ]
89
m.bear.dates = list()
90
v.bear.dates = c()
91
for( i in 1:length(start.bear) ){
92
m.bear.dates[[i]] = c( as.yearmon(start.bear[i]) , as.yearmon(end.bear[i]) )
93
v.bear.dates = c( v.bear.dates , seq(start.bear[i],end.bear[i],"days") )
94
}
95
v.bear.dates = as.Date( v.bear.dates )
96
97
98
###############
99
100
101
mincriterion = "mES" # "mES" , "StdDev"
102
load(paste(getwd(),"/","/oosreturns/", "simplereturns_",mincriterion ,".Rdata" ,sep="") )
103
104
criteria = paste( rep("weights/",length(names) ) , rep(mincriterion,length(names) ) , "/", names , sep="")
105
criteria[ criteria == "weights/StdDev/EqualWeight" ] = "weights/mES/EqualWeight"
106
107
#colnames(simplereturns) = names
108
date = time(simplereturns)
109
110
#> names(simplereturns)
111
#[1] "weights/mES/EqualWeight_CC" "weights/mES/MinRisk_CC" "weights/mES/MinRisk_PositionLimit_CC"
112
#[4] "weights/mES/MinRisk_RiskLimit_CC" "weights/mES/EqualRisk_CC" "weights/mES/MinRiskConc_CC"
113
#[7] "weights/mES/MinRiskConc_PositionLimit_CC"
114
115
116
117
118
###############
119
120
121
if(CC){
122
postscript(file="portfolioCVaRRelPerf_CC.eps")
123
}else{
124
postscript(file="portfolioCVaRRelPerf.eps")
125
}
126
par(mfrow=c(2,1),mar=c(3,4,1,2))
127
128
chart.TimeSeries( mCVaR[sel,c(1,5,2)] , ylim=c(0,max(mCVaR)), ylab = "Portfolio CVaR" , main = "",
129
col=c("black","black","darkgray"), lty=c("dashed","solid","solid"), lwd=c(2,2,2,2) ,
130
auto.grid = TRUE, minor.ticks = FALSE ,
131
period.areas = m.bear.dates , period.color="lightgray",
132
date.format.in = "%Y-%m-%d",date.format = "%b %Y")
133
134
legend("topleft", legend = namelabels[c(1,5,2)],
135
col=c("black","black","darkgray"), lty=c("dashed","solid","solid"), lwd=c(2,2,2) ,cex=0.7)
136
137
chart.RelativePerformance( simplereturns[,c(6,2)] , simplereturns[,c(1)] ,
138
main = "" , lty=c("solid","solid") , ylab="Relative performance vs equal-weight", xlab="",
139
col=c("black","darkgray") , las=1, lwd=c(2,2,5) ,
140
auto.grid = TRUE, minor.ticks = FALSE ,ylim=c(0.7,1.65),
141
period.areas = m.bear.dates , period.color="lightgray",
142
date.format.in = "%Y-%m-%d",date.format = "%b %Y")
143
legend("topleft", legend = c("Min CVaR Concentration","Min CVaR"),
144
col=c("black","darkgray"), lty=c("solid","solid"), lwd=c(2,2,5) ,cex=0.7)
145
146
dev.off()
147
148
if(CC){
149
postscript(file="portfolioCVaRRelPerf_CC_alternatives.eps")
150
}else{
151
postscript(file="portfolioCVaRRelPerf.eps")
152
}
153
par(mfrow=c(2,1),mar=c(3,4,1,2))
154
155
chart.TimeSeries( mCVaR[sel,c(7,4,3,6)] , ylim=c(0,max(mCVaR)), ylab = "Portfolio CVaR" , main = "",
156
col=c("black","darkgray","darkgray","black"), lty=c("solid","dashed","solid","dashed"), lwd=c(2,2,2,2) ,
157
auto.grid = TRUE, minor.ticks = FALSE ,
158
period.areas = m.bear.dates , period.color="lightgray",
159
date.format.in = "%Y-%m-%d",date.format = "%b %Y")
160
161
legend("topleft", legend = namelabels[c(7,4,3,6)],
162
col=c("black","darkgray","darkgray","black"), lty=c("solid","dashed","solid","dashed"), lwd=c(2,2,2) ,cex=0.7)
163
164
chart.RelativePerformance( simplereturns[,c(5,4,3,7)] , simplereturns[,c(1)] ,
165
main = "" , lty=c("solid","dashed","solid","dashed") , ylab="Relative performance vs equal-weight", xlab="",
166
col=c("black","darkgray","darkgray","black") , las=1, lwd=c(2,2,5) ,
167
auto.grid = TRUE, minor.ticks = FALSE ,ylim=c(0.7,1.65),
168
period.areas = m.bear.dates , period.color="lightgray",
169
date.format.in = "%Y-%m-%d",date.format = "%b %Y")
170
171
legend("topleft", legend = namelabels[c(7,4,3,6)],
172
col=c("black","darkgray","darkgray","black"), lty=c("solid","dashed","solid","dashed"), lwd=c(2,2,2) ,cex=0.7)
173
174
175
176
dev.off()
177
178