TensorTau PathToAGI Probability and Uncertainty Level 03

Level 03 · Probability and Uncertainty · Chapter 033

Maximum Likelihood: Where Loss Functions Come From

If a model is supposed to explain the data, how do we derive what it should try to maximize?

Where We Are

We just learned Maximum Likelihood: Where Loss Functions Come From.

But using it creates a new question.

What problem does this idea still leave us unable to solve?

That question leads naturally to Linear Regression, Properly.

Next → Linear Regression, Properly.

1. The Problem: Two Coins, One Mystery

Imagine two coins:

  • Coin A lands heads about half the time.
  • Coin B is unknown. It might be fair, or heavily biased.

You toss Coin B 10 times and see:

H H H T H H T H H H

There are 8 heads and 2 tails.

Now ask the machine:

What value of the coin’s head probability should we believe?

Let that unknown probability be pp.

If p=0.5p=0.5, eight heads in ten tosses is possible. If p=0.8p=0.8, the observation looks much more natural. If p=0.99p=0.99, eight heads and two tails starts looking surprisingly unlikely again.

So neither “pick the middle” nor “pick the biggest pp” works. We need a rule that asks:

Which pp makes the data we observed most plausible?

That is the question that forces maximum likelihood.


2. What Would a Solution Need?

Our rule should:

  1. Use the observed data, not imaginary future data.
  2. Give us a way to compare candidate parameter values.
  3. Reward parameters that make the observed data plausible.
  4. Return the parameter value that is best according to that comparison.

Notice what we have not said. We have not chosen a loss function yet.

That comes later.


3. First Attempt: Just Choose the Most Frequent Value

We saw 8 heads and 2 tails. So perhaps simply say:

p=0.8p = 0.8

For this tiny example, that answer is actually correct.

But why?

And what happens when the observations have different probabilities, as they will in real models?

The frequency is not a general principle. It is the answer for this particular likelihood model.

A Tempting Wrong Idea

“Maximum likelihood just means matching the sample frequency.”

Frequency matching is a consequence of a Bernoulli likelihood. The deeper principle is broader: choose parameters that maximize the probability assigned to the observed data.

We need to write that principle mathematically.


4. The Discovery: Likelihood

For one coin toss,

P(X=1∣p)=p,P(X=0∣p)=1−p.P(X=1\mid p)=p, \qquad P(X=0\mid p)=1-p.

Suppose our data are D={x1,…,xn}D=\{x_1,\ldots,x_n\} and each observation is modeled with parameter θ\theta.

If observations are conditionally independent given θ\theta, their joint probability is

P(D∣θ)=∏i=1nP(xi∣θ).P(D\mid\theta)=\prod_{i=1}^{n}P(x_i\mid\theta).

Now hold the data fixed and let the parameter move.

The same mathematical expression is called the likelihood:

L(θ∣D)=P(D∣θ)\boxed{L(\theta\mid D)=P(D\mid\theta)}

The data are no longer the thing changing. The parameter is.

LevelThe same idea
💡 IntuitionTry different parameter dials. Ask: “With these dial settings, how believable is the dataset I actually saw?”
✏️ NumbersFor 8 heads and 2 tails: L(p)=p8(1−p)2L(p)=p^8(1-p)^2. At p=0.5p=0.5, L=0.01953125L=0.01953125. At p=0.8p=0.8, L=0.880.22≈0.02684L=0.8^8 0.2^2\approx0.02684.
🎓 AbstractionL(θ∣D)=∏iP(xi∣θ)L(\theta\mid D)=\prod_i P(x_i\mid\theta). Maximum likelihood estimation chooses θ^=arg⁡max⁡θL(θ∣D)\hat\theta=\arg\max_\theta L(\theta\mid D).

The notation arg⁡max⁡\arg\max means: the parameter value at which the expression is largest.


5. Why Products Become a Problem

The likelihood is a product.

For ten observations, that is manageable. For one million observations, the product can become an extremely tiny number.

For independent data:

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

A product is also awkward to differentiate.

So we ask:

Can we transform the likelihood without changing which parameter value is best?

Yes.

Take the logarithm.

Because log⁡\log is strictly increasing,

arg⁡max⁡θL(θ)=arg⁡max⁡θlog⁡L(θ).\arg\max_\theta L(\theta) = \arg\max_\theta \log L(\theta).

And the logarithm turns products into sums:

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

This is the log-likelihood.

Why this is more than a numerical trick

The logarithm preserves the ordering of candidates while changing multiplication into addition. That gives us both numerical stability and much simpler derivatives.


6. The Coin Derivation

For a Bernoulli observation xi∈{0,1}x_i\in\{0,1\},

P(xi∣p)=pxi(1−p)1−xi.P(x_i\mid p)=p^{x_i}(1-p)^{1-x_i}.

Therefore

L(p)=∏i=1npxi(1−p)1−xi.L(p)=\prod_{i=1}^{n}p^{x_i}(1-p)^{1-x_i}.

Take logs:

