Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Relative entropy and Kullback–Leibler divergence

Heinrich Heine University Düsseldorf

Logarithms

The elementary properties of the logarithm – definition, change of base, bit and nat – are covered in the digression in the chapter Expected value and variance. Here we are after a different question: why it is the logarithm, of all functions, that turns up in information theory, and nothing else.

Equally clearly, the same lemma holds for log⁡2\log_2 in place of ln⁡\ln (only the constant is different).

So if we want to count something in such a way that the count of a product is the sum of the individual counts, we have to use a logarithm.

We have argued this heuristically before; now we can prove it rigorously:

Definition and properties

Joint entropy

Entropy, conditional entropy, joint entropy and mutual information in a Venn diagram. Figure: Wikimedia Commons, user KonradVoelkel (2010, public domain).

Figure 1:Entropy, conditional entropy, joint entropy and mutual information in a Venn diagram. Figure: Wikimedia Commons, user KonradVoelkel (2010, public domain).

A summand with pi>0p_i > 0 and ri=0r_i = 0 is ∞\infty here (with log⁡0=−∞\log 0 = -\infty): an event that is impossible under rr but occurs under pp comes at infinite cost.

Careful: some authors unfortunately write the cross-entropy as H(X,Y)H(X, Y) too, which is easily confused with the joint entropy. And the notation H(X;Y)H(X;Y) resembles that of the mutual information I(X;Y)I(X;Y) above, which is something else entirely.

If we hold XX fixed and vary YY, minimising H(X;Y)H(X;Y) and minimising H(X∥Y)H(X \Vert Y) come to the same thing, which is why machine learning often substitutes one for the other. The maximum likelihood method can be read as minimising a cross-entropy, and hence as minimising a relative entropy. That is how, and why, entropy matters to machine learning.

Otherwise, the cross-entropy is used considerably less often in the theory than the relative entropy (usually called the Kullback–Leibler divergence DKLD_{KL}).

Continuous entropy

One might think that the formula H(p)=−∑pilog⁡piH(p) = -\sum p_i \log p_i generalises to H(P)=−∫log⁡PdPH(P) = -\int \log P dP, but as it stands that makes no sense. What one can do is this:

It already has some of the pleasant properties we would expect of a continuous entropy, translation invariance for instance: h(X)=h(X−v)h(X) = h(X - v) for a vector v∈Rnv \in \mathbb{R}^n.

Unfortunately the differential entropy is not invariant under arbitrary transformations, not even under isomorphisms.

Jaynes suggested picturing ever finer histograms, that is, a set of NN discrete points {xi}\{x_i\}. We then write M(a,b)=lim⁡N→∞1N(number of xi:a<xi<b)M(a,b) = \lim_{N \to \infty} \frac{1}{N} (\text{number of } x_i : a < x_i < b) and call a density mm with M(a,b)=∫abm(x)dxM(a,b) = \int_a^b m(x) dx an invariant measure. With that in hand we write

lim⁡N→∞log⁡(N)−∫p(x)log⁡p(x)m(x)dx\lim_{N \to \infty} \log(N) - \int p(x) \log \dfrac{p(x)}{m(x)} dx

but since lim⁡N→∞log⁡(N)=∞\lim_{N \to \infty} \log(N) = \infty, we cheekily drop that part and so obtain a quantity that behaves much like h(x)h(x) but is scale invariant:

H(X):=−∫p(x)log⁡p(x)m(x)dxH(X) := - \int p(x) \log \dfrac{p(x)}{m(x)} dx

The first thing to note is that the “discrete entropy” of a continuous distribution is really ∞\infty. The second is that a workable notion of entropy evidently cannot do without reference to a further distribution (here the “invariant measure”).

A theoretical analysis of what h(X)h(X) and H(X)H(X) really are leads to the general relative entropy H(P,Q)H(P,Q), with h(X)=−H(fλ ∥ λ)h(X) = -H(f\lambda \,\Vert\, \lambda), where λ\lambda is the Lebesgue measure and ff a density of XX.

We will not pursue that any further either; instead we record that for PP and RR with density functions p,r ⁣:Rn→[0,∞)p,r \colon \mathbb{R}^n \to [0,\infty) it looks simpler:

H(P∥R)=∫Ωp(x)log⁡p(x)r(x)dxH(P \Vert R) = \int_\Omega p(x) \log \frac{p(x)}{r(x)} dx

This expression, in turn, we may evaluate by Riemann sums (if the density functions are friendly enough). If for instance n=1n=1 and Ω=R\Omega = \mathbb{R}, we can look at

H(P∥R)=∫Rp(x)log⁡p(x)r(x)dxH(P \Vert R) = \int_{\mathbb{R}} p(x) \log \dfrac{p(x)}{r(x)} dx =∑k=−∞∞∫kk+1p(x)log⁡p(x)r(x)dx= \sum_{k = -\infty}^{\infty} \int_{k}^{k+1} p(x) \log \dfrac{p(x)}{r(x)}dx

and bound each of the integrals on the right by a value, so as to estimate H(P∥R)H(P \Vert R).

H(P∥R)≈∑k=−∞∞p(k)log⁡p(k)r(k)H(P \Vert R) \approx \sum_{k = -\infty}^{\infty} p(k) \log \dfrac{p(k)}{r(k)}

The subdivision into intervals of length 1 can of course be made as fine as we like, making the approximation as accurate as we like. Replacing a continuous distribution by such a discrete one is the analogue of forming a histogram.

If you still doubt that abstract information theory has any relevance to the practical work of data scientists and machine learning engineers, a trip into the Wikipedia page on applications of mutual information and conditional entropy is recommended.