Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

The most important continuous distributions in SciPy

Heinrich Heine University Düsseldorf

Now that we have discussed the Gaussian distribution at length, we want an overview of some of the most important continuous distributions, much as we did for the discrete ones in the chapter The most important discrete distributions. For this we use the Python module scipy.stats.

SciPy Statistics

some examples: [4.02801895 1.9033209  6.49477125 1.83362523]
mean: 3.0004822746684243 expected 3.0
std: 1.9994507762900595 expected 2.0
3rd moment: 62.99999999999999
4th moment: 344.99999999999994
entropy: 2.112085713764618
3.0004822746684243 1.9994507762900595

There are further useful methods, such as st.norm.sf, the ‘survival function’, defined as 1-cdf, or st.norm.logpdf for when you want to work directly with the logarithm of the density function (both are potentially faster than computing it by hand).

Matplotlib

For each distribution we will look at plots of the density function (pdf) and the cumulative distribution function (cdf). For this we use the Python module matplotlib.pyplot, which emulates the behaviour of MATLAB. We will come back to Matplotlib in more detail.

As a first example we simulate the continuous distribution on [5,8)[5,8) from the chapter Continuous distributions: first one of the numbers 5,6,75, 6, 7 is chosen with weights 47,27,17\frac{4}{7}, \frac{2}{7}, \frac{1}{7}, then a uniformly distributed number from [0,1)[0,1) is added. That chapter also works out the expected value 6+1146 + \frac{1}{14}, which we can compare with the mean of the sample.

sample[:10] = [5.96301841 5.60942173 5.72908826 5.80068973 5.59819708 5.35377228
 6.093922   6.36189002 5.42430139 5.87334923]
<Figure size 1600x900 with 1 Axes>
<Figure size 1600x900 with 1 Axes>
<Figure size 1600x900 with 1 Axes>

To plot a function we define it as a Python function and evaluate it at enough points; the plot is then interpolated between them:

<Figure size 1600x900 with 1 Axes>

With that, we turn to the most important continuous distributions:

Normal distribution

  • Parameters μ,σ\mu,\sigma, notation N(μ,σ2)\mathcal{N}(\mu,\sigma^2)

  • Density ϕ(x)=1σ2πe−(x−μ)22σ2\phi(x) = \frac{1}{\sigma\sqrt{2\pi}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}

  • Expected value μ\mu

  • Variance σ2\sigma^2

  • All odd central moments are 0 (the density is symmetric, so an odd moment ≠0\neq 0 is a sign of asymmetry)

  • Maximum entropy for a fixed expected value, a fixed variance and support all of R\mathbb{R}

  • Central limit theorem: for iid XiX_i with expected value μ\mu and variance σ2\sigma^2, the standardised sum ZN=1σN∑i=1N(Xi−μ)Z_N = \frac{1}{\sigma\sqrt{N}} \sum_{i=1}^N (X_i - \mu) is approximately N(0,1)\mathcal{N}(0,1)-distributed for large NN, equivalently 1N∑i=1NXi≈N ⁣(μ,σ2N)\frac{1}{N}\sum_{i=1}^N X_i \approx \mathcal{N}\!\left(\mu, \frac{\sigma^2}{N}\right) (chapter The normal distribution)

  • Example applications:

    • Measurement errors in physical systems

    • Manufacturing precision

    • Biological quantities (body height etc.)

<Figure size 1600x900 with 1 Axes>
<Figure size 1600x900 with 1 Axes>
<Figure size 1600x900 with 1 Axes>

Contaminated normal distribution

