Skip to content

Instantly share code, notes, and snippets.

@ycytai
Last active December 8, 2023 13:37
Show Gist options
  • Save ycytai/9dbdf46921ab4efa96b41701c221e7bd to your computer and use it in GitHub Desktop.
Save ycytai/9dbdf46921ab4efa96b41701c221e7bd to your computer and use it in GitHub Desktop.
import scipy.stats as st
import numpy as np
def black_scholes_pricing_model(
option_type: str,
s: float,
k: float,
r: float,
v: float,
q: float,
t: float
) -> float:
"""
Parameters:
option_type: option type, use 'call' or 'put'
s: spot price
k: strike price
r: interest rate
v: volatility
q: yield rate
t: maturity(year)
"""
d1 = (np.log(s / k) + (r - q + v**2 / 2) * t) / (v * t**0.5)
d2 = d1 - v * t**0.5
call = s * st.norm.cdf(d1) - k * np.exp(-r * t) * st.norm.cdf(d2)
put = k * np.exp(-r * t) * st.norm.cdf(-d2) - s * st.norm.cdf(-d1)
if option_type == "call":
return max(call, 0)
elif option_type == "put":
return max(put, 0)
else:
raise ValueError("Wrong option input.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment