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.8 — Probability models for random samples

This notebook contains the code examples from Section 2.8 Probability models for random samples 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>

Definitions

  • XX: a random variable with probability distribution fXf_X

  • X=(X1,X2,X3,…,Xn)\mathbf{X} = (X_1,X_2,X_3, \ldots, X_n): random sample of size nn. Each XiX_i is independent copy of the random variable X∼fXX \sim f_X.

  • x=(x1,x2,…,xn)\mathbf{x} = (x_1,x_2, \ldots, x_n): a particular sample, which consists of nn observations from the distribution fXf_X.

  • statistic: any function computed from a particular sample x\mathbf{x}.

  • sampling distribution of statistic: the variability of a statistic when computed on a random sample X\mathbf{X}

Sample statistics

A statistics is any function we compute from a sample x\mathbf{x}.

Examples of sample statistics:

  • sample mean x‾=1n∑i=1nxi\overline{\mathbf{x}} = \frac{1}{n}\sum_{i=1}^n x_i

  • sample variance s2=1n−1∑i=1n(xi−x‾)2s^2 = \frac{1}{n-1}\sum_{i=1}^n \left(x_i-\overline{\mathbf{x}}\right)^2

  • median Fx−1(12)F_{\mathbf{x}}^{-1}(\frac{1}{2}), where Fx−1F_{\mathbf{x}}^{-1} is the inverse of the empirical CDF.

  • 90th percentile Fx−1(0.9)F_{\mathbf{x}}^{-1}(0.9)

Sample mean

Let’s focus on the sample mean:

x‾=1n∑i=1nxi=mean(x)\overline{x} = \frac{1}{n}\sum_{i=1}^n x_i = \texttt{mean(} \mathbf{x} \texttt{)}
5.0

Example 1U: Samples from a uniform distribution

0.4280460079337365
0.4756821051022538
0.485479154547072
0.5008027883588269

Example 1Z: Samples from the standard normal

-0.26951611032632805
0.04528630589790069
-0.03750017240797483
0.0015932431337300676

Example 1E: Samples from the exponential distribution

3.4770188924465337
4.850632215012492
5.041138061882218
5.00849380798984

Law of large numbers

TODO: demo of some sort as sample size increases

maybe using all three examples...

Sampling distribution of the mean

Consider the sample mean statistics, which can be computed using the Python function mean above. Given the random sample X\mathbf{X}, the quantity Mean(X)\mathbf{Mean}(\mathbf{X}) is a random variable. This should make sense intuitively, since the inputs to the function X\mathbf{X} are random, the output of the function is also random.

The probability distribution of the random variable Mean(X)\mathbf{Mean}(\mathbf{X}) is called the sampling distribution of the mean.

Example 2U: the sampling distribution Mean(U)\mathbf{Mean}(\mathbf{U})

In this example, we’ll study the sampling distribution of the Mean\mathbf{Mean} statistic computed from samples of the form U=(U1,U2,…,Un)\mathbf{U} = (U_1, U_2, \ldots, U_n), where each UiU_i is an instance of the standard uniform random variable U∼U(0,1)U \sim \mathcal{U}(0,1).

We can create a computer model rvU for the random variable U∼U(0,1)U \sim \mathcal{U}(0,1) by initializing an instance of the uniform family of distributions defined in scipy.stats.

We can now generate observations of the random sample U=(U1,U2,…,Un)\mathbf{U} = (U_1, U_2, \ldots, U_n) by calling the method rvU.rvs(n), which generates a sequence of n observations of the random variable UU.

array([0.311, 0.867, 0.216, 0.468, 0.519, 0.265, 0.035, 0.705, 0.937, 0.76 , 0.806, 0.235, 0.454, 0.757, 0.352, 0.546, 0.465, 0.54 , 0.205, 0.535, 0.868, 0.95 , 0.083, 0.281, 0.121, 0.183, 0.857, 0.505, 0.078, 0.25 ])
0.471777206335568

We now generate some 10 data samples of size n=30n=30, and plot the observations as a scatter plot.

<Figure size 700x400 with 1 Axes>

Visualizing the sampling distribution of the mean.
Let’s now generate N=1000N=1000 samples u1,u2,u3,…,uN\mathbf{u}_1, \mathbf{u}_2, \mathbf{u}_3, \ldots, \mathbf{u}_N, compute the mean of each sample to form a list of observations ubars = [u1‾,u2‾,u3‾,…,uN‾][\overline{\mathbf{u}_1}, \overline{\mathbf{u}_2}, \overline{\mathbf{u}_3}, \ldots, \overline{\mathbf{u}_N}].

[0.5166580187797896, 0.494019340353339, 0.4952366746096193, 0.5034161964218155, 0.4939887221557366]

We can now plot a histogram of the observations from the sampling distribution we have ubars by calling sns.histplot(ubars). We can also generate a strip plot using sns.stripplot(ubars, marker="D"), specifying the D (diamond) marker to indicate we are plotting the means from each sample.

