Advanced Octave A22 — Weighted least squares and regularization
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
- Why does
Wcontain1/sigmaand not directlysigmain this criterion? - What add
sqrt(lambda)*eye(2)rows to the problem? - Does the test show that the coefficients are identical to infinite precision?
Worked solution
- Because the residual is divided by its scale before calculating the square norm.
- A penalty proportional to
lambda*norm(x)^2. - No: they only match the print accuracy shown.
Other steps of this project
- Advanced Octave A75 — Dynamical systems in phase space
- Advanced Octave A81 — Geography of the waves and a 3D surface
- Advanced Octave A82 — Age, sex and demographic inequalities
- Advanced Octave A83 — All-cause mortality, certified COVID deaths and surveillance
- Advanced Octave A84 — A reproducible public account
- Advanced Octave A73 — Optimization landscapes and 3D trajectories