.. 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_03_cos_method.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_03_cos_method.py: Fang and Oosterlee's COS method =============================== Fang and Oosterlee (2008) truncated the density of the log return to an interval :math:`[a, b]` set by its cumulants and expanded it in a cosine series, whose coefficients are read directly off the characteristic function. The payoff's cosine coefficients are closed form, so a price is a dot product of two short vectors. For smooth densities the error falls exponentially with the number of terms; for a density with a kink, like short-dated variance gamma, only algebraically. .. GENERATED FROM PYTHON SOURCE LINES 15-38 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.stochastic_volatility import BlackScholesModel, HestonModel, VarianceGammaModel, cos_density, cos_price, lewis_price N = 2 ** np.arange(3, 11) cases = { "Black-Scholes": (BlackScholesModel(0.25), 100.0, 1.0, 0.0), "Heston (Fang-Oosterlee)": (HestonModel(0.0175, 1.5768, 0.0398, 0.5751, -0.5711), 100.0, 1.0, 0.0), "VG, T = 1": (VarianceGammaModel(0.12, 0.2, -0.14), 90.0, 1.0, 0.1), "VG, T = 0.1": (VarianceGammaModel(0.12, 0.2, -0.14), 90.0, 0.1, 0.1), } fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4)) for name, (m, K, T, r) in cases.items(): ref = lewis_price(m, 100.0, K, T, r) err = [abs(cos_price(m, 100.0, K, T, r, N=n) - ref) for n in N] ax1.semilogy(N, np.maximum(err, 1e-16), marker="o", ms=3, label=name) ax1.set_xscale("log", base=2) ax1.set_xlabel("cosine terms N") ax1.set_ylabel("|COS - reference|") ax1.legend() .. image-sg:: /api/gallery/stochastic_volatility/fourier/images/sphx_glr_plot_03_cos_method_001.png :alt: plot 03 cos method :srcset: /api/gallery/stochastic_volatility/fourier/images/sphx_glr_plot_03_cos_method_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 39-41 Recovering the density ---------------------- .. GENERATED FROM PYTHON SOURCE LINES 41-53 .. code-block:: Python x = np.linspace(-0.6, 0.4, 400) vg = VarianceGammaModel(0.12, 0.2, -0.14) for n in (16, 64, 512): ax2.plot(x, cos_density(vg, x, 0.5, N=n), label=f"N = {n}") ax2.plot(x, vg.pdf(x, 0.5), "k--", lw=1, label="exact (Bessel)") ax2.set_xlabel("log return") ax2.set_title("variance gamma density, T = 0.5") ax2.legend() fig.tight_layout() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.195 seconds) .. _sphx_glr_download_api_gallery_stochastic_volatility_fourier_plot_03_cos_method.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_03_cos_method.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_cos_method.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_cos_method.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_cos_method.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_