Python has some nice packages such as numpy, scipy, and matplotlib for numerical computing and data visualization. Another package that deserves a mention that we have seen increasingly is Python’s pandas library. Pandas has fast and efficient data analysis tools to store and process large amounts of data. All of these packages can easily be integrated with the NAG Library for Python.
Below is an example which uses the NAG Library for Python and the pandas library to calculate the implied volatility of options prices. The code below can be downloaded to calculate your own implied volatility surface for data on the Chicago Board of Options Exchange website. Full NAG Library product trials are available – see the link at the end of the blog.
The famous Black Scholes formula for pricing a Call/Put option on a stock is a function of 6 variables: the underlying price, the interest rate, the dividend rate, the strike price, the time-to-expiration, and the volatility. Note that for a given option contract we can observe the underlying price, interest rate, and dividend rate. In addition, the options contract specifies the strike price and time-to-expiration.
Thus the one variable we have to tweak is the volatility. We can then ask the question: For what volatility* does the Black Scholes equation price equal the market price.
F(volatility*)=Market Option Price
This volatility* is then denoted as the implied volatility observed in the market. We can use the NAG routine opt_imp_vol to compute implied volatilities for arrays of input data.
This routine was introduced at Mark 27.1 and gives the user a choice of two algorithms. The first is the method of Jäckel (2015), which uses a third order Householder method to achieve close to machine accuracy for all but the most extreme inputs. This method is fast for short vectors of input data.
The second algorithm is based on that of Glau et al. (2018), with additional performance enhancements developed in a very productive collaboration between NAG and Dr Kathrin Glau and Dr Linus Wunderlich at Queen Mary University of London. This method uses Chebyshev interpolation and is designed for long vectors of input data, where SIMD vector instructions can be exploited to improve performance. For applications in which accuracy to machine precision is not required, the algorithm can also be instructed to aim for accuracy to roughly single precision (approximately seven decimal places), giving even further performance improvements.
Once we have computed the implied volatilities, we can then use dim2_cheb_lines to perform a least squares Chebyshev fit to the volatility surface, and dim2_cheb_eval to evaluate the surface at intermediate points.
The program uses the pandas package to easily store and manipulate the data via DataFrames. In the script, you’ll notice pandas takes care of the many data processing functions and then calls the NAG Library for more complex analysis. The program will automatically read in the options data, calculate implied volatility for the call and put options, and plot the volatility curves and surface. The above code can be run as follows (given that you have pandas, matplotlib, and the NAG Library for Python):
python implied_volatility.py QuoteData.dat
I ran the program on Apple Options data (SYM AAPL). Below is the output, generated in about two seconds:
Implied Volatility for AAPL (APPLE INC),546.0255,-4.7445, Dec 19 2013 @ 15:02 ET,Bid,545.97,Ask,546.09,Size,2×1,Vol,8897300,
Number of data points found: 17905
Calculating Implied Vol of Calls…
Calculating Implied Vol of Puts…
Plotting Volatility Curves/Surface
In the picture (right) we see some nice volatility skews/smiles that level out as the option expiration date increases. This reflects the fact that real option prices do not follow the Black Scholes model. Especially for short maturities, real prices can display far more variability than the Black Scholes model assumes. Hence when we compute implied volatility for real options data, we see pronounced curvature for short dated options, and flatter surfaces for longer dated options where the Black Scholes model is a better fit.
Below is the volatility surface (plotting Strike, Expiration, and Implied Volatility from Figure 1 on the same graph).
So go ahead, download any options data the CBOE provides (make sure you download data during CBOE trading hours to ensure the graphs are not null) and plot your own volatility surface! Many thanks to John Morrissey and Chris Seymour for code suggestions. Learn more about Implied Volatilities in the NAG Library.