Skip to content

Commit e26ddf6

Browse files
committed
math_background docs
1 parent 12fb18e commit e26ddf6

10 files changed

+460
-1
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

binomial_cis.egg-info/PKG-INFO

Lines changed: 253 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,262 @@
11
Metadata-Version: 2.1
22
Name: binomial_cis
3-
Version: 0.0.10
3+
Version: 0.0.11
44
Summary: Confidence intervals for binomial distributions.
55
Author: Joe Vincent
6+
Description-Content-Type: text/markdown
67
License-File: LICENSE
78
Requires-Dist: numpy
89
Requires-Dist: numba
910
Requires-Dist: scipy
1011
Requires-Dist: matplotlib
12+
13+
# binomial_cis
14+
This package computes confidence intervals for the probability of success parameter, $p$, of a binomial distribution.
15+
The confidence intervals computed by this package cover $p$ with exactly the user-specified probability and have minimal excess length.
16+
17+
18+
## Installation
19+
Install the package with pip:
20+
```
21+
pip install git+https://github.com/TRI-ML/binomial_cis.git
22+
```
23+
24+
25+
26+
## What is a binomial confidence interval?
27+
The binomial distribution represents the likelihood of observing $k$ successes in $n$ trials where the probability of success for each trial is $p$. One often does not know the true value of $p$ and wishes to estimate this value. After observing $k$ successes in $n$ trials with unknown probability of success $p$, a confidence interval (CI) is constructed in such a way that it contains the true value of $p$ with some high probability.
28+
29+
In constructing confidence intervals one has to tradeoff between three quantities:
30+
1. Confidence: The probability that the CI contains the true parameter. Often written as $1-\alpha$ where $\alpha$ is small.
31+
2. Volume: The length of the CI. If the CI is constructed as a lower bound on $p$ (i.e. $[\underline{p}, 1]$), then we care about the length of the CI which is below $p$. This is known as the *shortage*.
32+
3. Number of samples: In general, with more samples one can construct CIs with higher confidence and smaller volume.
33+
34+
35+
36+
37+
## Why does this package exist?
38+
Existing implementations of binomial CIs fail to optimally control the tradeoffs between coverage, volume, and number of samples. What this means in practice is that if a user specifies $k$ and $n$, existing implementations return CIs with more/less coverage than desired and/or CIs with higher volume than necessary.
39+
40+
Existing software implementations for binomial CIs include:
41+
- [statsmodels.stats.proportion.proportion_confint](https://www.statsmodels.org/devel/generated/statsmodels.stats.proportion.proportion_confint.html) (Python)
42+
- [scipy.stats._result_classes.BinomTestResult.proportion_ci](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats._result_classes.BinomTestResult.proportion_ci.html) (Python)
43+
- [astropy.stats.binom_conf_interval](https://docs.astropy.org/en/stable/api/astropy.stats.binom_conf_interval.html) (Python)
44+
- [scipy.stats.binom](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.binom.html) (Python)
45+
- [EBCIC](https://github.com/KazKobara/ebcic) (Python)
46+
- [binom.test](https://stat.ethz.ch/R-manual/R-devel/library/stats/html/binom.test.html) (R)
47+
- [binom.confint](https://cran.r-project.org/web/packages/binom/binom.pdf) (R)
48+
- [BinomCI](https://search.r-project.org/CRAN/refmans/DescTools/html/BinomCI.html) (R)
49+
- [HypothesisTests.jl](https://juliastats.org/HypothesisTests.jl/stable/methods/#Confidence-interval) (Julia)
50+
- [RobustStats.jl](https://github.com/mrxiaohe/RobustStats.jl/tree/67cec0ea9ac9c4c8a3fe8f2a4e93cadbe68b1d77) (Julia)
51+
- [ClinicalTrialUtilities.jl](https://docs.juliahub.com/ClinicalTrialUtilities/lEJUs/0.3.1/) (Julia)
52+
- [binofit](https://www.mathworks.com/help/stats/binofit.html) (Matlab)
53+
54+
The methods these packages implement include:
55+
- Wald/Normal [1]
56+
- Agresti-Coull [1]
57+
- Clopper-Pearson (*) [1]
58+
- Wilson [1]
59+
- Modified Wilson [1]
60+
- Wilson with continuity correction (*) [1]
61+
- Jeffreys [1]
62+
- Bayesian uniform prior
63+
- Inverting the binomial test (*) [3]
64+
- Arcsine [1]
65+
- Logit [1]
66+
- Probit [8]
67+
- Complementary log [8]
68+
- Likelihood (Profile) [1]
69+
- Witting (*) [4]
70+
- Pratt [5]
71+
- Mid-p [7]
72+
- Blaker (*) [6]
73+
- Second-order corrected [2]
74+
75+
The reference of each method points to a survey paper (if possible) rather than the original derivation of the method.
76+
77+
Only the methods marked with (*) are guaranteed to provide at least as much coverage as desired. However, none of these methods provide exactly the coverage desired (Witting might, but the listed reference is not freely available online and is only published in German).
78+
79+
It is also worth noting the [ump](http://CRAN.R-project.org/package=ump) R package. This package implements UMP and UMPU hypothesis tests for the binomial distribution. Such tests could be leveraged to construct UMA and UMAU confidence intervals, but it this doesn't appear to be implemented based on the documented functions. In addition, in the documentation the authors of this package note that their implemetation has issues with numerical stability.
80+
81+
Existing software implementations for computing binomial CIs with exact coverage are then either non-existent or unsatisfactory. Unsurprisingly then, there is also no open-source implementation of computing the expected shortage (or expected excess or expected width) of UMA and UMAU CIs, and no implementation for computing the worst-case values of these quantities. This package exists to fill this gap.
82+
83+
84+
85+
86+
87+
88+
89+
90+
91+
## **What exactly does this package do?**
92+
This package constructs optimal confidence intervals for the probability of success parameter of a binomial distribution.
93+
94+
#### **Lower Bounds**
95+
Given user specified miscoverage rate ($\alpha$) and maximum expected shortage ($\text{MES}$), return a lower bound on $p$ that satisfies the following requirements:
96+
1. achieves exact desired coverage: $\mathbb{P}[\underline{p} \le p] = 1-\alpha$
97+
2. $[\underline{p}, 1]$ is uniformly most accurate
98+
3. achieves exact desired maximum expected shortage: $\max_p \ \mathbb{E}_p[\max (p - \underline{p}, 0)] = \text{MES}$
99+
4. uses the minimum number of samples $n$ to achieve requirements 1,2,3.
100+
101+
102+
#### **Upper Bounds**
103+
Given user specified miscoverage rate ($\alpha$) and maximum expected excess ($\text{MEE}$), return an upper bound on $p$ that satisfies the following requirements:
104+
1. achieves exact desired coverage: $\mathbb{P}[p \le \overline{p}] = 1-\alpha$
105+
2. $[0, \overline{p}]$ is uniformly most accurate
106+
3. achieves exact desired maximum expected excess: $\max_p \ \mathbb{E}_p[\max (\overline{p} - p, 0)] = \text{MEE}$
107+
4. uses the minimum number of samples $n$ to achieve requirements 1,2,3.
108+
109+
110+
111+
112+
#### **Simultaneous Lower and Upper Bounds**
113+
Given the user specified miscoverage rate ($\alpha$) and maximum expected width ($\text{MEW}$), return simultaneous lower and upper bounds on $p$ that satisfy the following requirements:
114+
1. achieves exact desired coverage: $\mathbb{P}[\underline{p} \le p \le \overline{p}] = 1-\alpha$
115+
2. $[\underline{p}, \overline{p}]$ is uniformly most accurate unbiased
116+
3. achieves exact desired maximum expected width: $\max_p \ \mathbb{E}_p[\overline{p} - \underline{p}] = \text{MEW}$
117+
4. uses the minimum number of samples $n$ to achieve requirements 1,2,3.
118+
119+
120+
## **How do I use this package?**
121+
### Lower Bounds
122+
123+
Find a lower bound on $p$:
124+
```
125+
from binomial_cis import binom_ci
126+
127+
k = 5 # number of successes
128+
n = 10 # number of trials
129+
alpha = 0.05 # miscoverage probability
130+
131+
lb = binom_ci(k, n, alpha, 'lb')
132+
133+
```
134+
135+
Find maximum expected shortage given miscoverage rate and number of samples:
136+
```
137+
mes_ub, mes_lb, p_lb, num_iters = max_expected_shortage(alpha, n, tol=1e-3)
138+
```
139+
140+
141+
### Upper Bounds
142+
143+
Find an upper bound on $p$:
144+
```
145+
from binomial_cis import binom_ci
146+
147+
k = 5 # number of successes
148+
n = 10 # number of trials
149+
alpha = 0.05 # miscoverage probability
150+
151+
ub = binom_ci(k, n, alpha, 'ub')
152+
153+
```
154+
155+
Find maximum expected excess given miscoverage rate and number of samples:
156+
```
157+
mee_ub, mee_lb, p_lb, num_iters = max_expected_excess(alpha, n, tol=1e-3)
158+
```
159+
160+
161+
### 2-Sided Bounds
162+
163+
Find simultaneous lower and upper bounds on $p$:
164+
```
165+
from binomial_cis import binom_ci
166+
167+
k = 5 # number of successes
168+
n = 10 # number of trials
169+
alpha = 0.05 # miscoverage probability
170+
171+
lb, ub = binom_ci(k, n, alpha, 'lb,ub')
172+
173+
```
174+
175+
Find maximum expected width given miscoverage rate and number of samples:
176+
```
177+
mew_ub, mew_lb, p_lb, num_iters = max_expected_width(alpha, n, tol=1e-3)
178+
```
179+
180+
181+
182+
183+
184+
185+
## **Notebooks**
186+
The `notebooks/` directory has the following notebooks:
187+
- `tradeoff_table.ipynb`: Computes MES vs miscoverage rate $\alpha$ and number of samples $n$. Precomputed values have been stored in `MES_table.csv` which is visualized in a plot from the last cell of the notebook.
188+
- `visualizations.ipynb`: Visualizes the mixed-monotonic forms of expected shortage and expected width. Also visualizes how these functions vary with $p$ and their maxima.
189+
190+
The `tests/` directory has the following notebooks:
191+
- `binom_helper_validation.ipynb`: Tests our implementation of the binomial coefficient, binomial pmf, and binomial cdf against their SciPy counterparts.
192+
- `conf_set_validation.ipynb`: Tests the theoretical guarantees of the CIs (coverage, shortage, excess, width, unbiasedness) using Monte-Carlo simulation.
193+
194+
195+
## Automated Tests
196+
Not implemented yet, but future automated tests may include
197+
- validating binomial_helper.py
198+
- comparing the bounds with those in the appendix of [Table of Neyman-shortest unbiased confidence intervals for the binomial parameter](https://www.jstor.org/stable/2333308) by Blyth and Hutchinson
199+
200+
201+
## **Caution!**
202+
#### **Randomized CIs**
203+
The methods used in this package to construct CIs are based on the inversion of randomized hypothesis tests. This means that calling `binom_ci()` with the same `k,n,alpha` will result in different CIs. For the guarantees of the CI to hold it is critical that the user only construct one CI for the experiment they have. Constructing multiple CIs and choosing the best one invalidates the guarantees of the CI.
204+
205+
For the 1-sided bounds there is the option to get less efficient but non-randomized CIs:
206+
```
207+
lb = binom_ci(k, n, alpha, 'lb', randomized=False)
208+
ub = binom_ci(k, n, alpha, 'ub', randomized=False)
209+
```
210+
211+
These non-randomized 1-sided bounds are equivalent to 1-sided Clopper-Pearson bounds. We currently don't have an implementation of non-randomized 2-sided bounds.
212+
213+
Randomization allows the CIs to be UMA. Although randomization has been a point of debate amongst statisticians, we take the view (first given by Mark Eudey) that insofar as construction of confidence intervals can be treated as a (von Neumann) game, randomization merely allows the statistician to employ a mixed strategy.
214+
215+
216+
217+
#### **Multiple Tests**
218+
As with all CIs one must take special care when interpreting the results of multiple CIs constructed from independent tests. If one constructs $m$ CIs where the probability of each CI containing the true parameter is $1-\alpha$, then the probability that **all** $m$ CIs contain their respective parameters is less than $1-\alpha$. For more explanation, see the [Wikipedia article](https://en.wikipedia.org/wiki/Multiple_comparisons_problem) on the multiple comparisons problem.
219+
220+
221+
222+
## Building the Package
223+
Activate the virtual environment and run
224+
```
225+
python -m build
226+
```
227+
You will need the `build` package for this.
228+
229+
230+
231+
232+
233+
## Relevant Literature
234+
Below are some of the papers that we found most useful for understanding binomial confidence intervals.
235+
- [Testing Statistical Hypotheses](https://link.springer.com/book/10.1007/978-3-030-70578-7) by Lehmann and Romano
236+
- [On the treatment of discontinuous random variables](https://search.library.berkeley.edu/permalink/01UCS_BER/s4lks2/cdi_proquest_journals_301822201) by Eudey
237+
- [Table of Neyman-shortest unbiased confidence intervals for the binomial parameter](https://www.jstor.org/stable/2333308) by Blyth and Hutchinson
238+
- [Length of Confidence Intervals](https://www.tandfonline.com/doi/abs/10.1080/01621459.1961.10480644) by Pratt
239+
- [More on length of confidence intervals](https://www.tandfonline.com/doi/abs/10.1080/01621459.1962.10500547) by Madansky
240+
- [Binomial confidence intervals](https://www.tandfonline.com/doi/abs/10.1080/01621459.1983.10477938) by Blyth and Still
241+
- [Smallest confidence intervals for one binomial proportion](https://www.sciencedirect.com/science/article/pii/S0378375805002430) by Wang
242+
- [Fuzzy and randomized confidence intervals and p-values](https://www.jstor.org/stable/20061193) by Geyer and Meeden
243+
- [Nonoptimality of Randomized Confidence Sets](https://www.stat.purdue.edu/docs/research/tech-reports/1988/tr88-09.pdf) by Casella and Robert
244+
245+
246+
247+
248+
249+
250+
251+
252+
253+
## **References**
254+
1. [*Interval Estimation for a Binomial Proportion*](https://www.jstor.org/stable/2676784) by Brown, Cai, and DasGupta
255+
2. [*One-sided confidence intervals in discrete distributions*](https://www.sciencedirect.com/science/article/pii/S0378375804000679) by Cai
256+
3. [*Some Remarks on Confidence or Fiducial Limits*](https://www.jstor.org/stable/2333026) by Sterne
257+
4. [*Mathematische Statistik I.*](https://link.springer.com/book/10.1007/978-3-322-90150-7) by Witting
258+
5. [*Binomial Confidence Intervals*](https://www.jstor.org/stable/2287116) by Blyth and Still
259+
6. [*Confidence Curves and Improved Exact Confidence Intervals for Discrete Distributions*](https://www.jstor.org/stable/3315916) by Blaker
260+
7. [*Comment: Randomized Confidence Intervals and the Mid-P Approach*](https://www.semanticscholar.org/paper/Comment%3A-Randomized-Confidence-Intervals-and-the-Agresti-Gottard/596fd2a98486f65599b56ac81aef32e5c1a0d586) by Agresti and Gottard
261+
8. [*binom*](https://cran.r-project.org/web/packages/binom/binom.pdf) by Sundar Dorai-Raj
262+
9. [*Fuzzy and randomized confidence intervals and p-values*](https://www.jstor.org/stable/20061193) by Geyer and Meeden.
23 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
746 Bytes
Binary file not shown.
1.18 KB
Binary file not shown.

0 commit comments

Comments
 (0)