[R-SIG-Finance] PortfolioAnalytics with turnover constraint

Ross Bennett rossbennett34 at gmail.com
Thu Jan 18 18:25:56 CET 2018


Roger,

The "assets" argument for the portfolio.spec function should be a vector.
You are passing in a data.frame object.

Try defining your wgts object as follows
wgts <- c("CA" = .1, "CTAG" = .3, "DS" = .4, "EM" = .1, "EQM" = .1)

Best,
Ross


On Thu, Jan 18, 2018 at 11:06 AM, Bos, Roger <roger.bos at rothschild.com>
wrote:

> Dear All,
>
> I am trying to use the PortfolioAnalytics package to run an optimization
> with a turnover constraint.  I didn't see any examples in the demos.  The
> example code below runs in I set the portfolio up using equal weights:
>
> init.portf <- portfolio.spec(assets=funds)
>
> But if I instead try to use a named vector of initial weights, as
> described in the docs, as such:
>
> init.portf <- portfolio.spec(assets=wgts) # Does not work with this line
> added
>
> I get an error when I try to run the optimization:
>
> 11:57:01  > minStdDev.DE <- optimize.portfolio(R=R, portfolio=init.portf,
> optimize_method="DEoptim", search_size = 2000)
>  Show Traceback
>
>  Rerun with Debug
>  Error in result[2, ] <- rep(1/length(seed), length(seed)) :
>   incorrect number of subscripts on matrix
>
> The portfolio.spec function did take in the initial weights properly, as
> shown using the print function:
>
> 11:57:01  > print.default(init.portf)
> $assets
>    CA CTAG  DS  EM EQM
> 1 0.1  0.3 0.4 0.1 0.1
>
> Can anyone provide me with a working example using both initial weights
> and a turnover constraint?  I don't care it if uses DEoptim or some other
> optimizer, I just want to get started with something.
>
> Thanks,
>
> Roger
>
>
> library(PortfolioAnalytics)
>
> data(edhec)
> R <- edhec[, 1:5]
> colnames(R) <- c("CA", "CTAG", "DS", "EM", "EQM")
> funds <- colnames(R)
> wgts <- data.frame("CA" = .1, "CTAG" = .3, "DS" = .4, "EM" = .1, "EQM" =
> .1)
> wgts
>
> # Set up portfolio with objectives and constraints
> init.portf <- portfolio.spec(assets=funds)
> init.portf <- portfolio.spec(assets=wgts) # Does not work with this line
> added
> init.portf <- add.constraint(portfolio = init.portf, type="weight_sum",
> min_sum = .99, max_sum = 1.01)
> init.portf <- add.constraint(portfolio = init.portf, type="long_only")
> init.portf <- add.constraint(portfolio = init.portf, type="turnover",
> turnover_target = .2)
>
> # Add an objective to minimize portfolio standard deviation
> init.portf <- add.objective(portfolio=init.portf, type="risk",
> name="StdDev")
> print.default(init.portf)
>
> # Solve with DEoptim
> minStdDev.DE <- optimize.portfolio(R=R, portfolio=init.portf,
> optimize_method="DEoptim", search_size = 2000)
> minStdDev.DE
>
>
>
>
>
> This message and any attachments are for the intended recipient’s use
> only. This message may contain confidential, proprietary or legally
> privileged information. No right to confidential or privileged treatment of
> this message is waived or lost by an error in transmission.
> If you have received this message in error, please immediately notify the
> sender by e-mail, delete the message, any attachments and all copies from
> your system and destroy any hard copies. You must not, directly or
> indirectly, use, disclose, distribute, print or copy any part of this
> message or any attachments if you are not the intended recipient.
>
>
>
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions
> should go.

	[[alternative HTML version deleted]]



More information about the R-SIG-Finance mailing list