-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions
37 lines (33 loc) · 1.29 KB
/
functions
1
#### print_statistics ####def print_statistics(a1, a2): sta1 = scs.describe(a1) sta2 = scs.describe(a2) print ('size', sta1[0], sta2[0]) print ('min', sta1[1][0], sta2[1][0]) print ('max', sta1[1][1], sta2[1][1]) print ('mean', sta1[2], sta2[2]) print ('std', np.sqrt(sta1[3]), np.sqrt(sta2[3])) print ('skew', sta1[4], sta2[4]) print ('kurtosis', sta1[5], sta2[5])#### BSM functions ####from math import log, sqrt, expfrom scipy import statsimport pandas as pd#### Call function ####def bsm_call_value(S0, K, T, r, sigma): S0 = float(S0) d1 = (log(S0 / K) + (r + 0.5 * sigma ** 2) * T) / (sigma * sqrt(T)) d2 = (log(S0 / K) + (r - 0.5 * sigma ** 2) * T) / (sigma * sqrt(T)) value = (S0 * stats.norm.cdf(d1, 0.0, 1.0) - K * exp(-r * T) * stats.norm.cdf(d2, 0.0, 1.0)) return value#### Vega function ####def bsm_vega(S0, K, T, r, sigma): S0 = float(S0) d1 = (log(S0 / K) + (r + 0.5 * sigma ** 2) * T) / (sigma * sqrt(T)) vega = S0 * stats.norm.cdf(d1, 0.0, 1.0) * sqrt(T) return vega#### Implied volatility function ####def bsm_call_imp_vol(S0, K, T, r, C0, sigma_est, it=100): for i in range(it): sigma_est -= ((bsm_call_value(S0, K, T, r, sigma_est) - C0) / bsm_vega(S0, K, T, r, sigma_est)) return sigma_est