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

In [3]:
# Print continuous distributions
# Many from exam C appear
for x in dir(stats):
    if isinstance(getattr(stats, x), stats.rv_continuous):
        print(x)


alpha
anglit
arcsine
argus
beta
betaprime
bradford
burr
burr12
cauchy
chi
chi2
cosine
crystalball
dgamma
dweibull
erlang
expon
exponnorm
exponpow
exponweib
f
fatiguelife
fisk
foldcauchy
foldnorm
frechet_l
frechet_r
gamma
gausshyper
genexpon
genextreme
gengamma
genhalflogistic
genlogistic
gennorm
genpareto
gilbrat
gompertz
gumbel_l
gumbel_r
halfcauchy
halfgennorm
halflogistic
halfnorm
hypsecant
invgamma
invgauss
invweibull
johnsonsb
johnsonsu
kappa3
kappa4
ksone
kstwobign
laplace
levy
levy_l
levy_stable
loggamma
logistic
loglaplace
lognorm
lomax
maxwell
mielke
moyal
nakagami
ncf
nct
ncx2
norm
norminvgauss
pareto
pearson3
powerlaw
powerlognorm
powernorm
rayleigh
rdist
recipinvgauss
reciprocal
rice
semicircular
skewnorm
t
trapz
triang
truncexpon
truncnorm
tukeylambda
uniform
vonmises
vonmises_line
wald
weibull_max
weibull_min
wrapcauchy

In [4]:
grid = np.linspace(-3, 3)

In [5]:
y = stats.norm.pdf(grid)

In [6]:
plt.plot(grid, y);



In [9]:
# Print help and plot the probability function of any continuous distribution
# Uniform interface to distributions for pdf, cdf, random values, etc.
def plot_cts_dist(dist, **kwargs):
    dist_ = getattr(stats, dist)
    print(dist_.__doc__)
    grid = np.linspace(-3, 3)
    plt.plot(grid, dist_.pdf(grid, **kwargs))

In [10]:
plot_cts_dist('norm')


A normal continuous random variable.

    The location (loc) keyword specifies the mean.
    The scale (scale) keyword specifies the standard deviation.

    As an instance of the `rv_continuous` class, `norm` object inherits from it
    a collection of generic methods (see below for the full list),
    and completes them with details specific for this particular distribution.
    
    Methods
    -------
    rvs(loc=0, scale=1, size=1, random_state=None)
        Random variates.
    pdf(x, loc=0, scale=1)
        Probability density function.
    logpdf(x, loc=0, scale=1)
        Log of the probability density function.
    cdf(x, loc=0, scale=1)
        Cumulative distribution function.
    logcdf(x, loc=0, scale=1)
        Log of the cumulative distribution function.
    sf(x, loc=0, scale=1)
        Survival function  (also defined as ``1 - cdf``, but `sf` is sometimes more accurate).
    logsf(x, loc=0, scale=1)
        Log of the survival function.
    ppf(q, loc=0, scale=1)
        Percent point function (inverse of ``cdf`` --- percentiles).
    isf(q, loc=0, scale=1)
        Inverse survival function (inverse of ``sf``).
    moment(n, loc=0, scale=1)
        Non-central moment of order n
    stats(loc=0, scale=1, moments='mv')
        Mean('m'), variance('v'), skew('s'), and/or kurtosis('k').
    entropy(loc=0, scale=1)
        (Differential) entropy of the RV.
    fit(data, loc=0, scale=1)
        Parameter estimates for generic data.
    expect(func, args=(), loc=0, scale=1, lb=None, ub=None, conditional=False, **kwds)
        Expected value of a function (of one argument) with respect to the distribution.
    median(loc=0, scale=1)
        Median of the distribution.
    mean(loc=0, scale=1)
        Mean of the distribution.
    var(loc=0, scale=1)
        Variance of the distribution.
    std(loc=0, scale=1)
        Standard deviation of the distribution.
    interval(alpha, loc=0, scale=1)
        Endpoints of the range that contains alpha percent of the distribution

    Notes
    -----
    The probability density function for `norm` is:

    .. math::

        f(x) = \frac{\exp(-x^2/2)}{\sqrt{2\pi}}

    The survival function, ``norm.sf``, is also referred to as the
    Q-function in some contexts (see, e.g.,
    `Wikipedia's <https://en.wikipedia.org/wiki/Q-function>`_ definition).

    The probability density above is defined in the "standardized" form. To shift
    and/or scale the distribution use the ``loc`` and ``scale`` parameters.
    Specifically, ``norm.pdf(x, loc, scale)`` is identically
    equivalent to ``norm.pdf(y) / scale`` with
    ``y = (x - loc) / scale``.

    Examples
    --------
    >>> from scipy.stats import norm
    >>> import matplotlib.pyplot as plt
    >>> fig, ax = plt.subplots(1, 1)
    
    Calculate a few first moments:
    
    
    >>> mean, var, skew, kurt = norm.stats(moments='mvsk')
    
    Display the probability density function (``pdf``):
    
    >>> x = np.linspace(norm.ppf(0.01),
    ...                 norm.ppf(0.99), 100)
    >>> ax.plot(x, norm.pdf(x),
    ...        'r-', lw=5, alpha=0.6, label='norm pdf')
    
    Alternatively, the distribution object can be called (as a function)
    to fix the shape, location and scale parameters. This returns a "frozen"
    RV object holding the given parameters fixed.
    
    Freeze the distribution and display the frozen ``pdf``:
    
    >>> rv = norm()
    >>> ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')
    
    Check accuracy of ``cdf`` and ``ppf``:
    
    >>> vals = norm.ppf([0.001, 0.5, 0.999])
    >>> np.allclose([0.001, 0.5, 0.999], norm.cdf(vals))
    True
    
    Generate random numbers:
    
    >>> r = norm.rvs(size=1000)
    
    And compare the histogram:
    
    >>> ax.hist(r, density=True, histtype='stepfilled', alpha=0.2)
    >>> ax.legend(loc='best', frameon=False)
    >>> plt.show()
    

    

