Unit 4, lesson 21

Weighted Least Squares

How a positive definite weight matrix gives a weighted inner product, weighted orthogonality of residuals, the weighted normal equations, why the fit is unique and when its coefficients are, and inner products from sample values.

  • Video 21
  • Projection and Best Approximation

Approximate the point b=(1,0)b = (1, 0) by a multiple t(1,1)t(1, 1). Ordinary distance picks t=12t = \tfrac12. If an error in the first coordinate should cost twice as much as an error in the second, the best multiple changes, although neither the target nor the line has moved. Weighted least squares replaces the dot product by a weighted inner product and applies the projection theorem there, which leads to the weighted normal equations and to a clear line between a unique fitted vector and coefficients that may not be unique. Throughout, F\mathbb{F} is R\mathbb{R} or C\mathbb{C}, A∗A^{*} is the conjugate transpose, inner products are conjugate-linear in the first argument, and a tuple such as (1,i)(1, i) stands for a column vector.

Key terms

Positive definite weight matrix

Definition A matrix W∈Fm×mW \in \mathbb{F}^{m \times m} is a positive definite weight matrix if it is Hermitian, W∗=WW^{*} = W, and

x∗Wx>0for every nonzero x∈Fm.\begin{gathered} x^{*}Wx > 0 \\[0.4em] \text{for every nonzero } x \in \mathbb{F}^m. \end{gathered}

Over R\mathbb{R} this means that WW is real symmetric with xTWx>0x^{\mathsf{T}}Wx > 0 for x≠0x \ne 0. A diagonal matrix qualifies exactly when its diagonal entries are positive real numbers. Such a WW is invertible, because Wx=0Wx = 0 gives x∗Wx=0x^{*}Wx = 0 and hence x=0x = 0. A Hermitian matrix with x∗Wx≤0x^{*}Wx \le 0 for some nonzero xx, such as a singular positive semidefinite matrix or an indefinite one, is not a weight in this sense. Multiplying WW by a constant c>0c > 0 multiplies every weighted squared error by cc, so it does not change which fit is best.

In plain words The weight matrix declares how much each error vector costs, and positive definiteness says that every nonzero error costs something.

Example The Hermitian matrix WW with rows (2,i)(2, i) and (−i,2)(-i, 2) is positive definite. Expanding ∣x1+ix2∣2\lvert x_1 + ix_2 \rvert^2 shows

x∗Wx=∣x1∣2+∣x2∣2+∣x1+ix2∣2,x^{*}Wx = \lvert x_1 \rvert^2 + \lvert x_2 \rvert^2 + \lvert x_1 + ix_2 \rvert^2,

which is positive unless x=0x = 0. At x=(1,i)x = (1, i) the value is 1+1+0=21 + 1 + 0 = 2. The real symmetric matrix with rows (1,2)(1, 2) and (2,1)(2, 1) is not a weight, since x=(1,−1)x = (1, -1) gives xTWx=−2x^{\mathsf{T}}Wx = -2. Fitting the target 00 by multiples t(1,−1)t(1, -1) with this matrix gives the would-be squared error −2t2-2t^2, which decreases without bound, so no best fit exists. The matrix diag⁡(1,0)\operatorname{diag}(1, 0) is not a weight either, since it gives the nonzero vector (0,1)(0, 1) zero cost.

Weighted inner product

Definition Let W∈Fm×mW \in \mathbb{F}^{m \times m} be a positive definite weight matrix. The weighted inner product on Fm\mathbb{F}^m and its norm are

⟨x,y⟩W=x∗Wy,∥x∥W=x∗Wx.\begin{gathered} \langle x, y \rangle_W = x^{*}Wy, \\[0.4em] \lVert x \rVert_W = \sqrt{x^{*}Wx}. \end{gathered}

