3  Maximum Likelihood Estimation

Far better an approximate answer to the right question, which is often vague, than an exact answer to the wrong question, which can always be made precise. (Tukey 1962)1

In probability, we are told the rules of the game and then we predict what it will look like. In statistics, we watch the game and try to figure out the rules. Roughly speaking, statistics (game to rules) is the reverse of probability (rules to game). When done well, statistics helps us learn from observations while accounting honestly for uncertainty. An outstanding early example of statistics applied to public health is the work of Florence Nightingale (1820-1910), who collected data and developed statistical graphics to demonstrate the need for public health reforms in the British Army in the 1850s (Cohen 1984; Winkelstein Jr 2009).2

Here, we will use estimation of a probability as an example of maximum likelihood estimation, which is used for parameter estimation throughout frequentist statistical inference, where the probability of an event \(A\) is interpreted as the limiting value of the proportion of \(n\) repetitions of an experiment in which \(A\) occurs as \(n \rightarrow \infty\). Maximum likelihood estimation gives us a way to find point estimates of parameters that are optimal in large samples. It is also the foundation for three types of hypothesis tests and confidence intervals that are widely used to quantify uncertainty in frequentist inference.

3.1 Binomial likelihood

In Section 3.1.1, we used the prevalence \(p\) in our population to figure out the distribution of the number \(X\) of diseased individuals in a sample of size \(n\). This is probability. The corresponding statistical problem would be to estimate the prevalence \(p\) after seeing \(X = x\) infected individuals in a sample of size \(n\).

When our experiment is to sample multiple individuals from a population, the analogy between the outcomes \(\omega \in \Omega\) and the individuals in the population breaks down. Recall that when we flip a coin twice, each \(\omega \in \Omega\) must specify the outcomes of both flips. When the experiment is to sample \(n\) individuals from a population, the entire sample is a single outcome \(\omega\) and \(\Omega\) contains all possible samples of \(n\) individuals from the population. If the population size is \(N\), then the number of possible samples of size \(n\) is given by the binomial coefficient \[ \binom{N}{n} = \frac{N!}{n! (N - n)!}, \] where \(k!\) denotes \(k\) factorial. Factorials are defined by \(0! = 1\) and \(k! = k \cdot (k - 1)!\) for any integer \(k > 0\). For example, \(1! = 1\), \(2! = 2\), \(3! = 6\), \(4! = 24\), \(5! = 120\), and so on. For \(k > 0\), \(k!\) is the product of all positive integers up to and including \(k\), which grows extremely fast as \(k\) increases.

3.1.1 Binomial distribution

Suppose we sample \(n\) individuals from a population \(\Omega\) and test them for disease. For simplicity, we assume that the diagnostic test has perfect sensitivity and specificity. Let \(Y_i\) denote whether person \(i\) in the sample has disease, and let \(X\) be the total number who have disease. Then \[ X = \sum_{i = 1}^n Y_i, \] so it is a linear combination of the \(Y_i\). Each \(Y_i\) is a Bernoulli(\(p\)) random variable, where \(p\) is the prevalence of disease in the population. When \(N\) is much larger than \(n\) (for which we write \(N \gg n\)), the test results for each person in the sample are approximately independent.

The distribution of a sum of \(n\) independent Bernoulli(\(p\)) random variables is called a binomial(n, p) distribution.3 The probability \(Y_1 = 1\) is \(p\), and the probability that \(Y_1 = 0\) is \((1 - p)\), so we can handle both cases by writing \[ \Pr(Y_1 = y_1) = p^{y_1} (1 - p)^{1 - y_1}. \] When the \(Y_i\) are independent, each \(Y_i\) has a Bernoulli(p) distribution (see Section 1.3.5) and \[ \Pr(Y_1 = y_1, Y_2 = y_2, \ldots, Y_n = y_n) = \prod_{i = 1}^n \Pr(Y_i = y_i) = \prod_{i = 1}^n p^{y_i} (1 - p)^{1 - y_i} \] by the multiplication rule for independent events. Substituting \(x = \sum_{i = 1}^n y_i\), we get \[ \Pr(Y_1 = y_1, Y_2 = y_2, \ldots, Y_n = y_n) = p^x (1 - p)^{n - x}. \] The value of \(x\) depends only on the sum of the \(y_i\), and there are \(\binom{n}{x}\) different ways to get \(x\) cases of disease out of \(n\) sampled individuals. By the addition rule for disjoint events, we get \[ \Pr(X = x) = \binom{n}{x} p^x (1 - p)^x. \tag{3.1}\] This is the probability mass function (PMF) of the binomial distribution. The set of possible values of a binomial(n, p) random variable \(X\) is \(\supp(X) = \{0, 1, \ldots, n\}\).

Section 1.3.5 showed that a Bernoulli(\(p\)) random variable has expected value \(p\) and variance \(p(1 - p)\). Because a binomial(\(n\), \(p\)) random variable is the sum of \(n\) independent Bernoulli(\(p\)) random variables, its expected value is \[ \E(X) = n p. \] by the rule for expectations of linear combinations in Equation 1.11. Its variance is \[ \Var(X) = n p (1 - p) \] by the rule for variances of linear combinations in Equation 1.12. The covariances are all zero because the \(Y_i\) are independent.

binomdist.R
## binomial distribution

# binomial PMF
# The second and third arguments are n ("size") and p ("prob").
dbinom(2, 10, 0.4)
dbinom(0:10, 10, 0.4)
sum(dbinom(0:10, 10, 0.4))

# binomial CDF
pbinom(0:10, 10, 0.4)
cumsum(dbinom(0:10, 10, 0.4))

# binomial quantiles
qbinom(c(0.25, 0.5, 0.75, 1), 10, 0.4)

# random samples
rbinom(20, 10, 0.4)
x <- rbinom(1000, 10, 0.4)
mean(x)
var(x)

3.1.2 Likelihood and log likelihood

In probability, we know the prevalence of disease \(p\) and we deduce the distribution of the number of diseased individuals \(X\) in a sample of size \(n\). In statistics, we observe \(X = x\) and use this to estimate \(p\). To do this, we rewrite the binomial PMF Equation 3.1 as a function of \(p\) instead of \(x\): \[ L(p) = \binom{n}{x} p^x (1 - p)^{n - x}. \tag{3.2}\] This is the binomial likelihood function. The right-hand sides of Equation 3.1 and Equation 3.2 are identical, and they produce exactly the same value given the same \(x\) and \(p\). However, the two equations define different functions. In binomial PMF in Equation 3.1, the prevalence \(p\) is fixed and the number of diseased individuals \(x\) is the argument of the function. In the binomial likelihood function in Equation 3.2, the number of diseased individuals \(x\) is fixed and the prevalence \(p\) is the argument of the function. The PMF belongs to probability, and the likelihood belongs to statistics.

The log likelihood is the natural logarithm (i.e., the logarithm to base \(e = 2.718281828\ldots\))4 of the likelihood function. For binomial log likelihood is \[ \ell(p) = \ln \binom{n}{x} + x \ln p + (n - x) \ln (1 - p). \] Because the logarithm turns products into sums, it is generally much easier to handle the log likelihood than the likelihood itself. The term \(\ln \binom{n}{x}\) does not depend on \(p\), so it can be ignored. Intuitively, this tells us that the total number \(x = y_1 + y_2 + \cdots + y_n\) of individuals with disease in our sample contains the same information about the prevalence of disease as the sequence \(y_1, y_2, \ldots, y_n\) of disease indicators.

