Stacking statistics: covariance, jackknife covariance and
DefinitionszStackStatisticsThe statistical layer of the stacking analysis of de Graaff et al. (2019), Sect. 3.3. A stack consists of binned one-dimensional profiles, indexed by the sample index and the bin index ; profiles are real-valued and there are bins.
-
Mean profile, Eq. (6). .
-
Covariance estimator, Eq. (3). , the covariance of a single map in the stack.
-
Jackknife covariance, Eq. (5). , where now indexes the jackknife sub-samples and is their mean as in Eq. (6).
-
Chi-squared, Eq. (4). for a covariance matrix and a mean profile .
-
Hartlap-corrected chi-squared, Eq. (7). , the same quadratic form with the Hartlap et al. (2007) correction for the bias of the inverse of an estimated covariance.
Matrix inversion is the nonsingular inverse, which returns the zero matrix on a singular argument; the counts , and are natural numbers and all arithmetic in the correction factors is performed over the reals, so no truncated subtraction occurs.
import Mathlib
namespace SZFilaments
open Finset Matrix
/-- The mean profile over a sample of `N` binned profiles: `ȳ_i = (∑_k y^k_i) / N`. -/
noncomputable def binMean {N n : ℕ} (y : Fin N → Fin n → ℝ) (i : Fin n) : ℝ :=
(∑ k, y k i) / N
/-- The covariance estimator of a stack of `N` binned profiles:
`C_{i,j} = (1/N) ∑_k (y^k_i - ȳ_i)(y^k_j - ȳ_j)`. -/
noncomputable def sampleCovariance {N n : ℕ} (y : Fin N → Fin n → ℝ) :
Matrix (Fin n) (Fin n) ℝ :=
Matrix.of fun i j => (∑ k, (y k i - binMean y i) * (y k j - binMean y j)) / N
/-- The jackknife covariance estimator built from `Nsub` sub-sample profiles:
`C^JK_{i,j} = ((Nsub - 1)/Nsub) ∑_k (y^k_i - ȳ_i)(y^k_j - ȳ_j)`. -/
noncomputable def jackknifeCovariance {Nsub n : ℕ} (y : Fin Nsub → Fin n → ℝ) :
Matrix (Fin n) (Fin n) ℝ :=
Matrix.of fun i j =>
(((Nsub : ℝ) - 1) / (Nsub : ℝ)) * ∑ k, (y k i - binMean y i) * (y k j - binMean y j)
/-- The chi-squared statistic of a mean profile against a covariance matrix:
`χ² = ∑_{i,j} ȳ_i (C⁻¹)_{i,j} ȳ_j`. -/
noncomputable def chiSquared {n : ℕ} (C : Matrix (Fin n) (Fin n) ℝ) (ybar : Fin n → ℝ) : ℝ :=
∑ i, ∑ j, ybar i * C⁻¹ i j * ybar j
/-- The chi-squared statistic with the Hartlap correction factor for the bias of the
inverse covariance: `χ² = ∑_{i,j} ȳ_i [((Nsub - n - 2)/(Nsub - 1)) C⁻¹]_{i,j} ȳ_j`. -/
noncomputable def chiSquaredHartlap {n : ℕ} (Nsub : ℕ) (C : Matrix (Fin n) (Fin n) ℝ)
(ybar : Fin n → ℝ) : ℝ :=
∑ i, ∑ j, ybar i * ((((Nsub : ℝ) - (n : ℝ) - 2) / ((Nsub : ℝ) - 1)) * C⁻¹ i j) * ybar j
end SZFilaments
Read-back
What the Lean code literally says, in plain math · Aristotle (Harmonic)
Disclosure — this read-back is not blind and not independent. It was written by the same agent that drafted the Lean statement it describes, at the explicit instruction of the proposal owner, and no separate auditor saw the code. It is therefore not independent testimony: a reviewer should treat it as the drafting agent's own restatement of its own code, and should not count it as a second, blind reading of the formalization.
This bundle introduces five real-valued definitions indexed by two natural numbers: a sample count and a bin count. Data is a function assigning to each sample index (ranging over a finite type with elements) and each bin index (ranging over a finite type with elements) a real number .
1. Mean. , with cast from a natural number to a real. For the sum is empty and the division is by zero, so .
2. Covariance. The real matrix with entries , using the mean of 1. For this is the zero matrix. Note the divisor is , not .
3. Jackknife covariance. The real matrix with entries , where and are real casts, so no truncated natural subtraction occurs; for the prefactor is and the matrix is zero. Unlike 2 there is no further division by the number of samples.
4. Quadratic form. for an arbitrary real matrix and arbitrary vector . The inverse is the nonsingular matrix inverse: if is singular the inverse is defined to be the zero matrix and the whole expression is . The vector is an arbitrary function on the bin index; nothing ties it to the data of 1.
5. Corrected quadratic form. , with and cast to reals before subtraction, so the factor is negative when and is when (division by zero). Again is arbitrary and the inverse is the nonsingular inverse.
None of the five asserts anything; each is a formula evaluated at arbitrary data.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.