4
$\begingroup$

I've been playing around with the R package PortfolioAnalytics and I have spent more time than I'm willing to admit to try and resolve this issue:

When I follow the regime switching example with the edhec data from the vignette (https://gist.github.com/rossb34/10328671) the code picks the 2 different individual regimes and it works, but if I use my data set, the single portfolio optimisations on each regime seem to be using only one of the regimes. The same issue happens if I randomly generate some data. I think it's perhaps something to do with the xts object, but I really have no idea. Any help would be appreciated.

Here is the code with random data, at the end running "opt1\$regime" and "opt2\$regime" gives the same number instead of 1 and 2, respectively:

library(PortfolioAnalytics)

set.seed(123)
obs <- matrix(rnorm(2680)/100,,10)

dates <- seq(as.Date("1999-02-01"), length = 268, by = "months")
dates

data<-xts(x = obs, order.by = dates)
colnames(data)<-c("a","b","c","d","e","f","g","h","i","j")

R<-data

set.seed(123)
regime <- xts(sample(1:2, nrow(R), replace=TRUE, prob=c(0.3, 0.7)), index(R))

port1 <- portfolio.spec(assets=colnames(R))
port1 <- add.constraint(port1, "weight_sum", min_sum=0.99, max_sum=1.01)
port1 <- add.constraint(port1, "box", min=0.1, max=0.5)
port1 <- add.objective(port1, type="risk", name="ES", arguments=list(p=0.9))
port1 <- add.objective(port1, type="risk_budget", name="ES", arguments=list(p=0.9), max_prisk=0.5)

#' Construct portfolio for regime 2.
port2 <- portfolio.spec(assets=colnames(R))
port2 <- add.constraint(port2, "weight_sum", min_sum=0.99, max_sum=1.01)
port2 <- add.constraint(port2, "box", min=0, max=0.6)
port2 <- add.objective(port2, type="risk", name="StdDev")
port2 <- add.objective(port2, type="risk_budget", name="StdDev", max_prisk=0.5)

#' Combine the portfolios.
portfolios <- combine.portfolios(list(port1, port2))

#' Now we construct the regime model and corresponding portfolios to use for
#' each regime.
regime.port <- regime.portfolios(regime, portfolios)

regime.port

#' This optimization should result in out portfolio for regime 1, but it picks regime 2.
opt1 <- optimize.portfolio(R, regime.port, 
                           optimize_method="random", 
                           search_size=200, 
                           trace=TRUE)
opt1
opt1$regime

#' This optimization should result in out portfolio for regime 2, this one picks regime 2 as well.
opt2 <- optimize.portfolio(R[1:(nrow(R)-1)], regime.port, 
                           optimize_method="random", 
                           search_size=200, 
                           trace=TRUE)
opt2
opt2$regime

$\endgroup$

1 Answer 1

1
$\begingroup$

https://gist.github.com/rossb34/10328671 differs in the portfolios passed as arguments in the different steps. Do try it that way and report. Essentially, it says:

  portfolio <- list(port1, port2)
  # ...
  opt_regime <- regime.backtest(R, portfolio, regime, ...)
  # ...
  opt_port1 <- optimize.portfolio.rebalancing(R, port1, ...)
  opt_port2 <- optimize.portfolio.rebalancing(R, port2, ...)

So in your code, try after changing the following way:

  opt1 <- optimize.portfolio(... port1 ...) # instead of regime.port
  opt2 <- optimize.portfolio(... port2 ...) # instead of regime.port
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.