It is linear in yy, conjugate symmetric because y∗Wx‾=x∗W∗y=x∗Wy\overline{y^{*}Wx} = x^{*}W^{*}y = x^{*}Wy, and positive definite because WW is, so it is an inner product. Conversely, every inner product on Fm\mathbb{F}^m has this form, with WW the Gram matrix Wij=⟨ei,ej⟩W_{ij} = \langle e_i, e_j \rangle of the standard basis. For a diagonal weight W=diag⁡(w1,…,wm)W = \operatorname{diag}(w_1, \dots, w_m) the squared length is

∥r∥W2=w1∣r1∣2+⋯+wm∣rm∣2.\lVert r \rVert_W^2 = w_1 \lvert r_1 \rvert^2 + \cdots + w_m \lvert r_m \rvert^2.

In plain words The weighted inner product measures lengths and angles with the costs built in. In the real plane its unit circle is an ellipse.

Example On R2\mathbb{R}^2 with W=diag⁡(2,1)W = \operatorname{diag}(2, 1), the vector (1,0)(1, 0) has weighted length 2\sqrt{2}, the same as (0,2)(0, \sqrt{2}), and the unit circle becomes the ellipse 2x12+x22=12x_1^2 + x_2^2 = 1. The vectors (1,1)(1, 1) and (1,−2)(1, -2) are orthogonal in this inner product, since 2⋅1⋅1+1⋅(−2)=02 \cdot 1 \cdot 1 + 1 \cdot (-2) = 0, although their dot product is −1-1.

Weighted orthogonality

Definition Let W∈Fm×mW \in \mathbb{F}^{m \times m} be a positive definite weight matrix. Vectors x,y∈Fmx, y \in \mathbb{F}^m are WW-orthogonal if ⟨x,y⟩W=x∗Wy=0\langle x, y \rangle_W = x^{*}Wy = 0. For A∈Fm×nA \in \mathbb{F}^{m \times n}, b∈Fmb \in \mathbb{F}^m and a coefficient vector x∈Fnx \in \mathbb{F}^n, the residual r=b−Axr = b - Ax is WW-orthogonal to the column space of AA exactly when

A∗W(b−Ax)=0.A^{*}W(b - Ax) = 0.

Indeed ⟨Az,r⟩W=z∗A∗Wr\langle Az, r \rangle_W = z^{*}A^{*}Wr for every z∈Fnz \in \mathbb{F}^n. If A∗Wr=0A^{*}Wr = 0, this is zero for every zz. If it is zero for every zz, the choice z=A∗Wrz = A^{*}Wr gives the ordinary squared norm ∥A∗Wr∥2=0\lVert A^{*}Wr \rVert^2 = 0, so A∗Wr=0A^{*}Wr = 0. By the projection theorem in the inner product ⟨⋅,⋅⟩W\langle \cdot, \cdot \rangle_W, the vector AxAx is the point of the column space closest to bb in the weighted norm exactly when this condition holds.

In plain words Perpendicular now means perpendicular for the weighted inner product. A residual can look slanted in an ordinary drawing and still be orthogonal in the weighted geometry.

Example Fit b=(1,0)b = (1, 0) by t(1,1)t(1, 1) with W=diag⁡(2,1)W = \operatorname{diag}(2, 1). At t=23t = \tfrac23 the residual is r=(13,−23)r = (\tfrac13, -\tfrac23), and

⟨(1,1),r⟩W=2⋅13−23=0,\langle (1, 1), r \rangle_W = 2 \cdot \tfrac13 - \tfrac23 = 0,

so rr is WW-orthogonal to the line, even though its dot product with (1,1)(1, 1) is −13-\tfrac13. The ordinary fit t=12t = \tfrac12 has residual (12,−12)(\tfrac12, -\tfrac12), which is orthogonal for the dot product but has weighted inner product 12\tfrac12 with (1,1)(1, 1). Its weighted squared error is 34\tfrac34, larger than the 23\tfrac23 of the weighted fit.

Weighted normal equations

