The CODATA least-squares adjustment: , Birge ratio, normalized residual
DefinitionCODATA2022_least_squaresThe objects of the CODATA least-squares adjustment. For a design matrix of linearized observational equations in adjusted constants, input data and a weight matrix (the inverse covariance matrix of the data), the statistic ; the degrees of freedom (as an integer); the Birge ratio ; and the normalized residual of an input datum with adjusted value and standard uncertainty .
import Mathlib
/-!
# The CODATA least-squares adjustment (LSA)
The adjustment fits `M` adjusted constants `x` to `N` input data `z` through linear
observational equations `z ≐ A x`, weighting the residuals by a symmetric positive definite
weight matrix `W` (the inverse of the covariance matrix of the input data).
Notation follows Mohr, Newell, Taylor, Tiesinga, Rev. Mod. Phys. **97**, 025002 (2025):
Sec. XIV.A and the Nomenclature entries for `ν = N − M`, `R_B = (χ²/ν)^{1/2}` and
`r_i = (X_i − ⟨X_i⟩)/u(X_i)`.
-/
noncomputable section
namespace CODATA2022
open Matrix
/-- The statistic `χ² = (z − A x)ᵀ W (z − A x)` of a linear least-squares adjustment with
design matrix `A`, weight matrix `W`, input data `z` and parameter vector `x`. -/
def chiSquare {N M : ℕ} (A : Matrix (Fin N) (Fin M) ℝ) (W : Matrix (Fin N) (Fin N) ℝ)
(z : Fin N → ℝ) (x : Fin M → ℝ) : ℝ :=
(z - A.mulVec x) ⬝ᵥ W.mulVec (z - A.mulVec x)
/-- Degrees of freedom `ν = N − M` of an adjustment with `N` input data and `M` adjusted
constants, as an integer (so that no truncation occurs when `M > N`). -/
def degreesOfFreedom (N M : ℕ) : ℤ := (N : ℤ) - (M : ℤ)
/-- Birge ratio `R_B = (χ²/ν)^{1/2}`. -/
def birgeRatio (chiSq nu : ℝ) : ℝ := Real.sqrt (chiSq / nu)
/-- Normalized residual `r = (X − ⟨X⟩)/u(X)` of an input datum `X` with adjusted value
`Xadj` and standard uncertainty `u`. -/
def normalizedResidual (X Xadj u : ℝ) : ℝ := (X - Xadj) / u
end CODATA2022
end
Read-back
What the Lean code literally says, in plain math · Aristotle by Harmonic (same agent as the drafter; non-blind)
Disclosure - non-blind read-back. This read-back was written by the same agent that drafted the Lean statement it describes, not by an independent auditor with a fresh context. It is therefore not independent testimony: the writer already knew what the code was intended to say, which is exactly the bias that blind read-backs exist to remove. A reviewer should treat it as the drafter's own restatement of the code and, where independence matters, obtain a genuinely blind read-back before relying on it.
This file introduces four declarations about a linear fitting problem, with no hypotheses attached to any of them.
The first takes two natural numbers , (implicit), a matrix of real entries with rows indexed by and columns by , a square real matrix of size , and two vectors , . It returns the real number
the ordinary dot product of the residual vector with its image under . No symmetry, positivity or invertibility is required of , and or may be , in which case the empty dot product is .
The second takes two natural numbers , and returns the integer , computed after casting both to , so it is negative when rather than truncated to .
The third takes two real numbers, called here and , and returns using the real square root that returns on negative arguments; with the quotient is and the value is , and if the value is .
The fourth takes three real numbers , , and returns ; when this is .
None of the four declarations asserts anything; they are definitions, and the names , Birge ratio, degrees of freedom and normalized residual are labels chosen in the docstrings.