.. 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_rectangular.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_rectangular.py: Rectangular Stratified Sampling ================================== In this example, the stratified sampling method is employed to generate samples from an exponential distribution. .. GENERATED FROM PYTHON SOURCE LINES 10-12 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 15-24 .. code-block:: default from UQpy.sampling.stratified_sampling.TrueStratifiedSampling import TrueStratifiedSampling from UQpy.distributions import Exponential import numpy as np import matplotlib.pyplot as plt from scipy.stats import expon from UQpy.sampling.stratified_sampling.strata import RectangularStrata .. 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-42 Create strata with equal volume -------------------------------- Create a strata object using :class:`.RectangularStrata` class. .. GENERATED FROM PYTHON SOURCE LINES 45-48 .. code-block:: default strata = RectangularStrata(strata_number=[5, 5]) .. GENERATED FROM PYTHON SOURCE LINES 49-50 Generate samples using :class:`.TrueStratifiedSampling` class, one sample is generate inside each stratum. .. GENERATED FROM PYTHON SOURCE LINES 53-59 .. code-block:: default x_sts = TrueStratifiedSampling(distributions=marginals, strata_object=strata, nsamples_per_stratum=1) .. GENERATED FROM PYTHON SOURCE LINES 60-61 Plot the resulting stratified samples and the boundaries of the strata in the :math:`U(0,1)` space. .. GENERATED FROM PYTHON SOURCE LINES 64-75 .. code-block:: default fig = strata.plot_2d() plt.title('Stratified Sample - U(0,1)') plt.scatter(x_sts.samplesU01[:, 0], x_sts.samplesU01[:, 1], color='r') plt.ylim(0, 1) plt.xlim(0, 1) plt.show() print(x_sts.weights) .. image-sg:: /auto_examples/sampling/true_stratified_sampling/images/sphx_glr_plot_true_stratified_rectangular_001.png :alt: Stratified Sample - U(0,1) :srcset: /auto_examples/sampling/true_stratified_sampling/images/sphx_glr_plot_true_stratified_rectangular_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04] .. GENERATED FROM PYTHON SOURCE LINES 76-77 Plot the resulting stratified exponential samples and the boundaries of the strata in the exponential space. .. GENERATED FROM PYTHON SOURCE LINES 80-97 .. code-block:: default fig, ax = plt.subplots() plt.title('Stratified Sample - Exponential') plt.scatter(x_sts.samples[:, 0], x_sts.samples[:, 1]) ax.set_yticks([1.0, expon.ppf(0.2, 1, 1), expon.ppf(0.4, 1, 1), expon.ppf(0.6, 1, 1), expon.ppf(0.8, 1, 1), expon.ppf(0.99, 1, 1)]) ax.set_xticks([1.0, expon.ppf(0.2, 1, 1), expon.ppf(0.4, 1, 1), expon.ppf(0.6, 1, 1), expon.ppf(0.8, 1, 1), expon.ppf(0.99, 1, 1)]) ax.yaxis.grid(True) ax.xaxis.grid(True) plt.ylim(1, expon.ppf(0.99, 1, 1)) plt.xlim(1, expon.ppf(0.99, 1, 1)) plt.show() print(x_sts.samples) .. image-sg:: /auto_examples/sampling/true_stratified_sampling/images/sphx_glr_plot_true_stratified_rectangular_002.png :alt: Stratified Sample - Exponential :srcset: /auto_examples/sampling/true_stratified_sampling/images/sphx_glr_plot_true_stratified_rectangular_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [[1.06747969 1.1476857 ] [1.35088007 1.08848268] [1.70971223 1.16154741] [2.00944686 1.10808709] [3.20143124 1.10579233] [1.12696147 1.31286982] [1.35529156 1.50162259] [1.54746999 1.35553594] [2.57394918 1.46039875] [3.33062399 1.28660311] [1.03400835 1.71505305] [1.29919453 1.68834455] [1.74173834 1.6955551 ] [2.26988974 1.78513653] [4.01017631 1.62498802] [1.20050619 2.19993679] [1.43544144 2.05890816] [1.76529376 2.52148202] [2.23453661 2.44300227] [3.93930259 1.92154706] [1.05156352 2.70996901] [1.36170414 3.31801085] [1.59300373 3.52495366] [2.30208459 2.8156912 ] [5.15123197 2.65657644]] .. GENERATED FROM PYTHON SOURCE LINES 98-101 Create stratification using seeds and widths ------------------------------------------------ Strata object can be initiated by defining seeds and widths of the strata. .. GENERATED FROM PYTHON SOURCE LINES 104-110 .. code-block:: default seeds = np.array([[0, 0], [0.4, 0], [0, 0.5], [0.4, 0.5]]) widths = np.array([[0.4, 0.5], [0.6, 0.5], [0.4, 0.5], [0.6, 0.5]]) strata_obj = RectangularStrata(seeds=seeds, widths=widths) .. GENERATED FROM PYTHON SOURCE LINES 111-114 Generate samples using :class:`.TrueStratifiedSampling` class. User can control the number of samples generated inside each stratum. In this illustration, 10 samples are generated such that :code:`nsamples_per_stratum` governs the number of samples. .. GENERATED FROM PYTHON SOURCE LINES 117-121 .. code-block:: default sts_obj = TrueStratifiedSampling(distributions=marginals, strata_object=strata_obj, random_state=20) sts_obj.run(nsamples_per_stratum=[1, 2, 3, 4]) .. GENERATED FROM PYTHON SOURCE LINES 122-123 Plot show the strata and samples generated in each stratum. .. GENERATED FROM PYTHON SOURCE LINES 126-134 .. code-block:: default fig = strata_obj.plot_2d() plt.title('Stratified Sample - U(0,1)') plt.scatter(sts_obj.samplesU01[:, 0], sts_obj.samplesU01[:, 1], color='r') plt.ylim(0, 1) plt.xlim(0, 1) plt.show() .. image-sg:: /auto_examples/sampling/true_stratified_sampling/images/sphx_glr_plot_true_stratified_rectangular_003.png :alt: Stratified Sample - U(0,1) :srcset: /auto_examples/sampling/true_stratified_sampling/images/sphx_glr_plot_true_stratified_rectangular_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 135-136 Probability weights corresponding to each sample computed using Stratified sampling. .. GENERATED FROM PYTHON SOURCE LINES 139-142 .. code-block:: default sts_obj.weights .. rst-class:: sphx-glr-script-out .. code-block:: none array([0.2 , 0.15 , 0.15 , 0.06666667, 0.06666667, 0.06666667, 0.075 , 0.075 , 0.075 , 0.075 ]) .. GENERATED FROM PYTHON SOURCE LINES 143-146 Create stratification using input file ----------------------------------------- Strata object can be defined using an input file, which contains the seeds and widths of each stratum. .. GENERATED FROM PYTHON SOURCE LINES 149-152 .. code-block:: default strata_obj1 = RectangularStrata(input_file='strata.txt') .. GENERATED FROM PYTHON SOURCE LINES 153-154 Generate samples inside each stratum using :class:`.TrueStratifiedSampling` class. .. GENERATED FROM PYTHON SOURCE LINES 157-167 .. code-block:: default sts_obj1 = TrueStratifiedSampling(distributions=marginals, strata_object=strata_obj1, nsamples_per_stratum=1) fig = strata_obj1.plot_2d() plt.title('Stratified Sample - U(0,1)') plt.scatter(sts_obj1.samplesU01[:, 0], sts_obj1.samplesU01[:, 1], color='r') plt.ylim(0, 1) plt.xlim(0, 1) plt.show() .. image-sg:: /auto_examples/sampling/true_stratified_sampling/images/sphx_glr_plot_true_stratified_rectangular_004.png :alt: Stratified Sample - U(0,1) :srcset: /auto_examples/sampling/true_stratified_sampling/images/sphx_glr_plot_true_stratified_rectangular_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 168-171 Proportional sampling ---------------------- :class:`.TrueStratifiedSampling` class can generate samples proportional to volume of each stratum. .. GENERATED FROM PYTHON SOURCE LINES 174-179 .. code-block:: default strata_obj.random_state = 24 sts_obj2 = TrueStratifiedSampling(distributions=marginals, strata_object=strata_obj) sts_obj2.run(nsamples=10) .. GENERATED FROM PYTHON SOURCE LINES 180-181 It can be noticed that new sample in each stratum is proportional to volume. .. GENERATED FROM PYTHON SOURCE LINES 184-195 .. code-block:: default print('Volume: ', sts_obj2.strata_object.volume) print('Number of samples in each stratum: ', sts_obj2.nsamples_per_stratum) fig = strata_obj.plot_2d() plt.title('Stratified Sample - U(0,1)') plt.scatter(sts_obj.samplesU01[:, 0], sts_obj.samplesU01[:, 1], color='r') plt.ylim(0, 1) plt.xlim(0, 1) plt.show() .. image-sg:: /auto_examples/sampling/true_stratified_sampling/images/sphx_glr_plot_true_stratified_rectangular_005.png :alt: Stratified Sample - U(0,1) :srcset: /auto_examples/sampling/true_stratified_sampling/images/sphx_glr_plot_true_stratified_rectangular_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Volume: [0.2 0.3 0.2 0.3] Number of samples in each stratum: [2. 3. 2. 3.] .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.224 seconds) .. _sphx_glr_download_auto_examples_sampling_true_stratified_sampling_plot_true_stratified_rectangular.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_rectangular.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_true_stratified_rectangular.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_true_stratified_rectangular.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_