Significance tests and multiple-comparison control for eval comparisons.
mcnemar/2- exact McNemar test (binomial on discordant pairs) with the mid-P variant, for paired binary outcomesfisher_exact/4- Fisher's exact test for an unpaired 2×2 tabletango_test/4,tango_ci/4- Tango's score test and interval for the difference of two paired proportions (Tango 1998, Statistics in Medicine 17:891–908; the interval of R'sPropCIs::scoreci.mp)noninferiority/5- the paired non-inferiority gate used for guard non-regression (H0: Δ ≥ margin)holm/2,benjamini_hochberg/2- adjusted p-values
Orientation of paired counts
For n paired cases, b counts cases where the candidate has the event
and the baseline does not, and c the reverse. The paired difference is
Δ = p_candidate − p_baseline = (b − c) / n. For a guard's false-allow
rate, b is "new misses" and c is "fixes".
Per-case significance is almost never reachable at realistic k (the most extreme 3-of-3 vs 0-of-3 table has two-sided Fisher p = 0.1), so gate on aggregate tests and use Holm across a small declared family of metrics; use Benjamini–Hochberg for exploratory per-tag breakdowns.
Summary
Functions
Benjamini–Hochberg adjusted p-values (false discovery rate control), for exploratory breakdowns such as per-tag deltas.
Fisher's exact test, two-sided, for the 2×2 table [[a, b], [c, d]].
Holm step-down adjusted p-values (family-wise error control, uniformly more powerful than Bonferroni).
Exact McNemar test on the discordant counts b and c.
Paired non-inferiority test with margin margin (Tango score test).
Tango's score confidence interval for the paired difference
Δ = (b − c) / n: the set of δ with |Z(δ)| ≤ z_{1−α/2}, found by
bisection. Unlike the Wald interval it stays inside [-1, 1] and behaves
well when b + c is small.
Tango's score test for the paired difference Δ = (b − c) / n against
delta0.
Functions
@spec benjamini_hochberg([number()], keyword()) :: {:ok, %{adjusted: [float()], rejected: [boolean()], alpha: float()}} | {:error, :invalid_values | {:invalid_option, :alpha}}
Benjamini–Hochberg adjusted p-values (false discovery rate control), for exploratory breakdowns such as per-tag deltas.
adjusted_(j) = min_{i ≥ j} min(1, m·p_(i)/i) over the ascending order;
results come back in input order. :rejected marks adjusted <= alpha.
Options: :alpha (default 0.05).
Examples
iex> Nous.Eval.Stats.Hypothesis.benjamini_hochberg([0.01, 0.04, 0.03])
{:ok, %{adjusted: [0.03, 0.04, 0.04], rejected: [true, true, true], alpha: 0.05}}
@spec fisher_exact( non_neg_integer(), non_neg_integer(), non_neg_integer(), non_neg_integer() ) :: {:ok, %{p_value: float(), odds_ratio: float() | :infinity | :undefined}} | {:error, :invalid_counts}
Fisher's exact test, two-sided, for the 2×2 table [[a, b], [c, d]].
The p-value sums the hypergeometric probabilities of every table with the
same margins that is no more likely than the observed one (relative
tolerance 1e-7, as in R). :odds_ratio is the sample odds ratio
a·d / (b·c): :infinity when only b·c is zero, :undefined when both
products are zero.
An all-zero table gives p_value: 1.0.
Examples
iex> {:ok, r} = Nous.Eval.Stats.Hypothesis.fisher_exact(3, 0, 0, 3)
iex> {Float.round(r.p_value, 6), r.odds_ratio}
{0.1, :infinity}
@spec holm([number()], keyword()) :: {:ok, %{adjusted: [float()], rejected: [boolean()], alpha: float()}} | {:error, :invalid_values | {:invalid_option, :alpha}}
Holm step-down adjusted p-values (family-wise error control, uniformly more powerful than Bonferroni).
adjusted_(j) = min(1, max_{i ≤ j} (m − i + 1)·p_(i)) over the ascending
order; results come back in input order. :rejected marks
adjusted <= alpha. An empty list gives empty results.
Options: :alpha (default 0.05).
Examples
iex> Nous.Eval.Stats.Hypothesis.holm([0.01, 0.04, 0.03])
{:ok, %{adjusted: [0.03, 0.06, 0.06], rejected: [true, false, false], alpha: 0.05}}
@spec mcnemar(non_neg_integer(), non_neg_integer()) :: {:ok, %{ b: non_neg_integer(), c: non_neg_integer(), statistic: non_neg_integer(), p_value: float(), mid_p_value: float() }} | {:error, :invalid_counts}
Exact McNemar test on the discordant counts b and c.
:p_value is the two-sided exact p-value, min(1, 2·P(X ≤ min(b, c)))
with X ~ Binomial(b + c, ½). :mid_p_value subtracts half the
probability of the observed table (Fagerland, Lydersen & Laake 2013), which
is less conservative and close to nominal size. :statistic is min(b, c).
b + c == 0 (no discordant pairs) gives p_value: 1.0, mid_p_value: 1.0.
Examples
iex> Nous.Eval.Stats.Hypothesis.mcnemar(0, 5)
{:ok, %{b: 0, c: 5, statistic: 0, p_value: 0.0625, mid_p_value: 0.03125}}
@spec noninferiority( non_neg_integer(), non_neg_integer(), pos_integer(), number(), keyword() ) :: {:ok, %{ estimate: float(), margin: float(), alpha: float(), z: float(), p_value: float(), ci: Nous.Eval.Stats.ci(), non_inferior?: boolean() }} | {:error, :empty | :invalid_counts | {:invalid_option, :margin | :alpha | :delta0 | :alternative}}
Paired non-inferiority test with margin margin (Tango score test).
H0: Δ ≥ margin (the candidate is worse by at least the margin) against
H1: Δ < margin, where Δ = (b − c) / n is the candidate's rate minus the
baseline's. For a guard's false-allow rate, b = new misses and c =
fixes on the same malicious items. The candidate passes
(non_inferior?: true) only if H0 is rejected at one-sided alpha, which
is the same as the upper bound of the two-sided 1 − 2α Tango interval
(:ci) lying below the margin.
Data that are too few to ever pass simply fail; use
Nous.Eval.Stats.Power.noninferiority_n/4 to report the n needed.
Options: :alpha (one-sided, default 0.05).
Examples
iex> {:ok, r} = Nous.Eval.Stats.Hypothesis.noninferiority(0, 0, 61, 0.05)
iex> {r.non_inferior?, Float.round(r.z, 3)}
{true, -1.792}
iex> {:ok, r} = Nous.Eval.Stats.Hypothesis.noninferiority(1, 0, 61, 0.05)
iex> {r.non_inferior?, Float.round(r.z, 3)}
{false, -1.204}
@spec tango_ci(non_neg_integer(), non_neg_integer(), pos_integer(), keyword()) :: {:ok, Nous.Eval.Stats.interval()} | {:error, :empty | :invalid_counts | :invalid_confidence}
Tango's score confidence interval for the paired difference
Δ = (b − c) / n: the set of δ with |Z(δ)| ≤ z_{1−α/2}, found by
bisection. Unlike the Wald interval it stays inside [-1, 1] and behaves
well when b + c is small.
Options: :confidence (default 0.95). Returns {:error, :empty} when
n == 0.
Examples
iex> {:ok, r} = Nous.Eval.Stats.Hypothesis.tango_ci(0, 0, 61)
iex> {r.estimate, r.ci |> elem(1) |> Float.round(4)}
{0.0, 0.0592}
@spec tango_test(non_neg_integer(), non_neg_integer(), pos_integer(), keyword()) :: {:ok, %{ estimate: float(), delta0: float(), alternative: :two_sided | :less | :greater, z: float(), p_value: float() }} | {:error, :empty | :invalid_counts | {:invalid_option, :delta0 | :alternative}}
Tango's score test for the paired difference Δ = (b − c) / n against
delta0.
The statistic is Z(δ₀) = (b − c − n·δ₀) / √(n·(2q̃ + δ₀(1 − δ₀))) where
q̃ is the maximum-likelihood estimate of the c-cell probability under
Δ = δ₀ (closed form, Tango 1998). At δ₀ = 0 it reduces to McNemar's
(b − c)/√(b + c).
Options:
:delta0- null difference in(-1, 1), default0.0:alternative-:two_sided(default),:less(H1: Δ < δ₀) or:greater(H1: Δ > δ₀)
When b + c == 0 and delta0 == 0 the data say nothing about Δ; z is
0.0. Returns {:error, :empty} when n == 0.
Examples
iex> {:ok, r} = Nous.Eval.Stats.Hypothesis.tango_test(9, 4, 50)
iex> {Float.round(r.z, 6), Float.round(r.p_value, 6)}
{1.38675, 0.165518}