Original link:tecdat.cn/?p=17829

 

Index-weighted volatility is a measure of volatility that gives more weight to recent observations. We will use the following formula to calculate exponentially weighted volatility:

S/t ^ 2 = SUM (1 – a) * ^ I a * (r/t – 1 – I – rhat [t]) ^ 2, I = 0… inf

Where Rhat [t] is the corresponding exponential weighted average

Rhat [t] = SUM (1-a) * a ^ I * r [t-1- I], I = 0… inf

The formula above depends on the complete price history at each point in time and takes some time to calculate. Therefore, I want to share how Rcpp and RcppParallel help us reduce computation time.

I will use historical data sets of exchange rates as test data.

First, we calculate the average rolling volatility

# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * # logarithm yield calculation #***************************************************************** ret = diff(log(data$prices)) tic(5) hist.vol = sqrt(252) * bt.apply.matrix(ret, runSD, n = 200) toc(5)Copy the code

The elapsed time is 0.17 seconds

Next, let’s write the exponentially weighted code logic

Load. Packages (' RCPP ') sourceCpp(code=' #include < rcpp.h > using namespace RCPP; using namespace std; // [[Rcpp::plugins(cpp11)]] //ema[1] = 0 //ema[t] = (1-a)*r[t-1] + (1-a)*a*ema[t-1] // [[Rcpp::exp { if(!NumericVector::is_na(x[t])) break; res[t] = NA_REAL; } int start_t = t; -a) * a^i * (r[t-1-i] - rhat[t])^2, i=0 ... inf // [[Rcpp::export]] NumericVector run_esd_cpp(NumericVector x, double ratio) { auto sz = x.siz // find start index; first non NA item for(t = 0; t < sz; t++) { if(!Num 0; for(t = start_t + 1; t < sz; t++) { ema = (1-ratio) * ( x[t-1] + ratio * ema); double sigma = 0; for(int i = 0; i < (t - start_t); i++) { sigma += pow(ratio,i) * pow(x[t-1-i] - ema, 2); } res[t] = (1-ratio) * sigma; } , n, ratio = n/(n+1)) run_ema_cpp(x, ratio) run.esd = functCopy the code

The elapsed time is 106.16 seconds.

It took a while to execute this code. However, the code can be run in parallel. Here is the RcppParallel version.

Load. Packages ('RcppParallel') sourceCpp(code=' using namespace RCPP; using namespace s s(cpp11)]] // [[Rcpp::depends(R to read from const RMatrix<double> mat; // internal variables const double ratio t; // initialize from Rcpp input and output matrixes run_esd_helper(const Nume all operator that work for th in, size_t end) { for (size_t c1 = begin; c1 < end; c1++) { int t; // find start index; firCopy the code

The elapsed time is 14.65 seconds

Less running time. Next, let’s get a visual look at the impact of using index-weighted volatility

dates = '2007::2010'
layout(1:2)
e='h', col='black', plotX=F)
	plota.legend(paste('Dai
s,1],type='l',col='black')
Copy the code

 

 

Not surprisingly, index-weighted volatility accounts for a larger portion of recent observations and is a more reactive measure of risk.


Most welcome insight

1. Har-rv-j and recursive neural network (RNN) hybrid model predict and trade high frequency volatility of large stock indexes

2.WinBUGS on multivariate stochastic volatility models: Bayesian estimation and model comparison

3. Realization of volatility: ARCH model and HAR-RV model

4. Arma-egarch model of R language and integrated prediction algorithm are used to predict the actual volatility of SPX

5. Use R language stochastic volatility model SV to process stochastic volatility in time series

6.R language multivariate COPULA GARCH model time series prediction

7. VAR fitting and prediction based on ARMA-GARCH process for R language

8.R language random search variable selection SSVS estimation Bayesian vector autoregression (BVAR) model

9. ARIMA + GARCH trading strategy for S&P500 stock index in R language