.. 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_02_tavella_randall_grid.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_02_tavella_randall_grid.py: Tavella-Randall non-uniform grids ================================= Away from the strike a vanilla option is almost linear, so a uniform grid spends most of its nodes where nothing happens. Tavella and Randall (2000) map a uniform coordinate through a hyperbolic sine to cluster the nodes around the strike, keeping the grid smooth so the three-point differences stay second order. :func:`~financialkit.pde_methods.tavella_randall_grid` builds the grid, and ``grid="tavella_randall"`` prices on it: the same accuracy with a fraction of the nodes. .. GENERATED FROM PYTHON SOURCE LINES 16-32 .. 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, tavella_randall_grid, uniform_grid from financialkit.pde_methods.visualizers import plot_grid S, K, T, r, sigma = 100.0, 100.0, 0.5, 0.05, 0.2 exact = black_scholes_price(S, K, T, r, sigma) fig, axes = plt.subplots(1, 2, figsize=(11, 4)) plot_grid(uniform_grid(0.0, 400.0, 60), ax=axes[0], label="uniform") for alpha in (30.0, 15.0, 5.0): plot_grid(tavella_randall_grid(0.0, 400.0, 60, K, alpha), ax=axes[0], K=K, label=f"sinh, alpha = {alpha:g}") axes[0].set_title("node spacing, 60 intervals") .. image-sg:: /api/gallery/pde_methods/accuracy/images/sphx_glr_plot_02_tavella_randall_grid_001.png :alt: node spacing, 60 intervals :srcset: /api/gallery/pde_methods/accuracy/images/sphx_glr_plot_02_tavella_randall_grid_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'node spacing, 60 intervals') .. GENERATED FROM PYTHON SOURCE LINES 33-35 Error against the number of spot intervals ------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 35-45 .. code-block:: Python ms = np.array([20, 40, 80, 160]) for grid, kw in (("uniform", {}), ("tavella_randall", {"concentration": 0.15})): err = [abs(CrankNicolson(m, 400, rannacher_steps=2, s_max=400.0, grid=grid, **kw).price(S, K, T, r, sigma).price - exact) for m in ms] axes[1].loglog(ms, err, "o-", label=grid) axes[1].set_xlabel("spot intervals M") axes[1].set_ylabel("absolute error") axes[1].legend() fig.tight_layout() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.142 seconds) .. _sphx_glr_download_api_gallery_pde_methods_accuracy_plot_02_tavella_randall_grid.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_02_tavella_randall_grid.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_tavella_randall_grid.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_tavella_randall_grid.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_tavella_randall_grid.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_