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.

Hypothesis testing using simulation

Click here to run the notebook interactively, so you can play with the code examples.

Notebook setup


[notice] A new release of pip is available: 25.2 -> 25.3
[notice] To update, run: pip install --upgrade pip
Note: you may need to restart the kernel to use updated packages.
<Figure size 640x480 with 0 Axes>

Introduction

Statistical inference

The goal of most statistical analysis is inferential statistics, which is the process of making inferences about a population based on a sample from that population. We usually assume the population is described by some probability distribution, which is a mathematical description for the type of individuals in the population.

For example, the population of IQ scores in the is know to follow the normal distribution with mean μ\mu (read miu) and standard deviation σ\sigma (sigma), which we denote as N(μ,σ)\mathcal{N}(\mu,\sigma). By convention, we denote the parameters of the population using Greek letters. You can think of the parameters μ\mu and σ\sigma as control knobs on the distribution that determine its shape.

It is usually prohibitively expensive to measure the IQ score of every individual in the population, so instead we obtain a random sample of individuals and measure their IQ scores. The goal of statistical inference is to learn information about the population based on a sample, as illustrated by the backward arrow in Figure 1. Specifically, we want to find the population parameters μ\mu and σ\sigma.


Figure 1. Illustration of the random selection mechanism which generates the data sample and the statistical inference process, which tries to guess the population parameters.

Example data analysis scenario

We’ll use an example to ground the discussions in the rest of the notebook.

IQ scores data

Consider the following dataset, which consists of IQ scores of 30 students who took a “smart drug” ☕. The IQ scores are recorded in the following list.

30
<Figure size 700x200 with 1 Axes>

Descriptive statistics

105.89
14.658229417469641

Statistical inference by eyeball

Move the sliders to choose the model parameters that best match the data.

Loading...

Research questions

The main research question we want to answer based on the IQ scores data is whether the smart drug had an effect on or not. We know the average IQ score is μ=100\mu=100 for the population in general (not the individuals from this experiment) with standard deviation σ=15\sigma=15 IQ points. If we compare the mean IQ score we observed in the sample of students who took the smart drug iqs‾=105.89\overline{\tt{iqs}}=105.89 to people in the mean of the general population μ=100\mu=100, we see the students on the smart drug did better, which suggests the drug is effective.

On the other hand, the standard deviation of IQ scores is 15, which means that variations of +/- 15 around the mean of 100 are common, so perhaps observing a difference between observed mean and the theoretical mean of 5.89 is not uncommon.

This is precisely the situation why we need the tools of statistical inference. We’ll use the hypothesis testing procedure to calculate how “surprising” it is to observe a difference of 5.89 from the expected population mean, and thus provide a principled answer to the question if the smart drug is effective.

Before we get to the hypothesis testing procedure, we’ll need to introduce some prerequsite concepts: probability models and sampling distribributions.

Probability model for the general population

Let’s now build the specific probability model we need for the analysis of the students’ IQ scores. We know the IQ scores in the general population has mean μ=100\mu=100 and standard deviation σ=15\sigma=15. We will denote the general population as X∼N(μ=100,σ=15)X \sim \mathcal{N}(\mu=100,\sigma=15).

<Figure size 700x200 with 1 Axes>

This is the distribution of IQ scores we can expect to observe for the general population.

Random samples from the general population

We’ll use the notation X=(X1,X2,…,X30)\mathbf{X} = (X_1, X_2, \ldots, X_{30}) to describe a random sample from the general population.

To get a idea of what these random samples look like, we can generate 10 samples x1,x2,x3,…,x10\mathbf{x}_1, \mathbf{x}_2, \mathbf{x}_3, \ldots, \mathbf{x}_{10} of size n=30n=30 from X∼N(μ=100,σ=15)X \sim \mathcal{N}(\mu=100, \sigma=15) as follows.

<Figure size 800x280 with 1 Axes>

The diamond markers indicate the position of the sample means computed from each sample: [x‾1,x‾2,x‾3,…,x‾10][\overline{\mathbf{x}}_1, \overline{\mathbf{x}}_2, \overline{\mathbf{x}}_3, \ldots, \overline{\mathbf{x}}_{10}].

