Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/demo/regime_switching.R
1433 views
1
#' ---
2
#' title: "Regime Switching Demo"
3
#' author: Ross Bennett
4
#' date: "7/17/2014"
5
#' ---
6
7
#' Load package and data.
8
library(PortfolioAnalytics)
9
data(edhec)
10
R <- edhec[,1:6]
11
colnames(R) <- c("CA", "CTAG", "DS", "EM", "EMN", "ED")
12
funds <- colnames(R)
13
14
#' Create an xts object of regimes.
15
#' Here I just randomly samples values to create regime 1 or regime 2. In
16
#' practice, this could based on volatility or other regime switching models
17
set.seed(123)
18
regime <- xts(sample(1:2, nrow(R), replace=TRUE, prob=c(0.3, 0.7)), index(R))
19
20
#' Construct portfolio for regime 1.
21
port1 <- portfolio.spec(funds)
22
port1 <- add.constraint(port1, "weight_sum", min_sum=0.99, max_sum=1.01)
23
port1 <- add.constraint(port1, "box", min=0.1, max=0.5)
24
port1 <- add.objective(port1, type="risk", name="ES", arguments=list(p=0.9))
25
port1 <- add.objective(port1, type="risk_budget", name="ES",
26
arguments=list(p=0.9), max_prisk=0.5)
27
28
#' Construct portfolio for regime 2.
29
port2 <- portfolio.spec(funds)
30
port2 <- add.constraint(port2, "weight_sum", min_sum=0.99, max_sum=1.01)
31
port2 <- add.constraint(port2, "box", min=0, max=0.6)
32
port2 <- add.objective(port2, type="risk", name="StdDev")
33
port2 <- add.objective(port2, type="risk_budget", name="StdDev", max_prisk=0.5)
34
35
#' Combine the portfolios.
36
portfolios <- combine.portfolios(list(port1, port2))
37
38
#' Now we construct the regime model and corresponding portfolios to use for
39
#' each regime.
40
regime.port <- regime.portfolios(regime, portfolios)
41
regime.port
42
43
#' This optimization should result in out portfolio for regime 2.
44
opt1 <- optimize.portfolio(R, regime.port,
45
optimize_method="random",
46
search_size=2000,
47
trace=TRUE)
48
opt1
49
opt1$regime
50
51
#' This optimization should result in out portfolio for regime 1.
52
opt2 <- optimize.portfolio(R[1:(nrow(R)-1)], regime.port,
53
optimize_method="DEoptim",
54
search_size=2000,
55
trace=TRUE)
56
opt2
57
opt2$regime
58
59
#' Run optimization with rebalancing using our regime switching portfolio.
60
opt.rebal <- optimize.portfolio.rebalancing(R, regime.port,
61
optimize_method="random",
62
rebalance_on="quarters",
63
training_period=130,
64
search_size=2000,
65
trace=TRUE)
66
67
#' The print and summary methods work the same as they do for optimizations
68
#' without regime switching.
69
opt.rebal
70
summary(opt.rebal)
71
72
#' We can extract which regime portfolio we optimized with at each rebalance date.
73
lapply(opt.rebal$opt_rebalancing, function(x) x$regime)
74
75
#' Extract the optimal weights at each rebalance date.
76
wt <- extractWeights(opt.rebal)
77
wt
78
79
#' Extract the objective measures*.
80
obj <- extractObjectiveMeasures(opt.rebal)
81
str(obj)
82
obj
83
84
# Extract the stats*.
85
xt <- extractStats(opt.rebal)
86
str(xt)
87
88
#' *
89
#' Note that this returns a list of N elements for N regimes. We may have
90
#' different objectives and/or a different number of objectives which makes
91
#' returning a single xts object difficult/
92
93
#' Extract the optimal weights at each rebalance date.
94
chart.Weights(opt.rebal, colorset=bluemono)
95
96
#' Chart the risk contribution for regime 1
97
chart.RiskBudget(opt.rebal, match.col="ES", risk.type="percentage",
98
regime=1, colorset=bluemono)
99
100
#' Chart the risk contribution for regime 2
101
chart.RiskBudget(opt.rebal, match.col="StdDev", risk.type="percentage",
102
regime=2, colorset=bluemono)
103
104
105