-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathtesting_minmax_sd.R
72 lines (53 loc) · 2.58 KB
/
testing_minmax_sd.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Script to test specifying a min and max instead of a target for an objective
library(PortfolioAnalytics)
data(edhec)
ret <- edhec[, 1:10]
constraints=constraint(assets = colnames(ret),
min = 0,
max = 1,
min_sum=0.99,
max_sum=1.01,
weight_seq = generatesequence())
# add objective to maximize return
constraints <- add.objective(constraints, type="return", name="mean", multiplier=-1, enabled=TRUE)
# add "tmp_minmax" type objective with "sd" as the name of the function. # This is matched to StdDev
# Penalize if sd is outside of the range of min and max
constraints <- add.objective(constraints, type="tmp_minmax", name="sd",
min=0.02, max=0.04, multiplier=0, enabled=TRUE)
# Maximize return subject to a range portfolio volatility values
##### test 1: just calculate portfolio sd #####
set.seed(123)
opt_out_rp1 <- optimize.portfolio(R=ret, constraints, optimize_method="random", search_size=2000, trace=FALSE)
# sd = 0.03541473 when when multiplier = 0
# This calculates the portfolio sd, but does not use it in the objective function
opt_out_rp1$objective_measures$sd
##### test 2: within range #####
# Change the multiplier to 1 and set the min and max outside of the calculated
# portfolio sd.
constraints$objectives[[2]]$multiplier=1
constraints$objectives[[2]]$min=0.02
constraints$objectives[[2]]$max=0.04
set.seed(123)
opt_out_rp2 <- optimize.portfolio(R=ret, constraints, optimize_method="random", search_size=2000, trace=TRUE)
# portfolio sd should be unchanged
opt_out_rp2$objective_measures$sd
##### test 3: lower max #####
# Lower the max below 0.03541473
constraints$objectives[[2]]$min=0.02
constraints$objectives[[2]]$max=0.031
set.seed(123)
opt_out_rp3 <- optimize.portfolio(R=ret, constraints, optimize_method="random", search_size=2000, trace=TRUE)
# Portfolio sd should now be less
opt_out_rp3$objective_measures$sd
##### test 4: raise min #####
# Raise the min above 0.03541473
constraints$objectives[[2]]$min=0.037
constraints$objectives[[2]]$max=0.04
set.seed(123)
opt_out_rp4 <- optimize.portfolio(R=ret, constraints, optimize_method="random", search_size=2000, trace=TRUE)
# Portfolio sd should now be greater
opt_out_rp4$objective_measures$sd
# constraints <- add.objective(constraints, type="risk", name="sd", target=0.012, multiplier=1, enabled=TRUE)
# constraints$objectives[[2]]$enabled=FALSE
# opt_out_rp <- optimize.portfolio(R=ret, constraints, optimize_method="random", search_size=2000, trace=FALSE)
# opt_out_rp$objective_measures