PortfolioAnalytics
PortfolioAnalytics copied to clipboard
Error when reading optimize_method in optimize.portfolio.rebalancing
The new CVXR method introduced a bug when reading the optimize_method
user input. This happens if the uses CVXR and defines an optimizer (e.g., c("CVXR", "SCS")
). The expectation is that optimized_method
is a character vector of length 1. However, if the user includes an optimizer, it becomes a character vector of length 2.
Example code:
library(PortfolioAnalytics)
library(CVXR)
library(data.table)
library(xts)
data(edhec)
# Use edhec for a returns object
ret_edhec <- tail(edhec, 60)
colnames(ret_edhec) <- c("CA", "CTAG", "DS", "EM", "EMN", "ED", "FIA",
"GM", "LSE", "MA", "RV", "SS", "FF")
print(head(ret_edhec, 5))
fund_edhec <- colnames(ret_edhec)
# Create portfolio object
pspec_maxret <- portfolio.spec(assets=fund_edhec)
# Add constraints to the portfolio object
pspec_maxret <- add.constraint(pspec_maxret, type="full_investment")
pspec_maxret <- add.constraint(portfolio = pspec_maxret, type = "box",
min = rep(0.02, 13),
max = c(rep(0.6, 4), rep(0.2, 9)))
# Add objective to the portfolio object
pspec_maxret <- add.objective(portfolio = pspec_maxret,
type = "risk", name = "StdDev")
pspec_maxret <- add.objective(portfolio = pspec_maxret,
type = "return", name = "mean")
pspec_maxret
# Run the optimization with default solver
opt_maxret <- optimize.portfolio(R=ret_edhec, portfolio=pspec_maxret,
optimize_method=c("CVXR", "SCS"), trace=TRUE,MaxSR=TRUE)
opt_maxret
bt_maxret <- optimize.portfolio.rebalancing(R=ret_edhec, portfolio=pspec_maxret,
optimize_method=c("CVXR", "SCS"),
rebalance_on="quarters",
training_period=36, trace=TRUE, MaxSR=TRUE)