✓ Official Syllabus VerifiedStandards & Rigor ↗
6

Linear Regression & Ordinary Least Squares

🤖 AI Math Unit 6 — Linear Models & OLS Applied STEM  ·  Simple linear regression: y = mx + b  ·  Matrix formulation Y = Xw + ε  ·  Ordinary Least Squares (OLS) closed form  ·  Normal equation w = (XᵀX)⁻¹ Xᵀ y  ·  Residuals, MSE and R² coefficient of determination

Linear regression is the foundational predictive model in data science. We explore both algebraic least-squares minimization and the closed-form matrix Normal Equation used across analytics libraries.

▲ Theorem 6.1 — The Matrix Normal Equation
For a linear model y = Xw where X \in \mathbb{R}^{N times (d+1)} is the design matrix (including a column of ones for intercept), the weight vector \( \mathbf{w} \) that minimizes the sum of squared errors \( \|\mathbf{y} - \mathbf{X}\mathbf{w}\|^2 \) satisfies:
\[ \mathbf{X}^T \mathbf{X} \mathbf{w} = \mathbf{X}^T \mathbf{y} \implies \mathbf{w} = (\mathbf{X}^T \mathbf{X})^{-1} \mathbf{X}^T \mathbf{y} \]
provided \( \mathbf{X}^T \mathbf{X} \) is invertible (non-singular).
● Deep Conceptual Intuition — Column-Space Projection & Normal Equations

Geometric Orthogonality of OLS Residuals: Fitting \(\hat{\mathbf{y}} = X\hat{\mathbf{w}}\) chooses the closest vector in \(\operatorname{Col}(X)\) to target \(\mathbf{y}\). The residual \(\mathbf{e} = \mathbf{y} - X\hat{\mathbf{w}}\) must be orthogonal to every column of \(X\): \(X^T(\mathbf{y} - X\hat{\mathbf{w}}) = \mathbf{0} \implies X^T X\hat{\mathbf{w}} = X^T\mathbf{y}\).

Ordinary Least Squares selects parameters \((w_0, w_1)\) minimizing the vertical sum of squared residuals \(\operatorname{SSE} = \sum_{i=1}^n (y_i - \hat{y}_i)^2\).
⊙ Explorer · Ordinary Least Squares (OLS) Regression Line & Residual MSE
interactive
Mean Squared Error MSE\((w, b)\)MSE = 0.124
Optimal Normal-Eq \((w*, b*)\)\(w* = 1.04, b* = 0.50\)
Coefficient of Determination \(R^{2}\)\(R^{2} = 0.943\)

Vertical amber segments show the residuals \(e_i = y_i -(w \cdot x_i + b)\) for each training point. Minimizing \(\Sigma\) e_\(i^{2}\) yields the dashed ordinary least squares line.

Regression Line and Observations — illustrating key geometric and analytical relationships in Linear Regression & Ordinary Least Squares.
Slope Formula
\( m = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})}{\sum (x_i - \bar{x})^2} = \frac{S_{xy}}{S_{xx}} \)
Intercept
\( b = \bar{y} - m\bar{x} \)
R² Score
\( R^2 = 1 - \frac{\text{SS}_{\text{res}}}{\text{SS}_{\text{tot}}} \in [0, 1] \)
Residual Vector
\( \mathbf{e} = \mathbf{y} - \hat{\mathbf{y}} \perp \text{col}(\mathbf{X}) \)
◆ Why It Works — Matrix Calculus Derivation of the Normal Equation

Expanding the Quadratic Objective: Let \(J(\mathbf{w}) = \|\mathbf{y} - X\mathbf{w}\|_2^2 = \mathbf{y}^T\mathbf{y} - 2\mathbf{w}^T X^T\mathbf{y} + \mathbf{w}^T X^T X\mathbf{w}\). Taking the gradient with respect to \(\mathbf{w}\) yields \(\nabla_{\mathbf{w}} J = -2X^T\mathbf{y} + 2X^T X\mathbf{w} = \mathbf{0}\). When \(X\) has full column rank, \(X^T X\) is symmetric positive definite and \(\hat{\mathbf{w}} = (X^T X)^{-1}X^T\mathbf{y}\).

