Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/demo/backwards_compat.R
1433 views
1
#' ---
2
#' title: "Backwards Compatibility Demo"
3
#' author: "Ross Bennett"
4
#' date: "7/17/2014"
5
#' ---
6
7
#' This script demonstrates how to solve optimization problems using what is
8
#' referred to as the v1 specification. The v1 specification was used in
9
#' before PortfolioAnalytics version 0.8.3 to define the optimization problem
10
#' with constraints and objectives.
11
12
library(PortfolioAnalytics)
13
library(DEoptim)
14
library(ROI)
15
require(ROI.plugin.glpk)
16
17
data(edhec)
18
ret <- edhec[, 1:4]
19
funds <- colnames(ret)
20
21
#' Set up constraint object using v1 specification
22
gen.constr <- constraint(assets=funds, min=0, max=0.55, min_sum=0.99, max_sum=1.01,
23
weight_seq=generatesequence(min=0, max=0.55, by=0.002))
24
class(gen.constr)
25
26
#' Add an objective to the gen.constr object
27
gen.constr <- add.objective(constraints=gen.constr, type="return", name="mean", enabled=TRUE)
28
29
#' Here we run the optimization. Note that optimize.portfolio will detect
30
#' that a v1_constraint object has been passed in and will update to the
31
#' v2 specification using a portfolio object with constraints and objectives
32
#' from the v1_constraint object.
33
34
#' Solve the problem using the random portfolios optimization engine
35
optrpv1 <- optimize.portfolio(R=ret, constraints=gen.constr, optimize_method="random", search_size=2000)
36
optrpv1
37
38
#' Solve the problem using the DEoption (Differential Evolution) optimization engine
39
optdev1 <- optimize.portfolio(R=ret, constraints=gen.constr, optimize_method="DEoptim", search_size=2000)
40
optdev1
41
42
#' Solve the problem using the ROI (R Optimization Infrastructure) optimization engine
43
optroiv1 <- optimize.portfolio(R=ret, constraints=gen.constr, optimize_method="ROI")
44
optroiv1
45
46
47
48