Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/demo/demo_min_expected_shortfall.R
1433 views
1
#' ---
2
#' title: "Minimum Expected Shortfall 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 expected shortfall (ES). The objective can also be
9
#' specified as "CVaR" or "ETL".
10
11
#' Load the package and data
12
library(PortfolioAnalytics)
13
data(edhec)
14
R <- edhec[, 1:10]
15
funds <- colnames(R)
16
17
#' Construct initial portfolio with basic constraints
18
init.portf <- portfolio.spec(assets=funds)
19
init.portf <- add.constraint(portfolio=init.portf, type="full_investment")
20
init.portf <- add.constraint(portfolio=init.portf, type="long_only")
21
#' Add objective to minimize expected shortfall with a confidence level of 0.9.
22
init.portf <- add.objective(portfolio=init.portf, type="risk", name="ES",
23
arguments=list(p=0.9))
24
print(init.portf)
25
26
#' Minimizing expected shortfall can be formulated as a linear programming
27
#' problem and solved very quickly using optimize_method="ROI". The linear
28
#' programming problem is formulated to minimize sample ES.
29
minES.lo.ROI <- optimize.portfolio(R=R, portfolio=init.portf,
30
optimize_method="ROI",
31
trace=TRUE)
32
print(minES.lo.ROI)
33
34
plot(minES.lo.ROI, risk.col="ES", return.col="mean",
35
main="Long Only Minimize Expected Shortfall")
36
37
#' It is more practical to impose box constraints on the weights of assets.
38
#' Update the second constraint element with box constraints.
39
init.portf <- add.constraint(portfolio=init.portf, type="box",
40
min=0.05, max=0.3, indexnum=2)
41
42
minES.box.ROI <- optimize.portfolio(R=R, portfolio=init.portf,
43
optimize_method="ROI",
44
trace=TRUE)
45
print(minES.box.ROI)
46
47
chart.Weights(minES.box.ROI, main="Minimize ES with Box Constraints")
48
49
#' Although the minimum ES objective can be solved quickly and accurately
50
#' with optimize_method="ROI", it is also possible to solve this optimization
51
#' problem using other solvers such as random portfolios or DEoptim. These
52
#' solvers have the added flexibility of using different methods to calculate
53
#' ES (e.g. gaussian, modified, or historical). The default is to calculate
54
#' modified ES.
55
56
#' For random portfolios and DEoptim, the leverage constraints should be
57
#' relaxed slightly.
58
init.portf$constraints[[1]]$min_sum=0.99
59
init.portf$constraints[[1]]$max_sum=1.01
60
61
#' Add mean as an objective with multiplier=0. The multiplier=0 argument means
62
#' that it will not be used in the objective function, but will be calculated
63
# 'for each portfolio so that we can plot the optimal portfolio in
64
#' mean-ES space.
65
init.portf <- add.objective(portfolio=init.portf, type="return",
66
name="mean", multiplier=0)
67
68
#' First run the optimization with a wider bound on the box constraints that
69
#' also allows shorting. Then use more restrictive box constraints. This is
70
#' useful to visualize impact of the constraints on the feasible space.
71
72
#' Create a new portfolio called 'port1' by using init.portf and modify the
73
#' box constraints.
74
port1 <- add.constraint(portfolio=init.portf, type="box",
75
min=-0.3, max=0.8, indexnum=2)
76
77
minES.box1.RP <- optimize.portfolio(R=R, portfolio=port1,
78
optimize_method="random",
79
search_size=2000,
80
trace=TRUE)
81
print(minES.box1.RP)
82
plot(minES.box1.RP, risk.col="ES", return.col="mean")
83
84
#' Create a new portfolio called 'port2' by using init.portf and modify the
85
#' box constraints
86
port2 <- add.constraint(portfolio=init.portf, type="box",
87
min=0.05, max=0.3, indexnum=2)
88
89
# Use random portfolios to run the optimization.
90
minES.box2.RP <- optimize.portfolio(R=R, portfolio=port2,
91
optimize_method="random",
92
search_size=2000,
93
trace=TRUE)
94
print(minES.box2.RP)
95
plot(minES.box2.RP, risk.col="ES", return.col="mean")
96
97
# Use DEoptim to run the optimization.
98
minES.box.DE <- optimize.portfolio(R=R, portfolio=init.portf,
99
optimize_method="DEoptim",
100
search_size=2000,
101
trace=TRUE)
102
print(minES.box.DE)
103
plot(minES.box.DE, risk.col="ES", return.col="mean")
104
105