## implement a weighted rolling 'sum of squares'
fun <- "
double out = 0;
const double m = mean( X_SUB );
for( int i=0; i < n; i++ ) {
out += weights[i] * ( (X(i)-m) * (X(i)-m) ) / (N-1);
}
return out;
"
rolling_var <- rollit_raw( fun )
x <- 1:5
rolling_var( x, 5 ) == var(x)
## a (slow-ish) implementation of rolling kurtosis
fun <- "
double numerator = 0;
double denominator = 0;
const double m = mean( X_SUB );
for( int i=0; i < n; i++ ) {
double tmp = ( X(i) - m ) * ( X(i) - m );
numerator += tmp * tmp;
denominator += tmp;
}
return N * numerator / ( denominator * denominator );
"
rolling_kurt <- rollit_raw( fun )
x <- rnorm(100)
rolling_kurt(x, 20)
Run the code above in your browser using DataLab