Step-by-Step Guide: Implementing the Black-Scholes Model in Python

Published: September 24, 2023

In the world of finance, the Black-Scholes-Merton model stands out as a pivotal tool for pricing options. Developed through rigorous mathematical derivations, this model calculates the theoretical value of an option based on five essential parameters:

Underlying Price (S): The current market price of the asset.

Strike Price (K): The predetermined price at which the option can be exercised.

Time to Expiration (T): The time left (in years) until the option’s expiration date.

Risk Free Rate (r): The constant rate of return on a risk-free asset, such as a government bond.

Volatility (σ): A measure of how much the price of the underlying asset fluctuates.

With these components in mind, let’s dive into the implementation in Python:

Step 1: Import Necessary Libraries

Before we begin our calculations, it’s crucial to have the right tools at our disposal. Thus, we’ll import the required Python libraries:

import math
from scipy.stats import norm

Step 2: Define the Variables

Here, we’ll establish the variables based on the parameters mentioned earlier:

S = 45 # Underlying Price
K = 40 # Strike Price
T = 2 # Time to Expiration
r = 0.1 # Risk-Free Rate
vol = 0.1 # Volatility (σ)

Step 3: Calculate d1

With our variables set, we first compute d1, a crucial intermediary value:

To determine the theoretical price of a call option:

C = S * norm.cdf(d1) - K * math.exp(-r * T) * norm.cdf(d2)

Step 6: Calculate Put Option Price

For the put option:

P = K * math.exp(-r * T) * norm.cdf(-d2) - S * norm.cdf(-d1)

Step 7: Print the Results

Lastly, let’s display our results:

print('The value of d1 is: ', round(d1, 4))
print('The value of d2 is: ', round(d2, 4))
print('The price of the call option is: $', round(C, 2))
print('The price of the put option is: $', round(P, 2))

In conclusion, the Black-Scholes-Merton model, while built on complex mathematics, can be easily implemented in Python. With just a few lines of code, you can determine the theoretical price of both call and put options.