Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/paper_analysis/R_interpretation/old/RCadjSharpeRatio.R
1433 views
1
2
setwd("c:/Documents and Settings/Administrator/Desktop/risk budget programs")
3
4
# Optimized portfolio you want to analyse out-of-sample performance through (Component) Sharpe ratios
5
6
estyears = 4;
7
percriskcontribcriterion = "mES"
8
RBcon = 0.4#
9
frequency = "quarterly" ;yearly = F;
10
alpha = 0.05; # probability level for which CVaR is computed
11
12
# Load additional programs to interpret the data
13
14
library(zoo); library("PerformanceAnalytics"); source("R_interpretation/pfolioreturn.R");
15
16
# Choose data
17
datacase = "equitybondscommodity" # "equitybondscommodity" or "equitybondscommodity"
18
19
# Define optimization criteria
20
optimcrit = "mES."
21
mainname = paste("weights/" , percriskcontribcriterion,"/", datacase , "/" , frequency , "/" , sep="")
22
criterion = paste(mainname,optimcrit,estyears,"yr",sep="")
23
criteria = c( paste(mainname,"EW",sep="") , paste( criterion , "-InfInf" , sep="") , paste( criterion , "-Inf" , 0.4 , sep="") ,
24
paste( criterion , "0.2" , "0.3" , sep="") );
25
# Load additional programs to interpret the data
26
library(zoo); library("PerformanceAnalytics");
27
28
29
# Load the data
30
31
firstyear = 1976 ; firstquarter = 1; lastyear = 2009; lastquarter = 2;
32
33
data = read.table( file= paste("data/",datacase,"/data.txt",sep="") ,header=T)
34
date = as.Date(data[,1],format="%Y-%m-%d")
35
data = zoo( data[,2:ncol(data)] , order.by = date )
36
37
# "Bond" "SP500" "EAFE" "SPGSCI" "TBill" "Inflation"
38
39
cAssets = 4;
40
head(data);
41
42
monthlyR = data[,(1:(cAssets+1))]
43
monthlyRTBill = monthlyR[,cAssets+1]
44
45
# Define the out-of-sample periods
46
47
from=to=c();
48
if( frequency == "yearly" ){
49
for( year in (firstyear):(lastyear) ){
50
from = c( from , paste( year,"-01-01",sep="" ) ); to = c( to , paste( (year),"-12-31",sep="" ) ) }
51
}else{
52
for( year in (firstyear+estyears):(lastyear) ){
53
from = c( from , paste( year,"-01-01",sep="" ) , paste( year,"-04-01",sep="" ) , paste( year,"-07-01",sep="" ) , paste( year,"-10-01",sep="" ) );
54
to = c( to , paste( year,"-03-31",sep="" ) , paste( year,"-06-30",sep="" ) , paste( year,"-09-30",sep="" ) , paste( year,"-12-31",sep="" ) )}
55
from = from[firstquarter:(length(from)-4+lastquarter)]; to = to[firstquarter:(length(to)-4+lastquarter)]
56
}
57
58
59
# Load the weights
60
61
# Load portfolio weights:
62
63
weightsEW = read.csv( file = paste("weights/", percriskcontribcriterion,"/",datacase,"/",frequency,"/EW.csv" , sep="") );
64
criterion = paste(crit1,".",estyears,"yr",sep="")
65
weightsInf= read.csv( file = paste("weights/", percriskcontribcriterion,"/",datacase,"/",frequency,"/",criterion , "-InfInf.csv" , sep="") );
66
weightsRBcon = read.csv( file = paste("weights/", percriskcontribcriterion,"/",datacase,"/",frequency,"/", criterion , "-Inf",RBconstraint,".csv" , sep="") );
67
weightsERisk = read.csv( file = paste("weights/", percriskcontribcriterion,"/",datacase,"/",frequency,"/", criterion , 0.2,0.3,".csv" , sep="") );
68
69
70
71
# Construct matrix holding the corresponding weights for each month
72
73
if(frequency=="yearly"){ cc = 12 }else{ cc = 3 }
74
75
cRebalancing = length(from);
76
WEW = WInf = WRBcon = WERisk =qEW = qInf = qRBcon = qERisk =c()
77
78
evalR = window(monthlyR , start = as.Date(from[1]) , end = as.Date(tail(to,n=1)) ) ;nobs = dim(evalR)[1];
79
80
for (row in 1:cRebalancing){
81
WInfrow = as.numeric(weightsInf[row,]); WRBconrow = as.numeric(weightsRBcon[row,]) ; WERiskrow = as.numeric(weightsERisk[row,]) ;
82
WEW = rbind( WEW , matrix( rep( c(rep(1/cAssets,cAssets),0) , cc ) , nrow= cc , byrow=T) )
83
WInf = rbind( WInf , matrix( rep( WInfrow , cc ) , nrow= cc , byrow=T) )
84
WRBcon = rbind( WRBcon , matrix( rep(WRBconrow , cc ) , nrow= cc , byrow=T) )
85
WERisk = rbind( WERisk , matrix( rep(WERiskrow , cc ) , nrow= cc , byrow=T) )
86
# compute also in this loop the 5% quantile: different for each portfolio weight
87
preturnsEWrow = rowSums( evalR*matrix( rep( c(rep(1/cAssets,cAssets),0),nrow(evalR) ), nrow =nrow(evalR) , byrow=T ) );
88
preturnsInfrow = rowSums( evalR*matrix( rep( WInfrow,nrow(evalR) ), nrow =nrow(evalR) , byrow=T ) );
89
preturnsRBconrow = rowSums( evalR*matrix(rep( WRBconrow,nrow(evalR)), nrow =nrow(evalR),byrow=T ) );
90
preturnsERiskrow = rowSums( evalR*matrix(rep( WERiskrow,nrow(evalR)), nrow =nrow(evalR),byrow=T ) );
91
qEW = c( qEW , as.numeric(quantile( preturnsInfrow, probs=alpha )) )
92
qInf = c( qInf , as.numeric(quantile( preturnsInfrow, probs=alpha )) )
93
qRBcon = c( qRBcon , as.numeric(quantile( preturnsRBconrow, probs=alpha )) )
94
qERisk = c( qERisk , as.numeric(quantile( preturnsERiskrow, probs=alpha )) )
95
}
96
97
# Compute portfolio returns (We need the aggregation property)
98
99
preturnsEW = rowSums( evalR*WEW )
100
preturnsInf = rowSums( evalR*WInf )
101
preturnsRBcon = rowSums( evalR*WRBcon )
102
preturnsERisk = rowSums( evalR*WERisk )
103
104
# Compute portfolio Sharpe ratio
105
106
riskfree.R = window(monthlyRTBill, start = as.Date(from[1]) , end = as.Date(tail(to,n=1)) ) ;
107
excessEW = mean(preturnsEW) - mean(riskfree.R)
108
excessInf = mean(preturnsInf) - mean(riskfree.R)
109
excessRBcon = mean(preturnsRBcon) - mean(riskfree.R)
110
excessERisk = mean(preturnsERisk) - mean(riskfree.R)
111
112
# Standard sharpe ratio
113
114
portfolioSREW = sqrt(12)*excessEW/( sd(preturnsEW) ) ; print(- portfolioSREW )
115
portfolioSRInf = sqrt(12)*excessInf/(sd(preturnsInf) ) ; print(- portfolioSRInf )
116
portfolioSRRBcon = sqrt(12)*excessRBcon/( sd(preturnsRBcon) ) ; print(- portfolioSRRBcon )
117
portfolioSRERisk = sqrt(12)*excessERisk /( sd(preturnsERisk) ) ; print(- portfolioSRERisk )
118
119
120
# Indicator for beyon VaR losses
121
122
indicatorEW = na.omit( c(1:nobs)[preturnsEW<qEW ]);
123
indicatorInf = na.omit( c(1:nobs)[preturnsInf<qInf ]);
124
indicatorRBcon = na.omit( c(1:nobs)[preturnsRBcon<qRBcon ]);
125
indicatorERisk = na.omit( c(1:nobs)[preturnsERisk<qERisk ]);
126
127
HCVaREW = -( sum(preturnsEW[indicatorEW]) )
128
HCVaRInf = -(sum(preturnsInf[indicatorInf]) )
129
HCVaRRBcon = -( sum(preturnsRBcon[indicatorRBcon]) )
130
HCVaRERisk = -( sum(preturnsERisk[indicatorERisk]) )
131
132
portfolioSREW = sqrt(12)*excessEW/HCVaREW
133
portfolioSRInf = sqrt(12)*excessInf/HCVaRInf
134
portfolioSRRBcon = sqrt(12)*excessRBcon/HCVaRRBcon
135
portfolioSRERisk = sqrt(12)*excessERisk /HCVaRERisk
136
137
138
# Compute Component Sharpe ratio
139
140
componentSREW = sqrt(12)*( apply( evalR*WEW - riskfree.R*WEW, 2 , 'mean' ) )/HCVaREW
141
componentSRInf = sqrt(12)*( apply( evalR*WInf - riskfree.R*WInf, 2 , 'mean' ) )/HCVaRInf
142
componentSRRBcon = sqrt(12)*( apply( evalR*WRBcon - riskfree.R*WRBcon, 2 , 'mean' ) )/HCVaRRBcon
143
componentSRERisk = sqrt(12)*( apply( evalR*WERisk - riskfree.R*WERisk, 2 , 'mean' ) )/HCVaRERisk
144
145
adjEW = ( apply( WEW[indicatorEW,]*(evalR[indicatorEW,]-preturnsEW[indicatorEW]) , 2 , 'sum') )/HCVaREW^2
146
adjInf = ( apply( WInf[indicatorInf,]*(evalR[indicatorInf,]-preturnsInf[indicatorInf]) , 2 , 'sum') )/HCVaRInf^2
147
adjRBcon = ( apply( WRBcon[indicatorRBcon,]*(evalR[indicatorRBcon,]-preturnsRBcon[indicatorRBcon]) , 2 , 'sum') )/HCVaRRBcon^2
148
adjERisk = ( apply( WERisk[indicatorERisk,]*(evalR[indicatorERisk,]-preturnsERisk[indicatorERisk]) , 2 , 'sum') )/HCVaRERisk^2
149
150
print("Equal-weighted") ;
151
print("------------------------------------------------------------")
152
print("Total Sharpe ratio portfolio without risk budget constraints")
153
print(portfolioSREW);
154
print("And its decomposition with lambda = 0")
155
print(componentSREW)
156
print("And its decomposition with lambda = 0.05")
157
print(componentSREW + 0.05*adjEW)
158
print("And its decomposition with lambda = 0.1")
159
print(componentSREW + 0.1*adjEW)
160
161
print("Min CVaR portfolios") ;
162
print("------------------------------------------------------------")
163
print("Total Sharpe ratio portfolio without risk budget constraints")
164
print(portfolioSRInf);
165
print("And its decomposition with lambda = 0")
166
print(componentSRInf)
167
print("And its decomposition with lambda = 0.05")
168
print(componentSRInf + 0.05*adjInf)
169
print("And its decomposition with lambda = 0.1")
170
print(componentSRInf + 0.1*adjInf)
171
172
print("------------------------------------------------------------")
173
print("Total Sharpe ratio portfolio with risk budget constraints")
174
print(portfolioSRRBcon );
175
print("And its decomposition without adjustment")
176
print(componentSRRBcon)
177
print("And its decomposition with lambda = 0.05")
178
print(componentSRRBcon+ 0.05*adjRBcon)
179
print("And its decomposition with lambda = 0.1")
180
print(componentSRRBcon+ 0.1*adjRBcon)
181
182
print("------------------------------------------------------------")
183
print("Total Sharpe ratio equal-risk portfolio")
184
print(portfolioSRERisk );
185
print("And its decomposition without adjustment")
186
print(componentSRERisk)
187
print("And its decomposition with lambda = 0.05")
188
print(componentSRERisk+ 0.05*adjERisk)
189
print("And its decomposition with lambda = 0.1")
190
print(componentSRERisk+ 0.1*adjERisk)
191
192
193