.. 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_04_heat_equation_transform.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_04_heat_equation_transform.py: Black-Scholes as the heat equation ================================== Black and Scholes (1973) found their formula by changing variables until the pricing equation became the heat equation. With :math:`x = \ln(S/K)` and :math:`\tau = \tfrac12\sigma^2(T - t)`, :math:`V = K e^{\alpha x + \beta\tau} u` turns Black-Scholes into :math:`u_\tau = u_{xx}`, whose initial condition is the transformed payoff. :func:`~financialkit.pde_methods.heat_equation_price` solves the heat equation by Crank-Nicolson on a uniform grid, watches the payoff diffuse, and transforms back. .. GENERATED FROM PYTHON SOURCE LINES 16-27 .. code-block:: Python import matplotlib.pyplot as plt from financialkit.options import black_scholes_price from financialkit.pde_methods import heat_equation_price from financialkit.pde_methods.visualizers import plot_heat_solution S, K, T, r, sigma, q = 100.0, 100.0, 1.0, 0.05, 0.25, 0.02 res = heat_equation_price(S, K, T, r, sigma, q, n_space=400, n_time=100) print(f"alpha = {res.alpha:.3f}, beta = {res.beta:.3f}, tau_end = {res.tau[-1]:.4f}") print(f"heat equation {res.price:.5f}, Black-Scholes {black_scholes_price(S, K, T, r, sigma, q):.5f}") .. rst-class:: sphx-glr-script-out .. code-block:: none alpha = 0.020, beta = -1.600, tau_end = 0.0312 heat equation 11.12264, Black-Scholes 11.12376 .. GENERATED FROM PYTHON SOURCE LINES 28-30 The payoff diffusing in heat-equation variables, and back in prices ------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 30-44 .. code-block:: Python fig, axes = plt.subplots(1, 2, figsize=(11, 4)) plot_heat_solution(res, n_curves=5, ax=axes[0]) axes[0].set_xlim(-1.0, 1.0) axes[0].set_title(r"$u_\tau = u_{xx}$") near = (res.S > 50) & (res.S < 180) axes[1].plot(res.S[near], black_scholes_price(res.S[near], K, T, r, sigma, q), "k", lw=1, label="Black-Scholes") axes[1].plot(res.S[near][::6], res.V[near][::6], "o", ms=3, label="heat equation, transformed back") axes[1].set_xlabel("spot S") axes[1].set_ylabel("call value") axes[1].legend() fig.tight_layout() plt.show() .. image-sg:: /api/gallery/pde_methods/schemes/images/sphx_glr_plot_04_heat_equation_transform_001.png :alt: $u_\tau = u_{xx}$ :srcset: /api/gallery/pde_methods/schemes/images/sphx_glr_plot_04_heat_equation_transform_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.092 seconds) .. _sphx_glr_download_api_gallery_pde_methods_schemes_plot_04_heat_equation_transform.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_04_heat_equation_transform.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_04_heat_equation_transform.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_04_heat_equation_transform.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_04_heat_equation_transform.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_