Linear Regression Introduction
Linear Regression
Notation and Problem Setup
Supervised Learning Recap
In supervised learning, the algorithm is given a training set of input-output pairs (x,y). The goal is to learn a hypothesis function h that maps inputs x to outputs y, generalizing to new, unseen inputs. In a regression problem, the output y is a continuous value (e.g., house price). This contrasts with classification, where y is discrete (e.g., category).
The simplest regression algorithm is linear regression, which models h as a linear (affine) function of the inputs.
Hypothesis Representation
For a single input feature x (e.g., house size), the hypothesis is:
where θ0 is the intercept and θ1 is the slope. The parameters θ=[θ0,θ1]T are chosen to fit the data.
For multiple features (e.g., size and number of bedrooms), let x1,x2,…,xn be the input features. The hypothesis becomes:
To simplify notation, define an extra dummy feature x0=1. Then:
Here θ and x are (n+1)-dimensional vectors: θ=[θ0,θ1,…,θn]T, x=[x0,x1,…,xn]T.
Notation
m: number of training examples.
n: number of features (excluding x0).
x(i): input features of the i-th training example.
y(i): output (target) value of the i-th training example.
θ: parameters (weights) of the model.
hθ(x): hypothesis (predicted output) for input x.
Key terms: Parameters θ are what the learning algorithm tunes. The hypothesis hθ(x) is the function that maps features to predictions.
Cost Function
The goal is to choose θ so that hθ(x) is close to y for all training examples. The cost function (also called the loss function) measures the error. For linear regression, we use the mean squared error (MSE) with a factor of 21 for convenience:
This is also known as ordinary least squares. The 21 simplifies derivatives later. Minimizing J(θ) yields the optimal θ.
Why squared error? It corresponds to assuming Gaussian noise in the data, as will be justified in the context of generalized linear models.
Gradient Descent
Gradient descent is an iterative optimization algorithm for finding θ that minimizes J(θ). The idea: start with an initial guess for θ (e.g., all zeros) and repeatedly update θ in the direction that reduces J(θ) the fastest.
Update Rule (for one training example)
The partial derivative of J(θ) with respect to θj (using a single example) is:
This follows from the chain rule. The gradient descent update for each θj is:
where α is the learning rate (step size). For the full training set, the derivative is averaged over all examples.
Batch Gradient Descent
In batch gradient descent, each update uses all m training examples. Repeat until convergence:
This algorithm looks at the entire "batch" of data before making a single step. It converges to the global minimum (for linear regression, J(θ) is convex and has no local optima). However, when m is very large (e.g., millions), each iteration is slow.
Stochastic Gradient Descent
Stochastic gradient descent (SGD) updates θ using only one training example at a time. For each i from 1 to m:
SGD takes a noisy, random path toward the optimum. It never converges exactly but oscillates around the minimum. For large datasets, SGD is much faster because a single pass through the data already provides many updates. In practice, the learning rate α is often decreased over time to reduce oscillations.
Mini-batch gradient descent, a compromise, uses a small batch of b examples (e.g., b=100) at each step.
Learning Rate Selection
The learning rate α controls step size.
Too large: may overshoot and cause J(θ) to increase.
Too small: slow convergence.
Common practice: try values on an exponential scale (e.g., 0.01, 0.03, 0.1, 0.3) and pick the one that minimizes J(θ) fastest. Feature scaling (scaling inputs to [−1,1]) helps make learning rate selection more robust.
Normal Equations
For linear regression, there exists a closed-form solution that gives the optimal θ directly, without iteration. This is derived by setting the derivative of J(θ) to zero.
Matrix Notation for the Cost Function
Define the design matrix X of size m×(n+1) where row i contains the features of the i-th example:
Let y=[y(1),y(2),…,y(m)]T be the vector of target values. Then:
Matrix Derivatives
To differentiate J(θ) with respect to θ, we use matrix calculus. Key definitions:
For a function f:Rp×q→R, the derivative ∇Af(A) is a matrix of the same size, with entries ∂Aij∂f.
Trace operator: For a square matrix A, tr(A)=∑iAii.
Useful identities:
∇Atr(AB)=BT
tr(ABC)=tr(CAB)=tr(BCA) (cyclic property)
∇Atr(AATC)=CA+CTA
Derivation of Normal Equations
Expanding J(θ):
Taking the gradient with respect to θ and setting to zero:
This yields the normal equations:
Solving for θ gives:
This is the optimal solution in one step (provided XTX is invertible). If XTX is singular, it usually indicates redundant (linearly dependent) features. In that case, one can use the pseudo-inverse or remove redundant features.
Key advantage: No need to choose a learning rate or iterate; one matrix computation yields the global minimum.
Gradient descent (especially stochastic) and the normal equations are foundational techniques for fitting linear models. Linear regression is the simplest supervised learning algorithm, but the concepts of hypothesis, cost function, and optimization carry over to more advanced algorithms discussed later.

Ask anything
We've read this whole study set, so questions about it get the best answers.