Implementing Monte Carlo pricing with Black-Scholes comparison
Providing Python code that vectorizes Monte Carlo simulation of a European call option with 100k paths using geometric Brownian motion, computes the discounted payoff, calculates the analytical Black-Scholes price, and prints both prices plus the Monte Carlo estimation error.
Here's a self–contained example in Python that uses NumPy to simulate the terminal price of a geometric Brownian motion under the risk–neutral measure, computes the discounted payoff of an at‑the‑money European call, and compares the Monte Carlo estimate for 100 000 paths to the analytical Black–Scholes price:
import numpy as np
from scipy.stats import norm
def black_scholes_call(S0, K, T, r, sigma):
"""Closed-form Black–Scholes price for a European call."""
sqrtT = np.sqrt(T)
d1 = (np.log(S0 / K) + (r + 0.5 * sigma**2) * T) / (sigma * sqrtT)
d2 = d1 - sigma * sqrtT
call_price = S0 * norm.cdf(d1) - K * np.exp(-r * T) * norm.cdf(d2)
return call_price
def monte_carlo_call(S0, K, T, r, sigma, n_paths, seed=None):
"""Monte Carlo estimate of a European call option price."""
if seed is not None:
np.random.seed(seed)
# draw all standard normal variates at once
z = np.random.randn(n_paths)
# simulate terminal prices under risk–neutral GBM
drift = (r - 0.5 * sigma**2) * T
diffusion = sigma * np.sqrt(T) * z
ST = S0 * np.exp(drift + diffusion)
# vectorized payoff computation and discounting
payoffs = np.maximum(ST - K, 0.0)
price = np.exp(-r * T) * payoffs.mean()
# optional: standard error of the estimate
std_error = np.exp(-r * T) * payoffs.std(ddof=1) / np.sqrt(n_paths)
return price, std_error
# Parameters
S0 = 100.0 # initial stock price
K = 100.0 # strike
T = 1.0 # time to maturity in years
r = 0.05 # risk-free rate
sigma = 0.2 # volatility
# Analytical Black–Scholes price
bs_price = black_scholes_call(S0, K, T, r, sigma)
print(f"Black–Scholes price: {bs_price:.4f}")
# Monte Carlo with 100k paths
mc_price, mc_se = monte_carlo_call(S0, K, T, r, sigma, n_paths=100_000, seed=42)
print(f"Monte Carlo price (100k paths): {mc_price:.4f} ± {1.96*mc_se:.4f} (95% CI)")
# If you want to see how the estimate converges as you increase n_paths:
for n in [1_000, 10_000, 100_000]:
mc, se = monte_carlo_call(S0, K, T, r, sigma, n_paths=n, seed=42)
print(f"{n:>6} paths → {mc:.4f} (std error {se:.4f})")
On a typical run, you should see that the Monte Carlo estimate with 100 000 paths is very close to the Black–Scholes benchmark, and that the standard error shrinks like (1/\sqrt{N}) as you increase the number of simulated paths. The use of NumPy arrays and vectorized operations avoids explicit Python loops and keeps the simulation fast.