| psi | R Documentation |
Compares the distribution of a new sample with a baseline sample, feature by feature, through the Population Stability Index, the symmetrised Kullback-Leibler divergence of their binned distributions (Kurian and Allali, 2024):
PSI = \sum_{i=1}^{B} (P_i - Q_i) \ln(P_i / Q_i),
where Q_i and P_i are the shares of the baseline and of the new
sample in bin i. The bins are quantile groups of the baseline
(deciles for bins = 10, demi-deciles for bins = 20).
psi(expected, actual, bins = 10, thresholds = c(0.1, 0.25))
expected |
The baseline sample: a numeric vector, or a data frame or matrix with one column per feature. |
actual |
The new sample, with the same features as |
bins |
Number of quantile groups of the baseline. |
thresholds |
Limits between the drift levels: PSI below the first is "little" drift, between the two "moderate" drift (to be reviewed), and above the second "significant" drift. |
The computation follows the SAS macro of the reference code
(https://github.com/FrancisKurian/Kullback, PSI_computation.sas):
The baseline is ranked (ties get their mean rank) and split into bins
groups as PROC RANK GROUPS=: group floor(rank * bins / (n + 1)).
Tied values always share a group, so heavily tied data may give fewer
bins. Bin i covers the baseline values from its smallest to its largest.
New values below the smallest baseline value or above the largest are moved to it (they fall in the first or the last bin).
The shares are the bin counts over the sample sizes. Missing new values count in the size of the new sample but in no bin, so a stream that starts sending missing values lowers every share. Missing baseline values are left out. A new sample whose values are all missing has every share 0, and so a PSI of 0 (step 4): missing data must be checked separately.
A bin without new values contributes 0, as in the reference (where the logarithm of 0 is missing and the missing part is set to 0).
The reference assigns a value through a SAS format built from the bin
ranges; a new value that falls strictly between the largest value of a bin
and the smallest of the next one matches no range and is dropped from the
counts (but kept in the sample size). Here such a value goes to the lower
bin. The paper's thresholds are 0.1 and 0.25 (Section 5; Section 3.1 uses
0.2 for the second). The PSI of two samples from the same distribution is
not 0: it is about (bins - 1) (1 / n_expected + 1 / n_actual), which
matters for small samples.
A psi object: a list with summary, a data frame with one row per
feature (feature, psi, level, number of bins), and bins, a data
frame with one row per feature and bin (feature, bin, lower and
upper baseline values, expected and actual shares, part, the
contribution of the bin).
Kurian, J. F. and Allali, M. (2024). Detecting drifts in data streams using Kullback-Leibler (KL) divergence measure for data engineering applications. Journal of Data, Information and Management, 6, 207-216. doi:10.1007/s42488-024-00119-y
dfr_psi() to monitor a stream, and dfr_kldist() for the same
divergence over a sliding window with equal-width bins.
set.seed(1)
base <- stats::rnorm(10000, 610, 50)
new <- base
top <- new > 643 & stats::runif(10000) < 0.5
new[top] <- new[top] - 200
result <- psi(base, new)
result$summary
head(result$bins)
psi(base, new, bins = 20)$summary
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.