.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/distributions/multivariate/plot_joint_independent.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_distributions_multivariate_plot_joint_independent.py: Multivariate from independent marginals =============================================== This examples shows the use of the multivariate normal distribution class. In particular: .. GENERATED FROM PYTHON SOURCE LINES 10-14 - How to define one multivariate distribution from independent marginals supported by UQpy - How to plot the pdf of the distribution - How to extract the moments of the distribution - How to draw random samples from the distribution .. GENERATED FROM PYTHON SOURCE LINES 19-20 Import the necessary modules. .. GENERATED FROM PYTHON SOURCE LINES 23-30 .. code-block:: default from UQpy.distributions.collection import Normal, Lognormal, JointIndependent import numpy as np import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 31-37 Define a multivariate distribution from independent univariate marginals. -------------------------------------------------------------------------- In order to define a JointIndependent distribution, a list of marginal distributions is initially created. These marginals are then provided as input to the JointIndependent class of UQpy. Retrieving the multivariate distribution parameters is achieved using the get_parameters method. .. GENERATED FROM PYTHON SOURCE LINES 40-48 .. code-block:: default marginals = [Normal(loc=2., scale=2.), Lognormal(s=1., loc=0., scale=np.exp(5))] dist = JointIndependent(marginals=marginals) print(dist.get_parameters()) .. rst-class:: sphx-glr-script-out .. code-block:: none {'loc_0': 2.0, 'scale_0': 2.0, 's_1': 1.0, 'loc_1': 0.0, 'scale_1': 148.4131591025766} .. GENERATED FROM PYTHON SOURCE LINES 49-53 Sample the distribution and plot the pdf of the distribution. --------------------------------------------------------------- In a similar manner to univariate distributions, samples can be drawn from an JointIndependent distribution using its rvs method. .. GENERATED FROM PYTHON SOURCE LINES 56-79 .. code-block:: default data = dist.rvs(nsamples=1000) fig, ax = plt.subplots(ncols=2, figsize=(10, 4)) ax[0].scatter(data[:, 0], data[:, 1], alpha=0.2) ax[0].set_xlabel('dimension 1') ax[0].set_ylabel('dimension 2') ax[0].set_title('random samples') ax[0].set_ylim([0, 500]) ax[0].set_xlim([-2, 6]) x = np.arange(-2.0, 6.0, 0.2) y = np.arange(0.01, 500, 1) X, Y = np.meshgrid(x, y) Z = dist.pdf(x=np.concatenate([X.reshape((-1, 1)), Y.reshape((-1, 1))], axis=1)) CS = ax[1].contour(X, Y, Z.reshape(X.shape)) ax[1].clabel(CS, inline=1, fontsize=10) ax[1].set_xlabel('dimension 1') ax[1].set_ylabel('dimension 2') ax[1].set_title('Contour plot of pdf') ax[0].set_ylim([0, 500]) ax[0].set_xlim([-2, 6]) plt.show() .. image-sg:: /auto_examples/distributions/multivariate/images/sphx_glr_plot_joint_independent_001.png :alt: random samples, Contour plot of pdf :srcset: /auto_examples/distributions/multivariate/images/sphx_glr_plot_joint_independent_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 80-87 Print the moments of the distribution ------------------------------------------------------------------------------- Providing a single or multiple consecutive initials of the following four distributions moments 'm': mean, 'v': variance, 's': skewness, 'k': kurtosis allows the user to obtains the respective moments from all underlying univariate distributions. In the following examples providing the string 'mv' to the moments function, returns the respective means and variances. .. GENERATED FROM PYTHON SOURCE LINES 90-95 .. code-block:: default print(dist.moments()) print(dist.moments(moments2return='mv')) .. rst-class:: sphx-glr-script-out .. code-block:: none (array([ 2. , 244.69193226]), array([4.0000000e+00, 1.0288065e+05]), array([0. , 6.18487714]), array([ 0. , 110.93639218])) (array([ 2. , 244.69193226]), array([4.0000000e+00, 1.0288065e+05])) .. GENERATED FROM PYTHON SOURCE LINES 96-100 Modify the parameters of the distribution. ------------------------------------------------- Use the update_parameters method. .. GENERATED FROM PYTHON SOURCE LINES 103-115 .. code-block:: default print(dist) print() print('Parameters of the marginals:') print([m.parameters for m in marginals]) print('Parameters of the joint:') print(dist.get_parameters()) print() print('Update the location parameter of the second marginal and scale parameter of first marginal...') dist.update_parameters(loc_1=1., scale_0=3., ) print('Parameters of the marginals:') print([m.parameters for m in marginals]) print('Parameters of the joint:') print(dist.get_parameters()) .. rst-class:: sphx-glr-script-out .. code-block:: none Parameters of the marginals: [{'loc': 2.0, 'scale': 2.0}, {'s': 1.0, 'loc': 0.0, 'scale': 148.4131591025766}] Parameters of the joint: {'loc_0': 2.0, 'scale_0': 2.0, 's_1': 1.0, 'loc_1': 0.0, 'scale_1': 148.4131591025766} Update the location parameter of the second marginal and scale parameter of first marginal... Parameters of the marginals: [{'loc': 2.0, 'scale': 3.0}, {'s': 1.0, 'loc': 1.0, 'scale': 148.4131591025766}] Parameters of the joint: {'loc_0': 2.0, 'scale_0': 3.0, 's_1': 1.0, 'loc_1': 1.0, 'scale_1': 148.4131591025766} .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.105 seconds) .. _sphx_glr_download_auto_examples_distributions_multivariate_plot_joint_independent.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/distributions/multivariate/plot_joint_independent.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_joint_independent.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_joint_independent.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_