5.6.1Machine Learning (Aerospace Applications)

Linear regression — normal equation, gradient descent derivation

2,554 words12 min readdifficulty · medium6 backlinks

Linear regression finds the best-fit line (or hyperplane) through data by minimizing the sum of squared errors. In aerospace, this powers trajectory prediction, sensor calibration, and control system parameter identification.

Why Linear Regression?


Problem Setup

Vectorized form: Stack data into matrix XRn×d\mathbf{X} \in \mathbb{R}^{n \times d} (each row is xiT\mathbf{x}_i^T), targets yRn\mathbf{y} \in \mathbb{R}^{n}. Absorb bias into w\mathbf{w} by adding a column of 1s to X\mathbf{X}. Then:

J(w)=12nyXw2J(\mathbf{w}) = \frac{1}{2n} \|\mathbf{y} - \mathbf{X}\mathbf{w}\|^2

Method 1: Normal Equation (Closed-Form Solution)

Derivation from First Principles

Goal: Find w\mathbf{w} that minimizes J(w)J(\mathbf{w}) using calculus.

Step 1: Expand the cost function.

J(w)=12n(yXw)T(yXw)J(\mathbf{w}) = \frac{1}{2n} (\mathbf{y} - \mathbf{X}\mathbf{w})^T (\mathbf{y} - \mathbf{X}\mathbf{w})

Why this step? Converting the norm into a dot product so we can apply matrix derivative rules.

Expand:

=12n(yTyyTXwwTXTy+wTXTXw)= \frac{1}{2n} (\mathbf{y}^T\mathbf{y} - \mathbf{y}^T\mathbf{X}\mathbf{w} - \mathbf{w}^T\mathbf{X}^T\mathbf{y} + \mathbf{w}^T\mathbf{X}^T\mathbf{X}\mathbf{w})

Since yTXw\mathbf{y}^T\mathbf{X}\mathbf{w} is a scalar, it equals its transpose: yTXw=wTXTy\mathbf{y}^T\mathbf{X}\mathbf{w} = \mathbf{w}^T\mathbf{X}^T\mathbf{y}.

=12n(yTy2wTXTy+wTXTXw)= \frac{1}{2n} (\mathbf{y}^T\mathbf{y} - 2\mathbf{w}^T\mathbf{X}^T\mathbf{y} + \mathbf{w}^T\mathbf{X}^T\mathbf{X}\mathbf{w})

Step 2: Take the derivative with respect to w\mathbf{w} and set to zero.

Jw=12n(2XTy+2XTXw)=1n(XTXwXTy)\frac{\partial J}{\partial \mathbf{w}} = \frac{1}{2n} (-2\mathbf{X}^T\mathbf{y} + 2\mathbf{X}^T\mathbf{X}\mathbf{w}) = \frac{1}{n}(\mathbf{X}^T\mathbf{X}\mathbf{w} - \mathbf{X}^T\mathbf{y})

Why this step? At the minimum, the gradient is zero (no slope in any direction). We use:

  • w(aTw)=a\frac{\partial}{\partial \mathbf{w}} (\mathbf{a}^T\mathbf{w}) = \mathbf{a}
  • w(wTAw)=2Aw\frac{\partial}{\partial \mathbf{w}} (\mathbf{w}^T\mathbf{A}\mathbf{w}) = 2\mathbf{A}\mathbf{w} (for symmetric A\mathbf{A})

Set gradient to zero:

XTXw=XTy\mathbf{X}^T\mathbf{X}\mathbf{w} = \mathbf{X}^T\mathbf{y}

Step 3: Solve for w\mathbf{w}.

Geometric intuition: Xw\mathbf{X}\mathbf{w}^* is the orthogonal projection of y\mathbf{y} onto the column space of X\mathbf{X}. The error vector yXw\mathbf{y} - \mathbf{X}\mathbf{w}^* is perpendicular to all columns of X\mathbf{X}, hence XT(yXw)=0\mathbf{X}^T(\mathbf{y} - \mathbf{X}\mathbf{w}^*) = 0.

Figure — Linear regression — normal equation, gradient descent derivation

When to Use Normal Equation

Pros:

  • One-shot solution (no iteration)
  • Exact (up to numerical precision)

Cons:

  • Computing (XTX)1(\mathbf{X}^T\mathbf{X})^{-1} is O(d3)O(d^3) where dd is feature count
  • Fails if XTX\mathbf{X}^T\mathbf{X} is singular (multicollinearity)
  • Impractical for d>10,000d > 10{,}000 (common in aerospace sensor fusion)

Method 2: Gradient Descent (Iterative Optimization)

Derivation from First Principles

Core idea: Start with a random w\mathbf{w}, repeatedly step in the direction that decreases JJ the fastest (negative gradient).

