Python for Finance: Analyze Big Financial Data

(Elle) #1
”’

from math import log, sqrt
from scipy import stats


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 function


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
”’

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


These are only the basic functions needed to calculate implied volatilities. What we need


as well, of course, are the respective option quotes, in our case for European call options


on the VSTOXX index, and the code that generates the single implied volatilities. We will


see how to do this based on an interactive IPython session.


Let us start with the day from which the quotes are taken; i.e., our t = 0 reference day. This


is March 31, 2014. At this day, the closing value of the index was V 0 = 17.6639 (we


change from S to V to indicate that we are now working with the volatility index):


In  [ 1 ]:  V0  =   17.6639

For the risk-free short rate, we assume a value of r = 0.01 p.a.:


In  [ 2 ]:  r   =   0.01

All other input parameters are given by the options data (i.e., T and K) or have to be


calculated (i.e.,


imp

). The data is stored in a pandas DataFrame object (see Chapter 6) and


saved in a PyTables database file (see Chapter 7). We have to read it from disk into


memory:


In  [ 3 ]:  import pandas as pd
h5 = pd.HDFStore(‘./source/vstoxx_data_31032014.h5’, ‘r’)
futures_data = h5[‘futures_data’] # VSTOXX futures data
options_data = h5[‘options_data’] # VSTOXX call option data
h5.close()

We need the futures data to select a subset of the VSTOXX options given their (forward)


moneyness. Eight futures on the VSTOXX are traded at any time. Their maturities are the

Free download pdf