## MC Simulation – Python Code

This is a simple Python 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

import numpy as np
#import pandas as pd

# inputs
S0 = 10 # stock price
K = 10 # strike price
sigma = 0.2 # volatility
t = 0.5 # number of years
r = 0.02 # risk-free rate
n = 1000 # number of simulations
Dt = 1/252 # frequency; 1/252 is daily
h = int(t / Dt) # number of simulated periods

# simulate stock prices using MC
rnd = np.array([[0.0 for x in range(h + 1)] for y in range(n)])
for j in range(0, h + 1, 1):
rnd[:, j] = np.random.normal(0, 1, n)

S = np.array([[0.0 for x in range(h + 1)] for y in range(n)])
S[:, 0] = S0
for i in range(0, n, 1):
for j in range(1, h + 1, 1):
S[i, j] = S[i, j - 1] * np.exp((r - 0.5 * sigma ** 2) * Dt + sigma * rnd[i, j - 1] * np.sqrt(Dt))

# calculate call price
c = np.mean(np.maximum(0, S[:, h - 1] - K))

# calculate put price
p = np.mean(np.maximum(0, K - S[:, h - 1]))
```