Step 1: Compute the gradient of JJ with respect to w\mathbf{w}.

From earlier:

wJ=1nXT(Xwy)\nabla_{\mathbf{w}} J = \frac{1}{n} \mathbf{X}^T (\mathbf{X}\mathbf{w} - \mathbf{y})

Why this form? Xwy\mathbf{X}\mathbf{w} - \mathbf{y} is the error vector (predictions minus actuals). Multiplying by XT\mathbf{X}^T weights each feature by how much it contributed to the error.

Step 2: Update rule.

Why this step? We move against the gradient (downhill). α\alpha controls how big the step is—too large and we overshoot, too small and convergence is slow.

Step 3: Repeat until convergence.

Convergence criteria:

  • wJ<ϵ\|\nabla_{\mathbf{w}} J\| < \epsilon (gradient nearly zero)
  • Jt+1Jt<ϵ|J_{t+1} - J_t| < \epsilon (cost stops decreasing)
  • Maximum iterations reached

Variants


Worked Examples

Add bias column: X=[111213]\mathbf{X} = \begin{bmatrix} 1 & 1 \\ 1 & 2 \\ 1 & 3 \end{bmatrix}, y=[245]\mathbf{y} = \begin{bmatrix} 2 \\ 4 \\ 5 \end{bmatrix}

XTX=[36614]\mathbf{X}^T\mathbf{X} = \begin{bmatrix} 3 & 6 \\ 6 & 14 \end{bmatrix}, XTy=[1125]\mathbf{X}^T\mathbf{y} = \begin{bmatrix} 11 \\ 25 \end{bmatrix}

Why this step? Matrix multiplication: (3×2)T(3×2)=(2×2)(3 \times 2)^T (3 \times 2) = (2 \times 2). The first entry of XTy\mathbf{X}^T\mathbf{y} is 12+14+15=111\cdot2 + 1\cdot4 + 1\cdot5 = 11; the second is 12+24+35=2+8+15=251\cdot2 + 2\cdot4 + 3\cdot5 = 2 + 8 + 15 = 25.

Solve [36614][bw]=[1125]\begin{bmatrix} 3 & 6 \\ 6 & 14 \end{bmatrix} \begin{bmatrix} b \\ w \end{bmatrix} = \begin{bmatrix} 11 \\ 25 \end{bmatrix}:

Inverse: (XTX)1=131436[14663]=16[14663](\mathbf{X}^T\mathbf{X})^{-1} = \frac{1}{3 \cdot 14 - 36} \begin{bmatrix} 14 & -6 \\ -6 & 3 \end{bmatrix} = \frac{1}{6} \begin{bmatrix} 14 & -6 \\ -6 & 3 \end{bmatrix}

w=16[14663][1125]=16[15415066+75]=16[49]=[0.6671.5]\mathbf{w}^* = \frac{1}{6} \begin{bmatrix} 14 & -6 \\ -6 & 3 \end{bmatrix} \begin{bmatrix} 11 \\ 25 \end{bmatrix} = \frac{1}{6} \begin{bmatrix} 154 - 150 \\ -66 + 75 \end{bmatrix} = \frac{1}{6}\begin{bmatrix} 4 \\ 9 \end{bmatrix} = \begin{bmatrix} 0.667 \\ 1.5 \end{bmatrix}

Result: b=0.667b = 0.667, w=1.5w = 1.5. So y^=1.5x+0.667\hat{y} = 1.5x + 0.667.

Why this matters: Aerospace sensor calibration often involves fitting lines to reference measurements.


Iteration 0:

  • Predictions: y^=X[00]=[000]\hat{\mathbf{y}} = \mathbf{X} \begin{bmatrix} 0 \\ 0 \end{bmatrix} = \begin{bmatrix} 0 \\ 0 \\ 0 \end{bmatrix}
  • Error: Xwy=[245]\mathbf{X}\mathbf{w} - \mathbf{y} = \begin{bmatrix} -2 \\ -4 \\ -5 \end{bmatrix}
  • Gradient: J=13[111123][245]=13[1125]=[3.6678.333]\nabla J = \frac{1}{3} \begin{bmatrix} 1 & 1 & 1 \\ 1 & 2 & 3 \end{bmatrix} \begin{bmatrix} -2 \\ -4 \\ -5 \end{bmatrix} = \frac{1}{3} \begin{bmatrix} -11 \\ -25 \end{bmatrix} = \begin{bmatrix} -3.667 \\ -8.333 \end{bmatrix}
  • Why this step? XT\mathbf{X}^T has shape (2×3)(2 \times 3), error is (3×1)(3 \times 1), result is (2×1)(2 \times 1). Second component: 1(2)+2(4)+3(5)=2815=251\cdot(-2) + 2\cdot(-4) + 3\cdot(-5) = -2 - 8 - 15 = -25.
  • Update: w1=[00]0.1[3.6678.333]=[0.3670.833]\mathbf{w}_1 = \begin{bmatrix} 0 \\ 0 \end{bmatrix} - 0.1 \begin{bmatrix} -3.667 \\ -8.333 \end{bmatrix} = \begin{bmatrix} 0.367 \\ 0.833 \end{bmatrix}

