Scenario Analysis of Risk Parity using...
Transcript of Scenario Analysis of Risk Parity using...
Scenario Analysis of Risk Parity using RcppParallel
Jason FosterCRAN - Package ‘roll’
May 20, 2017
roll: rolling statistics
The roll package provides parallel functions for computing rolling statisticsof time-series data; it uses Rcpp, RcppArmadillo, and RcppParallel:
install.packages("roll")
Function Description
roll_sum Sumsroll_prod Productsroll_mean Meansroll_var Variancesroll_cov Covariancesroll_sd Standard deviations
Function Description
roll_cor Correlationsroll_scale Z-scoresroll_lm Linear modelsroll_eigen Eigendecompsroll_pcr PCA regressionsroll_vif Multicollinearity
Jason Foster CRAN - Package ’roll’ 2 / 15
Rolling beta for 2,000 portfolios!Speed gains from parallel computing help decrease the trade-off betweennumber of observations and window size:
0
3
6
9
12
500 1,000 1,500 2,000 2,500
Number of observations
Min
utes
FunctionfastLmPureroll_lm
Window size1,260882504126
Less than 1−minute!
Jason Foster CRAN - Package ’roll’ 3 / 15
RcppArmadillo versionDownload the S&P 500 index using quantmod and calculate returns:
library(quantmod)getSymbols("^GSPC")
Then use RcppArmadillo and estimate a portfolio’s sensitivity to the S&P500 index by rolling the fastLmPure function:
library(RcppArmadillo)fastLm_coef <- function(data) {
return(coef(fastLmPure(cbind(1, data$x), data$y)))}
arma_lm_coef <- rollapplyr(data = returns, width = 252,FUN = fastLm_coef,by.column = FALSE)
Jason Foster CRAN - Package ’roll’ 4 / 15
RcppParallel version
Repeat the exercise and take advantage of parallel processing in C++ byusing the roll_lm function in the roll package:
library(roll)roll_lm_coef <- roll_lm(x = returns$x,
y = returns$y,width = 252)$coefficients
And test that the coefficients from the fastLmPure and roll_lm functionsare equal:
all.equal(arma_lm_coef, roll_lm_coef)
## [1] TRUE
Jason Foster CRAN - Package ’roll’ 5 / 15
RcppParallel: parallel programming tools for ‘Rcpp’
RcppParallel, developed by the Rcpp Core team, makes it easy to createsafe, portable, high-performance parallel algorithms using C++ and Rcpp:
install.packages("RcppParallel")
In particular, RcppParallel provides two high-level parallel algorithms:
parallelFor(): convert the work of a standard serial “for” loop intoa parallel oneparallelReduce(): compute and aggregate multiple values in parallel
For more information, visit the RcppParallel website:http://rcppcore.github.io/RcppParallel/
Jason Foster CRAN - Package ’roll’ 6 / 15
Style analysis of multi-asset portfolios
Download daily returns for 150 portfolios in the Tactical Allocation andWorld Allocation categories and apply a 15 asset class factor model on adaily rolling window:
Stocks Bonds & other
US Large Cap Value US Gov 1-10YUS Large Cap Growth US Gov 10Y+US Mid Cap Non-US GovUS Small Cap IG CorpEurope HY CorpJapan MBSPacific ex Japan CommoditiesEmerging Markets
Sharpe, William F. 1988. “Determining a Fund’s Effective Asset Mix.” Investment Management Review.
Jason Foster CRAN - Package ’roll’ 7 / 15
Asset allocation
Daily returns are readily available and help make inferences about style:
71.4 secs
2.9 secs
0
20
40
60
80
0 250 500 750 1,000
Number of simulations
Sec
onds
fastLmPureroll_lm
25x over RcppArmadillo version!
Speed test uses univariate betas
0.0
0.2
0.4
0.6
0.8
2013 2014 2015 2016 2017
Coe
ffici
ents
Stocks Bonds & other
Median portfolio
Jason Foster CRAN - Package ’roll’ 8 / 15
Concentrated in equity risk
A significant driver of return variability is the concentration of equity risk:
94.9 secs
2.2 secs
0
28
55
82
110
0 250 500 750 1,000
Number of simulations
Sec
onds
cov.wtroll_cov
44x over R version!
Speed test uses univariate betas
0.0
2.5
5.0
7.5
10.0
2013 2014 2015 2016 2017
Ris
k co
ntrib
utio
n (%
)
Stocks Bonds & other
Median portfolio
Jason Foster CRAN - Package ’roll’ 9 / 15
Decline in realized volatilityFrom mixed economic data to rising geopolitical risk, volatility acrossmarkets and asset classes is near an all-time. . . low?
78.8 secs
0.8 secs0
22
45
68
90
0 250 500 750 1,000
Number of simulations
Sec
onds
sdroll_sd
93x over R version!
Speed test uses univariate betas
0
9
18
27
36
2013 2014 2015 2016 2017
Vol
atili
ty (
%)
Stocks Bonds & other
Asset classes
Jason Foster CRAN - Package ’roll’ 10 / 15
Simple risk parity strategy
Consider a portfolio of N assets and let w = (w1, . . . ,wN) be a vector ofasset weights. Also define Σ to be the covariance matrix of returns. Thenthe volatility of the portfolio is defined as:
σ =√
wT Σw
and, by Euler’s theorem, the risk contribution of asset i is given by:
σi = wi∂σ
∂wi
Goal: solve for weights such that each asset contributes equal risk
However, in practice, it is possible for managers to have discretion over thetiming of investment decisions!
Jason Foster CRAN - Package ’roll’ 11 / 15
Scenario analysisCompute rolling statistics using the roll package and feed into a simplerisk parity strategy:
Instruments: 15 asset classesRisk settings: long-term; exponential decayApproach: passive; equal risk contributionsVolatility target: 10% volatility levelRebalancing: monthly; systematic
Now, against the backdrop of the current environment of low realizedvolatility, consider a few generic scenarios:
Scenario Volatility Correlation
Scenario 1 Average AverageScenario 2 Low LowScenario 3 High High
Jason Foster CRAN - Package ’roll’ 12 / 15
A toy example: return to average
Simulate scenarios and mechanics of the simple risk parity strategy:
83.4 secs
1 secs0
24
48
71
95
0 250 500 750 1,000
Number of simulations
Sec
onds
RRcppParallel
81x over R version!
Speed test uses univariate betas
1.8
2.0
2.2
2.4
2.6
2014 2015 2016 2017 2018
Leve
rage
Volatility Correlation Total
Scenario 1
Jason Foster CRAN - Package ’roll’ 13 / 15
Explore the scenarios
Parallel computing via RcppParallel helps to dig deeper and faster:
2.1
2.3
2.4
2.6
2016−07 2017−01 2017−07 2018−01
Leve
rage
Volatility Correlation Total
Scenario 2
2.1
2.3
2.4
2.6
2016−07 2017−01 2017−07 2018−01
Leve
rage
Volatility Correlation Total
Scenario 3
Jason Foster CRAN - Package ’roll’ 14 / 15
roll: rolling statistics
Get the released version from CRAN:
install.packages("roll")
Or the development version from GitHub:
# install.packages("devtools")devtools::install_github("jjf234/roll")
Jason Foster CRAN - Package ’roll’ 15 / 15