Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/paper_analysis/insample/bondequity.R
1433 views
1
setwd("c:/Documents and Settings/Administrator/Desktop/risk budget programs/insample")
2
3
# Equal risk portfolio
4
cAssets = 4; # number of assets influences the cleaning
5
p = priskbudget = 0.95;
6
7
mincriterion = "mES" ; percriskcontribcriterion = "mES";
8
9
# Load programs
10
11
source("Risk_budget_functions.R");
12
library(zoo); library(fGarch); library("PerformanceAnalytics"); library("PortfolioAnalytics")
13
clean = TRUE; CC = T
14
15
# Load the data
16
firstyear = 1976 ; firstquarter = 1; lastyear = 2010; lastquarter = 2;
17
data = read.table( file= paste(getwd(),"/data.txt",sep="") ,header=T)
18
date = as.Date(data[,1],format="%Y-%m-%d")
19
20
monthlyR = zoo( data[,2:(1+cAssets)] , order.by = date )
21
22
set.seed(1234)
23
if(clean){ monthlyR = clean.boudt2(monthlyR,alpha=0.05)[[1]] }
24
monthlyR = monthlyR[,1:2]
25
mu = apply(monthlyR,2,'mean')
26
sigma = cov(monthlyR)
27
# N = 2 : no need for CC
28
M3 = PerformanceAnalytics:::M3.MM(monthlyR-matrix( rep(as.numeric(mu),nrow(monthlyR)) , nrow=nrow(monthlyR) , byrow=TRUE) );
29
M4 = PerformanceAnalytics:::M4.MM(monthlyR-matrix( rep(as.numeric(mu),nrow(monthlyR)) , nrow=nrow(monthlyR) , byrow=TRUE) )
30
31
N = ncol(monthlyR)
32
33
# Summary stats individual assets
34
35
apply(monthlyR,2,'mean')*12
36
apply(monthlyR,2,'sd')*sqrt(12)
37
apply(monthlyR,2,'skewness')
38
apply(monthlyR,2,'kurtosis')
39
40
41
42
mESfun2 = function( w ){ return( operPortMES(w,mu=mu,alpha=0.05,sigma=sigma,M3=M3,M4=M4)[[1]] ) }
43
assetCVaR = rep(0,2);
44
for( i in 1:2 ){
45
w = rep(0,2); w[i]=1;
46
assetCVaR[i] = mESfun2( as.matrix(w) )
47
}
48
assetCVaR
49
50
51
#################################################################################
52
# Make Exhibit 2 Risk budget paper: weight and CVaR allocation static portfolios
53
#################################################################################
54
55
indexes = monthlyR
56
57
# Equal-weight portfolio
58
w5050 <- c(0.5,0.5)
59
sum( w5050*mu*12 ) ;
60
#VaR(R=indexes[,1:2], weights=w5050, portfolio_method="component")
61
ES(R=indexes[,1:2], weights=w5050, portfolio_method="component", mu = mu, sigma = sigma, m3=M3, m4=M4,invert=FALSE)
62
63
# 60/40 bond equity allocation
64
w6040 <- c(0.6,0.4);
65
sum( w6040*mu*12 ) ;
66
#VaR(R=indexes[,1:2], weights=w6040, portfolio_method="component")
67
ES(R=indexes[,1:2], weights=w6040, portfolio_method="component", mu = mu, sigma = sigma, m3=M3, m4=M4,invert=FALSE)
68
69
70
# Min CVaR portfolio
71
72
73
library(DEoptim)
74
obj <- function(w) {
75
if (sum(w) == 0) { w <- w + 1e-2 }
76
w <- w / sum(w)
77
ES(R=indexes[,1:2],weights = matrix(w,ncol=1), mu = mu, sigma = sigma, m3=M3, m4=M4,invert=FALSE)
78
}
79
out <- DEoptim(fn = obj, lower = rep(0, 2), upper = rep(1, 2),
80
DEoptim.control(itermax=100))
81
wstar <- out$optim$bestmem
82
wMinCVaR <- wstar / sum(wstar)
83
print(wMinCVaR)
84
# par1 par2
85
#0.96183602 0.03816398
86
# wMinCVaR = c( 0.96864323 , 0.03135677 )
87
print(sum(wMinCVaR*mu*12))
88
ES(R=indexes[,1:2], weights=matrix(wMinCVaR,ncol=1),portfolio_method="component", mu = mu, sigma = sigma, m3=M3, m4=M4)
89
90
91
# Min CVaR Concentration portfolio
92
93
obj <- function(w) {
94
if (sum(w) == 0) { w <- w + 1e-2 }
95
w <- w / sum(w)
96
CVaR <- ES(R=indexes[,1:2], weights=matrix(w,ncol=1),portfolio_method="component", mu = mu, sigma = sigma, m3=M3, m4=M4)
97
out <- max(CVaR$contribution)
98
}
99
out <- DEoptim(fn = obj, lower = rep(0, 2), upper = rep(1, 2),
100
DEoptim.control(itermax=100))
101
wstar <- out$optim$bestmem
102
wMinCVaRConc <- wstar / sum(wstar)
103
print(wMinCVaRConc)
104
#> print(wMinCVaRConc)
105
# par1 par2
106
#0.7751015 0.2248985
107
# wMinCVaRConc = c( 00.7700542 , 0.2299458 )
108
print(sum(wMinCVaRConc*mu*12))
109
ES(R=indexes[,1:2], weights=wMinCVaRConc, portfolio_method="component", mu = mu, sigma = sigma, m3=M3, m4=M4)
110
111
112
113
# 60/40 Risk allocation portfolio
114
obj <- function(w) {
115
if (sum(w) == 0) { w <- w + 1e-2 }
116
w <- w / sum(w)
117
CVaR <- ES(R=indexes[,1:2], weights=matrix(w,ncol=1),portfolio_method="component", mu = mu, sigma = sigma, m3=M3, m4=M4)
118
tmp1 <- CVaR$MES
119
tmp2 <- max(CVaR$pct_contrib_MES - c(0.601,0.401 ) , 0)
120
tmp3 <- max(c(0.599,0.399 ) - CVaR$pct_contrib_MES , 0)
121
out <- tmp1 + 1e3 * tmp2 + 1e3 * tmp3
122
}
123
out <- DEoptim(fn = obj, lower = rep(0, 2), upper = rep(1, 2),
124
DEoptim.control(itermax=100))
125
wstar <- out$optim$bestmem
126
w6040riskalloc <- wstar / sum(wstar)
127
print(w6040riskalloc)
128
# par1 par2
129
#0.8193828 0.1806172
130
print(sum(w6040riskalloc*mu*12))
131
# w6040riskalloc = c( 0.7290461 , 0.2709539 )
132
ES(R=indexes[,1:2], weights=w6040riskalloc, portfolio_method="component", mu = mu, sigma = sigma, m3=M3, m4=M4)
133
134
135
136
137
138
139
140
141
142
143
144