How to compute the Black Scholes model in Python

7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

The Black-Scholes model is a widely used model for pricing European call and put options. In Python, you can compute the Black-Scholes model by using the following formula:

import math

def black_scholes(S, K, T, r, sigma, option_type):
    d1 = (math.log(S / K) + (r + 0.5 * sigma**2) * T) / (sigma * math.sqrt(T))
    d2 = d1 - sigma * math.sqrt(T)
    if option_type == "call":
        return S * math.norm.cdf(d1) - K * math.exp(-r * T) * math.norm.cdf(d2)
    else:  # option_type == "put"
        return K * math.exp(-r * T) * math.norm.cdf(-d2) - S * math.norm.cdf(-d1)

The black_scholes function takes the following parameters:

  • S: the current price of the underlying asset
  • K: the strike price of the option
  • T: the time to expiration of the option in years
  • r: the risk-free interest rate
  • sigma: the volatility of the underlying asset
  • option_type: the type of option, either "call" or "put"

The function returns the price of the European call or put option based on the Black-Scholes model. The cumulative normal distribution function math.norm.cdf is used to calculate the probabilities associated with the standard normal distribution.

7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Free Newsletter

Master Data Science in Days, Not Months 🚀

Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚

Weekly simple and practical lessons
Access to ready to use code examples
Skip the math, focus on results
Learn while drinking your coffee

By subscribing, you agree to receive our newsletter. You can unsubscribe at any time.