Binomial Proportion Intervals¶
See the fixed-sample guide for assumptions, precision planning and optional indicator summary metadata.
trade_study.ProportionInterval(k, n, estimate, low, high, confidence, method)
dataclass
¶
Two-sided, marginal interval for a fixed-sample Bernoulli probability.
Wilson coverage is approximate; Clopper-Pearson is conservative under independent identically distributed Bernoulli sampling at a fixed n. Neither method supplies a sequential or post-selection guarantee.
Attributes:
| Name | Type | Description |
|---|---|---|
k |
int
|
Number of successes. |
n |
int
|
Number of trials. |
estimate |
float
|
Observed proportion k / n, not the Wilson interval center. |
low |
float
|
Lower probability bound. |
high |
float
|
Upper probability bound. |
confidence |
float
|
Nominal two-sided confidence level. |
method |
str
|
Wilson without continuity correction, or Clopper-Pearson. |
trade_study.proportion_interval(k, n, *, confidence=0.95, method='wilson')
¶
Estimate a binomial proportion and its two-sided fixed-sample interval.
Trials must be independent Bernoulli outcomes with a common probability and a prespecified sample size. Callers establish these assumptions; counts alone cannot verify them. Intervals are marginal, without a multiplicity adjustment, and are not valid for arbitrary optional stopping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
Integer successes, including NumPy integer scalars; 0 <= k <= n. |
required |
n
|
int
|
Positive integer trial count, at most 2**53 for float64 arithmetic. |
required |
confidence
|
float
|
Nominal level in (0, 1), default 0.95. |
0.95
|
method
|
str
|
"wilson" without continuity correction (core), or "clopper-pearson" using the optional stats extra. |
'wilson'
|
Returns:
| Type | Description |
|---|---|
ProportionInterval
|
Observed estimate and interval with counts and method recorded. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If counts, confidence or method are invalid or unsupported. |
ImportError
|
If Clopper-Pearson is requested without the stats extra. |
Examples:
>>> interval = proportion_interval(8, 10)
>>> interval.estimate, interval.n, interval.method
(0.8, 10, 'wilson')
>>> interval.low < interval.estimate < interval.high
True
Source code in src/trade_study/proportions.py
trade_study.replicates_for_half_width(half_width, *, confidence=0.95)
¶
Plan a fixed trial count from the worst-case Wilson interval half-width.
The largest continuous Wilson half-width occurs at observed proportion 1/2 and equals z / (2 * sqrt(n + z**2)). Inverting this expression gives the smallest positive integer satisfying that conservative bound, even when n is odd. This is interval-precision planning, not power analysis, a guarantee of Wilson coverage, or a rule for stopping after each trial.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
half_width
|
float
|
Target (high - low) / 2 in (0, 0.5]. This is not the largest distance from the observed estimate to a bound. |
required |
confidence
|
float
|
Nominal two-sided level in (0, 1). |
0.95
|
Returns:
| Type | Description |
|---|---|
int
|
Planned integer n, at most 2**53. Fix n before collecting outcomes. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If inputs are invalid or the count exceeds float64 support. |
Examples:
>>> replicates_for_half_width(0.05)
381
>>> replicates_for_half_width(0.10) < replicates_for_half_width(0.05)
True