Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/demo/demo_proportional_cost.R
1433 views
1
#' ---
2
#' title: "Proportional Cost Constraint Demo"
3
#' author: Ross Bennett
4
#' date: "7/17/2014"
5
#' ---
6
7
#' Load the package and data
8
library(PortfolioAnalytics)
9
data(edhec)
10
N <- 4
11
R <- edhec[, 1:N]
12
colnames(R) <- c("CA", "CTAG", "DS", "EM")
13
funds <- colnames(R)
14
15
#' Transaction costs are calculated using the optimal weights and initial set of weights.
16
#' The initial set of weights is specified in the portfolio object. In the case
17
#' below, the initial set of weights is an equally-weighted portfolio.
18
19
#' Set up initial portfolio specification object with basic constraints.
20
pspec <- portfolio.spec(assets=funds)
21
pspec <- add.constraint(portfolio=pspec, type="full_investment")
22
pspec <- add.constraint(portfolio=pspec, type="long_only")
23
24
#' Add the proportional transaction cost constraint.
25
pspec <- add.constraint(portfolio=pspec, type="transaction", ptc=0.01)
26
27
#' Add objective to minimize portfolio variance.
28
minvar <- add.objective(portfolio=pspec, type="risk", name="var")
29
30
#' Note that if a return target is not specified, the results may not make sense.
31
optimize.portfolio(R=R, portfolio=minvar, optimize_method="ROI")
32
33
#' Add a target return constraint
34
minvar <- add.constraint(portfolio=minvar, type="return", return_target=0.007)
35
36
#' Run the optimization.
37
optimize.portfolio(R=R, portfolio=minvar, optimize_method="ROI")
38
39
#' Add return and risk objective for quadratic utility.
40
#' Note that target return can be specified as a constraint or in the return
41
#' objective as shown below.
42
qu <- add.objective(portfolio=pspec, type="risk", name="var", risk_aversion=0.3)
43
qu <- add.objective(portfolio=qu, type="return", name="mean", target=0.007)
44
optimize.portfolio(R=R, portfolio=qu, optimize_method="ROI")
45
46
#' Now use random portfolios for the optimization.
47
#' Set up portfolio with equally weighted portfolio for initial weights.
48
pspec <- portfolio.spec(assets=funds)
49
pspec <- add.constraint(portfolio=pspec, type="leverage", min_sum=0.99, max_sum=1.01)
50
pspec <- add.constraint(portfolio=pspec, type="long_only")
51
#' Add the proportional transaction cost constraint.
52
pspec <- add.constraint(portfolio=pspec, type="transaction", ptc=0.01)
53
54
# There is no transaction cost, the penalty should be 0.
55
# constrained_objective(w=rep(1/4, 4), R=R, portfolio=pspec)
56
# wts <- c(0.2, 0.3, 0.25, 0.25)
57
# sum(abs(wts - pspec$assets)*pspec$constraints[[3]]$ptc)
58
# constrained_objective(w=wts, R=R, portfolio=pspec)
59
60
#' Add objective to minimize standard deviation
61
pspec <- add.objective(portfolio=pspec, type="risk", name="StdDev")
62
63
#' This pushes the optimal portfolio to the initial weights to limit
64
#' transaction costs.
65
opt_rp <- optimize.portfolio(R=R, portfolio=pspec, optimize_method="random",
66
search_size=2000, trace=TRUE)
67
opt_rp
68
69
#' Set up portfolio with initial weights w = {0.15 0.15 0.2 0.5}
70
pspec <- portfolio.spec(assets=c(0.15, 0.15, 0.2, 0.5))
71
pspec <- add.constraint(portfolio=pspec, type="leverage", min_sum=0.99, max_sum=1.01)
72
pspec <- add.constraint(portfolio=pspec, type="long_only")
73
pspec <- add.constraint(portfolio=pspec, type="transaction", ptc=0.01)
74
75
# There is no transaction cost, the penalty should be 0.
76
# constrained_objective(w=rep(1/4, 4), R=R, portfolio=pspec)
77
# wts <- c(0.2, 0.3, 0.25, 0.25)
78
# sum(abs(wts - pspec$assets)*pspec$constraints[[3]]$ptc)
79
# constrained_objective(w=wts, R=R, portfolio=pspec)
80
81
#' Add objective to minimize standard deviation.
82
pspec <- add.objective(portfolio=pspec, type="risk", name="StdDev")
83
84
#' This pushes the optimal portfolio to the initial weights to limit
85
#' transaction costs.
86
opt_rp <- optimize.portfolio(R=R, portfolio=pspec, optimize_method="random",
87
search_size=2000, trace=TRUE)
88
opt_rp
89
90