11 Statistical Machine Learning
11.1 Classification Problem
Classification problem asks: Given an input, which category does it
belong to?
In supervised learning context, we are given training data of pairs of
input and the corresponding labels.
The computer learns from these pairs and produces a hypothesis function
,
where
The label set
is finite. In comparison, regression problems’ outputs are
continuous.
There are two flavours of classification:
Binary classfication: there are exactly two possible labels. Implication is that we only need to model the probability of one class. Loss function is logistic loss.
Multiclass classification: there are more than two labels. Implication is that we need to output a probability for each of the classes, and they must all sum to 1 using tools like the softmax function. Loss function is cross-entropy loss.
11.2 Binary classification - Logistic Regression
Logistic Regression is for binary classification.
Recall the sigmoid function that converts any real number into a
probability:
When
is positive and large,
,
when
negative and large,
,
when
.
In effect, it acts as a squashing function.
The probabilistic model is (where
are the weights/parameters and
is the linear scoring function),
Decision boundary: the set of points where
is the hyperplane separating the two classes. Points on one side are
classified +1, the other side -1.
The unified expression, for
,
is
Using MLE (by taking argmax of sum of log-probabilities), the MLE
becomes:
And the logistic loss is:
Intuition:
captures whether the prediction is correct. If
is positive, the prediction and true label agree
the loss is small. Otherwise
is large and negative
the loss is large.
Once
is learned, predictions are made by:
11.3 Multiclass classification - Softmax, Cross-entropy loss
Here we need to output a probability for each of the
classes.
The hypothesis function outputs a vector of
scores:
Each
is the raw score, or logit, for class
.
Given these scores, the predicted class is just the one with the highest
score:
That is for prediction. But for learning
,
we need to convert the scores to probabilities (to use MLE).
The softmax function converts the score vector into a valid probability
distribution.
For a vector
,
i.e.
corresponds to
.
Softmax preserves the ranking of scores, but squashes everything to make
the sum = 1.
We can then write a probabilistic model. Assume the true structure on
given
is:
Note that this just softmax applied
,
evaluated at position
,
the true class.
Then apply MLE to find the
that makes the training data most probable.
The log-likelihood for a single data point
()
is:
Maximising this over all training data is equivalent to minimising its
negative, i.e.
This is the cross-entropy loss.
Intuition:
pushes the score of the true class to be as large as possible.
grows whenever any score class gets large, acting as a penalty.
So the loss is minimized when the true class score is large relative to all other scores.
Context on the rough training flow:
With
a set of
weight vectors that parameterise the hypothesis function. Initially
random,
is learned by repeating this loop:
Feed each training example through to get raw scores.
Apply softmax to convert scores into probabilities.
Since we know the true label , compute the cross-entropy loss. How wrong was the model about the true class?
Average the losses across all examples ().
Compute the gradient .
Update in the opposite direction: .
11.3.1 Expected loss, KL Divergence
So far we are minimising the empirical loss (the avg loss over the
training data).
But we care about how the model performs over all possible inputs, not
just the ones we trained on.
The expected loss is the average loss over the true data distribution:
We get
The second term does not
depend on
at all.
Therefore minimising the expected cross-entropy loss is equivalent to
minimising just the KL divergence term. This means finding the
that makes the model’s predicted distribution
as close as possible to the true distribution
.
Recall that KL divergence measures the degree of discrepancy between two
probability distributions
and
:
So minimising cross entropy is just minimising:
where
is the full probability distribution over all possible classes
,
given input
and parameters
.
So we are comparing the true distribution
vs the model’s predicted distribution
.
11.4 Loss functions
A loss function
measures the error between a prediction
and the true label
.
Training a model means finding the
that minimizes the total loss over training data. Different problems
require different loss functions.
A recap:
Regression problems: the output is a continuous
value.
Squared loss:
Penalizes large errors heavily due to the square. Sensitive to
outliers.
-sensitive
loss:
Ignores errors smaller than
- useful when small errors are acceptable.
Binary classification lossses Used when
.
Logistic loss:
The product
captures correctness - positive means correct, negative means
wrong.
Hinge loss:
Used in Support Vector Machines (SVMs). Zero loss when the prediction is
correct and confident
().
Doesn’t care about how confident correct predictions are beyond a
margin.
Exponential loss:
Used in boosting algorithms. Penalizes wrong predictions extremely
aggressively - more sensitive to outliers than logistic loss.
All binary classfication losses depend on the product
.
When
is large and positive, it is a correct, confident prediction and the
loss should be small.
Exponential loss grows fastest, logistic loss grows moderately, hinge
loss is zero for sufficiently correct predictions, and linear
otherwise.
Multiclass Classification Loss
Cross-entropy loss:
Minimizing this is equivalent to minimizing KL divergence from the true
distribution.
11.5 Supervised learning framework
The goal of supervised learning is to find a hypothesis function that performs well on new, unseen data.
11.5.1 Expected loss minimisation
The ideal objective is to minimise the expected loss:
This means averaging over all possible inputs, weighted by how likely
each input is to occur in the real world.
However, we don’t have every possible input, but only a finite training
dataset. So we cannot compute
directly.
11.5.2 Empirical loss minimisation
Instead, we approximate using the empirical loss over training data : This is just the average loss over the training examples. As grows, becomes a better approximation of . But minimising the empirical loss does not guarantee minimizing the expected loss, because the training data is just a sample of the real world. If optimise too hard for that specific sample, the model learns the training data rather than the underlying true pattern. Hence overfitting: the empirical loss is very low but the expected loss remains high.
11.5.3 Overfitting
Overfitting occurs when the gap between empirical loss and expected
loss is large. The model performs well on training data but poorly on
new unseen data:
where
is the hypothesis function learned from training data
.
It happens when the model is too complex and has enough flexibility to
memorise the training data rather than learn the underlying
pattern.
Also, with more data, the empirical loss becomes a better approximation
of the expected loss, reducting the gap. Overfitting is most dangerous
when data is scarce relative to model complexity.
We can prevent it with appropriate modelling (choose model whose
complexity matches the true structure of the problem) and regularisation
(penalise model complexity during training, prevent
from growing too large).
11.5.4 The Model
We want to find the best
,
but there are infinite functions. So we restrict the search to a model,
i.e. a specific family of functions parameterised by
:
We decide a shape for the function output first, then training just
finds the best version of that shape.
Example - we choose a linear function for the model:
.
Then we restrict the search to only straight lines. Even if the true
relationship is curved, the model will never capture it. This is
bias.
On the other hand, if we choose a complex model like deep neural
network, it can represent almost any function, but it has so many
parameters that it can memorise the training data. This is variance.
11.6 Regularisation
Overfitting happens when the model is too complex. Regularisation is
one way to fix this by penalising complexity directly in the training
objective.
By adding the regularisation term, it pushes
to stay small.
is the regularisation coefficient.
too large: regularisation dominates and model may be too simple. May cause underfitting.
too small: empirical loss dominates and model may get too complex. May cause overfitting.
Hence choosing the right
is a hyperparameter tuning problem.
Common regularisation terms:
L2 Regularisation (Ridge):
Penalizes large weights by squaring them. Shrinks all weights uniformly
toward zero but never exactly to zero. Good when all features are
expected to contribute something.
L1 Regularisation (Lasso):
Penalizes weights by their absolute value. Tends to push weights to
exactly zero — effectively removing irrelevant features from the model.
Good when you expect only a few features to matter (sparsity).
Elastic Net:
A combination of L1 and L2.
controls the mix between the two. Useful when you want some sparsity
(L1) but also stable shrinkage (L2).
11.7 Bias-variance tradeoff
Model complexity is defined as the diversity of functions contained
in the model class
.
A model
is more complex than
if
.
For example, a polynomial model is more complex than a linear model
because every linear function is a polynomial, but not every polynomial
is linear.
Bias measures how wrong the model is on average due to
its simplifying assumptions.
Variance measures how much your learned model changes
depending on the training data.
Complex model has more parameters, more sensitive to the specific
training data.
The expected loss can be decomposed into bias and variance:
We want the optimal complexity where the total expected loss is
minimized.
11.8 Model selection
We know there is an optimal model complexity. To find it, we need to
choose the model class to use, the number of parameters, the value of
for regularisation etc.
These are hyperparameters, i.e. settings that control the learning
process itself, not learned from training.
We need a way to estimate the expected loss without using the test
data.
11.8.1 Holdout Method
Split data into 2 parts:
Train the model on .
Evaluate the average loss on (validation data).
Use the validation loss as an estimate of the expected loss.
Choose the hyperparameters that give the lowest validation data.
Since the model never sees
during training, the validation loss is an unbiased estimate of how well
the model generalises.
However, if the process is repeated many times trying different
hyperparameters, the model starts to indirectly overfit to the
validation data too. So the final evaluation must always be done on a
completely separate test set that is never touched during model
selection.
Holdout method is also high variance: the validation estimate depends
heavily on which specific examples ended up in
.
11.8.2 Cross-validation (-fold CV)
Instead of one fixed split, rotate through different splits:
Divide data into equal subsets.
For each subset :
Use subset as validation data.
Train on all other subsets.
Record validation loss on subset .
Average the validation losses to give a CV score.
Every example is used for both training and validation, just never at
the same time. This makes CV more data-efficient and less sensitive to
how the data is split than the holdout method.
Computing the CV score for each candidate value of the hyperparameter,
we choose the value that gives the lowest CV score.
Leave-one-out CV (LOOCV)
A special case where
.
Each fold contains exactly one example as validation data, trained on
all remaining
examples.
Advantage — uses almost all data for training each time, very low bias
in the estimate.
Disadvantage — computationally expensive, requires n separate training
runs.
LOOCV is useful when data is very scarce and you cannot afford to leave
much out for validation.