# Monte Carlo option pricing - comparison of R and Julia languages

** Published:**

This example investigates the performance of R in comparison to Julia language. Additionally shows how to easily call Julia inside R code. With that being said, we will load `JuliaCall`

library that enables us to do so. Alternatively, there is also `XRJulia`

library available.

It is necessery to tell R where is Julia.exe stored, so the loaded library can communicate with it accordingly.

Next we will define function that price European options based on Monte Carlo simulation. Namely, simulates random path of prices many times for a given set of parameters and calculates value of option based on expected value of simulated payoffs.

The model of motion of prices will be a following commonly used stochastic differential equation, called geometric Brownian motion:

\(dS_t = \mu S_t dt + \sigma S_t dW_t\) Where $W_t$, $\mu$ and $\sigma$ are respectively a Wiener process, drift and volatility.

Price dynamics model as well as chosen derivative is not very complex. The point of herein document is to compare R and Julia performance on some well-known example. As show below, it is also easy to price such an option with the same price dynamics using a well known workhorse in finance, namely Black-Scholes formula

Now we can check if the function is working properly. To do so, we will define Black-Scholes formula with a following equation for pricing call options:

\(C(S_T, t) = N(d_1)S_t - N(d_2)PV(K)\) \(d_1 = \frac{1}{\sigma \sqrt{T} }\bigg[ln \bigg(\frac{S_t}{K}\bigg) + \bigg(r + \frac{\sigma^2}{2} \bigg)t \bigg]\) \(d_2 = d_1 - \sigma \sqrt{t}\) \(PV(K) = Ke^{-rt}\) Where,

- $N(\cdot)$ is a C.D.F. of a standard normal distribution
- $S_t$ - Current price of underlying asset
- $K$ - Option strike price
- $t$ - time to option expiry
- $r$ - Interest rate of risk-free asset
- $\sigma$ - Returns volatility of underlying asset price

Prices are almost the same. The difference is insignificant and is only related to the number of simulations executed by `MC_option_pricing`

function. Results from this function will converge to the result of analytical function as number of simulations increases.

We will use `julia_command()`

function to define very similar function but in Julia, which can be used in R.

Alternatively, we can import whole script to R.

Next, we can define variables for both of the functions to compute. The most important will be the number of simulations that a particular function have to perform. This will ultimately determine how computationally expensive function will be.

Script for Julia have dependency, so we should also load a `Distributions`

library.

Now we will use both functions to price put option based on 1000000 simulations, each with 100 rows/observations.

Note that Julia is sensitive to object types and sometimes need to have objects specified in explicit way. (use of as.integer() function).

As before, Monte Carlo method have stochastic properities so the price vary a little. That is why, in order to obtain very precise value, it is neccessery to execute many simulations. According to R the price is 11.993 and Julia said it costs 12.011, a difference of 0.018.

The computation took 18.56 seconds for R and 4.97 seconds for Julia, a difference of 13.59 seconds. Julia was 3.734 times faster.

It might be also interesting how long it takes to execute these functions many times but with fewer simulations. Package `microbenchmark`

enables to do it easily.

As we see in the summary, Julia function is on average 2.653 faster than R (by a median it’s x 2.582). Over 100 repeats, none of R executions was faster than the slowest one of Julia.