Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/demo/relative_ranking.R
1433 views
1
#' ---
2
#' title: "Regime Switching Demo"
3
#' author: Ross Bennett
4
#' date: "7/17/2014"
5
#' ---
6
7
#' This script demonstrates expressing views on the relative ranking of
8
#' expected returns based on two methods:
9
#' 1. R. Almgren and N. Chriss, "Portfolios from Sorts"
10
#' 2. A. Meucci, "Fully Flexible Views: Theory and Practice"
11
12
#' Load package and data.
13
library(PortfolioAnalytics)
14
data(edhec)
15
R <- edhec[,1:4]
16
funds <- colnames(R)
17
18
#' Construct initial portfolio with basic constraints.
19
init.portf <- portfolio.spec(assets=funds)
20
init.portf <- add.constraint(portfolio=init.portf, type="weight_sum",
21
min_sum=0.99, max_sum=1.01)
22
init.portf <- add.constraint(portfolio=init.portf, type="box",
23
min=0.05, max=0.5)
24
init.portf <- add.objective(portfolio=init.portf, type="risk", name="StdDev")
25
init.portf <- add.objective(portfolio=init.portf, type="return", name="mean")
26
27
#' Here we express views on the relative rank of the asset returns.
28
#' E{ R[,2] < R[,3] < R[,1] < R[,4] }
29
asset.rank <- c(2, 3, 1, 4)
30
31
#' Use Meucci Fully Flexible Views framework to express views on the relative
32
#' order of asset returns.
33
#' Define prior probabilities.
34
p <- rep(1 / nrow(R), nrow(R))
35
36
#' Express view on the relative ordering of asset returns
37
m.moments <- meucci.ranking(R, p, asset.rank)
38
39
#' Express views using the method described in Almgren and Chriss,
40
#' "Portfolios from Sorts".
41
ac.moments <- list()
42
ac.moments$mu <- ac.ranking(R, asset.rank)
43
# Sample estimate for second moment
44
ac.moments$sigma <- cov(R)
45
46
#' Generate random portfolios for use in the optimization.
47
rp <- random_portfolios(init.portf, 5000)
48
49
#' Run the optimization using first and second moments estimated from
50
#' Meucci's Fully Flexible Views framework using the moments we calculated
51
#' from our view.
52
opt.meucci <- optimize.portfolio(R,
53
init.portf,
54
optimize_method="random",
55
rp=rp,
56
trace=TRUE,
57
momentargs=m.moments)
58
59
#' Run the optimization using first moment estimated based on Almgren and Chriss,
60
#' "Portfolios from Sorts". The second moment uses the sample estimate.
61
opt.ac <- optimize.portfolio(R,
62
init.portf,
63
optimize_method="random",
64
rp=rp,
65
trace=TRUE,
66
momentargs=ac.moments)
67
68
#' For comparison, run the optimization using sample estimates for first and
69
#' second moments.
70
opt.sample <- optimize.portfolio(R,
71
init.portf,
72
optimize_method="random",
73
rp=rp,
74
trace=TRUE)
75
76
#' Here we plot the optimal weights of each optimization.
77
chart.Weights(combine.optimizations(list(meucci=opt.meucci,
78
ac=opt.ac,
79
sample=opt.sample)),
80
ylim=c(0,1), plot.type="barplot")
81
82
#' Here we define a custom moment function to estimate moments based on
83
#' relative ranking views.
84
#' Asset are ranked according to a momentum or reversal view based on the
85
#' previous n periods.
86
moment.ranking <- function(R, n=1, momentum=TRUE, method=c("meucci", "ac")){
87
# Moment function to estimate moments based on relative ranking of
88
# expected returns.
89
90
method <- match.arg(method)
91
92
# Use the most recent n periods of returns
93
tmpR <- apply(tail(R, n), 2, function(x) prod(1 + x) - 1)
94
95
if(momentum){
96
# Assume that the assets with the highest return will continue to outperform
97
asset.rank <- order(tmpR)
98
} else {
99
# Assume that the assets with the highest return will reverse
100
asset.rank <- rev(order(tmpR))
101
}
102
switch(method,
103
meucci = {
104
# Meucci Fully Flexible Views framework
105
# Prior probabilities
106
p <- rep(1 / nrow(R), nrow(R))
107
108
# Relative ordering view
109
moments <- meucci.ranking(R, p, asset.rank)
110
},
111
ac = {
112
# Almgren and Chriss Portfolios from Sorts
113
moments <- list()
114
moments$mu <- ac.ranking(R, asset.rank)
115
# Sample estimate for second moment
116
moments$sigma <- cov(R)
117
}
118
)
119
return(moments)
120
}
121
122
#' Here we run out of sample backtests to test the out of sample performance
123
#' using using the different frameworks to express our views on relative
124
#' asset return ranking.
125
opt.bt.meucci <- optimize.portfolio.rebalancing(R, init.portf,
126
optimize_method="random",
127
rebalance_on="quarters",
128
training_period=100,
129
rp=rp,
130
momentFUN="moment.ranking",
131
n=2,
132
momentum=TRUE,
133
method="meucci")
134
135
opt.bt.ac <- optimize.portfolio.rebalancing(R, init.portf,
136
optimize_method="random",
137
rebalance_on="quarters",
138
training_period=100,
139
rp=rp,
140
momentFUN="moment.ranking",
141
n=2,
142
momentum=TRUE,
143
method="ac")
144
145
opt.bt.sample <- optimize.portfolio.rebalancing(R, init.portf,
146
optimize_method="random",
147
rebalance_on="quarters",
148
training_period=100,
149
rp=rp)
150
151
#' Compute returns and chart performance summary.
152
ret.meucci <- Return.portfolio(R, extractWeights(opt.bt.meucci))
153
ret.ac <- Return.portfolio(R, extractWeights(opt.bt.ac))
154
ret.sample <- Return.portfolio(R, extractWeights(opt.bt.sample))
155
ret <- cbind(ret.meucci, ret.ac, ret.sample)
156
colnames(ret) <- c("meucci.rank", "ac.rank", "sample")
157
charts.PerformanceSummary(ret, main="Ranking Views Performance")
158
159
160