For any given \(p\), we can think of \(\ell(p)\) as a random variable whose value is determined by our sample of size \(n\). Let \(\ptrue\) be the true prevalence of disease. By Gibb’s inequality,5 \[ \E[\ell(\ptrue)] > \E[\ell(p)] \] for all \(p \neq \ptrue\). This inequality is about the expected value of the log likelihood over all possible samples of size \(n\). For any given sample, it is possible that \(\ell(\ptrue)\) is not the maximum of the log likelihood. However, this inequality is an important part of the justification for estimating \(p\) by maximizing the log likelihood (Boos and Stefanski 2013). Because function \(v \mapsto \ln(v)\) is strictly increasing in \(v\), the likelihood \(L(p)\) and the log likelihood \(\ell(p)\) are maximized at exactly the same value of \(p\).

3.1.3 Score function

To find the maximum of the log likelihood, we find the value of \(p\) where its slope is zero. This is the mathematical version of the insight that the ground at the top of a hill is level. The score function is the first derivative of the log likelihood \[ U(p) = \frac{\text{d}}{\text{d} p} \ell(p) = \frac{x}{p} - \frac{n - x}{1 - p}, \] which is the slope of \(\ell(p)\) at \(p\). To find where the slope equals zero, we solve the score equation \[ U(\hat{p}) = \frac{x}{\hat{p}} - \frac{n - x}{1 - \hat{p}} = 0 \tag{3.3}\] where \(\hat{p}\) denotes the maximum likelihood estimate (MLE) of \(\ptrue\). When the dust settles, we get \[ \hat{p} = \frac{x}{n} \] so our MLE of the prevalence is just the proportion of our sample who has disease.

To confirm that this is a maximum instead of a minimum, we need to look at the second derivative of \(\ell\). When we walk across the top of a hill, we go from walking uphill to walking downhill so the slope is decreasing. If \(\ell(p)\) is maximized at \(\hat{p}\), then the slope of the slope (i.e., the second derivative) should be negative. The second derivative of \(\ell(p)\) at \(\hat{p}\) is \[ \frac{\text{d}}{\text{d} p} U(p) = \frac{\text{d}^2}{\text{d} p^2} \ell(p) = -\frac{x}{p^2} - \frac{n - x}{(1 - p)^2}. \tag{3.4}\] This is negative for any \(p \in (0, 1)\). Thus, the log likelihood is maximized at \(\hat{p}\) if \(x > 0\) and \(x < n\).

When \(x = 0\) or \(x = n\), the log likelihood \(\ell(p)\) has no maximum at any \(p \in (0, 1)\). Instead, the maximum occurs at one of the boundaries of the set of possible \(p\). When \(x = 0\), our MLE of \(\ptrue\) is \(\hat{p} = 0\). When \(x = n\), our maximum likelihood estimate is \(\hat{p} = 1\).

3.1.4 Expected and observed information*

For any given \(p\), we can think of the score \(U(p)\) as a random variable that has an expected value and a variance. If \(\ptrue = p\), the expected value of the score is always zero: \[ \E_p[U(p)] = \E_p\bigg[\frac{X}{p} - \frac{n - X}{1 - p}\bigg] = \frac{\E_p(X)}{p} - \frac{\E_p(n - X)}{1 - p} = \frac{n p}{p} - \frac{n (1 - p)}{1 - p} = 0 \] where we use the subscript \(p\) to indicate that the expected value is calculated assuming that \(\ptrue = p\). Because \(\E_p[U(p)] = 0\), the corresponding variance of the score is \[ \mathcal{I}(p) = \Var_p[U(p)] = \E_p[U(p)^2], \] by Equation 1.10. This is called the expected Fisher information or expected information.6 It can be used to calculate confidence limits for \(\ptrue\).

Under regularity conditions that are met when \(\ptrue \in (0, 1)\), the Fisher information \(\mathcal{I}(p)\) can be calculated using the second derivative of the log likelihood \(\ell(p)\) from Equation 3.4.7 Specifically, \(\mathcal{I}(p)\) is the expected value of the negative second derivative of \(\ell(p)\): \[ \mathcal{I}(p) = \E_p\Bigg[-\frac{\text{d}^2}{\text{d} p^2} \ell(p)\Bigg] = \E_p\bigg[\frac{X}{p^2} + \frac{n - X}{(1 - p)^2}\bigg], \tag{3.5}\] where the subscript \(p\) indicates that the expected value is calculated assuming that \(\ptrue = p\). Using Equation 1.11 and the binomial(\(n\), \(p\)) distribution for \(X\), this simplifies to \[ \mathcal{I}(p) = \frac{\E(X)}{p^2} + \frac{\E(n - X)}{(1 - p)^2} = \frac{n}{p} + \frac{n}{1 - p} = \frac{n}{p (1 - p)}. \] Because \(\ptrue\) is unknown, the expected information is often evaluated at \(\hat{p}\). In some models, the expected information can be difficult to calculate.

The negative second derivative of \(\ell(p)\) inside the expectation in Equation 3.5 evaluated is the observed Fisher information or observed information \[ I(p) = -\frac{\text{d}^2}{\text{d} p^2} \ell(p) = \frac{x}{p^2} + \frac{n - x}{(1 - p)^2}. \tag{3.6}\] For the binomial distribution \(I(\hat{p}) = \mathcal{I}(\hat{p})\) but this equality does not hold at other values of \(p\). The observed information is an unbiased estimator of the expected information, and it can always be calculated from the data. It often produces more accurate variance estimates than the expected information (Efron and Hinkley 1978; Kenward and Molenberghs 1998; Reid 2003). However, it is generally safe to use whichever is most convenient (Boos and Stefanski 2013).

3.2 Large-sample theory

The log likelihood, the score function, and the Fisher and observed information give us all of the pieces we need to calculate point and interval estimates of \(\ptrue\). To put them together, we use two fundamental results from probability theory about the behavior of sample means. The law of large numbers justifies point estimates and the central limit theorem justifies hypothesis tests and interval estimates, which can be obtained in three standard ways.

3.2.1 Sample mean (average)

If \(Y_1, Y_2, \ldots, Y_n\) are random variables, then the sample mean or average is \[ \hat{\mu}_n = \frac{1}{n} \sum_{i = 1}^n Y_i. \] This sample mean can be thought of as a random variable whose value is determined when we observe \(Y_1 = y_1, Y_2 = y_2, \ldots, Y_n = y_n\). If each \(Y_i\) has \(\E(Y_i) = \mu\), then \[ \E[\hat{\mu}_n] = \frac{1}{n} \sum_{i = 1}^n \E[Y_i] = \frac{1}{n} n \mu = \mu \tag{3.7}\] by Equation 1.11. Thus, the sample mean \(\hat{\mu}_n\) is an unbiased estimate of \(\mu\) for any sample size \(n\). When the \(Y_i\) are indicator variables, \(\hat{\mu}_n\) is just the proportion of the sample with \(Y_i = 1\).

3.2.2 Law of large numbers and consistency

If the \(Y_i\) are independent and each has \(\Var(Y_i) = \sigma^2\), then \[ \Var(\hat{\mu}_n) = \frac{1}{n^2} \sum_{i = 1}^n \Var(Y_i) = \frac{1}{n^2} n \sigma^2 = \frac{\sigma^2}{n} \tag{3.8}\] by Equation 1.12. Thus, the variance of \(\hat{\mu}_n\) decreases as the sample size \(n\) increases. The standard deviation of \(\hat{\mu}_n\) is proportional to \(1 / \sqrt{n}\). As \(n \rightarrow \infty\), we should have \(\hat{\mu}_n \rightarrow \mu\). This is called the law of large numbers, and it holds even when \(\sigma^2 = \infty\).