We’ll use the function sns.scatterplot(x=stats, y=-5, marker="D") to draw a scatter plot below the histogram, so you can see individual observations.

<Figure size 700x400 with 1 Axes>

We can compute the mean and the variance of the sampling distribution:

(0.5009440637092808, 0.05207476878510105)

General-purpose generator of sampling distributions

We can make generic function from the above example.

The function gen_sampling_dist_of_mean generates N samples of size n from the random variable object rv and calculates the means of each of these samples. The function returns a lists of means.

[0.4769002058840572, 0.5793002328266957, 0.45995858106973064, 0.580177535702757, 0.4101793585600197]
(0.5027831006852861, 0.053368150863753364)

Example 2Z: the sampling distribution Mean(Z)\mathbf{Mean}(\mathbf{Z})

Let’s define a random variable rvZ ∼N(μ=0,σ=1)\sim \mathcal{N}(\mu=0, \sigma=1).

We now generate some 10 data samples of size n=30n=30, and plot the observations as a scatter plot.

<Figure size 700x400 with 1 Axes>

Visualizing the sampling distribution of the mean.
Let’s now generate N=1000N=1000 samples z1,z2,z3,…,zN\mathbf{z}_1, \mathbf{z}_2, \mathbf{z}_3, \ldots, \mathbf{z}_N, compute the mean of each sample zbars = [z1‾,z2‾,z3‾,…,zN‾][\overline{\mathbf{z}_1}, \overline{\mathbf{z}_2}, \overline{\mathbf{z}_3}, \ldots, \overline{\mathbf{z}_N}].

array([-0.019, -0.194, -0.028, -0.074, 0.127])

The plot helper function plot_sampling_dist(stats) generates a combined plot that shows both a histogram and a scatter plot, as shown in the figures below.

<Figure size 700x400 with 1 Axes>

We can compute the mean and the variance of the sampling distribution:

(-0.0009476378449287116, 0.18378975146790222)

TODO: show math of Zoe’s derivaiton

Example 2E: the sampling distribution Mean(E)\mathbf{Mean}(\mathbf{E})

Let’s define a random variable rvE ∼Expon(λ=0.2)\sim \textrm{Expon}(\lambda=0.2).

We now generate some 10 data samples of size n=30n=30, and plot the observations as a scatter plot.

<Axes: >
<Figure size 700x400 with 1 Axes>

Visualizing the sampling distribution of the mean.
We will now generate N=1000N=1000 samples e1,e2,e3,…,eN\mathbf{e}_1, \mathbf{e}_2, \mathbf{e}_3, \ldots, \mathbf{e}_N, compute the mean of each sample e1‾,e2‾,e3‾,…,eN‾\overline{\mathbf{e}_1}, \overline{\mathbf{e}_2}, \overline{\mathbf{e}_3}, \ldots, \overline{\mathbf{e}_N}.

array([3.192, 5.005, 4.812, 4.367, 6.386])
<Figure size 700x400 with 1 Axes>

We can compute the mean and the variance of the sampling distribution:

(5.025212594362794, 0.9297733370012011)

Central limit theorem

The Central Limit Theorem (CLT) is a mathy result tells us two very important practical facts:

  1. The sampling distribution of the sample mean X‾\overline{X} is normally distributed for samples taken for ANY random variable, as the sample size nn increases.

  2. The standard deviation of sampling distribution of the sample mean X‾\overline{X} decrease σX‾=σn\sigma_{\overline{X}} = \frac{\sigma}{\sqrt{n}}, where σ\sigma is the standard deviation of the random variable XX.

Let’s verify both claims of the CLT by revisiting the sampling distributions we obtained above.

Example 3U: sampling distribution as a function of nn

<Figure size 700x200 with 3 Axes>
<Figure size 700x200 with 3 Axes>

Let’s compare these observations from the simulation, to the theoretical standard deviations predicted by the CLT.

(0.09097033522620938, 0.09128709291752768)
(0.052507004698028686, 0.05270462766947299)
(0.028881259542408586, 0.028867513459481287)

Example 3Z: sampling distribution as a function of nn

<Figure size 700x200 with 3 Axes>
<Figure size 700x200 with 3 Axes>

Let’s compare these observations from the simulation, to the theoretical standard deviations predicted by the CLT.

(0.31531583269320146, 0.31622776601683794)
(0.18357450148601615, 0.18257418583505536)
(0.10022896783175735, 0.1)

Example 3E: sampling distribution as a function of nn

<Figure size 700x200 with 3 Axes>
<Figure size 700x200 with 3 Axes>
(1.599726626168737, 1.5811388300841895)
(0.9078552687478914, 0.9128709291752769)
(0.50136020903652, 0.5)

Discussion

Sampling distributions of other statistics

(0.5002836237852907, 0.04103096896120843)
(0.5002836237852907, 0.04103096896120843)

CLT is the basis for all analytical approximations in statistics

CLT is the reason why statistics works. We can use properties of sample to estimate the parameters of the population, and our estimates get more and more accurate as the samples get larger.

Exercises

See main text.