1. When Parametric Assumptions Fail
Classical parametric tests (such as Student's $t$-test and ANOVA) assume data is drawn from continuous Gaussian distributions.
However, in many real-world software scenarios:
- Data is categorical (e.g., error codes, operating system choice, subscription tier).
- Data is ordinal (e.g., star ratings 1 to 5, user satisfaction tiers).
- Data has extreme outliers and extreme skew where normal approximations completely fail.
Non-parametric tests make no assumptions regarding the underlying mathematical probability distribution.
text
2. Chi-Square ($\chi^2$) Goodness-of-Fit Test
Tests whether an observed categorical frequency distribution $O_1, O_2, \dots, O_k$ matches an expected theoretical probability distribution $E_1, E_2, \dots, E_k$.
2.1 The Test Statistic
$$\chi^2 = \sum_{i=1}^k \frac{(O_i - E_i)^2}{E_i} \sim \chi^2_{k - 1}$$
where:
- $O_i$ is the observed count in bucket $i$.
- $E_i = n \cdot p_i$ is the expected count under $H_0$.
- Degrees of freedom: $\nu = k - 1$ (minus additional estimated parameters).
2.2 Application: Testing Random Number Generator Uniformity
Testing whether a hash function distributes $n = 10,000$ keys uniformly across $k = 10$ shards ($E_i = 1000$ for all $i$):
- If $\chi^2$ is small, differences between observed and expected counts are consistent with pure random chance.
- If $\chi^2 > \chi^2_{\text{crit}}$, shard routing is non-uniform (hash collision bias detected!).
Cochran's Criterion: The Chi-Square approximation requires that all expected counts $E_i \ge 1$ and at least $80%$ of cells have $E_i \ge 5$.
3. Chi-Square Test of Independence (Contingency Tables)
Used to test whether two categorical variables are statistically independent (e.g., Browser Type vs Subscription Conversion).
Given an $r \times c$ contingency table:
| Browser | Converted | Not Converted | Row Total ($R_i$) |
|---|---|---|---|
| Chrome | $O_{11}$ | $O_{12}$ | $R_1$ |
| Safari | $O_{21}$ | $O_{22}$ | $R_2$ |
| Firefox | $O_{31}$ | $O_{32}$ | $R_3$ |
| Column Total ($C_j$) | $C_1$ | $C_2$ | Total $N$ |
3.1 Expected Counts under Null Hypothesis ($H_0: \text{Independent}$)
Under independence, $P(\text{Row } i \cap \text{Col } j) = P(\text{Row } i) P(\text{Col } j) = \frac{R_i}{N} \frac{C_j}{N}$.
Multiplying by total sample size $N$:
$$E_{ij} = \frac{R_i \cdot C_j}{N}$$
3.2 Test Statistic & Degrees of Freedom
$$\chi^2 = \sum_{i=1}^r \sum_{j=1}^c \frac{(O_{ij} - E_{ij})^2}{E_{ij}} \sim \chi^2_{\nu}$$
$$\text{Degrees of Freedom: } \nu = (r - 1)(c - 1)$$
3.3 Fisher's Exact Test (Small Samples)
When cell counts are small ($E_{ij} < 5$), the asymptotic $\chi^2$ approximation fails. For a $2 \times 2$ table:
$$P = \frac{\binom{a + b}{a} \binom{c + d}{c}}{\binom{N}{a + c}} = \frac{(a+b)!(c+d)!(a+c)!(b+d)!}{a! , b! , c! , d! , N!}$$
Fisher's test computes exact hypergeometric probabilities without any large-sample approximations.
4. Rank-Based Non-Parametric Tests
When continuous data is heavily skewed (e.g., web latencies), rank-based tests discard raw values and analyze order ranks $1, 2, \dots, N$. This renders the test immune to extreme outliers!
4.1 Mann-Whitney $U$ Test (Wilcoxon Rank-Sum)
Non-parametric test for comparing two independent groups:
$$H_0: P(X > Y) = P(Y > X) = 0.5 \quad \text{(Stochastically identical)}$$
Algorithm:
- Combine observations from Group A ($n_1$) and Group B ($n_2$) into a single pool of size $N = n_1 + n_2$.
- Sort all $N$ values in ascending order and assign ranks $1, 2, \dots, N$.
- Compute the rank sum $R_1$ for Group 1 and $R_2$ for Group 2.
- Compute the $U$-statistics: $$U_1 = n_1 n_2 + \frac{n_1(n_1 + 1)}{2} - R_1, \qquad U_2 = n_1 n_2 + \frac{n_2(n_2 + 1)}{2} - R_2$$ $$U = \min(U_1, U_2)$$
- For large samples ($n_1, n_2 > 20$), $U$ is normally distributed with: $$\mu_U = \frac{n_1 n_2}{2}, \qquad \sigma_U = \sqrt{\frac{n_1 n_2 (n_1 + n_2 + 1)}{12}}$$
4.2 Wilcoxon Signed-Rank Test (Paired Samples)
The non-parametric counterpart to the paired $t$-test:
- Calculate differences $d_i = x_{1, i} - x_{2, i}$. Discard pairs with $d_i = 0$.
- Rank the absolute differences $|d_i|$ from smallest to largest.
- Sum the ranks corresponding to positive differences ($W^+$) and negative differences ($W^-$).
- The test statistic is $W = \min(W^+, W^-)$.
4.3 Kruskal-Wallis $H$-Test (3+ Groups)
The non-parametric counterpart to One-Way ANOVA:
Ranks all observations across all $k$ groups simultaneously:
$$H = \frac{12}{N(N + 1)} \sum_{j=1}^k \frac{R_j^2}{n_j} - 3(N + 1) \sim \chi^2_{k - 1}$$
where $R_j$ is the sum of ranks in group $j$, and $N = \sum n_j$.
5. Python Implementation: Chi-Square & Mann-Whitney U
python
6. Key Takeaways
- Robust to Outliers: Rank-based tests operate on ordinal ordering, making them immune to extreme numerical spikes.
- Chi-Square for Categorical Data: Use goodness-of-fit to test distribution conformity, and contingency tables to test independence.
- Fisher's Exact for Sparse Cells: When expected counts $E_{ij} < 5$, use Fisher's exact test.