2.1 Bayesian statistics

In the previous section we saw that the difference between the frequentist methods and the Bayesian methods is that the frequentist averages over different samples while the Bayesian averages over different parameter values. The Bayesian method is therefore to assign a prior distribution \(p(\theta)\) to the unknown parameter. This distribution reflects our belief about the parameter before we see the data.

We model how data is generated by \(p(x\mid \theta)\). That is, the probability or density of obtaining a particular sample, conditioned on knowing \(\theta\).

We then calculate the posterior distribution of the parameter, given the observations \[ p(\theta \mid x) = \frac{p(x\mid \theta)p(\theta)}{p(x)}. \]

Here \(p(x)\) is the marginal density of the observed data. We can write it formally as

\[ p(x) = \int p(x,\theta)d\theta = \int p(x\mid \theta)p(\theta)d\theta. \]

However, note that since \(x\) is fixed and we are interested in the distribution of \(\theta\), \(p(x)\) can be regarded as a constant. Also, \(p(x\mid \theta)\) is the likelihood, \(L(\theta)\). We may therefore write

\[ p(\theta\mid x) = c L(\theta)p(\theta) \propto L(\theta)p(\theta). \]

That is, the posterior is proportional to the likelihood times the prior. Another way to write this is by applying the logarithm. \[ \ln p(\theta\mid x) = l(\theta) + \ln p(\theta) + c \] We see that the total information regarding \(\theta\), expressed as the posterior distribution, is a combination of the likelihood obtained from the observations and the knowledge before the observations, expressed in the prior distribution.

Once we have the posterior, we can keep it as it is. Perhaps \(\theta\) is one part of a larger model and by using the full distribution we are able to take full account of the uncertainty. But we might also summarize the posterior in the posterior mean \[ \bar \theta = \int \theta p(\theta\mid x)d\theta, \] or construct a \(1-\alpha\) posterior interval That is \(a\) and \(b\) such that \[ P\left( a\leq \theta \leq b \right) = \int_a^b p(\theta\mid x)d\theta = 1-\alpha. \] We might also want to predict a new observation \(x^\text{new}\), \[ p(x^\text{new}\mid x) = \int p(x^\text{new}\mid \theta) p(\theta\mid x)d\theta. \]

Let us examine a simple example: We flip a coin \(n\) times and we want to make Bayesian inference regarding the probability of heads, \(\theta\). The first step is to decide on the data generating model. It seems natural to assume that \(X_1,\ldots X_n \overset{iid}\sim \mathsf{Bernoulli}(\theta)\). The second step is to decide on a prior distribution of \(\theta\). Since \(\theta\) represents a probability, the prior distribution should be confined to \([0,1]\). A popular choice of prior distribution on probabilities is the beta distribution. It has density,

\[ p(\theta) = \frac{\theta^{\alpha-1}(1-\theta)^{\beta-1}}{B(\alpha,\beta)},\quad 0\leq \theta\leq 1, \]

where \(\alpha>0\) and \(\beta>0\) are parameters. Also, \(B(\alpha,\beta)\) is the beta function, defined simply such that the density integrates to one. We plot the density for a few choices of \(\alpha\) and \(\beta\).

## Warning: package 'ggplot2' was built under R version 4.6.1
## Warning: package 'latex2exp' was built under R version 4.6.1
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
Density of the beta distribution

Figure 2.1: Density of the beta distribution

We see from the figure that by changing \(\alpha\) and \(\beta\) the beta prior can reflect different kinds of prior information. For example, \(\alpha = \beta = 1\) is a uniform distribution, that is a prior that gives equal probability to any value of \(\theta\). On the other hand, \(\alpha =3\), \(\beta = 2\) gives small probability to \(\theta\) close to 0 and 1 and puts more probability to \(\theta>0.5\) than \(\theta < 0.5\).

Now let us calculate the posterior distribution of \(\theta\) after observing \(x=(x_1,\ldots, x_n)\). First we need the likelihood of the observation \[ L(\theta) = \prod_{i=1}^n\theta^{x_i}(1-\theta)^{1-x_i} = \theta^{n\bar x}(1-\theta)^{n-n\bar x}, \] where \(n \bar x = \sum_i x_i\). Then the posterior is \[ p(\theta\mid x) \propto L(\theta)p(\theta) \propto \theta^{n\bar x}(1-\theta)^{n-n\bar x} \theta^{\alpha-1}(1-\theta)^{\beta-1} = \theta^{\alpha + n\bar x -1}(1-\theta)^{\beta + n - n\bar x -1}, \] we recognize this as a \(\mathsf{Beta}(\alpha+n\bar x, \beta+n-n\bar x)\) distribution. In this situation, when the prior and posterior happen to be in the same family of distributions, the prior is said to be conjugate with respect to the model.

From here we easily get that the mean of the posterior is \[ E\left[ \theta \mid x \right] = \frac{\alpha + n\bar x}{\alpha+\beta + n}. \] It can be interesting to note here that as \(n\to \infty\), the above converges to \(\bar x\), i.e. for a large sample, the influence of the prior becomes small and the mean of the posterior is simply the MLE. If we wish to predict a new sample, we would calculate \[ p(x^\text{new} = 1 \mid x^n) = \int p(x^\text{new} = 1\mid \theta) p(\theta\mid x^n)d\theta = \int \theta p(\theta \mid x^n)d\theta = E\left[ \theta \mid x^n \right]. \] To make things more concrete, let us say that we observe \(\bar x = 0.3\). Note how the likelihood, and therefore also the posterior, is a function of data only through \(\bar x\). This is because \(\bar x\) is a sufficient statistic. As an illustration, let us also choose \(\mathsf{Beta}(5.0, 2.0)\) as the prior. Below we plot the prior, likelihood and posterior for \(n=10\) and \(n=100\). Note how the posterior becomes more like the likelihood as the sample size increase.

Prior, likelihood and posterior when n = 10 (top) and n=100 (bottom)

Figure 2.2: Prior, likelihood and posterior when n = 10 (top) and n=100 (bottom)