In the next section we’re going to talk about the distribution of the diamond shapes, which correspond to the means computed from random samples.

Sampling distributions

The sampling distribution of the mean for samples of size n=30n=30 from the general population X∼N(100,15)X \sim \mathcal{N}(100,15) is the probability distribution of the random variable X‾=Mean(X)\overline{\mathbf{X}} = \mathbf{Mean}(\mathbf{X}), where X=(X1,X2,…,X30)\mathbf{X} = (X_1, X_2, \ldots, X_{30}) is a random sample from N(100,15)\mathcal{N}(100,15). The sampling distribution describes the kind of means we can expect to observe if we compute the mean for a sample of size n=30n=30 from the standard normal.

To visualize the sampling distribution of the mean X‾=mean(X)\overline{\mathbf{X}} = \texttt{mean}(\mathbf{X}), we can generate N=1000N=1000 samples from the population model: x1,x2,x3,…,x1000\mathbf{x}_1, \mathbf{x}_2, \mathbf{x}_3, \ldots, \mathbf{x}_{1000}, then plot a histogram of the means computed from the 1000 random samples, xbars = [x‾1,x‾2,x‾3,…,x‾1000][\overline{\mathbf{x}}_1, \overline{\mathbf{x}}_2, \overline{\mathbf{x}}_3, \ldots, \overline{\mathbf{x}}_{1000}], where x‾j\overline{\mathbf{x}}_j denotes the sample mean computed from the data in the jjth sample, x‾j=mean(xj)\overline{\mathbf{x}}_j = \texttt{mean}(\mathbf{x}_j).

<Figure size 800x280 with 1 Axes>

The above figure shows the sampling distribution of the mean for samples of size n=30n=30 from the general population model X∼N(μ=100,σ=15)X \sim \mathcal{N}(\mu=100, \sigma=15). The histogram shows the “density of diamond shapes,” and provides a representation of the sampling distribution of the mean X‾=mean(X)\overline{\mathbf{X}} = \tt{mean}(\mathbf{X}).

Two ways to work with sampling distributions

You can use either a math description (analytical formulas) or a computational description (simulations) for the sampling distribution. Although the two descriptions seem very different on the surface, they both describe the same underlying concept. I’ve compiled a little list of the key properties of the estimator mean\tt{mean} to show the correspondences between two types of descriptions.

math_descr_and_comp_descr.png

The two descriptions give us two different ways to do probability calculations. For example, we can visualize the sampling distribution of the estimator mean\tt{mean}, by plotting the probability density function of the random variable X‾=mean(X)\overline{\mathbf{X}} = \tt{mean}(\mathbf{X}) or by plotting a histogram of the values [mean(x1),mean(x2),…,mean(xN)][\tt{mean}(\mathbf{x}_1), \tt{mean}(\mathbf{x}_2), \ldots, \tt{mean}(\mathbf{x}_N)].

Suppose someone asks us to compute the probability Pr⁡({a≤X‾≤b})\Pr(\{ a \leq \overline{\mathbf{X}} \leq b \}), which describes the probability that the estimate computed from a random sample of size nn will be between aa and bb. We can obtain the answer using the probability description by calculating the difference in the cumulative distribution function Pr⁡({a≤X‾≤b})=FX‾(b)−FX‾(a)\Pr(\{ a \leq \overline{\mathbf{X}} \leq b \}) = F_{\overline{\mathbf{X}}}(b) - F_{\overline{\mathbf{X}}}(a).
Using the computational approach, we can obtain the same probability calculating by computing the \emph{proportion} of observations in the list [mean(x1),mean(x2),…,mean(xN)][\tt{mean}(\mathbf{x}_1), \tt{mean}(\mathbf{x}_2), \ldots, \tt{mean}(\mathbf{x}_N)] whose values are between aa and bb.

Hypothesis testing using simulation

TODO: import from blog post and slides

We have seen this before, but now generate using N=10000N=10000 to get higher presision.

<Figure size 700x200 with 1 Axes>
105.89
0.0184
<Figure size 700x200 with 1 Axes>

Alternative pp-value calculation using formula

The classical formula approach would be to plug the numbers into the formula...

np.float64(0.01792942680682752)

Conclusion