.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/pde_methods/accuracy/plot_01_richardson_extrapolation.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_pde_methods_accuracy_plot_01_richardson_extrapolation.py: Richardson extrapolation ======================== Lewis Fry Richardson (1911), computing stresses in masonry dams by finite differences, noticed that when the error behaves like :math:`C h^p`, two solutions on grids :math:`h` and :math:`h/2` determine both the constant and the limit: :math:`V \approx V_{h/2} + (V_{h/2} - V_h)/(2^p - 1)`. :func:`~financialkit.pde_methods.richardson_extrapolation` applies this to Crank-Nicolson prices; three grids also confirm that the observed order is 2, which needs the strike on a node and Rannacher's start so that the error is a clean power of :math:`h`. .. GENERATED FROM PYTHON SOURCE LINES 17-35 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.options import black_scholes_price from financialkit.pde_methods import CrankNicolson, richardson_extrapolation S, K, T, r, sigma = 100.0, 100.0, 1.0, 0.05, 0.2 exact = black_scholes_price(S, K, T, r, sigma) ms = np.array([20, 40, 80, 160, 320]) prices = np.array([CrankNicolson(m, m, rannacher_steps=2, s_max=400.0).price(S, K, T, r, sigma).price for m in ms]) extrapolated = [richardson_extrapolation(prices[: k + 1]).extrapolated for k in range(1, len(ms))] res = richardson_extrapolation(prices) print(f"observed order {res.observed_order:.3f}") for m, p in zip(ms, prices, strict=True): print(f"M = N = {m:3d}: {p:.6f} (error {p - exact:+.2e})") print(f"extrapolated: {res.extrapolated:.6f} (error {res.extrapolated - exact:+.2e})") .. rst-class:: sphx-glr-script-out .. code-block:: none observed order 2.016 M = N = 20: 9.214957 (error -1.24e+00) M = N = 40: 10.189260 (error -2.61e-01) M = N = 80: 10.387971 (error -6.26e-02) M = N = 160: 10.435070 (error -1.55e-02) M = N = 320: 10.446714 (error -3.87e-03) extrapolated: 10.450537 (error -4.62e-05) .. GENERATED FROM PYTHON SOURCE LINES 36-38 The extrapolated sequence converges much faster ----------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 38-47 .. code-block:: Python fig, ax = plt.subplots(figsize=(6, 4)) ax.loglog(ms, np.abs(prices - exact), "o-", label="Crank-Nicolson") ax.loglog(ms[1:], np.abs(np.array(extrapolated) - exact), "s-", label="Richardson from the last two") ax.loglog(ms, np.abs(prices[0] - exact) * (ms / ms[0]) ** -2.0, ":", color="0.4", label="$M^{-2}$") ax.set_xlabel("grid size M = N") ax.set_ylabel("absolute error") ax.legend() plt.show() .. image-sg:: /api/gallery/pde_methods/accuracy/images/sphx_glr_plot_01_richardson_extrapolation_001.png :alt: plot 01 richardson extrapolation :srcset: /api/gallery/pde_methods/accuracy/images/sphx_glr_plot_01_richardson_extrapolation_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.067 seconds) .. _sphx_glr_download_api_gallery_pde_methods_accuracy_plot_01_richardson_extrapolation.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/pde_methods/accuracy/plot_01_richardson_extrapolation.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_richardson_extrapolation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_richardson_extrapolation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_richardson_extrapolation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_