# Finding the $2\sigma$ confidence interval for a coin flip with unknown $p$

Suppose I have a hypothetical coin flip experiment where the probability $$p$$ of getting head in a single throw is unknown and can be anywhere such that $$0 < p < 1$$. I do have a random sample with $$n$$ flips and $$x$$ successes (heads). Flips are known to be independent of each other and the sample is assumed to be free of bias.

Obviously the best estimation for $$p$$ is $$p = \frac{x}{n}$$.

What I do want to find is a confidence interval $$(p1, p2)$$ so that $$x$$ is in the $$2\sigma$$ interval (or any arbitrary interval for that matter) of any $$p \in (p1, p2)$$ for $$n$$ tries.

How would I go about this? Is there an easy formula I can just plug $$n$$ and $$x$$ into?

I suppose I could do a binary search for $$p1$$ and $$p2$$ where I calculate the probability of getting at least/most $$x$$ heads in $$n$$ tries and repeat that until I find a $$p1$$/$$p2$$ that is "close enough" to the desired confidence interval, but that does not feel right. Is suppose I'm asking whether there is a closed form formula for this.

Sidenote: My statistics knowledge and research abilities have forsaken me on this. It feels like the most basic question in statistics, yet I could not find a satisfying answer. I'm probably lacking the keywords for this or misunderstood examples I came across.

Would, e.g., the Clopper–Pearson method for confidence intervals work for you ?

The calculation is relatively accessible.

The following can be run in R, or at rdrr.io/snippets/.

The estimate is x/n, and the confidence interval is the Clopper–Pearson confidence interval for this proportion.

 x =  7
n = 21
binom.test(x, n, conf.level=0.95)

### number of successes = 7, number of trials = 21
###
### 95 percent confidence interval:
###    0.1458769 0.5696755
###
### sample estimates:
### probability of success
###      0.3333333


Some other potential methods are listed here, under Details: www.rdocumentation.org/packages/DescTools/versions/0.99.44/topics/BinomCI

"wald", "wilson", "wilsoncc", "agresti-coull", "jeffreys", "modified wilson", "modified jeffreys", "clopper-pearson", "arcsine", "logit", "witting", "pratt", "midp", "lik", "blaker"

• Thanks, that looks exactly like what I need. I integrated it in my excel worksheet using this , and the results seem plausible. Commented Aug 9, 2022 at 12:40
• While I have you here: The background of this question is a 15 sided unfair die. I modelled each side as a coin flip where either this side is rolled or any other side is rolled. Is this a sensible thing to do? Or is there a different approach that is more suitable for this situation? Commented Aug 9, 2022 at 13:01
• Wouldn't your observations be counts for each side of the die ? Like, if you roll it 1000 times, #1 comes up 70 times, and #2 comes up 50 times, and so on ? ... I think in this case you may want to look at multinomial confidence intervals for proportions. One method is the Sison-Glaz. For example, a 6-sided die is rolled 60 times. In R, observed = c(10, 10, 10, 6, 14, 10); library(DescTools); MultinomCI(observed, conf.level=0.95, method="sisonglaz") . Here all the the confidence intervals overlap 0.167, suggesting there's not good evidence that the die is not fair. Commented Aug 9, 2022 at 16:38