Path: blob/master/demo/demo_proportional_cost.R
1433 views
#' ---1#' title: "Proportional Cost Constraint Demo"2#' author: Ross Bennett3#' date: "7/17/2014"4#' ---56#' Load the package and data7library(PortfolioAnalytics)8data(edhec)9N <- 410R <- edhec[, 1:N]11colnames(R) <- c("CA", "CTAG", "DS", "EM")12funds <- colnames(R)1314#' Transaction costs are calculated using the optimal weights and initial set of weights.15#' The initial set of weights is specified in the portfolio object. In the case16#' below, the initial set of weights is an equally-weighted portfolio.1718#' Set up initial portfolio specification object with basic constraints.19pspec <- portfolio.spec(assets=funds)20pspec <- add.constraint(portfolio=pspec, type="full_investment")21pspec <- add.constraint(portfolio=pspec, type="long_only")2223#' Add the proportional transaction cost constraint.24pspec <- add.constraint(portfolio=pspec, type="transaction", ptc=0.01)2526#' Add objective to minimize portfolio variance.27minvar <- add.objective(portfolio=pspec, type="risk", name="var")2829#' Note that if a return target is not specified, the results may not make sense.30optimize.portfolio(R=R, portfolio=minvar, optimize_method="ROI")3132#' Add a target return constraint33minvar <- add.constraint(portfolio=minvar, type="return", return_target=0.007)3435#' Run the optimization.36optimize.portfolio(R=R, portfolio=minvar, optimize_method="ROI")3738#' Add return and risk objective for quadratic utility.39#' Note that target return can be specified as a constraint or in the return40#' objective as shown below.41qu <- add.objective(portfolio=pspec, type="risk", name="var", risk_aversion=0.3)42qu <- add.objective(portfolio=qu, type="return", name="mean", target=0.007)43optimize.portfolio(R=R, portfolio=qu, optimize_method="ROI")4445#' Now use random portfolios for the optimization.46#' Set up portfolio with equally weighted portfolio for initial weights.47pspec <- portfolio.spec(assets=funds)48pspec <- add.constraint(portfolio=pspec, type="leverage", min_sum=0.99, max_sum=1.01)49pspec <- add.constraint(portfolio=pspec, type="long_only")50#' Add the proportional transaction cost constraint.51pspec <- add.constraint(portfolio=pspec, type="transaction", ptc=0.01)5253# There is no transaction cost, the penalty should be 0.54# constrained_objective(w=rep(1/4, 4), R=R, portfolio=pspec)55# wts <- c(0.2, 0.3, 0.25, 0.25)56# sum(abs(wts - pspec$assets)*pspec$constraints[[3]]$ptc)57# constrained_objective(w=wts, R=R, portfolio=pspec)5859#' Add objective to minimize standard deviation60pspec <- add.objective(portfolio=pspec, type="risk", name="StdDev")6162#' This pushes the optimal portfolio to the initial weights to limit63#' transaction costs.64opt_rp <- optimize.portfolio(R=R, portfolio=pspec, optimize_method="random",65search_size=2000, trace=TRUE)66opt_rp6768#' Set up portfolio with initial weights w = {0.15 0.15 0.2 0.5}69pspec <- portfolio.spec(assets=c(0.15, 0.15, 0.2, 0.5))70pspec <- add.constraint(portfolio=pspec, type="leverage", min_sum=0.99, max_sum=1.01)71pspec <- add.constraint(portfolio=pspec, type="long_only")72pspec <- add.constraint(portfolio=pspec, type="transaction", ptc=0.01)7374# There is no transaction cost, the penalty should be 0.75# constrained_objective(w=rep(1/4, 4), R=R, portfolio=pspec)76# wts <- c(0.2, 0.3, 0.25, 0.25)77# sum(abs(wts - pspec$assets)*pspec$constraints[[3]]$ptc)78# constrained_objective(w=wts, R=R, portfolio=pspec)7980#' Add objective to minimize standard deviation.81pspec <- add.objective(portfolio=pspec, type="risk", name="StdDev")8283#' This pushes the optimal portfolio to the initial weights to limit84#' transaction costs.85opt_rp <- optimize.portfolio(R=R, portfolio=pspec, optimize_method="random",86search_size=2000, trace=TRUE)87opt_rp888990