The contaminated normal distribution is a simple case of a mixture model, more precisely a Gaussian mixture.

  • Parameters ε,μ1,σ1,μ2,σ2\varepsilon,\mu_1,\sigma_1,\mu_2,\sigma_2, notation εN(μ1,σ12)+(1−ε)N(μ2,σ22)\varepsilon\mathcal{N}(\mu_1,\sigma_1^2) + (1-\varepsilon)\mathcal{N}(\mu_2,\sigma_2^2)

  • Density ϕ(x)=εσ12πe−(x−μ1)22σ12+1−εσ22πe−(x−μ2)22σ22\phi(x) = \frac{\varepsilon}{\sigma_1\sqrt{2\pi}} e^{-\frac{(x-\mu_1)^2}{2\sigma_1^2}} + \frac{1 - \varepsilon}{\sigma_2\sqrt{2\pi}} e^{-\frac{(x-\mu_2)^2}{2\sigma_2^2}}

  • Expected value εμ1+(1−ε)μ2\varepsilon\mu_1 + (1-\varepsilon)\mu_2

  • Variance εσ12+(1−ε)σ22+ε(1−ε)(μ1−μ2)2\varepsilon\sigma_1^2 + (1 - \varepsilon)\sigma_2^2 + \varepsilon(1 - \varepsilon)(\mu_1-\mu_2)^2

  • Example applications: a two-stage process in which the outcome of a Bernoulli experiment decides which normal distribution is sampled from. One concrete experimental setup would be measuring the height of the participants in a computer science lecture. Gender is (approximately!) Bernoulli distributed (presumably not uniformly), and within each gender group height is then normally distributed (but with different means). The example also shows that every act of modelling has the potential to render invisible a part of reality that you may not want invisible at all. Fortunately, mixture models can also be set up with more than one parameter ε\varepsilon.

<Figure size 1600x900 with 1 Axes>

Dirac distribution

  • Parameter ξ\xi, notation δ(ξ)\delta(\xi)

  • No density, but P(X=ξ)=1P(X = \xi) = 1

  • Expected value ξ\xi

  • Variance 0

  • Entropy 0 (read as a discrete distribution; it has no differential entropy)

  • Example applications:

    • Writing down ‘density functions’ in the continuous sense for discrete distributions

    • Photographs of the night sky (taken with Hubble, say) – every star is a smeared-out heap of pixels (normally distributed), but the ‘actual’ distribution of the stars behind it is a sum of Dirac measures, since every star sits at exactly one point (the star’s true extent is irrelevant at that distance). Some super-resolution techniques work with this model.

Strictly speaking the Dirac distribution cannot be plotted (there is no density, after all). Some people draw an arrow of length 1 at ξ\xi by way of illustration. The cumulative distribution function is a Heaviside step function:

<Figure size 1600x900 with 1 Axes>

Continuous uniform distribution

  • Parameters a,ba,b, notation U([a,b])U([a,b])

  • Density 1b−a\frac{1}{b-a}

  • Expected value a+b2\frac{a+b}{2}

  • Variance (a−b)212\frac{(a-b)^2}{12}

  • Maximum entropy among all distributions with support [a,b][a,b]

  • Example applications:

    • If a bus arrives at your stop extremely punctually every hour but you do not know when the last one left, your waiting time at the stop, in minutes, is uniformly distributed on [0,60][0,60].

    • The temperature of your CPU from the third decimal place on, (T * 100 - int(T * 100)), is approximately uniformly distributed.

0.5 0.75
<Figure size 1600x900 with 1 Axes>
<Figure size 1600x900 with 1 Axes>

Exponential distribution

  • Parameter λ\lambda, notation Exp(λ)Exp(\lambda)

  • Density λe−λx\lambda e^{-\lambda x} for x≥0x \geq 0 (and 0 for x<0x < 0)

  • Expected value 1λ\dfrac{1}{\lambda}

  • Variance 1λ2\dfrac{1}{\lambda^2}

  • Maximum entropy among all distributions with support [0,∞)[0,\infty) and expected value 1λ\dfrac{1}{\lambda}

  • Example applications:

    • How long does it take until the next packet arrives at the server?

The exponential distribution is the continuous flip side of the Poisson distribution from the chapter The most important discrete distributions: count the events per unit of time and there are Pois⁡(λ)\operatorname{Pois}(\lambda) many of them; ask instead for the waiting time until the next event, and it is Exp⁡(λ)\operatorname{Exp}(\lambda)-distributed. In both cases λ\lambda is the same rate.

<Figure size 1600x900 with 1 Axes>
<Figure size 1600x900 with 1 Axes>

