.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/pde_methods/schemes/plot_02_laasonen_implicit.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_schemes_plot_02_laasonen_implicit.py: Laasonen's implicit scheme ========================== Pentti Laasonen (1949) evaluated the space derivatives of the heat equation at the *new* time level, :math:`(I - \Delta\tau A) V^{n+1} = V^n`. Every step is then a linear solve, but no step is too large: the scheme is stable and monotone for any :math:`\Delta\tau`, at the price of first-order accuracy in time. :class:`~financialkit.pde_methods.ImplicitScheme` shows both sides: a large step that would wreck the explicit scheme gives a sensible answer, and halving the step halves the error. .. GENERATED FROM PYTHON SOURCE LINES 16-26 .. 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, ImplicitScheme, explicit_stability from financialkit.pde_methods.visualizers import plot_grid_convergence S, K, T, r, sigma = 100.0, 100.0, 1.0, 0.05, 0.2 exact = black_scholes_price(S, K, T, r, sigma, option_type="put") .. GENERATED FROM PYTHON SOURCE LINES 27-29 A time step 400 times the explicit limit ---------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 29-35 .. code-block:: Python res = ImplicitScheme(400, 10).price(S, K, T, r, sigma, option_type="put") limit = explicit_stability(res.S, 1.0, sigma, r).limit print(f"explicit limit {limit:.2e}, implicit step {T / 10:.2e}") print(f"implicit put with 10 steps {res.price:.4f}, Black-Scholes {exact:.4f}") .. rst-class:: sphx-glr-script-out .. code-block:: none explicit limit 1.63e-04, implicit step 1.00e-01 implicit put with 10 steps 5.4785, Black-Scholes 5.5735 .. GENERATED FROM PYTHON SOURCE LINES 36-38 First order in time, against Crank-Nicolson's second ---------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 38-48 .. code-block:: Python ns = np.array([10, 20, 40, 80, 160]) imp = [abs(ImplicitScheme(800, n, s_max=400.0).price(S, K, T, r, sigma, option_type="put").price - exact) for n in ns] cn = [abs(CrankNicolson(800, n, rannacher_steps=2, s_max=400.0).price(S, K, T, r, sigma, option_type="put").price - exact) for n in ns] fig, ax = plt.subplots(figsize=(6, 4)) plot_grid_convergence(ns, imp, ax=ax, label="Laasonen (implicit)", rates=(1.0,)) ax.loglog(ns, cn, "s-", label="Crank-Nicolson") ax.set_xlabel("time steps N") ax.legend() plt.show() .. image-sg:: /api/gallery/pde_methods/schemes/images/sphx_glr_plot_02_laasonen_implicit_001.png :alt: plot 02 laasonen implicit :srcset: /api/gallery/pde_methods/schemes/images/sphx_glr_plot_02_laasonen_implicit_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.101 seconds) .. _sphx_glr_download_api_gallery_pde_methods_schemes_plot_02_laasonen_implicit.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/schemes/plot_02_laasonen_implicit.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_laasonen_implicit.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_laasonen_implicit.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_laasonen_implicit.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_