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.

Two-sample equivalence test

To perform two-sample equivalence test, we’ll use the same procedure as Welch’s two-sample tt-test, but this time prove that two unknown population X∼N(μX,σX)X \sim \mathcal{N}(\mu_X, \sigma_X) and Y∼N(μY,σY)Y \sim \mathcal{N}(\mu_Y, \sigma_Y) are not different.

\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, ...)

Data

Two samples of numerical observations x=[x1,x2,…,xn]\mathbf{x}=[x_1, x_2, \ldots, x_n] and y=[y1,y2,…,ym]\mathbf{y}=[y_1, y_2,\ldots, y_m] from independent populations.

Modeling assumptions

We assume the unknown populations are normally distributed (NORM)\textbf{(NORM)}, or the sample is large enough (LARGEn)\textbf{(LARGEn)}.

Hypotheses

H0:∣μX−μY∣≥ΔminH_0: |\mu_X - \mu_Y| \geq \Delta_{\text{min}} versus HA:∣μX−μY∣<ΔminH_A: |\mu_X - \mu_Y| < \Delta_{\text{min}}.

equiv_test_hypotheses.png

Statistical design

for n=5n=5 ...

for n=20n=20 ...

Estimates

Compute the sample means x‾=mean(x)\overline{\mathbf{x}} = \Mean(\mathbf{x}), y‾=mean(y)\overline{\mathbf{y}} = \Mean(\mathbf{y}), and the difference between means d^=dmeans(x,y)=x‾−y‾\hat{d} = \DMeans(\mathbf{x}, \mathbf{y}) = \overline{\mathbf{x}} - \overline{\mathbf{y}}.

Formulas

The estimated standard error of the difference between means estimator is

se^d^  =  se^x‾2+se^y‾2=sx2n+sy2m,\stderrhat{\hat{d}} \; = \; \sqrt{ \stderrhat{\overline{\mathbf{x}}}^2 + \stderrhat{\overline{\mathbf{y}}}^2 } = \sqrt{ \tfrac{s_{\mathbf{x}}^2}{n} + \tfrac{s_{\mathbf{y}}^2}{m} },

where sxs_{\mathbf{x}} and sys_{\mathbf{y}} are the sample standard deviations.

Test statistic

Compute the tt-statistic t=d^−0se^d^t = \frac{\hat{d} - 0}{ \stderrhat{\hat{d}} }.

Sampling distribution

Student’s tt-distribution with νd\nu_d degrees of freedom, where the degrees of freedom parameter is computed using the helper function calcdf: νd=calcdf(sx,n,sy,m)\nu_d = \tt{calcdf}(s_{\mathbf{x}}, n, s_{\mathbf{y}}, m), which implements the Welch--Satterthwaite formula.

P-value calculation

To perform the two-sample tt-test on the samples xs and ys, we call tost_dmeans(xs, ys).

Examples

For all the examples we present below, we assume the unknown distribution are normally distributed

rvX=X∼N(μX,σX)andrvY=Y∼N(μY,σY).\texttt{rvX} = X \sim \mathcal{N}(\mu_X,\sigma_X) \qquad \text{and} \qquad \texttt{rvY} = Y \sim \mathcal{N}(\mu_Y,\sigma_Y).

Example A: populations are different

Suppose the XX-population is normally distributed with mean μX=104\mu_{X}=104 and standard deviation σX=3\sigma_{X} = 3, while the YY-population is normally distributed with mean μX=100\mu_{X}=100 and standard deviation σX=5\sigma_{X} = 5

rvX=X∼N(104,3)andrvY=Y∼N(100,5).\texttt{rvX} = X \sim \mathcal{N}(104,3) \qquad \text{and} \qquad \texttt{rvY} = Y \sim \mathcal{N}(100,5).

Let’s generate a sample xAs and yAs of size n=20n=20 from the random variables X=rvXAX = \texttt{rvXA} and Y=rvYAY = \texttt{rvYA}.

(array([105.49014246, 103.5852071 , 105.94306561, 108.56908957, 103.29753988, 103.29758913, 108.73763845, 106.30230419, 102.59157684, 105.62768013, 102.60974692, 102.60281074, 104.72588681, 98.26015927, 98.8252465 , 102.31313741, 100.96150664, 104.942742 , 101.27592777, 99.7630889 ]), array([107.32824384, 98.8711185 , 100.33764102, 92.87625907, 97.27808638, 100.55461295, 94.24503211, 101.87849009, 96.99680655, 98.54153125, 96.99146694, 109.26139092, 99.93251388, 94.71144536, 104.11272456, 93.89578175, 101.04431798, 90.20164938, 93.35906976, 100.98430618]))
4.815979892644748
<Figure size 700x100 with 1 Axes>
<Figure size 700x100 with 1 Axes>

