Theorem 1 (Maximum likelihood minimizes the divergence from the data) Let \(x_1, \ldots, x_n\) be data, and let \(\mathop{\hat{\mathbb{p}}}\nolimits\) be their empirical distribution. Let \(q_\theta\) be probability mass functions with \(q_\theta(v) > 0\) wherever \(\mathop{\hat{\mathbb{p}}}\nolimits(v) > 0\), for parameter values \(\theta\). Then \(\theta\) maximizes the likelihood\(\prod_{i=1}^n q_\theta(x_i)\) exactly when it minimizes the Kullback–Leibler divergence\({{D_{\mathrm{KL}}}\mathopen{}\left(\mathop{\hat{\mathbb{p}}}\nolimits \,\Vert\, q_\theta\right)\mathclose{}}\).
ProofProof
Proof. Let \(n_v\) be the number of data equal to \(v\). Then \(n_v = n \, \mathop{\hat{\mathbb{p}}}\nolimits(v)\).
\[\sum_{i=1}^n \log_2 q_\theta(x_i) = \sum_v n \, \mathop{\hat{\mathbb{p}}}\nolimits(v) \log_2 q_\theta(v).\]
Divide by \(n\). The right side is minus the cross-entropy\({\mathbb{H}\mathopen{}\left(\mathop{\hat{\mathbb{p}}}\nolimits, q_\theta\right)\mathclose{}}\):
The entropy \({\mathbb{H}\mathopen{}\left(\mathop{\hat{\mathbb{p}}}\nolimits\right)\mathclose{}}\) does not depend on \(\theta\). So \(\theta\) maximizes the average log-likelihood exactly when it minimizes \({{D_{\mathrm{KL}}}\mathopen{}\left(\mathop{\hat{\mathbb{p}}}\nolimits \,\Vert\, q_\theta\right)\mathclose{}}\). The logarithm increases, and dividing by \(n\) keeps the order, so \(\theta\) maximizes the average log-likelihood exactly when it maximizes the likelihood.
Example 1 (The divergence from the data for coin flips)
A coin is flipped \(100\) times, and it lands heads \(70\) times. The chunk below follows Theorem 1 for the Bernoulli models with the probability of heads \(0.1, 0.2, \ldots, 0.9\). It prints the divergence of each model from the empirical distribution, the average log-likelihood, and minus the entropy of the empirical distribution minus the divergence.
The last two columns agree, as the proof says. The divergence is smallest, at \(0\)bits, and the average log-likelihood is largest for the model with probability of heads \(0.7\), which is the fraction of heads in the data, so that model is the empirical distribution.
Remark 1 (One labeled example). For a single labeled example, \(n = 1\), and the empirical distribution puts all its mass on the observed label. Then Theorem 1 says that the model with the largest probability for that label is the model with the smallest divergence from it. That is the case that the cross-entropy loss remark describes.
---title: "KL divergence and maximum likelihood"format: html: default revealjs: output-file: kl-divergence-slides.html pdf: output-file: kl-divergence-handout.pdf docx: output-file: kl-divergence.docx---{{< include latex-macros/macros.qmd >}}The [Kullback--Leibler divergence](https://morrison-lab.github.io/pds/expectation.html#def-kl-divergence) of one model from anotheris the number of [bits](entropy.qmd#def-bit) per value that a code built for the model wastes,by the [cross-entropy decomposition](source-coding.qmd#thm-cross-entropy-decomposition).This page applies that to data,and shows that the model a [maximum likelihood estimate](https://morrison-lab.github.io/sds/intro-MLEs.html#def-mle) picks is the model with the least waste.## Maximum likelihood {#sec-maximum-likelihood-kl}<!-- No slidebreak here: the heading shares its slide with the thm-maximum-likelihood-kl-divergence div. -->{{< include ids/_subfiles/_thm-maximum-likelihood-kl-divergence.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_exm-maximum-likelihood-kl-divergence.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_rem-maximum-likelihood-kl-divergence-loss.qmd >}}## References {.unnumbered}::: {#refs}:::