Kullback-Leibler Divergence

The Kullback-Leibler divergence, sometimes also called the relative entropy, of a distribution \(p\) from a distribution \(q\) is defined as:

\[\DKL{p || q} = \sum_{x \in \mathcal{X}} p(x) \log_2 \frac{p(x)}{q(x)}\]

The Kullback-Leibler divergence quantifies the average number of extra bits required to represent a distribution \(p\) when using an arbitrary distribution \(q\). This can be seen through the following identity:

\[\DKL{p || q} = \xH{p || q} - \H{p}\]

Where the Cross Entropy quantifies the total cost of encoding \(p\) using \(q\), and the Entropy quantifies the true, minimum cost of encoding \(p\). For example, let’s consider the cost of representing a biased coin by a fair one:

In [1]: from dit.divergences import kullback_leibler_divergence

In [2]: p = dit.Distribution(['0', '1'], [3/4, 1/4])

In [3]: q = dit.Distribution(['0', '1'], [1/2, 1/2])

In [4]: kullback_leibler_divergence(p, q)
Out[4]: 0.18872187554086717

That is, it costs us \(0.1887\) bits of wasted overhead by using a mismatched distribution.

Not a Metric

Although the Kullback-Leibler divergence is often used to see how “different” two distributions are, it is not a metric. Importantly, it is neither symmetric nor does it obey the triangle inequality. It does, however, have the following property:

\[\DKL{p || q} \ge 0\]

with equality if and only if \(p = q\). This makes it a premetric.

The Jeffreys Divergence

The asymmetry can be removed by adding both directions together, giving the Jeffreys divergence [Jef46]:

\[J(p, q) = \DKL{p || q} + \DKL{q || p}\]
In [5]: from dit.divergences import jeffreys_divergence

In [6]: jeffreys_divergence(p, q)
Out[6]: 0.39624062518028913

This is symmetric and still vanishes only when \(p = q\), but it is not a metric either: it does not obey the triangle inequality. It is also infinite unless the two distributions share a support, since each direction separately requires absolute continuity. For a symmetric alternative that stays finite, see the Jensen-Shannon Divergence; for symmetric quantities that are metrics, see Variational Distance.

API

relative_entropy() is an alias for kullback_leibler_divergence().

kullback_leibler_divergence(dist1, dist2, rvs=None, crvs=None)[source]

The Kullback-Liebler divergence between dist1 and dist2.

Parameters:
  • dist1 (Distribution) – The first distribution in the Kullback-Leibler divergence.

  • dist2 (Distribution) – The second distribution in the Kullback-Leibler divergence.

  • rvs (list, None) – The indexes of the random variable used to calculate the Kullback-Leibler divergence between. If None, then the Kullback-Leibler divergence is calculated over all random variables.

Returns:

dkl – The Kullback-Leibler divergence between dist1 and dist2.

Return type:

float

Raises:

ditException – Raised if either dist1 or dist2 doesn’t have rvs or, if rvs is None, if dist2 has an outcome length different than dist1.

jeffreys_divergence(dist1, dist2, rvs=None, crvs=None)[source]

The Jeffreys divergence between dist1 and dist2.

The Jeffreys divergence is the symmetrized Kullback-Leibler divergence:

J(P, Q) = D_KL(P || Q) + D_KL(Q || P)

Unlike the Kullback-Leibler divergence it is symmetric in its arguments, though it is still not a metric: it does not satisfy the triangle inequality. It is infinite unless dist1 and dist2 have the same support, since each direction requires absolute continuity.

Parameters:
  • dist1 (Distribution) – The first distribution in the Jeffreys divergence.

  • dist2 (Distribution) – The second distribution in the Jeffreys divergence.

  • rvs (list, None) – The indexes of the random variable used to calculate the Jeffreys divergence between. If None, then the Jeffreys divergence is calculated over all random variables.

  • crvs (list, None) – The indexes of the random variables to condition on. If None, then no variables are conditioned on.

Returns:

dj – The Jeffreys divergence between dist1 and dist2.

Return type:

float

Raises:

ditException – Raised if either dist1 or dist2 doesn’t have rvs or, if rvs is None, if dist2 has an outcome length different than dist1.

References

Jeffreys, H. (1946). An invariant form for the prior probability in estimation problems. Proceedings of the Royal Society of London A, 186(1007), 453-461.