Theorem 3.1 (Law of Large Numbers) If \(Y_1, Y_2, \ldots\) is an infinite sequence of independent and identically-distributed (IID) random variables with mean \(\mu < \infty\) and variance \(\sigma^2 \leq \infty\), then
\[ \hat{\mu}_n \rightarrow \mu \] as \(n \rightarrow \infty\).8

Our maximum likelihood estimate \(\hat{p}_n\) is a sample mean: \[ \hat{p}_n = \frac{X}{n} = \frac{1}{n} \sum_{i = 1}^n Y_i. \] where each \(Y_i \sim \text{Bernoulli}(\ptrue)\) and the \(Y_i\) are independent. Therefore, the LLN implies that \[ \hat{p}_n \rightarrow \ptrue \] as \(n \rightarrow \infty\). This convergence is shown in Figure 3.1. An estimate that converges to its true value as \(n \rightarrow \infty\) is called consistent. Intuitively, this means that \(\hat{p}_n\) is guaranteed to be close to \(\ptrue\) in a large sample. However, the LLN does not specify how close or how large a sample we need.

Code
lln.R
## Law of large numbers

n <- 1000
x <- seq(n)
plot(x, cumsum(rbinom(n, 1, .5)) / x, type = "n", ylim = c(0, 1),
     xlab = "Number of samples", ylab = "Sample mean")
grid()
lines(x, cumsum(rbinom(n, 1, .5)) / x, lty = "solid")
lines(x, cumsum(rbinom(n, 1, .5)) / x, lty = "dashed")
lines(x, cumsum(rbinom(n, 1, .5)) / x, lty = "dotted")
abline(h = .5)
Figure 3.1: The LLN at work. Each line traces the sample means calculated from a sequence of random samples \(x_1, x_2, x_3, \ldots\) from a Bernoulli(0.5) distribution. For each sequence, the y-coordinate above \(n\) is the sample mean from the first \(n\) random samples in the sequence. The true mean of 0.5 is marked by a solid horizontal line.

3.2.3 Central limit theorem and the normal distribution

When both the mean and variance of the \(Y_i\) are finite, the central limit theorem (CLT) allows us to say something about how far away our sample mean \(\hat{\mu}_n\) is from the true value \(\mu\). It is the most important result in all of probability and statistics.

Theorem 3.2 (Central Limit Theorem) If \(Y_1, Y_2, \ldots\) is an infinite sequence of IID random variables with finite mean \(\mu\) and variance \(\sigma^2 < \infty\), then \[ Z_n = \frac{\hat{\mu}_n - \E(\hat{\mu_n})}{\sqrt{\Var(\hat{\mu}_n)}} = \frac{\sqrt{n} (\hat{\mu}_n - \mu)}{\sqrt{\sigma^2}} \] has a distribution that converges to a normal distribution or Gaussian distribution with mean zero and variance one as \(n \rightarrow \infty\).9 Because of this, we say that \(\hat{\mu}_n\) is asymptotically normal.

The normal distribution is a distribution for a continuous random variable, which can take any value on an interval or even on all of \(\mathbb{R}\). Instead of a PMF, a continuous random variable \(Z\) has a probability density function (PDF). If \(Z\) is a continuous random variable with PDF \(f(z)\) and \([a, b]\) is an interval, then \[ \Pr\bigl(Z \in [a, b]\bigr) = \int_a^b f(z) \,\text{d} z. \] The integral on the right-hand side represents the area under \(f(z)\) over the interval \([a, b]\). The cumulative distribution function of \(Z\) is \[ F(z) = \int_{-\infty}^z f(u) \,\text{d} u, \] where the integral on the right-hand side represents the area under \(f(z)\) over the interval \((-\infty, u]\). For the same reason that the values of the PMF for any discrete random variable add up to one, we have \[ \Pr(Z \in \mathbb{R}) = \int_{-\infty}^\infty f(z) \,\text{d} z = 1 \] for any continuous random variable \(Z\). Like the PMF and CDF of a discrete random variable, the PDF and CDF of a continuous random variable contain the same information about the distribution of \(Z\).

The PDF of the normal distribution with mean \(\mu\) and variance \(\sigma^2\) is \[ f(z, \mu, \sigma^2) = \frac{1}{\sqrt{2 \pi \sigma^2}} e^{-\frac{(z - \mu)^2}{2 \sigma^2}}. \] The standard normal distribution has \(\mu = 0\) and \(\sigma^2 = 1\). It is such an important distribution that its PDF and CDF have special notation. The standard normal PDF is \[ \phi(z) = \frac{1}{\sqrt{2 \pi}} e^{-\frac{z^2}{2}}, \] and its CDF is \(\Phi(z)\). These functions and the relationship between them are illustrated in Figure 3.2. A normal distribution is denoted \(N(\mu, \sigma^2)\), so the standard normal distribution is written \(N(0, 1)\).

Code
normplots.R
## Normal distribution PDF and CDF

# set grid of plots
par(mfrow = c(2, 1), mar = c(2, 5, 2, 2) + 0.1)

# define variables
x <- seq(-3.5, 3.5, by = 0.01)
a <- 0
b <- 2

# plot of PDF
plot(x, dnorm(x), type = "n",
    ylab = expression(paste("PDF ", phi1(z))))
grid()
lines(x, dnorm(x))
polygon(x = c(b, a, seq(a, b, by = 0.01)),
        y = c(0, 0, dnorm(seq(a, b, by = 0.01))),
        lty = "dashed", col = "darkgray")
text(0.4, 0.18, labels = "Area = Pr(0 < Z < 2)", srt = 90)

# plot of CDF
plot(x, pnorm(x), type = "n",
     ylab = expression(paste("CDF ", Phi(z))))
grid()
lines(x, pnorm(x))
segments(c(-4, -4), pnorm(c(a, b)), c(a, b), pnorm(c(a, b)),
         lty = "dashed")
segments(c(a, b), c(-1, -1), c(a, b), pnorm(c(a, b)), lty = "dashed")
arrows(-3, pnorm(a), -3, pnorm(b), code = 3, length = 0.1)
text(-1.7, sum(pnorm(c(a, b))) / 2, labels = "Change = Pr(0 < Z < 2)")
Figure 3.2: The PDF (top) and CDF (bottom) of a standard normal random variable \(Z\). If \(X \sim N(0, 1)\), then \(\Pr(0 < X < 2)\) equals the shaded area under the PDF as well as the change in the CDF from \(0\) to \(2\). This same relationship between the CDF and the PDF holds for all continuous random variables and any interval \((a, b)\).
normdist.R
## normal (Gaussian) distribution

# normal PDF
# Second and third arguments are mean and SD (not variance).
# The defaults are mean = 0 and SD = 1.
dnorm(2, 1.2, 5)

# normal CDF (using default mean and variance)
pnorm(1.96)
pnorm(1.96) - pnorm(-1.96)

# normal quantiles
qnorm(0.975)
pnorm(qnorm(0.975))

# random samples (using named arguments)
rnorm(25, mean = 2.3, sd = 3)

For our estimated probability \(\hat{p}_n\) is a sample mean of IID \(Y_i\) with \(\E(Y_i) = \ptrue\) and \(\Var(Y_i) = \ptrue (1 - \ptrue)\). When \(n\) is large, \[ Z_n = \frac{\sqrt{n} (\hat{p}_n - \ptrue)}{\sqrt{\ptrue (1 - \ptrue)}} = \frac{\hat{p}_n - \ptrue}{\sqrt{\mathcal{I}(\ptrue)^{-1}}} \tag{3.9}\] has a distribution that is close to a standard normal distribution. Figure 3.3 shows this convergence is shown for sample means where \(Y_i \sim \Bernoulli(0.1)\). The CLT does not guarantee that the distribution of \(Z_n\) is approximately normal in any given sample. It only guarantees that the normal approximation holds eventually as \(n\) increases. When the \(Y_i \sim \Bernoulli(p)\), the normal approximation is typically good when \(n p (1 - p) > 5\).

