Loss Functions and Optimization Foundations

Loss Functions

Loss functions as negative log-likelihoods that turn probabilistic assumptions into optimization objectives.

Lesson
17

Goal

Understand why machine learning usually talks about minimizing a loss instead of maximizing a likelihood, and why these are often the same optimization problem written in different forms.

By the end of this lesson, you should be able to explain why least squares comes from a Gaussian noise assumption rather than appearing as an arbitrary design choice.

Motivation

MLE gave us a clean principle:

Choose the parameter values that make the observed data most likely.

But machine learning code, papers, and libraries usually do not say “maximize likelihood.” They say things like:

Why the change in language?

Because optimization systems like minimization. Also, once we take logs and drop constants, the resulting expressions are easier to compute, easier to differentiate, and easier to recognize across models.

This lesson is the moment where statistics starts looking like optimization.

Prerequisites

Notation / Symbol Table

SymbolPlain-English nameMeaning
DDdatasetThe full collection of observed examples.
θ\thetaparametersThe values the model can change during fitting.
L(θ)L(\theta)likelihoodProbability or density of the data under the model.
(θ)\ell(\theta)log-likelihoodlogL(θ)\log L(\theta).
(θ)-\ell(\theta)negative log-likelihoodA minimization version of the same fitting objective.
xix_iinputFeatures for example ii.
yiy_itargetObserved output for example ii.
fθ(xi)f_{\theta}(x_i)predictionModel output for input xix_i.
ϵi\epsilon_inoiseDifference between observation and prediction.
σ2\sigma^2noise varianceSpread of the Gaussian error model.

The core transformation is

θ^=argmaxθL(θ)=argmaxθ(θ)=argminθ((θ)).\hat{\theta} = \arg\max_{\theta} L(\theta) = \arg\max_{\theta} \ell(\theta) = \arg\min_{\theta} \bigl(-\ell(\theta)\bigr).

Core Ideas

Log-Likelihood Keeps The Same Best Parameters

For independent observations,

L(θ)=iP(xiθ).L(\theta) = \prod_i P(x_i \mid \theta).

Taking logs turns this into

(θ)=ilogP(xiθ).\ell(\theta) = \sum_i \log P(x_i \mid \theta).

This does not change the maximizing parameter because the logarithm preserves ordering.

That means the log-likelihood is not a new objective. It is a friendlier encoding of the old one.

Negative Log-Likelihood Turns Maximization Into Minimization

Many optimization tools are built around minimization. So after taking logs, we flip the sign:

Loss(θ)=(θ).\text{Loss}(\theta) = -\ell(\theta).

Now the question becomes:

Which parameters make the penalty as small as possible?

This is just a convention, but it is a powerful one. It makes very different models look structurally similar.

A Loss Function Often Falls Out Of A Probability Model

This is the key lesson:

Many standard loss functions are negative log-likelihoods.

Examples:

So a loss function is often not something we invent from taste. It is something implied by the distribution we chose for the data.

Gaussian Noise Produces Squared Error

Suppose the model is

yi=fθ(xi)+ϵiy_i = f_{\theta}(x_i) + \epsilon_i

with

ϵiN(0,σ2).\epsilon_i \sim \mathcal{N}(0, \sigma^2).

This says:

Observed value = model prediction + random Gaussian noise.

Under this assumption, the conditional density of yiy_i is

P(yixi,θ)=12πσ2exp((yifθ(xi))22σ2).P(y_i \mid x_i, \theta) = \frac{1}{\sqrt{2\pi \sigma^2}} \exp\left( -\frac{(y_i - f_{\theta}(x_i))^2}{2\sigma^2} \right).

Taking logs gives

logP(yixi,θ)=(yifθ(xi))22σ2+constant.\log P(y_i \mid x_i, \theta) = -\frac{(y_i - f_{\theta}(x_i))^2}{2\sigma^2} + \text{constant}.

Summing over the dataset and negating yields

(θ)=12σ2i(yifθ(xi))2+constant.-\ell(\theta) = \frac{1}{2\sigma^2} \sum_i (y_i - f_{\theta}(x_i))^2 + \text{constant}.

Everything except the squared residual term is constant with respect to θ\theta. So minimizing the negative log-likelihood is equivalent to minimizing

i(yifθ(xi))2.\sum_i (y_i - f_{\theta}(x_i))^2.

That is the least-squares objective.

Constants And Positive Rescaling Need Different Attention

Two common transformations behave differently:

  1. Add a constant:

    J(θ)=J(θ)+5000J'(\theta) = J(\theta) + 5000

    The optimum does not change, and the gradients do not change either.

  2. Multiply by a positive constant:

    J(θ)=100J(θ)J'(\theta) = 100 J(\theta)

    The optimum still does not change, but the gradients become 100 times larger.

That second point matters once we start using gradient descent. The best parameter can stay the same while the optimization path changes a lot.

Worked Example

Assume a one-parameter regression model predicts

y^i=fθ(xi)\hat{y}_i = f_{\theta}(x_i)

and the observed targets satisfy

yi=y^i+ϵi,ϵiN(0,σ2).y_i = \hat{y}_i + \epsilon_i, \qquad \epsilon_i \sim \mathcal{N}(0, \sigma^2).

For one example, the density is

P(yixi,θ)=12πσ2exp((yiy^i)22σ2).P(y_i \mid x_i, \theta) = \frac{1}{\sqrt{2\pi \sigma^2}} \exp\left( -\frac{(y_i - \hat{y}_i)^2}{2\sigma^2} \right).

Now apply the sequence slowly:

  1. Multiply these terms across independent examples to get the likelihood.
  2. Take logs so the product becomes a sum.
  3. Negate so we can minimize instead of maximize.
  4. Drop constants that do not depend on θ\theta.

What survives is the sum of squared residuals.

That is the whole punchline:

Squared-error loss is the optimization face of a Gaussian probabilistic model.

Why This Shows Up In ML

When ML practitioners choose a loss, they are often making a statement about the data-generating process.

This connection is valuable because it lets us reason in both directions:

  1. Start from a probabilistic assumption and derive the loss.
  2. Inspect a loss and ask what statistical assumptions it implies.

That is one of the big organizing ideas in statistical machine learning.

Exercises

Answer these in words first, then with formulas:

  1. Why does maximizing log-likelihood pick the same optimum as maximizing likelihood?
  2. Why do we introduce the negative sign after taking logs?
  3. If we add +5000 to a loss, what changes and what stays the same?
  4. If we multiply a loss by 100, what stays the same and what changes once gradient descent enters the picture?
  5. In one paragraph, explain why least squares emerges naturally from a Gaussian noise assumption.

Implementation Exercise

Write a TypeScript function that computes a Gaussian negative log-likelihood for a fixed variance:

type Example = { x: number; y: number };

const gaussianNll = (
  examples: Example[],
  predict: (x: number, theta: number) => number,
  theta: number,
  sigma2: number,
): number =>
  examples.reduce((total, { x, y }) => {
    const residual = y - predict(x, theta);
    return total + residual ** 2 / (2 * sigma2);
  }, 0);

Then compare its minimizer against the minimizer of plain squared error on a small grid of parameter values. They should occur at the same place when sigma2 is fixed.

Reflection Questions