Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/demo/demo_factor_exposure.R
1433 views
1
#' ---
2
#' title: "Factor Exposure Demo"
3
#' author: Ross Bennett
4
#' date: "7/17/2014"
5
#' ---
6
7
#' This script demonstrates how to solve a portfolio optimization problem with
8
#' factor exposure constraints.
9
10
#' Load the required packages
11
library(PortfolioAnalytics)
12
library(ROI)
13
require(ROI.plugin.quadprog)
14
require(ROI.plugin.glpk)
15
library(Rglpk)
16
library(DEoptim)
17
18
#' Load the data
19
data(edhec)
20
ret <- edhec[, 1:4]
21
22
#' Create portfolio object
23
pspec <- portfolio.spec(assets=colnames(ret))
24
25
#' Here we define individual constraint objects.
26
#' Leverage constraint.
27
lev_constr <- weight_sum_constraint(min_sum=1, max_sum=1)
28
29
#' Box constraint
30
lo_constr <- box_constraint(assets=pspec$assets, min=c(0.01, 0.02, 0.03, 0.04), max=0.65)
31
32
#' Group constraint'
33
grp_constr <- group_constraint(assets=pspec$assets, groups=list(1:2, 3, 4), group_min=0.1, group_max=0.4)
34
35
#' Position limit constraint
36
pl_constr <- position_limit_constraint(assets=pspec$assets, max_pos=4)
37
38
#' Make up a B matrix for an industry factor model.
39
#' dummyA, dummyB, and dummyC could be industries, sectors, etc.
40
B <- cbind(c(1, 1, 0, 0),
41
c(0, 0, 1, 0),
42
c(0, 0, 0, 1))
43
rownames(B) <- colnames(ret)
44
colnames(B) <- c("dummyA", "dummyB", "dummyC")
45
lower <- c(0.1, 0.1, 0.1)
46
upper <- c(0.4, 0.4, 0.4)
47
48
#' Industry exposure constraint.
49
#' The exposure constraint and group constraint are equivalent to test that
50
#' they result in the same solution.
51
exp_constr <- factor_exposure_constraint(assets=pspec$assets, B=B, lower=lower, upper=upper)
52
53
#' Here we define objectives.
54
#'
55
#' Objective to minimize variance.
56
var_obj <- portfolio_risk_objective(name="var")
57
58
#' Objective to maximize return.
59
ret_obj <- return_objective(name="mean")
60
61
#' Objective to minimize ETL.
62
etl_obj <- portfolio_risk_objective(name="ETL")
63
64
#' Run optimization on minimum variance portfolio with leverage, long only,
65
#' and group constraints.
66
opta <- optimize.portfolio(R=ret, portfolio=pspec,
67
constraints=list(lev_constr, lo_constr, grp_constr),
68
objectives=list(var_obj),
69
optimize_method="ROI")
70
opta
71
72
#' Run optimization on minimum variance portfolio with leverage, long only,
73
#' and factor exposure constraints.
74
optb <- optimize.portfolio(R=ret, portfolio=pspec,
75
constraints=list(lev_constr, lo_constr, exp_constr),
76
objectives=list(var_obj),
77
optimize_method="ROI")
78
optb
79
80
#' Note that the portfolio with the group constraint and exposure constraint
81
#' should result in same solution.
82
all.equal(opta$weights, optb$weights)
83
84
#' Run optimization on maximum return portfolio with leverage, long only,
85
#' and group constraints.
86
optc <- optimize.portfolio(R=ret, portfolio=pspec,
87
constraints=list(lev_constr, lo_constr, grp_constr),
88
objectives=list(ret_obj),
89
optimize_method="ROI")
90
optc
91
92
#' Run optimization on maximum return portfolio with leverage, long only,
93
#' and factor exposure constraints.
94
optd <- optimize.portfolio(R=ret, portfolio=pspec,
95
constraints=list(lev_constr, lo_constr, exp_constr),
96
objectives=list(ret_obj),
97
optimize_method="ROI")
98
optd
99
100
#' Note that the portfolio with the group constraint and exposure constraint
101
#' should result in same solution.
102
all.equal(optc$weights, optd$weights)
103
104
#' Run optimization on minimum expected tail loss portfolio with leverage,
105
#' long only, and group constraints.
106
opte <- optimize.portfolio(R=ret, portfolio=pspec,
107
constraints=list(lev_constr, lo_constr, grp_constr),
108
objectives=list(etl_obj),
109
optimize_method="ROI")
110
opte
111
112
#' Run optimization on minimum expected tail loss portfolio with leverage,
113
#' long only, and factor exposure constraints.
114
optf <- optimize.portfolio(R=ret, portfolio=pspec,
115
constraints=list(lev_constr, lo_constr, exp_constr),
116
objectives=list(etl_obj),
117
optimize_method="ROI")
118
optf
119
120
#' Note that the portfolio with the group constraint and exposure constraint
121
#' should result in same solution.
122
all.equal(opte$weights, optf$weights)
123
124
#' Run optimization on maximum return portfolio with leverage, long only,
125
#' and group constraints using DEoptim as the optimization engine.
126
set.seed(123)
127
optde1 <- optimize.portfolio(R=ret, portfolio=pspec,
128
constraints=list(lev_constr, lo_constr, grp_constr),
129
objectives=list(ret_obj),
130
optimize_method="DEoptim",
131
search_size=2000,
132
trace=TRUE)
133
optde1
134
135
#' Run optimization on maximum return portfolio with leverage, long only,
136
#' and factor exposure constraints using DEoptim as the optimization engine.
137
set.seed(123)
138
optde2 <- optimize.portfolio(R=ret, portfolio=pspec,
139
constraints=list(lev_constr, lo_constr, exp_constr),
140
objectives=list(ret_obj),
141
optimize_method="DEoptim",
142
search_size=2000,
143
trace=TRUE)
144
optde2
145
146
#' Note that the portfolio with the group constraint and exposure constraint
147
#' should result in same solution.
148
all.equal(optde1$weights, optde2$weights)
149
150
#' Run optimization on maximum return portfolio with leverage, long only,
151
#' and group constraints using random portfolios as the optimization
152
#' engine.
153
optrp1 <- optimize.portfolio(R=ret, portfolio=pspec,
154
constraints=list(lev_constr, lo_constr, grp_constr),
155
objectives=list(ret_obj),
156
optimize_method="random",
157
search_size=2000,
158
trace=TRUE)
159
optrp1
160
161
#' Run optimization on maximum return portfolio with leverage, long only,
162
#' and factor exposure constraints using random portfolios as the optimization
163
#' engine.
164
optrp2 <- optimize.portfolio(R=ret, portfolio=pspec,
165
constraints=list(lev_constr, lo_constr, exp_constr),
166
objectives=list(ret_obj),
167
optimize_method="random",
168
search_size=2000,
169
trace=TRUE)
170
optrp2
171
172
#' Note that the portfolio with the group constraint and exposure constraint
173
#' should result in same solution.
174
all.equal(optrp1$weights, optrp2$weights)
175
176