Probability distributions and statistical functions with scipy

TODO

  • ...

In [ ]:
%matplotlib inline

In [ ]:
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats

Discrete distributions

Binomial distribution


In [ ]:
n = 20
p = 0.25

dist = scipy.stats.binom(n, p)

Probability mass function


In [ ]:
xmin = 0
xmax = n

x = np.arange(xmin, xmax, 1)
y = dist.pmf(x)

plt.plot(x, y, 'k.');

Cumulative distribution function


In [ ]:
xmin = 0
xmax = n + 1

x = np.arange(xmin, xmax, 1)
xstart = x[:-1]
xend = x[1:]

y = dist.cdf(xstart)

plt.hlines(y, xstart, xend, colors='k')
plt.plot(xstart, y, 'k.');

Poisson distribution


In [ ]:
k = 3

dist = scipy.stats.poisson(k)

Probability mass function


In [ ]:
xmin = 0
xmax = 14 + 1  # TODO

x = np.arange(xmin, xmax, 1)
y = dist.pmf(x)

plt.plot(x, y, 'k.');

Cumulative distribution function


In [ ]:
xmin = 0
xmax = 14 + 2 # TODO

x = np.arange(xmin, xmax, 1)
xstart = x[:-1]
xend = x[1:]

y = dist.cdf(xstart)

plt.hlines(y, xstart, xend, colors='k')
plt.plot(xstart, y, 'k.');

Continuous distributions

Normal distribution


In [ ]:
mu = 0.
sigma = 1.

dist = scipy.stats.norm(mu, sigma)

Probability density function


In [ ]:
xmin = -5.
xmax = 5.

x = np.arange(xmin, xmax, 0.01)
y = dist.pdf(x)

plt.plot(x, y, 'k-');

Cumulative distribution function


In [ ]:
xmin = -5.
xmax = 5.

x = np.arange(xmin, xmax, 0.01)
y = dist.cdf(x)

plt.plot(x, y, 'k-');