Definition Let A∈Fm×nA \in \mathbb{F}^{m \times n} and b∈Fmb \in \mathbb{F}^m, and let W∈Fm×mW \in \mathbb{F}^{m \times m} be a positive definite weight matrix. A coefficient vector x^∈Fn\hat{x} \in \mathbb{F}^n minimizes the weighted squared error (b−Ax)∗W(b−Ax)(b - Ax)^{*}W(b - Ax) exactly when it solves the weighted normal equations

A∗WA x^=A∗Wb.A^{*}WA\,\hat{x} = A^{*}Wb.

They are the orthogonality condition of the previous card, rearranged. They always have a solution, because the projection theorem supplies a closest point pp of the column space, and every x^\hat{x} with Ax^=pA\hat{x} = p solves them. The n×nn \times n matrix A∗WAA^{*}WA is Hermitian with z∗A∗WAz=∥Az∥W2≥0z^{*}A^{*}WAz = \lVert Az \rVert_W^2 \ge 0. It is positive definite, hence invertible, exactly when the columns of AA are linearly independent, and then

x^=(A∗WA)−1A∗Wb.\hat{x} = (A^{*}WA)^{-1}A^{*}Wb.

With W=IW = I these are the ordinary normal equations, and over R\mathbb{R} they read ATWA x^=ATWbA^{\mathsf{T}}WA\,\hat{x} = A^{\mathsf{T}}Wb. The formula describes the answer. Forming and inverting A∗WAA^{*}WA is not always the most accurate way to compute it.

In plain words Each column gives one equation, which says that the residual is orthogonal to that column in the weighted inner product. Solving them gives the coefficients of the best fit.

Example Fit a line c0+c1tc_0 + c_1 t to the data points (0,1)(0, 1), (1,0)(1, 0) and (2,3)(2, 3) with weights 33, 33 and 11, so that the last point counts a third as much as each of the others. Here W=diag⁡(3,3,1)W = \operatorname{diag}(3, 3, 1) and

A=[101112],b=[103],A = \begin{bmatrix} 1 & 0 \\ 1 & 1 \\ 1 & 2 \end{bmatrix}, \qquad b = \begin{bmatrix} 1 \\ 0 \\ 3 \end{bmatrix},

so the weighted normal equations are

[7557][c0c1]=[66],\begin{bmatrix} 7 & 5 \\ 5 & 7 \end{bmatrix} \begin{bmatrix} c_0 \\ c_1 \end{bmatrix} = \begin{bmatrix} 6 \\ 6 \end{bmatrix},

with solution c0=c1=12c_0 = c_1 = \tfrac12. The fitted values are (12,1,32)(\tfrac12, 1, \tfrac32), and the residual r=(12,−1,32)r = (\tfrac12, -1, \tfrac32) satisfies ATWr=0A^{\mathsf{T}}Wr = 0, since 3⋅12−3+32=03 \cdot \tfrac12 - 3 + \tfrac32 = 0 and −3+2⋅32=0-3 + 2 \cdot \tfrac32 = 0. The unweighted fit is 13+t\tfrac13 + t. Giving the third point less weight produces a flatter line that stays closer to the first two points.

Uniqueness of the fitted vector

Definition In the setting of the weighted normal equations, the fitted vector p=Ax^p = A\hat{x} is the same for every minimizer x^\hat{x}. It is the orthogonal projection of bb onto the column space of AA in the inner product ⟨⋅,⋅⟩W\langle \cdot, \cdot \rangle_W, and it depends only on bb, WW and that column space. If x^0\hat{x}_0 is one minimizer, the set of all minimizers is

x^0+ker⁡A={x^0+z∣Az=0}.\hat{x}_0 + \ker A = \{ \hat{x}_0 + z \mid Az = 0 \}.

