設定引數
r=0.032 # risk-free interest rate
t=float(30)/365 # time to expire (30 days)
q=0 # dividend yield
S0=2.3 # underlying price
X=2.2 # strike price
mktprice=0.18 # market price
# 用二分法求implied volatility,暫只針對call option
sigma=0.3 # initial volatility
C=P=0
upper=1
lower=0
while abs(C-mktprice)>1e-6:
d1=(np.log(S0/X)+(r-q+sigma**2/2)*t)/(sigma*np.sqrt(t))
d2=d1-sigma*np.sqrt(t)
C=S0*np.exp(-q*t)*norm.cdf(d1)-X*np.exp(-r*t)*norm.cdf(d2)
P=X*np.exp(-r*t)*norm.cdf(-d2)-S0*np.exp(-q*t)*norm.cdf(-d1)
if C-mktprice>0:
upper=sigma
sigma=(sigma+lower)/2
else:
lower=sigma
sigma=(sigma+upper)/2
print sigma # implied volatility
設定引數
r=0.032 # risk-free interest rate
t=float(30)/365 # time to expire (30 days)
q=0 # dividend yield
S0=2.3 # underlying price
X=2.2 # strike price
mktprice=0.18 # market price
# 用二分法求implied volatility,暫只針對call option
sigma=0.3 # initial volatility
C=P=0
upper=1
lower=0
while abs(C-mktprice)>1e-6:
d1=(np.log(S0/X)+(r-q+sigma**2/2)*t)/(sigma*np.sqrt(t))
d2=d1-sigma*np.sqrt(t)
C=S0*np.exp(-q*t)*norm.cdf(d1)-X*np.exp(-r*t)*norm.cdf(d2)
P=X*np.exp(-r*t)*norm.cdf(-d2)-S0*np.exp(-q*t)*norm.cdf(-d1)
if C-mktprice>0:
upper=sigma
sigma=(sigma+lower)/2
else:
lower=sigma
sigma=(sigma+upper)/2
print sigma # implied volatility