RcppArmadillo: Accelerating R with high-performance C++ linear algebra

https://doi.org/10.1016/j.csda.2013.02.005Get rights and content

Abstract

The R statistical environment and language has demonstrated particular strengths for interactive development of statistical algorithms, as well as data modelling and visualisation. Its current implementation has an interpreter at its core which may result in a performance penalty in comparison to directly executing user algorithms in the native machine code of the host CPU. In contrast, the C++ language has no built-in visualisation capabilities, handling of linear algebra or even basic statistical algorithms; however, user programs are converted to high-performance machine code, ahead of execution. A new method avoids possible speed penalties in R by using the Rcpp extension package in conjunction with the Armadillo C++ matrix library. In addition to the inherent performance advantages of compiled code, Armadillo provides an easy-to-use template-based meta-programming framework, allowing the automatic pooling of several linear algebra operations into one, which in turn can lead to further speedups. With the aid of Rcpp and Armadillo, conversion of linear algebra centred algorithms from R to C++ becomes straightforward. The algorithms retain the overall structure as well as readability, all while maintaining a bidirectional link with the host R environment. Empirical timing comparisons of R and C++ implementations of a Kalman filtering algorithm indicate a speedup of several orders of magnitude.

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)

  • J. Li

    An unscented Kalman smoother for volatility extraction: evidence from stock prices and options

    Computational Statistics and Data Analysis

    (2013)
  • D. Abrahams et al.

    C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond

    (2004)
  • J.W. Demmel

    Applied Numerical Linear Algebra

    (1997)
  • D. Eddelbuettel

    Seamless R and C++ Integration with Rcpp

    (2013)
  • D. Eddelbuettel 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...
  • S. Meyers

    Effective C++: 55 Specific Ways to Improve Your Programs and Designs

    (2005)
There are more references available in the full text version of this article.

Cited by (354)

  • Optimal hierarchical EWMA forecasting

    2024, International Journal of Forecasting
  • CRPS learning

    2023, Journal of Econometrics
View all citing articles on Scopus
1

The RcppArmadillo package is available with the electronic version of this article, as well as via every CRAN mirror.

View full text