In [11]:
stats.norm.cdf(0)


Out[11]:
0.5

In [12]:
np.random.seed(314)
vals = stats.norm.rvs(size=100)

In [13]:
vals.mean()


Out[13]:
-0.024290543811315835

In [14]:
vals.std()


Out[14]:
0.9396883594273587

In [15]:
# Print discrete distributions
for x in dir(stats):
    if isinstance(getattr(stats, x), stats.rv_discrete):
        print(x)


bernoulli
binom
boltzmann
dlaplace
geom
hypergeom
logser
nbinom
planck
poisson
randint
skellam
zipf

In [16]:
# Print help and plot the probability function of any discrete distribution
def plot_disc_dist(dist, **kwargs):
    dist_ = getattr(stats, dist)
    print(dist_.__doc__)
    grid = list(range(0, 11))
    plt.plot(grid, dist_.pmf(grid, **kwargs))

In [17]:
plot_disc_dist('poisson', mu=5)


A Poisson discrete random variable.

    As an instance of the `rv_discrete` class, `poisson` object inherits from it
    a collection of generic methods (see below for the full list),
    and completes them with details specific for this particular distribution.
    
    Methods
    -------
    rvs(mu, loc=0, size=1, random_state=None)
        Random variates.
    pmf(k, mu, loc=0)
        Probability mass function.
    logpmf(k, mu, loc=0)
        Log of the probability mass function.
    cdf(k, mu, loc=0)
        Cumulative distribution function.
    logcdf(k, mu, loc=0)
        Log of the cumulative distribution function.
    sf(k, mu, loc=0)
        Survival function  (also defined as ``1 - cdf``, but `sf` is sometimes more accurate).
    logsf(k, mu, loc=0)
        Log of the survival function.
    ppf(q, mu, loc=0)
        Percent point function (inverse of ``cdf`` --- percentiles).
    isf(q, mu, loc=0)
        Inverse survival function (inverse of ``sf``).
    stats(mu, loc=0, moments='mv')
        Mean('m'), variance('v'), skew('s'), and/or kurtosis('k').
    entropy(mu, loc=0)
        (Differential) entropy of the RV.
    expect(func, args=(mu,), loc=0, lb=None, ub=None, conditional=False)
        Expected value of a function (of one argument) with respect to the distribution.
    median(mu, loc=0)
        Median of the distribution.
    mean(mu, loc=0)
        Mean of the distribution.
    var(mu, loc=0)
        Variance of the distribution.
    std(mu, loc=0)
        Standard deviation of the distribution.
    interval(alpha, mu, loc=0)
        Endpoints of the range that contains alpha percent of the distribution

    Notes
    -----
    The probability mass function for `poisson` is:

    .. math::

        f(k) = \exp(-\mu) \frac{mu^k}{k!}

    for :math:`k \ge 0`.

    `poisson` takes :math:`\mu` as shape parameter.

    The probability mass function above is defined in the "standardized" form.
    To shift distribution use the ``loc`` parameter.
    Specifically, ``poisson.pmf(k, mu, loc)`` is identically
    equivalent to ``poisson.pmf(k - loc, mu)``.

    Examples
    --------
    >>> from scipy.stats import poisson
    >>> import matplotlib.pyplot as plt
    >>> fig, ax = plt.subplots(1, 1)
    
    Calculate a few first moments:
    
    >>> mu = 0.6
    >>> mean, var, skew, kurt = poisson.stats(mu, moments='mvsk')
    
    Display the probability mass function (``pmf``):
    
    >>> x = np.arange(poisson.ppf(0.01, mu),
    ...               poisson.ppf(0.99, mu))
    >>> ax.plot(x, poisson.pmf(x, mu), 'bo', ms=8, label='poisson pmf')
    >>> ax.vlines(x, 0, poisson.pmf(x, mu), colors='b', lw=5, alpha=0.5)
    
    Alternatively, the distribution object can be called (as a function)
    to fix the shape and location. This returns a "frozen" RV object holding
    the given parameters fixed.
    
    Freeze the distribution and display the frozen ``pmf``:
    
    >>> rv = poisson(mu)
    >>> ax.vlines(x, 0, rv.pmf(x), colors='k', linestyles='-', lw=1,
    ...         label='frozen pmf')
    >>> ax.legend(loc='best', frameon=False)
    >>> plt.show()
    
    Check accuracy of ``cdf`` and ``ppf``:
    
    >>> prob = poisson.cdf(x, mu)
    >>> np.allclose(x, poisson.ppf(prob, mu))
    True
    
    Generate random numbers:
    
    >>> r = poisson.rvs(mu, size=1000)

    

In [ ]: