Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when reading optimize_method in optimize.portfolio.rebalancing #36

Open
goodnewz opened this issue Jan 9, 2023 · 0 comments
Open

Comments

@goodnewz
Copy link

goodnewz commented Jan 9, 2023

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)
goodnewz added a commit to goodnewz/PortfolioAnalytics that referenced this issue Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant