9 Linear & Logistic Regression

9.1 Types of machine learning

9.1.0.1 Supervised learning

The training data consists of input-output pairs (x,y)(x, y), where:

The goal is to learn the true underlying relationship between xx and yy from these examples, so that when you see a new, unseen xx, you can correctly predict its yy.
The real-life challenge is that the quality of a supervised learning model is heavily dependent on the quality and quantity of labelled data.

9.1.0.2 Unsupervised learning

The training data consists of only inputs xx — there are no output labels yy.
The model must find structure, patterns, or useful knowledge entirely on its own.

9.2 Types of Supervised learning problems

9.2.0.1 Regression

The output yy is a continuous value - meaning it can take any real number within some range, rather than belonging to a fixed set of categories.

9.2.0.2 Classification

The output yy is a discrete value - meaning it belongs to one of a fixed, finite set of categories or classes.
Both types of problems have different loss functions and model designs.

9.3 Linear Regression

The simplest form of regression problem is linear regression.
For an input data point x∈ℝdx \in \mathbb{R}^d (meaning xx is a vector of dd numbers, i.e. dd features), we predict its label y∈ℝy \in \mathbb{R} using y≈wTx+by \approx w^T x + b where w∈ℝdw \in \mathbb{R}^d is the weight vector and each element wjw_j controls how much the jj-th feature of xx contributes to the prediction. The bias parameter b∈ℝb \in \mathbb{R} shifts the entire prediction up or down regardless of xx.

9.3.1 Basis functions

A limitation of the basic equation wTx+bw^T x + b is that it can only represent straight, linear functions.
We introduce basis functions ϕj:ℝd→ℝ\phi_j : \mathbb{R}^d \rightarrow \mathbb{R} for j=1,2,...,pj=1, 2, ..., p. Instead of feeding the raw input xx into a linear model, we first transform xx using these basis functions, then apply a linear model to the transformed features. y≈wTϕ(x)+by \approx w^T \phi(x) + b Note that the model remains linear with respect to its learnable weights (ww), meaning it is still computationally easy to train. However, it becomes non-linear with respect to the original inputs (xx), allowing it to fit curved data.

9.3.1.1 Example - polynomial basis functions

If our input is 1D (d=1d=1), we can define our basis functions simply as powers of xx: ϕj(x)=xj\phi_j(x) = x^j.
Plugging this into the model: wTϕ(x)+b=wpxp+wp−1xp−1+...+w1x+bw^T \phi(x) + b = w_p x^p + w_{p-1} x^{p-1} + ... + w_1 x + b

9.4 Parameter Estimation for Linear Regression

Given the linear model and training data, how to find the best possible set of weights?
Here, we assume bias b=0b=0.

9.4.1 Least Squares Method (LSM)

Given a dataset of nn training examples denoted as (xi,yi)(x_i, y_i) for i=1,…,ni = 1, \dots, n , LSM attempts to find the optimal weight vector, denoted as ŵ\hat{w}, by minimizing the average squared difference between the model’s predictions and the actual true labels. ŵ=arg⁡minw∈ℝd∑i=1n(yi−wTxi)2\hat{w} = \arg \min_{w \in \mathbb{R}^d} \sum_{i=1}^n (y_i - w^T x_i)^2 Note that in standard Least Squares, the averaging factor of 1/n1/n is conventionally omitted from the formula, though it is usually included in broader concepts like Empirical Risk Minimization.
Why squared error?

In vector/matrix formulation, if we stack the nn labels into a vector y∈ℝny \in \mathbb{R}^n, and stack all input features into a matrix XT∈ℝd×nX^T \in \mathbb{R}^{d \times n}, we get ŵ=arg⁡minw∈ℝd∥y−Xw∥2\hat{w} = \arg \min_{w \in \mathbb{R}^d} \|y - Xw\|^2 Because of this matrix formulation, if we assume that the covariance-like matrix Σ̂=XTX\hat{\Sigma} = X^T X is invertible, we can solve for the optimal weights directly without iterative guessing. The exact, closed-form solution is: ŵ=Σ̂−1XTy\hat{w} = \hat{\Sigma}^{-1} X^T y This is derived by taking the gradient of ∥y−Xw∥2\|y - Xw\|^2 with respect to ww, setting it to zero, and solving. Σ̂=XTX\hat{\Sigma} = X^TX is invertible when the columns of XX are linearly independent. This means that n≥dn \geq d (i.e. at least as many training examples as features), and no redundant features (i.e. no linear combination of another column).
If Σ̂\hat{\Sigma} is not invertible, the solution is not unique — multiple values of ww give the same minimum loss.

9.4.2 MLE

MLE takes a probabilistic perspective rather than purely looking at geometric errors. It assumes that there is some "true" unknown weight w*w_* , but the real-world labels we observe (yy) are corrupted by random Gaussian noise.
Therefore, the rule generating our data is y=w*Tx+ε,ε∼N(0,σ2),y∼N(w*Tx,σ2)y = w_*^T x + \varepsilon, \quad \varepsilon \sim N(0, \sigma^2), \quad y \sim N(w_*^T x, \sigma^2) We want to find the true weights w*w_*.
The probabilistic model of yy that is normal distributed, centered at w*Txw_*^T x, with spread σ2\sigma^2 is P(y|w,x)=N(wTx,σ2)(y)P(y|w,x)=N(w^T x,\sigma^2)(y) Assuming the training examples are independent of each other given their inputs, the joint probability of observing all labels y=[y1,...,yn]Ty = [y_1, ..., y_n]^T is: p(y|w,X)=∏i=1nN(wTxi,σ2)(yi)=1(2πσ2)n/2∏i=1nexp⁡(−12σ2(yi−wTxi)2)\begin{align*} p(y|w,X)&=\prod_{i=1}^n N(w^T x_i, \sigma^2)(y_i)\\ &= \frac{1}{(2\pi\sigma^2)^{n/2}} \prod_{i=1}^n \exp\left(-\frac{1}{2\sigma^2}(y_i - w^T x_i)^2\right) \end{align*} Taking logarithm on both sides, log⁡(p(y|w,X))=−n2log⁡(2πσ2)−∑i=1n12σ2(yi−wTxi)2\log(p(y|w,X)) = -\frac{n}{2}\log(2\pi\sigma^2) - \sum_{i=1}^n \frac{1}{2\sigma^2}(y_i - w^T x_i)^2 The MLE problem is then: ŵ=arg⁡maxw∈ℝd{−n2log(2πσ2)−∑i=1n12σ2(yi−wTxi)2}\hat{w} = \arg \max_{w\in\mathbb{R}^d} \left\{-\frac{n}{2}\log(2\pi\sigma^2) - \sum_{i=1}^n \frac{1}{2\sigma^2}(y_i - w^T x_i)^2\right\} Note that

  1. The first term is a constant with respect to ww — it doesn’t affect which ww is optimal.

  2. The second term is a negative scaled version of the LSM objective. Maximising the log-likelihood is exactly equivalent to minimising the sum of squared errors.

Hence, LSM and MLE under Gaussian give the same answer.

9.4.3 Other loss functions (not Gaussian)

The squared error formula ∑(yi−wTxi)2\sum (y_i - w^T x_i)^2 is entirely attributed to our assumption that the underlying noise is Gaussian.
But what if the data has a different noise structure?
Assume the noise follows a Laplace distribution, defined as p(y)=12bexp⁡(−|y−μ|b)p(y) = \frac{1}{2b}\exp(-\frac{|y-\mu|}{b}) The Laplace distribution has heavier tails — meaning it assigns higher probability to values far from the mean. In practice this means it’s a better model for data with outliers.
Following the same logic as before, the joint likelihood is: p(y|w,X)=1(2b)n∏i=1nexp⁡(−1b|yi−wTxi|)p(y|w,X)=\frac{1}{(2b)^n}\prod_{i=1}^{n}\exp(-\frac{1}{b}|y_i-w^T x_i|) Maximising the log-likelihood gives: ŵ=arg⁡maxw∈ℝd{−nlog(2b)−∑i=1n1b|yi−wTxi|[citestart]}\hat{w} = \arg \max_{w \in \mathbb{R}^d} \left\{ -n \log(2b) - \sum_{i=1}^n \frac{1}{b}|y_i - w^T x_i| [cite_start]\right\} Dropping constants, we get ŵ=arg⁡minw∈ℝd∑i=1n|yi−wTxi|\hat{w} = \arg \min_{w \in \mathbb{R}^d} \sum_{i=1}^n |y_i - w^T x_i| This simplifies to finding the weights that minimize the absolute error instead of squared error.
The implication is that every choice of loss function corresponds to an implicit assumption about the noise distribution in the data.

9.5 Overfitting & Regularisation

9.5.1 Overfitting

Increasing a model’s complexity (like using higher-degree polynomials) gives it more expressive power. If a model has a higher degree of freedom than the true function that generated the data, it can cause overfitting. In other words, the model learns not just the true underlying pattern in the data, but also the random noise specific to the training set. Graphically, an overfitted curve would try to hit every data point, causing aggressive fluctuations, and making a new prediction on unseen data would be inaccurate.

Overfitting (orange curve)

9.5.2 Ridge regression (L2L_2-Regularisation)

To combat overfitting, we add a regularisation term to the LSM objective. This term penalises large parameter values, discouraging the model from becoming too complex and fitting the noise. ŵ=arg⁡minw∈ℝd{||y−Xw||2+λ||w||2}\hat{w} = \arg \min_{w \in \mathbb{R}^d} \{||y - Xw||^2 + \lambda||w||^2\} The L2L_2 regularisation term λ||w||2\lambda||w||^2 consists of:

Recall that overfitting is caused by the model making wild swings between data points, and this requires large weight values.
Hence, the model now has to balance two competing goals:

  1. Minimising the squared error, and

  2. keeping the weights as small as possible.

Just like standard LSM, Ridge regression has a closed-form solution.
With Σ̂=XTX\hat{\Sigma} = X^T X, ŵ=(λI+Σ̂)−1XTy\hat{w} = (\lambda I + \hat{\Sigma})^{-1} X^T y Recall that standard LSM requires Σ̂=XTX\hat{\Sigma} = X^T X to be invertible, which fails when there aren’t enough training examples or features are redundant. Adding λI\lambda I to Σ̂\hat{\Sigma} always makes the matrix invertible for any λ>0\lambda > 0, since it shifts all eigenvalues away from zero. This means Ridge regression is well-defined even when standard LSM breaks down.

9.5.2.1 Effect of regularisation strength λ\lambda

λ\lambda affects how much to penalise large weights.

  1. No regularization (λ=0\lambda = 0): This is just pure LSM with no penalty on weights, prone to overfitting.

  2. Proper regularization (λ\lambda at the right value): The model is constrained enough to ignore noise but flexible enough to capture the true shape. Strikes a good balance between training fit and generalisation

  3. Strong regularization (λ\lambda very large): The penalty on weight is too high, flattening the curve entirely into a straight line, leading to underfitting

In practice, λ\lambda might be chosen through cross-validation, i.e. testing different values of λ\lambda using training set and a validation set. λ\lambda is a hyperparameter, meaning it’s a parameter of the learning algorithm itself, not learned from the data directly like ww, but chosen by the user.

9.6 Bias-variance tradeoff

Recall the linear regression model: yi=w*Txi+εiy_i = w_*^T x_i + \varepsilon_i for fixed input data points xix_i, for i=1,…,ni=1,\ldots,n.
The noise has mean of 0, variance of σ2\sigma^2, and all the εi\varepsilon_i are independent of each other.
From the training data, we train the model, and get an estimated set of weights. This is the estimator ŵ\hat{w}, which is a function of the observed labels yiy_i. The implication is that ŵ\hat{w} is a random variable.

9.6.1 How good is the estimator ŵ\hat{w}?

We measure the expected squared error: 1n𝔼[||X(ŵ−w*)||2]\frac{1}{n} \mathbb{E}[||X(\hat{w} - w_*)||^2] This is the average squared difference between what the model predicts (XŵX\hat{w}) and what the true prediction should be (Xw*Xw_*).
By adding and subtracting E[ŵ]E[\hat{w}], we get 1n𝔼[||X(ŵ−𝔼[ŵ])||2]⏟Variance+1n||X(𝔼[ŵ]−w*)||2⏟Bias squared\underbrace{\frac{1}{n} \mathbb{E}[||X(\hat{w} - \mathbb{E}[\hat{w}])||^2]}_{\text{Variance}} \:+\: \underbrace{\frac{1}{n} ||X(\mathbb{E}[\hat{w}] - w_*)||^2}_{\text{Bias squared}} Interpretation:

Connection to overfitting/underfitting:

In both cases, the total error is high. The goal is find the sweet spot that minimises their sum by choosing the right regularisation weight λ\lambda.

9.6.2 Performance of Ridge Regression

Recall the formula for Ridge Regression weights (our estimator, ŵ\hat{w}): ŵ=(λI+Σ̂)−1XTY,Σ̂=XTX\hat{w} = (\lambda I + \hat{\Sigma})^{-1} X^T Y, \quad \hat{\Sigma} = X^TX Bias term
Since the true labels are generated by Y=Xw*+εY = Xw_* + \varepsilon, subbing in, ŵ=(λI+Σ̂)−1XT(Xw*+ε)=(λI+Σ̂)−1(Σ̂w*+XTε)\begin{align*} \hat{w} &= (\lambda I + \hat{\Sigma})^{-1} X^T (Xw_* + \varepsilon)\\ &= (\lambda I + \hat{\Sigma})^{-1} (\hat{\Sigma} w_* + X^T \varepsilon) \end{align*} Then taking expectation, 𝔼[ŵ]=(λI+Σ̂)−1Σ̂w*\mathbb{E}[\hat{w}] = (\lambda I + \hat{\Sigma})^{-1}\hat{\Sigma} w_* because 𝔼[ε]=0\mathbb{E}[\varepsilon]=0.
Note that 𝔼[ε]≠w*\mathbb{E}[\varepsilon]\neq w_*, instead it depends on both λ\lambda and Σ̂\hat{\Sigma}.
Since the bias is the difference between 𝔼[ε]\mathbb{E}[\varepsilon] and the truth w*w_*, bias squared=1n∥X(𝔼[ŵ]−w*)∥2=λ2n∥X(λI+Σ̂)−1w*∥2=λ2nw*T(λI+Σ̂)−2Σ̂w*\begin{align*} \text{bias squared} &= \frac{1}{n}\|X(\mathbb{E}[\hat{w}] - w_*)\|^2\\ &= \frac{\lambda^2}{n}\|X(\lambda I + \hat{\Sigma})^{-1}w_*\|^2\\ &= \frac{\lambda^2}{n} w_*^T (\lambda I + \hat{\Sigma})^{-2} \hat{\Sigma} w_* \end{align*} Variance term
The variance for Ridge Regression estimator is Variance=1nE[||X(ŵ−E[ŵ])||2]=σ2nTr((λI+Σ̂)−2Σ̂2)\text{Variance} = \frac{1}{n} E[||X(\hat{w} - E[\hat{w}])||^2] = \frac{\sigma^2}{n} Tr((\lambda I + \hat{\Sigma})^{-2} \hat{\Sigma}^2)

Therefore, for Ridge regression with L2L_2-regularisation (λ\lambda), we have: 1n𝔼[||X(ŵ−w*)||2]=λ2nw*T(λI+Σ̂)−2Σ̂w*⏟Bias2+σ2nTr((λI+Σ̂)−2Σ̂2)⏟Variance\frac{1}{n} \mathbb{E}[||X(\hat{w} - w_*)||^2] = \underbrace{\frac{\lambda^2}{n} w_*^T (\lambda I + \hat{\Sigma})^{-2} \hat{\Sigma} w_*}_{\text{Bias}^2} + \underbrace{\frac{\sigma^2}{n} Tr((\lambda I + \hat{\Sigma})^{-2} \hat{\Sigma}^2)}_{\text{Variance}} Key observations:

