Advanced Octave A22 — Weighted least squares and regularization

by Giux GX-TXT, octave-en, scripts-en 2 min read

Advanced GNU Octave with GX-TXT · Module 04

When observations have different uncertainties or the problem is poorly conditioned, the criterion of adaptation must express the weights and any additional hypotheses. In this example we compare small squares weighed and a square penalization.

Objectives and prerequisites

The introductory course introduced linear systems; A20 and A21 discussed conditioning and rank. We will use a synthetic system, with didactic weights and without real measurements, in GX-TXT dev112-P011-bis and GNU Octave 9.4.0.

Weights and increased system

Import regolarizzazione.m, record it in the Session and run it in the Runner.

The script defines three equations and two parameters. sigma = [0,1; 0,2; 0,2] quantities are synthetic scales; W = diag(1./ sigma) multiplies each residue by the reverse of its scale. The problem without penalty minimizes norm(W*(A*x-b)).

For the regularized version, the program adds to the sqrt(lambda)*eye(2) row system and known zero terms. The solution therefore minimizes the sum of the square of the weighed residue and lambda*norm(x)^2. The lambda = 10⁻⁴ value is a didactic choice, not a universally correct threshold.

Both rectangular systems are solved via NUMERIC_API_v1/linear_matrix with solve action, using the containers of real and imaginary parts provided by the API. The printed residue is calculated on the three original equations and weighed with W.

In the test the coefficients are about [1, 1] in both cases to the accuracy displayed. The weighed residue passes from about 2,2 × 10⁻¹⁵ to 8,2 × 10⁻⁶. In this specific example, we do not see an appreciable variation of the coefficients to the precision shown; however, the penalization introduces a different criterion.

Interpretation

Uncertainty-based weights require an appropriate error model: if errors are related, a simple diagonal matrix may not be enough. Penalization favors solutions with smaller norm and can reduce sensitivity, but introduces bias. The choice of lambda should be based on the scale of parameters, noise and the objective of the analysis; it is not enough to choose the value that gives the lesser residual on the data used for the fit.

Exercise

  1. Why does W contain 1/sigma and not directly sigma in this criterion?
  2. What add sqrt(lambda)*eye(2) rows to the problem?
  3. Does the test show that the coefficients are identical to infinite precision?

Worked solution

  1. Because the residual is divided by its scale before calculating the square norm.
  2. A penalty proportional to lambda*norm(x)^2.
  3. No: they only match the print accuracy shown.

Download the Module 04 scripts (ZIP)

#GNU Octave #GX-TXT #GX-TXT public APIs

Other steps of this project

Leave a Reply

Your email address will not be published. Required fields are marked *