# Using RcppArmadillo to price European Put Options

**Rcpp Gallery**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

### Introduction

In the quest for ever faster code, one generally begins exploring ways to integrate C++ with R using Rcpp. This post provides an example of multiple implementations of a European Put Option pricer. The implementations are done in pure R, pure Rcpp using some Rcpp sugar functions, and then in Rcpp using RcppArmadillo, which exposes the incredibly powerful linear algebra library, Armadillo.

Under the Black-Scholes model The value of a European put option has the closed form solution:

where

and

Armed with the formulas, we can create the pricer using just R.

[1] 5.52021

[1] 5.52021 4.58142 3.68485 2.85517 2.11883 1.49793

Let’s see what we can do with Rcpp. Besides explicitely stating the
types of the variables, not much has to change. We can even use the sugar function,
`Rcpp::pnorm()`

, to keep the syntax as close to R as possible. Note how we are being
explicit about the symbols we import from the `Rcpp`

namespace: the basic vector type, and
well the (vectorized) ‘Rcpp Sugar’ calls `log()`

and `pnorm()`

calls. Similarly, we use
`sqrt()`

and `exp()`

for the calls on an atomic `double`

variables from the C++ Standard
Library. With these declarations the code itself is essentially identical to the R code
(apart of course from requiring both static types and trailing semicolons).

We can call this from R as well:

[1] 5.52021

[1] 5.52021 4.58142 3.68485 2.85517 2.11883 1.49793

Finally, let’s look at
RcppArmadillo. Armadillo has a
number of object types, including `mat`

, `colvec`

, and `rowvec`

. Here, we just use
`colvec`

to represent a column vector of prices. By default in Armadillo, `*`

represents
matrix multiplication, and `%`

is used for element wise multiplication. We need to make
this change to element wise multiplication in 1 place, but otherwise the changes are just
switching out the types and the sugar functions for Armadillo specific functions.

Note that the `arma::normcdf()`

function is in the upcoming release of
RcppArmadillo, which is
`0.8.400.0.0`

at the time of writing and still in CRAN’s incoming. It also requires the
`C++11`

plugin.

Use from R:

[,1] [1,] 5.52021

[,1] [1,] 5.52021 [2,] 4.58142 [3,] 3.68485 [4,] 2.85517 [5,] 2.11883 [6,] 1.49793

Finally, we can run a speed test to see which comes out on top.

test replications elapsed relative 2 Arma 100 6.409 1.000 3 Rcpp 100 7.917 1.235 1 R 100 9.091 1.418

Interestingly, Armadillo comes out on top here on this (multi-core) machine (as Armadillo uses OpenMP where available in newer versions). But the difference is slender, and there is certainly variation in repeated runs. And the nicest thing about all of this is that it shows off the “embarassment of riches” that we have in the R and C++ ecosystem for multiple ways of solving the same problem.

**leave a comment**for the author, please follow the link and comment on their blog:

**Rcpp Gallery**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.