Two minimizers give the same unique closest point, so their difference lies in ker⁡A\ker A, and adding a vector of ker⁡A\ker A does not change Ax^A\hat{x}. The coefficient vector is therefore unique exactly when ker⁡A={0}\ker A = \{0\}, that is, when the columns of AA are linearly independent.

In plain words The best fitted output is always unique. Its recipe in terms of the columns is unique only when no column is a combination of the others.

Example Take both columns of AA equal to (1,1)(1, 1), with b=(1,0)b = (1, 0) and W=diag⁡(2,1)W = \operatorname{diag}(2, 1). The weighted normal equations

[3333][x1x2]=[22]\begin{bmatrix} 3 & 3 \\ 3 & 3 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} = \begin{bmatrix} 2 \\ 2 \end{bmatrix}

say only that x1+x2=23x_1 + x_2 = \tfrac23. The coefficient vectors (23,0)(\tfrac23, 0), (0,23)(0, \tfrac23) and (13,13)(\tfrac13, \tfrac13) all solve them. They differ by multiples of (1,−1)(1, -1), which spans ker⁡A\ker A, and each one gives the same fitted vector (23,23)(\tfrac23, \tfrac23).

Inner product from sample values

Definition Let t1,…,tNt_1, \dots, t_N be distinct real nodes and w1,…,wNw_1, \dots, w_N positive weights. On Pd\mathcal{P}_d, the polynomials of degree at most dd with coefficients in F\mathbb{F}, set

⟨p,q⟩=∑k=1Nwk p(tk)‾ q(tk).\langle p, q \rangle = \sum_{k=1}^{N} w_k\,\overline{p(t_k)}\,q(t_k).

This is linear in qq and conjugate symmetric. It is positive definite, and so an inner product, exactly when N≥d+1N \ge d + 1. If N≥d+1N \ge d + 1, then ⟨p,p⟩=∑kwk∣p(tk)∣2=0\langle p, p \rangle = \sum_k w_k \lvert p(t_k) \rvert^2 = 0 forces p(tk)=0p(t_k) = 0 at d+1d + 1 or more points, so p=0p = 0. If N≤dN \le d, the nonzero polynomial (t−t1)⋯(t−tN)(t - t_1) \cdots (t - t_N) lies in Pd\mathcal{P}_d and has norm 00, so the formula gives only a seminorm. With the sample vector s(p)=(p(t1),…,p(tN))s(p) = (p(t_1), \dots, p(t_N)) and W=diag⁡(w1,…,wN)W = \operatorname{diag}(w_1, \dots, w_N) the formula reads ⟨p,q⟩=s(p)∗Ws(q)\langle p, q \rangle = s(p)^{*}W s(q). Projecting pp onto a subspace U=span⁡{u1,…,un}U = \operatorname{span}\{u_1, \dots, u_n\} in this inner product is therefore weighted least squares, with b=s(p)b = s(p) and with the columns s(u1),…,s(un)s(u_1), \dots, s(u_n) forming AA.

In plain words Two polynomials are compared only through their weighted values at the chosen nodes. There must be enough nodes to tell any two different polynomials apart.

Example The line fit in the normal equations card is a projection of this kind. On P2\mathcal{P}_2 with nodes 0,1,20, 1, 2 and weights 3,3,13, 3, 1, the data (1,0,3)(1, 0, 3) are the samples of p(t)=2t2−3t+1p(t) = 2t^2 - 3t + 1, and the projection of pp onto P1\mathcal{P}_1 is 12+12t\tfrac12 + \tfrac12 t. With only the nodes −1-1 and 11 and weights 11, the formula is an inner product on P1\mathcal{P}_1 but not on P2\mathcal{P}_2, because t2−1t^2 - 1 vanishes at both nodes. The polynomials t2t^2 and 11 then have the same samples (1,1)(1, 1). A fit from P2\mathcal{P}_2 to these samples has a unique fitted sample vector, but every polynomial 1+c(t2−1)1 + c(t^2 - 1) produces it.

