Fundamentals / Numerical mathematics / Inverse problems and least squares

Weighted least squares and residual diagnostics

Derive the fitting criterion from measurement error and separate the uncertainty of an estimated mean from the scatter of a new observation.

From first principles

Derive the least-squares distance from the instrument noise

Suppose \(y=Ax+\epsilon\), with Gaussian measurement noise of zero mean and known positive definite covariance \(\Sigma\). Independent errors of unequal sizes should not count equally in raw units, and correlated errors should not be counted twice. Factor \(\Sigma=LL^T\) and multiply the model by \(L^{-1}\). The transformed noise has unit covariance,

\[b=L^{-1}y,\]
\[B=L^{-1}A,\]
\[\operatorname{Cov}(L^{-1}\epsilon)=I .\]

For a candidate state, the negative log likelihood differs by a constant from half the squared norm of the transformed residual. This follows from the Gaussian density,

\[\tfrac12(y-Ax)^T\Sigma^{-1}(y-Ax)=\tfrac12\|b-Bx\|_2^2 .\]

The likeliest state therefore minimizes ordinary squared residuals in these whitened coordinates. Solves with the triangular factor \(L\) preserve this interpretation without forming its inverse. If the covariance is estimated or state dependent, the stated probability model and resulting uncertainty estimates need to include that extra information.

Background · Whitened residual
\[r=\Sigma^{-1/2}(y-G(m))\]

For a known positive covariance this expresses mismatch in noise units and removes the modelled correlations between measurements.

Derivation and applications

Construct a model check that requires no fitted parameters

A residual can be compared with noise most cleanly when the particular combination of readings was forced to vanish by the model itself. Consider \(y=Ax+\epsilon\) with zero-mean noise of known positive definite covariance \(\Sigma\). Choose a nonzero contrast \(c\) satisfying \(A^Tc=0\). No value of the unknown \(x\) can change the predicted contrast because its mean is

\[\operatorname{E}(c^Ty)=c^TAx=0,\]
\[\operatorname{var}(c^Ty)=c^T\Sigma c .\]

Under a Gaussian noise model its standardized value is a unit normal random variable. Squaring it gives a one-degree-of-freedom discrepancy,

\[Z=\frac{c^Ty}{\sqrt{c^T\Sigma c}},\qquad Z^2\sim\chi^2_1 .\]

The corresponding fit interpretation follows by whitening \(b=L^{-1}y,\ B=L^{-1}A\). The vector \(u=L^Tc/\sqrt{c^T\Sigma c}\) has unit norm and is perpendicular to every column of \(B\). Thus every fitted residual has projection \(Z\) along the same direction,

\[u^T(b-B\widehat x)=u^Tb=Z,\qquad \|b-B\widehat x\|^2\ge Z^2 .\]

When there is exactly one independent residual direction, equality holds for the least-squares fit. The contrast is a model check only to the extent that the covariance and zero-mean error assumption are justified. It identifies a combination of inconsistent readings, without uniquely locating a faulty sensor.

Worked example

Check an affine trend from three measurements

At times \(0,1,2\) a linear trend predicts zero second difference. Observations \(2,3,6\), each with independent standard deviation \(0.20\), give contrast \(2-2(3)+6=2\). Its standard deviation is \(0.20\sqrt6=0.490\), hence \(Z=4.08\) and \(Z^2=16.7\). Fitting the intercept and slope cannot remove that discrepancy.

Second application

Include a shared calibration mode in a conservation test

Three independent channels should satisfy a balance with contrast \(c=(1,1,-1)^T\). Suppose their independent errors each have deviation \(0.10\) and a common additive offset has independent deviation \(0.20\). Since \(c^T(1,1,1)^T=1\), the offset contributes \(0.20^2\) to contrast variance. For readings \(1.0,2.0,2.6\) the discrepancy is \(0.4/\sqrt{0.03+0.04}=1.51\) standard deviations. Dropping the shared offset would inflate it to \(2.31\).

OneSimulation laboratory

Find out what a temperature series identifies

Start from this chapter. Increase the noise scale while keeping temperatures and sampling times fixed. Predict how the covariance of the activation energy changes before refitting the same model.

Compare recorded OneSimulation runs on this page. To test your own inputs, download the notebook and model files and run the model in an activated OneSimulation environment.

Fit \(k_r\) and \(E\) simultaneously to five simulated concentration histories with a fixed noise seed. The data-generating values are \(k_r=0.001\) s⁻¹ and \(E=115\) kJ/mol. Keep the same starting guess. Predict which change most increases the standard error of \(E\): doubling noise, or moving the five temperatures closer to their common reference of 323.15 K.

