forked from systematicinvestor/SIT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaa.bl.r
200 lines (156 loc) · 6.82 KB
/
aa.bl.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
###############################################################################
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###############################################################################
# Black-Litterman model Functions
# Copyright (C) 2011 Michael Kapler
#
# For more information please visit my blog at www.SystematicInvestor.wordpress.com
# or drop me a line at TheSystematicInvestor at gmail
###############################################################################
###############################################################################
# Black-Litterman model Functions
###############################################################################
# He & Litterman: The intuition Behind Black- Litterman Model Portfolios
# T. Idzorek: A STEP-BY-STEP GUIDE TO THE BLACK-LITTERMAN MODEL
# note (5)
#
#' @export
bl.compute.risk.aversion <- function(bench, risk.free = 0)
{
# The implied risk aversion coefficient can be estimated by dividing
# the expected excess return by the variance of the portfolio
lambda = mean(coredata(bench) - coredata(risk.free)) / var(coredata(bench))
return( as.double(lambda) )
}
# He & Litterman: The intuition Behind Black- Litterman Model Portfolios
# formulas (2)
#
# T. Idzorek: A STEP-BY-STEP GUIDE TO THE BLACK-LITTERMAN MODEL
# formulas (1)
#
# use reverse optimization to compute the vector of equilibrium returns
#' @export
bl.compute.eqret <- function
(
risk.aversion, # Risk Aversion
cov, # Covariance matrix
cap.weight, # Market Capitalization Weights
risk.free = 0 # Rsik Free Interest Rate
)
{
return( risk.aversion * cov %*% as.vector(cap.weight) + as.double(risk.free))
}
# He & Litterman: The intuition Behind Black- Litterman Model Portfolios
# formulas (8), (9), (10)
# compute the posterior estimate of the returns and covariance
#' @export
bl.compute.posterior <- function
(
mu, # Equilibrium returns
cov, # Covariance matrix
pmat=NULL, # Views pick matrix
qmat=NULL, # View mean vector
tau=0.025 # Measure of uncertainty of the prior estimate of the mean returns
)
{
out = list()
if( !is.null(pmat) ) {
omega = diag(c(1,diag(tau * pmat %*% cov %*% t(pmat))))[-1,-1]
temp = solve(solve(tau * cov) + t(pmat) %*% solve(omega) %*% pmat)
out$cov = cov + temp
out$expected.return = temp %*% (solve(tau * cov) %*% mu + t(pmat) %*% solve(omega) %*% qmat)
} else { # no views
temp = tau * cov
out$cov = cov + temp
out$expected.return = temp %*% (solve(tau * cov) %*% mu )
}
return(out)
}
# He & Litterman: The intuition Behind Black- Litterman Model Portfolios
# formulas (13)
#
# T. Idzorek: A STEP-BY-STEP GUIDE TO THE BLACK-LITTERMAN MODEL
# formulas (2)
#
# compute the portfolio weights for the optimal portfolio on the unconstrained efficient frontier
#' @export
bl.compute.optimal <- function(risk.aversion, mu, cov)
{
return( (1/risk.aversion) * solve(cov) %*% mu )
}
aa.black.litterman.examples <- function()
{
# He & Litterman: The intuition Behind Black- Litterman Model Portfolios.
data =
'1,0.4880,0.4780,0.5150,0.4390,0.5120,0.4910
0.4880,1,0.6640,0.6550,0.3100,0.6080,0.7790
0.4780,0.6640,1,0.8610,0.3550,0.7830,0.6680
0.5150,0.6550,0.8610,1,0.3540,0.7770,0.6530
0.4390,0.3100,0.3550,0.3540,1,0.4050,0.3060
0.5120,0.6080,0.7830,0.7770,0.4050,1,0.6520
0.4910,0.7790,0.6680,0.6530,0.3060,0.6520,1'
Corrmat = matrix( as.double(spl( gsub('\n', ',', data), ',')),
nrow = len(spl(data, '\n')), byrow=TRUE)
RiskAversion = 2.5
stdevs = c(16.0, 20.3, 24.8, 27.1, 21.0, 20.0, 18.7)/100
MktWeight = c(1.6, 2.2, 5.2, 5.5, 11.6, 12.4, 61.5)/100
tau = 0.05
Covmat = Corrmat * (stdevs %*% t(stdevs))
EqRiskPrem = RiskAversion * Covmat %*% MktWeight
EqRiskPrem = bl.compute.eqret(RiskAversion, Covmat, MktWeight)
AssetNames = c('Australia','Canada','France','Germany','Japan','UK','USA')
Table2 = cbind(AssetNames, round(cbind(stdevs, MktWeight, EqRiskPrem) * 100,1))
colnames(Table2) = c('Assets','Std Dev','Weq','PI')
Table2
#View1 is The German Equity Market Will Outperform the rest of European Markets by 5% a year.
P = matrix(c(0, 0, -29.5, 100, 0, -70.5, 0)/100, nrow=1)
Q = 5/100
Omega = diag(c(1,diag(tau * P %*% Covmat %*% t(P))))[-1,-1]
PostCov = solve(solve(tau*Covmat) + (t(P) %*% solve(Omega) %*% P))
SigmaP = Covmat + PostCov
ExpRet = PostCov %*% (solve(tau * Covmat) %*% EqRiskPrem + t(P) %*% solve(Omega) %*% Q)
post = bl.compute.posterior(EqRiskPrem, Covmat, P, Q, tau)
ExpRet = post$expected.return
SigmaP = post$cov
OptimalWeights = (1/RiskAversion) * solve(SigmaP) %*% ExpRet
OptimalWeights = bl.compute.optimal(RiskAversion, ExpRet, SigmaP)
Tab4Col4 = OptimalWeights - (MktWeight)/(1+tau)
Table4 = cbind(AssetNames, round(cbind(t(P), ExpRet, OptimalWeights, round(Tab4Col4 * 1000)/1000)*100,1))
colnames(Table4) = c('Assets', 'P', 'MU', 'W','W - Weq/1+tau')
Table4
# example from Thomas M. Idzorek's paper "A STEP-BY-STEP GUIDE TO THE BLACK-LITTERMAN MODEL"
x =
c(0.001005,0.001328,-0.000579,-0.000675,0.000121,0.000128,-0.000445,-0.000437 ,
0.001328,0.007277,-0.001307,-0.000610,-0.002237,-0.000989,0.001442,-0.001535 ,
-0.000579,-0.001307,0.059852,0.027588,0.063497,0.023036,0.032967,0.048039 ,
-0.000675,-0.000610,0.027588,0.029609,0.026572,0.021465,0.020697,0.029854 ,
0.000121,-0.002237,0.063497,0.026572,0.102488,0.042744,0.039943,0.065994 ,
0.000128,-0.000989,0.023036,0.021465,0.042744,0.032056,0.019881,0.032235 ,
-0.000445,0.001442,0.032967,0.020697,0.039943,0.019881,0.028355,0.035064 ,
-0.000437,-0.001535,0.048039,0.029854,0.065994,0.032235,0.035064,0.079958 )
varCov <- matrix(x, ncol = 8, nrow = 8)
mu <- c(0.08, 0.67,6.41, 4.08, 7.43, 3.70, 4.80, 6.60) / 100
pick <- matrix(0, ncol = 8, nrow = 3, dimnames = list(NULL, letters[1:8]))
pick[1,7] <- 1
pick[2,1] <- -1; pick[2,2] <- 1
pick[3, 3:6] <- c(0.9, -0.9, .1, -.1)
post = bl.compute.posterior(mu, varCov, pick, c(0.0525, 0.0025, 0.02), tau = 0.025)
library(BLCOP)
confidences <- 1 / c(0.000709, 0.000141, 0.000866)
myViews <- BLViews(pick, c(0.0525, 0.0025, 0.02), confidences, letters[1:8])
myPosterior <- posteriorEst(myViews, tau = 0.025, mu, varCov )
myPosterior
myPosterior@posteriorMean - post$expected.return
myPosterior@posteriorCovar - post$cov
}