Equal means for two samples do not mean they come from the same distribution: one may concentrate in the middle while the other puts more mass at the ends. The Epps–Singleton (ES) two-sample test turns this question into a comparison of empirical characteristic functions. Its null is that the two independent samples come from the same population distribution, not merely that their means agree.[1]

From a sample to a few frequencies

For a sample x₁,…,xₙ, the empirical characteristic function at frequency s is φ̂(s)=n⁻¹∑exp(isxⱼ). By Euler’s formula, its real part is the sample average of cos(sxⱼ) and its imaginary part the sample average of sin(sxⱼ). Each frequency can be read as a lens on the shape of the data: transform every observation into cosine and sine, then compare the two groups’ averages of those transforms.

SciPy’s default uses two distinct positive numbers t=(0.4, 0.8), scaled by the pooled sample’s semi-interquartile range σ=(Q75−Q25)/2, so the frequencies actually used are t/σ.[1] For each group one obtains the cosine means at both frequencies and the sine means at both frequencies, yielding a four-dimensional vector. Frequencies should be chosen before the analysis; repeatedly changing them after seeing results until the p-value becomes significant changes the interpretation of the test.

Measure differences together with covariance

The four components are not mutually uncorrelated, so an ordinary Euclidean distance between the two vectors is not enough. Let g(z)=[cos(t₁z/σ), cos(t₂z/σ), sin(t₁z/σ), sin(t₂z/σ)], let d be the difference of the two groups’ mean vectors, and let the sample sizes be nₓ and nᵧ with total n. Let Cₓ and Cᵧ be the covariance matrices of each group’s g vectors computed with sample size as the denominator; then S=(n/nₓ)Cₓ+(n/nᵧ)Cᵧ, and the test statistic is W=n·dᵀS⁺d, where S⁺ is the pseudoinverse of S.

This quadratic form weighs component differences together with their variability and correlation. SciPy computes the p-value from an asymptotic chi-square distribution whose degrees of freedom equal the rank of the pseudoinverse; when the covariance is rank-deficient the degrees of freedom need not equal four, so check any warnings the routine emits.[1] If the pooled semi-interquartile range is zero, the scaling above cannot be applied directly.

Same mean, different distributional shape

Construct two teaching samples of 40 values each, taking values in {0, 1, 2, 3, 4}. X has frequencies 2, 8, 20, 8, 2 on those five values; Y has frequencies 10, 8, 4, 8, 10. Both means equal 2, but half of X’s observations sit at 2 while half of Y’s sit at the extremes 0 and 4.

Discrete frequency proportions of the constructed samples with empirical characteristic-function real and imaginary parts and preset frequency lines

Two constructed discrete samples of 40 values each, both with sample mean 2. Left: frequency proportions; center and right: real and imaginary parts of the empirical characteristic functions; vertical dashed lines mark the preset frequencies after scaling by the pooled semi-interquartile range. Test values were checked by an independent feature-covariance calculation against SciPy.

In this example the pooled semi-interquartile range is 1, so the two working frequencies remain 0.4 and 0.8. At 0.8 the sine means for X and Y are about 0.7754 and 0.3639; that is only one of the four comparison components, and the statistic also combines the others with the covariance. The calculation yields W≈29.0909 on 4 degrees of freedom, with asymptotic p≈0.00000749. At the usual 0.05 significance level, this constructed sample rejects the null that the two populations share the same distribution.

The code below rebuilds the samples from frequencies and computes both SciPy’s result and the matrix formula above. Each group has 40 observations, so SciPy’s small-sample correction is not triggered here.[1]

import numpy as np
from scipy import stats

values = np.arange(5)
x = np.repeat(values, [2, 8, 20, 8, 2])
y = np.repeat(values, [10, 8, 4, 8, 10])
t = np.array([0.4, 0.8])

result = stats.epps_singleton_2samp(x, y, t=t)
print(result.statistic, result.pvalue)

pooled = np.concatenate([x, y])
q25, q75 = np.percentile(pooled, [25, 75])
sigma = (q75 - q25) / 2
if sigma == 0:
    raise ValueError("pooled semi-interquartile range is zero")

def features(sample):
    angles = np.outer(sample, t / sigma)
    return np.column_stack((np.cos(angles), np.sin(angles)))

gx, gy = features(x), features(y)
nx, ny = len(x), len(y)
n = nx + ny
d = gx.mean(axis=0) - gy.mean(axis=0)
S = ((n / nx) * np.cov(gx, rowvar=False, bias=True)
     + (n / ny) * np.cov(gy, rowvar=False, bias=True))
S_inv = np.linalg.pinv(S)
W = n * d @ S_inv @ d
df = np.linalg.matrix_rank(S_inv)
p = stats.chi2.sf(W, df)
print(W, df, p)

How to read the result, and when to use the test

A small p-value means that if the two groups truly came from the same distribution, a statistic at least this large would be uncommon under the asymptotic chi-square calibration used. What is detected here is a difference in distributional shape in the constructed sample; it does not say the means differ, and it is neither an effect size nor a causal conclusion. Reporting frequencies or a distribution plot alongside the test makes clear where the difference appears.

The ES test can be used for discrete distributions; SciPy’s documentation also recommends it for continuous samples with at least 25 observations per group. Each group needs at least 5 observations. The two groups should be independent samples; technical replicate readings on the same study unit cannot be treated as independent units to inflate the sample size; paired or clustered repeated observations need methods that match their dependence structure.[1]

SciPy returns an asymptotic chi-square p-value, not a finite-sample exact p-value; when both sample sizes are below 25 the routine applies a small-sample correction to the statistic.[1] Moreover, equal characteristic-function values at a few frequencies do not prove that the full characteristic functions—or the population distributions—agree. Failing to reject the null is a different conclusion from proving the two distributions are identical.

References

  1. [1] SciPy community. scipy.stats.epps_singleton_2samp. SciPy v1.18.0 Manual.