.. 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/true_stratified_voronoi.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_true_stratified_voronoi.py: Voronoi Stratified Sampling ================================== In this example, the stratified sampling method is employed to generate samples from an exponential distribution using Voronoi 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:`.TrueStratifiedSampling` class from :py:mod:`UQpy.sampling`. .. GENERATED FROM PYTHON SOURCE LINES 16-23 .. code-block:: default from UQpy.sampling.stratified_sampling.TrueStratifiedSampling import TrueStratifiedSampling from UQpy.sampling.stratified_sampling.strata import VoronoiStrata from UQpy.distributions import Exponential import matplotlib.pyplot as plt from scipy.spatial import voronoi_plot_2d .. GENERATED FROM PYTHON SOURCE LINES 24-31 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 34-37 .. code-block:: default marginals = [Exponential(loc=1., scale=1.), Exponential(loc=1., scale=1.)] .. GENERATED FROM PYTHON SOURCE LINES 38-41 Equal number of samples in each stratum ---------------------------------------- Create strata object using :class:`.VoronoiStrata` class. .. GENERATED FROM PYTHON SOURCE LINES 44-48 .. code-block:: default strata_obj = VoronoiStrata(seeds_number=8, dimension=2) sts_vor_obj = TrueStratifiedSampling(distributions=marginals, strata_object=strata_obj, random_state=3) .. GENERATED FROM PYTHON SOURCE LINES 49-50 Figure shows the stratification of domain using randomly generated seed points .. GENERATED FROM PYTHON SOURCE LINES 53-62 .. code-block:: default strata_obj voronoi_plot_2d(strata_obj.voronoi) plt.title('Stratified Sample - U(0,1)') plt.xlim([0, 1]) plt.ylim([0, 1]) plt.show() .. GENERATED FROM PYTHON SOURCE LINES 63-64 Run stratified sampling .. GENERATED FROM PYTHON SOURCE LINES 67-70 .. code-block:: default sts_vor_obj.run(nsamples_per_stratum=3) .. GENERATED FROM PYTHON SOURCE LINES 71-72 Plot the resulting stratified samples and the boundaries of the strata in the :math:`U(0,1)` space. .. GENERATED FROM PYTHON SOURCE LINES 75-85 .. code-block:: default voronoi_plot_2d(strata_obj.voronoi) plt.title('Stratified Sample - U(0,1)') plt.plot(sts_vor_obj.samplesU01[:, 0], sts_vor_obj.samplesU01[:, 1], 'dm') plt.xlim([0, 1]) plt.ylim([0, 1]) plt.show() sts_vor_obj.weights .. GENERATED FROM PYTHON SOURCE LINES 86-89 Proportional Sampling --------------------- :class:`.TrueStratifiedSampling` class can generate samples proportional to volume of each stratum. .. GENERATED FROM PYTHON SOURCE LINES 92-96 .. code-block:: default sts_vor_obj1 = TrueStratifiedSampling(distributions=marginals, strata_object=strata_obj, random_state=1) sts_vor_obj1.run(nsamples=10) .. GENERATED FROM PYTHON SOURCE LINES 97-98 It can be noticed that new sample in each stratum is proportional to volume .. GENERATED FROM PYTHON SOURCE LINES 101-111 .. code-block:: default print('Volume: ', sts_vor_obj1.strata_object.volume) print('Number of samples in each stratum: ', sts_vor_obj1.nsamples_per_stratum) voronoi_plot_2d(strata_obj.voronoi) plt.title('Stratified Sample - U(0,1)') plt.plot(sts_vor_obj1.samplesU01[:, 0], sts_vor_obj1.samplesU01[:, 1], 'dm') plt.xlim([0, 1]) plt.ylim([0, 1]) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.000 seconds) .. _sphx_glr_download_auto_examples_sampling_true_stratified_sampling_true_stratified_voronoi.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/true_stratified_voronoi.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: true_stratified_voronoi.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: true_stratified_voronoi.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_