Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/demo/demo_leverage_exposure_constraint.R
1433 views
1
#' ---
2
#' title: "Leverage Exposure Constraint Demo"
3
#' author: Ross Bennett
4
#' date: "7/17/2014"
5
#' ---
6
7
#' This script demonstrates how to solve a portfolio optimization problem with
8
#' leverage exposure constraints.
9
#'
10
11
#' Load the package and data
12
library(PortfolioAnalytics)
13
data(edhec)
14
R <- edhec[, 1:10]
15
funds <- colnames(R)
16
17
#' Set up an initial portfolio object with basic constraints.
18
init.portf <- portfolio.spec(assets=funds)
19
20
#' Add an objective to maximize mean return per unit expected shortfall.
21
init.portf <- add.objective(portfolio=init.portf, type="return", name="mean")
22
init.portf <- add.objective(portfolio=init.portf, type="risk", name="ES")
23
24
#' The leverage_exposure constraint type is supported for random, DEoptim, pso,
25
#' and GenSA solvers. The following examples use DEoptim for solving the
26
#' optimization problem.
27
28
#' Dollar neutral portfolio with max 2:1 leverage constraint.
29
dollar.neutral.portf <- init.portf
30
dollar.neutral.portf <- add.constraint(portfolio=dollar.neutral.portf,
31
type="weight_sum",
32
min_sum=-0.01, max_sum=0.01)
33
dollar.neutral.portf <- add.constraint(portfolio=dollar.neutral.portf,
34
type="box", min=-0.5, max=0.5)
35
dollar.neutral.portf <- add.constraint(portfolio=dollar.neutral.portf,
36
type="leverage_exposure", leverage=2)
37
38
#' Run optimization using DEoptim as optimization backend.
39
dollar.neutral.opt <- optimize.portfolio(R=R, portfolio=dollar.neutral.portf,
40
optimize_method="DEoptim",
41
search_size=2000)
42
dollar.neutral.opt
43
44
#' Set up leveraged portfolio with max 1.6:1 leverage exposure constraint.
45
leveraged.portf <- init.portf
46
leveraged.portf <- add.constraint(portfolio=leveraged.portf,
47
type="weight_sum",
48
min_sum=0.99, max_sum=1.01)
49
leveraged.portf <- add.constraint(portfolio=leveraged.portf,
50
type="box", min=-0.3, max=0.8)
51
leveraged.portf <- add.constraint(portfolio=leveraged.portf,
52
type="leverage_exposure", leverage=1.6)
53
54
#' Run optimization using DEoptim as optimization backend.
55
leveraged.opt <- optimize.portfolio(R=R, portfolio=leveraged.portf,
56
optimize_method="DEoptim",
57
search_size=2000)
58
leveraged.opt
59
60
61