To obtain the pp-value, we first compute the observed tt-statistic, then calculate the tail probabilities in the two tails of the standard normal distribution T0∼T(νd)T_0 \sim \mathcal{T}(\nu_d).

obst+ 0.6479055169799628   pvalue+ 0.7390885154632982
obst- 7.000076915517506    pvalue- 3.7301428501557155e-08
0.7390885154632982

The helper function ttost_ind in the statsmodels module performs exactly the same sequence of steps to compute the pp-value.

(0.7390885154633053, (7.000076915517528, 3.730142848917544e-08, 30.95572939468825), (0.6479055169799854, 0.7390885154633053, 30.95572939468825))

The pp-value we obtain is 0.739, which is above the cutoff value α=0.05\alpha=0.05, so our conclusion is we fail to reject the null hypothesis: the means of the two unknown populations differ significantly from the smallest effect size of interest Δmin\Delta_{\text{min}}.

Example B: sample from a population as expected under H0H_0

Let’s generate a sample xs of size n=20n=20 from the random variable X=rvXX = \texttt{rvX}, which has the same distribution as the theoretical distribution we expect under the null hypothesis.

(98.49345582110894, 99.90646412616485)
-1.4130083050559108
1.3075392073324235
<Figure size 700x100 with 1 Axes>
<Figure size 700x100 with 1 Axes>
obsdhatB = -1.4130083050558824
seDB = 1.3075392073324235   sesoi/seDB = 3.0591816884486405
dfDB = 37.59083188299806
obst+ = -4.139843971561842   pvalue+ = 9.438014051414072e-05
obst- = 1.978519405335439    pvalue- = 0.02761615756744984
0.02761615756744984
(0.02761615756745112, (1.978519405335417, 0.02761615756745112, 37.59083188299806), (-4.139843971561863, 9.438014051413471e-05, 37.59083188299806))

The pp-value we obtain is 0.0276, which is below the cutoff value α=0.05\alpha=0.05 so our conclusion is that we’ve reject the null hypothesis: this means of two samples are not significantly different.

Confidence interval for the effect size

The confidence interval for the effect size Δ=μX−μY\Delta = \mu_X - \mu_Y in Example A is

[2.6805293784320785, 6.951430406857417]

The confidence interval for the effect size Δ=μX−μY\Delta = \mu_X - \mu_Y in Example B is

[-3.6180594963139185, 0.7920428862020961]

Discussion

Comprare with R

> install.packages("TOSTER")
> library(TOSTER)
> tsum_TOST(m1 = 98.49345582110894, sd1 = 3.913169794238548, n1 = 20,
+           m2 = 99.90646412616485, sd2 = 4.345144155915784, n2 = 20,
+           eqb = 4, alpha = 0.05, var.equal = FALSE)

Welch Modified Two-Sample t-Test
The equivalence test was significant, t(37.59) = 1.979, p = 2.76e-02
The null hypothesis test was non-significant, t(37.59) = -1.081, p = 2.87e-01
NHST: do not reject null significance hypothesis that the effect is equal to zero 
TOST: reject null equivalence hypothesis

TOST Results 
                t    df p.value
t-test     -1.081 37.59   0.287
TOST Lower  1.979 37.59   0.028
TOST Upper -4.140 37.59 < 0.001

Effect Sizes 
               Estimate     SE              C.I. Conf. Level
Raw             -1.4130 1.3075  [-3.6181, 0.792]         0.9
Hedges g(av)    -0.3349 0.3412 [-0.8463, 0.1809]         0.9

Simulation to check TOST sensitivity

Let’s run a simulation to see how the accuracy of the TOST procedure for detecting when two samples come from the same population. We’ll use the standard normal Z∼N(0,1)Z \sim \mathcal{N}(0,1) as the population, and run ttost_ind for different choices of sesoi and sample sizes n=m.

TOST sensitivity results

<Figure size 640x480 with 1 Axes>

The above graph shows that, for n=30n=30 we need to use sesoi of around 0.7 standard deviations if we want TOST to have 80% power at detecting the equivalence.

For n=20n=20 we need sesoi to be 1 standard deviation if we want 80% power.