Repository navigation
Expand file tree
/
Copy pathdrift.py
More file actions
43 lines (33 loc) · 1.46 KB
/
Copy pathdrift.py
File metadata and controls
43 lines (33 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""Distribution-drift example: KL and Jensen-Shannon between two batches.
Run: python examples/drift.py
Compares a reference batch (e.g. training data) against a drifted batch (e.g.
later production data). Higher divergence means the distributions have moved
apart. JS is symmetric and bounded to [0, 1] bits; KL is directional. Synthetic
data lives in ``_synthetic``; swap ``data.distribution_drift()`` for two batches
of your own values.
"""
from entroscope import divergence
try:
from . import _synthetic as data
except ImportError:
import _synthetic as data
def drift_detection():
"""Divergence between a reference batch and a drifted batch."""
reference, drifted = data.distribution_drift()
kl = divergence.kl(reference, drifted, bins=30)
js = divergence.js(reference, drifted, bins=30)
js_self = divergence.js(reference, reference.copy(), bins=30)
print("Distribution drift (divergence, bits)")
print(f" KL(reference || drifted): {kl:6.3f}")
print(f" JS(reference, drifted) : {js:6.3f}")
print(f" JS(reference, itself) : {js_self:6.3f}")
print(" -> JS near 0 means no drift; larger means the batches have moved apart")
print(" (KL and JS are relative-entropy measures between distributions)\n")
return reference, drifted, kl, js
def main():
print("=" * 60)
print("DISTRIBUTION DRIFT — KL & JENSEN-SHANNON DIVERGENCE")
print("=" * 60)
drift_detection()
if __name__ == "__main__":
main()