Computing the number of trials required in the binomial distribution I want to know how I would go about computing how many times I would need to flip a coin where $P(H) = 4/5$ to make sure that the proportion of heads that shows up is in between $75$ and $85$ percent with a probability of at least $95$%. Would appreciate any help. 
 A: There are two ways to approach this kind of problem.  One method is to use the normal approximation to the binomial distribution, and solve the problem algebraically using this approximation, which gives you a reasonable approximation to the true value.  Another method is to proceed with the exact distribution by programming the probability of the interval in an appropriate statistical language, and finding the required value using a loop.  This gets you the exact answer, but it requires a bit of rudimentary programming.

Approximate method: Let $X_n \sim \text{Bin}(n, \theta)$ be the number of heads on your coin and take $\theta = 0.8$ be the probability of a head in a single toss.  The normal approximation to the binomial says that for large $n$ the proportion of heads follows the approximate distribution:
$$\frac{X_n}{n} \overset{\text{approx}}{\sim} \text{N} \Big( \theta, \frac{\theta (1-\theta)}{n} \Big).$$
Hence, for large $n$ we have the approximate standard normal random variable:
$$Z \equiv \frac{X_n/n - \theta}{\sqrt{\theta (1-\theta)}} \cdot \sqrt{n} \overset{\text{approx}}{\sim} \text{N} (0,1).$$
Taking the appropriate lower and upper bounds for your interval, you obtain:
$$\begin{equation} \begin{aligned}
\mathbb{P} \Bigg( 0.75 \leqslant \frac{X_n}{n} \leqslant 0.85 \Bigg) 
&= \mathbb{P} \Bigg( \frac{0.75 - 0.80}{\sqrt{0.80 \cdot 0.20}} \cdot \sqrt{n} \leqslant Z \leqslant \frac{0.85 - 0.80}{\sqrt{0.80 \cdot 0.20}} \cdot \sqrt{n} \Bigg)  \\[6pt]
&\approx \Phi \Bigg( \frac{0.85 - 0.80}{\sqrt{0.80 \cdot 0.20}} \cdot \sqrt{n} \Bigg) - \Phi \Bigg( \frac{0.75 - 0.80}{\sqrt{0.80 \cdot 0.20}} \cdot \sqrt{n} \Bigg) \\[6pt]
&= \Phi \Bigg( \frac{0.05}{\sqrt{0.16}} \cdot \sqrt{n} \Bigg) - \Phi \Bigg( -\frac{0.05}{\sqrt{0.16}} \cdot \sqrt{n} \Bigg) \\[6pt]
&= \Phi ( 0.125 \sqrt{n} ) - \Phi ( -0.125 \sqrt{n} ) \\[6pt]
&= 1 - 2 \cdot \Phi ( -0.125 \sqrt{n} ). \\[6pt]
\end{aligned} \end{equation}$$
Thus, to obtain a value of $n$ with the requisite probability, you require:
$$1 - 2 \cdot \Phi ( -0.125 \sqrt{n} ) \geqslant 0.95.$$
Re-arranging with some simple algebra then gives:
$$n \geqslant \frac{z_{0.025}^2}{0.125^2} = \frac{1.959964^2}{0.125^2} = 245.8.$$
Hence, you obtain the value $n = 246$.  Substituting this value back into the probability equation, we obtain the probability $\mathbb{P} ( 0.75 \leqslant X_{246}/246 \leqslant 0.85 ) \approx 0.9500683$, which confirms that this value satisfies your desired probability level under the normal approximation.

Exact method: To calculate the result using the exact binomial distribution (i.e., without the normal approximation), you can program the problem as a loop, checking each value of $n$ until you find one that satisfies the required condition.  Here is the problem programmed in R code:
#Set preliminary values
IND   <- FALSE;
p     <- 4/5;
n     <- 0;

#Find the value of n
while (!IND) {
    n     <- n + 1;
    UPPER <- floor(0.85*n);
    LOWER <- ceiling(0.75*n);
    PROB  <- pbinom(UPPER, n, p) - pbinom(LOWER-1, n, p);
    IND   <- (PROB >= 0.95); }

#Print the value of n
n;
[1] 232

#Print the probability with this value
UPPER <- floor(0.85*n);
LOWER <- ceiling(0.75*n);
PROB  <- pbinom(UPPER, n, p) - pbinom(LOWER-1, n, p);
PROB;
[1] 0.9516543

These calculations give the exact solution $n = 232$, so we can see that the result obtained with the approximation was a conservative value.
