Correlation in Rcpp
11 Apr 2015
To install Systematic Investor Toolbox (SIT) please visit About page.
There are many great examples of using Rcpp at Rcpp Gallery.
Below, I will use RcppParallel to speed computations of correlation matrix.
First let’s implement correlation using Rcpp:
Please save above code in the correlation.cpp
file or download correlation.cpp.
Next let’s make sure it produces same results.
item1 |
item2 |
equal |
r.cor |
c.cor |
TRUE |
The RcppParallel is an easy way to speed up
above computations. For more details about RcppParallel
I recommend following resources:
Next let’s implement correlation using RcppParallel:
Please save above code in the correlation.cpp
file or download correlation.cpp.
Next let’s make sure it produces same results and compare the run times.
item1 |
item2 |
equal |
r.cor |
c.cor |
TRUE |
r.cor |
cp.cor |
TRUE |
c.cor |
cp.cor |
TRUE |
rownames(x) |
test |
replications |
elapsed |
relative |
3 |
cp_cor(ret) |
20 |
0.14 |
1.000 |
2 |
c_cor(ret) |
20 |
0.50 |
3.571 |
1 |
cor(ret) |
20 |
0.52 |
3.714 |
The RcppParallel version is about 3.5 times faster.
(this report was produced on: 2015-04-11)