.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/doe/algorithms/plot_lhs.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. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_doe_algorithms_plot_lhs.py: Optimal LHS vs LHS ================== .. GENERATED FROM PYTHON SOURCE LINES 24-32 .. code-block:: Python from __future__ import annotations import matplotlib.pyplot as plt from numpy import linspace from gemseo import compute_doe .. GENERATED FROM PYTHON SOURCE LINES 33-42 Latin hypercube sampling (LHS) is a technique to generate a set of :math:`n` points in dimension :math:`d`, with good space-filling properties. LHS is also the name of the resulting design of experiments (DOE). We can use the ``"OT_LHS"`` algorithm, coming from OpenTURNS as indicated by the ``"OT_"`` prefix, to generate such a DOE, say with 15 points in dimension 2: .. GENERATED FROM PYTHON SOURCE LINES 42-53 .. code-block:: Python n = 15 d = 2 samples = compute_doe(d, algo_name="OT_LHS", n_samples=n) plt.plot(samples[:, 0], samples[:, 1], "o") plt.xticks(linspace(0, 1, n + 1), minor=True) plt.yticks(linspace(0, 1, n + 1), minor=True) plt.grid(which="both") plt.title("An LHS") plt.show() .. image-sg:: /examples/doe/algorithms/images/sphx_glr_plot_lhs_001.png :alt: An LHS :srcset: /examples/doe/algorithms/images/sphx_glr_plot_lhs_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 54-67 From a technical point of view, the range of each variable is divided into :math:`n` equally probable intervals. Then, the :math:`n` points are added sequentially to satisfy the Latin hypercube requirement: one and only one point per interval. When adding a point, an interval is chosen at random for each variable, conditionally to this requirement, then the point is drawn uniformly into the resulting hypercube. Thus, LHS is not a deterministic technique, and so we can generate another LHS (if we change the ``seed``): .. GENERATED FROM PYTHON SOURCE LINES 67-76 .. code-block:: Python samples = compute_doe(d, algo_name="OT_LHS", n_samples=n, seed=123) plt.plot(samples[:, 0], samples[:, 1], "o") plt.xticks(linspace(0, 1, n + 1), minor=True) plt.yticks(linspace(0, 1, n + 1), minor=True) plt.grid(which="both") plt.title("Another LHS") plt.show() .. image-sg:: /examples/doe/algorithms/images/sphx_glr_plot_lhs_002.png :alt: Another LHS :srcset: /examples/doe/algorithms/images/sphx_glr_plot_lhs_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 77-86 These DOEs are different, but share things in common: they cover the space well in some places and bad in others with points close to each other. For both DOEs, there is room for improvement. To search for this improvement, one can use the ``"OT_OPT_LHS"`` algorithm by disabling its ``annealing`` option, to select the best LHS among a 1000 Monte Carlo instances: .. GENERATED FROM PYTHON SOURCE LINES 86-95 .. code-block:: Python samples = compute_doe(d, algo_name="OT_OPT_LHS", n_samples=n, annealing=False) plt.plot(samples[:, 0], samples[:, 1], "o") plt.xticks(linspace(0, 1, n + 1), minor=True) plt.yticks(linspace(0, 1, n + 1), minor=True) plt.grid(which="both") plt.title("An LHS optimized by Monte Carlo") plt.show() .. image-sg:: /examples/doe/algorithms/images/sphx_glr_plot_lhs_003.png :alt: An LHS optimized by Monte Carlo :srcset: /examples/doe/algorithms/images/sphx_glr_plot_lhs_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 96-100 The result is a little better but there is still room for improvement. Finally, we can use the ``"OT_OPT_LHS"`` algorithm with its default settings, to get an LHS improved by simulated annealing, a global optimization algorithm. .. GENERATED FROM PYTHON SOURCE LINES 100-109 .. code-block:: Python samples = compute_doe(d, algo_name="OT_OPT_LHS", n_samples=n) plt.plot(samples[:, 0], samples[:, 1], "o") plt.xticks(linspace(0, 1, n + 1), minor=True) plt.yticks(linspace(0, 1, n + 1), minor=True) plt.grid(which="both") plt.title("An LHS optimized by simulated annealing") plt.show() .. image-sg:: /examples/doe/algorithms/images/sphx_glr_plot_lhs_004.png :alt: An LHS optimized by simulated annealing :srcset: /examples/doe/algorithms/images/sphx_glr_plot_lhs_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 110-131 We see that this DOE covers the space much better. .. note:: These DOEs are optimal according to the C2 discrepancy, measuring the distance between the empirical distribution of the points and the uniform distribution. ``"OT_OPT_LHS"`` has options to change this space-filling criterion (``criterion``) the number of Monte Carlo instances (``n_replicates``), and the profile temperature for simulated annealing (``temperature``). See :ref:`OT_OPT_LHS_options` for more information about the settings. .. seealso:: This example uses the ``"OT_OPT_LHS"`` algorithm from OpenTURNS to create an optimal LHS. For the same purpose, we could also use the ``"LHS"`` algorithm from SciPy with its option ``optimization`` set to ``"random-cd"`` or ``"lloyd"``. See :ref:`LHS_options` for more information about the settings. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.261 seconds) .. _sphx_glr_download_examples_doe_algorithms_plot_lhs.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_lhs.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_lhs.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_lhs.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_