-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbsm_functions
33 lines (23 loc) · 965 Bytes
/
bsm_functions
1
from math import log, sqrt, expfrom scipy import statsimport pandas as pdS0 = 100.; K = 105.; T = 1.0; r = 0.05; sigma = 0.2def 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 functiondef 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 functiondef 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##### MONTECARLO ####bsm_call_value(S0, K, T, r, sigma)