Code
clt.R
## Central limit theorem

# probability mass function for sample mean
dbline <- function(n, p=.5, ...) {
  x <- (seq(-.5, n + .5) / n - p) * sqrt(n / (p * (1 - p)))
  y <- c(0, dbinom(0:n, n, p), 0) * sqrt(p * (1 - p) * n)
  lines(stepfun(x, y), pch = NA, ...)
}

# define grid of plots
par(mfrow = c(2, 2))
x <- seq(-4, 4, by = .01)

# n = 20
plot(x, dnorm(x), type = "n", ylim = c(0, .5),
     main = "n = 20", xlab = "Z score", ylab = "Probability density")
grid()
dbline(20, p = .1, lty = "dashed")
lines(x, dnorm(x), col = "darkgray")

# n = 50
plot(x, dnorm(x), type = "n", ylim = c(0, .5),
     main = "n = 50", xlab = "Z score", ylab = "Probability density")
grid()
dbline(50, p = .1, lty = "dashed")
lines(x, dnorm(x), col = "darkgray")

# n = 100
plot(x, dnorm(x), type = "n", ylim = c(0, .5),
     main = "n = 100", xlab = "Z score", ylab = "Probability density")
grid()
dbline(100, p = .1, lty = "dashed")
lines(x, dnorm(x), col = "darkgray")

# n = 250
plot(x, dnorm(x), type = "n", ylim = c(0, .5),
     main = "n = 250", xlab = "Z score", ylab = "Probability density")
grid()
dbline(250, p = .1, lty = "dashed")
lines(x, dnorm(x), col = "darkgray")
Figure 3.3: The CLT at work. The dashed lines show the PMF of the distribution of the average from a sample of size \(n\) from a Bernoulli(0.1) distribution. The solid line is the standard normal PDF.

3.2.4 Efficiency of maximum likelihood estimators*

We have used the LLN and the CLT to show that \(\hat{p}_n\) is consistent and asymptotically normal, which are both wonderful properties for an estimator to have. However, they do not prove that \(\hat{p}_n\) is the best estimator of \(\ptrue\) in any particular sense. In Equation 3.9, the variance of \(\hat{p}_n\) was \[ \mathcal{I}(\ptrue)^{-1} = \frac{\ptrue (1 - \ptrue)}{n}, \] which is the inverse of the Fisher information. It turns out that no other unbiased estimator of \(\ptrue\) can have lower variance, so \(\hat{p}_n\) is the minimum-variance unbiased estimator of \(\ptrue\).

Suppose \(\theta\) is a parameter for a family of PMFs or PDFs \(f(y, \theta)\) such that the true PMF or PDF is \(f(y, \theta_\true)\). When we observe \(Y_1 = y_1, Y_2 = y_2, \ldots Y_n = y_n\), the likelihood is \[ L(\theta) = \prod_{i = 1} f(y_i, \theta), \] and the log likelihood is \[ \ell(\theta) = \ln L(\theta) = \sum_{i = 1}^n \ln f(y_i, \theta). \] The score function is \[ U(\theta) = \frac{\text{d}}{\text{d} \theta} \ell(\theta), \] and the MLE is the solution of the score equation \(U(\hat{\theta}) = 0\). The Fisher information is \[ \mathcal{I}(\theta) = \E_\theta\bigg[\frac{\text{d}^2}{\text{d} \theta^2} \ell(\theta)\bigg], \] and \(\Var(\hat{\theta}) = \mathcal{I}(\theta)^{-1}\). If \(\bar{\theta}\) is any unbiased estimator of the true value \(\theta_\text{true}\), then \[ \Var(\bar{\theta}) \geq \mathcal{I}(\theta_\text{true})^{-1}. \] This result is called the Cramér-Rao lower bound (Rao 1945; Cramér 1946),10 No unbiased estimator of \(\theta_\true\) can have smaller variance than the MLE \(\hat{\theta}\). Maximum likelihood estimates are consistent, asymptotically normal, and asymptotically efficient when the likelihood is correct (Boos and Stefanski 2013).

3.3 Hypothesis testing

In a hypothesis test, we specify a null hypothesis and then decide whether to reject it based on the value of a test statistic. A null hypothesis often takes the form \[ H_0: \theta_\text{true} = \theta_0. \tag{3.10}\] We reject \(H_0\) if the test statistic appears inconsistent with its distribution under \(H_0\). Otherwise, we fail to reject \(H_0\). It is traditional to avoid saying that \(H_0\) was accepted.

3.3.1 Hypothesis tests and diagnostic tests

If we think of \(H_0\) as not having the disease and rejecting \(H_0\) as testing positive for the disease, a hypothesis test is analogous to a diagnostic test. Table 3.1 shows the possible outcomes of a hypothesis test, and its margins show the correspondence to diagnostic testing (Diamond and Forrester 1983). A false positive occurs when we reject \(H_0\) when it is true, which is called a type I error. A false negative occurs when we fail to reject \(H_0\) when it is false, which is called type II error.

Table 3.1: Truth of \(H_0\) and hypothesis test results.
Reject \(H_0\) (\(T^+\)) Fail to reject \(H_0\) (\(T^-\))
\(H_0\) false (\(D^+\)) True positive False negative = type II error
\(H_0\) true (\(D^-\)) False positive = type I error True negative

A hypothesis test has analogs of sensitivity and specificity. The equivalent of specificity is \(1 - \alpha\) where \[ \alpha = \Pr(\text{reject } H_0 \given{} H_0 \text{ true}) \] is the probability of a type I error. This is also called the significance level of the test. The equivalent of sensitivity is the power of the test, which is \(1 - \beta\) where \[ \beta = \Pr(\text{fail to reject } H_0 \given{} H_0 \text{ false}) \] is the probability of a type II error.

A hypothesis test also has analogs of positive and negative predictive values (PPV and NPV). Just like the PPV and NPV of a dignostic test depend on the prevalence of disease, the PPV and NPV of a hypothesis test depend on the prior probability that \(H_0\) is true, which is the probability that \(H_0\) is true based on what we know before we see the test result. For a hypothesis test, the PPV is \[ \Pr(H_0 \text{ false} \given{} H_0 \text{ rejected}) = \frac{(1 - \beta) \Pr(H_0 \text{ false})} {(1 - \beta) \Pr(H_0 \text{ false}) + \alpha \Pr(H_0 \text{ true})} \tag{3.11}\] by Bayes’ rule. Similarly, the NPV of the hypothesis test is \[ \Pr(H_0 \text{ true} \given H_0 \text{ not rejected}) = \frac{(1 - \alpha) \Pr(H_0 \text{ true})} {(1 - \alpha) \Pr(H_0 \text{ true}) + \beta \Pr(H_0 \text{ false})}. \tag{3.12}\] The conditional probability that \(H_0\) is true given the result of the hypothesis test is called the posterior probability of \(H_0\).

3.3.2 Wald, score, and likelihood ratio tests

