.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/distributions/multivariate/plot_multivariate_normal.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_multivariate_normal.py: Multivariate normal distribution ================================== This examples shows the use of the multivariate normal distributions class. In particular: .. GENERATED FROM PYTHON SOURCE LINES 10-14 - How to define one of the univariate distributions 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 Initially we have to import the necessary modules. .. GENERATED FROM PYTHON SOURCE LINES 23-28 .. code-block:: default import numpy as np import matplotlib.pyplot as plt from UQpy.distributions.collection.MultivariateNormal import MultivariateNormal .. GENERATED FROM PYTHON SOURCE LINES 29-35 Example of a multivariate normal distribution --------------------------------------------- Note that multivariate normal distribution can facilitate any number of dimensions. In order to define it two arguments are necessary, specifically, a list containing the mean values for each one of the dimensions and a covariance matrix with shape (ndimensions, ndimensions) .. GENERATED FROM PYTHON SOURCE LINES 38-42 .. code-block:: default print(MultivariateNormal.__bases__) dist = MultivariateNormal(mean=[1., 2.], cov=[[4., -0.2], [-0.2, 1.]]) .. rst-class:: sphx-glr-script-out .. code-block:: none (,) .. GENERATED FROM PYTHON SOURCE LINES 43-46 Plot the two-dimensional pdf of the distribution. ------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 49-64 .. code-block:: default fig, ax = plt.subplots(ncols=1, figsize=(10, 4)) x = np.arange(-6.0, 6.0, 0.1) y = np.arange(-6.0, 6.0, 0.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.contour(X, Y, Z.reshape(X.shape)) ax.clabel(CS, inline=1, fontsize=10) ax.set_xlabel('dimension 1') ax.set_ylabel('dimension 2') ax.set_title('Contour plot of pdf') ax.set_xlim([-4, 6]) ax.set_ylim([-2, 6]) plt.show() .. image-sg:: /auto_examples/distributions/multivariate/images/sphx_glr_plot_multivariate_normal_001.png :alt: Contour plot of pdf :srcset: /auto_examples/distributions/multivariate/images/sphx_glr_plot_multivariate_normal_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 65-77 Print the multivariate 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 obtain 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 80-83 .. code-block:: default print(dist.moments()) print(dist.moments(moments2return='mv')) .. rst-class:: sphx-glr-script-out .. code-block:: none ([1.0, 2.0], [[4.0, -0.2], [-0.2, 1.0]]) ([1.0, 2.0], [[4.0, -0.2], [-0.2, 1.0]]) .. GENERATED FROM PYTHON SOURCE LINES 84-88 Generate 1000 random samples from the binomial distribution. ------------------------------------------------------------- Important: the output of rvs is a (nsamples, 1) ndarray. .. GENERATED FROM PYTHON SOURCE LINES 91-102 .. code-block:: default data = dist.rvs(nsamples=1000) fig, ax = plt.subplots(ncols=1, figsize=(10, 4)) ax.scatter(data[:, 0], data[:, 1], alpha=0.2) ax.set_xlabel('dimension 1') ax.set_ylabel('dimension 2') ax.set_title('random samples') ax.set_xlim([-4, 6]) ax.set_ylim([-2, 6]) .. image-sg:: /auto_examples/distributions/multivariate/images/sphx_glr_plot_multivariate_normal_002.png :alt: random samples :srcset: /auto_examples/distributions/multivariate/images/sphx_glr_plot_multivariate_normal_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (-2.0, 6.0) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.240 seconds) .. _sphx_glr_download_auto_examples_distributions_multivariate_plot_multivariate_normal.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_multivariate_normal.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_multivariate_normal.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_multivariate_normal.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_