Skip to content

Commit

Permalink
Update black_scholes.py
Browse files Browse the repository at this point in the history
Add gamma, theta, rho formulas
  • Loading branch information
jkirkby3 authored Apr 21, 2024
1 parent 62e4ade commit 5d2fd34
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions fypy/pricing/analytical/black_scholes.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,76 @@ def black76_price_strikes(F: float,
return prices


def black76_gamma(F: float,
K: Union[float, np.ndarray],
vol: Union[float, np.ndarray],
disc: float,
T: float) -> Union[float, np.ndarray]:
"""
Gamma(s) for strike(s)
:param F: float, forward price
:param K: float or array, the Strike(s)
:param vol: float or array, the Volatility(ies) ... if float, all strikes get same vol, else a vol smile
:param disc: float, the discount factor, e.g. 0.99
:param T: float, time to maturity of option
:return: float or np.ndarray, same shape as strikes
"""
vol_st = vol * np.sqrt(T)
d_1 = (np.log(F / K) + 0.5 * vol_st ** 2) / vol_st
return disc * norm.pdf(d_1) / (F * vol_st)


def black76_theta(F: float,
K: Union[float, np.ndarray],
is_call: bool,
vol: Union[float, np.ndarray],
disc: float,
T: float) -> Union[float, np.ndarray]:
"""
Theta(s) for strike(s)
:param F: float, forward price
:param K: float or array, the Strike(s)
:param is_call: bool, determines if ALL strikes are call or all are put
:param vol: float or array, the Volatility(ies) ... if float, all strikes get same vol, else a vol smile
:param disc: float, the discount factor, e.g. 0.99
:param T: float, time to maturity of option
:return: float or np.ndarray, same shape as strikes
"""
sqt = np.sqrt(T)
vol_st = vol * sqt
d_1 = (np.log(F / K) + 0.5 * vol_st ** 2) / vol_st
d_2 = d_1 - vol_st

term1 = - F * disc * norm.pdf(d_1) * vol / (2 * sqt)
r = - np.log(disc) / T

phi = 1 if is_call else -1
return term1 - phi * r * K * disc * norm.cdf(phi * d_2) + phi * r * F * disc * norm.cdf(phi * d_1)


def black76_rho(F: float,
K: Union[float, np.ndarray],
is_call: bool,
vol: Union[float, np.ndarray],
disc: float,
T: float) -> Union[float, np.ndarray]:
"""
Rhos(s) for strike(s)
:param F: float, forward price
:param K: float or array, the Strike(s)
:param is_call: bool, determines if ALL strikes are call or all are put
:param vol: float or array, the Volatility(ies) ... if float, all strikes get same vol, else a vol smile
:param disc: float, the discount factor, e.g. 0.99
:param T: float, time to maturity of option
:return: float or np.ndarray, same shape as strikes
"""
vol_st = vol * np.sqrt(T)
d_1 = (np.log(F / K) + 0.5 * vol_st ** 2) / vol_st
d_2 = d_1 - vol_st
phi = 1 if is_call else -1
return -T * disc * phi * (F * norm.cdf(phi * d_1) - K * norm.cdf(phi * d_2))


def black76_vega(F: float,
K: Union[float, np.ndarray],
vol: Union[float, np.ndarray],
Expand Down

0 comments on commit 5d2fd34

Please sign in to comment.