.. 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_user_criterion.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_user_criterion.py: User Design Criterion ================================== This example illustrates use of user-defined latin hypercube design criteria. In particular: .. GENERATED FROM PYTHON SOURCE LINES 10-14 - How to define the Monte Carlo sampling method supported by UQpy - How to append new samples to the existing ones - How to transform existing samples to the unit hypercube - How to plot histograms for each one of the sampled parameters and 2D scatter of the produced samples .. GENERATED FROM PYTHON SOURCE LINES 19-20 Initially we have to import the necessary modules. .. GENERATED FROM PYTHON SOURCE LINES 23-31 .. 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 * from UQpy.sampling.stratified_sampling.latin_hypercube_criteria import Criterion .. GENERATED FROM PYTHON SOURCE LINES 32-37 Create user-defined criterion ---------------------------------------------- In order to create a user defined criterion, a concrete implementation of the :class:`.Criterion` abstract class must be created. The :code:`generate_samples` method must be implemented, which receives as input the :code:`random_state`, the randomly generated latin hypercube samples. .. GENERATED FROM PYTHON SOURCE LINES 40-51 .. code-block:: default class UserCriterion(Criterion): def generate_samples(self, random_state): lhs_samples = np.zeros_like(self.samples) for j in range(self.samples.shape[1]): order = np.random.permutation(self.samples.shape[0]) lhs_samples[:, j] = self.samples[order, j] return lhs_samples .. GENERATED FROM PYTHON SOURCE LINES 52-66 Define Latin Hypercube sampling ---------------------------------------------- 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` In the case of user-defined criteria an instantiation of the UserCriterion class is provided instead of the built-in criteria. .. GENERATED FROM PYTHON SOURCE LINES 70-78 .. code-block:: default dist1 = Uniform(loc=0., scale=1.) dist2 = Uniform(loc=0., scale=1.) lhs_user_defined = LatinHypercubeSampling(distributions=[dist1, dist2], nsamples=5, criterion=UserCriterion()) print(lhs_user_defined._samples) .. rst-class:: sphx-glr-script-out .. code-block:: none [[0.68090275 0.60549567] [0.14175822 0.5948103 ] [0.39720161 0.39503455] [0.88606061 0.18531564] [0.44924163 0.86012738]] .. GENERATED FROM PYTHON SOURCE LINES 79-84 Plot the samples ------------------------------------ The samples generated using the LatinHypercube sampling method can be retrieved using the *samples* attribute. This attribute is a :any:`numpy.ndarray`. .. GENERATED FROM PYTHON SOURCE LINES 87-97 .. code-block:: default # plot the samples fig, ax = plt.subplots() plt.title('LHS sampling - User Criterion') plt.scatter(lhs_user_defined._samples[:, 0], lhs_user_defined._samples[:, 1], marker='o') ax.yaxis.grid(True) ax.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_user_criterion_001.png :alt: LHS sampling - User Criterion :srcset: /auto_examples/sampling/latin_hypercube/images/sphx_glr_plot_latin_hypercube_user_criterion_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.043 seconds) .. _sphx_glr_download_auto_examples_sampling_latin_hypercube_plot_latin_hypercube_user_criterion.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_user_criterion.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_latin_hypercube_user_criterion.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_latin_hypercube_user_criterion.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_