Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/demo/testing_ROI.R
1433 views
1
#' ---
2
#' title: "ROI Demo"
3
#' date: "7/17/2014"
4
#' ---
5
6
#' This script demonstrates running optimizations using ROI as the
7
#' optimization backend. Note that this script uses the v1 specification
8
#' previous to version 0.8.3.
9
10
#' Load packages
11
library(PortfolioAnalytics)
12
13
#' General Parameters for sample code
14
data(edhec)
15
funds <- names(edhec)
16
mu.port <- mean(colMeans(edhec))
17
N <- length(funds)
18
19
#' Define problem with constraints and objectives
20
gen.constr <- constraint(assets = colnames(edhec), min=-Inf, max =Inf, min_sum=1, max_sum=1, risk_aversion=1)
21
gen.constr <- add.objective(constraints=gen.constr, type="return", name="mean", enabled=FALSE, multiplier=0, target=mu.port)
22
gen.constr <- add.objective(constraints=gen.constr, type="risk", name="var", enabled=FALSE, multiplier=0, risk_aversion=10)
23
gen.constr <- add.objective(constraints=gen.constr, type="risk", name="CVaR", enabled=FALSE, multiplier=0)
24
25
26
#' Max return under box constraints, fully invested
27
max.port <- gen.constr
28
max.port$min <- rep(0.01,N)
29
max.port$max <- rep(0.30,N)
30
max.port$objectives[[1]]$enabled <- TRUE
31
max.port$objectives[[1]]$target <- NA
32
max.solution <- optimize.portfolio(R=edhec, constraints=max.port, optimize_method="ROI")
33
34
35
#' Mean-variance: Fully invested, Global Minimum Variance Portfolio
36
gmv.port <- gen.constr
37
gmv.port$objectives[[2]]$enabled <- TRUE
38
gmv.port$objectives[[2]]$risk_aversion <- 1
39
gmv.solution <- optimize.portfolio(R=edhec, constraints=gmv.port, optimize_method="ROI")
40
41
42
#' Mean-variance: Maximize quadratic utility, fully invested, target portfolio return
43
target.port <- gen.constr
44
target.port$objectives[[1]]$enabled <- TRUE
45
target.port$objectives[[2]]$enabled <- TRUE
46
target.solution <- optimize.portfolio(R=edhec, constraints=target.port, optimize_method="ROI")
47
48
49
#' Mean-variance: Maximize quadratic utility, dollar-neutral, target portfolio return
50
dollar.neu.port <- gen.constr
51
dollar.neu.port$min_sum <- 0
52
dollar.neu.port$max_sum <- 0
53
dollar.neu.port$objectives[[1]]$enabled <- TRUE
54
dollar.neu.port$objectives[[2]]$enabled <- TRUE
55
dollar.neu.solution <- optimize.portfolio(R=edhec, constraints=dollar.neu.port, optimize_method="ROI")
56
57
58
#' Minimize CVaR with target return
59
cvar.port <- gen.constr
60
cvar.port$objectives[[1]]$enabled <- TRUE
61
cvar.port$objectives[[3]]$enabled <- TRUE
62
cvar.solution <- optimize.portfolio(R=edhec, constraints=cvar.port, optimize_method="ROI")
63
64
65
#' Mean-variance: Fully invested, Global Minimum Variance Portfolio, Groups Constraints
66
groups.port <- gen.constr
67
groups <- list(1:3, 4:6, 7:9, 10:13)
68
groups.port$groups <- groups
69
groups.port$cLO <- rep(0.15,length(groups))
70
groups.port$cUP <- rep(0.30,length(groups))
71
groups.port$objectives[[2]]$enabled <- TRUE
72
groups.port$objectives[[2]]$risk_aversion <- 1
73
groups.solution <- optimize.portfolio(R=edhec, constraints=groups.port, optimize_method="ROI")
74
75
76
#' Minimize CVaR with target return and group constraints
77
group.cvar.port <- gen.constr
78
groups <- list(1:3, 4:6, 7:9, 10:13)
79
group.cvar.port$groups <- groups
80
group.cvar.port$cLO <- rep(0.15,length(groups))
81
group.cvar.port$cUP <- rep(0.30,length(groups))
82
group.cvar.port$objectives[[1]]$enabled <- TRUE
83
group.cvar.port$objectives[[3]]$enabled <- TRUE
84
group.cvar.solution <- optimize.portfolio(R=edhec, constraints=group.cvar.port, optimize_method="ROI", maxSTARR=FALSE)
85
86
87