Skip to content

BCompare groups · Guide test 153

Two sample Hotelling T squared test

Compares independent multivariate mean vectors under a common covariance assumption.

Runs in CodeSplashPython example validatedR example validated

Do it in CodeSplash

In the app, set it up in Analyze with this method:

In Hotelling's T² (one-sample, two-sample, paired), choose Covariance matrices = Equal (pooled T²) or choose Design = Two independent samples.

Do it in CodeSplash Run this test on your own data, in Python or R.

Bench

Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.

Bench · Two sample Hotelling T squared test

20
25
0.50
0.10
0.50
0.050

Your data

Simulated with known truth

2 groups on two measurements; crosses mark the group means.

If H₀ were true

F = 4.21
  1. The reference: how F varies across samples when H₀ is true.
  2. Your statistic, from the data above.
  3. The p-value: the area at least as extreme as yours (p = .022).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

T2=n1n2n1+n2 (xˉ1−xˉ2)⊤Sp−1(xˉ1−xˉ2),F=n1+n2−p−1p(n1+n2−2)T2T^2 = \dfrac{n_1 n_2}{n_1+n_2}\,(\bar{\mathbf x}_1-\bar{\mathbf x}_2)^{\top}\mathbf S_p^{-1}(\bar{\mathbf x}_1-\bar{\mathbf x}_2),\quad F = \dfrac{n_1+n_2-p-1}{p(n_1+n_2-2)}T^2

With your numbers

T2=8.61,F=4.21 on (2,42) dfT^2 = 8.61,\qquad F = 4.21\ \text{on}\ (2, 42)\ df
Sp\mathbf S_p
pooled covariance matrix
xˉg\bar{\mathbf x}_g
mean vector of group g
p=2p = 2
number of variables

The verdict

F(2, 42) = 4.21, p = .022

p ≤ α = .050: reject H₀

One test for both variables that uses their correlation; it assumes equal covariance matrices (Box's M) and roughly normal data.

Statistic
4.207
p-value
0.0216
Two sample Hotelling T squared test on two groups of 20 and 25 rows on two variables: F(2, 42) = 4.21, p = .022. The p-value is below α = .050, so the data are hard to reconcile with H0.

Two sample Hotelling T squared test on two groups of 20 and 25 rows on two variables: F(2, 42) = 4.21, p = .022. The p-value is below α = .050, so the data are hard to reconcile with H₀.

Question and hypotheses

H0: two population mean vectors are equal.

Assumptions

Independent multivariate normal samples with equal covariance matrices; n1+n2>p+1.

Formula

T2=n1n2n1+n2(xˉ1−xˉ2)TSp−1(xˉ1−xˉ2)F=n1+n2−p−1p(n1+n2−2)T2\begin{gathered}T^2=\frac{n_1n_2}{n_1+n_2}(\bar x_1-\bar x_2)^TS_p^{-1}(\bar x_1-\bar x_2)\\[5pt] F=\frac{n_1+n_2-p-1}{p(n_1+n_2-2)}T^2\end{gathered}

Parameters and notation

Sp=[(n1−1)S1+(n2−1)S2]/(n1+n2−2); F df=p,n1+n2−p−1.

Structure of the method

Estimate two mean vectors and pool their unbiased within-group covariances under a common-covariance assumption. Form the covariance-standardized difference with the two-sample effective size, then use its F transformation with the declared response dimension.

Estimated and nuisance parameters

Both population means and the common covariance are estimated. Separate sample covariances are intermediate quantities. Group labels and response selection are design information, not trained features.

Settings and design choices

Fix the variable set, group definition, alpha and covariance assumptions. Choosing a ridge parameter or dimension-reduction basis after viewing the group separation changes the statistic and needs selection-aware calibration.

Fitting and adaptation

For new independent samples refit both means and the pooled covariance. Under a null bootstrap, impose a common mean and retain the intended covariance model; ordinary label permutations require exchangeability, which equality of means alone does not imply under unequal covariances. Paired data instead call for a one-sample analysis of vector differences. No neural adaptation is involved.

Sensitivity and robustness

Unequal covariances combined with unequal sample sizes can distort inference. Collinearity, p near available residual degrees of freedom, nonnormal tails and dependent observations also matter. A significant global mean difference does not identify which response changed without justified simultaneous follow-up analysis.

How to use and calibrate

Use the upper F tail; for matched multivariate data apply entry 152 to difference vectors.

Example application

Compare correlated outcome profiles in two independent groups.

Interpretation and pitfalls

Watch out

Unequal covariances need a multivariate Welch/James-type method or valid resampling.

What changed in edition 2

  • New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.

Python and R

Pooled two-sample Hotelling test assumes equal covariance matrices and independent multivariate normal groups.

Python · native

Validated — Executed successfully in the validation environment · needs numpy, scipy

import numpy as np
from scipy import stats
rng = np.random.default_rng(2026)
X=rng.normal(size=(40,3));Y=rng.normal(.3,1,size=(50,3))
n,p=X.shape;m=len(Y);S=((n-1)*np.cov(X,rowvar=False)+(m-1)*np.cov(Y,rowvar=False))/(n+m-2);d=X.mean(0)-Y.mean(0);T2=n*m/(n+m)*d@np.linalg.solve(S,d);F=(n+m-p-1)*T2/(p*(n+m-2))
print(T2,F,stats.f.sf(F,p,n+m-p-1))
Output
7.803785261277084 2.542142168446323 0.06156789422477455
R

Validated — Executed successfully in the validation environment · needs Base/standard library

set.seed(2026)
X<-matrix(rnorm(120),ncol=3);Y<-matrix(rnorm(150,mean=.3),ncol=3)
n<-nrow(X);m<-nrow(Y);p<-ncol(X);S<-((n-1)*cov(X)+(m-1)*cov(Y))/(n+m-2);d<-colMeans(X)-colMeans(Y);T2<-n*m/(n+m)*drop(t(d)%*%solve(S,d));F<-(n+m-p-1)*T2/(p*(n+m-2));print(c(T2=T2,F=F,p=pf(F,p,n+m-p-1,lower.tail=FALSE)))
Output
          T2            F            p 
2.439872e+01 7.948068e+00 9.729572e-05

Sources

Statistics guide v4 (historical):
R: Executed successfully in the validation environment
Reference's record:
Edition 2 review: Python: executed · R: not executed (Rscript unavailable). Python: Ran in the reference's review, on this exact code. R: Reviewed but not run: R wasn't available to the reference's authors.

From the CodeSplash statistics guide (v4), entry 153, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT153.