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
44
45
//! Jensen–Shannon divergence.
use kl;
use ;
use crateHistogram;
use crateResult;
/// Jensen–Shannon divergence between a `reference` and `live` histogram, in nats.
///
/// ```text
/// M = ½ (live + reference)
/// JS = ½ D_KL(live ‖ M) + ½ D_KL(reference ‖ M)
/// ```
///
/// Unlike [`kl_divergence`](crate::kl_divergence), JS is **symmetric** and
/// **bounded** to `[0, ln 2]` (≈ `[0, 0.693]`) when computed in nats. Reach for
/// it when you want a single symmetric, bounded score that is directly
/// comparable across features — PSI and KL are unbounded and so harder to
/// compare on a common scale.
///
/// Both distributions are epsilon-smoothed first (see
/// [`DEFAULT_EPSILON`](crate::metrics::DEFAULT_EPSILON)).
///
/// # Errors
/// Returns [`DriftError::BinCountMismatch`](crate::DriftError::BinCountMismatch)
/// if the histograms have different bin counts.
/// [`js_divergence`] with an explicit epsilon smoothing constant.
///
/// # Errors
/// Same as [`js_divergence`], plus
/// [`DriftError::InvalidConfig`](crate::DriftError::InvalidConfig) if `epsilon`
/// is negative or non-finite.