Python Scipy Stats Multivariate_Normal

In this Python tutorial, we will learn about the “Scipy Stats Multivariate_Normal” where we will create a multivariate normal distribution or draw a random sample from it. We will now explore the following topic with several examples to help you learn how to use “Python Scipy Multivariate_Normal.”

  • Python Scipy Stats Multivariate_Normal
  • Python Scipy Stats Multivariate_Normal Cdf
  • Python Scipy Stats Multivariate_Normal Logpdf
  • Python Scipy Stats Multivariate_Normal Logcdf
  • Python Scipy Stats Multivariate_Normal Rvs

Also, check this related tutorial: Python Scipy Stats Poisson

Python Scipy Stats Multivariate_Normal

Multivariate distributions display comparisons between two or more variables as well as their connections. A broader multivariate distribution exists for any univariate distribution that contains a single random variable.

The Python Scipy has an object multivariate_normal() in a module scipy.stats which is a normal multivariate random variable to create a multivariate normal distribution

The keyword “mean” describes the mean. The covariance matrix is specified via the cov keyword.

The syntax is given below.

scipy.stats.multivariate_normal.method_name(x,mean,cov,allow_singular, random_state)

Where parameters are:

  • x(array_data): Quantiles, with the components represented by the last axis of x.
  • mean(array_data): The distribution’s mean.
  • cov(array_data): The distribution’s matrix of covariance.
  • allow_singular(boolean): Whether or not to permit a single covariance matrix.
  • random_state(int): The numpy.random function is used if seed is None (or np.random). A singleton of RandomState is employed. If the seed is an integer, the seed is used to create a new RandomState instance. That instance is utilized if the seed already has a Generator or RandomState instance.

Let’s take an example by following the below steps:

READ:  Matplotlib multiple plots

Import the required libraries using the below python code.

import numpy as np
from scipy import stats

Create x data whose pdf we want to find using the below code.

x_data = np.linspace(0, 10, 100, endpoint=False)
y_pdf = stats.multivariate_normal.pdf(x_data, mean=4.5, cov=1.0)
print(y_pdf)
Scipy Stats Multivariate Normal
Scipy Stats Multivariate_Normal

This is how to compute the pdf of multivariate normal distribution using the method multivariate_normal.pdf() of Python Scipy.

Also, read: Python Scipy Freqz

Python Scipy Stats Multivariate_Normal Cdf

When describing the probability distribution of random variables, the cumulative distribution function is utilized. The probability for a discrete, continuous, or mixed variable can be described using it. To derive the cumulative probability for a random variable, the probability density function is added together.

The object multivariate_normal has a method cdf to compute the cumulative distribution of multivariate normal distribution.

The syntax is given below.

scipy.stats.multivariate_normal.cdf(x,mean,cov,allow_singular, random_state)

The parameters are already defined in the above subsection.

Let’s take an example by following the below steps:

Import the required libraries using the below python code.

import numpy as np
from scipy import stats

Create x data whose cdf we are going to calculate using the below code.

x_data = np.linspace(0, 20, 200, endpoint=False)
y_cdf = stats.multivariate_normal.cdf(x_data, mean=5.5, cov=2.0)
print(y_cdf)
Scipy Stats Multivariate Normal Cdf
Scipy Stats Multivariate_Normal Cdf

This is how to compute the cdf of multivariate normal distribution using the method multivariate_normal.cdf() of Python Scipy.

Read: Python Scipy Confidence Interval

Python Scipy Stats Multivariate_Normal Logpdf

The multivariate normal density function evaluated at a given vector x is represented by its natural logarithm, which is the log-likelihood for that vector. The log-density function is also known as a log-probability density function (PDF), which is the standard abbreviation for a probability density function.

The syntax of the method is given below.

scipy.stats.multivariate_normal.logpdf(x,mean,cov,allow_singular, random_state)

Let’s take an example by following the below steps:

READ:  Python Tkinter ToDo List (Build Step by Step)

Import the required libraries using the below python code.