In a maximum likelihood framework, there are three classical tests for a null hypothesis of the form \[ H_0: \ptrue = p_0. \] These tests are asymptotically equivalent, which means that they produce similar results in large samples. The best way to visualize the different tests is to look at a graph of the log likelihood function. Figure 3.4 shows the log likelihood function for a binary outcome with \(x = 60\) events out of \(n = 100\) trials and a null hypothesis \(H_0: p_\true = 0.5\). All three tests generalize to null hypotheses involving multiple parameters (Boos and Stefanski 2013).

The Wald test (Wald 1943) of \(H_0\) looks at the distance between the MLE \(\hat{p}\) and the hypothesized value \(p_0\)(Wald 1943), rejecting \(H_0\) when this distance is sufficiently large.11 An example is shown in Figure 3.4. The Wald test statistic is \[ W = \frac{(\hat{p} - p_0)^2}{I(\hat{p})} = \frac{n (\hat{p} - p_0)^2}{\hat{p} (1 - \hat{p})} \approxsim \chi^2_1 \tag{3.13}\] under \(H_0\), where \(I(\hat{p})\) is the observed information from Equation 3.6. The \(\chi^2_1\) distribution is the distribution of \(Z^2\) if \(Z \sim N(0, 1)\).

The score test looks at the slope of the log likelihood at \(p_0\), rejecting \(H_0\) if this slope is sufficiently far from zero (Rao 1948; Aitchison and Silvey 1958). An example is shown in Figure 3.4. It score test statistic is \[ S = \frac{U(p_0)^2}{\mathcal{I}(p_0)} = \frac{n (\hat{p} - p_0)^2}{p_0 (1 - p_0)} \approxsim \chi^2_1 \tag{3.14}\] under \(H_0\), where \(\mathcal{I}(p_0)\) is the expected information from Equation 3.5. The numerator of the score statistic is the same as for the Wald statistic in Equation 3.13, but the denominator uses the expected information at \(p_0\) instead of the observed information at \(\hat{p}\). In score tests, it generally better to use the expected information than the observed information (Freedman 2007). The most important advantage of the score test is that it only needs the hypothesized null value \(p_0\), so it can be done without finding the maximum likelihood estimate \(\hat{p}\).

The likelihood ratio test looks at the vertical distance between \(\ell(\hat{p})\) (which is the maximum) and \(\ell(p_0)\), rejecting \(H_0\) if this distance is sufficiently large Wilks (1938).12 An example is shown in Figure 3.4. The likelihood ratio test statistic is \[ L = 2\big(\ell(\hat{p}) - \ell(p_0)\big) \approxsim \chi^2_1 \tag{3.15}\] under \(H_0\). The Neyman-Pearson lemma (Neyman and Pearson 1933) shows that the likelihood ratio test is the most powerful of all hypothesis test for comparing two hypotheses \(H_0: \ptrue = p_0\) and \(H_1: \ptrue = p_1\) at a fixed significance level.

Code
htests.R
## Hypothesis tests based on the log likelihood

# binomial log likelihood, score, and information functions
bin_loglik <- function(p, k=60, n=100) {
  k * log(p) + (n - k) * log(1 - p)
}
bin_score <- function(p, k=60, n=100) {
  k / p - (n - k) / (1 - p)
}
bin_information <- function(p, k=60, n=100) {
  k / p^2 + (n - k) / (1 - p)^2
}

# plot showing Wald, score, and likelihood ratio tests
p <- seq(0.4, 0.8, length.out = 200)
plot(p, bin_loglik(p), type = "n",
     xlim = c(0.40, 0.70), ylim = c(-72, -66),
     main = "Tests of the null hypothesis p = 0.5",
     xlab = "p", ylab = "ln L(p)")
grid()
lines(p, bin_loglik(p))
abline(v = c(0.5, 0.6), lty = "dotted")
abline(h = c(bin_loglik(0.5), bin_loglik(0.6)), lty = "dashed")
abline(a = bin_loglik(0.5) - bin_score(0.5) * 0.5, b = bin_score(0.5),
       col = "darkgray")
text(c(0.5, 0.6), c(-67.05, -67),
     labels = c(expression(p[0]), expression(hat(p))))
text(0.55, -70.7, labels = "Wald test")
arrows(0.5, -70.5, 0.6, code = 3, length = 0.1)
arrows(0.475, bin_loglik(0.5), y1 = bin_loglik(0.6),
       code = 3, length = 0.1)
text(0.45, -68.3, labels = "LRT")

# The slope is the tangent of the angle to the x-axis.
# We also must account for the different scales on the x- and y-axes.
# 0.3 / 6 is xdist / ydist (see xlim and ylim above)
score_angle <- atan(bin_score(0.5) * 0.3 / 6)
angles <- seq(0, score_angle, by = 0.01)
score_x <- 0.5 + 0.04 * cos(angles)
score_y <- bin_loglik(0.5) + 0.04 * (6 / 0.3) * sin(angles)
lines(score_x, score_y)
text(0.56, -68.8, "Score test")
arrows(score_x[2], score_y[2], score_x[1], score_y[1], length = 0.1)
arrows(rev(score_x)[2], rev(score_y)[2], rev(score_x)[1], rev(score_y)[1],
       length = 0.1)
Figure 3.4: Binomial log likelihood function for \(x = 60\) and \(n = 100\). The null value of \(p\) is \(p_0 = 0.5\) and the maximum likelihood estimate is \(\hat{p} = 0.6\).

3.3.3 Critical values and p-values

The Neyman-Pearson approach to hypothesis testing fixes the significance level \(\alpha\) before calculating the test statistic and deciding whether to reject \(H_0\).13 The decision to reject the null hypothesis depends on the value of the test statistic, which is compared to a critical value calculated based on the distribution of the test statistic under \(H_0\). If \(Z \sim N(0, 1)\) under \(H_0\) \[ \Pr\big(|Z| \geq z_{1 - \frac{\alpha}{2}} \given{} H_0 \text{ true}\big) = 1 - \alpha. \] Because \(Z^2 \sim \chi^2_1\) when \(Z \sim N(0, 1)\), this is equivalent to \[ \Pr\big(Z^2 \geq z_{1 - \frac{\alpha}{2}}^2 \given{} H_0 \text{ true}\big) = 1 - \alpha. \] In the Wald, score, and likelihood ratio tests above, \(H_0\) is rejected if the test statistic is larger than the critical value \(z_{1 - \frac{\alpha}{2}}^2\). For \(\alpha = 0.05\), we have \(z_{0.975} \approx 1.96\) so critical value for the \(\chi^2_1\) distribution is \(1.96^2 \approx 3.84\). The test statistic and critical value in a hypothesis test are analogous to the clinical measurement and cutoff in a diagnostic test.

Instead of making a binary decision, it is more informative to calculate a measure of the evidence against \(H_0\). The p-value for a given test statistic is the lowest value of \(\alpha\) at which the test would still fail to reject \(H_0\). A hypothesis test with significance level \(\alpha\) rejects \(H_0\) if the p-value is \(\leq \alpha\). For the Wald, score, or likelihood ratio tests above, \[ \text{p-value} = 1 - F_{\chi^2_1}(\text{test statistic}) \] where \(F_{\chi^2_1}\) is the CDF of the \(\chi^2_1\) distribution If we think of the test statistic as the clinical measurement underlying a diagnostic test, the p-value equals \(1 - \text{spec}_\text{max}\) where \(\text{spec}_\text{max}\) is the highest specificity under which we would still get a positive test (i.e., reject \(H_0\)).

3.4 Confidence intervals