Iteration 1:

  • y^=Xw1=[0.367+0.8330.367+1.6670.367+2.500]=[1.2002.0332.867]\hat{\mathbf{y}} = \mathbf{X}\mathbf{w}_1 = \begin{bmatrix} 0.367 + 0.833 \\ 0.367 + 1.667 \\ 0.367 + 2.500 \end{bmatrix} = \begin{bmatrix} 1.200 \\ 2.033 \\ 2.867 \end{bmatrix}
  • Error: [0.8001.9672.133]\begin{bmatrix} -0.800 \\ -1.967 \\ -2.133 \end{bmatrix}
  • Gradient: 13[4.90011.133]=[1.6333.711]\frac{1}{3}\begin{bmatrix} -4.900 \\ -11.133 \end{bmatrix} = \begin{bmatrix} -1.633 \\ -3.711 \end{bmatrix}
  • Update: w2=[0.5301.204]\mathbf{w}_2 = \begin{bmatrix} 0.530 \\ 1.204 \end{bmatrix}

Continue until convergence → approaches [0.667,1.5]T[0.667, 1.5]^T.

Why gradient descent? When d=10,000d = 10{,}000 (e.g., image features for spacecraft pose estimation), computing the inverse is infeasible, but each gradient step is fast.


Common Mistakes


Active Recall

Recall Explain to a 12-Year-Old

Imagine you're trying to draw the best straight line through a bunch of dots on graph paper. "Best" means the line is as close to all the dots as possible. You measure "closeness" by drawing vertical lines from each dot to your line and adding up the lengths of those lines (squared so big mistakes count more).

The normal equation is like using a ruler and protractor to calculate the exact perfect angle and position in one go. You do some matrix math and boom—done.

Gradient descent is like starting with a random line, then slowly tilting and sliding it. You look at where the dots are, figure out which way makes things worse, and move the opposite way. You repeat this over and over, each time making a small adjustment, until the line can't get any better. It takes longer but works even if the "ruler method" is too complicated (like if you have a million dots).

Aerospace engineers use this to predict things like how much fuel a plane will burn at different speeds—plot speed vs. fuel, find the best line, then use it to plan flights.


Gradient Descent = "ITERATIVE steps ALPHA tuned"

  • Iterative process
  • Tiny steps (learning rate α\alpha)
  • Efficient for high dimensions

Connections

  • Least Squares Estimation — same MSE criterion, generalized to weighted/nonlinear cases
  • Matrix Pseudoinverse — handles singular XTX\mathbf{X}^T\mathbf{X}
  • Convex Optimization — why gradient descent converges (MSE is convex)
  • Feature Scaling — essential for GD convergence
  • Regularization (Ridge, Lasso) — adds penalty term to prevent overfitting
  • Kalman Filtering — recursive least squares for dynamic systems (spacecraft tracking)
  • Neural Networks — gradient descent generalizes to backpropagation

Summary

  • Normal equation: w=(XTX)1XTy\mathbf{w}^* = (\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}^T\mathbf{y} (one-shot, O(d3)O(d^3), requires invertibility)
  • Gradient descent: wt+1=wtαJ\mathbf{w}_{t+1} = \mathbf{w}_t - \alpha \nabla J (iterative, O(nd)O(nd) per step, scales to large dd)
  • Derived from minimizing 12nyXw2\frac{1}{2n}\|\mathbf{y} - \mathbf{X}\mathbf{w}\|^2 by setting J=0\nabla J = 0 or stepping against J\nabla J
  • Key tradeoffs: exact vs. iterative, memory vs. computation, batch vs. stochastic

#flashcards/coding

What is the normal equation for linear regression? :: w=(XTX)1XTy\mathbf{w}^* = (\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}^T\mathbf{y}

