tests/testthat/test-riskparity_regularize.R

#
# test-riskparity_regularize.R
# ----------------------------
#
#
library(ResilientPortfolio)
library(testthat)

context("Testing riskparity_regularize")

test_that("Testing riskparity_regularize function", {



  ##### Code for testing function only  ####
  library(ResilientPortfolio)
  prices     = xts_alldata[, c(1:6)]   # ensure no NAs in prices
  maxweights = c(0.5, 0.4, 0.3, 0.4)
  names(maxweights) <- c("BND", "SPY", "QQQ", "VTI")
  defaultweight = 0.35
  riskpar_feature = "sd63"
  riskpar_thresh  = 0.12
  momfeature = "mom84"
  absmom_thresh  = 0.0
  reguwindow  = 63   # rename to rankwindow, for rankscore
  cashasset  = "BND"
  Nassets    = 3   # number of assets in portfolio, including CASH
  wfodates   = "months"

  sellrank = 1
  rho      = 0.0
  sigma    = 0.0
  epsilon  = 0
  eta      = 1
  abscor   = FALSE
  daily_rate = 0.001 / 252

  symbols  <- colnames(prices)
  wfodates <- wfo_getdates(prices, features = c(momfeature, riskpar_feature),
                           rollwindow = reguwindow, wfo_span = "months")

  rprlist <- riskparity_regularize(prices          = prices,
                                   maxweights      = maxweights,
                                   defaultweight   = defaultweight,
                                   riskpar_feature = riskpar_feature,
                                   riskpar_thresh  = riskpar_thresh,
                                   momfeature      = momfeature,
                                   absmom_thresh   = absmom_thresh,
                                   reguwindow      = reguwindow,
                                   Nassets         = Nassets,
                                   cashasset       = cashasset,
                                   wfodates        = wfodates,
                                   sellrank        = sellrank,
                                   rho             = rho,
                                   sigma           = sigma,
                                   epsilon = 0, eta = 1, abscor = FALSE,
                                   daily_rate = 0.001/252
                                   )

  m1wts <- rprlist$max_weights[, symbols]

  rpslist <- riskparity_screen(prices             = prices,
                               maxweights         = maxweights,
                               riskpar_feature    = riskpar_feature,
                               riskpar_thresh     = riskpar_thresh,
                               momfeature         = momfeature,
                               absmom_thresh      = absmom_thresh,
                               cashasset          = cashasset,
                               Nassets            = Nassets,
                               defaultweight      = defaultweight,
                               return_matrices    = TRUE)


  m2wts <- rpslist$max_weights[wfodates, symbols]

  # Are both matrices identical?
  all(m1wts == m2wts)


  #############


  max_weights <- rprlist$max_weights
  mommat      <- rprlist$momentum_mat
  holdingsmat <- rprlist$holdingsmat
  max_wtsmom  <- rprlist$max_wtsmom
  volmat      <- rprlist$volatility_mat
  regumommat  <- rprlist$regumommat


  print(head(max_weights, 10))
  print(head(holdingsmat, 10))
  print(head(max_wtsmom, 10))
  print(head(mommat, 10))
  print(head(volmat, 10))



})
jeanmarcgp/ResilientPortfolio documentation built on April 8, 2018, 5:43 p.m.