A p-value is more informative than a binary decision whether to reject \(H_0\), but it is still more useful to know what values of \(p\) are plausibly consistent with the data we observed (Rothman 1978). The \(1 - \alpha\) confidence interval for \(\ptrue\) is the set of all possible null values \(p_0\) such that we would fail to reject \(H_0: \ptrue = p_0\) in a hypothesis test with significance level \(\alpha\). The endpoints of the confidence interval are called are called confidence limits. Just as different clinical measurements lead to different diagnostic tests, different hypothesis tests lead to different confidence intervals.

If we calculate a confidence interval many times with independent data sets, the \(1 - \alpha\) confidence interval should contain \(\ptrue\) with probability \(1 - \alpha\). The actual probability that the confidence interval contains \(\ptrue\) is called the coverage probability. A good confidence interval should have a coverage probability close to \(1 - \alpha\) while being as narrow as possible. The Wald, score, and likelihood ratio tests from Section 3.3.2 are large-sample tests because they rely on consistency and asymptotic normality of the maximum likelihood estimate \(\hat{p}\). All three tests can be inverted to produce confidence intervals that perform well in large samples. In smaller samples, the score and likelihood ratio confidence intervals often have better coverage probability and width than the Wald confidence interval (Agresti and Coull 1998; Brown, Cai, and DasGupta 2001).

3.4.1 Wald confidence intervals and the delta method

The Wald confidence limits come from solving the equation \[ \frac{(\hat{p} - p)^2}{\hat{p} (1 - \hat{p}) / n} = z_{1 - \frac{\alpha}{2}}^2. \tag{3.16}\] for \(p\), which gives us \[ \hat{p} \pm z_{1 - \frac{\alpha}{2}} \sqrt{\frac{\hat{p} (1 - \hat{p})}{n}}. \tag{3.17}\] The coverage probabilities of Wald confidence intervals can be much lower than \(1 - \alpha\), especially when \(p_\true\) is close to zero or one (Agresti and Coull 1998; Brown, Cai, and DasGupta 2001).

Another problem with the Wald confidence interval for \(\ptrue\) is that it can have bounds outside \([0, 1]\). One way to avoid this is to calculate confidence limits for a transformation of \(\hat{p}\) using the delta method. A good transformation \(g(p)\) should have continuous first derivatives and be strictly increasing or decreasing, so each value of \(g(p)\) corresponds to a single value of \(p\) (i.e., \(g\) is one-to-one). The delta method derives the approximate normal distribution \(g(\hat{p})\) using the approximation \[ g(\hat{p}) \approx g(\ptrue) + g'(\ptrue) (\hat{p} - \ptrue). \] where \(g'(\ptrue)\) is the slope of \(g\) at \(\ptrue\). An example of this approximation is shown in Figure 3.5. The key insight is that \[ \Var[g(\hat{p})] \approx g'(\ptrue)^2 \Var(\hat{p}), \] which is a generalization of the fact that \(\Var(c \hat{p}) = c^2 \Var(\hat{p})\) for any constant \(c\). If \(\hat{p}\) has an approximate \(N\bigl(\ptrue, \Var(\hat{p})\bigr)\) distribution in large samples, then \[ g(\hat{p}) \approxsim N\bigl(g(\ptrue), g'(\ptrue)^2 \Var(\hat{p})\bigr). \] in large samples. Because our estimator \(\hat{p}\) is consistent, we can replace the unknown \(\ptrue\) with \(\hat{p}\). Because \(g\) is one-to-one, we can calculate confidence limits for \(\ptrue\) using the confidence limits for \(g(\ptrue)\).

Code
delta.R
## Approximation used by the delta method

p <- seq(0.02, 0.98, by = 0.01)
logit <- function(p) log(p) - log(1 - p)

# plot
plot(p, logit(p), type = "n",
     xlab = "p", ylab = "logit(p)")
grid()
lines(p, logit(p))
points(0.6, logit(0.6))
abline(logit(0.6) - 2.5, 1 / 0.24, lty = "dashed")
text(0.6, -1,
     labels = expression(paste("logit(p) - ", logit(0.6) %~~% logit,
                               "'(0.6) (p - 0.6)")))
Figure 3.5: The approximation used by the delta method using the logistic transformation for a binomial confidence interval near \(\hat{p} = 0.6\). The black curve is \(\logit(p)\), and the dashed line shows the tangent line at \(p = 0.6\).

A widely used transformation for probabilities is the logit transformation \[ \logit(p) = \ln\Bigl(\frac{p}{1 - p}\Bigr). \tag{3.18}\] The odds corresponding to the probability \(p\) is \(\frac{p}{1 - p}\), so the logit is the natural logarithm of the odds. The logit transformation maps the interval \((0, 1)\) onto all of \(\mathbb{R}\):

  • As \(p \rightarrow 0\), the odds \(p / (1 - p) \rightarrow 0\) and \(\logit(p) \rightarrow -\infty\).
  • When \(p = 1 / 2\), the odds \(p / (1 - p) = 1\) and \(\logit(p) = 0\).
  • As \(p \rightarrow 1\), the odds \(p / (1 - p) \rightarrow \infty\) and \(\logit(p) \rightarrow \infty\).

To use the delta method, we need to calculate the derivative of \(\logit(p)\). By the chain rule, \[ \logit'(p) = \frac{1 - p}{p} \frac{1}{(1 - p)^2} = \frac{1}{p (1 - p)}, \] which is continuous and strictly positive for all \(p \in (0, 1)\). By the delta method, the variance of \(\logit(\hat{p})\) is approximately \[ \logit'(\ptrue)^2 \frac{\ptrue (1 - \ptrue)}{n} = \frac{1}{\ptrue^2 (1 - \ptrue)^2} \frac{\ptrue (1 - \ptrue)}{n} = \frac{1}{n \ptrue (1 - \ptrue)}. \] When we replace the unknown \(\ptrue\) with our MLE \(\hat{p}\), we get the following confidence limits for \(\logit(\ptrue)\): \[ \logit(\hat{p}) \pm z_{1 - \frac{\alpha}{2}} \sqrt{\frac{1}{n \hat{p} (1 - \hat{p})}}. \] To get confidence limits for \(\ptrue\), we use the inverse function for the logit, which is \[ \expit(v) = \frac{e^v}{1 + e^v} = \frac{1}{1 + e^{-v}}. \tag{3.19}\] This is also called the logistic function. If the confidence limits for \(\logit(\ptrue)\) are \(a\) and \(b\), then the confidence limits for \(\ptrue\) are \(\expit(a)\) and \(\expit(b)\). These are guaranteed to be in \((0, 1)\) because \(\expit(v) \in (0, 1)\) for any \(v \in \mathbb{R}\). The logit-transformed confidence interval can have narrower width and a coverage probability closer to \(1 - \alpha\) than the untransformed Wald confidence interval (Agresti 2013).

3.4.2 Score (Wilson) confidence intervals

