$X\sim B(n,p), \hat{p}_{n} = \frac{X}{n}$
I am trying to invert the following statement to obtain a confidence interval.
$P(-1.96 \leq \frac{\sqrt n(\hat{p}_n-p)}{\sqrt{p(1-p)}} \leq 1.96)=0.95$
Not sure how to center the following inequality at $p$, while making sure the bounds do not depend on $p$.
Help here would be appreciated.
You basically need to solve a quadratic for $p$: let $$\frac{\sqrt{n}(\hat p_n - p)}{\sqrt{p(1-p)}} = z_{\alpha/2}.$$ Squaring everything gives $$\frac{n(\hat p_n - p)^2}{p(1-p)} = z_{\alpha/2}^2$$ and clearing denominators and collecting like terms in $p$ gives $$\begin{align} 0 &= n(\hat p_n - p)^2 - z_{\alpha/2}^2 p(1-p) \\ &= (n+z_{\alpha/2}^2)p^2 - (2n \hat p_n + z_{\alpha/2}^2) p + n\hat p_n^2. \end{align}$$ Then the quadratic formula yields $$\begin{align}p &= \frac{(2n\hat p_n + z_{\alpha/2}^2) + \sqrt{(2n \hat p_n + z_{\alpha/2}^2)^2 - 4(n + z_{\alpha/2}^2)(n \hat p_n^2)}}{2(n + z_{\alpha/2}^2)} \\ &= \frac{1}{1+z_{\alpha/2}^2/n}\left( \hat p_n + \frac{z_{\alpha/2}^2}{2n} + z_{\alpha/2} \sqrt{\frac{\hat p_n (1 - \hat p_n)}{n} + \frac{z_{\alpha/2}^2}{4n^2} } \right) \end{align}$$ This furnishes the upper bound of the confidence interval; the lower bound is the negative root of the quadratic since if we replace $z_{\alpha/2}$ with $-z_{\alpha/2}$, the only term that changes is the one with the square root.
The above is the derivation of the Wilson score interval for a binomial parameter. The form written at the end is used to illustrate the differences from the Wald interval, $$\hat p_n \pm z_{\alpha/2} \sqrt{\frac{\hat p_n (1 - \hat p_n)}{n}},$$ which arises from the solution of the inequality $$-z_{\alpha/2} < \frac{\hat p_n - p}{\sqrt{\hat p_n (1 - \hat p_n)/n}} < z_{\alpha/2},$$ in which the denominator uses the point estimate $\hat p_n$ rather than the parameter $p$.