9.6.3 The Bias-Variance Tradeoff Curve

Bias-variance tradeoff (error vs model complexity)
Bias-variance tradeoff (Error against λ\lambda)

The goal is find λ\lambda that balances bias (underfitting) and variance (overfitting).

9.7 Logistic Regression

Despite its name, Logistic regression focuses on binary classification problem. We are trying to predict a discrete outcome that only has two possible states, mathematically represented as y∈{−1,1}y \in \{-1, 1\}.
We use a sigmoid function to squash a continuous output from the standard linear regression model (wTxw^T x), into a valid probability between 0 and 1. The sigmoid function is σ(z)=11+exp⁡(−z)\sigma(z) = \frac{1}{1 + \exp(-z)} Note that:

9.7.0.1 The probabilistic structure

For an input x∈ℝdx \in \mathbb{R}^d, and an unknown true parameter w*∈ℝdw_*\in\mathbb{R}^d,

9.7.0.2 The decision boundary

Note this critical equivalence: w*Tx≥0⇔σ(w*Tx)≥0.5w_*^T x \ge 0 \Leftrightarrow \sigma(w_*^T x) \ge 0.5 Hence when wTx>0w^T x > 0, the probability of Y=+1Y = +1 is >50%> 50\%, so we can predict class of +1.
Conversely, when wTx<0w^T x < 0, the probability of Y=+1Y = +1 is <50%< 50\%, so we can predict class of -1.
wTx=0w^T x = 0 is the decision boundary. Geometrically, it is a hyperplane in the input space, dividing it into two sides.

9.7.0.3 Unifying the probabilistic structure

Combining the 2 probabiliy equations, p(y|x,w)=11+exp⁡(−ywTx)p(y|x,w) = \frac{1}{1 + \exp(-y w^T x)} We can check that:

9.7.1 MLE for Logistic Regression

Given training data (xi,yi)(x_i, y_i) for i=1,…,ni = 1,\ldots,n and assuming the training examples are independent, the MLE objective is to find ŵ=arg⁡maxw∈ℝd∑i=1nlog⁡(p(yi|xi,w))=arg⁡maxw∈ℝd∑i=1nlog⁡(11+exp⁡(−yiwTxi))=arg⁡maxw∈ℝd∑i=1n−log⁡(1+exp⁡(−yiwTxi))=arg⁡minw∈ℝd∑i=1nlog⁡(1+exp⁡(−yiwTxi))\begin{align*} \hat{w} &= \arg \max_{w \in \mathbb{R}^d} \sum_{i=1}^n \log(p(y_i|x_i,w))\\ &= \arg \max_{w \in \mathbb{R}^d} \sum_{i=1}^n \log \left(\frac{1}{1 + \exp(-y_i w^T x_i)}\right)\\ &= \arg \max_{w \in \mathbb{R}^d} \sum_{i=1}^n -\log (1 + \exp(-y_i w^T x_i))\\ &= \arg \min_{w \in \mathbb{R}^d} \sum_{i=1}^n \log (1 + \exp(-y_i w^T x_i)) \end{align*} Intuition behind this logistic loss:

To prevent overfitting, we can add L2L_2 regularisation to obtain the final logistic regression objective: ŵ=arg⁡minw∈ℝd{1n∑i=1nlog(1+exp(−yiwTxi))+λ||w||2}\hat{w} = \arg \min_{w \in \mathbb{R}^d} \left\{ \frac{1}{n} \sum_{i=1}^n \log(1 + \exp(-y_i w^T x_i)) + \lambda||w||^2 \right\} Probabilistic framework: Logistic regression uses Bernoulli via sigmoid, instead of Gaussian in linear regression.
Closed-form solution: Unlike linear/Ridge regression, logistic regression does not have a closed-form analytical solution. The logistic loss is not quadratic in ww, so we can’t simply set the gradient to zero and solve algebraically. Instead, iterative numerical methods such as gradient descent must be used to find ŵ\hat{w}. However, the objective is convex, which guarantees that iterative methods will converge to the global minimum.