Check yourself

  1. Fit b=(1,0)b = (1, 0) by t(1,1)t(1, 1) with the weight W=diag⁡(1,3)W = \operatorname{diag}(1, 3). Find tt, the residual and the weighted squared error, and check that the residual is WW-orthogonal to (1,1)(1, 1).
Show the answer

The normal equation is the scalar equation (1+3) t=1(1 + 3)\,t = 1, so t=14t = \tfrac14. The fitted point is (14,14)(\tfrac14, \tfrac14) and the residual is (34,−14)(\tfrac34, -\tfrac14). Its weighted inner product with (1,1)(1, 1) is 34+3⋅(−14)=0\tfrac34 + 3 \cdot (-\tfrac14) = 0, and the weighted squared error is

916+3⋅116=34.\tfrac{9}{16} + 3 \cdot \tfrac{1}{16} = \tfrac{3}{4}.

The heavy weight now sits on the second coordinate, so the fit moves toward the second coordinate of bb, which is 00.

  1. Is ⟨p,q⟩=p(0) q(0)+3 p(2) q(2)\langle p, q \rangle = p(0)\,q(0) + 3\,p(2)\,q(2) an inner product on the real polynomials P1\mathcal{P}_1? On P2\mathcal{P}_2?
Show the answer

On P1\mathcal{P}_1 it is. The formula is symmetric and bilinear, and a polynomial of degree at most 11 that vanishes at the two distinct nodes 00 and 22 is the zero polynomial, so ⟨p,p⟩>0\langle p, p \rangle > 0 for p≠0p \ne 0. On P2\mathcal{P}_2 it is not, because t2−2t=t(t−2)t^2 - 2t = t(t - 2) is nonzero and has ⟨p,p⟩=0\langle p, p \rangle = 0. Positive weights alone are not enough. The number of distinct nodes must be at least d+1d + 1.

  1. Let W=diag⁡(1,1,2)W = \operatorname{diag}(1, 1, 2) and b=(1,2,4)b = (1, 2, 4), and let AA have columns (1,0,1)(1, 0, 1), (0,1,0)(0, 1, 0) and (1,1,1)(1, 1, 1). Find all minimizers of the weighted squared error, the fitted vector and the residual, and check weighted orthogonality.
Show the answer

The third column is the sum of the first two, so the columns are dependent and ker⁡A\ker A is spanned by (1,1,−1)(1, 1, -1). The weighted normal equations are

[303011314]x^=[9211].\begin{bmatrix} 3 & 0 & 3 \\ 0 & 1 & 1 \\ 3 & 1 & 4 \end{bmatrix} \hat{x} = \begin{bmatrix} 9 \\ 2 \\ 11 \end{bmatrix}.

The third equation is the sum of the first two, which say x^1+x^3=3\hat{x}_1 + \hat{x}_3 = 3 and x^2+x^3=2\hat{x}_2 + \hat{x}_3 = 2. So the minimizers are x^=(3,2,0)+s(−1,−1,1)\hat{x} = (3, 2, 0) + s(-1, -1, 1) for every scalar ss, and all of them give the fitted vector p=(3,2,3)p = (3, 2, 3). The residual r=(−2,0,1)r = (-2, 0, 1) satisfies

⟨(1,0,1),r⟩W=−2+2⋅1=0,⟨(0,1,0),r⟩W=0,\begin{aligned} \langle (1, 0, 1), r \rangle_W &= -2 + 2 \cdot 1 = 0, \\[0.4em] \langle (0, 1, 0), r \rangle_W &= 0, \end{aligned}

so rr is also WW-orthogonal to the third column, which is the sum of the first two. The weighted squared error is 4+0+2=64 + 0 + 2 = 6. The ordinary dot product of rr with (1,0,1)(1, 0, 1) is −1-1, so the residual is orthogonal only in the weighted sense.

How this lesson connects

It also uses least squares and the normal equations from a first linear algebra course.