The score or Wilson confidence limits come from solving the equation \[ \frac{(\hat{p} - p)^2}{p (1 - p) / n} = z_{1 - \frac{\alpha}{2}}^2. \tag{3.20}\] for \(p\) (Wilson 1927). This differs from Equation 3.16 for the Wald confidence interval because it uses \(p\) instead of \(\hat{p}\) in the denominator. It is a quadratic equation in \(p\), so it has two solutions. The center of the resulting confidence interval is \[ \tilde{p} = \hat{p} \Bigg(\frac{n}{n + z_{1 - \frac{\alpha}{2}}^2}\Bigg) + \frac{1}{2} \Bigg(\frac{z_{1 - \frac{\alpha}{2}}^2}{n + z_{1 - \frac{\alpha}{2}}^2}\Bigg) = \frac{x + \frac{1}{2} z_{1 - \frac{\alpha}{2}}^2}{n + z_{1 - \frac{\alpha}{2}}^2}, \tag{3.21}\] where \(x\) is the number of diseased individuals in our sample. This is a weighted average of \(\hat{p}\) and \(1 / 2\) with weights proportional to \(n\) and \(z_{1 - \frac{\alpha}{2}}^2\), respectively. The resulting confidence interval is \[ \tilde{p} \pm z_{1 - \frac{\alpha}{2}} \sqrt{\tilde{V}} \] where \[ \tilde{V} = \frac{\hat{p} (1 - \hat{p})}{n + z_{1 - \frac{\alpha}{2}}^2} \Bigg(\frac{n}{n + z_{1 - \frac{\alpha}{2}}^2}\Bigg) + \frac{\big(\frac{1}{2}\big)^2}{n + z_{1 - \frac{\alpha}{2}}^2} \Bigg(\frac{z_{1 - \frac{\alpha}{2}}^2}{n + z_{1 - \frac{\alpha}{2}}^2}\Bigg). \] This variance is a weighted average of the variances of sample proportions equal to \(\hat{p}\) and \(1 / 2\) with the same weights as in \(\tilde{p}\) and with \(n + z_{1 - \frac{\alpha}{2}}^2\) instead of \(n\) in the denominator. Wilson confidence intervals are narrower than the corresponding Wald intervals, and they have coverage probabilities much closer to \(1 - \alpha\) (Agresti and Coull 1998; Brown, Cai, and DasGupta 2001).

The Agresti-Coull confidence interval is a simplification of the Wilson confidence interval that replaces \(\hat{p}\) with \(\tilde{p}\) in the Wald confidence interval to get the confidence limits \[ \tilde{p} \pm z_{1 - \frac{\alpha}{2}} \sqrt{\frac{\tilde{p} (1 - \tilde{p})}{n}}. \] Because \(z_{0.975} \approx 1.96\), we have \(\tilde{p} \approx \frac{k + 2}{n + 4}\) for a 95% confidence interval. In this case, the Agresti-Coull interval is often implemented as follows: ``Add two successes and two failures and then use the Wald formula’’ (Agresti and Coull 1998). This interval is only slightly wider than the score confidence interval, and the two intervals are nearly identical for \(n > 40\) (Brown, Cai, and DasGupta 2001).

The likelihood ratio test can also be inverted to get confidence intervals, but these can only be calculated numerically. For the binomial model, the likelihood ratio and score confidence intervals are nearly identical (Agresti and Coull 1998; Brown, Cai, and DasGupta 2001). The score intervals are more common in practice because they are easier to calculate.

binconf.R
## Binomial confidence intervals

# using BinomCI() function from the DescTools package
library(DescTools)
BinomCI(15, 22, method = "wald")            # Wald confidence interval
BinomCI(15, 22, method = "logit")           # logit-transformed Wald CI
BinomCI(15, 22, method = "wilson")          # score CI (default)
BinomCI(15, 22, method = "agresti-coull")   # Agresti-Coull CI
BinomCI(15, 22, method = "lik")             # likelihood ratio CI

# using binconf() function from the Hmisc package
library(Hmisc)
binconf(15, 22, method = "asymptotic")  # Wald CI
binconf(15, 22, method = "wilson")      # score CI (default)

# using prop.test in base R (stats package)
# Wilson confidence interval with continuity correction by default
# The continuity correction is not generally recommended. Like the exact CI,
# it can be too wide and have a coverage probability greater than 1 - \alpha.
prop.test(15, 22)
names(prop.test(15, 22))
prop.test(15, 22, correct = FALSE)      # score CI

# using binom.test (exact confidence interval)
binom.test(15, 22)    # same as binconf with method = "exact"
names(binom.test(15, 22))

# changing the confidence level (1 - alpha) to 80%
# All are score (Wilson) confidence intervals by default.
BinomCI(15, 22, conf.level = 0.8)
binconf(15, 22, alpha = 0.2)
prop.test(15, 22, conf.level = 0.8, correct = FALSE)

# writing a function to get Wald confidence limits
bconf_wald <- function(x, n, level=0.95) {
  # x is number of successes out of n trials
  p_hat <- x / n
  alpha <- 1 - level
  pvar <- p_hat * (1 - p_hat) / n
  p_int <- p_hat + c(-1, 1) * qnorm(1 - alpha / 2) * sqrt(pvar)

  # return named vector (names do not need quotes)
  return(c(point = p_hat, lower = p_int[1], upper = p_int[2]))
}
bconf_wald(15, 22)
bconf_wald(15, 22, level = 0.80)

3.5 Small-sample estimation*

Maximum likelihood estimates are consistent, asymptotically normal, and asymptotically efficient. However, they are not guaranteed to perform well in any finite sample. For a sample of \(n\) independent Bernoulli(\(p\)) random variables, the sum has a binomial(\(n, p\)) distribution and this can be used to find the finite-sample distribution of the sample mean. This distribution can be used directly to calculate point estimates, p-values, and confidence limits.

Confidence limits calculated using the finite-sample distribution of a test statistic under \(H_0\) are called exact confidence limits. They can often be constructed to have a coverage probability of at least \(1 - \alpha\). However, their coverage probabilities are often higher than \(1 - \alpha\), and they can be much wider than approximate \(1 - \alpha\) confidence intervals for the same parameter (Agresti and Coull 1998).

If the finite-sample distribution of the test statistic is not known exactly, it is possible to calculate point estimates, p-values, or confidence limits using simulations. This is the basic idea behind the bootstrap (Efron and Tibshirani 1994) and Monte Carlo methods (Robert and Casella 2004).

3.5.1 Median unbiased estimate

The median unbiased estimate of \(\ptrue\) is the value of \(p\) that makes \[ \Pr\nolimits_p(X < x) = \Pr\nolimits_p(X > x) \] where we use the subscript \(p\) to indicate that these probabilities are calculated assuming \(\ptrue = p\). If \(p_\text{med}\) is the median unbiased estimate, then \[ \sum_{k = 0}^{x - 1} \binom{n}{k} p_\text{med}^k \big(1 - p_\text{med}\big)^{n - k} + \frac{1}{2} \binom{n}{x} p_\text{med}^k \big(1 - p_\text{med}\big)^{n - x} = \frac{1}{2}, \] and \[ \frac{1}{2} \binom{n}{x} p_\text{med}^x \big(1 - p_\text{med}\big)^{n - x} + \sum_{k = x + 1}^n \binom{n}{k} p_\text{med}^k \big(1 - p_\text{med}\big)^{n - k} = \frac{1}{2}. \] The median of the distribution of \(p_\text{med}\) is always \(\ptrue\) (Birnbaum 1964), which is a slightly different notion of unbiasedness than the unbiasedness of \(\hat{p}\) where \(\E(\hat{p}) = \ptrue\).

3.5.2 Exact (Clopper-Pearson) and mid-p confidence intervals

