## Monte Carlo Simulation – R Code

This is a simple R code for pricing European call and put options using Monte Carlo simulation. According to the law of large numbers, as the number of simulations increase , the actual ratio will converge to the expected ratio of outcomes. In this case, for a large number of simulations the concluded prices will converge to the values generated by the Black-Scholes closed-form solution.

## European call / put option

# clear environment and console
rm(list = ls())
cat("\014")

# inputs
S = 10 # stock price
K = 10 # strike price
sigma = 0.2 # volatility
t = 1.5 # number of years
r = 0.02 # risk-free rate
d = 0 # expected dividend yield
n = 1000 # number of simulations
Dt = 1/252 # frequency; 1/252 is daily
h = t / Dt

# simulate stock prices using MC
ones = matrix(data = 1, nrow = n, ncol = 1)
rand = matrix(data = rnorm(n * h), nrow = n, ncol = h)
expon= exp((r - 0.5 * sigma ^ 2) * Dt + sigma * sqrt(Dt) * rand)
St = S * cbind(ones, t(apply(expon, 1, cumprod)))

# calculate call price
c = mean(pmax(0, St[, h + 1] - K))
c

# calculate put price
p = mean(pmax(0, K - St[, h + 1]))
p