.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/surrogates/srom/plot_srom_gamma.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_surrogates_srom_plot_srom_gamma.py: SROM on a Gamma distribution ====================================================================== In this example, Stratified sampling is used to generate samples from Gamma distribution and weights are defined using Stochastic Reduce Order Model (SROM). .. 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` and :class:`.SROM` class from UQpy. .. GENERATED FROM PYTHON SOURCE LINES 16-25 .. code-block:: default from UQpy.surrogates import SROM from UQpy.sampling import RectangularStrata from UQpy.sampling import TrueStratifiedSampling from UQpy.distributions import Gamma import scipy.stats as stats import matplotlib.pyplot as plt import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 26-28 Create a distribution object for :class:`.Gamma` distribution with shape, shift and scale parameters as :math:`2`, :math:`1` and :math:`3`. .. GENERATED FROM PYTHON SOURCE LINES 31-34 .. code-block:: default marginals = [Gamma(a=2., loc=1., scale=3.), Gamma(a=2., loc=1., scale=3.)] .. GENERATED FROM PYTHON SOURCE LINES 35-36 Create a strata object. .. GENERATED FROM PYTHON SOURCE LINES 39-42 .. code-block:: default strata = RectangularStrata(strata_number=[4, 4]) .. GENERATED FROM PYTHON SOURCE LINES 43-45 Using UQpy :class:`.TrueStratifiedSampling` class to generate samples for two random variables having :class:`.Gamma` distribution. .. GENERATED FROM PYTHON SOURCE LINES 48-51 .. code-block:: default x = TrueStratifiedSampling(distributions=marginals, strata_object=strata, nsamples_per_stratum=1) .. GENERATED FROM PYTHON SOURCE LINES 52-58 Run :class:`.SROM` using the defined :class:`.Gamma` distribution. Here we use the following parameters. - :class:`.Gamma` distribution with shape, shift and scale parameters as :math:`2`, :math:`1` and :math:`3`. - First and second order moments about origin are :math:`6` and :math:`54`. - Notice that :code:`pdf_target` references the :class:`.Gamma` function directly and does not designate it as a string. - Samples are uncorrelated, i.e. also default value of correlation. .. GENERATED FROM PYTHON SOURCE LINES 61-67 .. code-block:: default y = SROM(samples=x.samples, target_distributions=marginals, moments=np.array([[6., 6.], [54., 54.]])) y.run(properties=[True, True, True, True]) .. GENERATED FROM PYTHON SOURCE LINES 68-69 Plot the samples and weights from :class:`.SROM` class. Also, compared with the CDF of gamma distribution. .. GENERATED FROM PYTHON SOURCE LINES 72-86 .. code-block:: default c = np.concatenate((y.samples, y.sample_weights.reshape(y.sample_weights.shape[0], 1)), axis=1) d = c[c[:, 0].argsort()] plt.plot(d[:, 0], np.cumsum(d[:, 2], axis=0), 'o') plt.plot(np.arange(1, 15, 0.1), stats.gamma.cdf(np.arange(1, 15, 0.1), 2, loc=1, scale=3)) plt.legend(['RV1_SROM', 'CDF']) plt.show() e = c[c[:, 1].argsort()] plt.plot(e[:, 1], np.cumsum(e[:, 2], axis=0), 'o') plt.plot(np.arange(1, 15, 0.1), stats.gamma.cdf(np.arange(1, 15, 0.1), 2, loc=1, scale=3)) plt.legend(['RV2_SROM', 'CDF']) plt.show() .. image-sg:: /auto_examples/surrogates/srom/images/sphx_glr_plot_srom_gamma_001.png :alt: plot srom gamma :srcset: /auto_examples/surrogates/srom/images/sphx_glr_plot_srom_gamma_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 87-90 A note on the weights corresponding to error in distribution, moments and correlation of random variables: - For this illustration, error_weights are not defined and default value is :math:`[1, 0.2, 0]`. These weights can be changed to obtain desired accuracy in certain properties. .. GENERATED FROM PYTHON SOURCE LINES 93-95 .. code-block:: default print(y.sample_weights) .. rst-class:: sphx-glr-script-out .. code-block:: none [9.89854571e-19 4.40931487e-02 5.98108486e-02 1.18746410e-01 1.10451429e-01 3.60641598e-02 3.46262838e-02 8.64764540e-02 2.33612832e-02 9.03621891e-02 4.47062747e-02 3.10261676e-02 1.06312183e-01 7.58251496e-02 9.58057278e-02 4.23322906e-02] .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.456 seconds) .. _sphx_glr_download_auto_examples_surrogates_srom_plot_srom_gamma.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/surrogates/srom/plot_srom_gamma.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_srom_gamma.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_srom_gamma.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_