Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/demo/demo_max_STARR.R
1433 views
1
#' ---
2
#' title: "Maximizing Modified Sharpe Ratio 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 maximize modified Sharpe Ratio using ES as the risk measure.
9
10
#' Load the package and data
11
library(PortfolioAnalytics)
12
data(edhec)
13
R <- edhec[, 1:8]
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="return", name="mean")
21
init.portf <- add.objective(portfolio=init.portf, type="risk", name="ES",
22
arguments=list(p=0.925))
23
init.portf
24
25
#' Maximizing STARR Ratio can be formulated as a linear programming
26
#' problem and solved very quickly using optimize_method="ROI".
27
28
#' The default action if "mean" and "ES" are specified as objectives with
29
#' optimize_method="ROI" is to maximize STARR. If we want to use
30
#' both mean and ES in the objective function, but only minimize ES, we need to
31
#' pass in maxSTARR=FALSE to optimize.portfolio.
32
33
maxSTARR.lo.ROI <- optimize.portfolio(R=R, portfolio=init.portf,
34
optimize_method="ROI",
35
trace=TRUE)
36
maxSTARR.lo.ROI
37
38
#' Although the maximum STARR Ratio objective can be solved quickly and accurately
39
#' with optimize_method="ROI", it is also possible to solve this optimization
40
#' problem using other solvers such as random portfolios or DEoptim. These
41
#' solvers have the added flexibility of using different methods to calculate
42
#' the Sharpe Ratio (e.g. we could specify annualized measures of risk and
43
#' return or use modified, guassian, or historical ES).
44
45
#' For random portfolios and DEoptim, the leverage constraints should be
46
#' relaxed slightly.
47
init.portf$constraints[[1]]$min_sum=0.99
48
init.portf$constraints[[1]]$max_sum=1.01
49
50
# Use random portfolios to run the optimization.
51
maxSTARR.lo.RP <- optimize.portfolio(R=R, portfolio=init.portf,
52
optimize_method="random",
53
search_size=2000,
54
trace=TRUE)
55
maxSTARR.lo.RP
56
57
chart.RiskReward(maxSTARR.lo.RP, risk.col="ES", return.col="mean")
58
59
# Use DEoptim to run the optimization.
60
maxSTARR.lo.DE <- optimize.portfolio(R=R, portfolio=init.portf,
61
optimize_method="DEoptim",
62
search_size=2000,
63
trace=TRUE)
64
maxSTARR.lo.DE
65
chart.RiskReward(maxSTARR.lo.DE, risk.col="ES", return.col="mean",
66
xlim=c(0.01, 0.08), ylim=c(0.004,0.008))
67
68