Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/demo/multi_layer_optimization.R
1433 views
1
#' ---
2
#' title: "Multi Layer Optimization Demo"
3
#' author: Ross Bennett
4
#' date: "7/17/2014"
5
#' ---
6
7
#' Demonstrate multi layer portfolio optimization
8
#' The top level (i.e. layer) optimization problem is to minimize modified ES
9
#' with equal component contribution to modified ES of the two portfolios in
10
#' the lower layer.
11
#'
12
#' The sub portfolios consist of different assets and different objectives
13
#' relative to each other. The out of sample returns for each sub portfolio
14
#' are calculated based on their respective constraints, objectives, and
15
#' optimization parameters. The out of sample returns are then used as the
16
#' returns input for the top level optimization.
17
18
#' Load package and data.
19
library(PortfolioAnalytics)
20
data(edhec)
21
R <- edhec[, 1:10]
22
funds <- colnames(R)
23
24
#' The first sub-portfolio, portf1, will contain assets 1:5 of edhec
25
#' with an objective to minimize standard deviation.
26
portf1 <- portfolio.spec(assets=funds[1:5])
27
portf1 <- add.constraint(portfolio=portf1, type="weight_sum",
28
min_sum=0.99, max_sum=1.01)
29
portf1 <- add.constraint(portfolio=portf1, type="long_only")
30
portf1 <- add.objective(portfolio=portf1, type="risk", name="StdDev")
31
32
#' The second sub-portfolio, portf2, will contain assets 6:10 of edhec
33
#' with an objective to minimize expected shortfall.
34
portf2 <- portfolio.spec(assets=funds[6:10])
35
# portf2 <- portfolio.spec(assets=5)
36
portf2 <- add.constraint(portfolio=portf2, type="weight_sum",
37
min_sum=0.99, max_sum=1.01)
38
portf2 <- add.constraint(portfolio=portf2, type="long_only")
39
portf2 <- add.objective(portfolio=portf2, type="risk", name="ES",
40
arguments=list(p=0.9))
41
42
#' portf1 and portf2 have the same constraints so they can used the same
43
#' set of random portfolios.
44
set.seed(123)
45
rp <- random_portfolios(portf2, 2000)
46
47
48
#' The 'top level' portfolio has objectives for equal contribution to risk
49
#' where modified ES is the risk measure.
50
portf <- portfolio.spec(assets=paste("proxy",1:2, sep="."))
51
portf <- add.constraint(portfolio=portf, type="weight_sum",
52
min_sum=0.99, max_sum=1.01)
53
portf <- add.constraint(portfolio=portf, type="long_only")
54
portf <- add.objective(portfolio=portf, type="risk", name="ES",
55
arguments=list(p=0.9))
56
portf <- add.objective(portfolio=portf, type="risk_budget", name="ES",
57
arguments=list(p=0.9), min_concentration=TRUE)
58
59
#' Specify a mult-layer portfolio.
60
mult.portf <- mult.portfolio.spec(portf)
61
62
#' Add portf1 as a sub portfolio with optimization parameters specific to
63
#' running optimize.portfolio.rebalancing with portf1.
64
mult.portf <- add.sub.portfolio(mult.portf, portf1, rp=rp,
65
optimize_method="random",
66
rebalance_on="quarters",
67
training_period=136)
68
69
#' Add portf2 as a sub portfolio with optimization parameters specific to
70
#' running optimize.portfolio.rebalancing with portf2.
71
mult.portf <- add.sub.portfolio(mult.portf, portf2, rp=rp,
72
optimize_method="random",
73
rebalance_on="months",
74
training_period=136,
75
trailing_periods=48)
76
77
#' Generate random portfolios for the top layer optimization
78
set.seed(123)
79
rp.top <- random_portfolios(portf, 1000)
80
81
#' Run the multi layer optimization
82
opt.mult <- optimize.portfolio(R, mult.portf,
83
optimize_method="random",
84
trace=TRUE, rp=rp.top)
85
opt.mult
86
87
88