Worked Example 6.A — Closed-Form Matrix Normal Equation
Problem. Fit a line \( y = w_1 x + w_0 \) to three points: \( (1, 2) \), \( (2, 3) \), and \( (3, 5) \) using the Normal Equation.
▸ Show step-by-step solution


1. Set up design matrix \( \mathbf{X} \) (with bias column) and target \( \mathbf{y} \):
\[ \mathbf{X} = \begin{pmatrix} 1 & 1 \\ 2 & 1 \\ 3 & 1 \end{pmatrix}, \quad \mathbf{y} = \begin{pmatrix} 2 \\ 3 \\ 5 \end{pmatrix} \]
2. Compute \( \mathbf{X}^T \mathbf{X} \):
\[ \mathbf{X}^T \mathbf{X} = \begin{pmatrix} 1 & 2 & 3 \\ 1 & 1 & 1 \end{pmatrix} \begin{pmatrix} 1 & 1 \\ 2 & 1 \\ 3 & 1 \end{pmatrix} = \begin{pmatrix} 1+4+9 & 1+2+3 \\ 1+2+3 & 1+1+1 \end{pmatrix} = \begin{pmatrix} 14 & 6 \\ 6 & 3 \end{pmatrix} \]
3. Invert \( \mathbf{X}^T \mathbf{X} \):
\( \det = (14)(3) - (6)(6) = 42 - 36 = 6 \).
\[ (\mathbf{X}^T \mathbf{X})^{-1} = \frac{1}{6} \begin{pmatrix} 3 & -6 \\ -6 & 14 \end{pmatrix} \]
4. Compute \( \mathbf{X}^T \mathbf{y} \):
\[ \mathbf{X}^T \mathbf{y} = \begin{pmatrix} 1 & 2 & 3 \\ 1 & 1 & 1 \end{pmatrix} \begin{pmatrix} 2 \\ 3 \\ 5 \end{pmatrix} = \begin{pmatrix} 2 + 6 + 15 \\ 2 + 3 + 5 \end{pmatrix} = \begin{pmatrix} 23 \\ 10 \end{pmatrix} \]
5. Compute \( \mathbf{w} = (\mathbf{X}^T \mathbf{X})^{-1} (\mathbf{X}^T \mathbf{y}) \):
\[ \mathbf{w} = \frac{1}{6} \begin{pmatrix} 3(23) - 6(10) \\ -6(23) + 14(10) \end{pmatrix} = \frac{1}{6} \begin{pmatrix} 69 - 60 \\ -138 + 140 \end{pmatrix} = \frac{1}{6} \begin{pmatrix} 9 \\ 2 \end{pmatrix} = \begin{pmatrix} 1.5 \\ 0.333 \end{pmatrix} \]
Line equation: \( \hat{y} = 1.5x + \frac{1}{3} \).
Answer: Slope \( w_1 = 1.5 \), Intercept \( w_0 = \frac{1}{3} \).

Worked Example \(B\) — Linear Regression & Ordinary Least Squares (Hard Application)
Problem. A simple linear regression model has been fitted to a dataset. The model equation is given by \( \hat{y} = ax + b \). The regression line passes through the points \( (2, 10) \) and \( (5, 22) \) in the feature-target space. Determine the value of the intercept \( b \).
▸ Show step-by-step solution

The problem states that the linear regression line \( \hat{y} = ax + b \) passes through two points: \( (x_1, y_1) = (2, 10) \) and \( (x_2, y_2) = (5, 22) \). We can use these two points to form a system of linear equations to solve for the unknown parameters \( a \) (the slope) and \( b \) (the intercept). Substituting the first point \( (2, 10) \) into the equation \( \hat{y} = ax + b \) gives: \( 10 = a(2) + b \) (Equation 1) Substituting the second point \( (5, 22) \) into the equation \( \hat{y} = ax + b \) gives: \( 22 = a(5) + b \) (Equation 2) To find the slope \( a \), we can subtract Equation 1 from Equation 2: \( (22 - 10) = (5a - 2a) + (b - b) \) \( 12 = 3a \) \( a = \frac{12}{3} = 4 \) Now that we have the value of \( a \), we can substitute it back into either Equation 1 or Equation 2 to solve for \( b \). Let's use Equation 1: \( 10 = 4(2) + b \) \( 10 = 8 + b \) \( b = 10 - 8 \) \( b = 2 \) Let's verify with Equation 2: \( 22 = 4(5) + b \) \( 22 = 20 + b \) \( b = 22 - 20 \) \( b = 2 \) Both equations yield \( b = 2 \). Therefore, the intercept \( b \) is 2. Common algebraic traps include incorrectly calculating the slope or making sign errors when substituting values back into the equations. For instance, if one incorrectly calculated the slope as \( \frac{3}{12} \) or made a sign error in \( 10 - 8 \), they might arrive at an incorrect intercept.

