🤖 AI Math Unit 6 — Linear Models & OLSApplied 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 = Xwy=Xw where X \in \mathbb{R}^{N times (d+1)}X∈RN×(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
Live numerical table for Ordinary Least Squares (OLS) Regression Line & Residual MSE
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.
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.
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 Trap
Why It Fails Mathematically
Correct 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}\).
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) \).
[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?
● 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:
⊙ Explorer · Linear Slope-Shift & Parent Functions Lab
interactive
Comparison of parent function \(f(x)\) and transformed function \(g(x) = a\,f(x - h) + k\) across integer inputs.
\(x\)
Parent \(f(x)\)
Transformed \(g(x)\)
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]\).
(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)
(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)
(c)2 pts
Calculate the average rate of change from \(x = 0\) to \(x = 2\).