AI State of the Art
Math/Probability and Statistics/11 min
Section 328

Maximum Likelihood Estimation (MLE) and MAP

Listen as a podcast →

The fundamental question

You have a model with parameters θ\theta and data D\mathcal{D}. The central question of learning is: what are the "best" parameters? Maximum likelihood and MAP are two answers to this question, and they are intimately linked to what neural networks do during training.

The likelihood

Definition

The likelihood is the probability of observing the data given the model parameters:

L(θ)=P(D∣θ)\mathcal{L}(\theta) = P(\mathcal{D} | \theta)

Pay attention to the subtlety: it's the same mathematical expression as conditional probability, but we look at it from a different angle. Instead of fixing θ\theta and asking what data might be generated, we fix the data (already observed) and ask which parameters best explain it.

Analogy

Imagine you find footprints in the snow. The likelihood asks: "If an animal of type X had walked here, what would be the probability of seeing exactly these footprints?" You test different animals (different θ\theta) and look for the one that makes the observed footprints most probable.

For i.i.d. data

If the data are independent and identically distributed (i.i.d.), the likelihood is the product of individual probabilities:

L(θ)=∏i=1nP(xi∣θ)\mathcal{L}(\theta) = \prod_{i=1}^n P(x_i | \theta)

This is a product of terms between 0 and 1, so it becomes very small very fast as nn grows. This is a major practical problem.

Maximum likelihood estimation (MLE)

Principle

MLE looks for the parameters that maximize the likelihood:

θMLE∗=arg⁡max⁡θL(θ)=arg⁡max⁡θ∏i=1nP(xi∣θ)\theta^*_{\text{MLE}} = \arg\max_\theta \mathcal{L}(\theta) = \arg\max_\theta \prod_{i=1}^n P(x_i | \theta)

It's simple and intuitive: you choose the parameters that make the observed data most probable.

Plot with ten blue data points lying on the horizontal axis between 7.5 and 12, and four candidate Gaussian curves drawn over them: f1 in blue centred at 10 with medium width, f2 in yellow very wide and flat around 10, f3 in pink very narrow and tall centred at 10, f4 in green wide and centred at 8.
The blue dots are the observed data, fixed once and for all. The four curves are four candidate parameter choices. The green one is badly centred, the pink one is too narrow and assigns almost no probability to the points at the edges, the yellow one is too wide and commits to nothing. The blue one gives the whole set of points the highest probability: that is the one maximum likelihood picks.Source : datascience.eu, Probability concepts explained: maximum likelihood estimation

Example: estimating a coin flip probability

You flip a coin 100 times and observe 60 heads. The parameter pp (probability of heads) that maximizes the likelihood is simply p∗=60/100=0.6p^* = 60/100 = 0.6. Here, intuition perfectly matches the formal calculation: the observed frequency is the MLE (Maximum Likelihood Estimator) of the Bernoulli parameter.

Likelihood curve L as a function of the parameter pi, a bell shape over the range 0 to 1 on the horizontal axis. It starts at zero, rises to a maximum of about 0.25 and falls back to zero. Two dashed lines mark the peak, a vertical one at pi equals 0.4 and a horizontal one at the height of the maximum.
This is what you actually maximise. The data are held fixed; you sweep the parameter pi from 0 to 1 and watch how plausible each value makes the observations. The top of the curve is the maximum likelihood estimate. In this example the peak sits at 0.4, which is exactly the observed frequency in the sample; with 60 heads out of 100 flips, the peak would sit at 0.6.Source : C.-C. Wang, Medical Statistics (bookdown), Likelihood chapter

The log-likelihood: why the logarithm

The problem with products

A product of thousands of numbers between 0 and 1 gives an astronomically small number, beyond the precision of floats. Moreover, optimizing a product is more complicated than optimizing a sum.

The solution

The logarithm transforms products into sums:

log⁡L(θ)=∑i=1nlog⁡P(xi∣θ)\log \mathcal{L}(\theta) = \sum_{i=1}^n \log P(x_i | \theta)

Since the log is a strictly increasing function, maximizing log⁡L\log \mathcal{L} and maximizing L\mathcal{L} give the same θ∗\theta^*. You lose nothing, and you gain:

  • Numerical stability: you add numbers instead of multiplying numbers close to zero
  • Simplicity: derivatives of sums are simpler than derivatives of products
  • Clean gradient: the gradient of the log-likelihood has better properties

Connection to the cross-entropy loss

The fundamental connection

In classification, the cross-entropy loss everyone uses is exactly the negative log-likelihood. Minimizing cross-entropy is the same as maximizing log-likelihood.

For multi-class classification with one-hot labels yy and softmax predictions y^\hat{y}:

Cross-Entropy=−∑k=1Kyklog⁡y^k\text{Cross-Entropy} = -\sum_{k=1}^K y_k \log \hat{y}_k

