Power, sample size and minimum detectable effect (MDE) for eval comparisons, all from normal approximations.
mde/3,sample_size/3,variance_components/2- paired comparison of per-case scores with k samples per case (Miller 2024, "Adding Error Bars to Evals", eq. 9–10). Reporters print the MDE of a run so a "no significant change" is read against what the run could detect.two_proportion_power/4,two_proportion_n/3- two independent proportions (Cohen's effect size h = 2·asin√p₁ − 2·asin√p₂).mcnemar_power/4,mcnemar_n/3- paired proportions, McNemar test (Connor 1987, Biometrics 43:207–211).noninferiority_power/5,noninferiority_n/4- the paired Tango non-inferiority test ofNous.Eval.Stats.Hypothesis.noninferiority/5(Nam 1997, Biometrics 53:1422–1430).
:alpha is two-sided for the difference tests and one-sided for
non-inferiority, default 0.05; :power defaults to 0.8. Sample sizes
are the smallest integer reaching the requested power.
Summary
Functions
Pairs needed for mcnemar_power/4 to reach :power (Connor 1987):
n = ⌈(z_{1−α/2}·√ψ + z_{power}·√(ψ − δ²))² / δ²⌉ with δ = p_b − p_c.
Power of the two-sided McNemar test over n pairs (Connor 1987), where
p_b and p_c are the probabilities of the two discordant outcomes (see
Nous.Eval.Stats.Hypothesis for the orientation)
Minimum detectable effect of a paired comparison over n cases (Miller
eq. 10): δ = (z_{1−α/2} + z_{power}) · √(variance / n).
Pairs needed for the paired non-inferiority test to reach :power:
n = ⌈((z_{1−α}·w̃ + z_{power}·w) / (M − δ))²⌉ (see
noninferiority_power/5).
Power of the paired non-inferiority test
(Nous.Eval.Stats.Hypothesis.noninferiority/5) over n pairs, for true
discordant probabilities p_b (candidate has the event, baseline not) and
p_c (the reverse), so the true difference is δ = p_b − p_c
Cases needed to detect a paired difference delta (Miller eq. 9):
n = ⌈(z_{1−α/2} + z_{power})² · variance / δ²⌉. See mde/3 for
variance.
Cases per group for two_proportion_power/4 to reach :power
(default 0.8).
Power of the two-sided test comparing two independent proportions with
n cases per group, using Cohen's h (matches statsmodels
NormalIndPower with proportion_effectsize).
Estimates the variance components of a paired comparison from per-case
sample scores, for planning n and k with mde/3 / sample_size/3.
Functions
@spec mcnemar_n(number(), number(), keyword()) :: {:ok, pos_integer()} | {:error, :no_effect | {:invalid_option, :alpha | :power | :p_b | :p_c}}
Pairs needed for mcnemar_power/4 to reach :power (Connor 1987):
n = ⌈(z_{1−α/2}·√ψ + z_{power}·√(ψ − δ²))² / δ²⌉ with δ = p_b − p_c.
Examples
iex> Nous.Eval.Stats.Power.mcnemar_n(0.15, 0.05)
{:ok, 155}
@spec mcnemar_power(number(), number(), pos_integer(), keyword()) :: {:ok, float()} | {:error, :empty | {:invalid_option, :n | :alpha | :p_b | :p_c}}
Power of the two-sided McNemar test over n pairs (Connor 1987), where
p_b and p_c are the probabilities of the two discordant outcomes (see
Nous.Eval.Stats.Hypothesis for the orientation):
Φ((|p_b − p_c|·√n − z_{1−α/2}·√ψ) / √(ψ − (p_b − p_c)²)), ψ = p_b + p_c.
With no discordance at all (ψ == 0) nothing can be detected and the
power is 0.0; if ψ == (p_b − p_c)² (every pair discordant one way) the
outcome is deterministic and the power is 0.0 or 1.0.
Options: :alpha (two-sided, default 0.05).
Examples
iex> {:ok, p} = Nous.Eval.Stats.Power.mcnemar_power(0.15, 0.05, 150)
iex> Float.round(p, 3)
0.788
@spec mde(pos_integer(), number(), keyword()) :: {:ok, float()} | {:error, :empty | :invalid_values | {:invalid_option, :n | :alpha | :power}}
Minimum detectable effect of a paired comparison over n cases (Miller
eq. 10): δ = (z_{1−α/2} + z_{power}) · √(variance / n).
variance is the variance of the per-case difference d̄_i at the
planned k. From a finished comparison it is the sample variance of the
per-case differences (describe/1 on them); for planning at other k use
variance_components/2 and ω² + σ_A²/k_A + σ_B²/k_B.
Options: :alpha (two-sided, default 0.05), :power (default 0.8).
Examples
# Miller's illustrative ω² = 1/9, σ² = 1/6, k = 3 ⇒ variance 2/9
iex> {:ok, mde} = Nous.Eval.Stats.Power.mde(100, 2 / 9)
iex> Float.round(mde, 3)
0.132
@spec noninferiority_n(number(), number(), number(), keyword()) :: {:ok, pos_integer()} | {:error, :effect_not_below_margin | {:invalid_option, :alpha | :power | :margin | :p_b | :p_c}}
Pairs needed for the paired non-inferiority test to reach :power:
n = ⌈((z_{1−α}·w̃ + z_{power}·w) / (M − δ))²⌉ (see
noninferiority_power/5).
With identical behaviour (p_b = p_c = 0) this is the smallest n that can
certify the margin at all, z²(1 − M)/M: 52 at M = 0.05, 133 at 0.02 and
268 at 0.01 (α = 0.05). A guard dataset smaller than that "cannot
certify; n ≥ X needed" and must fail closed.
Options: :alpha (one-sided, default 0.05), :power (default 0.8).
Examples
iex> Nous.Eval.Stats.Power.noninferiority_n(0.0, 0.0, 0.02)
{:ok, 133}
@spec noninferiority_power(number(), number(), number(), pos_integer(), keyword()) :: {:ok, float()} | {:error, :empty | :effect_not_below_margin | {:invalid_option, :n | :alpha | :margin | :p_b | :p_c}}
Power of the paired non-inferiority test
(Nous.Eval.Stats.Hypothesis.noninferiority/5) over n pairs, for true
discordant probabilities p_b (candidate has the event, baseline not) and
p_c (the reverse), so the true difference is δ = p_b − p_c:
Φ((√n·(M − δ) − z_{1−α}·w̃) / w) with w² = p_b + p_c − δ² and w̃²
the variance at the restricted estimate under Δ = M (Nam 1997).
Requires δ < margin. With no discordance (w == 0) the outcome is
deterministic: 1.0 once n is large enough, else 0.0.
Options: :alpha (one-sided, default 0.05).
@spec sample_size(number(), number(), keyword()) :: {:ok, pos_integer()} | {:error, :no_effect | :invalid_values | {:invalid_option, :alpha | :power}}
Cases needed to detect a paired difference delta (Miller eq. 9):
n = ⌈(z_{1−α/2} + z_{power})² · variance / δ²⌉. See mde/3 for
variance.
Examples
iex> Nous.Eval.Stats.Power.sample_size(0.10, 2 / 9)
{:ok, 175}
@spec two_proportion_n(number(), number(), keyword()) :: {:ok, pos_integer()} | {:error, :no_effect | {:invalid_option, :alpha | :power | :p1 | :p2}}
Cases per group for two_proportion_power/4 to reach :power
(default 0.8).
Examples
iex> Nous.Eval.Stats.Power.two_proportion_n(0.6, 0.7)
{:ok, 356}
@spec two_proportion_power(number(), number(), pos_integer(), keyword()) :: {:ok, float()} | {:error, :empty | {:invalid_option, :n | :alpha | :p1 | :p2}}
Power of the two-sided test comparing two independent proportions with
n cases per group, using Cohen's h (matches statsmodels
NormalIndPower with proportion_effectsize).
Options: :alpha (two-sided, default 0.05).
Examples
iex> {:ok, p} = Nous.Eval.Stats.Power.two_proportion_power(0.6, 0.7, 356)
iex> Float.round(p, 3)
0.801
@spec variance_components([[number()]], [[number()]]) :: {:ok, %{ omega2: float(), sigma2_a: float(), sigma2_b: float(), k_a: float(), k_b: float(), n: pos_integer() }} | {:error, :length_mismatch | :insufficient_data | :invalid_values}
Estimates the variance components of a paired comparison from per-case
sample scores, for planning n and k with mde/3 / sample_size/3.
candidate and baseline are lists (same case order) of per-case lists
of sample scores; every case needs at least two samples per arm.
sigma2_a,sigma2_b- mean within-case sample variance per arm (unbiased for E[σᵢ²]; for binary scores it isp̂(1−p̂)·k/(k−1))omega2- between-case variance of the true difference,Var(d̄) − σ_A²/k_A − σ_B²/k_B, floored at 0k_a,k_b- mean samples per case per arm
The variance at other sample counts is
omega2 + sigma2_a / k_a' + sigma2_b / k_b'.