.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/stochastic_volatility/fourier/plot_01_carr_madan_fft.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via JupyterLite. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_stochastic_volatility_fourier_plot_01_carr_madan_fft.py: Carr and Madan's FFT ==================== Carr and Madan (1999) noticed that the call price, as a function of the log strike :math:`k`, is not integrable, but :math:`e^{\alpha k} C(k)` is, and that its Fourier transform is a simple expression in the characteristic function. One fast Fourier transform then prices calls at thousands of strikes at once, for any model with a known characteristic function. The grids are linked: integration spacing :math:`\eta` and strike spacing :math:`\lambda` satisfy :math:`\lambda\eta = 2\pi/N`. .. GENERATED FROM PYTHON SOURCE LINES 16-24 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.options import black_scholes_price from financialkit.stochastic_volatility import BlackScholesModel, HestonModel, carr_madan_fft, carr_madan_price, lewis_price S, T, r = 100.0, 1.0, 0.03 .. GENERATED FROM PYTHON SOURCE LINES 25-27 A whole strike grid from one FFT -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 27-42 .. code-block:: Python bs = carr_madan_fft(BlackScholesModel(0.2), S, T, r) mid = (bs.strikes > 50) & (bs.strikes < 200) err = np.abs(bs.calls[mid] - black_scholes_price(S, bs.strikes[mid], T, r, 0.2)) print(f"{mid.sum()} strikes between 50 and 200, max error against Black-Scholes {err.max():.1e}") heston = HestonModel(0.04, 1.5, 0.04, 0.5, -0.7) res = carr_madan_fft(heston, S, T, r) fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4)) ax1.plot(res.strikes[mid], res.calls[mid], label="Heston, FFT") ax1.plot(bs.strikes[mid], bs.calls[mid], "--", label="Black-Scholes 20%") ax1.set_xlabel("strike K") ax1.set_ylabel("call price") ax1.legend() .. image-sg:: /api/gallery/stochastic_volatility/fourier/images/sphx_glr_plot_01_carr_madan_fft_001.png :alt: plot 01 carr madan fft :srcset: /api/gallery/stochastic_volatility/fourier/images/sphx_glr_plot_01_carr_madan_fft_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 226 strikes between 50 and 200, max error against Black-Scholes 2.2e-07 .. GENERATED FROM PYTHON SOURCE LINES 43-49 The damping exponent -------------------- Too little damping and the integrand is large near :math:`v = 0`; too much and the payoff's growth is amplified. Mid-range values of :math:`\alpha` are accurate to many digits. .. GENERATED FROM PYTHON SOURCE LINES 49-60 .. code-block:: Python K = np.array([80.0, 100.0, 120.0]) ref = np.asarray(lewis_price(heston, S, K, T, r)) alphas = np.linspace(0.1, 8.0, 40) errors = [np.max(np.abs(carr_madan_price(heston, S, K, T, r, alpha=a) - ref)) for a in alphas] ax2.semilogy(alphas, errors, marker="o", ms=3) ax2.set_xlabel("damping alpha") ax2.set_ylabel("max error at K = 80, 100, 120") fig.tight_layout() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.749 seconds) .. _sphx_glr_download_api_gallery_stochastic_volatility_fourier_plot_01_carr_madan_fft.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../../../lite/lab/index.html?path=api/gallery/stochastic_volatility/fourier/plot_01_carr_madan_fft.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_carr_madan_fft.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_carr_madan_fft.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_carr_madan_fft.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_