import numpy as np
from scipy import stats

Create x data whose log pdf is calculated using the below code.

x_data = np.linspace(0, 10, 150, endpoint=False)
y_logpdf = stats.multivariate_normal.logpdf(x_data, mean=1.5, cov=1.0)
print(y_logpdf)
Scipy Stats Multivariate Normal Logpdf
Scipy Stats Multivariate_Normal Logpdf

This is how to compute the logpdf of multivariate normal distribution using the method multivariate_normal.logpdf() of Python Scipy.

Read: Python Scipy Exponential

Python Scipy Stats Multivariate_Normal Logcdf

The lognormal distribution’s CDF function gives the likelihood that observation from a lognormal distribution, with the log scale parameter and the shape parameter, is less than or equal to x. The same concept applies to multivariate normal distribution.

The syntax of the method is given below.

scipy.stats.multivariate_normal.logcdf(x,mean,cov,allow_singular, random_state)

Let’s take an example by following the below steps:

Import the required libraries using the below python code.

import numpy as np
from scipy import stats

Create x data whose log cdf is calculated using the below code.

x_data = np.linspace(0, 30, 300, endpoint=False)
y_logcdf = stats.multivariate_normal.logcdf(x_data, mean=3.5, cov=2.0)
print(y_logcdf)
Scipy Stats Multivariate Normal Logcdf
Scipy Stats Multivariate_Normal Logcdf

This is how to compute the logcdf of multivariate normal distribution using the method multivariate_normal.logcdf() of Python Scipy.

Read: Python Scipy FFT

Python Scipy Stats Multivariate_Normal Rvs

The method rvs() of object multivariate_normal in a module scipy.stats create a multivariate normal distribution and take random samples from it.

The syntax is given below.

scipy.stats.multivariate_normal.rvs(mean,cov,size, random_state)

Where parameters are:

  • mean(array_data): The distribution’s mean.
    cov(array_data): The distribution’s matrix of covariance.
  • size(int): It is the sample size.
  • random_state(int): If the seed is None, the numpy.random method is utilized (or np.random). It uses a single instance of RandomState. If the seed is an integer, a new RandomState object is made using the seed. If the seed already has a Generator or RandomState instance, that instance is used.
READ:  Python Scipy Eigenvalues [7 Useful Examples]

Let’s draw a random sample from a multivariate normal distribution by following the below steps:

Import the required libraries using the below python code.

import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
import seaborn as sns

Create a multivariate normal distribution using the below code.

multi_norm = stats.multivariate_normal()

Create a x data and pdf of multivariate normal distribution using the below code.

xdata = np.linspace(-2, 2, 50000)
multi_pdf = multi_norm.pdf(xdata)

Draw a random sample from a multivariate normal distribution using the below code.

samp_size = 100000
sample_data = multi_norm.rvs(samp_size)

Plot the above-drawn sample using the below code.

fig, ax = plt.subplots(figsize=(12, 5))
sns.distplot(sample_data, kde=False, norm_hist=True, color='blue', ax=ax)
ax.plot(xdata, multi_pdf, color='red')

ax.set_title('Sampling Histogram Vs Normal Pdf', fontsize=24)
ax.set_xlabel('x', fontsize=20)
ax.set_ylabel('Density', fontsize=20);
Scipy Stats Multivariate Normal Rvs
Scipy Stats Multivariate_Normal Rvs

This is how to draw a random sample from a multivariate normal distribution using the method rvs() of object multivariate_normal in Python Scipy.

Also, take a look at some more Python SciPy tutorials.

So, in this tutorial, we have learned about the “Python Scipy Stats Multivariate Normal” and covered the following topics.

  • Python Scipy Stats Multivariate_Normal
  • Python Scipy Stats Multivariate_Normal Cdf
  • Python Scipy Stats Multivariate_Normal Logpdf
  • Python Scipy Stats Multivariate_Normal Logcdf
  • Python Scipy Stats Multivariate_Normal Rvs