Python for Finance: Analyze Big Financial Data

(Elle) #1

volatility smile, which is most pronounced for the shortest maturity and which becomes a


bit less pronounced for the longer maturities:


In  [ 15 ]: import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(figsize=( 8 , 6 ))
for maturity in maturities:
data = plot_data[options_data.MATURITY == maturity]
# select data for this maturity
plt.plot(data[‘STRIKE’], data[‘IMP_VOL’],
label=maturity.date(), lw=1.5)
plt.plot(data[‘STRIKE’], data[‘IMP_VOL’], ‘r.’)
plt.grid(True)
plt.xlabel(‘strike’)
plt.ylabel(‘implied volatility of volatility’)
plt.legend()
plt.show()

Figure 3-1. Implied volatilities (of volatility) for European call options on the VSTOXX on March 31, 2014

To conclude this example, we want to show another strength of pandas: namely, for


working with hierarchically indexed data sets. The DataFrame object options_data has an


integer index, which we have used in several places. However, this index is not really


meaningful — it is “just” a number. The option quotes for the day March 31, 2014 are


uniquely described (“identified”) by a combination of the maturity and the strike — i.e.,


there is only one call option per maturity and strike.


The groupby method can be used to capitalize on this insight and to get a more meaningful


index. To this end, we group by MATURITY first and then by the STRIKE. We only want to


keep the PRICE and IMP_VOL columns:


In  [ 16 ]: keep    =   [‘PRICE’,   ‘IMP_VOL’]
group_data = plot_data.groupby([‘MATURITY’, ‘STRIKE’])[keep]
group_data
Out[16]: <pandas.core.groupby.DataFrameGroupBy object at 0x7faf483d5710>

The operation returns a DataFrameGroupBy object.


[ 14 ]

To get to the data, we need to apply


an aggregation operation on the object, like taking the sum. Taking the sum yields the


single data point since there is only one data element in every group:


In  [ 17 ]: group_data  =   group_data.sum()
Free download pdf