Worked Example \(C\) — Linear Regression & Ordinary Least Squares (Easy Application)
Problem. In gradient descent on loss \( f(x) \), parameter \( x_0 = 30 \) is updated with learning rate \( \eta = 0.1 \) and gradient \( \nabla f(x_0) = 20 \). Determine the updated parameter \( x_1 = x_0 - \eta \nabla f(x_0) \).
▸ Show step-by-step solution

Computing \( x_1 = 30 - 0.1(20) = 28 \).

Worked Example \(D\) — Linear Regression & Ordinary Least Squares (Medium Application)
Problem. Consider a simple linear regression model where the predicted value \( \hat{y} \) is related to the independent variable \( x \) by the equation \( \hat{y} = mx + c \). If the line of best fit passes through the points \( (1, 5) \) and \( (3, 11) \), what is the slope \( m \) of this line?
▸ Show step-by-step solution

The slope \( m \) of a line passing through two points \( (x_1, y_1) \) and \( (x_2, y_2) \) is given by the formula \( m = \frac{y_2 - y_1}{x_2 - x_1} \). Given the points \( (1, 5) \) and \( (3, 11) \), we have \( x_1 = 1 \), \( y_1 = 5 \), \( x_2 = 3 \), and \( y_2 = 11 \). Plugging these values into the formula, we get \( m = \frac{11 - 5}{3 - 1} = \frac{6}{2} = 3 \). Therefore, the slope of the line of best fit is 3.

⚠️ Common Exam Traps — Intercept Column, Collinearity & R² Bounds

1. Omitting the Column of Ones in \(X\): Without prepending \(\mathbf{1}_n\) to design matrix \(X\), the hyperplane is forced through the origin \((b_0 = 0)\) and residuals need not sum to zero.
2. Singular \(X^T X\) under Collinearity: If two features are linearly dependent, \(X^T X\) is non-invertible; Ridge regression \((X^T X + \lambda I)^{-1}X^T\mathbf{y}\) restores invertibility for any \(\lambda > 0\).

Error Diagnosis — Common Student Traps vs. Exam-Board Method (Linear Regression & Ordinary Least Squares)
Common Student TrapWhy It Fails MathematicallyCorrect Exam-Board Method
Claiming \(+25\%\) followed by \(-25\%\) returns to \(100\%\)The \(25\%\) reduction applies to the larger base \(1.25 Q_0\), not \(Q_0\).Multiply scale factors: \(1.25 \times 0.75 = 0.9375\) (a \(6.25\%\) net decrease).
Solving \(-3x > 12\) as \(x > -4\)Dividing by a negative number reverses order on the real number line.Flip the inequality symbol whenever multiplying or dividing by a negative: \(x < -4\).
Computing round-trip average speed as \(\frac{v_1 + v_2}{2}\)Time spent at the slower speed is longer than time spent at the faster speed.Use total distance over total time: \(v_{\text{avg}} = \frac{2v_1 v_2}{v_1 + v_2}\).
▲ Exam Pacing & Verification Strategy — Linear Regression & OLS

Centroid & Zero-Sum Residual Checks: Every simple OLS line with an intercept passes strictly through the sample centroid \((\bar{x}, \bar{y})\) and satisfies \(\sum_{i=1}^n e_i = 0\) and \(\sum_{i=1}^n x_i e_i = 0\)—verify both in 5 seconds!

