Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/demo/demo_random_portfolios.R
1433 views
1
# Demonstrate examples from script.workshop2012.R using the v2 specification
2
3
# The following optimization problems will be run
4
# mean-StdDev
5
# - maximize mean-to-volatility (i.e. reward-to-risk)
6
# - BUOY 1
7
# minmETL
8
# - minimize modified Expected Tail Loss
9
# - BUOY 4
10
# eqmETL
11
# - equal risk modified Expected Tail Loss
12
# - BUOY 6
13
14
# Implement BUOY 1, BUOY 4, and BUOY 6 from script.workshop2012.R
15
16
# Note: The script.workshop2012.R examples use pamean and pasd, I will simply
17
# use mean and StdDev.
18
19
# Include optimizer and multi-core packages
20
library(PortfolioAnalytics)
21
require(quantmod)
22
require(foreach)
23
24
# The multicore package, and therefore registerDoMC, should not be used in a
25
# GUI environment, because multiple processes then share the same GUI. Only use
26
# when running from the command line
27
# require(doMC)
28
# registerDoMC(3)
29
30
data(edhec)
31
32
# Drop some indexes and reorder
33
edhec.R = edhec[,c("Convertible Arbitrage", "Equity Market Neutral","Fixed Income Arbitrage", "Event Driven", "CTA Global", "Global Macro", "Long/Short Equity")]
34
35
# Define pamean function
36
# pamean <- function(n=12, R, weights, geometric=TRUE){
37
# as.vector(sum(Return.annualized(last(R,n), geometric=geometric)*weights))
38
# }
39
40
# Define pasd function
41
# pasd <- function(R, weights){
42
# as.numeric(StdDev(R=R, weights=weights)*sqrt(12)) # hardcoded for monthly data
43
# }
44
45
# Set some parameters
46
rebalance_period = 'quarters' # uses endpoints identifiers from xts
47
clean = "none" #"boudt"
48
permutations = 4000
49
50
##### v2: Initial Portfolio Object #####
51
## Set up an initial portfolio object with constraints and objectives using
52
## v2 specification
53
54
# Create initial portfolio object used to initialize ALL the bouy portfolios
55
init.portf <- portfolio.spec(assets=colnames(edhec.R),
56
weight_seq=generatesequence(by=0.005))
57
# Add leverage constraint
58
init.portf <- add.constraint(portfolio=init.portf,
59
type="leverage",
60
min_sum=0.99,
61
max_sum=1.01)
62
# Add box constraint
63
init.portf <- add.constraint(portfolio=init.portf,
64
type="box",
65
min=0.05,
66
max=0.3)
67
68
#Add measure 1, annualized return
69
init.portf <- add.objective(portfolio=init.portf,
70
type="return", # the kind of objective this is
71
name="mean", # name of the function
72
enabled=TRUE, # enable or disable the objective
73
multiplier=0 # calculate it but don't use it in the objective
74
)
75
76
# Add measure 2, annualized standard deviation
77
init.portf <- add.objective(portfolio=init.portf,
78
type="risk", # the kind of objective this is
79
name="StdDev", # to minimize from the sample
80
enabled=TRUE, # enable or disable the objective
81
multiplier=0 # calculate it but don't use it in the objective
82
)
83
84
# Add measure 3, ES with p=(1-1/12)
85
# set confidence for ES
86
p=1-1/12 # for monthly
87
88
init.portf <- add.objective(portfolio=init.portf,
89
type="risk", # the kind of objective this is
90
name="ES", # the function to minimize
91
enabled=FALSE, # enable or disable the objective
92
multiplier=0, # calculate it but don't use it in the objective
93
arguments=list(p=p)
94
)
95
print(init.portf)
96
summary(init.portf)
97
98
##### v2: BUOY 1 #####
99
### Construct BUOY 1: Constrained Mean-StdDev Portfolio
100
MeanSD.portf <- init.portf
101
# Turn back on the return and sd objectives
102
MeanSD.portf$objectives[[1]]$multiplier = -1 # pamean
103
MeanSD.portf$objectives[[2]]$multiplier = 1 # pasd
104
print(MeanSD.portf)
105
summary(MeanSD.portf)
106
107
108
##### v2: BUOY 4 #####
109
### Construct BUOY 4: Constrained Minimum mETL Portfolio
110
MinmETL.portf <- init.portf
111
# Turn back on the mETL objective
112
MinmETL.portf$objectives[[3]]$multiplier = 1 # mETL
113
MinmETL.portf$objectives[[3]]$enabled = TRUE # mETL
114
print(MinmETL.portf)
115
summary(MinmETL.portf)
116
117
##### v2: BUOY 6 #####
118
### Construct BUOY 6: Constrained Equal mETL Contribution Portfolio
119
EqmETL.portf <- add.objective(init.portf,
120
type="risk_budget",
121
name="ES",
122
enabled=TRUE,
123
min_concentration=TRUE,
124
arguments = list(p=(1-1/12), clean=clean)
125
)
126
EqmETL.portf$objectives[[3]]$multiplier = 1 # min mETL
127
EqmETL.portf$objectives[[3]]$enabled = TRUE # min mETL
128
print(EqmETL.portf)
129
summary(EqmETL.portf)
130
131
### Choose our 'R' variable
132
R=edhec.R # for monthlies
133
134
# Generate a single set of random portfolios to evaluate against all constraint set
135
print(paste('constructing random portfolios at',Sys.time()))
136
rp = random_portfolios(portfolio=init.portf, permutations=permutations)
137
print(paste('done constructing random portfolios at',Sys.time()))
138
139
start_time<-Sys.time()
140
print(paste('Starting optimization at',Sys.time()))
141
142
##### v2: Evaluate BUOY 1 #####
143
MeanSD.RND <- optimize.portfolio(R=R,
144
portfolio=MeanSD.portf,
145
optimize_method="random",
146
trace=TRUE,
147
rp=rp)
148
print(MeanSD.RND)
149
print(MeanSD.RND$elapsed_time)
150
151
# Evaluate the objectives with RP through time
152
# MeanSD.RND.t <- optimize.portfolio.rebalancing(R=R,
153
# portfolio=MeanSD.portf,
154
# optimize_method="random",
155
# trace=TRUE,
156
# rp=rp,
157
# rebalance_on=rebalance_period,
158
# training_period=36)
159
# MeanSD.w = extractWeights.rebal(MeanSD.RND.t)
160
# MeanSD=Return.rebalancing(edhec.R, MeanSD.w)
161
# colnames(MeanSD) = "MeanSD"
162
163
print(paste('Completed meanSD optimization at',Sys.time(),'moving on to MinmETL'))
164
165
##### v2: Evaluate BUOY 4 #####
166
MinmETL.RND <- optimize.portfolio(R=R,
167
portfolio=MinmETL.portf,
168
optimize_method="random",
169
trace=TRUE,
170
rp=rp)
171
print(MinmETL.RND)
172
print(MinmETL.RND$elapsed_time)
173
174
# Evaluate the objectives with RP through time
175
# MinmETL.RND.t <- optimize.portfolio.rebalancing(R=R,
176
# portfolio=MinmETL.portf,
177
# optimize_method="random",
178
# trace=TRUE,
179
# rp=rp,
180
# rebalance_on=rebalance_period,
181
# training_period=36)
182
# MinmETL.w = extractWeights.rebal(MinmETL.RND.t)
183
# MinmETL=Return.rebalancing(edhec.R, MinmETL.w)
184
# colnames(MinmETL) = "MinmETL"
185
186
print(paste('Completed MinmETL optimization at',Sys.time(),'moving on to EqmETL'))
187
188
##### v2: Evaluate BUOY 6 #####
189
EqmETL.RND <- optimize.portfolio(R=R,
190
portfolio=EqmETL.portf,
191
optimize_method="random",
192
trace=TRUE,
193
rp=rp)
194
print(EqmETL.RND)
195
print(EqmETL.RND$elapsed_time)
196
197
# Evaluate the objectives with RP through time
198
# EqmETL.RND.t <- optimize.portfolio.rebalancing(R=R,
199
# portfolio=EqmETL.portf,
200
# optimize_method="random",
201
# trace=TRUE,
202
# rp=rp,
203
# rebalance_on=rebalance_period,
204
# training_period=36)
205
# EqmETL.w = extractWeights.rebal(EqmETL.RND.t)
206
# EqmETL=Return.rebalancing(edhec.R, EqmETL.w)
207
# colnames(EqmETL) = "EqmETL"
208
209
end_time<-Sys.time()
210
print("Optimization Complete")
211
print(end_time-start_time)
212
213