Techniqueadvanced
Alpha spending function
Also called Lan-DeMets alpha spending, error spending function.
Lan and DeMets's rule that spends the type I error as a function of the information fraction, so the timing and number of interim looks need not be fixed in advance.
An alpha spending function describes how a group sequential design uses its type I error over the course of a study. It maps the information fraction , the share of the planned maximum information observed so far, to the error spent up to that point, with and . When an analysis happens at fraction , its critical value is chosen so that the probability under of crossing first at this analysis is exactly . Lan and DeMets introduced the method in 1983. Its value is flexibility: the number and the times of the analyses need not be fixed in advance, and the overall type I error is still , as long as the times do not depend on the observed effect. The choice of sets the shape: some functions give boundaries close to the O'Brien-Fleming boundary, others give boundaries close to Pocock's.
In plain words
Think of the allowed false positive rate as a budget, for example 5%. Each look at the data spends some of it. A spending function is a plan, written before the study, that says how much of the budget may be used by each point of the study, measured by the share of the data collected. Early looks spend little, so they need very strong evidence to stop. When a look actually happens, you check how far the study is, see how much budget the plan allows by then, and spend the difference since the last look. You can look more often or less often than you expected. As long as the plan stays the same, the total spent cannot exceed the budget.
When it applies
- A study with interim analyses whose number or timing is not known in advance, for example because a data monitoring committee meets on calendar dates while patients enter the trial at an uneven rate.
- The test statistics have the canonical joint distribution of a group sequential design: the score , on the information fraction scale, is a Brownian motion with drift observed at the times .
- The maximum information can be planned, so the information fraction is known at each analysis.
- The times are chosen without looking at the estimated effect. They may depend on calendar time, on recruitment, or on the previous decisions to continue.
The template
Fix the spending function and the two-sided level . Under , has independent increments . Let and . At analysis the procedure needs only .
SPENDING-BOUNDARIES(f, t_1 < t_2 < ... < t_K) ▷ 0 < t_1, t_K ≤ 1, f increasing- 1
c_1 ← z_{1 - f(t_1)/2}; g ← density of N(0, t_1), restricted to |s| < c_1·sqrt(t_1) - 2
for k ← 2 to K do - 3
π_k ← f(t_k) - f(t_{k-1}) ▷ the error to spend now - 4
c_k ← the root c of ∫ g(u)[Φ̄((c·sqrt(t_k) - u)/σ_k) + Φ((-c·sqrt(t_k) - u)/σ_k)] du = π_k, by bisection - 5
g ← ∫ g(u) φ((s - u)/σ_k)/σ_k du, restricted to |s| < c_k·sqrt(t_k) - 6
return c_1, ..., c_K
The function in line L1 and line L5 is the sub-density of on the event that the boundary was not crossed at analyses . Its total mass is the probability of reaching analysis . The integral in line L4 is the probability of crossing first at analysis with critical value ; it is the recursion of Armitage, McPherson and Rowe (1969) from the group sequential design page. The implementation evaluates the integrals by Simpson's rule on a grid (numerical integration) and solves line L4 by bisection.
Why the total is . Let be the event that the test crosses first at analysis . Line L1 gives . Line L4 gives for . The events are disjoint, so
with equality when the last analysis is at . Nothing in this sum depends on or on the values ; that is the whole point of the method.
Why line L4 has exactly one root. Let be the total mass of before line L4, the probability of reaching analysis . The crossing probability in line L4 is continuous and strictly decreasing in , from at to 0 as . Since when , there is exactly one root. If , no error is left to spend and : the analysis cannot stop the study.
Times that depend on the past. The times may be chosen after the earlier analyses, as long as the choice uses only information that is independent of the effect estimates, or only the decisions to continue. The argument above conditions on the times, so it still holds. If the timing reacts to the observed effect, this argument does not apply and the level is no longer guaranteed.
Cost. With grid points per analysis and bisection steps, each analysis costs for line L5 and for line L4, so the whole set costs time and space.
Example: five equally spaced analyses, two-sided. Lan and DeMets's O'Brien-Fleming type function is , which equals at . Its cumulative spending at is , , , and . The boundaries from the template are:
| spending function | |||||
|---|---|---|---|---|---|
| O'Brien-Fleming type | 4.3826 | 3.0997 | 2.5534 | 2.2538 | 2.0635 |
| same shape spent on each tail with | 4.8769 | 3.3570 | 2.6803 | 2.2898 | 2.0310 |
| Pocock type | 2.4380 | 2.4268 | 2.4102 | 2.3966 | 2.3860 |
| power () | 2.5758 | 2.4920 | 2.4108 | 2.3391 | 2.2755 |
| power () | 3.5401 | 2.9743 | 2.6045 | 2.3064 | 2.0455 |
| Hwang-Shih-DeCani, | 3.2527 | 2.9860 | 2.6917 | 2.3737 | 2.0253 |
| Hwang-Shih-DeCani, | 2.4487 | 2.4190 | 2.3984 | 2.3912 | 2.3948 |
For comparison, the exact O'Brien-Fleming boundary is 4.5617, 3.2256, 2.6337, 2.2809, 2.0401, and the exact Pocock boundary is 2.4132 at every analysis. The spending versions are close but not equal.
There are two conventions for the O'Brien-Fleming type function in a two-sided test, and they give different numbers. In the first row, the function is applied to the total two-sided , as in Lan and DeMets (1983). In the second row, which many software packages use, each tail spends with the one-sided function , so the total is . The second convention spends less early and more late. A report must state which one it used.
Example: the analyses happen at other times. The study planned five analyses but the committee met only at , and . With the O'Brien-Fleming type function on the total , the template gives 3.5784, 2.3454 and 2.0238, and the total error is still exactly 0.05.
Classic examples
- Approximating the O'Brien-Fleming design. The function gives boundaries close to the O'Brien-Fleming boundary for equally spaced analyses, and stays valid when the spacing changes. It is the most common choice in clinical trials.
- Approximating the Pocock design. gives nearly constant boundaries close to Pocock's, as the table shows.
- Flexible monitoring in a group sequential design. A data monitoring committee that meets every six months while recruitment speeds up computes each boundary at the meeting, from the information fraction at that time.
- Linear spending and Bonferroni. With and equally spaced analyses, each analysis spends . A Bonferroni correction would also test each analysis at , but the spending boundaries are lower (2.58 down to 2.28 against at every look), because they use the correlation of the statistics. The family-wise error rate is exactly for the spending boundaries and below for Bonferroni.
- Continuous monitoring as a limit. With very many analyses the boundary tends to a curved boundary for Brownian motion. For truly continuous monitoring without a planned maximum, the mixture SPRT and confidence sequences are the matching tools.
Variations
- Families of spending functions. Kim and DeMets (1987) use the power family : is close to Pocock, is close to O'Brien-Fleming. Hwang, Shih and DeCani (1990) use : is close to Pocock, is close to O'Brien-Fleming, and means linear spending.
- Beta spending. A second function spends the type II error and gives a futility boundary. The two boundaries are found together, analysis by analysis.
- One-sided designs. For a one-sided test of level , line L1 uses and line L4 counts only the upper crossing.
- Changing the maximum information. If the final analysis falls above the planned , spend the rest of at the final analysis, setting its fraction to 1. If the study ends early at , it can spend the rest of there instead of only , as long as that decision does not depend on the observed effect.
- Repeated confidence intervals. The boundaries give the intervals , which cover at all analyses together with probability .
Pitfalls
- Choosing after seeing data. The function must be fixed in the protocol. Switching to a less strict function after an interim result inflates the error.
- Timing that reacts to the effect. Calling an extra analysis because the last one looked promising breaks the assumption that the times are independent of the effect estimates.
- Mixing conventions. The two-sided O'Brien-Fleming type function has two conventions, as the example shows. Software defaults differ, so record the convention and the resulting boundaries.
- Information fraction in survival trials. Information is the number of events, not the number of patients. Use the observed number of events divided by the planned number, or the fraction will be wrong.
- Overrunning. Data that arrive after the decision to stop, for example patients already treated, change the final statistic. The plan should say how they enter the final analysis.
Code
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/alpha-spending-function/. The authors are listed in prov/9MKH4V.jsonl.
"""Lan-DeMets alpha spending: two-sided group sequential boundaries from a spending function.
A spending function f maps the information fraction t in [0, 1] to the type I
error spent so far, with f(0) = 0, f(1) = alpha, and f nondecreasing. At
analysis k, with information fraction t_k, the boundary c_k solves
P0(|Z_j| < c_j for j < k, |Z_k| >= c_k) = f(t_k) - f(t_(k-1)).
Z_k is the standardized statistic. The score S_k = Z_k sqrt(t_k) is a standard
Brownian motion at the times t_1 < ... < t_K. The sub-density of S_k on the
continuation event is carried on a Simpson grid (recursive numerical
integration), and c_k is found by bisection. Cost O(K (m^2 + b m)) for m grid
points per stage and b bisection steps.
"""
import math
from collections.abc import Callable, Sequence
_SQRT2 = math.sqrt(2.0)
_INV_SQRT_2PI = 1.0 / math.sqrt(2.0 * math.pi)
def norm_cdf(x: float) -> float:
return 0.5 * math.erfc(-x / _SQRT2)
def norm_sf(x: float) -> float:
return 0.5 * math.erfc(x / _SQRT2)
def norm_pdf(x: float) -> float:
return _INV_SQRT_2PI * math.exp(-0.5 * x * x)
def norm_quantile(p: float) -> float:
"""The lower p-quantile of the standard normal, by bisection to 1e-15."""
if not 0.0 < p < 1.0:
raise ValueError("p must be in (0, 1)")
lo, hi = -40.0, 40.0
for _ in range(200):
mid = 0.5 * (lo + hi)
if norm_cdf(mid) < p:
lo = mid
else:
hi = mid
if hi - lo < 1e-15:
break
return 0.5 * (lo + hi)
def obf_spending(alpha: float) -> Callable[[float], float]:
"""Lan-DeMets O'Brien-Fleming type: f(t) = 2 - 2 Phi(z_(1 - alpha/2) / sqrt(t))."""
zq = norm_quantile(1.0 - alpha / 2.0)
return lambda t: 0.0 if t <= 0.0 else 2.0 * norm_sf(zq / math.sqrt(t))
def pocock_spending(alpha: float) -> Callable[[float], float]:
"""Lan-DeMets Pocock type: f(t) = alpha log(1 + (e - 1) t)."""
return lambda t: alpha * math.log(1.0 + (math.e - 1.0) * t)
def power_spending(alpha: float, rho: float) -> Callable[[float], float]:
"""Kim-DeMets power family: f(t) = alpha t^rho, rho > 0."""
if rho <= 0:
raise ValueError("rho must be positive")
return lambda t: alpha * t ** rho
def hsd_spending(alpha: float, gamma: float) -> Callable[[float], float]:
"""Hwang-Shih-DeCani family: f(t) = alpha (1 - e^(-gamma t)) / (1 - e^(-gamma)); gamma = 0 is linear."""
if gamma == 0:
return lambda t: alpha * t
return lambda t: alpha * (1.0 - math.exp(-gamma * t)) / (1.0 - math.exp(-gamma))
def _simpson_grid(b: float, step: float) -> tuple[list[float], list[float]]:
n = max(2, 2 * math.ceil(b / step))
h = 2.0 * b / n
xs = [-b + i * h for i in range(n + 1)]
ws = [(1.0 if i in (0, n) else (4.0 if i % 2 else 2.0)) * h / 3.0 for i in range(n + 1)]
return xs, ws
def spending_boundaries(spend: Callable[[float], float], times: Sequence[float], r: int = 16,
tol: float = 1e-10) -> list[float]:
"""The z-scale two-sided boundaries c_1, ..., c_K for the spending function spend.
Precondition: 0 < times[0] < ... < times[-1] <= 1, and spend increases
strictly on the times. A zero increment would need c_k = +inf, so it is rejected.
"""
if not times or any(b <= a for a, b in zip(times, times[1:])) or times[0] <= 0:
raise ValueError("times must be positive and increasing")
out = []
t0 = times[0]
inc = spend(t0)
if inc <= 0:
raise ValueError("each stage must spend a positive amount")
c = norm_quantile(1.0 - inc / 2.0) # @line L1
out.append(c)
b = c * math.sqrt(t0)
xs, ws = _simpson_grid(b, math.sqrt(t0) / r)
g = [norm_pdf(x / math.sqrt(t0)) / math.sqrt(t0) for x in xs] # @line L1
for k in range(1, len(times)): # @line L2
target = spend(times[k]) - spend(times[k - 1]) # @line L3
if target <= 0:
raise ValueError("each stage must spend a positive amount")
sd = math.sqrt(times[k] - times[k - 1])
wg = [w * gu for w, gu in zip(ws, g)]
st = math.sqrt(times[k])
def cross(ck: float) -> float:
bk = ck * st
return sum(a * (norm_sf((bk - u) / sd) + norm_cdf((-bk - u) / sd)) for u, a in zip(xs, wg))
lo, hi = 0.0, 40.0
if cross(lo) < target:
raise ValueError("the spending increment is larger than the mass that is still in play")
while hi - lo > tol: # @line L4
mid = 0.5 * (lo + hi)
if cross(mid) > target:
lo = mid
else:
hi = mid
c = 0.5 * (lo + hi) # @line L4
out.append(c)
if k + 1 < len(times):
b = c * st
step = min(sd, math.sqrt(times[k + 1] - times[k])) / r
nxs, nws = _simpson_grid(b, step)
g = [sum(a * norm_pdf((s - u) / sd) for u, a in zip(xs, wg)) / sd for s in nxs] # @line L5
xs, ws = nxs, nws
return out # @line L6
def crossing_by_stage(c: Sequence[float], times: Sequence[float], r: int = 16) -> list[float]:
"""P0 of the first crossing at each analysis, for given z-scale boundaries c."""
out = []
t0 = times[0]
out.append(2.0 * norm_sf(c[0]))
b = c[0] * math.sqrt(t0)
xs, ws = _simpson_grid(b, math.sqrt(t0) / r)
g = [norm_pdf(x / math.sqrt(t0)) / math.sqrt(t0) for x in xs]
for k in range(1, len(times)):
sd = math.sqrt(times[k] - times[k - 1])
bk = c[k] * math.sqrt(times[k])
wg = [w * gu for w, gu in zip(ws, g)]
out.append(sum(a * (norm_sf((bk - u) / sd) + norm_cdf((-bk - u) / sd)) for u, a in zip(xs, wg)))
if k + 1 < len(times):
step = min(sd, math.sqrt(times[k + 1] - times[k])) / r
nxs, nws = _simpson_grid(bk, step)
g = [sum(a * norm_pdf((s - u) / sd) for u, a in zip(xs, wg)) / sd for s in nxs]
xs, ws = nxs, nws
return out
test_alpha_spending_function.py 113 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/alpha-spending-function/. The authors are listed in prov/9MKH4V.jsonl.
import math
import random
import pytest
from alpha_spending_function import (
crossing_by_stage,
hsd_spending,
norm_quantile,
obf_spending,
pocock_spending,
power_spending,
spending_boundaries,
)
TIMES = [0.2, 0.4, 0.6, 0.8, 1.0]
def phi(x: float) -> float:
return math.exp(-0.5 * x * x) / math.sqrt(2.0 * math.pi)
def upper(x: float) -> float:
return 0.5 * math.erfc(x / math.sqrt(2.0))
def second_look_mass(c1: float, c2: float, t1: float, n: int = 400) -> float:
"""P(|Z1| < c1, |Z2| >= c2) by a 1-D Simpson rule, with Z2 | Z1 = z ~ N(sqrt(t1) z, 1 - t1)."""
s, v = math.sqrt(t1), math.sqrt(1.0 - t1)
h = 2.0 * c1 / n
acc = 0.0
for i in range(n + 1):
z = -c1 + i * h
w = 1.0 if i in (0, n) else (4.0 if i % 2 else 2.0)
acc += w * phi(z) * (upper((c2 - s * z) / v) + upper((c2 + s * z) / v))
return acc * h / 3.0
def test_spending_functions_end_at_alpha():
for f in (obf_spending(0.05), pocock_spending(0.05), power_spending(0.05, 2.0), hsd_spending(0.05, -4.0),
hsd_spending(0.05, 0.0)):
assert f(0.0) == pytest.approx(0.0, abs=1e-15)
assert f(1.0) == pytest.approx(0.05, abs=1e-12)
values = [f(t / 20) for t in range(21)]
assert all(a <= b for a, b in zip(values, values[1:]))
def test_worked_example_obf_type():
c = spending_boundaries(obf_spending(0.05), TIMES)
expected = [4.3826, 3.0997, 2.5534, 2.2538, 2.0635]
assert all(abs(a - b) < 1e-4 for a, b in zip(c, expected))
def test_per_tail_convention_gives_the_software_table():
# Spending 2 - 2 Phi(z_(1 - 0.025/2) / sqrt t) in each tail.
g = obf_spending(0.025)
c = spending_boundaries(lambda t: 2.0 * g(t), TIMES)
expected = [4.8769, 3.3570, 2.6803, 2.2898, 2.0310]
assert all(abs(a - b) < 1e-4 for a, b in zip(c, expected))
@pytest.mark.parametrize("f", [obf_spending(0.05), pocock_spending(0.05), power_spending(0.05, 3.0),
hsd_spending(0.05, 1.0)])
def test_each_stage_spends_its_increment(f):
c = spending_boundaries(f, TIMES)
stage = crossing_by_stage(c, TIMES)
prev = 0.0
for t, p in zip(TIMES, stage):
assert abs(p - (f(t) - prev)) < 1e-8
prev = f(t)
assert abs(sum(stage) - 0.05) < 1e-8
def test_one_look_is_fixed_sample():
c = spending_boundaries(power_spending(0.05, 1.0), [1.0])
assert abs(c[0] - norm_quantile(0.975)) < 1e-12
def test_two_looks_against_quadrature():
f = obf_spending(0.05)
c1, c2 = spending_boundaries(f, [0.5, 1.0])
assert abs(c1 - norm_quantile(1.0 - f(0.5) / 2.0)) < 1e-12
assert abs(second_look_mass(c1, c2, 0.5) - (0.05 - f(0.5))) < 1e-7
def test_random_two_look_designs_against_quadrature():
rng = random.Random(1983)
for _ in range(500):
t1 = rng.uniform(0.15, 0.85)
alpha = rng.uniform(0.01, 0.1)
rho = rng.uniform(0.5, 4.0)
f = power_spending(alpha, rho)
c1, c2 = spending_boundaries(f, [t1, 1.0])
# Both sides use Simpson grids; their discretization errors stay below 1e-7.
assert abs(second_look_mass(c1, c2, t1) - (f(1.0) - f(t1))) < 1e-7
def test_unplanned_times_keep_the_level():
f = obf_spending(0.05)
for times in ([0.3, 0.7, 1.0], [0.1, 0.25, 0.5, 0.55, 1.0], [0.9, 1.0]):
c = spending_boundaries(f, times)
assert abs(sum(crossing_by_stage(c, times)) - 0.05) < 1e-8
def test_bad_input():
with pytest.raises(ValueError):
spending_boundaries(obf_spending(0.05), [0.5, 0.5])
with pytest.raises(ValueError):
spending_boundaries(lambda t: 0.0, [0.5, 1.0])
with pytest.raises(ValueError):
power_spending(0.05, 0.0)
Exercises
- Show that is increasing on , tends to 0 as and equals at . For , compute and explain why the first boundary in the example is above 4.
- Prove that the crossing probability in line L4 is strictly decreasing in when has positive mass.
- With and two analyses at and , write the equation for as a bivariate normal probability with correlation .
References
- K. K. Gordon Lan and David L. DeMets, Discrete sequential boundaries for clinical trials, Biometrika 70(3) (1983), pages 659 to 663, the definition of the spending function, the computation of the boundaries from it, and the spending functions that approximate the O'Brien-Fleming and Pocock boundaries
- Kyungmann Kim and David L. DeMets, Design and analysis of group sequential tests based on the type I error spending rate function, Biometrika 74(1) (1987), the power family of spending functions alpha t^rho
- Irving K. Hwang, Weichung J. Shih and John S. De Cani, Group sequential designs using a family of type I error probability spending functions, Statistics in Medicine 9(12) (1990), the one-parameter family alpha (1 - e^(-gamma t)) / (1 - e^(-gamma))
- P. Armitage, C. K. McPherson and B. C. Rowe, Repeated significance tests on accumulating data, Journal of the Royal Statistical Society, Series A 132(2) (1969), the recursive numerical integration of the crossing probabilities
- Christopher Jennison and Bruce W. Turnbull, Group Sequential Methods with Applications to Clinical Trials, Chapman and Hall/CRC, 2000, the chapter on the error spending approach
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag 9MKH4V Edit this page History Report an error Markdown