Implementing the Black-Scholes Model with Python
I’ll provide you with a financial mathematics example and solve it using $Python$, including proper mathematical notation and visualization.
1 | import numpy as np |
Black-Scholes Option Pricing Model in Financial Mathematics
In this example, I’ll solve and analyze a classic financial mathematics problem: option pricing using the Black-Scholes model.
The Black-Scholes model is fundamental in financial mathematics for pricing European-style options.
Mathematical Background
The Black-Scholes formula for European call and put options is given by:
For a call option:
$$C(S,t) = S \cdot N(d_1) - K e^{-r(T-t)} \cdot N(d_2)$$
For a put option:
$$P(S,t) = K e^{-r(T-t)} \cdot N(-d_2) - S \cdot N(-d_1)$$
Where:
$$d_1 = \frac{\ln(S/K) + (r + \sigma^2/2)(T-t)}{\sigma\sqrt{T-t}}$$
$$d_2 = d_1 - \sigma\sqrt{T-t}$$
And:
- $S$ is the current stock price
- $K$ is the strike price
- $r$ is the risk-free interest rate
- $T-t$ is the time to maturity
- $\sigma$ is the volatility of the stock
- $N(·)$ is the cumulative distribution function of the standard normal distribution
Code Explanation
The code implements the Black-Scholes model and provides three detailed examples:
- 3D Visualization of Option Prices: Shows how call option prices change with stock price and time to maturity
- Option Greeks Analysis: Calculates and visualizes Delta, Gamma, and Theta
- Monte Carlo Simulation: Prices options using simulation and compares with the analytical solution
Key Functions:
black_scholes()
: Implements the analytical Black-Scholes formulabs_delta()
,bs_gamma()
,bs_theta()
: Calculate option Greeksmonte_carlo_option_pricing()
: Simulates stock price paths using geometric Brownian motion
Visualization Analysis
1. 3D Surface Plot of Call Option Prices
The 3D plot shows how the call option price (z-axis) varies with:
- Stock price (x-axis): As stock price increases, call option value increases
- Time to maturity (y-axis): The time value of the option is visible
This visualization helps understand the non-linear relationship between these variables.
2. Option Greeks Visualization
The Greeks represent sensitivities of option prices to various factors:
Delta: Measures the rate of change of option price with respect to changes in the underlying asset’s price
- Call delta ranges from $0$ to $1$
- Put delta ranges from $-1$ to $0$
- At-the-money options have delta near $0.5$ (calls) or $-0.5$ (puts)
Gamma: Measures the rate of change of delta with respect to changes in the underlying price
- Highest at-the-money and decreases as the option moves in or out of the money
- Same for both calls and puts
Theta: Measures the rate of change of option price with respect to the passage of time (time decay)
- Generally negative for both calls and puts (options lose value as time passes)
- Most significant for at-the-money options
3. Monte Carlo Simulation
Monte Carlo Call Option Price: $6.8874 Black-Scholes Call Option Price: $6.8887 Difference: $0.0013
The histogram shows the distribution of simulated stock prices at expiration:
- The red curve represents the theoretical lognormal distribution
- The vertical lines mark the initial stock price and strike price
- The simulation validates the Black-Scholes model by producing a similar price
When we compare the Monte Carlo estimate with the analytical Black-Scholes solution, they should be very close, with differences attributable to simulation error.
Conclusion
This example demonstrates several fundamental concepts in financial mathematics:
- Analytical solution of the Black-Scholes equation
- Option Greeks for risk management
- Monte Carlo methods for pricing financial instruments
The visualizations help develop intuition about option pricing behavior and the relationships between different variables in the model.