Euler Hall — free maths guides

Write the normal equations for a data-fitting least-squares problem

Build A transpose A for a line fit from m and the sums of t and t squared. The idea in plain words, a worked example, and a check with the answers shown — part of the free Linear Algebra guides.

Plain words first

You have the design matrixAA and the height vectorb\mathbf b. The normal equations are ATA x^=ATb.A^{T}A\,\hat{\mathbf x}=A^{T}\mathbf b.

For a line fit,AA has two columns — the ones and the times — so you never need to write the matrix out. Just take the four dot products:

So the system is

Read it as two facts:

  • - *ATAA^{T}A is made of dot products of columns with columns**, so it is symmetric — the off-diagonal entries are the same number twice.
  • - *ATbA^{T}b is made of dot products of columns with the heights**, so its first entry is a plain sum (because the first column is all ones) and its second weights every height by its own time.

Five numbers plus the count, and the setup is complete. Then — and only then — solve.

Why these five numbers and not others

BecauseAA has only two columns, and every entry ofATAA^{T}A is one dot product of two columns.

Write the columns out: column 1 is1=(1,…,1)\mathbf 1=(1,\dots,1) and column 2 ist=(t1,…,tm)t=(t_1,\dots,t_m). Then:

  • - entry(1,1)(1,1) is1⋅1=1+⋯+1=m\mathbf 1\cdot\mathbf 1=1+\dots+1=m;
  • - entry(1,2)(1,2) is1⋅t=t1+⋯+tm=∑ti\mathbf 1\cdot t=t_1+\dots+t_m=\sum t_i;
  • - entry(2,1)(2,1) ist⋅1t\cdot\mathbf 1, the same sum, which is why the matrix is symmetric;
  • - entry(2,2)(2,2) ist⋅t=t12+⋯+tm2=∑ti2t\cdot t=t_1^{2}+\dots+t_m^{2}=\sum t_i^{2}.

And for the right-hand side:

  • - entry 1 is1⋅b=∑yi\mathbf 1\cdot \mathbf b=\sum y_i;
  • - entry 2 ist⋅b=∑tiyit\cdot\mathbf b=\sum t_i y_i.

Nothing else can appear, because there are only two columns. This is the concrete form of the general rule "each entry ofATAA^{T}A is a dot product of two columns ofAA" — and it is why a parabola fit gives a3×33\times3 system: three columns, so nine dot products, and the powers climb tot4t^{4}.

When the system is diagonal. If∑ti=0\sum t_i=0 the two columns are orthogonal,ATAA^{T}A is diagonal, andCC andDD decouple:C=∑yimC=\frac{\sum y_i}{m} andD=∑tiyi∑ti2D=\frac{\sum t_iy_i}{\sum t_i^{2}} separately. Shifting the times to have mean zero is therefore not a trick but a way of removing the coupling.

Worked example, set up and checked

Fity=C+Dty=C+Dt to(1,2)(1,2),(2,3)(2,3),(4,7)(4,7).

Step 1, the five numbers.m=3m=3;∑ti=1+2+4=7\sum t_i=1+2+4=7;∑ti2=1+4+16=21\sum t_i^{2}=1+4+16=21;∑yi=2+3+7=12\sum y_i=2+3+7=12;∑tiyi=2+6+28=36\sum t_iy_i=2+6+28=36.

Step 2, assemble. [37721][CD]=[1236].\begin{bmatrix}3&7\\7&21\end{bmatrix}\begin{bmatrix}C\\D\end{bmatrix}=\begin{bmatrix}12\\36\end{bmatrix}.

Step 3, the cheap checks. Top-left ism=3m=3 ✓. The matrix is symmetric ✓. The right-hand side has one plain sum and one weighted sum ✓.

Step 4, solve (only because the fit is asked for here).det⁡=63−49=14\det=63-49=14, so C=12⋅21−7⋅3614=252−25214=0,D=3⋅36−7⋅1214=108−8414=2414=127.C=\frac{12\cdot21-7\cdot36}{14}=\frac{252-252}{14}=0,\qquad D=\frac{3\cdot36-7\cdot12}{14}=\frac{108-84}{14}=\frac{24}{14}=\frac{12}{7}.

The best line isy=127ty=\frac{12}{7}t.

Step 5, the check that proves it.Ax^=(127,247,487)A\hat{x}=\left(\frac{12}{7},\frac{24}{7},\frac{48}{7}\right) ande=(27,−37,17)e=\left(\frac{2}{7},-\frac{3}{7},\frac{1}{7}\right). Then ATe=[2−3+172−6+47]=[00] ✓.\def\arraystretch{1.6}A^{T}e=\begin{bmatrix}\frac{2-3+1}{7}\\\frac{2-6+4}{7}\end{bmatrix}=\begin{bmatrix}0\\0\end{bmatrix}\ ✓.

Notice thatC=0C=0 here — the best line passes through the origin. That happens with these numbers rather than by design, and it is worth not "correcting" it: the data asked for it.

By the end you can
  • Build A transpose A for a line fit from m and the sums of t and t squared
  • Build A transpose b from the sum of y and the sum of t y
  • Check the result: the top-left entry is m, the matrix is symmetric, and the right-hand side has one plain and one weighted sum
  • Say when the two equations decouple and why that happens
Test yourself without looking back

Points(2,1)(2,1),(3,2)(3,2),(5,6)(5,6).

  1. 1. Write the normal equations.
  2. 2. What is the top-left entry, and why must it be that?
  3. 3. Without solving, isATAA^{T}A diagonal here? How do you know at a glance?
Answers

1.m=3m=3,∑t=10\sum t=10,∑t2=4+9+25=38\sum t^{2}=4+9+25=38,∑y=9\sum y=9,∑ty=2+6+30=38\sum ty=2+6+30=38: [3101038][CD]=[938].\begin{bmatrix}3&10\\10&38\end{bmatrix}\begin{bmatrix}C\\D\end{bmatrix}=\begin{bmatrix}9\\38\end{bmatrix}. 2.33, the number of data points. It is1⋅1\mathbf 1\cdot\mathbf 1, and the ones column has one entry per row. 3. No — the off-diagonal is10≠010\neq0. A diagonalATAA^{T}A needs∑ti=0\sum t_i=0, which cannot happen with three positive times.

Work out where to start — free, no accountAnswer six real questions — free, no account