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 3.4 — Hypothesis testing using analytical approximations

Section 3.4 — Hypothesis testing using analytical approximations

This notebook contains the code examples from Section 3.4 Hypothesis testing using analytical approximations of the No Bullshit Guide to Statistics.

Notebook setup


[notice] A new release of pip is available: 26.2.1 -> 26.2.2
[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>
datasets/ directory already exists.

\def\stderr#1{\mathbf{se}_{#1}} \def\stderrhat#1{\hat{\mathbf{se}}_{#1}} \def\Mean{\textbf{mean}} \def\Var{\textbf{var}} \def\Std{\textbf{std}} \def\Freq{\textbf{freq}} \def\RelFreq{\textbf{relfreq}} \def\CI#1{\textbf{CI}_{#1}} \def\CIL#1{\textbf{L}_{#1}} \def\CIU#1{\textbf{U}_{#1}} \def\ci#1{\textbf{ci}_{#1}} \def\cil#1{\textbf{l}_{#1}} \def\ciu#1{\textbf{u}_{#1}} \def\DMeans{\textbf{dmeans}} \def\Prop{\textbf{prop}} \def\DProps{\textbf{dprops}} \def\Err{ {\Large \varepsilon}} \def\calN{\mathcal{N}} \def\Tdist{\mathcal{T}} \def\Expon{\mathrm{Expon}}

(this cell contains the macro definitions like sex‾\stderr{\overline{\mathbf{x}}}, se^\stderrhat{}, mean\Mean, ...)

Definitions

Context

  • test statistic

  • sampling distribution of the test statistic

  • pivotal transformation

  • reference distributions

Examples of analytical formulas for sampling distributions

Normal distribution of the sample mean

If we know that σX0=10\sigma_{X_0} = 10, we can obtain an exact formula for the sampling distribution of the mean from the central limit theorem.

<Figure size 500x160 with 1 Axes>

Student’s tt-approximation to sampling distribution of the mean

Suppose now that the population standard deviation σK\sigma_K is not known. In this case, we can use the sample standard deviation sxs_{\mathbf{x}} as an estimate for σX\sigma_X.

8.519494731273129

When using the estimated standard deviation sk=8.52s_{\mathbf{k}} = 8.52, we can approximate the sampling distribution of the mean in terms of Student’s tt-distribution.

<Figure size 500x160 with 1 Axes>

The χ2\chi^2-approximation to the sample variance

<Figure size 500x160 with 1 Axes>

Calculating pp-values

<Figure size 900x230 with 3 Axes>

Pivotal transformations

Calculating pp-values for standardized test statistics

<Figure size 900x230 with 3 Axes>

Test for the mean (known variance)

Analytical approximation for the sample mean

If the theoretical distribution under the null is normally distributed X0∼N(μX0,σX0)X_0 \sim \mathcal{N}(\mu_{X_0}, \sigma_{X_0}), then the central limit theorem tells us the sampling distribution of the mean is described by the formula

X‾0∼N(μX0,σX0n).\overline{\mathbf{X}}_0 \sim \mathcal{N}(\mu_{X_0}, \frac{\sigma_{X_0}}{\sqrt{n}}).

Kombucha bottling process

We’ll use the kombucha scenario for all the examples in this section. Recall, the probability distirbution of the kombucha volume is described by the theoretical model K0∼N(μK0=1000,σK0=10)K_0 \sim \mathcal{N}(\mu_{K_0} = 1000, \sigma_{K_0}=10) when the production line is working as expected.

We can use central limit theorem to obtain the sampling distribution of the mean since the parameters μK0\mu_{K_0} and σK0\sigma_{K_0} are known:

K‾0∼N(μK0,σK0n).\overline{\mathbf{K}}_0 \sim \mathcal{N}(\mu_{K_0}, \frac{\sigma_{K_0}}{\sqrt{n}}).

Example 1N: test for the mean of Batch 04

40
1003.8335
1.5811388300841895

We’ll perform pp-value calculations based on the standardized test statistic

zk=k‾−μK0sek‾,0.z_{\mathbf{k}} = \frac{ \overline{\mathbf{k}} - \mu_{K_0} }{ \stderr{\overline{\mathbf{k}},0} }.
2.42451828205107

The reference distribution is the standard normal distribution Z0∼N(loc=0,  scale=1)Z_0 \sim \mathcal{N}(\tt{loc}=0, \; \tt{scale}=1).

We can use cumulative distribution function FZ0F_{Z_0}.

0.015328711497996474
<Figure size 500x160 with 1 Axes>

Alternative calculation without pivotal transformation

Note also the pivotal transformation to the standard ZZ is not necessary. We could have obtained the same pp-value directly from the sampling distribution of the mean, which is described by a non-standard normal distribution:

K‾0=N(loc=μK0,  scale=sek‾04,0)\overline{\mathbf{K}}_0 = \mathcal{N}(\texttt{loc}=\mu_{K_0},\; \texttt{scale}=\stderr{\overline{\mathbf{k}}_{04},0})
3.833499999999958
0.015328711497996474
<Figure size 500x160 with 1 Axes>

The pp-value we obtain is exactly the same.

Example 2N: test for the mean of Batch 01

40
999.10375
1.5811388300841895

Note the standard error is the same as se04 we calculated in Example 1N. This is because the sample size is the same, and we’re relying on the same assumptions about the standard deviation of the theoretical distribution.

-0.5668382705851878
0.5708240666473916

The pp-value is large, so there is no reason to reject H0H_0. We conclude that Batch 01 must be regular.

<Figure size 500x160 with 1 Axes>

Test for the mean (unknown variance)

Analytical approximation based on Student’s tt-distribution

Consider again a theoretical distribution X0∼N(μX0,σX0)X_0 \sim \mathcal{N}(\mu_{X_0}, \sigma_{X_0}), but this time assume that σX0\sigma_{X_0} is not known.

The sampling distribution of the mean X‾0\overline{\mathbf{X}}_0 for samples of size nn, after applying the location-scale transformation, can be modelled in terms of the standard tt-distribution with n−1n-1 degrees of freedom:

T0=X‾0−μX0se^x‾    ∼    T(df=n−1,  loc=0,  scale=1).T_0 = \frac{ \overline{\mathbf{X}}_0-\mu_{X_0} }{ \stderrhat{\overline{\mathbf{x}}} } \;\; \sim \;\; \mathcal{T}(\tt{df}=n-1, \; \tt{loc}=0, \; \tt{scale}=1).

Example 1T: test for the mean of Batch 04

Assume we know mean, but not variance

40
7.852174139558339
1.24153774326386

Recall the value se04 = sek‾04,0=σK040=1.58\stderr{\overline{\mathbf{k}}_{04},0}=\frac{\sigma_{K_0}}{\sqrt{40}} = 1.58, which we obtained by assuming the population standard deviation is known. We see that sehat04 is an underestimate, because the sample standard deviation s04 happens to be smaller than the true population standard deviation σK0=10\sigma_{K_0} = 10.

1003.8335
3.0877031494202725
0.0037056653503329314

Effect size estimates

We can estimate the effect size using the formula Δ^=k‾04−μK0\widehat{\Delta} = \overline{\mathbf{k}}_{04} - \mu_{K_0}.

3.833499999999958

We can also obtain confidence interval for the effect size ciΔ,0.9\ci{\Delta,0.9} by first computing a 90% confidence interval for the population mean ciμ,0.9\ci{\mu,0.9}, then subtracting theoretical mean μK0\mu_{K_0}.

[1001.7416639437092, 1005.9253360562907]
[1.7416639437092272, 5.925336056290689]

Example 2T: test for the mean of Batch 01

1.5446402654597249

cf. se01 = sek‾01,0=σK040=1.58\stderr{\overline{\mathbf{k}}_{01},0}=\frac{\sigma_{K_0}}{\sqrt{40}} = 1.58,

999.10375
-0.5802321874169595
0.5650956637295473
<Figure size 500x160 with 1 Axes>

Generic function

def ttest_mean(sample, mu0, alt="two-sided"): """ T-test to detect mean deviation from a population with known mean `mu0`. """ assert alt in ["greater", "less", "two-sided"] obsmean = np.mean(sample) n = len(sample) std = np.std(sample, ddof=1) sehat = std / np.sqrt(n) obst = (obsmean - mu0) / sehat rvT = tdist(df=n-1) pvalue = tailprobs(rvT, obst, alt=alt) return pvalue

Test for the variance

Formula for sampling distribution of the variance

Chi-square test for variance

If the theoretical distribution under the null hypothesis is normal K0∼N(μK0,σK0)K_0 \sim \mathcal{N}(\mu_{K_0}, \sigma_{K_0}), then the sampling distribution of variance for samples of size nn is described by a scaled chi-square distribution:

SK,02    ∼    χ2(df ⁣= ⁣n−1,  scale ⁣= ⁣σK02n−1)  =  σK02n−1⋅χ2(n−1).S_{\mathbf{K},0}^2 \;\; \sim \;\; \chi^2(\tt{df}\!=\!n-1, \; \tt{scale}\!=\!\tfrac{ \sigma_{K_0}^2 }{ n-1 } ) \; = \; \tfrac{ \sigma_{K_0}^2 }{ n-1 } \cdot \chi^2(n-1).

Example 3X: test for the variance of Batch 02

20
124.31760105263139

We can now compute the qq-statistic, which is the observed sample variance estimate sk022=124.32s_{\mathbf{k}_{02}}^2 = 124.32 divided by the scale factor σK02n−1\tfrac{ \sigma_{K_0}^2 }{ n-1 }.

23.620344199999963
0.21112073283603838

The pp-value is large, so there is no reason to reject H0H_0.

<Figure size 500x160 with 1 Axes>

Example 4X: test for the variance of Batch 08

40
169.9979220512824
66.29918960000013
0.0041211873587608805

The pp-value is vary small, so we reject H0H_0. According to our analysis based on the sample variance from Batch 08, this batch seems to be irregular: it has an abnormally large variance.

Effect size estimates

We can estimate the effect size using the formula Δ^=sk082/σK02\widehat{\Delta} = s^2_{\mathbf{k}_{08}} / \sigma_{K_0}^2.

1.699979220512824

We can also obtain confidence interval for the effect size ciΔ,0.9\ci{\Delta,0.9} by first computing a 90% confidence interval for the variance ciσ2,0.9\ci{\sigma^2,0.9}, then dividing by the theoretical variance σK02\sigma_{K_0}^2.

[1.2148888239061653, 2.58019779302895]

Compare this to the bootstrap confidence interval for the effect size ciΔ,0.9∗=[1.17,2.18]\ci{\Delta,0.9}^* = [1.17, 2.18] , which we obtained earlier in Section 3.3.

Alternative calculation methods

Using scipy.stats.ttest_1samp for one-sample tt-test

0.0037056653503329644
[1.7416639437092272, 5.925336056290689]

We see the pp-value and the 90% confidence interval are same the ones we calculated earlier in Example 1T.

Bootstrap estimate of the standard error

Another way to obtain the standard error of the mean (the standard deviation of the sampling distribution of the mean) is to use the bootstrap estimate.

See problems PNN and PMM in the notebook chapter3_problems.ipynb.

Explanations

Helper function for calculating pp-values

One-sided pp-value calculation check

Reusing the variables n02=20 and obsq02=23.62 we calculated in Example 3X above, we can calculate the pp-value by calling the helper function tailprobs with the option alt="greater":

0.21112073283603838

The value we obtained is identical to the value pvalue02 we obtained in Example 3X, so we know the function tailprobs works as expected for pp-value calculations of type (a), where we want to detect only positive deviations from the expected distribution under H0H_0.

Two-sided pp-value calculation check

Reusing the variable obsz04 we calculated in Example 1N above, we can calculate the pp-value by calling the function tailprobs with the option alt="two-sided":

0.015328711497996474

The value we obtained is identical to the value pvalue04 we obtained in Example 1N, so we know the function tailprobs works as expected for pp-value calculations of type (c), where we want to detect both positive and negative deviations from the expected distribution under H0H_0.

Discussion

Statistical modelling and assumptions

Exercises

Exercise NN: use integration to obtain of the probability ...

0.015328711497995967