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.

Section 2.6 — Inventory of continuous distributions

This notebook contains all the code examples from Section 2.6 Inventory of continuous distributions of the No Bullshit Guide to Statistics.

Notebook setup


[notice] A new release of pip is available: 26.0.1 -> 26.1.2
[notice] To update, run: /Users/ivan/Projects/Minireference/STATSbook/noBSstats/venv/bin/python3.12 -m pip install --upgrade pip
Note: you may need to restart the kernel to use updated packages.
<Figure size 640x480 with 0 Axes>

Uniform distribution

The uniform distribution U(α,β)\mathcal{U}(\alpha,\beta) is described by the following probability density function:

pX(x)={1β−αfor α≤x≤β,0for x<0 or x>1.p_X(x) = \begin{cases} \frac{1}{\beta-\alpha} & \textrm{for } \alpha \leq x \leq \beta, \\ 0 & \textrm{for } x<0 \textrm{ or } x>1. \end{cases}

For a uniform distribution U(α,β)\mathcal{U}(\alpha,\beta), each xx between α\alpha and β\beta is equally likely to occur, and values of xx outside this range have zero probability of occurring.

array([3.87270059, 6.75357153, 5.65996971, 4.99329242, 2.7800932 , 2.7799726 , 2.29041806, 6.33088073, 5.00557506, 5.54036289])
<Figure size 600x200 with 1 Axes>

Cumulative distribution function

<Figure size 600x200 with 2 Axes>

Standard uniform distribution

The standard uniform distribution Us∼U(0,1)U_s \sim \mathcal{U}(0,1) is described by the following probability density function:

pU(x)={1for 0≤x≤1,0for x<0 or x>1.p_U(x) = \begin{cases} 1 & \textrm{for } 0 \leq x \leq 1, \\ 0 & \textrm{for } x<0 \textrm{ or } x>1. \end{cases}

where UU is the name of the random variable and uu are particular values it can take on.

The above equation describes tells you how likely it is to observe {Us=x}\{U_s=x\}. For a uniform distribution U(0,1)\mathcal{U}(0,1), each xx between 0 and 1 is equally likely to occur, and values of xx outside this range have zero probability of occurring.

array([0.02058449])
0.23796462709189137
0.5442292252959519
array([0.37454012, 0.95071431, 0.73199394, 0.59865848, 0.15601864, 0.15599452, 0.05808361, 0.86617615, 0.60111501, 0.70807258])
<Figure size 600x200 with 2 Axes>

Simulating other random variables

We can use the uniform random variable to generate random variables from other distributions. For example, suppose we want to generate observations of a coin toss random variable which comes out heads 50% of the time and tails 50% of the time.

We can use the standard uniform random variables obtained from random.random() and split the outcomes at the “halfway point” of the sample space, to generate the 50-50 randomness of a coin toss. The function flip_coin defined below shows how to do this:

'heads'
['tails', 'tails', 'heads', 'heads', 'tails', 'heads', 'heads', 'tails', 'heads', 'tails']

Exponential distribution

To create the model rvE, we had to specify a location parameter loc, which we set to zero, and a scale parameter, which we set to the inverse of the rate parameter λ=lam\lambda = \texttt{lam}. The location parameter can be used to shift the exponential distribution to the right, but we set loc=0 to get the simple case that corresponds to the un-shifted distribution Expon(λ)\textrm{Expon}(\lambda).

(0.14285714285714285, 0.02040816326530612)
(0.14285714285714285, 0.02040816326530612)
1.7261787475912451
<Figure size 600x200 with 1 Axes>

Normal distribution

A random variable NN with a normal distribution N(μ,σ)\mathcal{N}(\mu,\sigma) is described by the probability density function:

fN(x)=1σ2πe−(x−μ)22σ2.f_N(x) = \tfrac{1}{\sigma\sqrt{2\pi}} e^{\small -\tfrac{(x-\mu)^2}{2\sigma^2}}.

The mean μ\mu and the standard deviation σ\sigma are called the parameters of the distribution. The math notation N(μ,σ)\mathcal{N}(\mu, \sigma) is used to describe the whole family of normal probability distributions.

(10.0, 9.0)
<Figure size 600x200 with 1 Axes>
<Figure size 600x200 with 1 Axes>

Standard normal distribution

A standard normal is denoted ZZ with a normal distribution N(μ=0,σ=1)\mathcal{N}(\mu=0,\sigma=1) and described by the probability density function:

fZ(z)=12πe−z22.f_Z(z) = \tfrac{1}{\sqrt{2\pi}} e^{\small -\tfrac{z^2}{2}}.
(0.0, 1.0)
<Figure size 600x200 with 1 Axes>

Cumulative probabilities in the tails

Probability of ZZ being smaller than -2.2.

0.01072411002167581

Probability of ZZ being greater than 2.2.

0.010724110021675837

Probability of ∣Z∣>2.2|Z| > 2.2.

0.021448220043351646
0.021448220043351646

Inverse cumulative distribution calculations

-1.6448536269514729
1.644853626951472
(-1.6448536269514729, 1.644853626951472)

Mathematical interlude: the gamma function

Gamma function

(1.0, 1)
(1.0, 1)
(2.0, 2)
(6.0, 6)
(24.0, 24)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[46], line 1
----> 1 factorial(4.1)

TypeError: 'float' object cannot be interpreted as an integer
27.93175373836837
[24.0, 27.93175373836837, 32.578096050331354, 52.34277778455352, 85.6217375127053, 101.27019121310353, 120.0]
<Figure size 600x200 with 1 Axes>

Student’s t-distribution

This is a generalization of the standard normal with “heavy” tails.

<Figure size 600x200 with 1 Axes>
(0.0, 1.25)
1.0
0.02212715664214358
(-1.8124611228116767, 1.812461122811676)
<Figure size 600x200 with 1 Axes>

Chi-squared distribution

(10.0, 20.0)
0.02925268807696113
<Figure size 600x200 with 1 Axes>

Fisher–Snedecor’s F-distribution

(1.25, 1.3541666666666667)
<Figure size 600x200 with 1 Axes>
(2.0, 1.0)
<Figure size 600x200 with 1 Axes>

Beta distribution

(0.3, 0.019090909090909092)
<Figure size 600x200 with 1 Axes>

Laplace distribution

(10.0, 18.0)
<Figure size 600x200 with 1 Axes>

Explanations

Location-scale families

Normal location-scale family example

Exam question 1:
1.8
0.03593031911292577
Exam question 2:
[91.77573186524263, 108.22426813475735]
Equivalent calculations using “custom” model
0.03593031911292577
[91.77573186524263, 108.22426813475735]

Student’s t location-scale family

1.8
0.06097621069194403
Equivalent calculations using “custom” model
0.06097621069194403

Uniform location-scale family

0.75
0.25
0.25

Chi-square scale family

0.03599940476342878
0.03599940476342878

Discussion

Relations between distributions

SEE BOOK

Continuity correction

<Figure size 600x200 with 1 Axes>

Exercises