.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/sampling/latin_hypercube/plot_latin_hypercube_simple.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 Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_sampling_latin_hypercube_plot_latin_hypercube_simple.py: Latin Hypercube Sampling ================================== This example shows the use of the Latin Hypercube sampling class. In particular: .. GENERATED FROM PYTHON SOURCE LINES 10-13 - How to define the Latin Hypercube sampling method supported by UQpy - How to use different sampling criteria - How to plot the 2D samples .. GENERATED FROM PYTHON SOURCE LINES 18-19 Initially we have to import the necessary modules. .. GENERATED FROM PYTHON SOURCE LINES 22-28 .. code-block:: default from UQpy.sampling import LatinHypercubeSampling import matplotlib.pyplot as plt from UQpy.distributions import Uniform from UQpy.sampling.stratified_sampling.latin_hypercube_criteria import * .. GENERATED FROM PYTHON SOURCE LINES 29-40 Define Latin Hypercube sampling class ---------------------------------------------- In order to initialize the LatinHypercube sampling class, the user needs to define a list of distributions for each one of the parameters that need to be sampled. Apart from the distributions list, the number of samples :code:`nsamples` to be drawn is required. The :code:`random_state` parameter defines the seed of the random generator. Finally, the design criterion can be defined by the user. The default case is the :class:`.Random`. For more details on the various criteria you can refer to the documentation of the criteria :class:`.Random`, :class:`.Centered`, :class:`.Maximin`, :class:`.MinCorrelation` .. GENERATED FROM PYTHON SOURCE LINES 43-65 .. code-block:: default dist1 = Uniform(loc=0., scale=1.) dist2 = Uniform(loc=0., scale=1.) lhs_random = LatinHypercubeSampling(distributions=[dist1, dist2], nsamples=5, random_state=np.random.RandomState(789), criterion=Random()) lhs_centered = LatinHypercubeSampling(distributions=[dist1, dist2], criterion=Centered(), random_state=np.random.RandomState(789), nsamples=5) lhs_maximin = LatinHypercubeSampling(distributions=[dist1, dist2], random_state=np.random.RandomState(789), criterion=MaxiMin(metric=DistanceMetric.CHEBYSHEV), nsamples=5) lhs_mincorrelate = LatinHypercubeSampling(distributions=[dist1, dist2], random_state=np.random.RandomState(789), criterion=MinCorrelation(iterations=100), nsamples=5) .. GENERATED FROM PYTHON SOURCE LINES 66-73 Plot the generated samples ------------------------------------ The :code:`samples` attribute of the latin hypercube class is a numpy array of with shape :code:`(nsamples, len(distributions))` Both :code:`samples` and :code:`samplesU01` are populated at the same time since the Latin Hypercube samples are initially drawn in the unit hypercube, thus in contrast to Monte Carlo sampling no transformation is required. Using the :py:meth:`run` method to generate samples replaces the previously created ones. .. GENERATED FROM PYTHON SOURCE LINES 76-114 .. code-block:: default # plot the samples fig, axs = plt.subplots(2, 2) fig.subplots_adjust(hspace=0.5) axs[0, 0].set_title('Random-LHS design') axs[0, 0].scatter(lhs_random._samples[:, 0], lhs_random._samples[:, 1]) axs[0, 0].set_yticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0]) axs[0, 0].set_xticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0]) axs[0, 0].yaxis.grid(True) axs[0, 0].xaxis.grid(True) axs[0, 1].set_title('Centered-LHS design') axs[0, 1].scatter(lhs_centered._samples[:, 0], lhs_centered._samples[:, 1]) axs[0, 1].set_yticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0]) axs[0, 1].set_xticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0]) axs[0, 1].yaxis.grid(True) axs[0, 1].xaxis.grid(True) axs[1, 0].set_title('Maximin-LHS design') axs[1, 0].scatter(lhs_maximin._samples[:, 0], lhs_maximin._samples[:, 1]) axs[1, 0].set_yticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0]) axs[1, 0].set_xticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0]) axs[1, 0].yaxis.grid(True) axs[1, 0].xaxis.grid(True) axs[1, 1].set_title('MinCorrelation-LHS design') axs[1, 1].scatter(lhs_random._samples[:, 0], lhs_random._samples[:, 1]) axs[1, 1].set_yticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0]) axs[1, 1].set_xticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0]) axs[1, 1].yaxis.grid(True) axs[1, 1].xaxis.grid(True) plt.ylim(0, 1) plt.xlim(0, 1) plt.show() .. image-sg:: /auto_examples/sampling/latin_hypercube/images/sphx_glr_plot_latin_hypercube_simple_001.png :alt: Random-LHS design, Centered-LHS design, Maximin-LHS design, MinCorrelation-LHS design :srcset: /auto_examples/sampling/latin_hypercube/images/sphx_glr_plot_latin_hypercube_simple_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.130 seconds) .. _sphx_glr_download_auto_examples_sampling_latin_hypercube_plot_latin_hypercube_simple.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/SURGroup/UQpy/master?urlpath=lab/tree/notebooks/auto_examples/sampling/latin_hypercube/plot_latin_hypercube_simple.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_latin_hypercube_simple.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_latin_hypercube_simple.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_