.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/monte_carlo/multilevel/plot_01_giles_mlmc.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_monte_carlo_multilevel_plot_01_giles_mlmc.py: Giles multilevel Monte Carlo ============================ Pricing with a discretised SDE has two errors: the bias of the time step and the statistical error. Balancing them with Euler's scheme costs :math:`O(\varepsilon^{-3})`. Michael Giles (2008), borrowing the idea of multigrid (and of Heinrich's multilevel integration), wrote the finest approximation as a telescoping sum, .. math:: E[P_L] = E[P_0] + \sum_{l=1}^{L} E[P_l - P_{l-1}], and simulated each correction on coupled fine and coarse paths that share Brownian increments. The corrections have small variance, so most samples go to the cheap levels and the cost falls to :math:`O(\varepsilon^{-2}(\log\varepsilon)^2)`. The paths are Euler solutions from :func:`financialkit.integrators.euler_maruyama`. .. GENERATED FROM PYTHON SOURCE LINES 21-31 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.monte_carlo import multilevel_monte_carlo from financialkit.monte_carlo.visualizers import plot_mlmc from financialkit.options import black_scholes_price S, K, T, r, sigma = 100.0, 100.0, 1.0, 0.05, 0.2 exact = black_scholes_price(S, K, T, r, sigma) .. GENERATED FROM PYTHON SOURCE LINES 32-36 Level diagnostics ----------------- The variance of :math:`P_l - P_{l-1}` falls by about 2 per level, and so does the mean (the weak error of Euler). .. GENERATED FROM PYTHON SOURCE LINES 36-43 .. code-block:: Python res = multilevel_monte_carlo(S, K, T, r, sigma, eps=0.02, rng=0) print(f"MLMC {res.estimate:.4f} +/- {res.std_error:.4f} (target RMSE {res.target_rmse}), Black-Scholes {exact:.4f}") print("samples per level:", res.level_samples.tolist()) fig, axes = plt.subplots(1, 3, figsize=(16, 4)) plot_mlmc(res, axes=axes[:2]) .. image-sg:: /api/gallery/monte_carlo/multilevel/images/sphx_glr_plot_01_giles_mlmc_001.png :alt: eps = 0.02 :srcset: /api/gallery/monte_carlo/multilevel/images/sphx_glr_plot_01_giles_mlmc_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none MLMC 10.4453 +/- 0.0141 (target RMSE 0.02), Black-Scholes 10.4506 samples per level: [1697090, 118731, 60806, 31872, 16009, 8055] array([, ], dtype=object) .. GENERATED FROM PYTHON SOURCE LINES 44-48 Cost against accuracy --------------------- :math:`\varepsilon^2 \times` cost grows only logarithmically for MLMC, like :math:`1/\varepsilon` for standard Monte Carlo. .. GENERATED FROM PYTHON SOURCE LINES 48-58 .. code-block:: Python eps = np.array([0.2, 0.1, 0.05, 0.03, 0.02]) runs = [multilevel_monte_carlo(S, K, T, r, sigma, eps=e, rng=i) for i, e in enumerate(eps)] axes[2].loglog(eps, eps**2 * np.array([x.cost for x in runs]), "o-", label="multilevel") axes[2].loglog(eps, eps**2 * np.array([x.standard_mc_cost for x in runs]), "s-", label="standard MC") axes[2].set_xlabel(r"target RMSE $\varepsilon$") axes[2].set_ylabel(r"$\varepsilon^2 \times$ cost (time steps)") axes[2].legend() fig.tight_layout() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.294 seconds) .. _sphx_glr_download_api_gallery_monte_carlo_multilevel_plot_01_giles_mlmc.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/monte_carlo/multilevel/plot_01_giles_mlmc.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_giles_mlmc.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_giles_mlmc.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_giles_mlmc.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_