[Practice 1 · Hard] A 2D loss surface is defined as \( L(w_1, w_2) = 4w_1^2 + 7w_2^2 \). Find the gradient vector \( \nabla L \) at the point \( (w_1, w_2) = (2, -1) \).
💡 Hint 1 of 3
Compute partial derivatives: \(\frac{\partial L}{\partial w_1} = 2(4)w_1 = 8(2)\).
💡 Hint 2 of 3
Rewrite and simplify to obtain: \(\frac{\partial L}{\partial w_2} = 2(7)w_2 = 14(-1)\).
💡 Hint 3 of 3
Therefore, \(\nabla L = [16, -14]^T\).
Compute partial derivatives: \( \frac{\partial L}{\partial w_1} = 2(4)w_1 = 8(2) = 16 \); \( \frac{\partial L}{\partial w_2} = 2(7)w_2 = 14(-1) = -14 \). Therefore, \( \nabla L = [16, -14]^T \).
[Practice 2 · Easy] A fully-connected dense layer maps an input \( \mathbf{x} \in \mathbb{R}^{50} \) to \( \mathbf{y} \in \mathbb{R}^{25} \) using \( \mathbf{y} = \mathbf{W}\mathbf{x} + \mathbf{b} \). How many total learnable parameters are in this layer?
💡 Hint 1 of 3
The weight matrix \( \mathbf{W} \) has shape \( 25 \times 50 \), giving \( 1250 \) weights.
💡 Hint 2 of 3
The bias vector \( \mathbf{b} \) has \( 25 \) parameters.
💡 Hint 3 of 3
Substitute the result from the previous step into the governing relation and verify the problem condition to find the final answer.
The weight matrix \( \mathbf{W} \) has shape \( 25 \times 50 \), giving \( 1250 \) weights. The bias vector \( \mathbf{b} \) has \( 25 \) parameters. Total = \( 1250 + 25 = 1275 \).
[Practice 3 · Easy] In a dataset, the prior probability of class \( C_1 \) is \( P(C_1) = 0.10 \). What is the probability of the complement class \( P(\neg C_1) \)?
💡 Hint 1 of 3
By the complement rule of probability: \(P( eg C_1) = 1 - P(C_1)\).
💡 Hint 2 of 3
Substitute and simplify: \(P( eg C_1) = 1 - 0.10\).
💡 Hint 3 of 3
Compute the final value from: \(1 - 0.10\).
By the complement rule of probability: \( P(\neg C_1) = 1 - P(C_1) = 1 - 0.10 = 0.90 \).
[Practice 4 · Medium] Consider a simple linear regression model where a dependent variable \( y \) is predicted from an independent variable \( x \). The model is given by the equation \( y = \beta_0 + \beta_1 x + \epsilon \), where \( \epsilon \) is the error term. Suppose we have estimated the regression line to be \( \hat{y} = 3 + 2x \). If we observe an independent variable value of \( x=4 \), what is the predicted value \( \hat{y} \) for the dependent variable?
💡 Hint 1 of 3
The problem asks for the predicted value \( \hat{y} \) given an independent variable value \( x=4 \) and a linear regression equation \( \hat{y} = 3 + 2x \). We substitute the value of \( x \) into the equation: \( \hat{y} = 3 + 2(4) \).
💡 Hint 2 of 3
Rewrite and simplify to obtain: \( \hat{y} = 3 + 8 \) \( \hat{y} = 11 \).
💡 Hint 3 of 3
Therefore, the predicted value for the dependent variable is 11.
The problem asks for the predicted value \( \hat{y} \) given an independent variable value \( x=4 \) and a linear regression equation \( \hat{y} = 3 + 2x \). We substitute the value of \( x \) into the equation: \( \hat{y} = 3 + 2(4) \) \( \hat{y} = 3 + 8 \) \( \hat{y} = 11 \) Therefore, the predicted value for the dependent variable is 11. Common errors might involve incorrectly multiplying 2 by 4 or incorrectly adding 3 to 8.
[Practice 5 · Hard] In anomaly detection, anomaly prior \( P(A) = 0.10 \), detection sensitivity \( P(+|A) = 0.95 \), and false alarm rate \( P(+|\neg A) = 0.05 \). By Bayes' theorem, what is the posterior probability \( P(A|+) \) that an alarm is a true anomaly?
💡 Hint 1 of 3
By Bayes' Rule: \(P(A|+) = \frac{P(+|A)P(A)}{P(+|A)P(A) + P(+| eg A)P( eg A)}\).
💡 Hint 2 of 3
Substitute and simplify: \(P(A|+) = \frac{0.95 \times 0.10}{0.95(0.10) + 0.05(0.90)}\).
💡 Hint 3 of 3
Compute the final value from: \(\frac{0.0950}{0.1400}\).
By Bayes' Rule: \( P(A|+) = \frac{P(+|A)P(A)}{P(+|A)P(A) + P(+|\neg A)P(\neg A)} = \frac{0.95 \times 0.10}{0.95(0.10) + 0.05(0.90)} = \frac{0.0950}{0.1400} = 0.679 \).
● Master Atlas of the 9 Main Parent Functions & Universal Transformation Law

