Deciding Whether Two Samples Share the Same Distribution
Statistics & ExperimentsModel answerLast updated
Walk through univariate and multivariate methods for comparing two distributions, covering test selection, effect sizes, multiple-testing control, and sampling design.
You have collected data from two separate systems or populations and want to know whether they follow the same underlying distribution.
Explain how you would approach this comparison, starting from a single variable and scaling up to many features at once. Also describe how sampling design affects the validity of the comparison.
Constraints and Assumptions
- State your null and alternative hypotheses explicitly.
- Choose your test based on variable type and what aspect of the distribution matters (mean, tails, full shape, proportions).
- Discuss when standard independence assumptions break down.
- Pair any significant p-value with a measure of practical importance.
- Address multiple comparisons, statistical power, and how the data were collected.
Clarifying Questions Worth Asking
- What decision will follow from this test?
- Are observations independent, or are they grouped by some unit such as a user, region, or time window?
- Should both samples represent the same target population?
- Are we comparing one variable or many?
- Do rare events or tail behavior matter more than the average?
Part 1 — Single-Variable Testing
How would you test whether two samples come from the same distribution for a single feature?
Cover at minimum:
- How to write down H₀ and H₁.
- At least two tests suited to continuous variables.
- At least one test suited to categorical variables.
- When a mean-comparison test is sufficient versus when you need a full-distribution test.
Part 2 — Comparing High-Dimensional Samples
How would you extend the comparison when you have many features simultaneously?
Cover at minimum:
- At least two methods that work in high dimensions without strong distributional assumptions.
- One method that does require distributional assumptions and what those are.
- How to diagnose which features drive any detected difference.
Part 3 — Interpreting Results and Handling Practical Complications
A test returns a small p-value. What do you do next?
Cover at minimum:
- Effect size and confidence intervals.
- How to set a business-relevant threshold for "meaningful" difference.
- What to do when you are testing many features at once.
- How to handle clustered or non-independent observations.
- How to reason about statistical power when the sample is small.
Part 4 — Sampling Design
What sampling strategies are available when you get to decide how evaluation data are collected, and how do they affect the comparison?
Cover at minimum:
- Simple random, stratified, cluster, and systematic sampling.
- Reservoir sampling for streaming data.
- Importance or weighted sampling and when you would reweight.
- The risk of convenience sampling and how to spot it.
Hints
Hint 1
Start by asking what the test needs to decide. A narrow question about means calls for a t-test; a question about full distributional shape calls for KS or energy distance. Matching the test to the question avoids over- or under-powered conclusions.
Hint 2
For high-dimensional data, consider training a binary classifier to separate the two samples. If it achieves accuracy well above 50%, the distributions differ in some detectable way. The features with the highest importance scores then tell you where the difference lives.
Hint 3
A statistically significant result is not automatically a meaningful one. With large samples even a negligible shift produces a tiny p-value. Always report an effect size — Cohen's d, Wasserstein distance, or a lift percentage — alongside the p-value so a stakeholder can judge whether the gap actually matters.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Setting Up the Problem
Before choosing any test, clarify what the result will be used for. Testing whether a new data pipeline produces the same feature distribution as the old one is different from testing whether two cities have similar ride-time distributions. The decision downstream determines which aspects of the distribution matter most: mean, variance, tails, or full shape.
Null and alternative hypotheses: In the general case, H₀ says the two populations share the same distribution (P = Q), and H₁ says they do not. If the question is specifically about means, restate accordingly: H₀: μ₁ = μ₂. Being precise here determines the test and what rejecting H₀ actually means.
Part 1 — Single-Variable Tests
Continuous variables — full distribution
The Kolmogorov-Smirnov (KS) test measures the largest gap between the two empirical CDFs. It is distribution-free and easy to compute, but it is most sensitive to differences near the median and less sensitive in the tails. Anderson-Darling gives more weight to the tails, making it preferable when rare events matter. For a scalar summary of distributional distance, the Wasserstein-1 distance (also called earth mover's distance) is interpretable in the original units and pairs naturally with a permutation test when the sample is small or the distribution is unusual.
Continuous variables — means only
When only the average matters, Welch's t-test is appropriate. Unlike the pooled t-test, Welch's version does not assume equal variances, making it the safer default. It loses information about shape and tails, so use it only when the question is genuinely narrow.
Categorical variables
For proportions across categories, the chi-squared test works when expected cell counts are at least 5. When cells are sparse — common with rare labels or small samples — switch to Fisher's exact test, which is exact by construction.
Part 2 — Multivariate Comparisons
Applying a univariate test to each feature independently misses joint dependencies and inflates false-positive risk. Several approaches handle the full multivariate case.
Classifier two-sample test (C2ST): Pool both samples and label them 0 or 1. Train a binary classifier (gradient boosting works well) and evaluate its cross-validated accuracy. If the distributions are identical the classifier cannot do better than 50% accuracy. Accuracy meaningfully above 50% is evidence they differ. The practical bonus is that feature importances from the classifier identify which variables drive the separation.
Maximum Mean Discrepancy (MMD): MMD computes a kernel-based distance between the two distributions in a reproducing kernel Hilbert space. It equals zero if and only if P = Q. Significance is assessed via permutation. MMD is parameter-free given a kernel choice (RBF with median heuristic bandwidth is a reasonable default) and scales reasonably with sample size.
Energy distance: A kernel-free measure of distributional distance that also reduces to zero under the null. Shares the permutation-test workflow with MMD.
Hotelling's T²: The multivariate generalization of the t-test for mean vectors. It requires multivariate normality and is sensitive to that assumption in high dimensions. Use it when the question is specifically about mean vectors and the dimensionality is modest (p ≪ n).
Feature-level diagnostics: Even after a global test, report per-feature KS statistics or standardized mean differences so teams know where to act.
Part 3 — Interpreting Results and Handling Complications
Effect size is not optional. With a large enough sample, a difference of 0.001 in a mean will be statistically significant. Always accompany a p-value with a practical measure: Cohen's d for means, Wasserstein distance in original units, or a percentage lift on a business metric. The decision threshold for "meaningful" should be set before looking at data, ideally from domain knowledge or cost-benefit analysis.
Multiple testing: Testing k features independently at α = 0.05 gives roughly 1 − 0.95ᵏ probability of at least one false positive. Bonferroni correction (divide α by k) controls the family-wise error rate conservatively. Benjamini-Hochberg (BH) controls the false discovery rate instead, which is more powerful when many features genuinely differ and you can tolerate some false positives among the flagged ones.
Clustered observations: If observations are grouped — multiple rides per driver, multiple transactions per user — treating them as independent inflates effective sample size. Use a clustered bootstrap: resample whole clusters, not individual observations, when constructing confidence intervals or permutation null distributions.
Statistical power: With small samples, even a large true difference can fail to reach significance. Compute power prospectively using the expected effect size and desired α. If power is low, the test is more informative about sample size than about the distributions.
Part 4 — Sampling Design
How data are collected can invalidate a comparison before any test is run if the two samples do not represent the same target population.
| Method | When to use |
|---|---|
| Simple random sampling | Default; each unit has equal selection probability. Easy to analyze. |
| Stratified sampling | When subgroups matter and you want guaranteed representation of each. Reduces variance on stratum-level estimates. |
| Cluster sampling | When a complete list of individuals is unavailable but a list of groups (cities, routes) exists. Less precise than stratified but operationally feasible. |
| Multistage sampling | Combines cluster and random sampling within clusters. Common in large geographic surveys. |
| Systematic sampling | Sample every kth unit from an ordered list. Efficient, but beware periodic patterns in the list that align with k. |
| Reservoir sampling | Maintains a uniform random sample of size n over a stream of unknown total length. Essential for online systems. |
| Importance / weighted sampling | Deliberately oversample rare strata, then reweight by the inverse of the inclusion probability before computing statistics. Use when tail events dominate the decision. |
| Convenience sampling | Data from whatever is easiest to collect. Often introduces selection bias. Flag it explicitly and consider reweighting if population membership probabilities can be estimated. |
The key question is whether the sampling mechanism is the same for both datasets. If one sample was stratified and the other was not, apparent distributional differences may reflect the sampling design rather than the underlying populations.
Common Mistakes
- Running only a t-test when full distributional shape matters.
- Ignoring clustering and treating correlated observations as independent.
- Stopping at the p-value without reporting an effect size.
- Testing many features at once without any multiple-testing correction.
- Comparing two samples that were collected under different sampling designs and attributing differences to the populations.