Problem 582818 · hard · Level 05 Advanced Algorithms & Graphs

How Many Patients Does the Trial Need?

power · sample size · exact binomial test · type I and II errors · discreteness

A standard treatment cures a fraction p0 of patients. A new one is hoped to cure p1 > p0. With n patients the trial will count the cures X and use the exact one-sided test of "the cure rate is p0" at level alpha: reject it (and declare the new treatment better) when X >= c, where c is the smallest count with P(X >= c) <= alpha for X ~ Binomial(n, p0). (If even X >= n is too likely, then c = n + 1 and the test can never reject.)

For that c, the test's size is P(X >= c) when the cure rate is p0 (its real type I error rate, at most alpha), and its power is P(X >= c) when the cure rate is p1.

Write plan_trial(p0, p1, alpha, target, n_max) that considers every n from 1 to n_max and returns a dict:

  • "first_n": the smallest n whose power is >= target, or None,
  • "c", "size", "power": the test's c, size and power at first_n (all None if there is no such n),
  • "stable_n": the smallest n such that every sample size from n up to n_max has power >= target, or None if n_max itself falls short.

Examples

Input:  p0 = 0.5, p1 = 0.8, alpha = 0.05, target = 0.8, n_max = 30
Output: {"first_n": 18, "c": 13, "size": 0.048126220703125, "power": 0.8670836657571759, "stable_n": 18}
Explanation: with 18 patients, 13 or more cures happen with probability 0.048 under p0,
and with probability 0.867 if the new treatment cures 80%.

Input:  p0 = 0.2, p1 = 0.35, alpha = 0.05, target = 0.8, n_max = 200
Output: {"first_n": 56, "c": 17, "size": 0.04320939968635351, "power": 0.806415503536769, "stable_n": 59}
Explanation: 56 patients reach 80% power, but 57 and 58 patients fall below it again.

Constraints

  • 0 < p0 < p1 < 1, 0 < alpha < 0.5, 0 < target < 1, 1 <= n_max <= 500
  • floats are compared with a tolerance of 1e-6
  • the whole call must finish well within half a second

Goals

  • Find the rejection region of an exact one-sided binomial test for every sample size
  • Compute the exact size and power of the test
  • Plan a sample size, and discover that power does not always rise with n
Starting Python…