Nous.Eval.Stats.Power (nous v0.21.1)

Copy Markdown View Source

Power, sample size and minimum detectable effect (MDE) for eval comparisons, all from normal approximations.

: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

mcnemar_n(p_b, p_c, opts \\ [])

@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}

mcnemar_power(p_b, p_c, n, opts \\ [])

@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

mde(n, variance, opts \\ [])

@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

noninferiority_n(p_b, p_c, margin, opts \\ [])

@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}

noninferiority_power(p_b, p_c, margin, n, opts \\ [])

@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).

sample_size(delta, variance, opts \\ [])

@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}

two_proportion_n(p1, p2, opts \\ [])

@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}

two_proportion_power(p1, p2, n, opts \\ [])

@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

variance_components(candidate, baseline)

@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 is p̂(1−p̂)·k/(k−1))
  • omega2 - between-case variance of the true difference, Var(d̄) − σ_A²/k_A − σ_B²/k_B, floored at 0
  • k_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'.