ℓ(p)=log⁡L(p)=∑i=1n[xilog⁡p+(1−xi)log⁡(1−p)].\ell(p)=\log L(p) =\sum_{i=1}^{n}\left[x_i\log p+(1-x_i)\log(1-p)\right].

Differentiate:

dℓdp=∑i[xip−1−xi1−p].\frac{d\ell}{dp} = \sum_i\left[\frac{x_i}{p}-\frac{1-x_i}{1-p}\right].

For our data, ∑ixi=8\sum_i x_i=8 and n=10n=10:

dℓdp=8p−21−p.\frac{d\ell}{dp} = \frac{8}{p}-\frac{2}{1-p}.

At the optimum the derivative is zero:

8p=21−p.\frac{8}{p}=\frac{2}{1-p}.

Cross-multiply:

8(1−p)=2p.8(1-p)=2p.

So

8=10p⇒p^=0.8.8=10p \qquad\Rightarrow\qquad \boxed{\hat p=0.8}.

The observed frequency did not appear by magic. Maximum likelihood derived it.


7. From Maximizing Likelihood to Minimizing Loss

Machine-learning code often minimizes a loss rather than maximizes a likelihood.

These are the same optimization written in opposite directions.

Define the negative log-likelihood:

L(θ)=−log⁡L(θ).\boxed{\mathcal L(\theta)=-\log L(\theta)}.

Then

arg⁡max⁡θL(θ)=arg⁡min⁡θ[−log⁡L(θ)].\arg\max_\theta L(\theta) = \arg\min_\theta[-\log L(\theta)].

For independent data,

L(θ)=−∑ilog⁡P(xi∣θ).\mathcal L(\theta) = -\sum_i\log P(x_i\mid\theta).

This is the key bridge:

probability model
      ↓
 likelihood of observed data
      ↓  take log
 log-likelihood
      ↓  multiply by −1
 loss / negative log-likelihood
      ↓
 gradient descent

Machine Learning Connection

A loss function does not have to be invented by taste. Once you specify a probabilistic model for the data, negative log-likelihood gives a principled objective.


8. The Gaussian Surprise: Why Squared Error Appears

Now use a different data story.

Suppose a house price is modeled as

y^i=wxi+b\hat y_i = w x_i+b

and the errors are assumed to be Gaussian with mean zero and standard deviation σ\sigma:

yi∣xi∼N(y^i,σ2).y_i\mid x_i \sim \mathcal N(\hat y_i,\sigma^2).

The density is

P(yi∣xi,w,b)=12πσ2exp⁡(−(yi−y^i)22σ2).P(y_i\mid x_i,w,b) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left(-\frac{(y_i-\hat y_i)^2}{2\sigma^2}\right).

Take the negative log and discard terms that do not depend on w,bw,b:

−log⁡P(yi∣xi,w,b)=constant+(yi−y^i)22σ2.-\log P(y_i\mid x_i,w,b) = \text{constant} + \frac{(y_i-\hat y_i)^2}{2\sigma^2}.

Summing over all observations gives

L(w,b)=constant+12σ2∑i(yi−wxi−b)2\boxed{ \mathcal L(w,b) = \text{constant} + \frac{1}{2\sigma^2} \sum_i(y_i-wx_i-b)^2 }

Therefore minimizing negative log-likelihood is equivalent to minimizing squared error.

This is the deeper lesson:

Squared error is not a law of nature. It is the negative log-likelihood that appears when we assume Gaussian noise.

Change the probability model, and the loss can change too.


9. The Geometry of the Objective

For a linear model, parameters such as ww and bb define a point in parameter space.

The loss assigns every point one height.

loss
 ^
 |                 •
 |            •         •
 |       •                 •
 |  •                           •
 +---------------------------------> parameter
                 minimum

Maximum likelihood says:

Find the parameter setting where the observed data receives the highest probability.

Negative log-likelihood says the same thing as a valley:

Find the parameter setting where the loss is smallest.


🔬 The Experiment

Use the notebook to vary one thing at a time:

  1. Keep the 8-head/2-tail dataset fixed and sweep pp from 0.01 to 0.99.
  2. Plot likelihood and negative log-likelihood.
  3. Verify that both choose the same pp.
  4. Change the number of heads and observe the optimum move.

Predict first. Then run.


How It Breaks

FailureWhat it looks likeWhy it is wrong
Maximize raw probability forevertiny underflowing productsnumerical scale collapses
Treat likelihood as probability of the parameter“P(θ∣D)P(\theta\mid D)” without a priorthat is a Bayesian quantity, not ordinary likelihood
Forget independence assumptionsmultiply incompatible termsjoint probability may not factor this way
Assume squared error is universaluse it for every target typethe implied noise model matters
Ignore constants carelesslyclaim two objectives are always numerically equalthey are often equivalent only up to parameter-independent constants/scales

Shapes

For nn independent scalar observations:

x       : (n,)
y       : (n,)
params  : depends on model
loglik  : scalar
loss    : scalar

The important shape fact is simple:

