Clopper-Pearson intervals can be used to calculate the Confidence Intervals of Bernoulli trials, particularly if $np < 5$ or $n(1-p) < 5$ (with $n$ being the number of trials and $p$ the chance of success).
## In Excel
For the lower bound:
```
BETA.INV($ALPHA / 2, $X, $N - $X + 1)
```
And for the upper bound:
```
BETA.INV(1 - $ALPHA / 2, $X + 1, $N - $X)
```
- `$ALPHA` is the significance, typically 0.05
- `$X` is the number of successful trials
- `$N` is the total number of trial
## In Python
```python
import numpy as np
from scipy.stats import beta
def clopper_pearson_interval(n_trials, successes, confint=0.95):
quant = (1 - confint) / 2.
low = beta.ppf(quant, n_trials, successes - n_trials + 1)
high = beta.ppf(1 - quant, n_trials + 1, successes - n_trials)
return (np.nan_to_num(low), np.where(np.isnan(high), 1, high))
```