OneSimulation least-squares fits. The last column is the local standard error of the fitted activation energy.
Temperature range and noise (mol/m³)Fitted E (kJ/mol)Standard error (kJ/mol)
313.15–333.15 K · noise 0.05114.03670.3636
313.15–333.15 K · noise 0.10113.06350.7228
321.15–325.15 K · noise 0.05111.14891.4300
Narrowing the temperature range raises the fitted energy’s standard error from 0.364 to 1.430 kJ/mol. With these runs that costs more precision than doubling the sensor noise.
Narrowing the temperature range raises the fitted energy’s standard error from 0.364 to 1.430 kJ/mol. With these runs that costs more precision than doubling the sensor noise.

In the wide case the five temperatures are spaced by 5 K. In the narrow case they are spaced by 1 K. Use the same 12 sampling times per experiment. Try a different random seed after checking the table. Compare the fitted error bar with the scatter of estimates across several seeds. Increasing the number of repeats probes noise; changing the temperature range also changes parameter identifiability. Inspect the model, residuals and fitting procedure.

Download experiment files (ZIP) Extract the files together and open arrhenius_parameter_estimation.ipynb in a local OneSimulation environment.

Change parameters · Change the laboratory inputs

Change one input at a time and compare the recalculated curves with the argument above. The browser evaluates this small model locally; the accompanying numerical values state its assumptions and reference checks. Reset restores the reference inputs, and the data can be downloaded as CSV.

Problem sheet 3 · Choose between repeating and spreading measurements

Read the model setup and preparatory problems

A distance in data space depends on the instrument

Let \(y=Ax+\epsilon\) with zero-mean error of known positive-definite covariance \(\Sigma\). If the errors are jointly Gaussian, the negative log likelihood differs from \((y-Ax)^\mathsf{T}\Sigma^{-1}(y-Ax)/2\) only by constants independent of \(x\). This expression still defines a weighted least-squares criterion without normality. Correlated readings can vary together substantially while their difference is very precise; individually dividing them by standard deviations fails to capture that information.

Factor \(\Sigma=LL^\mathsf{T}\) and solve \(L b=y,\ L B=A\) by triangular substitution. Then \(b=Bx+\eta\) with \(\operatorname{Cov}\eta=I\); \(\|b-Bx\|^2\) is the original weighted distance. This is whitening. It also cures a units problem: residuals measured in different units are compared after division by their uncertainty scales. For truly independent readings of standard deviations \(s_i\), the transformation simply divides each row and response by \(s_i\).

Derive the projection and compute it without losing precision

Differentiating the squared norm gives

\[B^\mathsf{T}(b-B\hat x)=0.\]

Thus \(B\hat x\) is the orthogonal projection of \(b\) onto the column space. For full column rank write a thin QR factorisation \(B=QR\) with \(Q^\mathsf{T}Q=I\). The minimiser solves \(R\hat x=Q^\mathsf{T}b\). Forming the normal matrix \(B^\mathsf{T}B\) instead squares its two-norm condition number. QR avoids this extra source of precision loss, though it cannot create information absent from the measurements.

Suppose two instruments measure one constant as 10 and 14 with independent standard deviations 1 and 2. The weighted mean is \((10+14/4)/(1+1/4)=10.8\), with standard deviation \((1+1/4)^{-1/2}=0.894\). The less precise instrument still contributes, but with one quarter of the weight. Giving it half the weight would confuse standard deviation with variance. If both instruments share an unknown calibration bias, the stated independence model overstates what averaging can achieve.

Background · Why can two whitening transformations give the same fitted curve?

A symmetric inverse square root and a triangular Cholesky solve generally give different coordinates for the whitened residual. Let both transformations \(W_1,W_2\) whiten the same positive definite covariance \(\Sigma\). Then the change of coordinates \(Q=W_2W_1^{-1}\) obeys

\[QQ^T=W_2\Sigma W_2^T=I.\]

Whitened residual vectors are therefore related by an orthogonal transformation. Their squared lengths and hence their least-squares minimisers agree. Two individual whitened components can nevertheless have different magnitudes. If a diagnostic needs to identify which physical sensor was faulty, interpret its residual in the original measurement coordinates alongside the correlation model. A large component obtained after rotation cannot by itself identify a physical channel.

Work with residuals after fitting

The fitted whitened response is \(Hb\) with \(H=QQ^\mathsf{T}\), and residual \(r=(I-H)b\) has covariance \(I-H\). Consequently raw whitened residuals after fitting do not have equal independent unit variance. Observation \(i\) has leverage \(H_{ii}\); a high-leverage point can pull the fit toward itself and show a deceptively small residual. Under the stated model, \(\mathbb E\|r\|^2=m-p\) for \(m\) observations and \(p\) independent fitted coefficients. With Gaussian errors the sum has a chi-square distribution with those degrees of freedom. Plot signed residuals against input, time and sensor identity to look for omitted curvature, drift or serial correlation.

Uncertainty in a fitted trend and a future measurement

