Implement a Monte Carlo simulation for option pricing.
- User
Implement a Monte Carlo simulation for option pricing.
- Assistant
Preparing code for user
I need to consider how to create a multi-step path for the user's request, but since they didn't ask for it explicitly, I should keep my explanation brief and focus on the core request. Since "implement" suggests that code is necessary, opting for Python makes sense. I'll outline a formula and include a straightforward Numpy-based Monte Carlo example, both naive and vectorized. It might include standard error and confidence intervals, ensuring clarity without overcomplicating things in my final response. Let's get that ready! Here’s a straightforward Monte Carlo implementation in Python for pricing a European call/put option under the Black–Scholes (GBM) model.
Model
Under the risk‑neutral measure, the terminal price ( S_T ) is
[ S_T = S_0 \exp\left[\left(r - \frac{1}{2}\sigma^2\right)T + \sigma \sqrt{T} Z \right], \quad Z \sim N(0,1). ]
Payoff:
- Call: ( \max(S_T - K, 0) )
- Put: ( \max(K - S_T, 0) )
Price:
[ \text{Price} = e^{-rT} \cdot \mathbb{E}[\text{payoff}] ]
Python Implementation (Vectorized)
import numpy as np def mc_european_option( S0: float, K: float, T: float, r: float, sigma: float, n_paths: int = 100_000, option_type: str = "call", antithetic: bool = False, seed: int | None = None, ): """ Monte Carlo price for a European call or put under Black–Scholes. Parameters ---------- S0 : float Initial underlying price. K : float Strike price. T : float Time to maturity (in years). r : float Risk-free continuously compounded rate. sigma : float Volatility of the underlying. n_paths : int Number of Monte Carlo paths. option_type : {"call", "put"} Type of option to price. antithetic : bool If True, use antithetic variates for variance reduction. seed : int or None Random seed for reproducibility. Returns ------- price : float Estimated option price. std_err : float Standard error of the estimate. """ if seed is not None: rng = np.random.default_rng(seed) else: rng = np.random.default_rng() if antithetic: # Generate half as many normals, then use ±Z m = n_paths // 2 Z = rng.standard_normal(m) Z = np.concatenate([Z, -Z]) if Z.size < n_paths: # guard in case n_paths is odd Z = np.concatenate([Z, rng.standard_normal(1)]) else: Z = rng.standard_normal(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) # Compute payoff if option_type.lower() == "call": payoff = np.maximum(ST - K, 0.0) elif option_type.lower() == "put": payoff = np.maximum(K - ST, 0.0) else: raise ValueError("option_type must be 'call' or 'put'.") # Discounted mean payoff discounted_payoff = np.exp(-r * T) * payoff price = discounted_payoff.mean() std_err = discounted_payoff.std(ddof=1) / np.sqrt(n_paths) return price, std_err # Example usage if __name__ == "__main__": S0 = 100.0 # spot K = 100.0 # strike T = 1.0 # 1 year r = 0.05 # 5% risk-free rate sigma = 0.2 # 20% volatility price, err = mc_european_option(S0, K, T, r, sigma, n_paths=200_000, option_type="call", antithetic=True, seed=42) print(f"MC Price: {price:.4f} ± {1.96*err:.4f} (95% CI)")This gives you:
- A Monte Carlo estimator for the price.
- A standard error and 95% confidence interval estimate.
If you tell me your preferred language (e.g., C++, Java, MATLAB) or want path‑dependent options (Asian, barrier), I can adapt the implementation.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.