#274Jensen-Shannon Divergence (Drift)MediumProbabilityML System DesignAsked atMeta · Amazon · Google
Jensen-Shannon Divergence (Drift)
Background
Jensen-Shannon divergence is a symmetric, bounded ( when using ) divergence between two probability distributions - the variance-reduction sibling of KL. As a drift detector it has nicer properties than raw KL: symmetric, finite even when one distribution has zeros, and interpretable as a similarity.
Problem statement
Implement js_divergence_drift(expected, actual, n_bins=10, eps=1e-10) returning the JS divergence between the binned histograms:
with so the result lies in .
Input
expected,actual- array-likes of floats.n_bins,eps- same convention as PSI.
Output
floatin .
Examples
Input: identical samples -> JS ~ 0
Input: disjoint shifted samples -> JS approaching 1
Constraints
- Equal-width bins on the expected sample's range, with clipping.
- so the bound is .
- Returns if expected has zero range.
Notes
- JS vs KL. KL goes to when but . JS averages with so it's always finite. That makes it monitoring-friendly.
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Example: moderate shift of +5
- •Reference: large shift of +12
- •Sample: small shift of +2