Multivariate from independent marginals

This examples shows the use of the multivariate normal distribution class. In particular:

  • 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

Import the necessary modules.

from UQpy.distributions.collection import Normal, Lognormal, JointIndependent
import numpy as np
import matplotlib.pyplot as plt

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.

marginals = [Normal(loc=2., scale=2.),
             Lognormal(s=1., loc=0., scale=np.exp(5))]

dist = JointIndependent(marginals=marginals)

print(dist.get_parameters())
{'loc_0': 2.0, 'scale_0': 2.0, 's_1': 1.0, 'loc_1': 0.0, 'scale_1': 148.4131591025766}

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.

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()
random samples, Contour plot of pdf

Modify the parameters of the distribution.

Use the update_parameters method.

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())
<UQpy.distributions.collection.JointIndependent.JointIndependent object at 0x713590def940>

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}

Total running time of the script: ( 0 minutes 0.111 seconds)

Gallery generated by Sphinx-Gallery