The exact or Clopper-Pearson confidence limits for \(\ptrue\) use the finite-sample distribution of the sample mean \(\hat{p}\) Clopper and Pearson (1934). When \(x > 0\), the lower \(1 - \alpha\) confidence limit is the solution to \[ \sum_{k = x}^n \binom{n}{k} p_\text{lower}^k (1 - p_\text{lower})^{n - k} = \frac{\alpha}{2}, \tag{3.22}\] so the upper tail of the binomial(\(n\), \(p_\text{lower}\)) distribution has probability \(\alpha / 2\). When \(x = 0\), we set \(p_\text{lower} = 0\). When \(x < n\), the upper confidence limit is the solution to \[ \sum_{k = 0}^x \binom{n}{k} p_\text{upper}^k (1 - p_\text{upper})^{n - k} = \frac{\alpha}{2}, \tag{3.23}\] so the lower tail of the binomial(\(n\), \(p_\text{upper}\)) distribution has probability \(\alpha / 2\). When \(x = n\), we set \(p_\text{upper} = 1\). This interval is guaranteed to have a coverage probability of at least \(1 - \alpha\), but the price for this is that it is always wider than the Wald and Wilson confidence intervals (Agresti and Coull 1998; Brown, Cai, and DasGupta 2001). In general, the score or likelihood ratio confidence intervals have better combinations of coverage probability and width.

To make exact confidence limits less conservative, we can include only \(\frac{1}{2} \Pr(X = x)\) instead of \(\Pr(X = x)\) in the calculation of the tail probabilities in Equation 3.23 and Equation 3.22. The resulting confidence intervals are called mid-p exact confidence intervals (Lancaster 1961, berry1995mid). The lower \(1 - \alpha\) mid-p exact confidence limit is the solution to \[ \frac{1}{2} \binom{n}{x} p_\text{lower}^x (1 - p_\text{lower})^{n - x} + \sum_{k = x + 1}^n \binom{n}{k} p_\text{lower}^k (1 - p_\text{lower})^{n - k} = \frac{\alpha}{2}. \] and the upper limit is the solution to \[ \sum_{k = 0}^{x - 1} \binom{n}{k} p_\text{upper}^k (1 - p_\text{upper})^{n - k} + \frac{1}{2} \binom{n}{x} p_\text{upper}^x (1 - p_\text{upper})^{n - x} = \frac{\alpha}{2}. \] The mid-p exact confidence limits are have good combinations of coverage probability and width as well as good perfomance in small samples (Brown, Cai, and DasGupta 2001).

binomial-small.R
## Small-sample binomial point and interval estimates

# median unbiased estimate
medp_binom <- function(k, n) {
  # k = number of successes, n = number of trials

  # binomial lower tail probability
  lower_tail <- function(p) pbinom(k, n, p) - dbinom(k, n, p) / 2

  # median unbiased estimate
  med <- uniroot(function(p) lower_tail(p) - 1 / 2, interval = c(0, 1))
  med$root
}
medp_binom(15, 22)


# exact (Clopper-Pearson) confidence intervals
binom.test(15, 22)                              # base R (stats)
names(binom.test(15, 22))
binom.test(15, 22, conf.level = 0.8)

library(Hmisc)
binconf(15, 22, method = "exact")
binconf(15, 22, method = "exact", alpha = 0.2)

library(DescTools)
BinomCI(15, 22, method = "clopper-pearson")     # exact CI
BinomCI(15, 22, method = "midp")                # mid-p exact CI

  1. John Tukey (1915-2000) was an American mathematician and statistician who worked at Bell Labs and Princeton University. He developed the box plot, Tukey’s range test for multiple comparisons, and the fast Fourier transform. In 1947, he coined the term “bit” as shorthand for “binary digit”.↩︎

  2. She was elected a member of the Royal Statistical Society in 1859, where she was the first woman to be a member. In 1860, she founded the world’s first modern nursing school at St. Thomas Hospital in London.↩︎

  3. For finite \(N\), \(X\) actually has a hypergeometric distribution because the test results are not exactly independent. If the first person in our sample has disease, the probability that the next person we sample has disease is slightly less than \(p\). If the first person in our sample does not have disease, the probability that the next person we sample has disease is slightly greater than \(p\). When \(N \gg n\), this hypergeometric distribution is approximately binomial(\(n\), \(p\)).↩︎

  4. Euler’s number \(e\) is named after Leonhard Euler (1707–1783), a Swiss mathematician who introduced the notation \(f(x)\) for mathematical functions and the letter \(i\) to denote the imaginary unit \(\sqrt{-1}\). He spent most of his life in Berlin and St. Petersburg, and he is widely considered the greatest mathematician of the 18th century. The number \(e\) was first discovered in 1683 by Jacob Bernoulli (the namesake of the Bernoulli distribution) when studying compound interest, where \(e = \lim_{n \rightarrow \infty} (1 + 1 / n)^n\). In 1748, Euler proved that \(e = \frac{1}{0!} + \frac{1}{1!} + \frac{1}{2!} + \frac{1}{3!} + \cdots\).↩︎

  5. This is named for Josiah Willard Gibbs (1839–1903), an American scientist who earned the first American doctorate in engineering in 1863 and went on to work on statistical mechanics, thermodynamics, optics, and vector calculus as a professor of physics at Yale. Albert Einstein called him the greatest mind in American history.↩︎

  6. Named after Ronald Fisher (1890–1962), who established the foundations of maximum likelihood inference between 1912 and 1922. He was the most important statistician of the 20th century, and he was one of the founders of population genetics. He had poor eyesight for his entire life, which led him to develop a formidable sense of geometry in his head. However, he was also a leading eugenicist and one of the most vocal opponents of the hypothesis that smoking causes lung cancer.↩︎

  7. For estimating a parameter \(\theta\), the conditions are these: (1) The set of possible values of the observed data \(X\) does not depend on \(\theta\). (2) Each \(\theta\) produces a different distribution of \(X\). (3) The true value of \(\theta\) is in the interior of the set of possible values. (4) The log likelihood \(\ell(\theta)\) has continuous first and second derivatives with respect to \(\theta\) in a neighborhood of \(\theta_\true\). These conditions are met by the binomial likelihood when \(\ptrue \in (0, 1)\).↩︎

  8. For simplicity, we are being vague about what we mean by \(\hat{\mu}_n \rightarrow \mu\). Probability has several different notions of convergence/. The weak LLN guarantees convergence in probability, which means that \(\lim_{n \rightarrow \infty} \Pr\big(|\hat{\mu}_n - \mu| > \varepsilon\big) = 0\) for any \(\varepsilon > 0\). The strong LLN guarantees convergence almost surely, which means that \(\Pr\big(\lim_{n \rightarrow \infty} \hat{\mu}_n = \mu\big) = 1\).↩︎

  9. Named after Carl Friedrich Gauss (1777-1855), a German mathematician who is widely considered one of the greatest mathematicians of all time. He discovered the normal distribution in 1809, but the CLT itself was first proved by Laplace in 1810 (see Chapter 1).↩︎

  10. Named after Swedish statistician Harald Cramér (1893–1985), who was a professor at Stockholm University, and Indian-American statistician Calyampudi Radhakrishna (C. R.) Rao (1920–2023), who was a professor at the Indian Statistical Institute, the University of Cambridge, the University of Pittsburgh, and Pennsylvania State University.↩︎

  11. Named after Abraham Wald (1902–1950), a Jewish Hungarian mathematician who was invited to move from Vienna to the United States in 1938 after Nazi Germany annexed Austria. He worked at the Statistical Research Group at Columbia University during World War II. In 1950, he and his wife were killed in a plane crash in India, where he was visiting the Indian Statistical Institute.↩︎

  12. Samuel S. Wilks (1906–1964) was an American mathematician and statistician who grew up on a farm in Texas, got a Ph.D. at the University of Iowa, and went on to be a professor at Princeton University.↩︎

  13. This approach to hypothesis testing was pioneered in the 1929s by Jerzy Neyman (1894–1981), a Polish mathematician and statistician who founded the first department of statistics in the United States at the University of California, Berkeley in 1938, and Egon Pearson (1895–1980), a British statistician who was a professor at University College London like his father Karl Pearson. ↩︎