Every algebraic and elementary transcendental model is built by transforming one of the nine core parent functions via \(g(x) = a\,f(x - h) + k\), where \(|a|\) scales vertically (reflecting across the \(x\)-axis if \(a < 0\)), \(h\) shifts horizontally, and \(k\) shifts vertically:

FamilyTransformed Form \(g(x)\)DomainRange (\(a > 0\))Key Geometric Feature
Constant\(f(x) = c\)\((-\infty, \infty)\)\(\{c\}\)Horizontal line, slope \(0\)
Linear\(a(x - h) + k\)\((-\infty, \infty)\)\((-\infty, \infty)\)Constant slope \(m = a\), pivot \((h, k)\)
Quadratic\(a(x - h)^2 + k\)\((-\infty, \infty)\)\([k, \infty)\)Parabola vertex \((h, k)\), axis \(x = h\)
Cubic\(a(x - h)^3 + k\)\((-\infty, \infty)\)\((-\infty, \infty)\)Inflection center \((h, k)\), S-curve
Absolute Value\(a|x - h| + k\)\((-\infty, \infty)\)\([k, \infty)\)V-corner vertex \((h, k)\), slopes \(\pm a\)
Radical\(a\sqrt{x - h} + k\)\([h, \infty)\)\([k, \infty)\)Endpoint \((h, k)\), half-parabola
Rational\(\dfrac{a}{x - h} + k\)\(x \neq h\)\(y \neq k\)Vertical asymptote \(x = h\), horizontal \(y = k\)
Exponential\(a \cdot 2^{x - h} + k\)\((-\infty, \infty)\)\((k, \infty)\)Horizontal asymptote \(y = k\), anchor \((h, a+k)\)
Logarithmic\(a\ln(x - h) + k\)\((h, \infty)\)\((-\infty, \infty)\)Vertical asymptote \(x = h\), anchor \((h+1, k)\)
⊙ Explorer · Linear Slope-Shift & Parent Functions Lab
interactive
Active Equationf(x) = x
Domain(−∞, ∞)
Range(−∞, ∞)
Vertex / AsymptotesAnchor (0, 0), Slope m = 1
Interceptsy-int: (0, 0) · x-int: (0, 0)

Starts on the linear family \(f(x) = a(x - h) + k\) with slope \(a\) and anchor point \((h, k)\). Drag \((h, k)\) directly on the coordinate plane or switch to any of the 9 parent families.

Linear Regression & Ordinary Least Squares — 6-Mark Structured Problem

6 pts Calculator Active
For Linear Regression & Ordinary Least Squares, examine the quadratic-rate model \(f(x) = 3x^2 - 3x + 1\) on \(x \in [0, 3]\).
  1. (a)2 pts
    Compute \(f(0)\) and \(f(2)\).
    Scoring Guideline & Justification
    • [M1] Substitutes x=0 and x=2 (1 pt)
    • [A1] Obtains 1 and 7 (1 pt)
  2. (b)2 pts
    Find the derivative/rate \(f'(x)\) and critical point \(x_c\).
    Scoring Guideline & Justification
    • [M1] Differentiates to 6x - 3 (1 pt)
    • [A1] Solves x_c = 3/6 (1 pt)
  3. (c)2 pts
    Calculate the average rate of change from \(x = 0\) to \(x = 2\).
    Scoring Guideline & Justification
    • [M1] Applies (f(2) - f(0)) / 2 (1 pt)
    • [A1] Evaluates 3 (1 pt)
Complete Worked Solution
(a) \(f(0)=1, f(2)=7\). (b) \(f'(x)=6x-3 \implies x_c = \frac{3}{6}\). (c) Average rate \(= \frac{6}{2} = 3\).
📝 Chapter Quiz