How to do it...

The probability of a given value in a multivariate Gaussian distribution is calculated as follows:

  1. Import the relevant packages:
from scipy.stats import multivariate_normal
import numpy as np
  1. Initialize the array of variables:
x = np.array([[1,2], [3,4]])

In the preceding code snippet, we have initialized two data points located in a two-dimensional space.

  1. Calculate the probability associated with the two data points using the pdf function, where the mean and variance along the two dimensions is given, as follows:
multivariate_normal.pdf(x, mean=[0, 1], cov=[5, 2])
array([ 0.0354664 , 0.00215671])

The output of the preceding code results in probabilities associated with the first data point [1, 2] and the second data point [3, 4].

Obtaining random samples of a distribution

Random numbers can be generated using multiple functions within numpy.random.

The following table lists the set of functions that are used to generate random data:

Function

Working

Example

Rand

Returns random values in a given shape

numpy.random.rand(2,2)

Randn

Returns a sample (or samples) from the standard normal distribution

numpy.random.randn(2,2)

Randint

Returns random integers from low (inclusive) to high (exclusive)

numpy.random.randint(2,20,5)

Random_integers

Returns random integers of np.int type among lowhigh, and inclusive

numpy.random.random_integers(2,20,5)

Random

Returns random floats in the half-open interval [0.0, 1.0)

numpy.random.random(5)

 

A list of all the possible distributions from which random samples can be drawn is as follows:

Function

Working

beta(a, b[, size])

Draws samples from a beta distribution

binomial(n, p[, size])

Draws samples from a binomial distribution

chisquare(df[, size])

Draws samples from a chi-square distribution

dirichlet(alpha[, size])

Draws samples from the Dirichlet distribution

exponential([scale, size])

Draws samples from an exponential distribution

f(dfnum, dfden[, size])

Draws samples from an F distribution

gamma(shape[, scale, size])

Draws samples from a gamma distribution

geometric(p[, size])

Draws samples from the geometric distribution

gumbel([loc, scale, size])

Draws samples from a Gumbel distribution

hypergeometric(ngood, nbad, nsample[, size])

Draws samples from a hypergeometric distribution

laplace([loc, scale, size])

Draws samples from the Laplace or double exponential distribution with a specified location (or mean) and scale (decay)

logistic([loc, scale, size])

Draws samples from a logistic distribution

lognormal([mean, sigma, size])

Draws samples from a log-normal distribution

logseries(p[, size])

Draws samples from a logarithmic series distribution

multinomial(n, pvals[, size])

Draws samples from a multinomial distribution

multivariate_normal(mean, cov[, size, ...)

Draws random samples from a multivariate normal distribution

negative_binomial(n, p[, size])

Draws samples from a negative binomial distribution

noncentral_chisquare(df, nonc[, size])

Draws samples from a noncentral chi-square distribution

noncentral_f(dfnum, dfden, nonc[, size])

Draws samples from the noncentral F distribution

normal([loc, scale, size])

Draws random samples from a normal (Gaussian) distribution

pareto(a[, size])

Draws samples from a Pareto II or Lomax distribution with a specified shape

poisson([lam, size])

Draws samples from a Poisson distribution

power(a[, size])

Draws samples in [0, 1] from a power distribution with a positive exponent, a - 1

rayleigh([scale, size])

Draws samples from a Rayleigh distribution

standard_cauchy([size])

Draws samples from a standard Cauchy distribution with mode = 0

standard_exponential([size])

Draws samples from the standard exponential distribution

standard_gamma(shape[, size])

Draws samples from a standard gamma distribution

standard_normal([size])

Draws samples from a standard normal distribution (mean=0, stdev=1)

standard_t(df[, size])

Draws samples from a standard Student's T distribution with df degrees of freedom

triangular(left, mode, right[, size])

Draws samples from the triangular distribution over the interval [left, right]

uniform([low, high, size])

Draws samples from a uniform distribution

vonmises(mu, kappa[, size])

Draws samples from a von Mises distribution

wald(mean, scale[, size])

Draws samples from a Wald or inverse Gaussian distribution

weibull(a[, size])

Draws samples from a Weibull distribution

zipf(a[, size])

Draws samples from a Zipf distribution

 

Given all the preceding functions to generate certain distributions, in order to understand how to generate random samples in a given distribution in Python, we will go through it in the following code:

  1. Import the relevant packages:
import numpy.random
  1. Generate 10 random numbers from a standard normal distribution:
numpy.random.standard_normal(10)

The preceding code generates the following output:

array([ 1.19322748,  0.15660068,  0.16968799, -0.9599914 , -1.60431604,
0.31921513, -0.26443506, 1.2290333 , 1.10691107, -0.23452172])
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset