ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Monte Carlo Simulation for Option Pricing (Black-Scholes)

MATLAB

Goal -- WPM

Ready
Exercise Algorithm Area
1function option_price = monteCarloOptionPricing(S0, K, T, r, sigma, num_simulations)
2% Monte Carlo simulation to price a European call option using Black-Scholes.
3% S0: Initial stock price
4% K: Strike price
5% T: Time to expiration (in years)
6% r: Risk-free interest rate (annual)
7% sigma: Volatility of the stock price (annual)
8% num_simulations: Number of Monte Carlo paths to simulate
9
10% Input validation
11if nargin ~= 6
12error('monteCarloOptionPricing:IncorrectNumberOfArguments', 'Requires 6 input arguments: S0, K, T, r, sigma, num_simulations.');
13end
14if S0 <= 0 || K <= 0 || T <= 0 || sigma <= 0 || num_simulations <= 0
15error('monteCarloOptionPricing:InvalidInput', 'All input parameters (S0, K, T, sigma, num_simulations) must be positive.');
16end
17if r < -1 || r > 1
18warning('monteCarloOptionPricing:RateOutOfRange', 'Risk-free rate r is outside typical range [-1, 1].');
19end
20
21% Generate random numbers for stock price movements
22% We need num_simulations random numbers from a standard normal distribution.
23% The formula for the stock price at expiration is S_T = S0 * exp((r - 0.5*sigma^2)*T + sigma*sqrt(T)*Z)
24% where Z is a standard normal random variable.
25
26Z = randn(num_simulations, 1);
27
28% Calculate the stock price at expiration for each simulation
29ST = S0 * exp((r - 0.5 * sigma^2) * T + sigma * sqrt(T) * Z);
30
31% Calculate the payoff for each simulation
32% Payoff for a European call option is max(ST - K, 0)
33payoffs = max(ST - K, 0);
34
35% Calculate the average payoff
36average_payoff = mean(payoffs);
37
38% Discount the average payoff back to present value
39option_price = exp(-r * T) * average_payoff;
40
41% Handle cases where average_payoff might be NaN if num_simulations is very small or inputs are extreme.
42if isnan(option_price)
43warning('monteCarloOptionPricing:NaNResult', 'Resulting option price is NaN. Check input parameters or number of simulations.');
44option_price = 0; % Default to 0 if NaN
45end
46
47end
Algorithm description viewbox

Monte Carlo Simulation for Option Pricing (Black-Scholes)

Algorithm description:

This MATLAB function estimates the price of a European call option using a Monte Carlo simulation based on the Black-Scholes model. It generates a large number of random stock price paths until expiration, calculates the option's payoff for each path, and then averages these payoffs. The average payoff is discounted back to the present value using the risk-free interest rate to obtain the option's theoretical price. Monte Carlo methods are powerful for pricing complex derivatives where analytical solutions are difficult or impossible to derive.

Algorithm explanation:

The Monte Carlo simulation for option pricing relies on simulating the underlying asset's price movement using a stochastic differential equation, typically the Geometric Brownian Motion (GBM) as described by the Black-Scholes model. The price of the stock at expiration `ST` is modeled as `ST = S0 * exp((r - 0.5*sigma^2)*T + sigma*sqrt(T)*Z)`, where `Z` is a standard normal random variable representing the random shock. The function generates `num_simulations` such random variables, calculates the corresponding `ST` for each, and then computes the payoff of a European call option, which is `max(ST - K, 0)`. The average of these payoffs, when discounted back to the present value by `exp(-r*T)`, provides an estimate of the option's fair price. The accuracy of the estimate increases with `num_simulations`. Time complexity is O(N), where N is `num_simulations`, due to the generation of random numbers and payoff calculations. Space complexity is O(N) to store the simulated prices and payoffs.

Pseudocode:

function monteCarloOptionPricing(S0, K, T, r, sigma, num_simulations):
  validate input parameters (positive values, correct number)
  
  generate `num_simulations` random numbers `Z` from standard normal distribution
  
  initialize payoffs array
  
  for each Z_i in Z:
    calculate stock price at expiration: ST_i = S0 * exp((r - 0.5*sigma^2)*T + sigma*sqrt(T)*Z_i)
    calculate payoff for this path: payoff_i = max(ST_i - K, 0)
    store payoff_i
    
  calculate average_payoff = mean(payoffs)
  
  calculate option_price = exp(-r * T) * average_payoff
  
  if option_price is NaN, return 0 with warning
  
  return option_price