Advanced Octave A34 — Residuals, weights and model diagnostics
Advanced GNU Octave with GX-TXT · Module 06
An adapted curve may seem convincing even when its errors show a systematic structure. The residuals and their scale with respect to the declared uncertainties help to evaluate the fit in addition to the only parameters.
Objectives and data
We repeat the synthetic exponential fit of A32 and the bases of the Course Octave. The signal is 3 exp(−0,5 t) + 0,02 sin(3t), with 21 points from 0 to 5. The sigma = 0,03 + 0,01 t scales are positive didactic values: they are not measured uncertainties and sinusoidal disturbance is deterministic. The test uses GX-TXT dev112-P011-bis and GNU Octave 9.4.0.
Weighted and residual fit
Import residui_pesati.m, register it and run it in the Runner.
The program passes sigma to the nonlinear fit and, after the estimate, calculates residual = y − previsione. Compare the root of the average of the squares of the raw residues with that of the residues divided by sigma:
r = gxtxt_api_call('NUMERIC_API_v1','nonlinear_fit',{t,y,h,[2.5,0.6],struct('sigma',sigma)});
p = r.parameters(:);
residual = y-p(1)*exp(-p(2)*t);
fprintf('Raw residual RMS: %.6f\n',sqrt(mean(residual.^2)));
fprintf('Standardized residual RMS: %.6f\n',sqrt(mean((residual./sigma).^2)));
Here “standardized” simply indicates residuo/sigma, dimensionless. It does not include additional fixes for point lever or for the uncertainty of parameters that may appear in other standardized residual definitions.
In the test the fit converges with parameters about 3,0094 and 0,5014; the rough RMS is about 0,013831 and the one divided by the scales about 0,265343. The two numbers have different meanings and should not be compared as if they were expressed in the same unit. A graph of the residues over time would help to see the sinusoidal structure, which a single RMS can hide.
Interpretation
The choice of sigma influences the relative contribution of observations to fit. Before interpreting weights and uncertainties of the parameters, check that the scales correspond to the error model and that they are positive and compatible with the data. A high R² value or a small residual do not experience the physical validity of the model alone; it also observes patterns, influential points and experimental conditions.
Exercise
- What is the mark of the residue when the observed value is less than prediction?
- Why is
residual/sigmadimensionless if both sizes have the same unit? - Can a small RMS hide a systematic trend over time?
Worked solution
- Negative, with the
osservato − previstoconvention. - The unit to the numberer is divided by the same unit to the denominator.
- Yes. It is a global summary and does not show the order or temporal form of the residues.
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