Sub topic 7
Testing one feature at a time: KS and chi-square
This chapter assumes you know basic hypothesis tests and p-values, can use NumPy and SciPy, and have already chosen a reference window and a current window (see the previous chapter). Here you test each feature on its own to see whether its distribution changed.
The working model
A univariate drift test asks one question per feature: could the reference values and the current values come from the same distribution? The null hypothesis is that they do. A small p-value means the data is unlikely under that assumption, so you flag the feature.
Pick the test by feature type:
| Feature type | Test | What it compares |
|---|---|---|
| Numeric | Two-sample Kolmogorov-Smirnov (KS) | Empirical cumulative distribution functions |
| Categorical | Chi-square test of independence | Category counts in the two windows |
KS test for numeric features
The KS statistic is the largest vertical gap between the two empirical cumulative distribution functions. It ranges from 0 (identical) to 1 (no overlap). The test is nonparametric, so you do not need to assume a normal distribution.
import numpy as np
from scipy.stats import ks_2samp
rng = np.random.default_rng(0)
reference = rng.normal(loc=0.0, scale=1.0, size=5000)
current = rng.normal(loc=0.3, scale=1.0, size=5000)
result = ks_2samp(reference, current)
print(f"KS statistic={result.statistic:.3f}, p-value={result.pvalue:.2e}")The mean moved by only 0.3 standard deviations, yet the p-value is tiny. With 5,000 rows per window, the test detects very small shifts.
Chi-square test for categorical features
Count how often each category appears in each window, stack the counts into a 2 x k table, and test whether the rows are independent of the column. Independence here means the category mix does not depend on the window.
import numpy as np
from scipy.stats import chi2_contingency
# Category counts for one categorical feature (rows: reference, current)
reference_counts = np.array([500, 300, 200])
current_counts = np.array([400, 300, 300])
chi2, p_value, dof, _ = chi2_contingency(np.vstack([reference_counts, current_counts]))
print(f"chi2={chi2:.2f}, dof={dof}, p-value={p_value:.2e}")Here dof is 2, which is (2 - 1) x (3 - 1). Use raw counts, not proportions. Proportions hide the sample size and give wrong p-values.
Common errors
- Reading the p-value as importance. With large windows, almost every feature drifts significantly. Report the KS statistic or a score such as PSI (next chapter) next to the p-value, and judge the size of the shift.
- Testing many features without correction. At a 0.05 threshold, 100 stable features give about 5 false alarms per run. Lower the threshold, for example with a Bonferroni correction (0.05 divided by the number of features), or alert only when several features drift.
- Sparse categories. Chi-square needs expected counts of roughly 5 or more per cell. Merge rare categories into an "other" bucket first.
- New categories. A category absent from the reference gives it a count of zero. Keep the category list consistent, and treat unseen values as a data quality issue as well.
- Discrete numeric features. KS assumes continuous data. For integer features with few distinct values, treat them as categorical.
- Unequal window sizes or ties. Very small windows give weak tests. Compare similar sizes when you can.
Exercise
Exercise
You monitor 50 independent features with a per-test threshold of 0.05, and no real drift has occurred. About how many features do you expect to flag in one run, and what is a simple fix?
Show answer
About 2.5 features (50 x 0.05). A simple fix is a Bonferroni correction, which uses a per-feature threshold of 0.05 / 50 = 0.001. You can also require several features to drift before alerting.