{"id":1442,"date":"2021-04-09T13:14:00","date_gmt":"2021-04-09T13:14:00","guid":{"rendered":"https:\/\/nag.com\/?post_type=insights&#038;p=1216"},"modified":"2023-08-03T16:50:50","modified_gmt":"2023-08-03T16:50:50","slug":"fast-implied-volatility-using-pythons-pandas-library-and-chebyshev-interpolation","status":"publish","type":"insights","link":"https:\/\/nag.com\/insights\/fast-implied-volatility-using-pythons-pandas-library-and-chebyshev-interpolation\/","title":{"rendered":"Fast Implied Volatility using Python&#8217;s Pandas Library and Chebyshev Interpolation"},"content":{"rendered":"<div class=\"container content-area-default \">\n    <div class=\"row justify-content--center\">\n        <div class=\"col-12 col-md-10 col-lg-8 col-xl-6\">\n            <p>Python has some nice packages such as\u00a0<a href=\"http:\/\/www.numpy.org\/\">numpy<\/a>,\u00a0<a href=\"http:\/\/scipy.org\/\">scipy<\/a>, and\u00a0<a href=\"http:\/\/matplotlib.org\/\">matplotlib<\/a>\u00a0for numerical computing and data visualization. Another package that deserves a mention that we have seen increasingly is Python&#8217;s\u00a0<a href=\"http:\/\/pandas.pydata.org\/\">pandas library<\/a>. 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\u00a0<a title=\"NAG Library for Python\" href=\"https:\/\/www.nag.com\/content\/nag-library-python\"><span class=\"nag-n-override\" style=\"margin-left: 0 !important;\"><i>n<\/i><\/span>AG Library for Python<\/a>.<\/p>\n<p>Below is an example which uses the\u00a0<a title=\"NAG Library for Python\" href=\"https:\/\/www.nag.com\/content\/nag-library-python\"><span class=\"nag-n-override\" style=\"margin-left: 0 !important;\"><i>n<\/i><\/span>AG Library for Python<\/a>\u00a0and 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 <span class=\"nag-n-override\" style=\"margin-left: 0 !important;\"><i>n<\/i><\/span>AG Library product trials are available &#8211; see the link at the end of the blog.\u00a0 \u00a0<\/p>\n<h3>Background on Implied Volatility<\/h3>\n<p>The famous\u00a0<a href=\"http:\/\/en.wikipedia.org\/wiki\/Black%E2%80%93Scholes\">Black Scholes<\/a>\u00a0formula 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.<\/p>\n<p>Thus the one variable we have to tweak is the volatility. We can then ask the question: For what\u00a0<b>volatility<\/b>* does the Black Scholes equation price equal the market price.<\/p>\n<p style=\"text-align: center;\"><b>F(volatility*)=Market Option Price<\/b><\/p>\n<p>This\u00a0<b>volatility*<\/b>\u00a0is then denoted as the\u00a0<a href=\"http:\/\/en.wikipedia.org\/wiki\/Implied_volatility\">implied volatility<\/a>\u00a0observed in the market. We can use the <span class=\"nag-n-override\" style=\"margin-left: 0 !important;\"><i>n<\/i><\/span>AG routine opt_imp_vol to compute implied volatilities for arrays of input data.<\/p>\n<p>This routine was introduced at Mark 27.1\u00a0and gives the user a choice of two algorithms. The first is the method of J\u00e4ckel (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.<\/p>\n<p>The second algorithm is based on that of Glau et al. (2018), with additional performance enhancements developed in a very productive collaboration between <span class=\"nag-n-override\" style=\"margin-left: 0 !important;\"><i>n<\/i><\/span>AG 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.<\/p>\n<p>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.<\/p>\n<h3>Running the Script<\/h3>\n<p>We have created an\u00a0<a href=\"https:\/\/github.com\/numericalalgorithmsgroup\/implied-vol\/blob\/master\/scripts\/implied_volatility.py\">example script<\/a>\u00a0that will run on data from the Chicago Board of Options Exchange (CBOE) website. The CBOE provides options data in downloadable format\u00a0<a href=\"http:\/\/www.cboe.com\/delayedquote\/QuoteTableDownload.aspx\">here<\/a>.<\/p>\n<p>The program uses the pandas package to easily store and manipulate the data via DataFrames. In the script, you&#8217;ll notice pandas takes care of the many data processing functions and then calls the <span class=\"nag-n-override\" style=\"margin-left: 0 !important;\"><i>n<\/i><\/span>AG 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 <span class=\"nag-n-override\" style=\"margin-left: 0 !important;\"><i>n<\/i><\/span>AG Library for Python):<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1218 size-full\" src=\"https:\/\/nag.com\/wp-content\/uploads\/2023\/05\/impl-vol-aapl_0-1.png\" alt=\"Implied Volatility\" width=\"780\" height=\"600\" \/><\/p>\n<p>python implied_volatility.py QuoteData.dat<\/p>\n<p>I ran the program on Apple Options data (SYM AAPL). Below is the output, generated in about two seconds:<\/p>\n<p>Implied Volatility for AAPL (APPLE INC),546.0255,-4.7445, Dec 19 2013 @ 15:02 ET,Bid,545.97,Ask,546.09,Size,2&#215;1,Vol,8897300,<\/p>\n<p>Number of data points found: 17905<\/p>\n<p>Calculating Implied Vol of Calls&#8230;<\/p>\n<p>Calculating Implied Vol of Puts&#8230;<\/p>\n<p>Plotting Volatility Curves\/Surface<\/p>\n<p>In the picture (right) we see some nice volatility skews\/smiles that level out as the option expiration date increases.\u00a0<strong>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<\/strong>.<\/p>\n<p>Below is the volatility surface (plotting Strike, Expiration, and Implied Volatility from Figure 1 on the same graph).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1219 size-full\" src=\"https:\/\/nag.com\/wp-content\/uploads\/2023\/05\/figure_2-1.png\" alt=\"Implied Volatility Surface\" width=\"893\" height=\"584\" \/><\/p>\n<p>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\u00a0<a href=\"https:\/\/www.nag.com\/content\/fast-implied-volatilities-nag-library\">Implied Volatilities in the <span class=\"nag-n-override\" style=\"margin-left: 0 !important;\"><i>n<\/i><\/span>AG Library<\/a>.<\/p>\n<div class=\"paragraph--color--transparent paragraph--alignment--left paragraph paragraph--type--text paragraph--view-mode--default\">\n<div class=\"field field--name-field-paragraph-text field--type-text-long field--label-hidden field--item\">\n<div class=\"tex2jax_process\">\u00a0<\/div>\n<\/div>\n<\/div>\n<div class=\"paragraph--color--transparent paragraph--align-- row bs-2col paragraph paragraph--type-text-with-media paragraph--view-mode-default\">\n<div class=\"col-sm-6 bs-region bs-region--left\">\n<div class=\"field field--name-field-paragraph-text field--type-text-long field--label-hidden field--item\">\n<div class=\"tex2jax_process\">\n<p>\u00a0<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n        <\/div>\n    <\/div>\n<\/div>\n\n\n<div class=\"gbc-title-banner tac tac-lg tac-xl\" style='border-radius: 0px; '>\n    <div class=\"container\" style='border-radius: 0px; '>\n        <div class=\"row justify-content--center\" >\n            <div class=\"col-12\"  >\n                <div class=\"wrap pv-4 \" style=\"0pxbackground-color: \">\n                                <div class=\"col-12 col-md-12 col-lg-12 col-xl-12  banner-content\"  >\n    \n                    \n                    <div class=\"mt-1 mb-1 content\"><\/div>\n\n                    \n                    <a href='' style='background-color: #ff7d21ff; color: #ffffffff; border-radius: 30px; font-weight: 600; ' class='btn mr-1  ' >Try the <span class=\"nag-n-override\" style=\"margin-left: 0 !important;\"><i>n<\/i><\/span>AG Library <i class='fas fa-angle-right'><\/i><\/a>                <\/div>\n                <\/div>\n            <\/div>\n        <\/div>\n    <\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Python has some nice packages such as\u00a0numpy,\u00a0scipy, and\u00a0matplotlib\u00a0for numerical computing and data visualization. Another package that deserves a mention that we have seen increasingly is Python&#8217;s\u00a0pandas 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\u00a0<span class=\"nag-n-override\" style=\"margin-left: 0 !important;\"><i>n<\/i><\/span>AG Library for Python.<\/p>\n","protected":false},"author":4,"featured_media":1217,"parent":0,"menu_order":0,"template":"","meta":{"content-type":"","footnotes":""},"post-tag":[34,36,27,35],"class_list":["post-1442","insights","type-insights","status-publish","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Fast Implied Volatility using Python&#039;s Pandas Library and Chebyshev Interpolation - nAG<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/nag.com\/insights\/fast-implied-volatility-using-pythons-pandas-library-and-chebyshev-interpolation\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Fast Implied Volatility using Python&#039;s Pandas Library and Chebyshev Interpolation - nAG\" \/>\n<meta property=\"og:description\" content=\"Python has some nice packages such as\u00a0numpy,\u00a0scipy, and\u00a0matplotlib\u00a0for numerical computing and data visualization. Another package that deserves a mention that we have seen increasingly is Python&#039;s\u00a0pandas 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\u00a0NAG Library for Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/nag.com\/insights\/fast-implied-volatility-using-pythons-pandas-library-and-chebyshev-interpolation\/\" \/>\n<meta property=\"og:site_name\" content=\"nAG\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-03T16:50:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/nag.com\/wp-content\/uploads\/2023\/05\/pricing-options.jpeg\" \/>\n\t<meta property=\"og:image:width\" content=\"2000\" \/>\n\t<meta property=\"og:image:height\" content=\"1000\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@NAGTalk\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/nag.com\/insights\/fast-implied-volatility-using-pythons-pandas-library-and-chebyshev-interpolation\/\",\"url\":\"https:\/\/nag.com\/insights\/fast-implied-volatility-using-pythons-pandas-library-and-chebyshev-interpolation\/\",\"name\":\"Fast Implied Volatility using Python's Pandas Library and Chebyshev Interpolation - nAG\",\"isPartOf\":{\"@id\":\"https:\/\/nag.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/nag.com\/insights\/fast-implied-volatility-using-pythons-pandas-library-and-chebyshev-interpolation\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/nag.com\/insights\/fast-implied-volatility-using-pythons-pandas-library-and-chebyshev-interpolation\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/nag.com\/wp-content\/uploads\/2023\/05\/pricing-options.jpeg\",\"datePublished\":\"2021-04-09T13:14:00+00:00\",\"dateModified\":\"2023-08-03T16:50:50+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/nag.com\/insights\/fast-implied-volatility-using-pythons-pandas-library-and-chebyshev-interpolation\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/nag.com\/insights\/fast-implied-volatility-using-pythons-pandas-library-and-chebyshev-interpolation\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/nag.com\/insights\/fast-implied-volatility-using-pythons-pandas-library-and-chebyshev-interpolation\/#primaryimage\",\"url\":\"https:\/\/nag.com\/wp-content\/uploads\/2023\/05\/pricing-options.jpeg\",\"contentUrl\":\"https:\/\/nag.com\/wp-content\/uploads\/2023\/05\/pricing-options.jpeg\",\"width\":2000,\"height\":1000,\"caption\":\"Fast Implied Volatility\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/nag.com\/insights\/fast-implied-volatility-using-pythons-pandas-library-and-chebyshev-interpolation\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/nag.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Insights\",\"item\":\"https:\/\/nag.com\/insights\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Fast Implied Volatility using Python&#8217;s Pandas Library and Chebyshev Interpolation\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/nag.com\/#website\",\"url\":\"https:\/\/nag.com\/\",\"name\":\"NAG\",\"description\":\"Robust, trusted numerical software and computational expertise.\",\"publisher\":{\"@id\":\"https:\/\/nag.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/nag.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/nag.com\/#organization\",\"name\":\"Numerical Algorithms Group\",\"alternateName\":\"NAG\",\"url\":\"https:\/\/nag.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/nag.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/nag.com\/wp-content\/uploads\/2023\/11\/NAG-Logo.png\",\"contentUrl\":\"https:\/\/nag.com\/wp-content\/uploads\/2023\/11\/NAG-Logo.png\",\"width\":1244,\"height\":397,\"caption\":\"Numerical Algorithms Group\"},\"image\":{\"@id\":\"https:\/\/nag.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/NAGTalk\",\"https:\/\/www.linkedin.com\/company\/nag\/\",\"https:\/\/www.youtube.com\/user\/NumericalAlgorithms\",\"https:\/\/github.com\/numericalalgorithmsgroup\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Fast Implied Volatility using Python's Pandas Library and Chebyshev Interpolation - nAG","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/nag.com\/insights\/fast-implied-volatility-using-pythons-pandas-library-and-chebyshev-interpolation\/","og_locale":"en_US","og_type":"article","og_title":"Fast Implied Volatility using Python's Pandas Library and Chebyshev Interpolation - nAG","og_description":"Python has some nice packages such as\u00a0numpy,\u00a0scipy, and\u00a0matplotlib\u00a0for numerical computing and data visualization. Another package that deserves a mention that we have seen increasingly is Python's\u00a0pandas 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\u00a0NAG Library for Python.","og_url":"https:\/\/nag.com\/insights\/fast-implied-volatility-using-pythons-pandas-library-and-chebyshev-interpolation\/","og_site_name":"nAG","article_modified_time":"2023-08-03T16:50:50+00:00","og_image":[{"width":2000,"height":1000,"url":"https:\/\/nag.com\/wp-content\/uploads\/2023\/05\/pricing-options.jpeg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_site":"@NAGTalk","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/nag.com\/insights\/fast-implied-volatility-using-pythons-pandas-library-and-chebyshev-interpolation\/","url":"https:\/\/nag.com\/insights\/fast-implied-volatility-using-pythons-pandas-library-and-chebyshev-interpolation\/","name":"Fast Implied Volatility using Python's Pandas Library and Chebyshev Interpolation - nAG","isPartOf":{"@id":"https:\/\/nag.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/nag.com\/insights\/fast-implied-volatility-using-pythons-pandas-library-and-chebyshev-interpolation\/#primaryimage"},"image":{"@id":"https:\/\/nag.com\/insights\/fast-implied-volatility-using-pythons-pandas-library-and-chebyshev-interpolation\/#primaryimage"},"thumbnailUrl":"https:\/\/nag.com\/wp-content\/uploads\/2023\/05\/pricing-options.jpeg","datePublished":"2021-04-09T13:14:00+00:00","dateModified":"2023-08-03T16:50:50+00:00","breadcrumb":{"@id":"https:\/\/nag.com\/insights\/fast-implied-volatility-using-pythons-pandas-library-and-chebyshev-interpolation\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/nag.com\/insights\/fast-implied-volatility-using-pythons-pandas-library-and-chebyshev-interpolation\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/nag.com\/insights\/fast-implied-volatility-using-pythons-pandas-library-and-chebyshev-interpolation\/#primaryimage","url":"https:\/\/nag.com\/wp-content\/uploads\/2023\/05\/pricing-options.jpeg","contentUrl":"https:\/\/nag.com\/wp-content\/uploads\/2023\/05\/pricing-options.jpeg","width":2000,"height":1000,"caption":"Fast Implied Volatility"},{"@type":"BreadcrumbList","@id":"https:\/\/nag.com\/insights\/fast-implied-volatility-using-pythons-pandas-library-and-chebyshev-interpolation\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/nag.com\/"},{"@type":"ListItem","position":2,"name":"Insights","item":"https:\/\/nag.com\/insights\/"},{"@type":"ListItem","position":3,"name":"Fast Implied Volatility using Python&#8217;s Pandas Library and Chebyshev Interpolation"}]},{"@type":"WebSite","@id":"https:\/\/nag.com\/#website","url":"https:\/\/nag.com\/","name":"NAG","description":"Robust, trusted numerical software and computational expertise.","publisher":{"@id":"https:\/\/nag.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/nag.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/nag.com\/#organization","name":"Numerical Algorithms Group","alternateName":"NAG","url":"https:\/\/nag.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/nag.com\/#\/schema\/logo\/image\/","url":"https:\/\/nag.com\/wp-content\/uploads\/2023\/11\/NAG-Logo.png","contentUrl":"https:\/\/nag.com\/wp-content\/uploads\/2023\/11\/NAG-Logo.png","width":1244,"height":397,"caption":"Numerical Algorithms Group"},"image":{"@id":"https:\/\/nag.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/NAGTalk","https:\/\/www.linkedin.com\/company\/nag\/","https:\/\/www.youtube.com\/user\/NumericalAlgorithms","https:\/\/github.com\/numericalalgorithmsgroup"]}]}},"_links":{"self":[{"href":"https:\/\/nag.com\/wp-json\/wp\/v2\/insights\/1442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nag.com\/wp-json\/wp\/v2\/insights"}],"about":[{"href":"https:\/\/nag.com\/wp-json\/wp\/v2\/types\/insights"}],"author":[{"embeddable":true,"href":"https:\/\/nag.com\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":2,"href":"https:\/\/nag.com\/wp-json\/wp\/v2\/insights\/1442\/revisions"}],"predecessor-version":[{"id":3366,"href":"https:\/\/nag.com\/wp-json\/wp\/v2\/insights\/1442\/revisions\/3366"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nag.com\/wp-json\/wp\/v2\/media\/1217"}],"wp:attachment":[{"href":"https:\/\/nag.com\/wp-json\/wp\/v2\/media?parent=1442"}],"wp:term":[{"taxonomy":"post-tag","embeddable":true,"href":"https:\/\/nag.com\/wp-json\/wp\/v2\/post-tag?post=1442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}