In the full-rank linear model \(\operatorname{Cov}\hat x=(B^\mathsf{T}B)^{-1}\). For the model mean \(c^\mathsf{T}\hat x\) the variance is \(c^\mathsf{T}\operatorname{Cov}(\hat x)c\); solve \(R^\mathsf{T}v=c\) to obtain it as \(\|v\|^2\). Predicting a future individual reading requires adding its independent measurement variance. This distinction matters when validating fitted response curves. A single point can legitimately lie far outside a narrow interval for the mean. When only relative weights are known, estimate a common variance scale from the residual degrees of freedom and state the additional assumption.

Separate common and independent errors before estimating a mean

Measurements made after one calibration often share a random offset. Write \(y_i=\mu+b+e_i\), where the common offset has mean zero and variance \(\tau^2\), and the individual errors are independent of one another and of \(b\), with variances \(s_i^2\). The covariance matrix is the diagonal matrix of individual variances plus the rank-one matrix \(\tau^2{\bf1}{\bf1}^T\). Consider any linear estimate \(w^Ty\) that reproduces an arbitrary constant, so that \(\sum_iw_i=1\). Its variance separates into \(\tau^2+\sum_iw_i^2s_i^2\). The first term survives every admissible choice of weights because the shared error is indistinguishable from the unknown mean within this set of readings.

Minimising the second term with a multiplier for the sum constraint gives

\[2s_i^2w_i=\lambda.\]

Normalisation yields weights proportional to the individual precisions, with variance \(\tau^2+1/\sum_i s_i^{-2}\). In particular, estimating the weights from the marginal variances \(s_i^2+\tau^2\) as if every reading were independent gives both an incorrect covariance and generally incorrect relative weights. This derivation uses only second moments. Gaussian assumptions enter if the standard deviation is subsequently turned into a normal-theory probability interval.

Decide whether to repeat a reading or recalibrate

A balance has a random calibration uncertainty of 0.30 g. The first reading is 12.40 g with individual standard deviation 0.10 g. The second, acquired during a shorter averaging window, is 11.90 g with individual standard deviation 0.20 g. The individual precisions are 100 and 25 per g², so the relative weights are 0.8 and 0.2. The estimate is 12.30 g. Its variance is \(0.09+1/125=0.098\) g², giving standard deviation 0.313 g. A long sequence of additional readings using this same calibration can only push that standard deviation toward 0.30 g.

The difference between these two readings removes both \(\mu\) and the shared offset. Its standard deviation is

\[\sqrt{0.10^2+0.20^2}=0.224\,\mathrm{g}.\]

The observed difference of 0.50 g is about 2.24 such standard deviations. That discrepancy is relevant to the model for individual readings and averaging windows. It supplies no direct evidence for or against a common calibration offset of a particular size, because that offset has cancelled algebraically.

What if the entire two-reading procedure is repeated after an independent calibration? Averaging the two independently calibrated group estimates gives variance \(0.098/2=0.049\) g² and standard deviation 0.221 g. Taking the same number of readings with one calibration instead gives variance \(0.09+1/250=0.094\) g². The choice of sampling protocol changes the uncertainty even when the balance, total reading count and individual variances are identical. If calibrations share an uncertain reference standard, independence between groups must itself be revised.

A separate reference mass weighed during each calibration provides another way to estimate a shared offset. Subtracting that reference reading removes the offset but also introduces the reference measurement error into every corrected value. The corrected readings are therefore correlated again, now through the shared reference. Their covariance should be recomputed from this modified measurement protocol.

Find a measurement floor that averaging cannot remove

Each reading has independent noise of variance one and the same additional zero-mean calibration offset. Repeating the measurement divides the independent variance by the number of readings. The calibration error is shared by every term in their average.

Find a measurement floor that averaging cannot remove. Horizontal axis: Number of readings. Vertical axis: Standard deviation of sample mean. Data follow the figure.
With common standard deviation 0.3, averaging one hundred readings with independent sensor noise would still leave total standard deviation about 0.316. Fitting a diagonal covariance and extrapolating a 1/√N law misses this limiting uncertainty.
Read values from the plot
Standard deviation of sample mean at selected values of Number of readings.
Number of readingsCommon calibration σ = 0Common calibration σ = 0.3Common calibration σ = 1
111.0441.4142
260.196120.358421.019
510.140030.331071.0098
750.115470.321461.0066
1000.10.316231.005

Download all 100 plotted samples (CSV)

Change parameters · Find a measurement floor that averaging cannot remove

Change one input at a time and compare the recalculated curves with the argument above. The browser evaluates this small model locally; the accompanying numerical values state its assumptions and reference checks. Reset restores the reference inputs, and the data can be downloaded as CSV.

Study the full derivation

Choose a regularisation scale by predicting omitted data · Inferring parameters and fields from observations

Regularised least squares can fit each observation partly with itself. Derive leave-one-out residuals from a single factorisation, approximate them by a trace and identify when a generalised cross-validation score misses a highly influential sensor.