Neural Mastery
← Practice
Population Stability Index (PSI) for Drift Detectionmedium

Population Stability Index (PSI) for Drift Detection

medium ML Systems & MLOps 15 min

NumPy allowed · plain Python earns 10 bonus points

Implement the Population Stability Index, the standard real metric MLOps pipelines use to detect feature/label distribution drift between training and production.

Task

Implement psi(expected_pct, actual_pct). Both lists give the fraction of data in each of the same N bins (they sum to 1 each). PSI = sum((actual[i]-expected[i]) * ln(actual[i]/expected[i])) over all bins. Treat any bin with 0 in either list as contributing 0 to the sum (skip it) to avoid a log(0) or division by zero.

Function Signature

psi(expected_pct: list[float], actual_pct: list[float]) -> float

Examples

Example 1: identical distributions, psi=0
Input: expected_pct = [0.3, 0.4, 0.3] actual_pct = [0.3, 0.4, 0.3]
Output: 0
Example 2: moderate shift
Input: expected_pct = [0.5, 0.5] actual_pct = [0.6, 0.4]
Output: 0.04054651081081642
Example 3: zero bin skipped safely
Input: expected_pct = [0.5, 0.5, 0] actual_pct = [0.4, 0.6, 0]
Output: 0.04054651081081642

Constraints

  • len(expected_pct) == len(actual_pct)
  • values in [0,1]
Python3Saved ✓
import math

def psi(expected_pct, actual_pct):
# Your implementation here
pass

Case 1: identical distributions, psi=0
Input: expected_pct = [0.3, 0.4, 0.3] actual_pct = [0.3, 0.4, 0.3]
Expected: 0
Case 2: moderate shift
Input: expected_pct = [0.5, 0.5] actual_pct = [0.6, 0.4]
Expected: 0.04054651081081642
Case 3: zero bin skipped safely
Input: expected_pct = [0.5, 0.5, 0] actual_pct = [0.4, 0.6, 0]
Expected: 0.04054651081081642