.. 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_eigenvalues.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_eigenvalues.py: SROM on the eigenvalues of a system ====================================================================== In this example, Uncertainty in eigenvalues of a system is studied using :class:`.SROM` and it is compared with the Monte Carlo Simulation results. Stiffness of each element (i.e. k1, k2 and k3) are treated as random variables which follows gamma distribution. :class:`.SROM` is created for all three random variables and distribution of eigenvalues are identified using :class:`.SROM`. .. GENERATED FROM PYTHON SOURCE LINES 13-15 Import the necessary libraries. Here we import standard libraries such as numpy and matplotlib, but also need to import the :class:`.MonteCarloSampling`, :class:`.TrueStratifiedSampling` and :class:`.SROM` class from UQpy. .. GENERATED FROM PYTHON SOURCE LINES 18-30 .. code-block:: default import shutil from UQpy import PythonModel from UQpy.sampling import MonteCarloSampling, TrueStratifiedSampling from UQpy.sampling import RectangularStrata from UQpy.distributions import Gamma from UQpy.surrogates import SROM from UQpy.run_model.RunModel import RunModel from scipy.stats import gamma import numpy as np import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 31-33 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 36-39 .. code-block:: default marginals = [Gamma(a=2., loc=1., scale=3.), Gamma(a=2., loc=1., scale=3.), Gamma(a=2., loc=1., scale=3.)] .. GENERATED FROM PYTHON SOURCE LINES 40-41 Create a strata object. .. GENERATED FROM PYTHON SOURCE LINES 44-47 .. code-block:: default strata = RectangularStrata(strata_number=[3, 3, 3]) .. GENERATED FROM PYTHON SOURCE LINES 48-50 Using UQpy :class:`.TrueStratifiedSampling` class to generate samples for two random variables having :class:`.Gamma` distribution. .. GENERATED FROM PYTHON SOURCE LINES 53-57 .. code-block:: default x = TrueStratifiedSampling(distributions=marginals, strata_object=strata, nsamples_per_stratum=1) .. GENERATED FROM PYTHON SOURCE LINES 58-59 Run :class:`.SROM` to minimize the error in distribution, first order and second order moment about origin. .. GENERATED FROM PYTHON SOURCE LINES 62-66 .. code-block:: default y = SROM(samples=x.samples, target_distributions=marginals, moments=[[6, 6, 6], [54, 54, 54]], weights_errors=[1, 1, 0], properties=[True, True, True, False]) .. GENERATED FROM PYTHON SOURCE LINES 67-68 Plot the sample sets and weights from :class:`.SROM` class. Also, compared with the CDF of gamma distribution of k1. .. GENERATED FROM PYTHON SOURCE LINES 71-87 .. code-block:: default # Arrange samples in increasing order and sort samples accordingly com = np.append(y.samples, np.transpose(np.matrix(y.sample_weights)), 1) srt = com[np.argsort(com[:, 0].flatten())] s = np.array(srt[0, :, 0]) a = srt[0, :, 3] a0 = np.array(np.cumsum(a)) # Plot the SROM approximation and compare with actual gamma distribution l = 3 fig = plt.figure() plt.rcParams.update({'font.size': 12}) plt.plot(s[0], a0[0], linewidth=l) plt.plot(np.arange(3, 12, 0.05), gamma.cdf(np.arange(3, 12, 0.05), a=2, loc=3, scale=1), linewidth=l) plt.legend(['SROM Approximation', 'Gamma CDF'], loc=5, prop={'size': 12}, bbox_to_anchor=(1, 0.75)) plt.show() .. image-sg:: /auto_examples/surrogates/srom/images/sphx_glr_plot_srom_eigenvalues_001.png :alt: plot srom eigenvalues :srcset: /auto_examples/surrogates/srom/images/sphx_glr_plot_srom_eigenvalues_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 88-90 Run the model 'eigenvalue_model.py' for each sample generated through :class:`.TrueStratifiedSampling` class. This model defines the stiffness matrix corresponding to each sample and estimate the eigenvalues of the matrix. .. GENERATED FROM PYTHON SOURCE LINES 93-100 .. code-block:: default m = PythonModel(model_script='local_eigenvalue_model.py',model_object_name="RunPythonModel" ) model = RunModel(model=m) # model = RunModel(model_script='local_eigenvalue_model.py') model.run(samples=y.samples) r_srom = model.qoi_list .. GENERATED FROM PYTHON SOURCE LINES 101-102 :class:`MonteCarloSampling` class is used to generate 1000 samples. .. GENERATED FROM PYTHON SOURCE LINES 105-109 .. code-block:: default x_mcs = MonteCarloSampling(distributions=marginals, nsamples=1000) .. GENERATED FROM PYTHON SOURCE LINES 110-111 Run the model 'eigenvalue_model.py' for each sample generated through :class:`.MonteCarloSampling` class. .. GENERATED FROM PYTHON SOURCE LINES 114-118 .. code-block:: default model.run(samples=x_mcs.samples, append_samples=False) r_mcs = model.qoi_list .. GENERATED FROM PYTHON SOURCE LINES 119-120 Plot the distribution of each eigenvalue, estimated using :class:`.SROM` and :class:`.MonteCarloSampling` weights. .. GENERATED FROM PYTHON SOURCE LINES 123-170 .. code-block:: default # Plot SROM and MCS approximation for first eigenvalue r = np.array([x[:,0] for x in r_srom]) r_mcs = np.squeeze(np.array(r_mcs)) com = np.append(np.atleast_2d(r), np.transpose(np.matrix(y.sample_weights)), 1) srt = com[np.argsort(com[:, 0].flatten())] s = np.array(srt[0, :, 0]) a = srt[0, :, 1] a0 = np.array(np.cumsum(a)) fig1 = plt.figure() plt.plot(s[0], a0[0], linewidth=l) r_mcs0 = r_mcs[np.argsort(r_mcs[:, 0].flatten())] plt.plot(r_mcs0[:, 0], np.cumsum(0.001*np.ones([1, 1000])), linewidth=l) plt.title('Eigenvalue, $\lambda_1$') plt.legend(['SROM', 'MCS'], loc=1, prop={'size': 12}, bbox_to_anchor=(1, 0.92)) plt.show() # Plot SROM and MCS approximation for second eigenvalue r = np.array([x[:,1] for x in r_srom]) com = np.append(np.atleast_2d(r), np.transpose(np.matrix(y.sample_weights)), 1) srt = com[np.argsort(com[:, 0].flatten())] s = np.array(srt[0, :, 0]) a = srt[0, :, 1] a0 = np.array(np.cumsum(a)) fig2 = plt.figure() plt.plot(s[0], a0[0], linewidth=l) r_mcs0 = r_mcs[np.argsort(r_mcs[:, 1].flatten())] plt.plot(r_mcs0[:, 1], np.cumsum(0.001*np.ones([1, 1000])), linewidth=l) plt.title('Eigenvalue, $\lambda_2$') plt.legend(['SROM', 'MCS'], loc=1, prop={'size': 12}, bbox_to_anchor=(1, 0.92)) plt.show() # Plot SROM and MCS approximation for third eigenvalue r = np.array([x[:,2] for x in r_srom]) com = np.append(np.atleast_2d(r), np.transpose(np.matrix(y.sample_weights)), 1) srt = com[np.argsort(com[:, 0].flatten())] s = np.array(srt[0, :, 0]) a = srt[0, :, 1] a0 = np.array(np.cumsum(a)) fig3 = plt.figure() plt.plot(s[0], a0[0], linewidth=l) r_mcs0 = r_mcs[np.argsort(r_mcs[:, 2].flatten())] plt.plot(r_mcs0[:, 2], np.cumsum(0.001*np.ones([1, 1000])), linewidth=l) plt.title('Eigenvalue, $\lambda_3$') plt.legend(['SROM', 'MCS'], loc=1, prop={'size': 12}, bbox_to_anchor=(1, 0.92)) plt.show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/surrogates/srom/images/sphx_glr_plot_srom_eigenvalues_002.png :alt: Eigenvalue, $\lambda_1$ :srcset: /auto_examples/surrogates/srom/images/sphx_glr_plot_srom_eigenvalues_002.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/surrogates/srom/images/sphx_glr_plot_srom_eigenvalues_003.png :alt: Eigenvalue, $\lambda_2$ :srcset: /auto_examples/surrogates/srom/images/sphx_glr_plot_srom_eigenvalues_003.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/surrogates/srom/images/sphx_glr_plot_srom_eigenvalues_004.png :alt: Eigenvalue, $\lambda_3$ :srcset: /auto_examples/surrogates/srom/images/sphx_glr_plot_srom_eigenvalues_004.png :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 171-172 Note: Monte Carlo Simulation used 1000 samples, whereas SROM used 27 samples. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 2.678 seconds) .. _sphx_glr_download_auto_examples_surrogates_srom_plot_srom_eigenvalues.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_eigenvalues.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_srom_eigenvalues.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_srom_eigenvalues.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_