KL Divergence Between Two PMFs
kl_divergence_pmf.RdComputes the Kullback-Leibler divergence \(D_{KL}(p \| q)\) between two probability mass functions.
Details
The KL divergence is defined as: $$D_{KL}(p \| q) = \sum_k p(k) \log\frac{p(k)}{q(k)}$$
Terms with \(p(k)=0\) contribute zero. If \(p(k)>0\) and
\(q(k)=0\), the result is Inf. Both inputs must already be valid,
normalized PMFs; this function never normalizes or smooths them. In keeping
with the package PMF contract, a sum differing from one by at most
.TOL_PMF_SUM is accepted without modifying the vector. A raw negative
value whose magnitude is fully explained by that validation tolerance and
floating-point roundoff is reported as zero.
Properties:
\(D_{KL}(p \| q) \geq 0\); for exactly normalized PMFs, equality holds iff \(p = q\). For sums accepted within the PMF tolerance, equality is interpreted up to that same numerical tolerance.
Not symmetric: \(D_{KL}(p \| q) \neq D_{KL}(q \| p)\)
See also
kl_divergence_K for KL divergence with induced PMF