Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/demo/demo_min_StdDev.R
1433 views
1
#' ---
2
#' title: "Minimum Standard Deviation Demo"
3
#' author: Ross Bennett
4
#' date: "7/17/2014"
5
#' ---
6
7
#' This script demonstrates how to solve a constrained portfolio optimization
8
#' problem to minimize standard deviation.
9
10
#' Load the package and data
11
library(PortfolioAnalytics)
12
data(edhec)
13
R <- edhec[, 1:10]
14
funds <- colnames(R)
15
16
#' Construct initial portfolio with basic constraints.
17
init.portf <- portfolio.spec(assets=funds)
18
init.portf <- add.constraint(portfolio=init.portf, type="full_investment")
19
init.portf <- add.constraint(portfolio=init.portf, type="long_only")
20
init.portf <- add.objective(portfolio=init.portf, type="risk", name="StdDev")
21
print(init.portf)
22
23
#' Minimizing standard deviation can be formulated as a quadratic programming
24
#' problem and solved very quickly using optimize_method="ROI". Although "StdDev"
25
#' was specified as an objective, the quadratic programming problem uses the
26
#' variance-covariance matrix in the objective function.
27
minStdDev.lo.ROI <- optimize.portfolio(R=R, portfolio=init.portf,
28
optimize_method="ROI",
29
trace=TRUE)
30
print(minStdDev.lo.ROI)
31
32
plot(minStdDev.lo.ROI, risk.col="StdDev", main="Long Only Minimize Portfolio StdDev")
33
34
#' It is more practical to impose box constraints on the weights of assets.
35
#' Update the second constraint element with box constraints.
36
init.portf <- add.constraint(portfolio=init.portf, type="box",
37
min=0.05, max=0.3, indexnum=2)
38
39
minStdDev.box.ROI <- optimize.portfolio(R=R, portfolio=init.portf,
40
optimize_method="ROI",
41
trace=TRUE)
42
print(minStdDev.box.ROI)
43
44
chart.Weights(minStdDev.box.ROI, main="Minimize StdDev with Box Constraints")
45
46
#' Although the maximum return objective can be solved quickly and accurately
47
#' with optimize_method="ROI", it is also possible to solve this optimization
48
#' problem using other solvers such as random portfolios or DEoptim.
49
50
#' For random portfolios, the leverage constraints should be relaxed slightly.
51
init.portf$constraints[[1]]$min_sum=0.99
52
init.portf$constraints[[1]]$max_sum=1.01
53
54
#' Add mean as an object with multiplier=0. The multiplier=0 argument means
55
#' that it will not be used in the objective function, but will be calculated
56
#' for each portfolio so that we can plot the optimal portfolio in
57
#' mean-StdDev space.
58
init.portf <- add.objective(portfolio=init.portf, type="return",
59
name="mean", multiplier=0)
60
61
#' First run the optimization with a wider bound on the box constraints that
62
#' also allows shorting. Then use more restrictive box constraints. This is
63
#' useful to visualize impact of the constraints on the feasible space
64
65
#' Create a new portfolio called 'port1' by using init.portf and modify the
66
#' box constraints.
67
port1 <- add.constraint(portfolio=init.portf, type="box",
68
min=-0.3, max=0.8, indexnum=2)
69
70
minStdDev.box1.RP <- optimize.portfolio(R=R, portfolio=port1,
71
optimize_method="random",
72
search_size=2000,
73
trace=TRUE)
74
print(minStdDev.box1.RP)
75
plot(minStdDev.box1.RP, risk.col="StdDev")
76
77
#' Create a new portfolio called 'port2' by using init.portf and modify the
78
#' box constraints.
79
port2 <- add.constraint(portfolio=init.portf, type="box",
80
min=0.05, max=0.3, indexnum=2)
81
82
#' Run the optimization with random portfolios.
83
minStdDev.box2.RP <- optimize.portfolio(R=R, portfolio=port2,
84
optimize_method="random",
85
search_size=2000,
86
trace=TRUE)
87
print(minStdDev.box2.RP)
88
plot(minStdDev.box2.RP, risk.col="StdDev")
89
90
#' Run the optimization with DEoptim.
91
minStdDev.box.DE <- optimize.portfolio(R=R, portfolio=init.portf,
92
optimize_method="DEoptim",
93
search_size=2000,
94
trace=TRUE)
95
print(minStdDev.box.DE)
96
plot(minStdDev.box.DE, risk.col="StdDev", return.col="mean")
97
98