from math import log, sqrt, exp
from scipy import stats
def bsm_call_value(S0, K, T, r, sigma):
''' Valuation of European call option in BSM model.
Analytical formula.
Parameters
==========
S0: float
initial stock/index level
K: float
strike price
T: float
maturity date (in year fractions)
r: float
constant risk-free short rate
sigma: float
volatility factor in diffusion term
Returns
=======
value: float
present value of the European call option
'''
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))
# stats.norm.cdf --> cumulative distribution function
# for normal distribution
value = (S0 * stats.norm.cdf(d1, 0.0, 1.0) -
K * exp(-r * T) * stats.norm.cdf(d2, 0.0, 1.0))
return value
def bsm_vega(S0, K, T, r, sigma):
''' Vega of European option in BSM model.
Parameters
==========
S0: float
initial stock/index level
K: float
strike price
T: float
maturity date (in year fractions)
r: float
constant risk-free short rate
sigma: float
volatility factor in diffusion term
Returns
=======
vega: float
partial derivative of BSM formula with respect
to sigma, i.e. Vega
'''
from math import log, sqrt
from scipy import stats
'''
YOUR CODE HERE
'''
return
def bsm_call_imp_vol(S0, K, T, r, C0, sigma_est, it=100):
''' Implied volatility of European call option in BSM model.
Parameters
==========
S0: float
initial stock/index level
K: float
strike price
T: float
maturity date (in year fractions)
r: float
constant risk-free short rate
sigma_est: float
estimate of impl. volatility
it: integer
number of iterations
Returns
=======
simga_est: float
numerically estimated implied volatility
'''
'''
YOUR CODE HERE
'''
return
# Parameters
S0= 500
K = 500
T = 2
r = 0
sigma_est = 0.9
C0= 50
it = 100
imp_vol = bsm_call_imp_vol(S0, K, T, r, C0, sigma_est, it)
fig = go.Figure(data=[go.Surface(z=sigma_vals, x=T_vals*365, y=K_vals)])
fig.update_layout(title='Volatility Surface', autosize=False,
width=500, height=500,
margin=dict(l=65, r=50, b=65, t=90))
fig.update_layout(scene = dict(
xaxis_title='Days to Expiry',
yaxis_title='Strike Price',
zaxis_title='Implied Volatility'))
fig.show()