.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/monte_carlo/variance_reduction/plot_04_latin_hypercube.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_variance_reduction_plot_04_latin_hypercube.py: McKay-Beckman-Conover Latin hypercube ===================================== Michael McKay, Richard Beckman and William Conover (1979), at Los Alamos, needed to run expensive computer codes on few input combinations. A Latin hypercube design of :math:`n` points stratifies every coordinate into :math:`n` equal intervals at once: each interval of each axis holds exactly one point, the axes shuffled independently. The variance of an additive integrand falls by an order of :math:`n`; interactions are left alone. .. GENERATED FROM PYTHON SOURCE LINES 15-23 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from scipy.stats import norm from financialkit.monte_carlo import latin_hypercube_estimate, monte_carlo_integrate from financialkit.monte_carlo.utils import qmc_engine from financialkit.monte_carlo.visualizers import plot_point_set .. GENERATED FROM PYTHON SOURCE LINES 24-26 One point per row and per column -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 26-31 .. code-block:: Python fig, axes = plt.subplots(1, 3, figsize=(14, 4)) plot_point_set(np.random.default_rng(0).random((10, 2)), ax=axes[0], grid=10, title="10 random points") plot_point_set(qmc_engine("lhs", 2, rng=0).random(10), ax=axes[1], grid=10, title="10-point Latin hypercube") .. image-sg:: /api/gallery/monte_carlo/variance_reduction/images/sphx_glr_plot_04_latin_hypercube_001.png :alt: 10 random points, 10-point Latin hypercube :srcset: /api/gallery/monte_carlo/variance_reduction/images/sphx_glr_plot_04_latin_hypercube_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 32-36 A basket of five lognormal assets --------------------------------- The basket's mean value is nearly additive in the inputs; a call on it is less so. Both gain, the additive one far more. .. GENERATED FROM PYTHON SOURCE LINES 36-53 .. code-block:: Python d, n = 5, 500 basket = lambda u: np.mean(np.exp(0.2 * norm.ppf(u) - 0.02), axis=1) # noqa: E731 call = lambda u: np.maximum(basket(u) - 1.0, 0.0) # noqa: E731 labels, ratios = [], [] for name, f in [("basket mean", basket), ("basket call", call)]: crude = monte_carlo_integrate(f, 20 * n, dim=d, rng=1) lhs = latin_hypercube_estimate(f, n, d, n_replications=20, rng=2) print(f"{name}: crude {crude.estimate:.5f} +/- {crude.std_error:.5f}, LHS {lhs.estimate:.5f} +/- {lhs.std_error:.5f}") labels.append(name) ratios.append(lhs.variance_ratio(crude)) axes[2].bar(labels, ratios) axes[2].set_yscale("log") axes[2].set_ylabel("variance reduction factor") axes[2].set_title(f"LHS, n = {n}, d = {d}") fig.tight_layout() plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none basket mean: crude 0.99987 +/- 0.00092, LHS 0.99999 +/- 0.00002 basket call: crude 0.03652 +/- 0.00056, LHS 0.03573 +/- 0.00021 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.078 seconds) .. _sphx_glr_download_api_gallery_monte_carlo_variance_reduction_plot_04_latin_hypercube.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/variance_reduction/plot_04_latin_hypercube.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_04_latin_hypercube.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_04_latin_hypercube.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_04_latin_hypercube.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_