Rare-Event Planning¶
Rare outcomes are estimated from few events, so precision depends on how many
events or positives are observed, not on how many people are enrolled or
tested. These functions plan follow-up and testing effort: exact Poisson rate
intervals over person-time, the number of tests or person-time needed to
observe a number of positives with a stated assurance, and the expected
relative precision of a proportion at a planned sample size. See the
planning guide for a worked example, and
person_time_for_events to convert a trial's event target into
person-time.
Intervals assume independent events at a common rate or probability.
Clustering and heterogeneity between sites or participants widen the true
uncertainty; assess them by simulation with run_grid.
All functions require SciPy (trade-study[stats]).
trade_study.RateInterval(events, person_time, estimate, low, high, confidence)
dataclass
¶
Exact two-sided interval for a Poisson rate per unit of person-time.
Attributes:
| Name | Type | Description |
|---|---|---|
events |
int
|
Observed events. |
person_time |
float
|
Total person-time at risk. |
estimate |
float
|
|
low |
float
|
Lower bound (0 when no events were observed). |
high |
float
|
Upper bound. |
confidence |
float
|
Nominal two-sided level. |
trade_study.rate_interval(events, person_time, *, confidence=0.95)
¶
Estimate an event rate with the exact Garwood interval.
The bounds are chi-square quantiles: chi2(a/2, 2k) / 2T and
chi2(1 - a/2, 2k + 2) / 2T, computed through the inverse regularized
incomplete gamma function. Events must follow a Poisson process with a
common rate over the person-time; clustering or heterogeneity between
participants widens the true uncertainty beyond this interval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
events
|
int
|
Observed event count. |
required |
person_time
|
float
|
Positive total person-time. |
required |
confidence
|
float
|
Two-sided level in (0, 1). |
0.95
|
Returns:
| Type | Description |
|---|---|
RateInterval
|
Estimate and exact bounds. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the count, person-time or level is invalid. |
Examples:
>>> interval = rate_interval(0, 1000.0)
>>> interval.low, round(interval.high * 1000, 3)
(0.0, 3.689)
Source code in src/trade_study/rates.py
trade_study.trials_for_positives(probability, positives, *, assurance=0.9)
¶
Smallest number tested with at least positives positives at assurance.
The chance P(Binomial(n, p) >= k) rises with n, so the boundary
is found by doubling and bisection. Use this to plan surveillance that
must observe enough positives to describe them (for example to sequence
or to estimate a positivity), not to guarantee interval precision.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
probability
|
float
|
Probability that one tested person is positive, in (0, 1]. |
required |
positives
|
int
|
Required number of positives (0 needs no testing). |
required |
assurance
|
float
|
Required probability in (0, 1). |
0.9
|
Returns:
| Type | Description |
|---|---|
int
|
Number of people to test. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If an argument is invalid or more than 2**53 are needed. |
Examples:
Source code in src/trade_study/rates.py
trade_study.person_time_for_positives(rate, positives, *, assurance=0.9)
¶
Person-time with at least positives Poisson events at assurance.
P(Poisson(rate * T) >= k) equals the regularized lower incomplete
gamma function P(k, rate * T), so the person-time is exact:
T = gammaincinv(k, assurance) / rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate
|
float
|
Positive event rate per unit of person-time. |
required |
positives
|
int
|
Required number of events. |
required |
assurance
|
float
|
Required probability in (0, 1). |
0.9
|
Returns:
| Type | Description |
|---|---|
float
|
Person-time (0 when no events are required). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If an argument is invalid. |
Examples:
Source code in src/trade_study/rates.py
trade_study.expected_relative_half_width(n, probability, *, method='clopper-pearson', confidence=0.95)
¶
Expected interval half-width relative to the true probability at n.
The half-width (high - low) / 2 is averaged over the binomial
distribution of the number of positives, including outcomes with none,
rather than evaluated at the expected count. Outcomes outside the central
1 - 1e-12 binomial mass are omitted and the remaining weights
renormalized.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Planned number tested (positive). |
required |
probability
|
float
|
True probability in (0, 1). |
required |
method
|
str
|
|
'clopper-pearson'
|
confidence
|
float
|
Two-sided level in (0, 1). |
0.95
|
Returns:
| Type | Description |
|---|---|
float
|
Expected half-width divided by |
Raises:
| Type | Description |
|---|---|
ValueError
|
If an argument is invalid. |
Examples:
>>> wide = expected_relative_half_width(1000, 0.01)
>>> narrow = expected_relative_half_width(10000, 0.01)
>>> narrow < wide
True
Source code in src/trade_study/rates.py
trade_study.trials_for_relative_precision(probability, relative_half_width, *, method='clopper-pearson', confidence=0.95)
¶
Number tested whose expected relative half-width meets a target.
The expectation of :func:expected_relative_half_width falls with n
almost monotonically; the boundary is found by doubling and bisection,
and the returned n satisfies the target. Because the expectation is
not exactly monotone, an adjacent smaller n can also satisfy it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
probability
|
float
|
True probability in (0, 1). |
required |
relative_half_width
|
float
|
Target half-width divided by the probability. |
required |
method
|
str
|
|
'clopper-pearson'
|
confidence
|
float
|
Two-sided level in (0, 1). |
0.95
|
Returns:
| Type | Description |
|---|---|
int
|
Planned number tested. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If an argument is invalid or more than 2**53 are needed. |
Examples:
>>> n = trials_for_relative_precision(0.01, 0.5)
>>> expected_relative_half_width(n, 0.01) <= 0.5
True