1 from math import log,sqrt,exp 2 from scipy import stats 3 4 def bsm_call_value(S0,K,T,r,sigma): 5 S0 = float(S0) 6 d1 = (log(S0 / K) + (r + 0.5 * sigma ** 2) * T) / (sigma * sqrt(T)) 7 d2 = (log(S0 / K) + (r - 0.5 * sigma ** 2) * T) / (sigma * sqrt(T)) 8 value = (S0 * stats.norm.cdf(d1,0.0,1.0) - K * exp(-r * T) * stats.norm.cdf(d2,0.0,1.0)) 9 print value 10 return value 11 12 def bsm_vega(S0,K,T,r,sigma): 13 S0 = float(S0) 14 d1 = (log(S0 / K) + (r + 0.5 * sigma ** 2) * T) / (sigma * sqrt(T)) 15 vega = S0 * stats.norm.cdf(d1,0.0,1.0) * sqrt(T) 16 return vega 17 18 def bsm_call_imp_vol(S0,K,T,r,C0,sigma_est,it = 100): 19 for i in range(it): 20 sigma_est -= ((bsm_call_value(S0,K,T,r,sigma_est) - C0) / bsm_vega(S0,K,T,r,sigma_est)) 21 print sigma_est 22 return sigma_est 23 24 S0 = 100 25 K = 105 26 T = 1.0 27 r = 0.05 28 sigma = 0.2 29 bsm_call_value(S0,K,T,r,sigma)