-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBS Function
43 lines (34 loc) · 1.33 KB
/
BS Function
1
### Valuation of European call options in Black-Scholes-Merton model# incl. Vega function and implied volatility estimation# bsm_functions.py## Analytical Black-Scholes-Merton (BSM) Formula#Parameters#==========S0 = 100 #initial stock/index levelK = 105 #strike priceT = 1 #maturity date (in year fractions)r = 0.05 #constant risk-free short ratesigma = 0.2 #volatility factor in diffusion termOtype='C'import numpy as npimport scipy.stats as ssimport time #Black and Scholesdef d1(S0, K, r, sigma, T): return (np.log(S0/K) + (r + sigma**2 / 2) * T)/(sigma * np.sqrt(T)) def d2(S0, K, r, sigma, T): return (np.log(S0 / K) + (r - sigma**2 / 2) * T) / (sigma * np.sqrt(T))def BlackScholes(type,S0, K, r, sigma, T): if type=="C": return S0 * ss.norm.cdf(d1(S0, K, r, sigma, T)) - K * np.exp(-r * T) * ss.norm.cdf(d2(S0, K, r, sigma, T)) else: return K * np.exp(-r * T) * ss.norm.cdf(-d2(S0, K, r, sigma, T)) - S0 * ss.norm.cdf(-d1(S0, K, r, sigma, T))print ("S0\tstock price at time 0:", S0)print ("K\tstrike price:", K)print ("r\tcontinuously compounded risk-free rate:", r)print ("sigma\tvolatility of the stock price per year:", sigma)print ("T\ttime to maturity in trading years:", T)c_BS = BlackScholes(Otype,S0, K, r, sigma, T)print ("c_BS\tBlack-Scholes price:", c_BS)