eradiate.testing.regression.ZTest#

class eradiate.testing.regression.ZTest(threshold, *, variable='brf_srf', dim=None)#

Bases: RegressionTest

Z-test with Šidák correction factor.

Implement a Z-test, testing the significance of paired differences between a set of observations and a set of references. It considers the variance of both the observations and the reference, which are both Monte Carlo estimates: the standard error of their difference is \(\sqrt{\sigma^2_\mathrm{result} + \sigma^2_\mathrm{reference}}\). The observation variance (<variable>_var) is mandatory; if the reference does not carry one, the test falls back to the observation variance alone and reports a warning. That fallback underestimates the standard error by up to a factor of \(\sqrt{2}\), making the test conservative.

Paired tests are aggregated into one p-value using a Šidák correction: the test passes if the null hypothesis is accepted for every pair at the corrected per-comparison level \(1 - (1 - \alpha)^{1/n}\). The reported metric is the equivalent family-wise p-value (see sidak_family_p_value()), so that the test passes iff the metric exceeds the threshold.

This paired Z-test requires an equal degree of freedom of the two groups.

Parameters:
  • threshold (float) – Test metric threshold.

  • variable (str, default: "brf_srf") – Tested variable.

  • dim (str, optional, default: None) – If set, evaluate the metric independently for each value of this dimension. The test then passes iff every slice passes, and the reported metric is that of the worst slice. This is stricter than comparing the flattened arrays in one go, and it is how datasets aggregating several independent measurements (e.g. one per wavelength) must be tested. Tests whose threshold is a false-positive rate rather than an absolute tolerance (e.g. ZTest) correct for the number of slices, so that threshold keeps its meaning; see evaluate().

Fields:
  • threshold (float) – Test metric threshold.

  • variable (str) – Tested variable.

  • dim (str or None) – If set, evaluate the metric independently for each value of this dimension.

Notes

The dim field is diagnostic only: the correction extends over the slices, so the verdict and the reported family-wise p-value are those of the un-sliced test. What dim adds is the identity of the worst slice, and per-slice details describing it. Judging each of \(N\) slices at level \(\alpha\) instead would let the overall false-positive rate grow to \(1 - (1 - \alpha)^N\) — 40 % for ten wavelengths at \(\alpha = 0.05\) — which is why slices are evaluated at \(1 - (1 - \alpha)^{1/N}\) and their p-values combined back with sidak_family_p_value(). Slices of a single dimension all hold the same number of comparisons, which is what makes the two routes exactly equivalent.

evaluate(result, reference)#

Compare result against reference using this test’s criterion.

Parameters:
  • result (Dataset) – Simulation result.

  • reference (Dataset) – Reference data.

Returns:

RegressionTestOutcome

Raises:

ValueError – If the data is malformed, e.g. if the shapes do not match or if a required data variable is missing. This is a broken comparison, not a failed one: a failed comparison is reported through the returned outcome.

Notes

When dim is set, each slice is evaluated on its own and the per-slice metrics are combined into the single value carried by the outcome. Two hooks control that combination: _slice_threshold() sets the threshold each slice is evaluated against, and _aggregate_metrics() turns the per-slice metrics back into one comparable with threshold. Their defaults simply take the worst slice, which is what an absolute tolerance such as an RMSE calls for. A test whose threshold is a false-positive rate must override both, otherwise judging N slices at level \(\alpha\) would let the overall false-positive rate grow to \(1 - (1 - \alpha)^N\); see ZTest.

plot(result, reference, outcome=None)#

Draw a comparison plot with reference and test data displayed together, with the result’s Monte Carlo variance shown as error bars.

plot_diagnostic(ax, z=None)#

Diagnostic plot for a Z-test.

Parameters:
  • ax (Axes) – Axes to draw on.

  • z (array-like) – Z-statistic for each pair of measurements.

plot_noref(result)#

Draw a chart of a result that has no reference to be compared with, i.e. a reference candidate.

Parameters:

result (Dataset) – Simulation result.

Returns:

  • figure (Figure)

  • axes (Axes)