.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/sampling/refined_stratified_sampling/refined_stratified_rectangular_random.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_refined_stratified_sampling_refined_stratified_rectangular_random.py: Rectangular Refined Stratified Sampling - Random Refinement ============================================================ 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-16 In this example, Stratified sampling is used to generate samples from Uniform probability distribution and sample are added using adaptive approach Refined Stratified Sampling. Import the necessary libraries. Here we import standard libraries such as numpy and matplotlib, but also need to import the :class:`.TrueStratifiedSampling` and :class:`.RefinedStratifiedSampling` class from :py:mod:`UQpy`. .. GENERATED FROM PYTHON SOURCE LINES 19-27 .. code-block:: default from UQpy.sampling.stratified_sampling.strata import RectangularStrata from UQpy.sampling import TrueStratifiedSampling, RefinedStratifiedSampling, RandomRefinement from UQpy.distributions import Uniform import matplotlib.pyplot as plt import matplotlib.patches as patches import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 28-29 Create a distribution object. .. GENERATED FROM PYTHON SOURCE LINES 32-35 .. code-block:: default marginals = [Uniform(loc=0., scale=1.), Uniform(loc=0., scale=1.)] .. GENERATED FROM PYTHON SOURCE LINES 36-37 Create a strata object .. GENERATED FROM PYTHON SOURCE LINES 40-43 .. code-block:: default strata = RectangularStrata(strata_number=[4, 4]) .. GENERATED FROM PYTHON SOURCE LINES 44-45 Run stratified sampling. .. GENERATED FROM PYTHON SOURCE LINES 48-51 .. code-block:: default x = TrueStratifiedSampling(distributions=marginals, strata_object=strata, nsamples_per_stratum=1, random_state=1) .. GENERATED FROM PYTHON SOURCE LINES 52-54 Using UQpy :class:`.RefinedStratifiedSampling` class to expand samples generated by :class:`.TrueStratifiedSampling` class. In this example, two new samples are generated using rectangular stratification . .. GENERATED FROM PYTHON SOURCE LINES 57-62 .. code-block:: default refinement = RandomRefinement(strata=strata) y = RefinedStratifiedSampling(stratified_sampling=x, refinement_algorithm=refinement, nsamples=18, samples_per_iteration=2, random_state=2) .. GENERATED FROM PYTHON SOURCE LINES 63-66 In the first figure shown below, samples generated from :class:`.TrueStratifiedSampling` class are plotted. Second figure, shows the new stratification corresponding to two generated samples using :class:`.RefinedStratifiedSampling` class. .. GENERATED FROM PYTHON SOURCE LINES 69-83 .. code-block:: default fig1 = strata.plot_2d() plt.plot(x.samples[:16, 0], x.samples[:16, 1], 'ro') plt.xlim([0, 1]) plt.ylim([0, 1]) plt.show() fig2 = strata.plot_2d() plt.xlim([0, 1]) plt.ylim([0, 1]) plt.plot(y.samples[:16, 0], y.samples[:16, 1], 'ro') plt.plot(y.samples[16:, 0], y.samples[16:, 1], 'yo') plt.show() .. GENERATED FROM PYTHON SOURCE LINES 84-87 Further, :class:`.RefinedStratifiedSampling` class is used to adaptively increase the sample size. In this example, samples are added in strata with maximum weights associated with it and strata's are cut randomly along the maximum width. .. GENERATED FROM PYTHON SOURCE LINES 90-93 .. code-block:: default y.run(nsamples=100) .. GENERATED FROM PYTHON SOURCE LINES 94-96 In the figure shown below, all samples generated from :class:`.TrueStratifiedSampling` and :class:`.RefinedStratifiedSampling` class are plotted. .. GENERATED FROM PYTHON SOURCE LINES 99-106 .. code-block:: default fig3 = strata.plot_2d() plt.xlim([0, 1]) plt.ylim([0, 1]) plt.plot(y.samplesU01[:16, 0], y.samplesU01[:16, 1], 'ro') plt.plot(y.samplesU01[16:, 0], y.samplesU01[16:, 1], 'go') plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.000 seconds) .. _sphx_glr_download_auto_examples_sampling_refined_stratified_sampling_refined_stratified_rectangular_random.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/refined_stratified_sampling/refined_stratified_rectangular_random.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: refined_stratified_rectangular_random.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: refined_stratified_rectangular_random.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_