Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Open In Colab

The Problem with Unweighted Residuals

As with most approximate methods, the moment we start to move away from an exact solution subtle effects start to show up!

Notice we are minimizing the residuals, but there is a subtle problem with the standard least squares formulation. Consider the inconsistent system from the pseudoinverse section:

2c+t=15c−t=10c+2t=14\begin{aligned} 2 c + t &= 15 \\ c - t &= 10 \\ c + 2 t &= 14 \end{aligned}

If we were to multiply the second equation by 10, the mathematical line it represents remains geometrically identical. However, the residuals of the second equation would scale up by an order of magnitude:

2c+t=1510c−10t=100c+2t=14\begin{aligned} 2 c + t &= 15 \\ 10 c - 10 t &= 100 \\ c + 2 t &= 14 \end{aligned}

Because the residuals for the second equation are now 10 times larger for the same geometric distance, the least squares algorithm will prioritize minimizing the error on the second equation, dragging our ‘center of mass’ solution towards it!

What does this remind you of?

Introducing Weights

Jacobi (diagonal) preconditioning!

P−1=[W1000W2000W3]P^{-1} = \begin{bmatrix} W_{1} & 0 & 0 \\ 0 & W_{2} & 0 \\ 0 & 0 & W_{3} \end{bmatrix}

If we define r1,r2,r3r_1, r_2, r_3:

2c+t−15=r1c−t−10=r2c+2t−14=r3\begin{aligned} 2 c + t - 15 &= r_1 \\ c - t - 10 &= r_2 \\ c + 2 t - 14 &= r_3 \end{aligned}

We say the residuals can be weighted, i.e.: the least squares problem becomes,

Minx ∑Wi2ri2\text{Min}_{\mathbf{x}} \ \sum W_i^2 r_i^2

Let’s code it and see how applying this diagonal matrix shifts the least squares solution!

Preconditioner / Weights Matrix:
 [[1 0 0]
 [0 2 0]
 [0 0 1]]

Weighted Least Squares Solution (c, t): [9.33333333 0.33333333]
Unweighted Least Squares Solution (c, t): [8.33333333 1.33333333]
Loading...

Pitfalls and opportunities

We have discovered that the magnitude of certain datapoints changes the best fit result, and awareness of this phenomenon is *critical for engineering applications.

Mitigation of these effects can be accomplished by sophisticated solver methods (beyond the scope of numpy / scipy), careful user guidance, or retroactive examination. However, the best practice are the ones you are already familiar with and centre around the removal of scale from data:

  • Using units

  • Scale transformations (linear vs log-scale)

  • Nondimensionalization

As usual, your engineering intuition and judgement will trump fancy numerics!

Measurement Uncertainty as Weights

A situtation where you will want to exploit weights is when you have redundant measurements and/or a known uncertainty.

If you have multiple measurements, yijy_i^j, at a single point, xix_i, then you can find the mean,

y~i=meanj(yij)\tilde{y}_i = \text{mean}_j (y_i^j)

and its variance,

σi2=mean([yij−y~i]2)\sigma_i^2 = \text{mean}\left([y_i^j - \tilde{y}_i]^2 \right)

and weight by the inverse of the variance, Wi=1σi2W_i = \frac{1}{\sigma_i^2}.

Most package implementations of weighted least squares regression have an option to input the weights, but exact usage varies. Consult the documentation to determine how to use it in practice.