Formula reference: statistics
This page is generated from the docstrings of
vaft/formula/statistics.py:
22 public functions. The category overview and notation come from the module
docstring; every entry below is what vaft.formula.describe("statistics.<name>") prints.
Back to the formula reference index.
Overview
This module is the single source of truth for the mathematics used to judge how well a reconstruction reproduces its own inputs: residual magnitude, residual bias, residual structure, goodness of fit against declared uncertainties, and the rate at which an iterative solver approaches its answer.
Every function here takes plain arrays or scalars and returns plain floats. Nothing in this module knows what an ODS, an EFIT run, a diagnostic channel or a validation policy is – deciding which physical arrays enter a formula belongs to the OMAS layer, and deciding what value is acceptable belongs to the validation layer. Keeping the mathematics separate means a definition can be tested against an analytic reference without constructing a tokamak.
- Non-finite samples are ignored rather than propagated, so a single dead channel does not erase a whole family’s statistic.
- A statistic that is undefined for the input given – too few samples, zero
variance, a zero denominator – returns
nanrather than raising, because these arise routinely from real diagnostics and every caller aggregates over many channels or slices. - No numpy warning is emitted for an empty or all-non-finite input; the degenerate case is guarded explicitly.
None of these statistics is a pass/fail rule on its own. Each is a diagnostic indicator whose interpretation depends on assumptions – stated per function – that the caller is responsible for defending.
Functions
bias_standard_error— Standard error of the mean of $n$ unit-variance samples, $1/\sqrt{n}$.chi_squared— Chi-square, $\sum_i (r_i/\sigma_i)^2$ over the finite terms.dynamic_range— Sample range, $\max - \min$ over the finite entries.fractional_rms_improvement— Fractional RMS improvement, $1 - \mathrm{RMS}(r)/\mathrm{RMS}(r_0)$.lag1_autocorrelation— Lag-1 autocorrelation of the finite entries, in the order given.linear_trend— Least-squares slope of $y$ against $x$, in units of $y$ per unit $x$.log10_decay_rate— Least-squares slope of $\log_{10}x$ against iteration index over the tail.median_absolute_deviation—Median absolute deviation, $\mathrm{median}( x - \mathrm{median}(x) )$. monotonic_fraction— Fraction of consecutive steps in which the sequence decreased.noise_band— Mean and population standard deviation of a reference (effect-free) sample.normalized_residual— Residuals in units of the fitted uncertainty, $z = r\,w/k$.outlier_fraction—Fraction of finite entries with $ x_i > \mathrm{level}$. pearson_correlation— Pearson correlation of two series over their pairwise-finite samples.percentile_scale— A high percentile of|x|over the finite entries, an amplitude scale one anomalous sample cannot set.reduced_chi_squared— Reduced chi-square, $\chi^2/\nu$ per degree of freedom.relative_spread—Relative spread, $(\max - \min)/\max x $ over the finite entries. residual_bias— Mean of the finite entries, the systematic offset of a residual sample.rms— Root mean square, $\sqrt{\mathrm{mean}(x_i^2)}$, over the finite entries.robust_z_scores— Deviation from the median in robust sigma units, sample by sample.runs_test_z— Wald-Wolfowitz runs-test z-score for the sign sequence of a sample.sigma_threshold_crossing— First time outside the reference window where the signal leaves its noise band.sigma_unit_factor— Recover the units-of-fit factor $k$ from a fit’s own per-channel chi-square.
bias_standard_error
bias_standard_error(count)
Standard error of the mean of $n$ unit-variance samples, $1/\sqrt{n}$.
\[\sigma_{\bar x} = \frac{1}{\sqrt{n}}\]| Parameter | Type | Unit | Description |
|---|---|---|---|
count | int | - | Number of samples. |
| Returns | Type | Unit | Description |
|---|---|---|---|
| float | - | Standard error; |
Physical interpretation.
The standard deviation of the sample mean when each sample has unit
variance, which is the case for correctly normalized residuals by
construction. It is the yardstick a measured residual_bias is held
against.
Assumptions.
Unit-variance, independent samples. Do not use this for a raw residual in physical units – there the sample standard deviation must be estimated from the data instead. Correlation between channels makes the true standard error larger than this value.
Notes.
A bias exceeding roughly twice this is unlikely to be a fluctuation, and so
points at a systematic. Because it shrinks only as 1/sqrt(n), a family
with few channels tolerates a visibly large bias before it becomes
significant.
References.
- P. R. Bevington and D. K. Robinson, Data Reduction and Error Analysis for the Physical Sciences, 3rd ed., McGraw-Hill (2003), Sec. 4.2 (standard deviation of the mean).
chi_squared
chi_squared(residual, sigma)
Chi-square, $\sum_i (r_i/\sigma_i)^2$ over the finite terms.
\[\chi^2 = \sum_i\left(\frac{r_i}{\sigma_i}\right)^2\]| Parameter | Type | Unit | Description |
|---|---|---|---|
residual | array-like | any | Residuals. |
sigma | array-like | any | One-sigma uncertainty of each residual, same unit. |
| Returns | Type | Unit | Description |
|---|---|---|---|
| float | - | Chi-square; |
Physical interpretation.
The weighted sum of squared residuals – the quantity a Gaussian
maximum-likelihood fit minimizes. Under the null hypothesis that the model
is correct and the errors are independent, zero-mean and Gaussian with the
stated sigma, it follows a chi-square distribution.
Assumptions.
Independent, unbiased, correctly scaled Gaussian errors. Correlated channels – a common situation for magnetic diagnostics sharing an integrator or a calibration – inflate or deflate this sum without any model being wrong.
Limitations.
Terms where sigma is zero or either input is non-finite are dropped.
Notes.
Rarely read raw, since its expected size grows with the number of channels;
divide by the degrees of freedom with reduced_chi_squared. Its
useful raw form is the share each diagnostic family contributes, which
localizes a bad fit.
References.
- P. R. Bevington and D. K. Robinson, Data Reduction and Error Analysis for the Physical Sciences, 3rd ed., McGraw-Hill (2003), Sec. 4.3 and Ch. 11 (chi-square test of goodness of fit).
dynamic_range
dynamic_range(values)
Sample range, $\max - \min$ over the finite entries.
\[D = \max_i x_i - \min_i x_i\]| Parameter | Type | Unit | Description |
|---|---|---|---|
values | array-like | any | Signal samples; non-finite entries are ignored. |
| Returns | Type | Unit | Description |
|---|---|---|---|
| float | any | Range in the unit of the signal; 0 for a constant signal, |
Physical interpretation.
The span the signal actually used, in its own physical units. Unlike
relative_spread it is not normalized, so it stays meaningful for a
signal whose values straddle zero, where dividing by max|x| is
dominated by whichever excursion happened to be larger.
Assumptions.
None, but note that the range is the least robust of the scale estimators
here: it is defined entirely by the two most extreme samples, so one spike
sets it. Screen for spikes (see robust_z_scores) before reading it
as the signal’s working span.
Notes.
The denominator that turns an absolute residual into a fraction of what the channel was actually doing. A 1 mT residual on a probe that swung 10 mT is a 10% model error; the same residual on a probe that swung 1 mT is a failure of the model.
References.
- NIST/SEMATECH e-Handbook of Statistical Methods (2012), https://doi.org/10.18434/M32189, Sec. 1.3.5.6.
fractional_rms_improvement
fractional_rms_improvement(baseline_residual, residual)
Fractional RMS improvement, $1 - \mathrm{RMS}(r)/\mathrm{RMS}(r_0)$.
\[\eta = 1 - \frac{\mathrm{RMS}(\mathrm{residual})}{\mathrm{RMS}(\mathrm{baseline})}\]| Parameter | Type | Unit | Description |
|---|---|---|---|
baseline_residual | array-like | any | Residual of the reference model. |
residual | array-like | any | Residual of the model under test, same samples and unit. |
| Returns | Type | Unit | Description |
|---|---|---|---|
| float | - | Skill score; |
Physical interpretation.
The fraction of the baseline residual amplitude removed by whatever distinguishes the two models. It is a relative, dimensionless skill score, not a significance test.
Assumptions.
The two residuals come from the same samples and the same measurement,
differing only in the model subtracted. The baseline must be non-zero and
finite; otherwise the ratio is meaningless and nan is returned.
Notes.
1.0 is a perfect reconstruction, 0.0 means the extra model term
contributed nothing, and a negative value means it made the agreement
worse – which is informative, since a physically motivated term that
degrades the fit points at a sign error or a bad response matrix rather
than at noise.
References.
- NIST/SEMATECH e-Handbook of Statistical Methods (2012), https://doi.org/10.18434/M32189, Sec. 4.4.4 (model validation).
lag1_autocorrelation
lag1_autocorrelation(values)
Lag-1 autocorrelation of the finite entries, in the order given.
\[r_1 = \frac{\sum_i (x_i - \bar x)(x_{i+1} - \bar x)}{\sum_i (x_i - \bar x)^2}\]| Parameter | Type | Unit | Description |
|---|---|---|---|
values | array-like | any | Ordered sample; non-finite entries are dropped before pairing. |
| Returns | Type | Unit | Description |
|---|---|---|---|
| float | - | Lag-1 autocorrelation in roughly $[-1, 1]$; |
Physical interpretation.
The normalized correlation between each sample and its neighbour in
sequence. Under independence its expectation is approximately -1/n,
i.e. zero for practical purposes.
Assumptions.
That the ordering of the array carries meaning – adjacency in time, in space, or in a physically ordered channel index. Applied to an arbitrarily ordered array the statistic is meaningless. The sample must have at least three finite entries and non-zero variance.
Notes.
A strongly positive value means neighbouring residuals move together, which for a spatially ordered channel array is the signature of a smooth unmodelled field rather than of independent measurement noise. A strongly negative value means alternation, which points at a wiring or sign convention error on alternate channels.
References.
- NIST/SEMATECH e-Handbook of Statistical Methods (2012), https://doi.org/10.18434/M32189, Sec. 1.3.5.12 (autocorrelation).
- G. E. P. Box, G. M. Jenkins and G. C. Reinsel, Time Series Analysis, 4th ed., Wiley (2008), Sec. 2.1.
linear_trend
linear_trend(x, y)
Least-squares slope of $y$ against $x$, in units of $y$ per unit $x$.
\[\hat b = \frac{\sum_i (x_i - \bar x)(y_i - \bar y)}{\sum_i (x_i - \bar x)^2}\]| Parameter | Type | Unit | Description |
|---|---|---|---|
x | array-like | any | Abscissae. |
y | array-like | any | Ordinates, same length. |
| Returns | Type | Unit | Description |
|---|---|---|---|
| float | any | Slope; |
Raises.
ValueError Mismatched lengths.
Physical interpretation.
The first-order coefficient of an ordinary least-squares fit, over the samples where both series are finite.
Assumptions.
That a straight line is a meaningful summary. It is the right question for integrator drift or a slowly walking baseline, and the wrong one for a signal whose shape is genuinely curved – the slope will report something in that case too, and it will not mean drift.
Numerical notes.
numpy.polyfit of degree 1 (unweighted).
Notes.
Signed, and in physical units, so it is compared against the signal’s own
scale rather than against a universal number: a drift of 1e-4 T/s matters
for a probe whose dynamic range is 1e-3 T and does not for one spanning
1 T. Unlike residual_bias it is insensitive to a constant offset,
which is what separates “this channel is offset” from “this channel is
walking”.
References.
- P. R. Bevington and D. K. Robinson, Data Reduction and Error Analysis for the Physical Sciences, 3rd ed., McGraw-Hill (2003), Sec. 6.3.
log10_decay_rate
log10_decay_rate(values, *, tail=5)
Least-squares slope of $\log_{10}x$ against iteration index over the tail.
\[\hat b = \operatorname{slope}\big(\log_{10}x_{n-m+1..n}\ \text{vs}\ 0, 1, \dots, m-1\big)\]| Parameter | Type | Unit | Description |
|---|---|---|---|
values | array-like | any | Iteration history; non-positive and non-finite entries are dropped. |
tail | int, optional | - | Number of trailing samples fitted; default 5. |
| Returns | Type | Unit | Description |
|---|---|---|---|
| float | - | Decades per iteration; |
Physical interpretation.
The exponential rate constant of the sequence, estimated in log space so that a geometric decay becomes a straight-line fit. Ordinary least squares in log space weights relative rather than absolute errors, which is the right choice for a quantity spanning orders of magnitude.
Assumptions.
At least three positive finite samples in the tail; non-positive entries cannot be logged and are dropped, which silently shortens the window. The fit assumes a single exponential regime, so a history that changes behaviour mid-run gives a slope describing neither phase. Only the tail is used, deliberately: the early transient of an iterative solve says nothing about whether it is converging now.
Numerical notes.
numpy.polyfit of degree 1 on log10 of the tail.
Notes.
A slope of -1 means the error falls by a decade per iteration. A value
near zero means the iteration has stagnated – it is still running but no
longer improving, which is a different and more troubling outcome than
hitting an iteration limit while still descending. What counts as “near
zero” is a solver policy decision and belongs to the caller.
References.
- C. T. Kelley, Iterative Methods for Linear and Nonlinear Equations, SIAM (1995), Sec. 1.2 (linear and q-linear convergence).
median_absolute_deviation
median_absolute_deviation(values)
| Median absolute deviation, $\mathrm{median}( | x - \mathrm{median}(x) | )$. |
| Parameter | Type | Unit | Description |
|---|---|---|---|
values | array-like | any | Sample; non-finite entries are ignored. |
| Returns | Type | Unit | Description |
|---|---|---|---|
| float | any | Raw MAD in the unit of the sample, not rescaled to a Gaussian sigma; |
Physical interpretation.
A scale estimator with a 50% breakdown point: half the samples may be arbitrarily corrupted before it moves. The standard deviation, by contrast, has a breakdown point of zero – one bad sample moves it without limit.
Assumptions.
None beyond the samples sharing a scale. In particular no distribution is
assumed; the value returned is the raw median deviation, not rescaled to
a Gaussian-equivalent sigma. robust_z_scores applies the 1.4826
consistency factor where that interpretation is wanted, so the two uses
cannot drift apart.
Notes.
For a signal being screened for spikes or dropouts, this is the width of the bulk of the samples – the noise level the anomalies must be measured against, rather than a noise level the anomalies have already inflated.
References.
- P. J. Rousseeuw and C. Croux, J. Am. Stat. Assoc. 88 (1993) 1273, Sec. 1.
monotonic_fraction
monotonic_fraction(values)
Fraction of consecutive steps in which the sequence decreased.
\[f = \frac{1}{n - 1}\,\operatorname{count}\big(x_{i+1} < x_i\big)\]| Parameter | Type | Unit | Description |
|---|---|---|---|
values | array-like | any | Ordered iteration history. |
| Returns | Type | Unit | Description |
|---|---|---|---|
| float | - | Decreasing-step fraction; |
Physical interpretation.
The empirical rate of the event “the next sample is smaller than this one”
over the n - 1 adjacent pairs. Comparisons involving a non-finite entry
count as non-decreases, so a broken history is penalized rather than
silently shortened.
Assumptions.
The sequence is an ordered iteration history in which decrease is the desired direction. A short history makes the fraction coarse – with four iterations it can only take five values.
Notes.
For a solver error history, 1.0 is a cleanly contracting iteration.
Values near 0.5 mean the error is bouncing, which indicates a step that
is too aggressive or a solution oscillating between two branches – a
distinct failure from steady but slow progress, which
log10_decay_rate detects instead.
References.
- C. T. Kelley, Iterative Methods for Linear and Nonlinear Equations, SIAM (1995), Sec. 1.2 (convergence histories).
noise_band
noise_band(values)
Mean and population standard deviation of a reference (effect-free) sample.
\[\mu = \frac{1}{n}\sum_i x_i, \qquad \sigma = \sqrt{\frac{1}{n}\sum_i (x_i - \mu)^2}\]| Parameter | Type | Unit | Description |
|---|---|---|---|
values | array-like | any | Reference stretch of signal; non-finite entries are ignored. |
| Returns | Type | Unit | Description |
|---|---|---|---|
mean | float | any | Offset of the reference; |
std | float | any | Population ( |
Physical interpretation.
The first two moments of a stretch of signal taken to contain no effect, estimating the offset and the amplitude of the measurement noise.
Assumptions.
The sample is genuinely effect-free and stationary over its extent. A drift, a switching transient, or a leaked part of the effect inside the reference window inflates the estimated noise and makes any threshold built on it too permissive.
Notes.
Deriving the threshold from the channel’s own measured quiet stretch, rather than from a global constant, means a noisy channel is judged by its own noise – essential when channels differ in gain, integrator drift and cabling.
References.
- P. R. Bevington and D. K. Robinson, Data Reduction and Error Analysis for the Physical Sciences, 3rd ed., McGraw-Hill (2003), Sec. 1.3.
normalized_residual
normalized_residual(residual, weight, k)
Residuals in units of the fitted uncertainty, $z = r\,w/k$.
\[z_i = \frac{r_i\,w_i}{k}, \qquad \sigma_i = \frac{k}{w_i}\]| Parameter | Type | Unit | Description |
|---|---|---|---|
residual | array-like | any | Residuals of a weighted least-squares fit. |
weight | array-like | any | Inverse-uncertainty weights of the same channels. |
k | float | any | Common units-of-fit factor, see |
| Returns | Type | Unit | Description |
|---|---|---|---|
| np.ndarray | - | Normalized residuals, shape of |
Physical interpretation.
A weighted least-squares fit assigns each channel an effective uncertainty
sigma = k / weight; dividing the residual by it puts every channel on
the same dimensionless scale, so a flux loop and a B-probe residual become
directly comparable.
Assumptions.
That weight really is the inverse uncertainty up to the single common
factor k – recover k from the fit’s own chi-square with
sigma_unit_factor rather than assuming it. If k is not finite
or is zero the normalization is undefined and an all-nan array of the
residual’s shape is returned.
Notes.
|z| ~ 1 means a channel is reproduced to within the uncertainty it was
given. Because the uncertainties are an input, a uniformly small |z|
can equally mean the uncertainties were overstated.
References.
- P. R. Bevington and D. K. Robinson, Data Reduction and Error Analysis for the Physical Sciences, 3rd ed., McGraw-Hill (2003), Sec. 4.1 (weighted means and standardized residuals).
outlier_fraction
outlier_fraction(values, level)
| Fraction of finite entries with $ | x_i | > \mathrm{level}$. |
| Parameter | Type | Unit | Description |
|---|---|---|---|
values | array-like | - | Normalized sample; non-finite entries are ignored. |
level | float | - | Threshold in the sample’s own units. |
| Returns | Type | Unit | Description |
|---|---|---|---|
| float | - | Tail fraction; |
Physical interpretation.
The empirical tail mass beyond a threshold. For standard normal samples the expected fractions are about 4.6% beyond 2 and 0.27% beyond 3.
Assumptions.
That values are normalized so level is a number of standard
deviations – for raw residuals the threshold has no such meaning. With
few channels the estimate is coarse: one outlier in ten channels is 10%,
which is not by itself evidence of anything.
Notes.
Distinguishes a fit that is uniformly mediocre from one that is good everywhere except a handful of channels. The second, an excess tail with an acceptable RMS, usually means a specific broken diagnostic rather than a bad reconstruction.
References.
- NIST/SEMATECH e-Handbook of Statistical Methods (2012), https://doi.org/10.18434/M32189, Sec. 1.3.5.17 (detection of outliers).
pearson_correlation
pearson_correlation(a, b)
Pearson correlation of two series over their pairwise-finite samples.
\[\rho = \frac{\sum_i (a_i - \bar a)(b_i - \bar b)} {\sqrt{\sum_i (a_i - \bar a)^2}\sqrt{\sum_i (b_i - \bar b)^2}}\]| Parameter | Type | Unit | Description |
|---|---|---|---|
a | array-like | any | First series. |
b | array-like | any | Second series, same length. |
| Returns | Type | Unit | Description |
|---|---|---|---|
| float | - | Correlation in $[-1, 1]$; |
Raises.
ValueError Mismatched lengths.
Physical interpretation.
Covariance normalized by both standard deviations: the cosine of the angle between the two mean-centred series.
Assumptions.
A linear relationship. Two series related by a strong but curved mapping correlate poorly, and the low value says the relationship is not linear rather than that it is absent.
Notes.
For a measured signal against a forward model it separates shape agreement from amplitude agreement, which an RMS residual conflates. A correlation near 1 with a large residual says the model has the dynamics right and the gain wrong – a calibration question. A small correlation with a small residual says neither signal is doing much, and the comparison is uninformative rather than successful.
References.
- P. R. Bevington and D. K. Robinson, Data Reduction and Error Analysis for the Physical Sciences, 3rd ed., McGraw-Hill (2003), Sec. 11.2 (linear-correlation coefficient).
percentile_scale
percentile_scale(values, percentile=90.0)
A high percentile of |x| over the finite entries, an amplitude scale one anomalous sample cannot set.
| Parameter | Type | Unit | Description |
|---|---|---|---|
values | ndarray or iterable of float | units of values | Samples; non-finite entries are ignored. |
percentile | float | % | Order statistic to take, between 0 and 100. |
| Returns | Type | Unit | Description |
|---|---|---|---|
| float | units of values | The percentile of the magnitudes; |
Physical interpretation.
The denominator for putting channels of one family on a common footing.
Normalising a family by its max lets one saturated or miswired channel
rescale every other channel’s residual – on the packaged VEST samples a
single probe reading 2.7 T against a 0.04 T population set the scale for
all 63 probes, and every derived residual statistic with it. A percentile
does not have that failure.
Assumptions.
None. It is not a noise estimator (see median_absolute_deviation);
it answers “how big is this signal, typically at its largest” and is meant
as a normalisation.
Notes.
An order statistic of the magnitudes. At the 90th percentile, up to a
tenth of the samples may be arbitrarily large before the value moves,
whereas max|x| is defined entirely by the single largest one.
References.
- R. J. Hyndman and Y. Fan, Am. Stat. 50 (1996) 361 (sample quantile definitions; NumPy’s default is their type 7).
reduced_chi_squared
reduced_chi_squared(chi_squared_total, degrees_of_freedom)
Reduced chi-square, $\chi^2/\nu$ per degree of freedom.
\[\chi^2_\nu = \frac{\chi^2}{\nu}\]| Parameter | Type | Unit | Description |
|---|---|---|---|
chi_squared_total | float | - | Total chi-square. |
degrees_of_freedom | float | - | Effective degrees of freedom $\nu$, positive. |
| Returns | Type | Unit | Description |
|---|---|---|---|
| float | - | Chi-square per degree of freedom; |
Physical interpretation.
The mean squared normalized residual per free parameter’s worth of freedom. Its expectation is 1 when the model is correct and the uncertainties are right, since each independent constraint contributes one unit of chi-square on average.
Assumptions.
All three of these must be defensible before the value means anything: the
supplied uncertainties are the true measurement uncertainties, the residuals
are independent, and dof is the effective number of degrees of freedom
rather than a nominal channel count. Regularized or constrained fits
routinely violate the last of these, and correlated magnetics violate the
second.
Notes.
Read it as a diagnostic indicator, never as a pass/fail gate. A value well
above 1 means the model does not explain the data given the stated
uncertainties – which may be a bad model, understated uncertainties, or a
mis-counted dof. A value well below 1 usually means overstated
uncertainties or over-fitting, not an unusually good reconstruction.
References.
- P. R. Bevington and D. K. Robinson, Data Reduction and Error Analysis for the Physical Sciences, 3rd ed., McGraw-Hill (2003), Sec. 11.1.
relative_spread
relative_spread(values)
| Relative spread, $(\max - \min)/\max | x | $ over the finite entries. |
| Parameter | Type | Unit | Description |
|---|---|---|---|
values | iterable of float | any | Independent estimates of one quantity. |
| Returns | Type | Unit | Description |
|---|---|---|---|
| float | - | Relative spread; 0 for all-zero values, |
Physical interpretation.
A dimensionless, non-parametric measure of disagreement within a small set: the full range normalized by the largest magnitude present. It is deliberately range-based rather than variance-based, because it is meant for a handful of values where a standard deviation would be noise.
Assumptions.
The values are independent estimates of the same quantity, so that any spread between them is inconsistency rather than physics. Being range-based it is maximally sensitive to a single bad estimate, which is the intent when used as a consistency check.
Notes.
A self-consistency metric: several routes to one quantity should agree, and
the spread is how far they do not. Returns 0.0 – not nan – when
every value is exactly zero, since that is perfect agreement, and nan
when fewer than two finite values are available to compare.
References.
- NIST/SEMATECH e-Handbook of Statistical Methods (2012), https://doi.org/10.18434/M32189, Sec. 1.3.5.6 (measures of scale).
residual_bias
residual_bias(values)
Mean of the finite entries, the systematic offset of a residual sample.
\[\bar r = \frac{1}{n}\sum_{i=1}^{n} r_i\]| Parameter | Type | Unit | Description |
|---|---|---|---|
values | array-like | any | Residual sample; non-finite entries are ignored. |
| Returns | Type | Unit | Description |
|---|---|---|---|
| float | any | Sample mean in the unit of the residual; |
Physical interpretation.
The first moment. For normalized residuals its sampling distribution under
the null hypothesis has mean 0 and standard deviation 1/sqrt(n) (see
bias_standard_error), so the mean is directly comparable against
that scale.
Assumptions.
Independent samples drawn from one distribution. Channels that share a systematic – a common calibration, a common integrator drift – are not independent, and the bias is then easier to trigger than the nominal standard error suggests.
Notes.
Random measurement noise averages to zero; a bias that does not is evidence
of something the model is not representing, such as an unmodelled field
component or a geometry offset, rather than of noise. Unlike rms,
it is signed, so cancelling positive and negative residuals give a small
bias with a large RMS – which is itself diagnostic of structure rather than
offset.
References.
- P. R. Bevington and D. K. Robinson, Data Reduction and Error Analysis for the Physical Sciences, 3rd ed., McGraw-Hill (2003), Sec. 1.2.
rms
rms(values)
Root mean square, $\sqrt{\mathrm{mean}(x_i^2)}$, over the finite entries.
\[\mathrm{RMS} = \sqrt{\frac{1}{n}\sum_{i=1}^{n} x_i^2}\]| Parameter | Type | Unit | Description |
|---|---|---|---|
values | array-like | any | Sample; non-finite entries are ignored. |
| Returns | Type | Unit | Description |
|---|---|---|---|
| float | any | Quadratic mean in the unit of the sample; |
Physical interpretation.
The quadratic mean: the scale of the sample about zero, not about its own mean. For a zero-mean sample it coincides with the population standard deviation; for a biased one it exceeds it, because a systematic offset contributes on equal footing with scatter.
Assumptions.
That zero is the meaningful reference. Applied to a residual this holds by
construction; applied to a signal it does not, and residual_bias
should be inspected alongside it. Samples are weighted equally, so a
channel array mixing units or uncertainties must be normalized first (see
normalized_residual).
Limitations.
Returns nan when no finite sample remains.
Notes.
The single number for “how far off is this reconstruction”, in the units of
the residual. It is dominated by the worst channels, so a small RMS with a
large outlier_fraction is contradictory and means the residual
distribution is not what the average suggests.
References.
- P. R. Bevington and D. K. Robinson, Data Reduction and Error Analysis for the Physical Sciences, 3rd ed., McGraw-Hill (2003), Sec. 1.3.
robust_z_scores
robust_z_scores(values)
Deviation from the median in robust sigma units, sample by sample.
\[z_i = \frac{x_i - \operatorname{median}(x)}{1.4826\,\mathrm{MAD}}\]where 1.4826 makes the denominator a consistent estimator of the standard deviation for Gaussian samples, so a threshold in these units carries its usual meaning.
| Parameter | Type | Unit | Description |
|---|---|---|---|
values | array-like | any | Sample. |
| Returns | Type | Unit | Description |
|---|---|---|---|
| np.ndarray | - | Robust z-scores with the input’s shape, |
Physical interpretation.
A z-score built on estimators that the anomalies being looked for cannot themselves corrupt. A single large spike inflates the mean and the standard deviation enough to hide itself from a conventional z-score; it moves neither the median nor the MAD.
Assumptions.
A unimodal bulk. These scores are meaningless for a genuinely bimodal sample, where the median falls between the modes and every sample scores as an outlier.
Limitations.
A zero MAD means the bulk of the samples are identical – a perfectly
linear ramp differenced, or a flatlined channel. Any departure from that
constant is then infinitely many sigmas out, and is reported as signed
inf rather than as nan, because such a sample is the clearest
possible anomaly and not an undefined one. Returns an all-nan array
when no finite sample remains.
Notes.
Unlike the scalar reductions elsewhere in this module, the result is positional: it has the shape of the input, so a caller can locate which samples deviate and not merely how many.
References.
- P. J. Rousseeuw and C. Croux, J. Am. Stat. Assoc. 88 (1993) 1273.
- NIST/SEMATECH e-Handbook of Statistical Methods (2012), https://doi.org/10.18434/M32189, Sec. 1.3.5.17.
runs_test_z
runs_test_z(values)
Wald-Wolfowitz runs-test z-score for the sign sequence of a sample.
With $n_+$ positive and $n_-$ negative entries out of $n$, and $R$ the number of maximal same-sign runs,
\[E[R] = \frac{2n_+n_-}{n} + 1, \qquad \mathrm{Var}[R] = \frac{2n_+n_-(2n_+n_- - n)}{n^2(n - 1)}, \qquad z = \frac{R - E[R]}{\sqrt{\mathrm{Var}[R]}}\]| Parameter | Type | Unit | Description |
|---|---|---|---|
values | array-like | any | Ordered sample; zeros and non-finite entries carry no sign and are dropped. |
| Returns | Type | Unit | Description |
|---|---|---|---|
| float | - | z-score; |
Physical interpretation.
A distribution-free test of whether a binary sequence is randomly ordered.
It uses only the signs, so it is insensitive to outlier magnitude – which
makes it complementary to rms and lag1_autocorrelation
rather than redundant with them.
Assumptions.
Exchangeable samples under the null, a meaningful ordering, and enough of
both signs for the normal approximation: this returns nan below three
non-zero samples or when one sign is absent entirely.
Notes.
|z| > 2 means the sign pattern is unlikely under independence. Too few
runs (negative z) is clustering – contiguous stretches of one-signed
residual, the fingerprint of an unmodelled coherent field component. Too
many (positive z) is alternation, which points at an indexing or
polarity error rather than at physics.
References.
- A. Wald and J. Wolfowitz, Ann. Math. Statist. 11 (1940) 147.
- NIST/SEMATECH e-Handbook of Statistical Methods (2012), https://doi.org/10.18434/M32189, Sec. 1.3.5.13 (runs test).
sigma_threshold_crossing
sigma_threshold_crossing(time, values, reference_mask, *, sigma)
First time outside the reference window where the signal leaves its noise band.
\[t^* = \min\big(t_i : i \notin \mathrm{ref},\ |x_i - \mu| > \sigma_{\mathrm{thr}}\,\sigma\big)\]with $(\mu, \sigma)$ from noise_band over the reference samples.
| Parameter | Type | Unit | Description |
|---|---|---|---|
time | array-like | any | Sample times. |
values | array-like | any | Signal samples. |
reference_mask | array-like of bool | bool |
|
sigma | float | - | Threshold in units of the reference noise. |
| Returns | Type | Unit | Description |
|---|---|---|---|
| float | any | Crossing time in the unit of |
Physical interpretation.
A fixed-threshold first-passage detector with the threshold expressed in
units of the sample’s own noise, so the nominal false-alarm probability per
sample is set by sigma alone rather than by the channel’s gain.
Assumptions.
A well-defined quiet reference stretch of at least two finite samples,
stationary noise across the whole record, and independent samples –
oversampled or filtered data has fewer independent samples than points, so
the effective false-alarm rate is lower than a naive count suggests. With
n samples searched, the expected number of noise-only crossings is
roughly n times the per-sample tail probability, which is why a large
sigma is used in practice.
Notes.
Onset detection: the first moment a channel sees something its own pre-event noise cannot explain. Comparing onsets across channels is the real test – a physical event appears everywhere at once, while a forward-model or response-matrix artifact does not.
References.
- NIST/SEMATECH e-Handbook of Statistical Methods (2012), https://doi.org/10.18434/M32189, Sec. 6.3.2 (Shewhart control limits).
sigma_unit_factor
sigma_unit_factor(residual, weight, chi_squared_per_channel)
Recover the units-of-fit factor $k$ from a fit’s own per-channel chi-square.
\[k = \operatorname{median}_i\frac{|r_i\,w_i|}{\sqrt{\chi^2_i}}, \qquad \mathrm{spread} = \frac{\max_i - \min_i}{k}\]from the identity $\chi^2_i = (r_i w_i/k)^2$.
| Parameter | Type | Unit | Description |
|---|---|---|---|
residual | array-like | any | Stored residual of each channel. |
weight | array-like | any | Stored inverse-uncertainty weight of each channel. |
chi_squared_per_channel | array-like | - | Stored per-channel chi-square. |
| Returns | Type | Unit | Description |
|---|---|---|---|
k | float | any | Median units-of-fit factor; |
spread | float | - | Peak-to-peak scatter of the per-channel ratios over |
Physical interpretation.
A robust (median) estimate of a single multiplicative constant that reconciles the units three stored arrays were written in. The median rather than the mean, so one corrupted channel cannot move the estimate.
Assumptions.
That one constant explains all channels. spread is the test of that
assumption, and it is what makes the estimate self-validating: it must be
small. A large spread means the stored chi-square and the stored residual
no longer describe the same fit, and every normalized residual built on
k is then suspect.
Limitations.
Channels are used only where the weight is positive and both the chi-square
and the residual are finite with a positive chi-square; (nan, nan) comes
back when none qualify.
Notes.
Recovering k rather than hard-coding a unit convention means a future
convention change surfaces as a spread warning instead of as silently wrong
normalized residuals. Note that spread here is a peak-to-peak-over-median
ratio and is deliberately not the same statistic as relative_spread.
References.
- P. R. Bevington and D. K. Robinson, Data Reduction and Error Analysis for the Physical Sciences, 3rd ed., McGraw-Hill (2003), Sec. 4.1.
Refreshing this snapshot
From a checkout of the develop branch, run:
python -m vaft.formula.catalog --output /path/to/vaft-gh/_data/formula_catalog.yml
The snapshot records the SHA-256 of every vaft/formula/*.py source file; documentation
validation compares them when VAFT_REGISTRY_SOURCE points to the corresponding source checkout.
The same text is available offline as vaft.formula.describe("<name>"),
vaft.formula.search("<text>") and vaft.formula.list_formulas(category="<category>").