KL Divergence
Shippingarray_kl_divergenceKullback-Leibler divergence D(p‖q) between two probability distributions
Signature
Inputs
pArrayrequired— The 'true' / reference distribution $p$ (non-negative).qArrayrequired— The 'model' / approximating distribution $q$ (non-negative, same length as $p$).
Outputs
kl_divergenceScalar— $D_{\mathrm{KL}}(p\,\|\,q) = \sum_i p_i \ln(p_i / q_i)$ (nats).
Description
KL Divergence computes the Kullback-Leibler divergence — the information lost, in nats, when distribution is used to approximate the reference distribution . It is if and only if and strictly positive otherwise, giving a directed 'distance' between distributions.
KL divergence is asymmetric: in general, so port order matters — p is the truth, q the approximation. The node is stateless.
Mathematics
Examples
Identical distributions
With p = q = [0.5, 0.5], every term , so kl_divergence — no information is lost when the model matches the truth exactly.
Applications
- Measuring how far a fitted/model distribution strays from an empirical one.
- Drift detection: divergence of a live histogram from a reference baseline.
- Objective terms in variational and information-theoretic methods.
Neat
The divergence is directed: swapping `p` and `q` generally changes the value, which is exactly why the ports are named rather than symmetric.
Feed two Array-Histogram count vectors to get an empirical KL between two datasets straight from raw samples.