.. 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_05_schwartz_explicit.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_05_schwartz_explicit.py: Schwartz: explicit finite differences ===================================== Eduardo Schwartz (1977) valued warrants, which no formula covered, by replacing the derivatives in the Black-Scholes equation with differences and stepping back from expiry one explicit step at a time: .. math:: V_i^{n+1} = V_i^n + \Delta\tau\,(a_i V_{i-1}^n + b_i V_i^n + c_i V_{i+1}^n). No linear system is solved, so each step is cheap, but the time step must stay below a limit set by the finest spot spacing. :class:`~financialkit.pde_methods.ExplicitScheme` reproduces Hull's Example 15.6 call, and its whole grid matches Black-Scholes. .. GENERATED FROM PYTHON SOURCE LINES 18-30 .. code-block:: Python import matplotlib.pyplot as plt from financialkit.options import black_scholes_price from financialkit.pde_methods import ExplicitScheme from financialkit.pde_methods.visualizers import plot_value_profile # Hull, 10th ed., Example 15.6 S, K, T, r, sigma = 42.0, 40.0, 0.5, 0.10, 0.20 res = ExplicitScheme(320, 2500, s_max=160.0).price(S, K, T, r, sigma) print(f"explicit {res.price:.4f}, Black-Scholes {black_scholes_price(S, K, T, r, sigma):.4f}") print(f"time step {res.stability.number:.2e} below the limit {res.stability.limit:.2e}: {res.stability.stable}") .. rst-class:: sphx-glr-script-out .. code-block:: none explicit 4.7576, Black-Scholes 4.7594 time step 2.00e-04 below the limit 2.57e-04: True .. GENERATED FROM PYTHON SOURCE LINES 31-33 The whole grid at once ---------------------- .. GENERATED FROM PYTHON SOURCE LINES 33-43 .. code-block:: Python fig, axes = plt.subplots(1, 2, figsize=(11, 4)) plot_value_profile(res, exact=black_scholes_price(res.S, K, T, r, sigma), ax=axes[0], xlim=(20, 70)) err = res.V - black_scholes_price(res.S, K, T, r, sigma) axes[1].plot(res.S, err) axes[1].set_xlim(0, 100) axes[1].set_xlabel("spot S") axes[1].set_ylabel("explicit - Black-Scholes") fig.tight_layout() plt.show() .. image-sg:: /api/gallery/pde_methods/schemes/images/sphx_glr_plot_05_schwartz_explicit_001.png :alt: plot 05 schwartz explicit :srcset: /api/gallery/pde_methods/schemes/images/sphx_glr_plot_05_schwartz_explicit_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.103 seconds) .. _sphx_glr_download_api_gallery_pde_methods_schemes_plot_05_schwartz_explicit.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_05_schwartz_explicit.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_05_schwartz_explicit.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_05_schwartz_explicit.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_05_schwartz_explicit.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_