NeuroGenesys.
Probability & InferenceAcademy • Foundations • Lesson 4 of 4

Maximum likelihood: where loss functions come from

7 min read • By Will Schulz • NeuroGenesys AI Academy

Squared error for regression, cross-entropy for classification, next-token log-loss for language models: these loss functions look like separate conventions, but they are one idea applied to different noise models. Maximum likelihood says to choose the parameters under which the data you actually observed would have been most probable.

The likelihood function

A model is a family of probability distributions p(x∣θ)p(x \mid \theta) indexed by parameters θ\theta: a coin's bias, a Gaussian's mean, or the billions of weights of a neural network. Given independent observations x1,…,xnx_1, \dots, x_n, the likelihood is the probability of those data viewed as a function of θ\theta:

L(θ)=∏i=1np(xi∣θ).L(\theta) = \prod_{i=1}^{n} p(x_i \mid \theta).

The likelihood is not a probability distribution over θ\theta: it need not integrate to one, and treating it as a posterior requires a prior (see Bayes' rule). It is a score for how well each candidate θ\theta explains the data.

The maximum likelihood estimate is θ^=arg⁡max⁡θL(θ)\hat\theta = \arg\max_\theta L(\theta). R. A. Fisher set out the method and its theory in his 1922 paper on the foundations of theoretical statistics, which also introduced the now-standard distinction between a population parameter and a sample statistic.[1]

Because a product of many small numbers underflows, and because logarithm is increasing (so it does not move the maximum), one always works with the log-likelihood and, by convention in machine learning, minimises its negative:

θ^=arg⁡min⁡θ  −∑i=1nlog⁡p(xi∣θ).\hat\theta = \arg\min_\theta \; -\sum_{i=1}^{n} \log p(x_i \mid \theta).

That negative log-likelihood (NLL) is the loss function.

Worked example: a biased coin

Observe 10 flips with k=7k = 7 heads. Model each flip as heads with probability θ\theta. The log-likelihood is

ℓ(θ)=7log⁡θ+3log⁡(1−θ).\ell(\theta) = 7 \log\theta + 3 \log(1 - \theta).

Set the derivative to zero: 7θ−31−θ=0\frac{7}{\theta} - \frac{3}{1-\theta} = 0, so 7(1−θ)=3θ7(1-\theta) = 3\theta and θ^=0.7\hat\theta = 0.7. In general θ^=k/n\hat\theta = k/n: the maximum likelihood estimate of a probability is the observed frequency. Unsurprising, but derived rather than assumed.

The same calculation shows a weakness. With 3 flips and 3 heads, θ^=1\hat\theta = 1: the model declares tails impossible. Maximum likelihood trusts the sample completely. Laplace's rule of succession, (k+1)/(n+2)(k+1)/(n+2), which results from a uniform prior on θ\theta, gives 4/54/5 instead.[2] In language modelling the same failure appears as assigning zero probability to unseen word sequences, which is why classical nn-gram models needed smoothing.

Different noise, different loss

Gaussian noise gives squared error

Suppose a model predicts fθ(x)f_\theta(x) and the observed target is y=fθ(x)+εy = f_\theta(x) + \varepsilon with ε∼N(0,σ2)\varepsilon \sim \mathcal N(0, \sigma^2). Then

−log⁡p(y∣x,θ)=(y−fθ(x))22σ2+12log⁡(2πσ2).-\log p(y \mid x, \theta) = \frac{(y - f_\theta(x))^2}{2\sigma^2} + \tfrac12 \log(2\pi\sigma^2).

With σ\sigma fixed, the second term is a constant, so maximising likelihood is exactly minimising the sum of squared errors.[3] Least squares is maximum likelihood under Gaussian noise. If the noise were Laplace-distributed, the same derivation would give absolute error instead, which is less sensitive to outliers. Choosing a loss is choosing a noise model, whether or not you say so.

Check your understanding

For data x1,…,xnx_1, \dots, x_n modelled as N(μ,σ2)\mathcal N(\mu, \sigma^2) with known σ\sigma, what is the maximum likelihood estimate of μ\mu?

Show answer +

The NLL is ∑i(xi−μ)2/2σ2\sum_i (x_i - \mu)^2 / 2\sigma^2 plus a constant. Setting its derivative −∑i(xi−μ)/σ2-\sum_i (x_i - \mu)/\sigma^2 to zero gives μ^=1n∑ixi\hat\mu = \frac{1}{n}\sum_i x_i, the sample mean.

Categorical outcomes give cross-entropy

For a classifier over KK classes, the model outputs a distribution qθ(⋅∣x)q_\theta(\cdot \mid x), usually via a softmax. The probability of the observed label yy is qθ(y∣x)q_\theta(y \mid x), and the NLL over a dataset is

L(θ)=−1n∑i=1nlog⁡qθ(yi∣xi).\mathcal L(\theta) = -\frac{1}{n}\sum_{i=1}^{n} \log q_\theta(y_i \mid x_i).

This is the cross-entropy between the data's labels and the model's predictions. Maximum likelihood is equivalent to minimising the KL divergence from the empirical data distribution to the model, because the two differ only by the data's own entropy, which does not depend on θ\theta.[4] The information theory course develops that view.

Language models are maximum likelihood estimators

An autoregressive language model factorises a text with the chain rule, pθ(x1:T)=∏tpθ(xt∣x<t)p_\theta(x_{1:T}) = \prod_t p_\theta(x_t \mid x_{<t}). Its log-likelihood is a sum over positions, so the training loss for a corpus is the average per-token NLL:

L(θ)=−1T∑t=1Tlog⁡pθ(xt∣x<t).\mathcal L(\theta) = -\frac{1}{T}\sum_{t=1}^{T} \log p_\theta(x_t \mid x_{<t}).

Every token in the training data is a classification example whose label is the token that actually came next. Bengio and colleagues' 2003 neural language model was trained by maximising this log-likelihood over its training corpus,[5] and the same objective, scaled up by many orders of magnitude, underlies modern pretraining. The empirical scaling laws of Kaplan and colleagues are statements about this cross-entropy loss as a function of model size, data and compute.[6]

Worked example: loss and perplexity

A model predicts four tokens of a sentence, assigning the true tokens probabilities 0.5, 0.25, 0.8 and 0.1. The per-token NLLs in nats are

−ln⁡0.5=0.693,−ln⁡0.25=1.386,−ln⁡0.8=0.223,−ln⁡0.1=2.303.-\ln 0.5 = 0.693,\quad -\ln 0.25 = 1.386,\quad -\ln 0.8 = 0.223,\quad -\ln 0.1 = 2.303.

The mean is 4.605/4=1.1514.605/4 = 1.151 nats per token. Perplexity is its exponential, e1.151≈3.16e^{1.151} \approx 3.16: the model was, on average, as uncertain as a uniform choice among about three tokens. Equivalently, perplexity is one over the geometric mean of the assigned probabilities, (0.5×0.25×0.8×0.1)−1/4=0.01−1/4≈3.16(0.5 \times 0.25 \times 0.8 \times 0.1)^{-1/4} = 0.01^{-1/4} \approx 3.16. Note how the single confident miss (0.1) contributes half the loss: log-loss punishes assigning low probability to what actually happened far more than it rewards being right with high confidence.

Check your understanding

A model assigns probability 0.02 to a token that actually occurs. What does that one token contribute to the loss, in nats? What would it contribute if the model had assigned probability 0?

Show answer +

−ln⁡0.02≈3.91-\ln 0.02 \approx 3.91 nats. At probability 0 the loss is −ln⁡0=∞-\ln 0 = \infty: maximum likelihood forbids ever declaring an observed outcome impossible, which is one reason softmax outputs are never exactly zero.

What maximum likelihood guarantees, and what it does not

Under regularity conditions and with a correctly specified model, the maximum likelihood estimator is consistent (it converges to the true parameter as n→∞n \to \infty) and asymptotically efficient (no consistent estimator has smaller asymptotic variance). Fisher's 1922 paper introduced consistency and efficiency as criteria for estimators and argued that maximum likelihood meets them; fully rigorous proofs came later.[1]

Three caveats matter more in practice:

  • Overfitting. With many parameters and finite data, the likelihood can be pushed up by memorising the sample. The coin with three heads is the smallest example. Adding a penalty, such as λ∥θ∥2\lambda\|\theta\|^2 (weight decay), is equivalent to maximising a posterior under a Gaussian prior on the weights, known as MAP estimation.[3] See Generalization.
  • Misspecification. If the true distribution is not in the model family, maximum likelihood finds the member closest in KL divergence. That can still be useful, but "best in the family" is not "correct".
  • Likelihood is not the goal. A language model trained to maximise the likelihood of internet text learns to reproduce that text's distribution, errors included. Later stages of training (preference learning, reinforcement learning with verifiable rewards) exist precisely because the maximum likelihood objective does not specify helpfulness or correctness; see From RLHF to verifiable rewards.

Key takeaways

  • Maximum likelihood picks the parameters that make the observed data most probable; in practice one minimises the negative log-likelihood.
  • The loss encodes a noise model: Gaussian noise gives squared error, categorical outcomes give cross-entropy.
  • Language-model pretraining is maximum likelihood estimation: minimising average per-token NLL, reported as loss in nats or as perplexity, its exponential.
  • Maximum likelihood is consistent and efficient for well-specified models with enough data, but it overfits small samples; priors and regularisation are the correction.

Sources

  1. R. A. Fisher (1922). "On the Mathematical Foundations of Theoretical Statistics." Philosophical Transactions of the Royal Society A 222: 309–368. https://doi.org/10.1098/rsta.1922.0009 ↩ ↩2

  2. P.-S. Laplace (1814). Essai philosophique sur les probabilités. Paris: Courcier. Digitised copy, Kyoto University Rare Materials Digital Archive: https://rmda.kulib.kyoto-u.ac.jp/en/item/rb00028954 ↩

  3. C. M. Bishop (2006). Pattern Recognition and Machine Learning. Springer. Free PDF via Microsoft Research: https://www.microsoft.com/en-us/research/publication/pattern-recognition-machine-learning/ ↩ ↩2

  4. I. Goodfellow, Y. Bengio, A. Courville (2016). Deep Learning, ch. 5 "Machine Learning Basics." MIT Press. https://www.deeplearningbook.org/ ↩

  5. Y. Bengio, R. Ducharme, P. Vincent, C. Jauvin (2003). "A Neural Probabilistic Language Model." Journal of Machine Learning Research 3: 1137–1155. https://www.jmlr.org/papers/v3/bengio03a.html ↩

  6. J. Kaplan et al. (2020). "Scaling Laws for Neural Language Models." arXiv:2001.08361. https://arxiv.org/abs/2001.08361 ↩

Leadership

A word from the founder — on why the brain is the blueprint, and why the next decade of intelligence will be built here.

"The brain is the universe's only proof that general intelligence is possible. We treat it as the blueprint."

From cortical interfaces to photonic silicon, every program at NeuroGenesys exists to answer one question: how does intelligence arise — and how do we build it safely.

Will Schulz

Will Schulz

Founder, NeuroGenesys • AW3 Technology