Log-normal distribution

  • Parameters μ,σ\mu, \sigma, notation Lognormal(μ,σ2)Lognormal(\mu,\sigma^2)

  • Density 1xσ2πe−(log⁡(x)−μ)22σ2\frac{1}{x\sigma\sqrt{2\pi}} e^{-\frac{(\log(x)-\mu)^2}{2\sigma^2}}

  • Expected value eμ+σ22e^{\mu + \frac{\sigma^2}{2}}, but E(log⁡X)=μ\mathbb{E}(\log X) = \mu

  • Variance (eσ2−1)e2μ+σ2(e^{\sigma^2}-1)e^{2\mu + \sigma^2}, but V(log⁡X)=σ2\mathbb{V}(\log X) = \sigma^2

  • Maximum entropy among all distributions with support (0,∞)(0,\infty) and E(log⁡X)=μ\mathbb{E}(\log X) = \mu and V(log⁡X)=σ2\mathbb{V}(\log X) = \sigma^2

  • Example applications:

<Figure size 1600x900 with 1 Axes>
<Figure size 1600x900 with 1 Axes>

Weibull distribution

  • Parameters λ>0,k>0\lambda>0, k>0, notation Weibull(λ,k)Weibull(\lambda,k)

  • Density f(x)=kλ(xλ)k−1e−(xλ)kf(x) = \dfrac{k}{\lambda} {\left(\frac{x}{\lambda}\right)}^{k-1} e^{-{\left(\frac{x}{\lambda}\right)}^k} for x≥0x \geq 0
    and f(x)=0f(x)=0 for x<0x < 0

  • Expected value λΓ(1+1k)\lambda \Gamma(1+\frac{1}{k}) (where Γ(n+1)=n!\Gamma(n+1) = n!)

  • Variance λ2[Γ(1+2k)−Γ(1+1k)2]\lambda^2 \left[ \Gamma(1+\frac{2}{k}) - \Gamma(1+\frac{1}{k})^2 \right]

  • Maximum entropy among all distributions with support [0,∞)[0,\infty), fixed E(Xk)\mathbb{E}(X^k) and fixed E(log⁡X)\mathbb{E}(\log X) – so the shape parameter kk says which moment is held fixed

  • Example applications:

    • How long does a heat pump run before it breaks down?

    • What is the wind speed?

    • What is the largest amount of precipitation to fall on a single day in the year?

<Figure size 1600x900 with 1 Axes>
<Figure size 1600x900 with 1 Axes>

Data to plot and fit

To get a little practice in plotting data and fitting distributions to it (and then visualising the fit in a plot), we need some toy data.

A very simple, and therefore very instructive, dataset is Fisher and Anderson’s ‘iris flower dataset’, which we can get from Scikit-Learn, for instance:

[[5.1 3.5 1.4 0.2]
 [4.9 3.  1.4 0.2]
 [4.7 3.2 1.3 0.2]
 [4.6 3.1 1.5 0.2]
 [5.  3.6 1.4 0.2]]
<Figure size 1600x900 with 1 Axes>

One source of reasonably well-maintained and much-discussed toy data is Kaggle. To work with these data you usually have to turn CSV files into Numpy arrays. That is particularly painless with Pandas (which builds on Numpy), which we will come to; for now, Numpy’s built-in np.genfromtxt will do.

In its Climate Data Center (in German), the German weather service DWD keeps a large collection of well-documented weather and climate data, some of it updated daily. It holds spatially distributed variables and time series of all manner of physical quantities and statistical distributions. One interesting (and answerable) question, for instance, is whether heavy rainfall events in Düsseldorf have become more frequent over the last few decades.

Have a look at these (and other!) data, work out how the variables might be distributed, plot the distributions and fit them. Comparing the histogram with the density function of the fitted distribution is the best way to see how close we are.

Since a great deal of data fits none of the distributions listed here, we need more complex models as well. Mostly, though, these are assembled in stages from the simple models, as with the contaminated normal distribution. One of the most important will later be the mixture of multivariate normal distributions, which lets us look at some of the principles of machine learning.

If you want to dig further into the relationships between the distributions – and have noticed that several of them can perfectly well be parametrised in different ways (the normal distribution with μ,σ\mu,\sigma, say, but also with μ,σ2\mu,\sigma^2) – then ProbOnto may be just the thing. It is an ontology and knowledge base originally developed for pharmacology. The overview diagram alone is already a good memory aid: