dppalomar / riskParityPortfolio

Design of Risk Parity Portfolios
https://CRAN.R-project.org/package=riskParityPortfolio
GNU General Public License v3.0
107 stars 27 forks source link

riskparity + portfolioBacktesting not working as shown in the vignette. #22

Closed cheenweekiang closed 2 years ago

cheenweekiang commented 2 years ago

Problem description

Tried the Vignette for faang using risk parity and portfolioBacktest, the codes are not working. errror message as follows:

1)NA was produced 
backtestSummary(bt)$performance
                   risk parity portfolio tangency portfolio
Sharpe ratio                          NA                 NA
max drawdown                          NA                 NA
annual return                         NA                 NA
annual volatility                     NA                 NA
Sortino ratio                         NA                 NA
downside deviation                    NA                 NA
Sterling ratio                        NA                 NA
Omega ratio                           NA                 NA
VaR (0.95)                            NA                 NA
CVaR (0.95)                           NA                 NA
rebalancing period                    NA                 NA
turnover                              NA                 NA
ROT (bps)                             NA                 NA
cpu time                              NA                 NA
failure rate                           1                  1

2) Check the error message and this was what it produced.
> bt$`risk parity portfolio`$data1$error_message
[1] "unused argument (ncol(dataset$adjusted))"
attr(,"error_stack")
attr(,"error_stack")$at
[1] "ncol(dataset$adjusted, ncol(dataset$adjusted))"

attr(,"error_stack")$stack

Example

library(xts)
library(portfolioBacktest)
library(riskParityPortfolio)

# download price data
faang_data <- stockDataDownload(c("GOOG", "NFLX", "AAPL", "AMZN", "FB"),
                                from = "2014-01-01", to = "2019-06-25",rm_stocks_with_na = TRUE)

# define portfolios to be backtested
# risk parity portfolio
risk_parity <- function(dataset, w_current) {
  w_currnet <- rep(1/ncol(dataset$adjusted,ncol(dataset$adjusted)))
  prices <- dataset$adjusted
  log_returns <- diff(log(prices))[-1]
  return(riskParityPortfolio(cov(log_returns))$w)
}

# tangency portfolio (maximum sharpe ratio)
library(quadprog)
max_sharpe_ratio <- function(dataset) {
  prices <- dataset$adjusted
  log_returns <- diff(log(prices))[-1]
  N <- ncol(prices)
  Sigma <- cov(log_returns)
  mu <- colMeans(log_returns)
  if (all(mu <= 1e-8))
    return(rep(0, N))
  Dmat <- 2 * Sigma
  Amat <- diag(N)
  Amat <- cbind(mu, Amat)
  bvec <- c(1, rep(0, N))
  dvec <- rep(0, N)
  res <- solve.QP(Dmat = Dmat, dvec = dvec, Amat = Amat, bvec = bvec, meq = 1)
  w <- res$solution
  return(w/sum(w))
}

# call portfolioBacktest and benchmark against the uniform (1/N) portfolio
bt <- portfolioBacktest(list("risk parity portfolio" = risk_parity,
                             "tangency portfolio"    = max_sharpe_ratio),
                        list(faang_data),
                        lookback = 12*20, 
                        optimize_every = 3*20, rebalance_every = 3*20)

1)NA was produced 
backtestSummary(bt)$performance
                   risk parity portfolio tangency portfolio
Sharpe ratio                          NA                 NA
max drawdown                          NA                 NA
annual return                         NA                 NA
annual volatility                     NA                 NA
Sortino ratio                         NA                 NA
downside deviation                    NA                 NA
Sterling ratio                        NA                 NA
Omega ratio                           NA                 NA
VaR (0.95)                            NA                 NA
CVaR (0.95)                           NA                 NA
rebalancing period                    NA                 NA
turnover                              NA                 NA
ROT (bps)                             NA                 NA
cpu time                              NA                 NA
failure rate                           1                  1

2) Check the error message and this was what it produced.
> bt$`risk parity portfolio`$data1$error_message
[1] "unused argument (ncol(dataset$adjusted))"
attr(,"error_stack")
attr(,"error_stack")$at
[1] "ncol(dataset$adjusted, ncol(dataset$adjusted))"

attr(,"error_stack")$stack
[1] ""

Expected behavior

# check performance summary
backtestSummary(bt)$performance
#>                   risk parity portfolio tangency portfolio
#> Sharpe ratio                  1.3800144          0.8787596
#> max drawdown                  0.3062046          0.3516856
#> annual return                 0.3117200          0.2324203
#> annual volatility             0.2258817          0.2644868
#> Sterling ratio                1.0180122          0.6608751
#> Omega ratio                   1.2710283          1.1793760
#> ROT (bps)                  8310.1199557        793.0188434

Environment:

mandarpriya commented 2 years ago

Same backtestSummary(bt)$performance risk parity portfolio tangency portfolio Sharpe ratio NA NA max drawdown NA NA annual return NA NA annual volatility NA NA Sortino ratio NA NA downside deviation NA NA Sterling ratio NA NA Omega ratio NA NA VaR (0.95) NA NA CVaR (0.95) NA NA rebalancing period NA NA turnover NA NA ROT (bps) NA NA cpu time NA NA failure rate 1 1

fviolette26 commented 2 years ago

Same

mirca commented 2 years ago

Hi all, thanks for reporting this.

The vignette in riskParityPortfolio hasn't kept up with the API changes over in portfolioBacktest (I've just updated it though :))! However, a simple change would fix this example:

1) change the signature of the portfolio functions:

2) the parameter T_rolling_window has been renamed to lookback

3) backtestChartCumReturns has been changed to backtestChartCumReturn

See the updated vignette :).

Hope that helps,