RcppArmadillo: Accelerating R with high-performance C++ linear algebra
Section snippets
Overview
Linear algebra is a cornerstone of statistical computing and statistical software systems. Various matrix decompositions, linear program solvers, and eigenvalue/eigenvector computations are key to many estimation and analysis routines. As generally useful procedures, these are often abstracted and regrouped in specific libraries for linear algebra which statistical programmers have provided for various programming languages and environments.
One such environment and statistical programming
Armadillo
The Armadillo C++ library provides vector, matrix and cube types (supporting integer, floating point and complex numbers) as well as a subset of trigonometric and statistics functions (Sanderson, 2010). In addition to elementary operations such as addition and matrix multiplication, various matrix factorisations and submatrix manipulation operations are provided. The corresponding application programming interface (syntax) enables the programmer to write code which is both concise and
RcppArmadillo
The RcppArmadillo package (François et al., 2012) employs the Rcpp package (Eddelbuettel and François, 2011, Eddelbuettel and François, 2012, Eddelbuettel, 2013) to provide a bidirectional interface between R and C++ at the object level. Using templates, R objects such as vectors and matrices can be mapped directly to the corresponding Armadillo objects.
Consider the simple example in Listing 1. Given a vector, the g() function returns both the outer and inner products. We load the inline
Kalman filtering example
The Kalman filter is ubiquitous in many engineering disciplines as well as in statistics and econometrics (Tusell, 2011). A recent example of an application is volatility extraction in a diffusion option pricing model (Li, 2013). Even in its simplest linear form, the Kalman filter can provide simple estimates by recursively applying linear updates which are robust to noise and can cope with missing data. Moreover, the estimation process is lightweight and fast, and consumes only minimal amounts
Empirical speed comparison
Listing 7 contains the code for creating a simple benchmarking exercise. It compares several functions for implementing the Kalman filter, all executed within the R environment. Specifically, we examine the initial R version FirstKalmanR shown in Listing 3, a refactored version KalmanR shown in Listing 4, an improved version3 due to an anonymous referee (not shown, but included in the package), as
Conclusion
This paper introduced the RcppArmadillo package for use within the R statistical environment. By using the Rcpp interface package, RcppArmadillo brings the speed of C++ along with the highly expressive Armadillo linear algebra library to the R language. A small example implementing a Kalman filter illustrated two key aspects. First, orders of magnitude of performance gains can be obtained by deploying C++ code along with R. Second, the ease of use and readability of the corresponding C++ code
Acknowledgements
NICTA is funded by the Australian Government as represented by the Department of Broadband, Communications and the Digital Economy, as well as the Australian Research Council through the ICT Centre of Excellence program.
Adam M. Johansen provided helpful comments on an earlier draft. Comments by two anonymous referees and one editor further improved the paper and are gratefully acknowledged.
References (18)
An unscented Kalman smoother for volatility extraction: evidence from stock prices and options
Computational Statistics and Data Analysis
(2013)- et al.
C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond
(2004) Applied Numerical Linear Algebra
(1997)Seamless R and C++ Integration with Rcpp
(2013)- et al.
Rcpp: seamless R and C++ integration
Journal of Statistical Software
(2011) - Eddelbuettel, D., François, R., 2012. Rcpp: seamless R and C++ integration, R package version 0.10.2. URL...
- François, R., Eddelbuettel, D., Bates, D., 2012. RcppArmadillo: Rcpp integration for Armadillo templated linear algebra...
Effective C++: 55 Specific Ways to Improve Your Programs and Designs
(2005)
Cited by (354)
Optimal hierarchical EWMA forecasting
2024, International Journal of ForecastingCRPS learning
2023, Journal of EconometricsCoarse-graining algorithms for the Eulerian-Lagrangian simulation of particle-laden flows
2023, Journal of Computational PhysicsBayesian D- and I-optimal designs for choice experiments involving mixtures and process variables
2023, Food Quality and PreferenceA regularised logistic regression model with structured features for classification of geographical origin in olive oils
2023, Chemometrics and Intelligent Laboratory Systems
- 1
The RcppArmadillo package is available with the electronic version of this article, as well as via every CRAN mirror.