# How to derive the sample size $n$ to achieve $P(\text{type 2 error}) = \beta$ for a two-tailed test?

Consider testing $$H_0 : \mu = \mu_0$$ vs $$H_a : \mu \neq \mu_0$$ at level $$\alpha$$. We then wish to control for the type II error, that is have $$P(\text{type II error} \ | \ \mu = \mu_1) = \beta$$ for a predetermined $$\beta$$

Assume $$\bar{X}$$ is approximately normal with mean $$\mu$$ and standard deviation $$\sigma / \sqrt{n}$$. Base a hypothesis test on the standardized variable $$Z = \frac{\bar{X}-\mu_0}{\sigma/\sqrt{n}}$$, where $$\bar{X} = \frac{1}{n} \sum_{i=1}^n X_i$$

Then the power function is

$$\gamma(\mu) = P(\text{reject} \ H_0 \ | \ \mu) = P(\frac{\bar{X}-\mu_0}{\sigma/\sqrt{n}} \geq -z_{\alpha/2} \ \text{or} \ \frac{\bar{X}-\mu_0}{\sigma/\sqrt{n}} \leq z_{\alpha/2} \ | \ \mu)$$

$$= P(\frac{\bar{X}-\mu_0}{\sigma/\sqrt{n}} \geq -z_{\alpha/2} \ | \ \mu) + P(\frac{\bar{X}-\mu_0}{\sigma/\sqrt{n}} \leq z_{\alpha/2} \ | \ \mu) = 1-\Phi(z_{\alpha/2} + \frac{\mu_0-\mu}{\sigma/\sqrt{n}}) + \Phi(-z_{\alpha/2} + \frac{\mu_0-\mu}{\sigma/\sqrt{n}})$$

where we have solved the inequalities for $$\bar{X}$$ and restandardized with $$\mu_1$$.

Then, for $$\mu_1 \neq \mu_0$$,

$$P(\text{type II error} \ | \ \mu=\mu_1) = P(\text{don't reject} \ H_0 \ | \ \mu=\mu_1)$$ $$=1 - P(\text{reject} \ H_0 \ | \ \mu=\mu_1) = 1 - (1-\Phi(z_{\alpha/2} + \frac{\mu_0-\mu_1}{\sigma/\sqrt{n}}) + \Phi(-z_{\alpha/2} + \frac{\mu_0-\mu_1}{\sigma/\sqrt{n}}))$$ $$= \Phi(z_{\alpha/2} + \frac{\mu_0-\mu_1}{\sigma/\sqrt{n}}) + \Phi(-z_{\alpha/2} + \frac{\mu_0-\mu_1}{\sigma/\sqrt{n}})$$

$$= \beta$$

How to solve this for $$n$$? The answer is supposed to be $$n = \bigg(\frac{\sigma (z_{\alpha/2} + z_{\beta})}{\mu_0 - \mu_1}\bigg)^2$$ (see Devore & Berk, Modern Mathematical Statistics with Applications, 2012: page 441)

Sidenote: The way I solved it in the one-tailed case was simply by doing

$$\Phi(z_{\alpha/2} + \frac{\mu_0-\mu_1}{\sigma/\sqrt{n}}) = \beta$$

$$\Phi^{-1}\Phi(z_{\alpha/2} + \frac{\mu_0-\mu_1}{\sigma/\sqrt{n}}) = \Phi^{-1}\beta = -z_\beta$$, etc

\begin{align*} & {\qquad} 1-\beta = \gamma(\mu) \\ & {\qquad} = 1 + \Phi \left( k-z_{\alpha/2} \right) - \Phi \left( k+z_{\alpha/2} \right), \quad \mbox{where} \quad k := \frac{\mu_0-\mu}{\sigma/\sqrt{n}} \\ & {\qquad} = P(Z \ge z_{\alpha/2}-|k|) + P(Z \ge z_{\alpha/2}+|k|) \\ \Rightarrow & {\qquad} 1-\beta \approx P(Z \ge z_{\alpha/2}-|k|), \quad \mbox{assuming} \quad P(Z \ge z_{\alpha/2}+|k|) \approx 0 \\ \iff & {\qquad} z_{1-\beta} \approx z_{\alpha/2}-|k| \\ \iff & {\quad} -z_{\beta} \approx z_{\alpha/2}-|k| \\ \iff & {\quad} |k| \approx z_{\alpha/2}+z_{\beta}, \end{align*} this gives $$n \approx \left[ \frac{\sigma(z_{\beta} + z_{\alpha/2})} {\mu_0-\mu} \right]^2,$$ as desired.
It is helpful to perform the calculation for a specific set of parameters. Let's choose $$\mu_0 = 0$$ and $$\mu_1 = 1$$, with $$\sigma = 1$$. Moreover, let $$\alpha = 0.05$$. We wish to find $$n$$ such that the test has power $$1 - \beta = 0.9$$. For the two-sided hypothesis $$H_0 : \mu = \mu_0 \quad \text{vs.} \quad H_a : \mu \ne \mu_0$$ the test statistic $$Z = \frac{\bar X - \mu_0}{\sigma/\sqrt{n}} = \bar X \sqrt{n}$$ will result in rejection of $$H_0$$ in favor of $$H_a$$ if $$|Z| > z_{\alpha/2}^* \approx 1.96,$$ where I have used the asterisk to indicate the quantile is the upper, rather than lower quantile. Therefore, failure to reject $$H_0$$ when $$H_1 : \mu = \mu_1$$ is true, is $$\beta = \Pr[|Z| \le z_{\alpha/2}^* \mid \mu = \mu_1] = \Pr[-1.96 \le \bar X\sqrt{n} \le 1.96 \mid \mu = \mu_1] = 0.1.$$ Since $$\bar X \sqrt{n} \mid H_1 \sim \operatorname{Normal}(\mu = \sqrt{n}, \sigma = 1),$$ it follows that we want to find the smallest positive integer $$n$$ such that $$\Pr[-1.96 - \sqrt{n} \le Z \le 1.96 - \sqrt{n}] \le 0.1$$ in order for the test to have Type II error control at level $$\beta$$. This is where you get stuck, because changing $$n$$ changes both endpoints of the inequality, so finding an inverse function is not tractable. But if we observe that the lower endpoint is already very far to the left--that is to say, $$\Pr[Z < -1.96 - \sqrt{n}] \approx 0$$ for any reasonable choice of positive integer $$n$$--then, we can ignore this condition and invert $$\Pr[Z \le 1.96 - \sqrt{n}] \le 0.1$$ to get $$1.96 - \sqrt{n} \le z_\beta = z_{0.1} \approx -1.282.$$ Consequently $$n \ge 10.5106,$$ which rounded up yields $$n = 11$$. The ignored tail probability is $$\Pr[Z \le -1.96 - \sqrt{10.5106}] = \Phi(-5.202) \approx 9.86 \times 10^{-8}.$$ Using a computer, we can numerically solve for the precise $$n$$ that meets the two-sided inequality: $$n \approx 10.50741940969075474768$$ to 20 decimal places. But this is effectively unnecessary since this $$n$$ is always less than the $$n$$ obtained by ignoring the smaller tail, since doing so means that a larger $$n$$ must be chosen to ensure Type II error control.