Definition
Sort the clients by score. At each score s, compute the share of defaulters with a score at or below s and the same share for non-defaulters.
KS = maxs | Fbad(s) − Fgood(s) |
At the score where the maximum is reached, the difference between the share of defaulters caught and the share of good payers wrongly caught is the largest. It is the same statistic as the two-sample Kolmogorov-Smirnov test, used here as a measure of separation and not as a test.
Worked example
import numpy as np
from scipy import stats
rng = np.random.default_rng(21)
n = 4000
risk = rng.normal(size=n)
default = rng.random(n) < 1 / (1 + np.exp(2.5 - 1.1 * risk))
score = risk + rng.normal(scale=0.9, size=n)
def ks(score, default):
order = np.argsort(score)
d = default[order]
f_bad, f_good = np.cumsum(d) / d.sum(), np.cumsum(~d) / (~d).sum()
k = int(np.argmax(np.abs(f_bad - f_good)))
return abs(f_bad[k] - f_good[k]), score[order][k]
value, at = ks(score, default)
print(f"defaults: {default.sum()} of {n}")
print(f"KS = {value:.3f} at score {at:.2f} scipy: {stats.ks_2samp(score[default], score[~default]).statistic:.3f}")
boot = [ks(score[i], default[i])[0] for i in rng.integers(0, n, (1000, n))]
print(f"95 % interval: [{np.percentile(boot, 2.5):.3f}, {np.percentile(boot, 97.5):.3f}]")
few = rng.choice(n, 400, replace=False)
boot_few = [ks(score[few][i], default[few][i])[0] for i in rng.integers(0, 400, (1000, 400))]
print(f"same model on 400 clients: KS = {ks(score[few], default[few])[0]:.3f}, interval [{np.percentile(boot_few, 2.5):.3f}, {np.percentile(boot_few, 97.5):.3f}]")Output, produced by running the code above when this page was built:
defaults: 453 of 4000 KS = 0.327 at score -0.03 scipy: 0.327 95 % interval: [0.293, 0.371] same model on 400 clients: KS = 0.324, interval [0.252, 0.477]
The hand computation and SciPy agree. On 400 clients the interval covers a range in which a model would be called weak at one end and good at the other.
Where it misleads
- KS describes one point of the score range. Two models with the same KS can behave differently everywhere else, and the cut-off actually used is rarely the one where the maximum occurs.
- As a maximum, it is biased upward on small samples: the largest gap found includes some noise. The bootstrap interval on a small sample is wide and not centred on the true value.
- Like the Gini, it depends on the population and falls when the worse applicants are no longer in the sample.
- It measures separation, not calibration.
The same statistic is used to compare the score distribution of today with the one at development, as a drift measure. On large samples it flags differences too small to matter, which is one reason the population stability index is reported next to it.
In our work
Our validation reports give discrimination with an interval and by segment, because one figure for the whole portfolio hides the segments where the model separates poorly.
Service: machine learning and model validation Research note: credit model validation, a worked report
References
- Massey, F. J. (1951). The Kolmogorov-Smirnov test for goodness of fit. Journal of the American Statistical Association, 46(253), 68-78. doi:10.1080/01621459.1951.10500769
- Thomas, L. C., Edelman, D. B., Crook, J. N. (2002). Credit scoring and its applications. SIAM, Philadelphia. doi:10.1137/1.9780898718317
- Siddiqi, N. (2005). Credit risk scorecards: developing and implementing intelligent credit scoring. Wiley, Hoboken.