A dataset can contain many observations, but the objective is one scalar.

That scalar is what gradient descent knows how to optimize.


Distinctions That Matter

Confusable pairDifference
Probability vs likelihoodProbability varies with possible data while parameters are fixed; likelihood treats observed data as fixed and compares parameters.
Likelihood vs posteriorLikelihood is P(D∣θ)P(D\mid\theta); posterior is proportional to P(D∣θ)P(θ)P(D\mid\theta)P(\theta).
Log-likelihood vs likelihoodSame optimum because log is strictly increasing, but different numerical scale.
Loss vs negative log-likelihoodNegative log-likelihood is a loss; not every hand-designed loss is automatically an NLL.

What We Discovered

  1. A model can be judged by how plausible it makes the data we actually observed.
  2. That comparison is the likelihood.
  3. Maximizing likelihood gives a general parameter-estimation rule.
  4. Taking logs turns products into sums without changing the best parameter.
  5. Minimizing negative log-likelihood turns statistical estimation into the optimization language of machine learning.
  6. Squared error appears naturally from a Gaussian observation model.

Mathematics We Built

L(θ∣D)=P(D∣θ)L(\theta\mid D)=P(D\mid\theta) θ^MLE=arg⁡max⁡θL(θ∣D)\hat\theta_{MLE}=\arg\max_\theta L(\theta\mid D) ℓ(θ)=log⁡L(θ)\ell(\theta)=\log L(\theta) arg⁡max⁡ℓ(θ)=arg⁡max⁡L(θ)\arg\max \ell(\theta)=\arg\max L(\theta) L(θ)=−ℓ(θ)\mathcal L(\theta)=-\ell(\theta) θ^MLE=arg⁡min⁡θL(θ)\hat\theta_{MLE}=\arg\min_\theta\mathcal L(\theta)

What Each Symbol Means

SymbolRead it asMeaningIn code
DD“data”observed examplesdata
θ\theta“theta”model parameterstheta
LL“likelihood”plausibility of observed data under θ\thetalikelihood
ℓ\ell“ell”log-likelihoodlog_likelihood
L\mathcal L“script L”negative log-likelihood lossloss
θ^\hat\theta“theta-hat”estimated parametertheta_hat

One-Minute Explanation

Suppose you have a model with a few adjustable knobs. You already saw some data. Try a candidate setting of the knobs and ask: how well would these settings explain the exact data I observed? That score is the likelihood. Pick the setting with the largest score. Taking a log makes products easier to work with, and putting a minus sign turns “make it large” into the usual machine-learning instruction “make the loss small.” Different assumptions about how data are generated lead to different losses.


Exercises

Level 1 — Observe

Look at the likelihood curve for 8 heads and 2 tails. Where is its peak, and why should the peak not be at p=0.5p=0.5?

Level 2 — Calculate

Compute L(p)L(p) for p=0.2p=0.2, 0.50.5, and 0.80.8 for the sequence with 8 heads and 2 tails.

Level 3 — Derive

Starting from ℓ(p)=8log⁡p+2log⁡(1−p)\ell(p)=8\log p+2\log(1-p), derive p^=0.8\hat p=0.8.

Level 4 — Investigate

Change the data to 3 heads and 7 tails. Predict the MLE before running the notebook.

Level 5 — Design

Invent a small observation model for another problem where you can derive the corresponding negative log-likelihood. Explain what assumption about noise or outcomes you made.


Common Mistakes

MistakeWhy it is wrong
Saying “likelihood is the probability that the parameter is true”That changes P(D∣θ)P(D\mid\theta) into a different quantity.
Dropping constants before identifying what depends on parametersConstants can be dropped only when optimizing over parameters and only if they truly do not depend on them.
Thinking the log changes the optimumA strictly increasing transform preserves ordering.
Treating every loss as an MLESome losses are design choices or arise from different assumptions.

Socratic Questions

  • Why does the logarithm preserve the maximizing parameter?
  • What would change if the observations were not independent?
  • Why does a Gaussian assumption lead to squared error rather than absolute error?
  • What extra ingredient would turn likelihood into a posterior?

🔭 Bridge to Chapter 038

We can now derive an objective instead of merely choosing one.

Next question:

What happens when we fit a real linear model to many observations and let maximum likelihood choose its parameters?

That is where linear regression stops being a memorized formula and becomes a complete statistical model.

Six ways to work the chapter, climbing from explaining it in your own words to investigating it like a researcher. Reading is not enough — this is where the chapter lands.

Exercises should progress from understanding to research behaviour.

Level 1 — Explain it

Explain the idea in plain language without equations.

Level 2 — Calculate it

Solve a tiny numerical example by hand.

Level 3 — Predict it

Predict code output, model behaviour, tensor shapes, or graph movement before running anything.

Level 4 — Break it

Deliberately change an assumption or parameter and explain what fails.

Level 5 — Build it

Implement the core mechanism from scratch before using a high-level library.

Level 6 — Investigate it

Form a hypothesis, design an experiment, compare alternatives, and explain the evidence.