Derive the normal equation from J(w)=12nyXw2J(\mathbf{w}) = \frac{1}{2n}\|\mathbf{y} - \mathbf{X}\mathbf{w}\|^2 :: Expand to 12n(yTy2wTXTy+wTXTXw)\frac{1}{2n}(\mathbf{y}^T\mathbf{y} - 2\mathbf{w}^T\mathbf{X}^T\mathbf{y} + \mathbf{w}^T\mathbf{X}^T\mathbf{X}\mathbf{w}), take derivative Jw=1n(XTXwXTy)\frac{\partial J}{\partial \mathbf{w}} = \frac{1}{n}(\mathbf{X}^T\mathbf{X}\mathbf{w} - \mathbf{X}^T\mathbf{y}), set to zero: XTXw=XTy\mathbf{X}^T\mathbf{X}\mathbf{w} = \mathbf{X}^T\mathbf{y}, solve: w=(XTX)1XTy\mathbf{w}^* = (\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}^T\mathbf{y}

What is the gradient descent update rule for linear regression?
wt+1=wtαnXT(Xwty)\mathbf{w}_{t+1} = \mathbf{w}_t - \frac{\alpha}{n} \mathbf{X}^T (\mathbf{X}\mathbf{w}_t - \mathbf{y})
What is the gradient of MSE cost J(w)=12nyXw2J(\mathbf{w}) = \frac{1}{2n}\|\mathbf{y} - \mathbf{X}\mathbf{w}\|^2?
wJ=1nXT(Xwy)\nabla_{\mathbf{w}} J = \frac{1}{n} \mathbf{X}^T (\mathbf{X}\mathbf{w} - \mathbf{y})
When should you use normal equation vs. gradient descent?
Normal equation: small dd (<10K features), need exact solution, invertible XTX\mathbf{X}^T\mathbf{X}. Gradient descent: large dd, online learning, singular matrix issues.
What is the time complexity of computing the normal equation?
O(d3)O(d^3) for matrix inversion plus O(nd2)O(nd^2) for XTX\mathbf{X}^T\mathbf{X}, dominated by O(d3)O(d^3)
What happens if learning rate α\alpha is too large in gradient descent?
Cost oscillates or diverges (overshoots minimum at each step)
What is stochastic gradient descent (SGD)?
Update weights using one sample at a time: wt+1=wtα(xiTwtyi)xi\mathbf{w}_{t+1} = \mathbf{w}_t - \alpha (\mathbf{x}_i^T \mathbf{w}_t - y_i) \mathbf{x}_i
Why do we square errors in MSE?
(1) Positive and

Concept Map

fit model

minimize

vectorized

set gradient zero

closed form

iterative

solve

invert XtX

step by learning rate

apply to

absorb bias

Data points xi yi

Linear model y-hat = wx + b

MSE cost J w

J = norm y - Xw squared

Gradient dJ/dw = 0

Normal equation

Gradient descent

XtX w = Xt y

Optimal weights w

Aerospace uses: trajectory, fuel burn

Add column of 1s to X

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, linear regression ka basic idea ye hai ki hum apne data points ke through ek aisi best-fit line ya hyperplane khinchna chahte hain jo predictions de sake. Jaise aerospace mein agar altitude se fuel burn rate predict karna ho, toh humein aisi line chahiye jiska total error sabse kam ho. Ab error ko hum square karte hain — kyunki agar positive aur negative errors ko aise hi jodenge toh wo ek dusre ko cancel kar denge, aur squaring se calculus bhi smoothly kaam karta hai (derivatives har jagah exist karti hain). Isi squared error ke sum ko minimize karna hi humara asli goal hai, aur isko hum cost function J(w) kehte hain.

Ab is minimum ko dhoondhne ka ek shaandaar tareeka hai — Normal Equation. Iska logic simple hai: jahan cost function minimum hoti hai, wahan uski gradient (slope) zero hoti hai. Toh hum J(w) ko w ke respect mein differentiate karke zero ke barabar rakhte hain, aur thoda matrix algebra karke seedha ek closed-form formula mil jaata hai: w* = (XᵀX)⁻¹Xᵀy. Iska matlab hum bina kisi iteration ke, ek hi step mein exact answer nikaal lete hain. Geometrically socho toh Xw* actually y ka orthogonal projection hai X ke column space par — yaani error vector baaki sabhi columns ke perpendicular ho jaata hai, jo minimum error ki guarantee deta hai.

Ye cheez isliye important hai kyunki aerospace jaise fields mein chhoti si prediction error bhi lambe flights mein compound hoti jaati hai, isliye humein globally best coefficients chahiye — approximate nahi. Normal equation fast aur exact hai jab features kam hain, lekin dhyaan rakho: agar features bahut zyada ho jaayein toh (XᵀX)⁻¹ nikaalna O(d³) ke saath mehenga pad jaata hai, aur multicollinearity ho toh matrix singular ban jaata hai. Isiliye aage chalke gradient descent jaisi iterative methods kaam aati hain — par unki foundation samajhne ke liye ye normal equation waali intuition pehle clear honi zaroori hai.

Go deeper — visual, from zero

Test yourself — Machine Learning (Aerospace Applications)

Connections