Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/paper_analysis/R_interpretation/old/performanceanalysis.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
datacase = "equitybondscommodity" # "intequitybonds" or "equitybondscommodity"
7
8
estyears = 4;
9
percriskcontribcriterion = "mES"
10
RBcon = 0.4#
11
frequency = "quarterly" ;yearly = F;
12
alpha = 0.05; # probability level for which CVaR is computed
13
14
# Load additional programs to interpret the data
15
16
library(zoo); library("PerformanceAnalytics"); source("R_interpretation/pfolioreturn.R");
17
18
# Choose data
19
datacase = "equitybondscommodity" # "equitybondscommodity" or "equitybondscommodity"
20
21
# Define optimization criteria
22
23
optimcrit = "mES."
24
mainname = paste("weights/" , percriskcontribcriterion,"/", datacase , "/" , frequency , "/" , sep="")
25
criterion = paste(mainname,optimcrit,estyears,"yr",sep="")
26
criteria = c( paste(mainname,"EW",sep="") ,
27
paste(mainname,"unconstrained/", optimcrit , "4yr-InfInf" , sep="") ,
28
paste( criterion , "-InfInf" , sep="") , paste( criterion , "-Inf" , 0.4 , sep="") ,
29
paste( mainname,"unconstrained/", optimcrit , "4yr0.220.28" , sep="") );
30
31
source("R_Allocation/allocation_functions_monthly.R"); source("R_Allocation/estimators.R");
32
library(zoo); library("PerformanceAnalytics");
33
memory.limit(2048)
34
35
#############################################################################
36
# Load the data
37
#############################################################################
38
39
firstyear = 1976 ; firstquarter = 1; lastyear = 2009; lastquarter = 2;
40
41
data = read.table( file= paste("data/",datacase,"/data.txt",sep="") ,header=T)
42
date = as.Date(data[,1],format="%Y-%m-%d")
43
monthlyR = zoo( data[,2:ncol(data)] , order.by = date )
44
45
# "Bond" "SP500" "EAFE" "SPGSCI" "TBill" "Inflation"
46
47
cAssets = 4;
48
head(monthlyR);
49
50
apply( monthlyR , 2 , 'mean' )*100
51
apply( monthlyR , 2 , 'sd' )*100
52
apply( monthlyR , 2 , 'skew' )
53
apply( monthlyR , 2 , 'exkur' )
54
cor(monthlyR)
55
monthlyRTBill = monthlyR[,cAssets+1]
56
57
# Define the out-of-sample periods
58
59
from=to=c();
60
if( frequency == "yearly" ){
61
for( year in (firstyear):(lastyear) ){
62
from = c( from , paste( year,"-01-01",sep="" ) ); to = c( to , paste( (year),"-12-31",sep="" ) ) }
63
}else{
64
for( year in (firstyear+estyears):(lastyear) ){
65
from = c( from , paste( year,"-01-01",sep="" ) , paste( year,"-04-01",sep="" ) , paste( year,"-07-01",sep="" ) , paste( year,"-10-01",sep="" ) );
66
to = c( to , paste( year,"-03-31",sep="" ) , paste( year,"-06-30",sep="" ) , paste( year,"-09-30",sep="" ) , paste( year,"-12-31",sep="" ) )}
67
from = from[firstquarter:(length(from)-4+lastquarter)]; to = to[firstquarter:(length(to)-4+lastquarter)]
68
}
69
70
# Compute daily out of sample returns, accounting for compounding effects
71
72
Returns.rebalancing( R = monthlyR , criteria = criteria, from = from, to = to , folder=paste("/oosreturns/",datacase,"/", frequency ,"/", sep="") )
73
oosdates = time( window (monthlyR , start = from[1] , end = to[ length(to) ] ) )
74
75
load(paste(getwd(),"/","/oosreturns/",datacase,"/", frequency , "/simplereturns.Rdata" ,sep="") )
76
77
colnames(simplereturns) = c( "EW" , "UnconstrainedMinCVaR", "MaxWeightConstrained" , "MaxRiskbudgetconstrained" , "EqualRisk" )
78
date = time(simplereturns)
79
80
# Assess out of sample performance of the minimum StdDev portfolio through the rolling centered 3-year rolling standard deviation
81
82
sd_EW = rollapply(data=simplereturns[,1],width=36, FUN=sd)
83
sd_MinCVaR = rollapply(data=simplereturns[,2],width=36, FUN=sd)
84
sd_weightconstrained = rollapply(data=simplereturns[,3],width=36, FUN=sd)
85
sd_RCconstrained = rollapply(data=simplereturns[,4],width=36, FUN=sd)
86
sd_equalrisk = rollapply(data=simplereturns[,5],width=36, FUN=sd)
87
88
# Assess out of sample performance of the maximum Sharpe ratio through relative cumulative performance chart
89
90
cumperf_EW = cumprod(1 + na.omit(simplereturns[,1]))
91
cumperf_MinCVaR = cumprod(1 + na.omit(simplereturns[,2]))
92
cumperf_weightconstrained = cumprod(1 + na.omit(simplereturns[,3]))
93
cumperf_RCconstrained = cumprod(1 + na.omit(simplereturns[,4]))
94
cumperf_equalrisk = cumprod(1 + na.omit(simplereturns[,5]))
95
96
97
postscript( file=paste("fig/",percriskcontribcriterion,"/",datacase,"/",frequency,"/relativeperformance.eps",sep="") )
98
99
par(mfrow=c(4,1), mar = c(2,2,3,2) , cex.main = 1.1 , cex.axis=0.8);
100
101
plot( sd_RCconstrained /sd_EW ,
102
main="Relative rolling 3 month standard deviation w.r.t. equal-weighted", type = "l" )
103
104
plot( sd_RCconstrained /sd_MinCVaR ,
105
main="Relative rolling 3 month standard deviation w.r.t. min CVaR", type = "l" )
106
107
plot( sd_RCconstrained /sd_weightconstrained ,
108
main="Relative rolling 3 month standard deviation w.r.t. max 40 % weight constrained", type = "l" )
109
110
plot( sd_RCconstrained /sd_equalrisk ,
111
main="Relative rolling 3 month standard deviation w.r.t. equal-risk", type = "l" )
112
113
114
par(mfrow=c(4,1), mar = c(2,2,3,2) , cex.main = 1.1 , cex.axis=0.8);
115
116
plot( cumperf_equalrisk /cumperf_EW , main="Relative cumulative performance w.r.t. equal-weighted", type = "l" )
117
118
plot( cumperf_equalrisk /cumperf_MinCVaR , main="Relative cumulative performance w.r.t. unconstrained Min CVaR", type = "l" )
119
120
plot( cumperf_equalrisk /cumperf_weightconstrained , main="Relative cumulative performance w.r.t. max 40 % weight constrained", type = "l" )
121
122
plot( cumperf_equalrisk /cumperf_RCconstrained , main="Relative cumulative performance w.r.t. equal-risk", type = "l" )
123
124
125
dev.off()
126
127
library(PerformanceAnalytics)
128
library(zoo)
129
oosreturns = zoo(simplereturns[,1:5],order.by = seq.Date(as.Date(from[1])+31, as.Date(tail(to,1)) + 1, by ="month") - 1)
130
charts.PerformanceSummary(oosreturns)
131
table.Drawdowns(oosreturns[,1],top=10)
132
table.Drawdowns(oosreturns[,2],top=10)
133
table.Drawdowns(oosreturns[,3],top=10)
134
table.Drawdowns(oosreturns[,4],top=10)
135
table.Drawdowns(oosreturns[,5],top=10)
136
137
# Portfolio turnover per strategy:
138
139
# compute endweights:
140
(1+simplereturns)/rowSums(simplereturns)
141
142
# frequency of rebalancing: yearly of quarterly
143
estyears = 4;
144
percriskcontribcriterion = "mES"
145
RBconstraint = 0.4#
146
frequency = "quarterly" ;
147
crit1="mES";
148
# Load portfolio weights:
149
150
Z = read.csv( file = paste(criteria[1],".csv" , sep="") );
151
wstart_EW = cbind(Z$Bond,Z$SP500,Z$EAFE,Z$TBill)
152
Z = read.csv( file = paste(criteria[2],".csv" , sep="") );
153
wstart_MinCVaR = cbind(Z$Bond,Z$SP500,Z$EAFE,Z$TBill)
154
Z = read.csv( file = paste(criteria[3],".csv" , sep="") );
155
wstart_weightconst = cbind(Z$Bond,Z$SP500,Z$EAFE,Z$TBill);
156
Z = read.csv( file = paste(criteria[4],".csv" , sep="") );
157
wstart_RCconst = cbind(Z$Bond,Z$SP500,Z$EAFE,Z$TBill)
158
Z = read.csv( file = paste(criteria[5],".csv" , sep="") );
159
wstart_equalrisk = cbind(Z$Bond,Z$SP500,Z$EAFE,Z$TBill)
160
161
162
cRebalancing = length(from)
163
164
cumR = c()
165
oosR = window (monthlyR , start = from[1] , end = to[ length(to) ] )
166
167
for( i in 1:cRebalancing ){
168
sel = seq( (i-1)*3+1 , i*3 )
169
cumR = rbind( cumR , apply((1+oosR[sel,]),2,'cumprod')[3,] )
170
}
171
172
wend_EW = (wstart_EW[1:cRebalancing]*cumR)/rowSums( wstart_EW[1:cRebalancing]*cumR )
173
wend_MinCVaR = (wstart_MinCVaR[1:cRebalancing]*cumR)/rowSums( wstart_MinCVaR[1:cRebalancing]*cumR )
174
wend_weightconst = (wstart_weightconst[1:cRebalancing]*cumR)/rowSums( wstart_weightconst[1:cRebalancing]*cumR )
175
wend_RCconst = (wstart_RCconst[1:cRebalancing]*cumR)/rowSums( wstart_RCconst[1:cRebalancing]*cumR )
176
wend_equalrisk = (wstart_equalrisk[1:cRebalancing]*cumR)/rowSums( wstart_equalrisk[1:cRebalancing]*cumR )
177
178
turnover_EW = mean( abs(wend_EW[1:(cRebalancing-1)] - wstart_EW[2:cRebalancing] ) )
179
turnover_MinCVaR = mean( abs(wend_MinCVaR[1:(cRebalancing-1)] - wstart_MinCVaR[2:cRebalancing] ) )
180
turnover_weightconst = mean( abs(wend_weightconst[1:(cRebalancing-1)] - wstart_weightconst[2:cRebalancing]) )
181
turnover_RCconst = mean( abs(wend_RCconst[1:(cRebalancing-1)] - wstart_RCconst[2:cRebalancing] ) )
182
turnover_equalrisk = mean( abs(wend_equalrisk[1:(cRebalancing-1)] - wstart_equalrisk[2:cRebalancing] ) )
183
184
185