.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/sampling/true_stratified_sampling/plot_true_stratified_delaunay.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_true_stratified_sampling_plot_true_stratified_delaunay.py: Delaunay Stratified Sampling ================================== In this example, the stratified sampling method is employed to generate samples from an exponential distribution using Delaunay stratification. .. GENERATED FROM PYTHON SOURCE LINES 11-13 Import the necessary libraries. Here we import standard libraries such as numpy and matplotlib, but also need to import the :class:`.DelaunayStrata` and :class:`.TrueStratifiedSampling` class from :py:mod:`UQpy.sampling`. .. GENERATED FROM PYTHON SOURCE LINES 16-24 .. code-block:: default from UQpy.sampling.stratified_sampling.TrueStratifiedSampling import TrueStratifiedSampling from UQpy.sampling.stratified_sampling.strata import DelaunayStrata from UQpy.distributions import Exponential import numpy as np import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 25-32 Run :class:`.TrueStratifiedSampling` for 25 samples. - 2 dimensions - Five strata in each dimension - Exponential distribution with location parameter = 1 and scale parameter = 1. Create a distribution object. .. GENERATED FROM PYTHON SOURCE LINES 35-38 .. code-block:: default marginals = [Exponential(loc=1., scale=1.), Exponential(loc=1., scale=1.)] .. GENERATED FROM PYTHON SOURCE LINES 39-43 Equal number of samples in each stratum --------------------------------------- Create strata object using :class:`.VoronoiStrata` class. .. GENERATED FROM PYTHON SOURCE LINES 46-52 .. code-block:: default seeds = np.array([[0, 0], [0.4, 0.8], [1, 0], [1, 1]]) strata_obj = DelaunayStrata(seeds=seeds) sts_obj = TrueStratifiedSampling(distributions=marginals, strata_object=strata_obj) .. GENERATED FROM PYTHON SOURCE LINES 53-56 Figure shows the stratification of domain using randomly generated seed points. Notice that :class:`.DelaunayStrata` class include the corners of :math:`[0, 1]^{dimension}` hypercube before constructing Delaunay Triangulation. In this plot, orange points are the seed points and left corner is also included in the delaunay construction .. GENERATED FROM PYTHON SOURCE LINES 59-64 .. code-block:: default plt.triplot(strata_obj.delaunay.points[:, 0], strata_obj.delaunay.points[:, 1], strata_obj.delaunay.simplices) plt.plot(seeds[:, 0], seeds[:, 1], 'or') plt.show() .. image-sg:: /auto_examples/sampling/true_stratified_sampling/images/sphx_glr_plot_true_stratified_delaunay_001.png :alt: plot true stratified delaunay :srcset: /auto_examples/sampling/true_stratified_sampling/images/sphx_glr_plot_true_stratified_delaunay_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 65-66 Run stratified sampling .. GENERATED FROM PYTHON SOURCE LINES 69-73 .. code-block:: default sts_obj = TrueStratifiedSampling(distributions=marginals, strata_object=strata_obj) sts_obj.run(nsamples_per_stratum=2) .. GENERATED FROM PYTHON SOURCE LINES 74-75 Plot the resulting stratified samples and the boundaries of the strata in the :math:`U(0,1)` space. .. GENERATED FROM PYTHON SOURCE LINES 78-88 .. code-block:: default plt.triplot(strata_obj.delaunay.points[:, 0], strata_obj.delaunay.points[:, 1], strata_obj.delaunay.simplices) plt.plot(seeds[:, 0], seeds[:, 1], 'or') plt.plot(sts_obj.samplesU01[:, 0], sts_obj.samplesU01[:, 1], 'dm') plt.title('Stratified Sample - U(0,1)') plt.show() sts_obj.weights .. image-sg:: /auto_examples/sampling/true_stratified_sampling/images/sphx_glr_plot_true_stratified_delaunay_002.png :alt: Stratified Sample - U(0,1) :srcset: /auto_examples/sampling/true_stratified_sampling/images/sphx_glr_plot_true_stratified_delaunay_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none array([0.1 , 0.1 , 0.2 , 0.2 , 0.05, 0.05, 0.15, 0.15]) .. GENERATED FROM PYTHON SOURCE LINES 89-92 Proportional Sampling ---------------------- Delaunay class can generate samples proportional to volume of each stratum. .. GENERATED FROM PYTHON SOURCE LINES 95-100 .. code-block:: default sts_obj = TrueStratifiedSampling(distributions=marginals, strata_object=strata_obj) sts_obj.run(nsamples=10) .. GENERATED FROM PYTHON SOURCE LINES 101-102 It can be noticed that new sample in each stratum is proportional to volume .. GENERATED FROM PYTHON SOURCE LINES 105-112 .. code-block:: default print('Volume: ', sts_obj.strata_object.volume) print('Number of samples in each stratum: ', sts_obj.nsamples_per_stratum) plt.triplot(strata_obj.delaunay.points[:, 0], strata_obj.delaunay.points[:, 1], strata_obj.delaunay.simplices) plt.plot(seeds[:, 0], seeds[:, 1], 'or') plt.plot(sts_obj.samplesU01[:, 0], sts_obj.samplesU01[:, 1], 'dm') plt.show() .. image-sg:: /auto_examples/sampling/true_stratified_sampling/images/sphx_glr_plot_true_stratified_delaunay_003.png :alt: plot true stratified delaunay :srcset: /auto_examples/sampling/true_stratified_sampling/images/sphx_glr_plot_true_stratified_delaunay_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Volume: [0.2 0.4 0.1 0.3] Number of samples in each stratum: [2. 4. 1. 3.] .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.172 seconds) .. _sphx_glr_download_auto_examples_sampling_true_stratified_sampling_plot_true_stratified_delaunay.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/true_stratified_sampling/plot_true_stratified_delaunay.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_true_stratified_delaunay.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_true_stratified_delaunay.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_