Since yy is one-hot (a single yc=1y_c = 1, the others are 0), this reduces to:

Cross-Entropy=−log⁡y^c\text{Cross-Entropy} = -\log \hat{y}_c

That's exactly −log⁡P(correct class∣x,θ)-\log P(\text{correct class} | x, \theta), which is the negative log-likelihood.

For binary classification

The binary cross-entropy:

BCE=−[ylog⁡y^+(1−y)log⁡(1−y^)]\text{BCE} = -[y \log \hat{y} + (1 - y) \log (1 - \hat{y})]

This is the negative log-likelihood of a Bernoulli with parameter y^\hat{y}.

For regression

The MSE (Mean Squared Error) corresponds to the negative log-likelihood of a Gaussian with constant variance:

MSE=1n∑i=1n(yi−y^i)2∝−log⁡L(θ)\text{MSE} = \frac{1}{n}\sum_{i=1}^n (y_i - \hat{y}_i)^2 \propto -\log \mathcal{L}(\theta)

if we assume yi∼N(y^i,σ2)y_i \sim \mathcal{N}(\hat{y}_i, \sigma^2).

In other words: minimizing MSE means assuming the noise is Gaussian and maximizing the likelihood.

MAP estimation

Beyond MLE

MLE has a flaw: it only considers the data and ignores any prior knowledge about the parameters. If you have little data, MLE can give absurd estimates. You flip the coin 3 times, get 3 heads, and MLE says p=1p = 1. But you know a fair coin is more likely.

Maximum A Posteriori

MAP adds a prior P(θ)P(\theta) on the parameters and maximizes the posterior:

θMAP∗=arg⁡max⁡θP(θ∣D)=arg⁡max⁡θP(D∣θ)⋅P(θ)\theta^*_{\text{MAP}} = \arg\max_\theta P(\theta | \mathcal{D}) = \arg\max_\theta P(\mathcal{D} | \theta) \cdot P(\theta)

In log form:

θMAP∗=arg⁡max⁡θ[∑i=1nlog⁡P(xi∣θ)+log⁡P(θ)]\theta^*_{\text{MAP}} = \arg\max_\theta \left[\sum_{i=1}^n \log P(x_i | \theta) + \log P(\theta)\right]

MAP is MLE with a bonus term that penalizes "unlikely" parameters according to the prior.

MAP and regularization: the connection

Gaussian prior = L2 regularization

If you choose a Gaussian prior centered at zero: P(θ)=N(0,σ2I)P(\theta) = \mathcal{N}(0, \sigma^2 I), then:

log⁡P(θ)=−12σ2∥θ∥2+constant\log P(\theta) = -\frac{1}{2\sigma^2} \|\theta\|^2 + \text{constant}

MAP becomes:

θMAP∗=arg⁡max⁡θ[∑i=1nlog⁡P(xi∣θ)−λ2∥θ∥2]\theta^*_{\text{MAP}} = \arg\max_\theta \left[\sum_{i=1}^n \log P(x_i | \theta) - \frac{\lambda}{2} \|\theta\|^2\right]

with λ=1/σ2\lambda = 1/\sigma^2. You get exactly the loss + L2 regularization (weight decay)!

L2 regularization, used in nearly every neural network training, is nothing other than a Gaussian Bayesian prior on the weights. It says: "I believe a priori that the weights are small."

Laplacian prior = L1 regularization

If you choose a Laplacian prior: P(θ)∝exp⁡(−λ∣θ∣)P(\theta) \propto \exp(-\lambda |\theta|), you get L1 regularization, which pushes weights toward exactly zero (sparsity).

Summary table

PriorRegularizationEffect
Gaussian N(0,σ2)\mathcal{N}(0, \sigma^2)L2 (weight decay)Small weights
LaplacianL1 (lasso)Sparse weights
Uniform (no prior)No regularizationPure MLE

MLE vs MAP in practice

In modern deep learning:

  • Standard training with cross-entropy loss and weight decay is MAP with a Gaussian prior
  • Without weight decay, it's pure MLE
  • Dropout can be interpreted as a form of approximate Bayesian inference
  • Data augmentation acts as an implicit prior on model invariance

MLE and MAP are not abstract concepts disconnected from practice. They are exactly what frameworks do when you call loss.backward() and optimizer.step().

Key takeaways

  • Likelihood measures the compatibility between parameters and observed data
  • MLE chooses the parameters that maximize this likelihood
  • We use log-likelihood for numerical and algebraic reasons
  • Cross-entropy loss = negative log-likelihood (the connection is direct)
  • MAP adds a prior: it's regularized MLE
  • L2 regularization = Gaussian prior, L1 regularization = Laplacian prior
  • In practice, training a network with weight decay is doing MAP

Where this is used. Pre-training a language model is nothing but maximum likelihood over billions of words.