Bayesian StatisticsContents

Bayesian Statistics

Notes to accompany the lectures

Dr Ben Powell, Department of Mathematics, University of York

Autumn 2026

About these notes

These notes accompany the lecture slides and recordings for the Bayesian statistics half of Bayesian Statistics and Decision Theory. The slides are deliberately brief. They serve as an outline, and much of the explanation is given aloud in the lectures. The notes are meant to supply that explanation in written form, so that the argument can be followed at your own pace and returned to when revising.

The notes follow the order of the slides. Where the slides state a result, the notes usually derive it, and where the lectures pause for a calculation, the notes give the calculation in full. Along the way they try to say not only what is true but why it is worth knowing, and how one idea leads to the next.

The notes are not a substitute for a textbook. Much of the material follows Hoff, A First Course in Bayesian Statistical Methods (2009), and references to Hoff’s sections are given where they help. Bernardo and Smith, Bayesian Theory, gives a more thorough account of the foundations, particularly of exchangeability and the justification of probability as a measure of belief. Gelman, Carlin, Stern and Rubin, Bayesian Data Analysis (abbreviated GCSR), is the source of several of the data sets and is the best general reference for applied work.

Each chapter ends with a few exercises. Each exercise is followed by its solution, hidden until you open it. It is worth making a genuine attempt at each exercise before opening its solution. Much of the understanding comes from the attempt, including the attempts that fail.

0.0.0.0.1 Notation.

We write p(⋅)p(\cdot) for a probability density or mass function, and let its argument indicate which distribution is meant: p(θ)p(\theta) is the prior, p(y∣θ)p(y\mid\theta) the sampling model, p(θ∣y)p(\theta\mid y) the posterior. This is an abuse of notation, since the three are different functions, but it is universal in Bayesian work and causes little confusion in practice. When we need to name a distribution explicitly we write, for example, Ga⁡(θ∣a,b)\operatorname{Ga}(\theta\mid a,b) for the Ga⁡(a,b)\operatorname{Ga}(a,b) density evaluated at θ\theta. The Gamma distribution is parameterised throughout by its shape aa and rate bb, so that its mean is a/ba/b.

1 The idea

This chapter sets out the central idea of the course: that probability can be used to represent what a person knows, and that Bayes’ theorem then tells us how that knowledge should change in the light of data. The rest of the course develops this idea for particular models and shows how to carry out the necessary calculations.

1.1 What is a random variable?

Before we can speak of Bayesian inference we need to be clear about what a probability is meant to describe. There are two common answers, and the difference between them is the root of most of what distinguishes Bayesian statistics from the methods you may have met before.

In the frequentist view, a random variable is the outcome of an experiment or game that could, at least in principle, be repeated indefinitely under the same conditions. The probability of an outcome is the long-run proportion of repetitions in which it occurs. This is how Fermat and Pascal thought about games of dice in the seventeenth century, and it remains a natural way to think about coins, cards and many physical processes. On this view a probability is a property of the system being studied, in the same way that its mass or length is.

In the Bayesian view, a random variable is any quantity whose value is unknown to us, and its probability distribution describes our uncertainty about it. A probability is then a property of a person, or more precisely of a person’s state of information, rather than of the system alone. Two people with different information may quite reasonably assign different probabilities to the same event.

The Bayesian view is the wider of the two. The number of heads in ten tosses of a coin is a random variable on either view. But consider the proportion of voters in Scotland who favoured independence in September 2014, the speed of light, or whether a particular patient has a particular disease. Each of these has a definite value. Nothing about them is repeatable, and on the frequentist view it makes no sense to give them probability distributions. We are nonetheless uncertain about them, and the Bayesian is content to describe that uncertainty by a probability distribution. This is what allows a Bayesian to make statements such as ‘the probability that the disease is present is 0.05’ or ‘there is a 95% probability that the true value lies in this interval’, which have no direct frequentist meaning.

1.2 Learning with Bayes’ theorem

Bayesian inference is the process of learning from data by means of Bayes’ theorem. Schematically, current beliefs+new information→ Bayes’ theorem updated beliefs.\text{current beliefs} + \text{new information} \xrightarrow{\ \text{Bayes' theorem}\ } \text{updated beliefs}. In most statistical problems the beliefs in question concern the values of parameters that describe a population or a process: the mean height of people in York, say, or the rate at which a machine fails. The new information usually takes the form of a sample of observations whose distribution depends on those parameters. Beliefs, both before and after the data are seen, are expressed as probability distributions over the possible values of the parameters.

We shall use the following notation throughout.

  • θ\theta denotes the parameter of interest, and Θ\Theta the set of its possible values, called the parameter space. The parameter may be a single number or a vector.

  • yy denotes the observed data, and Y\mathcal{Y} the set of possible observations, called the sample space.

  • p(θ)p(\theta) is the prior distribution. It describes our beliefs about θ\theta before the data are seen.

  • p(y∣θ)p(y\mid\theta) is the sampling model. For each possible value of θ\theta it gives the probability (or probability density) of observing the data yy if θ\theta were the true value. When we fix yy at the value actually observed and regard p(y∣θ)p(y\mid\theta) as a function of θ\theta, we call it the likelihood.

  • p(θ∣y)p(\theta\mid y) is the posterior distribution. It describes our beliefs about θ\theta after the data have been seen.

The prior and the sampling model together specify a joint distribution for θ\theta and yy, namely p(θ,y)=p(y∣θ) p(θ)p(\theta,y)=p(y\mid\theta)\,p(\theta). The posterior is the conditional distribution of θ\theta given yy under this joint distribution. Bayes’ theorem is simply the rule for computing it: p(θ∣y)=p(y∣θ) p(θ)p(y)=p(y∣θ) p(θ)∫Θp(y∣θ~) p(θ~) dθ~.(1.1)\tag{1.1} p(\theta\mid y) = \frac{p(y\mid\theta)\,p(\theta)}{p(y)} = \frac{p(y\mid\theta)\,p(\theta)}{\int_\Theta p(y\mid\tilde\theta)\,p(\tilde\theta)\,\mathrm{d}\tilde\theta}. (If θ\theta is discrete, the integral is replaced by a sum.)

The denominator p(y)p(y) deserves a comment. It is obtained by averaging the probability of the observed data over all possible values of the parameter, weighted by the prior. It is therefore the probability of what was actually observed, computed before the data were seen, and it is called the marginal likelihood or prior predictive probability of the data. It does not depend on θ\theta, and this is what matters for calculation. Its only role in (1.1) is to make the posterior integrate to one. For this reason we often write Bayes’ theorem as a proportionality, p(θ∣y)∝p(y∣θ) p(θ),p(\theta\mid y) \propto p(y\mid\theta)\,p(\theta), or, in words, posterior is proportional to likelihood times prior. The prior and the information from the data combine by multiplication, and the product is then rescaled to integrate to one. A value of θ\theta is favoured by the posterior if it was plausible beforehand and if it makes the observed data probable; a value that fails on either count is discounted.

The marginal likelihood will play little part in this half of the module, but it becomes important when competing models are to be compared, since it measures how well each model predicted the data before they were seen.

This gives us the central idea of the course. Bayesian statistics describes how an idealised agent ought to learn from data, provided two conditions hold. First, the agent’s knowledge must be expressible, at least approximately, as a probability distribution. Second, the agent must be able to carry out the necessary calculations, and in particular to evaluate the integral in (1.1). Neither condition is trivial. The first raises questions about where priors come from, which we take up in Chapter 10. The second is a matter of computation, and a large part of the course, from Chapter 8 onwards, is concerned with it.

1.3 A diagnostic test

The following example is simple, but it displays in miniature most of what Bayesian reasoning involves, and it shows how easily intuition can go astray without it.

A doctor is concerned that a patient may have a rare but serious disease. Let θ=1\theta=1 if the patient has the disease and θ=0\theta=0 otherwise, and let y=1y=1 if a diagnostic test returns a positive result and y=0y=0 otherwise. Here both the parameter and the data are binary.

1.3.0.0.1 The prior.

The doctor knows that about one person in a thousand in the relevant population has the disease. Before any test is done, and in the absence of other information about this patient, the doctor’s prior is therefore p(θ=1)=0.001,p(θ=0)=0.999.p(\theta=1) = 0.001, \qquad p(\theta=0) = 0.999.

1.3.0.0.2 The sampling model.

The test has been evaluated in the laboratory. When the disease is present it returns a positive result 95% of the time; when the disease is absent it returns a (false) positive 2% of the time: p(y=1∣θ=1)=0.95,p(y=1∣θ=0)=0.02.p(y=1\mid\theta=1) = 0.95, \qquad p(y=1\mid\theta=0) = 0.02. The first of these numbers is called the sensitivity of the test, and one minus the second, 0.980.98, is its specificity. Both look reassuringly high.

1.3.0.0.3 The posterior.

The test comes back positive. By Bayes’ theorem, p(θ=1∣y=1)=p(y=1∣θ=1) p(θ=1)p(y=1∣θ=1) p(θ=1)+p(y=1∣θ=0) p(θ=0)=0.95×0.0010.95×0.001+0.02×0.999.\begin{aligned} p(\theta=1\mid y=1) &= \frac{p(y=1\mid\theta=1)\,p(\theta=1)}{p(y=1\mid\theta=1)\,p(\theta=1)+p(y=1\mid\theta=0)\,p(\theta=0)}\\ &= \frac{0.95\times0.001}{0.95\times0.001 + 0.02\times0.999}. \end{aligned} The numerator is 0.000950.00095 and the denominator is 0.00095+0.01998=0.020930.00095+0.01998=0.02093, so p(θ=1∣y=1)≈0.045,p(θ=0∣y=1)≈0.955.p(\theta=1\mid y=1)\approx0.045, \qquad p(\theta=0\mid y=1)\approx0.955. Despite the positive result, the patient probably does not have the disease.

Most people find this surprising at first, and it is worth seeing why the answer is right. Imagine testing a thousand people drawn from the population. About one of them has the disease, and that person will very probably test positive. Of the 999 who do not have the disease, about 2%, or roughly twenty, will also test positive. A positive result therefore places our patient in a group of about twenty-one people, only one of whom is ill. The test has done something: it has raised the probability of disease from one in a thousand to about one in twenty-two, an increase by a factor of more than forty. But it began from so low a base that the disease remains unlikely.

The example shows the two ingredients of Bayesian reasoning working together. The prior, which reflects the rarity of the disease, and the likelihood, which reflects the behaviour of the test, both matter, and neither alone determines the conclusion. A doctor who looked only at the test’s accuracy, ignoring the prior, would badly overstate the chance of disease. The error is common enough to have a name, the base rate fallacy.

It also suggests what the doctor should do next. A single positive result is not conclusive, so a sensible course is to repeat the test, or to use a different and more specific one. Exercise 1.1 shows how much a second positive result would change matters.

1.4 Objections to Bayesian inference

For much of the twentieth century there was a divide, at times a hostile one, between frequentist and Bayesian statisticians. It is worth knowing the main lines of the argument, both because they clarify what Bayesian methods claim to do and because you will meet them in practice.

The principal frequentist objection is that Bayesian inference is not objective. Science, on this view, is meant to reach conclusions that any competent investigator would reach from the same evidence. If the conclusions of an analysis depend on the prior beliefs of the analyst, then two scientists looking at the same data may disagree, and the conclusions seem to say as much about the scientists as about the world.

The Bayesian reply comes in several parts. The first is that data acquire meaning only by changing what we believe; a set of numbers without any context about what they measure or what was expected tells us nothing. The second is that frequentist methods also rest on choices and assumptions (of model, of test statistic, of significance level, of stopping rule) and that these play a role similar to that of a prior, though less openly. Some Bayesians go further and argue that frequentist procedures, where they are sensible, are Bayesian procedures with an unstated prior.

A Bayesian may respond to the charge of subjectivity in either of two ways. One is to accept it. On this view Bayesian inference is a tool for an individual, with genuinely held beliefs, to learn from data and to make decisions, and there is nothing to apologise for in that. The Decision Theory half of the module develops this view further. The other is to make statements that do not depend on any one person’s beliefs. For example, one can say: these data are sufficient to move anyone who held a prior of such a form to a posterior of such a form. The information in the data is then measured by its capacity to change the minds of a range of hypothetical people. Reporting how conclusions vary as the prior is varied, known as a sensitivity analysis, is a practical form of the same idea. And, as we shall see repeatedly, when the data are plentiful the posterior depends very little on the prior, so that people who begin with different beliefs are brought into agreement by the evidence.

Exercises

Exercise 1.1

The doctor repeats the test and both results are positive. The two results are independent given the patient’s disease status. Find the posterior probability that the patient has the disease. Check that you get the same answer whether you use both results at once or update the prior with one result and then update the resulting posterior with the other.

Show solutionHide solution

Let y=(1,1)y=(1,1). By conditional independence, p(y∣θ=1)=0.952=0.9025p(y\mid\theta=1)=0.95^2=0.9025 and p(y∣θ=0)=0.022=0.0004p(y\mid\theta=0)=0.02^2=0.0004. So p(θ=1∣y)=0.9025×0.0010.9025×0.001+0.0004×0.999=0.00090250.0013021≈0.69.p(\theta=1\mid y) = \frac{0.9025\times0.001}{0.9025\times0.001+0.0004\times0.999} = \frac{0.0009025}{0.0013021}\approx0.69. Updating in two stages gives the same answer. After the first test the posterior probability of disease is 0.04540.0454. Using this as the prior for the second test, 0.95×0.04540.95×0.0454+0.02×0.9546=0.043130.06222≈0.69.\frac{0.95\times0.0454}{0.95\times0.0454+0.02\times0.9546}=\frac{0.04313}{0.06222}\approx0.69. The second positive result is much more persuasive than the first, not because the test has changed but because it is now applied to a patient who is already under suspicion.

Exercise 1.2

Repeat Exercise 1.1 using the odds form of Bayes’ theorem (Section 2.3.1). What does each positive result do to the odds of disease?

Show solutionHide solution

The prior odds of disease are 0.001/0.999≈0.0010.001/0.999\approx0.001. Each positive result has Bayes factor 0.95/0.02=47.50.95/0.02=47.5. After one test the odds are 0.04750.0475, a probability of 0.04540.0454. After two they are 47.52×0.001/0.999≈2.2647.5^2\times0.001/0.999\approx2.26, a probability of 2.26/3.26≈0.692.26/3.26\approx0.69. Each positive result multiplies the odds by 47.547.5. With independent tests, evidence accumulates additively on the log-odds scale.

2 A review of probability

This chapter reviews the parts of probability theory that we shall use. Most of it will be familiar. The emphasis, however, is slightly different from that of a first course: we are interested in probability as a description of uncertain knowledge, and so we pay particular attention to conditional probability, which is the mathematical form of learning.

2.1 Sample spaces, events and probabilities

A sample space Ω\Omega is a set of possible outcomes, or states of the world. One can think of it as an ‘atomic’ description: each outcome specifies everything that is relevant to the problem at hand. For a single roll of a die, Ω={1,2,3,4,5,6}\Omega=\{1,2,3,4,5,6\}. For a patient undergoing a test, Ω\Omega might consist of the four combinations of disease status and test result.

An event E⊆ΩE\subseteq\Omega is a set of outcomes, and we say that EE occurs if the outcome that is realised belongs to it. For the die, ‘an even number is rolled’ is the event {2,4,6}\{2,4,6\}. An event space FF is a collection of events to which probabilities will be assigned. One can think of it as describing what we are able to observe or ask about. (For technical reasons, when Ω\Omega is uncountable, it is not always possible to assign probabilities consistently to every subset, and FF must be restricted to a suitable collection called a σ\sigma-algebra. We shall not need these details.)

A probability function PP assigns a number P(E)P(E) to each event EE in FF.

2.2 Axioms

Kolmogorov, in 1933, gave the axioms that now form the standard basis of probability theory. A triple (Ω,F,P)(\Omega,F,P) is called a probability space if

  1. P(E)≥0P(E)\ge0 for all E∈FE\in F;

  2. P(Ω)=1P(\Omega)=1;

  3. P(⋃i=1∞Ei)=∑i=1∞P(Ei)P\big(\bigcup_{i=1}^\infty E_i\big)=\sum_{i=1}^\infty P(E_i) whenever the events E1,E2,…E_1,E_2,\ldots are pairwise disjoint, that is, Ei∩Ej=∅E_i\cap E_j=\emptyset for i≠ji\ne j.

Here Ei∪EjE_i\cup E_j is the event that EiE_i or EjE_j (or both) occurs, Ei∩EjE_i\cap E_j the event that both occur, and ∅\emptyset the empty set. In Kolmogorov’s system, conditional probability is then defined by P(Ei∣Ej)=P(Ei∩Ej)P(Ej),provided P(Ej)>0.P(E_i\mid E_j) = \frac{P(E_i\cap E_j)}{P(E_j)}, \qquad \text{provided } P(E_j)>0.

Kolmogorov’s axioms say nothing about what probability means. They are equally at home with frequencies and with degrees of belief. Bayesians have therefore asked a different question: what properties ought a person’s degrees of belief to have if they are to be coherent? Hoff, following de Finetti and most Bayesian authors, lists the following:

  1. 0≤P(E)≤10\le P(E)\le1 for all EE;

  2. P(Ei∪Ej)=P(Ei)+P(Ej)P(E_i\cup E_j)=P(E_i)+P(E_j) if Ei∩Ej=∅E_i\cap E_j=\emptyset;

  3. P(Ei∩Ej)=P(Ei∣Ej) P(Ej)P(E_i\cap E_j)=P(E_i\mid E_j)\,P(E_j).

In this system conditional probability is a primitive notion. P(Ei∣Ej)P(E_i\mid E_j) is the degree of belief one would have in EiE_i on learning that EjE_j is true, and Axiom P3 is a requirement that such conditional beliefs fit together consistently with unconditional ones. The conclusion is that degrees of belief should obey the same rules as Kolmogorov’s probabilities, so that the whole of probability theory is available to describe them. (The one difference, that de Finetti’s system requires additivity only for finitely many events rather than countably many, will not concern us.)

Why should degrees of belief satisfy these rules? One well-known argument, due to de Finetti, uses betting. Suppose your degree of belief in an event EE is the price P(E)P(E) at which you would be willing either to buy or to sell a ticket that pays £1 if EE occurs and nothing otherwise. If your prices violate the axioms, then an opponent can construct a collection of bets, each of which you regard as fair, that together lose you money whatever happens. Such a collection is called a Dutch book. For example, if you set P(E)=0.6P(E)=0.6 and P(Ec)=0.6P(E^c)=0.6, violating P2 (since EE and its complement are disjoint with union Ω\Omega), the opponent sells you both tickets for £1.20; exactly one of them pays out, so you receive £1 and lose 20 pence for certain. Beliefs that cannot be exploited in this way are called coherent, and it can be shown that coherence is equivalent to satisfying the axioms. Arguments of this kind are developed further in the Decision Theory half of the module.

2.3 Partitions and Bayes’ rule

Many calculations involve dividing the sample space into a number of mutually exclusive possibilities.

Definition 2.1. A collection of sets {H1,H2,…}\{H_1,H_2,\ldots\} is a partition of a set HH if

  1. the sets are disjoint, Hi∩Hj=∅H_i\cap H_j=\emptyset for i≠ji\ne j, and

  2. their union is HH, that is, ⋃kHk=H\bigcup_kH_k=H.

In other words, exactly one of the HkH_k is true whenever HH is. In the diagnostic example, the events ‘disease present’ and ‘disease absent’ form a partition of Ω\Omega.

Suppose that {H1,H2,…}\{H_1,H_2,\ldots\} is a partition of Ω\Omega, so that ∑kP(Hk)=1\sum_kP(H_k)=1, and let EE be any event with P(E)>0P(E)>0. Two results follow directly from the axioms.

  • The law of total probability, or marginal probability rule: P(E)=∑kP(E∩Hk)=∑kP(E∣Hk) P(Hk).P(E) = \sum_k P(E\cap H_k) = \sum_k P(E\mid H_k)\,P(H_k). The first equality holds because the events E∩HkE\cap H_k partition EE; the second is Axiom P3 applied to each term. In words, the probability of EE is a weighted average of its conditional probabilities under each hypothesis, weighted by the probabilities of the hypotheses.

  • Bayes’ rule: P(Hj∣E)=P(E∣Hj) P(Hj)P(E)=P(E∣Hj) P(Hj)∑kP(E∣Hk) P(Hk).P(H_j\mid E) = \frac{P(E\mid H_j)\,P(H_j)}{P(E)} = \frac{P(E\mid H_j)\,P(H_j)}{\sum_k P(E\mid H_k)\,P(H_k)}. This follows by writing P(Hj∩E)P(H_j\cap E) in two ways using Axiom P3, P(Hj∣E)P(E)=P(E∣Hj)P(Hj)P(H_j\mid E)P(E)=P(E\mid H_j)P(H_j), and then using the law of total probability for the denominator.

The statistical reading of Bayes’ rule is the one we met in Chapter 1. Think of the HkH_k as competing hypotheses about the state of the world, perhaps corresponding to different values of a parameter, and of EE as the event that an experiment produces a particular result. Bayes’ rule converts the probabilities P(E∣Hk)P(E\mid H_k) of the result under each hypothesis, which are usually what a scientific model provides, into the probabilities P(Hk∣E)P(H_k\mid E) of the hypotheses given the result, which are what we actually want to know.

2.3.1 Odds and Bayes factors

When attention is on just two hypotheses, HiH_i and HjH_j, it is often convenient to work with the ratio of their probabilities. Writing Bayes’ rule for each and dividing, the denominator P(E)P(E) cancels: P(Hi∣E)P(Hj∣E)=P(E∣Hi)P(E∣Hj)×P(Hi)P(Hj).\frac{P(H_i\mid E)}{P(H_j\mid E)} = \frac{P(E\mid H_i)}{P(E\mid H_j)}\times\frac{P(H_i)}{P(H_j)}. The ratio on the left is the posterior odds of HiH_i against HjH_j, and the second ratio on the right is the prior odds. The first ratio on the right, which depends only on how probable the data are under each hypothesis, is called the Bayes factor. So posterior odds=Bayes factor×prior odds.\text{posterior odds} = \text{Bayes factor}\times\text{prior odds}. This form separates cleanly the contribution of the data from that of prior belief. In the diagnostic example, the Bayes factor for disease against no disease after a positive test is 0.95/0.02=47.50.95/0.02=47.5. Whatever one’s prior odds, a positive result multiplies them by 47.547.5. The prior odds of about 1/9991/999 become posterior odds of about 47.5/999≈0.04847.5/999\approx0.048, corresponding to the probability 0.0450.045 found earlier. If there are only two hypotheses, a posterior odds of rr corresponds to a posterior probability of r/(1+r)r/(1+r).

2.4 Independence and conditional independence

Events FF and GG are independent if P(F∩G)=P(F) P(G),P(F\cap G) = P(F)\,P(G), and they are conditionally independent given HH if P(F∩G∣H)=P(F∣H) P(G∣H).P(F\cap G\mid H) = P(F\mid H)\,P(G\mid H). Axiom P3, applied conditionally on HH, gives P(F∩G∣H)=P(G∣H) P(F∣H∩G)P(F\cap G\mid H)=P(G\mid H)\,P(F\mid H\cap G). Comparing this with the definition shows that, provided P(G∣H)>0P(G\mid H)>0, conditional independence of FF and GG given HH is equivalent to P(F∣H∩G)=P(F∣H).P(F\mid H\cap G) = P(F\mid H). This is the more intuitive form: if we already know that HH is true, then learning that GG is also true does not change our belief about FF.

Independence and conditional independence are different properties, and neither implies the other. The distinction is central to this course, and the diagnostic example illustrates it well. Suppose the test is applied twice, and let FF and GG be the events that the first and second results are positive. Given the patient’s disease status, the two results are independent, since each depends only on the behaviour of the test. But before we know the disease status, they are not independent. A positive first result makes disease more likely, and so makes a positive second result more likely too. Indeed, using the numbers from Chapter 1, P(G)=0.95×0.001+0.02×0.999≈0.021,P(G∣F)=P(F∩G)P(F)=0.952×0.001+0.022×0.9990.021≈0.062,\begin{aligned} P(G) &= 0.95\times0.001+0.02\times0.999\approx0.021,\\ P(G\mid F) &= \frac{P(F\cap G)}{P(F)} = \frac{0.95^2\times0.001+0.02^2\times0.999}{0.021}\approx0.062, \end{aligned} so learning FF roughly triples the probability of GG. The dependence arises entirely through the unknown disease status.

Almost every model in this course has this structure. The observations are modelled as independent given the value of a parameter, but since the parameter is unknown the observations are not independent unconditionally. It is precisely this dependence that allows us to learn: each observation tells us something about the parameter, and so something about the observations yet to come. Chapter 3 makes this idea precise.

2.5 Random variables

To a Bayesian, any unknown quantity may be treated as a random variable, and its probability distribution describes our uncertainty about it. We distinguish two main kinds.

2.5.1 Discrete random variables

A random variable YY is discrete if the set Y\mathcal{Y} of its possible values is countable (finite, or listable as a sequence). Its distribution is described by a probability mass function p(y)=P(Y=y),p(y) = P(Y=y), which satisfies 0≤p(y)≤10\le p(y)\le1 for all y∈Yy\in\mathcal{Y}, ∑y∈Yp(y)=1\sum_{y\in\mathcal{Y}}p(y)=1, and P(Y∈A)=∑y∈Ap(y)for A⊆Y.P(Y\in A) = \sum_{y\in A}p(y) \qquad \text{for } A\subseteq\mathcal{Y}. The Bernoulli, binomial and Poisson distributions are examples.

Often Y\mathcal{Y} is a set of integers, which have a natural ordering. We can then define the cumulative distribution function (CDF), F(y)=P(Y≤y).F(y) = P(Y\le y). It is non-decreasing, tends to 00 as y→−∞y\to-\infty and to 11 as y→∞y\to\infty, and satisfies P(a<Y≤b)=F(b)−F(a)P(a<Y\le b)=F(b)-F(a). For a discrete random variable, a plot of FF is a staircase, jumping upwards at each point of Y\mathcal{Y} by an amount equal to the probability of that point.

2.5.2 Continuous random variables

A random variable YY is (absolutely) continuous if its CDF is an (absolutely) continuous function. It then has a probability density function p(y)p(y), related to the CDF by F(y)=∫−∞yp(u) du,F(y) = \int_{-\infty}^y p(u)\,\mathrm{d}u, so that p(y)=F′(y)p(y)=F'(y) wherever the derivative exists, and more generally P(Y∈A)=∫Ap(y) dy.P(Y\in A) = \int_A p(y)\,\mathrm{d}y. The exponential and normal distributions are examples, and Y\mathcal{Y} is usually an interval of the real line.

We use the same symbol pp for densities as for mass functions, because the two play the same role in almost every formula we shall write, with sums replaced by integrals. But the two are not the same kind of object, and it is important not to confuse them. A density is not a probability. For a continuous random variable, P(Y=y)=0P(Y=y)=0 for every single value yy, and the density at a point may be larger than one. What the density does tell us is the probability of a small interval: P(y<Y≤y+δ)≈p(y) δfor small δ>0.P(y<Y\le y+\delta) \approx p(y)\,\delta \qquad\text{for small }\delta>0. So a density measures probability per unit length, much as physical density measures mass per unit volume. This is why densities change when the variable is rescaled or transformed, as we see in Section 2.6.

2.5.3 Summaries of a distribution

A distribution is a complete description of uncertainty about a quantity, but it is often useful to summarise it by a few numbers describing its location and spread.

The mean, or expectation, of YY is E⁡[Y]=∑y∈Yy p(y)(discrete),E⁡[Y]=∫Yy p(y) dy(continuous).\operatorname{E}[Y] = \sum_{y\in\mathcal{Y}} y\,p(y) \quad\text{(discrete)}, \qquad \operatorname{E}[Y]=\int_\mathcal{Y}y\,p(y)\,\mathrm{d}y \quad\text{(continuous)}. It is the centre of mass of the distribution. Two other measures of location are the mode, the most probable value (for a continuous variable, the value at which the density is greatest), and the median, which divides the distribution into two halves of equal probability. For a symmetric distribution with a single peak the three coincide. For a skewed distribution they may differ considerably, and the choice between them is not merely technical: as the Decision Theory half of the module will show, each is the best single guess under a different notion of what it means for a guess to be good.

The variance of YY is the expected squared distance from the mean, Var⁡[Y]=E⁡[(Y−E⁡[Y])2],\operatorname{Var}[Y]=\operatorname{E}\big[(Y-\operatorname{E}[Y])^2\big], and its square root is the standard deviation. The variance is often easier to compute from the formula Var⁡[Y]=E⁡[Y2]−E⁡[Y]2,\operatorname{Var}[Y] = \operatorname{E}[Y^2]-\operatorname{E}[Y]^2, which follows by expanding the square and using linearity of expectation.

Other measures of spread are based on quantiles. If FF is continuous and strictly increasing, the α\alpha-quantile of YY is the value yαy_\alpha such that F(yα)=P(Y≤yα)=α.F(y_\alpha) = P(Y\le y_\alpha) = \alpha. The median is y0.5y_{0.5}. Two commonly used intervals are the interquartile range, the length of the interval (y0.25,y0.75)(y_{0.25},y_{0.75}) that contains the middle half of the distribution, and the 95% central interval (y0.025,y0.975)(y_{0.025},y_{0.975}), which leaves probability 0.0250.025 in each tail. When the distribution in question is a posterior, this last interval is called a 95% central posterior interval or credible interval, and it will be our main way of reporting uncertainty about a parameter.

2.5.4 Conditional expectation

Two identities for conditional expectations will be used many times, particularly when we come to prediction. For random variables XX and YY, E⁡[Y]=E⁡[E⁡[Y∣X]],(2.1)\tag{2.1} \operatorname{E}[Y] = \operatorname{E}\big[\operatorname{E}[Y\mid X]\big], Var⁡[Y]=E⁡[Var⁡[Y∣X]]+Var⁡[E⁡[Y∣X]].(2.2)\tag{2.2} \operatorname{Var}[Y] = \operatorname{E}\big[\operatorname{Var}[Y\mid X]\big] + \operatorname{Var}\big[\operatorname{E}[Y\mid X]\big]. The first, sometimes called the tower property, says that the overall mean of YY can be found by first computing the mean of YY for each value of XX and then averaging these over the distribution of XX. It is the analogue for expectations of the law of total probability.

The second, the law of total variance, says that the variability of YY has two sources: the variability of YY about its conditional mean for fixed XX, averaged over XX; and the variability of the conditional mean itself as XX varies. To prove it, write m(X)=E⁡[Y∣X]m(X)=\operatorname{E}[Y\mid X]. Then Var⁡[Y∣X]=E⁡[Y2∣X]−m(X)2\operatorname{Var}[Y\mid X]=\operatorname{E}[Y^2\mid X]-m(X)^2, so by (2.1) E⁡[Var⁡[Y∣X]]=E⁡[Y2]−E⁡[m(X)2],Var⁡[m(X)]=E⁡[m(X)2]−E⁡[m(X)]2=E⁡[m(X)2]−E⁡[Y]2,\begin{aligned} \operatorname{E}\big[\operatorname{Var}[Y\mid X]\big] &= \operatorname{E}[Y^2]-\operatorname{E}[m(X)^2],\\ \operatorname{Var}[m(X)] &= \operatorname{E}[m(X)^2]-\operatorname{E}[m(X)]^2 = \operatorname{E}[m(X)^2]-\operatorname{E}[Y]^2, \end{aligned} and adding these gives E⁡[Y2]−E⁡[Y]2=Var⁡[Y]\operatorname{E}[Y^2]-\operatorname{E}[Y]^2=\operatorname{Var}[Y].

2.6 Transformations

We shall often need the distribution of a function of a random variable: of 1/θ1/\theta when we know the distribution of θ\theta, for example. For discrete variables this is a matter of adding up probabilities. For continuous variables the densities must be adjusted.

Let YY be continuous with density pYp_Y, and let W=f(Y)W=f(Y), where ff is one-to-one and continuously differentiable. Then WW has density pW(w)=pY(f−1(w))∣ dy dw∣,p_W(w) = p_Y\big(f^{-1}(w)\big)\left|\frac{\,\mathrm{d}y}{\,\mathrm{d}w}\right|, where y=f−1(w)y=f^{-1}(w). To see why, suppose ff is increasing. Then P(W≤w)=P(Y≤f−1(w))=FY(f−1(w))P(W\le w)=P(Y\le f^{-1}(w))=F_Y(f^{-1}(w)), and differentiating with respect to ww by the chain rule gives the formula. If ff is decreasing, the same argument gives the formula with a minus sign, which the absolute value takes care of. The factor ∣ dy/ dw∣|\,\mathrm{d}y/\,\mathrm{d}w| records how the transformation stretches or compresses the axis: a small interval of length δ\delta in ww corresponds to an interval of length about ∣ dy/ dw∣ δ|\,\mathrm{d}y/\,\mathrm{d}w|\,\delta in yy, and the two intervals must carry the same probability.

The same reasoning extends to random vectors. If W=f(Y)\boldsymbol W=f(\boldsymbol Y) with ff one-to-one and differentiable, then pW(w)=pY(f−1(w)) ∣det⁡J∣,p_{\boldsymbol W}(\boldsymbol w) = p_{\boldsymbol Y}\big(f^{-1}(\boldsymbol w)\big)\,\big|\det J\big|, where JJ is the Jacobian matrix of the inverse transformation, with entries Jij=∂yi/∂wjJ_{ij}=\partial y_i/\partial w_j. The absolute determinant plays the part of ∣ dy/ dw∣|\,\mathrm{d}y/\,\mathrm{d}w|, measuring how the transformation changes small volumes.

These formulae have a consequence that will matter when we discuss the choice of prior. A density on θ\theta, and the density it induces on a transformed parameter ϕ=g(θ)\phi=g(\theta), do not in general have the same shape. In particular, a prior that is flat (uniform) in θ\theta is not flat in log⁡θ\log\theta or in 1/θ1/\theta. So the statement ‘I have no preference among the possible values of the parameter’ is ambiguous until we say which parameterisation we mean. We return to this in Chapter 10.

Exercises

Exercise 2.1

Let Y∼Exp⁡(λ)Y\sim\operatorname{Exp}(\lambda) and W=Y2W=Y^2. Find the density of WW.

Show solutionHide solution

f(y)=y2f(y)=y^2 is one-to-one on y>0y>0, with inverse y=wy=\sqrt w and  dy/ dw=1/(2w)\,\mathrm{d}y/\,\mathrm{d}w=1/(2\sqrt w). So pW(w)=λe−λw⋅12w,w>0.p_W(w) = \lambda e^{-\lambda\sqrt w}\cdot\frac1{2\sqrt w}, \qquad w>0.

Exercise 2.2

Let Y1Y_1 and Y2Y_2 be independent, with Yi∼Ga⁡(αi,β)Y_i\sim\operatorname{Ga}(\alpha_i,\beta). Let W1=Y1+Y2W_1=Y_1+Y_2 and W2=Y1/(Y1+Y2)W_2=Y_1/(Y_1+Y_2). Find the joint density of (W1,W2)(W_1,W_2). What do you notice?

Show solutionHide solution

The inverse is y1=w1w2y_1=w_1w_2, y2=w1(1−w2)y_2=w_1(1-w_2), with w1>0w_1>0 and 0<w2<10<w_2<1. The Jacobian matrix is J=(w2w11−w2−w1),∣det⁡J∣=w1.J = \begin{pmatrix} w_2 & w_1\\ 1-w_2 & -w_1\end{pmatrix}, \qquad |\det J| = w_1. So p(w1,w2)=βα1Γ(α1)(w1w2)α1−1e−βw1w2⋅βα2Γ(α2){w1(1−w2)}α2−1e−βw1(1−w2)⋅w1=βα1+α2Γ(α1+α2)w1α1+α2−1e−βw1×Γ(α1+α2)Γ(α1)Γ(α2)w2α1−1(1−w2)α2−1.\begin{aligned} p(w_1,w_2) &= \frac{\beta^{\alpha_1}}{\Gamma(\alpha_1)}(w_1w_2)^{\alpha_1-1}e^{-\beta w_1w_2}\cdot\frac{\beta^{\alpha_2}}{\Gamma(\alpha_2)}\{w_1(1-w_2)\}^{\alpha_2-1}e^{-\beta w_1(1-w_2)}\cdot w_1\\ &= \frac{\beta^{\alpha_1+\alpha_2}}{\Gamma(\alpha_1+\alpha_2)}w_1^{\alpha_1+\alpha_2-1}e^{-\beta w_1}\times\frac{\Gamma(\alpha_1+\alpha_2)}{\Gamma(\alpha_1)\Gamma(\alpha_2)}w_2^{\alpha_1-1}(1-w_2)^{\alpha_2-1}. \end{aligned} The density factorises. W1∼Ga⁡(α1+α2,β)W_1\sim\operatorname{Ga}(\alpha_1+\alpha_2,\beta) and W2∼Beta⁡(α1,α2)W_2\sim\operatorname{Beta}(\alpha_1,\alpha_2), and they are independent. This gives a way to simulate Beta variables from Gamma variables.

3 Exchangeability

This chapter follows Hoff §2.7. It addresses a question that is easy to pass over: why do statistical models take the form they do? Almost every model in this course, and in statistics generally, supposes that the observations are independent and identically distributed given some parameter, and that the parameter has a prior distribution. The idea of exchangeability, together with a remarkable theorem of de Finetti, shows that this structure is not an arbitrary assumption but follows from a simple and natural judgement about the observations themselves.

3.1 Motivation

Roll a fair die nn times, and let Yi=1Y_i=1 if the iith roll shows a six and Yi=0Y_i=0 otherwise. The rolls are independent, and each shows a six with probability 1/61/6, so the probability of any particular sequence of results (y1,…,yn)(y_1,\ldots,y_n) is p(y1,…,yn)=∏i=1n(16)yi(56)1−yi=(16)y(56)n−y,y=∑i=1nyi.p(y_1,\ldots,y_n) = \prod_{i=1}^n\left(\tfrac16\right)^{y_i}\left(\tfrac56\right)^{1-y_i} = \left(\tfrac16\right)^{y}\left(\tfrac56\right)^{n-y}, \qquad y=\sum_{i=1}^n y_i. This probability depends only on the number of sixes, yy, and the number of other results, n−yn-y. Every sequence with the same number of sixes has the same probability, whatever the order in which the sixes appear. The sequence (1,0,0)(1,0,0) is exactly as probable as (0,0,1)(0,0,1).

This symmetry under reordering turns out to be the essential property, and it holds much more widely than for independent rolls of a fair die.

3.2 Definition

Definition 3.1. Random variables Y1,…,YnY_1,\ldots,Y_n are exchangeable if their joint distribution is unchanged by any permutation of their labels: p(y1,…,yn)=p(yπ1,…,yπn)p(y_1,\ldots,y_n) = p(y_{\pi_1},\ldots,y_{\pi_n}) for every permutation π\pi of {1,…,n}\{1,\ldots,n\}. An infinite sequence Y1,Y2,…Y_1,Y_2,\ldots is exchangeable if Y1,…,YnY_1,\ldots,Y_n are exchangeable for every nn.

To judge a set of quantities exchangeable is to judge that their labels carry no information: knowing which observation is which would not change your beliefs about the collection. This is often a reasonable judgement. If the YiY_i are measurements on individuals drawn at random from a population, and there is nothing else to distinguish one individual from another, then we have no reason to expect the first measurement to differ systematically from the fifth. It is not always reasonable. Measurements taken in time order, where later values may depend on earlier ones, are not usually exchangeable. Nor are measurements on individuals known to belong to different groups, such as patients given different treatments, though they may be exchangeable within each group.

Independent and identically distributed (i.i.d.) random variables are exchangeable, since p(y1,…,yn)=∏i=1np(yi)=∏i=1np(yπi)=p(yπ1,…,yπn),p(y_1,\ldots,y_n) = \prod_{i=1}^n p(y_i) = \prod_{i=1}^n p(y_{\pi_i}) = p(y_{\pi_1},\ldots,y_{\pi_n}), the middle equality holding because a product does not depend on the order of its factors. The same applies to infinite i.i.d. sequences.

The converse is false: exchangeable variables need not be independent. The next theorem shows the most important way in which exchangeable but dependent variables arise.

Theorem 3.2. Suppose that Y1,…,YnY_1,\ldots,Y_n are conditionally i.i.d. given θ\theta, with Yi∣θ∼p(y∣θ)Y_i\mid\theta\sim p(y\mid\theta), and that θ∼p(θ)\theta\sim p(\theta). Then, marginally (that is, without conditioning on θ\theta), Y1,…,YnY_1,\ldots,Y_n are exchangeable.

Proof. The marginal distribution is obtained by averaging over θ\theta: p(y1,…,yn)=∫p(y1,…,yn∣θ) p(θ) dθ=∫∏i=1np(yi∣θ) p(θ) dθ=∫∏i=1np(yπi∣θ) p(θ) dθ=p(yπ1,…,yπn).\begin{aligned} p(y_1,\ldots,y_n) &= \int p(y_1,\ldots,y_n\mid\theta)\,p(\theta)\,\mathrm{d}\theta = \int \prod_{i=1}^n p(y_i\mid\theta)\,p(\theta)\,\mathrm{d}\theta \\ &= \int \prod_{i=1}^n p(y_{\pi_i}\mid\theta)\,p(\theta)\,\mathrm{d}\theta = p(y_{\pi_1},\ldots,y_{\pi_n}). \end{aligned} The second equality uses conditional independence and the third the commutativity of multiplication. ◻

Such variables are, however, not independent marginally, for the reason explained in the previous chapter. Observing Y1Y_1 changes our beliefs about θ\theta, and hence about Y2Y_2. It is exactly this dependence that makes learning possible. If the observations were marginally independent, no number of them would tell us anything about the next.

3.3 De Finetti’s theorem

Theorem 3.2 says that the familiar model structure, conditionally i.i.d. observations with a prior on the parameter, always produces exchangeable observations. The converse also holds, provided the sequence can be extended indefinitely.

Theorem 3.3 (de Finetti). Let Y1,Y2,…Y_1,Y_2,\ldots be an infinite sequence of random variables with a common sample space, and suppose that Y1,…,YnY_1,\ldots,Y_n are exchangeable for every nn. Then there exist a parameter θ\theta, a prior distribution p(θ)p(\theta) and a sampling model p(y∣θ)p(y\mid\theta) such that, for every nn, p(y1,…,yn)=∫{∏i=1np(yi∣θ)}p(θ) dθ.p(y_1,\ldots,y_n) = \int\left\{\prod_{i=1}^n p(y_i\mid\theta)\right\}p(\theta)\,\mathrm{d}\theta.

Combining the two theorems, Y1,…,Yn∣θ are i.i.d.θ∼p(θ)}  ⟺  Y1,…,Yn are exchangeable for all n.\left.\begin{array}{l} Y_1,\ldots,Y_n\mid\theta \text{ are i.i.d.}\\ \theta\sim p(\theta)\end{array}\right\} \iff Y_1,\ldots,Y_n \text{ are exchangeable for all } n.

We do not prove the theorem here. For binary sequences a fairly elementary proof is given in Bernardo and Smith, §4.3. The binary case also shows what the parameter is. If Y1,Y2,…Y_1,Y_2,\ldots is an exchangeable sequence of zeros and ones, then the proportion of ones among the first nn, Yˉn=1n∑i=1nYi\bar Y_n=\frac1n\sum_{i=1}^nY_i, converges as n→∞n\to\infty, and the limit is the parameter θ\theta. The prior p(θ)p(\theta) is therefore our belief about the long-run proportion of ones in the sequence, and given that proportion the observations behave like independent Bernoulli trials with that probability of success.

It is worth pausing on why this matters. A person who judges only that the labels on a sequence of observations are irrelevant, and who is willing to imagine the sequence continuing indefinitely, is thereby committed to modelling the observations as if they were i.i.d. given some parameter, with a prior distribution on that parameter. The parameter and its prior are not extra assumptions laid on top of the data. They arise from a judgement of symmetry about quantities that can actually be observed. De Finetti held that probability statements should ultimately concern observable quantities, and his theorem explains how parameters, which are not directly observable, nonetheless earn their place in a model. For a modern account that places prediction at the centre of Bayesian modelling, see Fortini and Petrone (2025), Statistical Science 40, 40–67.

The theorem does not tell us which sampling model or prior to use. That remains a matter of judgement, informed by what we know of the problem. It tells us only the form the model must take.

3.4 Finite populations

De Finetti’s theorem requires an infinite exchangeable sequence, and the requirement is not a technicality. Suppose we draw individuals at random, without replacement, from a finite population of size NN. The resulting observations are exchangeable, since the order of drawing is irrelevant, but they are not in general conditionally i.i.d. given any parameter. Exercise 3.1 gives an extreme example. The reason is that drawing without replacement induces negative dependence (drawing one individual with a property makes the next less likely to have it), whereas conditionally i.i.d. observations can only be positively dependent or independent.

When the sample size nn is small compared with the population size NN, however, the effect of not replacing is negligible, and we may treat the observations as approximately conditionally i.i.d. This is the situation in most applications, and it is the justification for the models used in the rest of the course. Most of the models we consider assume that the data are conditionally i.i.d. given some parameter θ\theta, which may be a vector.

Exercises

Exercise 3.1

An urn contains two balls, one marked 11 and one marked 00. Two balls are drawn without replacement, and YiY_i is the mark on the iith ball drawn.

  1. Show that Y1,Y2Y_1,Y_2 are exchangeable.

  2. Show that there is no distribution p(θ)p(\theta) on [0,1][0,1] under which Y1,Y2∣θY_1,Y_2\mid\theta are i.i.d. Bernoulli(θ)(\theta).

Show solutionHide solution

(a) The possible sequences are (1,0)(1,0) and (0,1)(0,1), each with probability 12\frac12, and (1,1)(1,1) and (0,0)(0,0), each with probability 00. The joint distribution is unchanged by swapping the two coordinates.

(b) Suppose such a p(θ)p(\theta) existed. Then P(Y1=1)=E⁡[θ]=12P(Y_1=1)=\operatorname{E}[\theta]=\frac12, and P(Y1=1,Y2=1)=E⁡[θ2]≥E⁡[θ]2=14,P(Y_1=1,Y_2=1) = \operatorname{E}[\theta^2] \ge \operatorname{E}[\theta]^2 = \tfrac14, by Jensen’s inequality, or because Var⁡[θ]≥0\operatorname{Var}[\theta]\ge0. But P(Y1=1,Y2=1)=0P(Y_1=1,Y_2=1)=0. Contradiction. More generally, conditionally i.i.d. variables cannot be negatively correlated, while draws without replacement are.

4 Binomial data

References: Hoff §3.1; GCSR §2.1–2.4.

We now apply the general machinery to the simplest useful model. Binomial data are common in their own right, and the model is simple enough that every step of a Bayesian analysis can be carried out by hand. Most of the ideas introduced here (conjugacy, the interpretation of the prior as imaginary data, the posterior mean as a compromise, the predictive distribution) recur in every later chapter.

4.1 Setting

A familiar kind of question runs as follows. We draw nn individuals at random from a population and find that some of them have a certain property. What does this tell us about the proportion of the whole population that has the property? Some examples:

  • In a clinical trial of a new treatment for psoriasis, 12 of 15 patients showed a marked improvement compared with the standard treatment. What is the probability that a future patient will improve?

  • On 5 September 2014, a YouGov poll of 1084 voters on the Scottish independence referendum found 51% for Yes and 49% for No, excluding those who would not vote or did not know. What is the probability that Yes was ahead in the population?

  • Of 12 lecturers in a small department at a UK university, 3 are foreign nationals.

  • Of 25 items selected from a day’s production for quality control, none is defective. Does that mean the production process makes no defective items?

To set up a model, label the individuals in the population i=1,…,Ni=1,\ldots,N and let Yi={1if individual i has the property,0otherwise.Y_i=\begin{cases}1 & \text{if individual } i \text{ has the property},\\ 0 & \text{otherwise}.\end{cases} The quantity of interest is the population proportion θ=∑i=1NYi/N\theta=\sum_{i=1}^NY_i/N. We observe YiY_i for a sample of nn individuals, usually with nn much smaller than NN, and ask how these observations should change our beliefs about θ\theta.

If we have no information that distinguishes one sampled individual from another, it is natural to judge Y1,…,YnY_1,\ldots,Y_n exchangeable. Since NN is large compared with nn, the discussion of Chapter 3 then justifies the model Y1,…,Yn∣θ∼iidBernoulli⁡(θ),Y_1,\ldots,Y_n\mid\theta \stackrel{\text{iid}}{\sim}\operatorname{Bernoulli}(\theta), in which each observation is a one with probability θ\theta, independently of the others, given θ\theta.

4.2 Likelihood and sufficiency

Under this model the probability of the observed sequence is p(y1,…,yn∣θ)=∏i=1nθyi(1−θ)1−yi=θy(1−θ)n−y,where y=∑i=1nyi.p(y_1,\ldots,y_n\mid\theta) = \prod_{i=1}^n\theta^{y_i}(1-\theta)^{1-y_i} = \theta^{y}(1-\theta)^{n-y}, \qquad\text{where } y=\sum_{i=1}^n y_i. Notice that the likelihood depends on the data only through yy, the total number of ones. Two samples of the same size with the same number of ones give exactly the same likelihood function, and so, whatever the prior, the same posterior. We express this by saying that Y=∑iYiY=\sum_iY_i is a sufficient statistic for θ\theta: once we know yy, the individual values yiy_i, and in particular the order in which the ones and zeros occurred, carry no further information about θ\theta.

The total YY has a binomial distribution, p(y∣θ)=(ny)θy(1−θ)n−y,y=0,1,…,n.p(y\mid\theta) = \binom ny\theta^y(1-\theta)^{n-y}, \qquad y=0,1,\ldots,n. This differs from the likelihood of the full sequence only by the factor (ny)\binom ny, which does not involve θ\theta. Since Bayes’ theorem determines the posterior only up to a constant factor, it makes no difference whether we work with the sequence or with the total. We shall move freely between the two.

4.3 A uniform prior

Next we need a prior. Suppose, to begin with, that we have no particular information about θ\theta, and that we regard all values between 00 and 11 as equally plausible. This suggests the uniform prior θ∼Unif⁡(0,1)\theta\sim\operatorname{Unif}(0,1), with density p(θ)=1,0<θ<1,p(\theta) = 1, \qquad 0<\theta<1, under which any two intervals of the same length have the same prior probability. (We shall see in Chapter 10 that ‘equally plausible’ is less straightforward than it sounds, but the uniform prior is a sensible starting point.)

The posterior is then p(θ∣y)∝p(y∣θ) p(θ)=θy(1−θ)n−y,0<θ<1.p(\theta\mid y) \propto p(y\mid\theta)\,p(\theta) = \theta^y(1-\theta)^{n-y}, \qquad 0<\theta<1. As a function of θ\theta, this has the form of the density of a Beta distribution, with its normalising constant omitted.

Definition 4.1. A random variable XX has a Beta⁡(a,b)\operatorname{Beta}(a,b) distribution, where a>0a>0 and b>0b>0, if its density is p(x)=Γ(a+b)Γ(a)Γ(b) xa−1(1−x)b−1,0<x<1.p(x) = \frac{\Gamma(a+b)}{\Gamma(a)\Gamma(b)}\,x^{a-1}(1-x)^{b-1}, \qquad 0<x<1. Its mean, mode and variance are E⁡[X]=aa+b,mode⁡[X]=a−1a+b−2(a,b>1),\operatorname{E}[X]=\frac{a}{a+b}, \qquad \operatorname{mode}[X]=\frac{a-1}{a+b-2}\quad(a,b>1), Var⁡[X]=ab(a+b)2(a+b+1)=E⁡[X] (1−E⁡[X])a+b+1.\operatorname{Var}[X]=\frac{ab}{(a+b)^2(a+b+1)} = \frac{\operatorname{E}[X]\,(1-\operatorname{E}[X])}{a+b+1}.

The Beta family is very flexible on the interval (0,1)(0,1). With a=b=1a=b=1 it is uniform. With a=b>1a=b>1 it is symmetric about 12\frac12 and peaked, more sharply as aa and bb increase. With a>ba>b it is skewed towards one, and with a<ba<b towards zero. With aa or bb below one the density is unbounded at the corresponding end of the interval. The form of the variance shows that, for a fixed mean, the distribution becomes more concentrated as a+ba+b increases.

Comparing the posterior with the Beta density, we conclude that θ∣y∼Beta⁡(y+1,  n−y+1).\theta\mid y\sim\operatorname{Beta}(y+1,\;n-y+1). For the psoriasis trial, with y=12y=12 and n=15n=15, the posterior is Beta⁡(13,4)\operatorname{Beta}(13,4). Its mean is 13/17≈0.7613/17\approx0.76, and the posterior probability that more than half of future patients would improve is P(θ>0.5∣y)≈0.99P(\theta>0.5\mid y)\approx0.99.

4.4 The kernel method

The step we have just taken will recur throughout the course, and it is worth setting it out explicitly.

  1. Write down the posterior up to a constant, p(θ∣y)∝p(y∣θ) p(θ)p(\theta\mid y)\propto p(y\mid\theta)\,p(\theta). At each stage, discard any factor that does not involve θ\theta.

  2. Recognise the result as the kernel of a known distribution, that is, as its density with the normalising constant removed.

  3. Conclude that the posterior is that distribution.

The argument is valid because a probability density is determined by its kernel. If two densities are proportional to one another, then, since both integrate to one, the constant of proportionality must be one and the densities are equal. The practical benefit is considerable. It spares us the integral in the denominator of Bayes’ theorem, which is the hardest part of the calculation, and it gives us the posterior in a form whose properties (mean, variance, quantiles) are already known.

4.5 Prediction

Often the most useful thing a posterior distribution can do is predict future observations. What should we expect of the next individual we sample? By the tower property (2.1), conditioning on θ\theta, E⁡[Yn+1∣y1,…,yn]=∫01E⁡[Yn+1∣θ,y1,…,yn] p(θ∣y) dθ.\operatorname{E}[Y_{n+1}\mid y_1,\ldots,y_n] = \int_0^1 \operatorname{E}[Y_{n+1}\mid\theta,y_1,\ldots,y_n]\,p(\theta\mid y)\,\mathrm{d}\theta. Given θ\theta, the new observation is independent of the old ones and has mean θ\theta, so E⁡[Yn+1∣θ,y1,…,yn]=θ\operatorname{E}[Y_{n+1}\mid\theta,y_1,\ldots,y_n]=\theta, and E⁡[Yn+1∣y1,…,yn]=∫01θ p(θ∣y) dθ=E⁡[θ∣y]=y+1n+2.\operatorname{E}[Y_{n+1}\mid y_1,\ldots,y_n] = \int_0^1\theta\,p(\theta\mid y)\,\mathrm{d}\theta = \operatorname{E}[\theta\mid y] = \frac{y+1}{n+2}. Since Yn+1Y_{n+1} takes only the values 00 and 11, this is also the probability that the next individual has the property. The prediction does not use any single ‘best’ value of θ\theta. It averages over all values of θ\theta, weighted by their posterior probability.

4.5.0.0.1 The rule of succession.

Suppose every one of nn observations so far has been a one. The predictive probability that the next is also a one is then (n+1)/(n+2)(n+1)/(n+2). This result is known as Laplace’s rule of succession. Laplace is said to have used it to assess the probability that the sun will rise tomorrow, given that it has risen on every day of recorded history. The example is not to be taken too seriously, since our knowledge of the sun is not well described by exchangeable Bernoulli trials, but the formula makes a reasonable point. However long a run of successes, the predictive probability of success never reaches one, and the probability of a failure, 1/(n+2)1/(n+2), decreases gradually as the evidence accumulates.

4.6 Beta priors

The uniform distribution is the special case Beta⁡(1,1)\operatorname{Beta}(1,1) of the Beta family. We now show that the calculation of the previous sections goes through for any Beta prior. Suppose θ∼Beta⁡(a,b)\theta\sim\operatorname{Beta}(a,b) and Y∣θ∼Bin⁡(n,θ)Y\mid\theta\sim\operatorname{Bin}(n,\theta). Then p(θ∣y)∝p(θ) p(y∣θ)=Γ(a+b)Γ(a)Γ(b)θa−1(1−θ)b−1×(ny)θy(1−θ)n−y∝θa+y−1(1−θ)b+n−y−1,\begin{aligned} p(\theta\mid y) &\propto p(\theta)\,p(y\mid\theta) = \frac{\Gamma(a+b)}{\Gamma(a)\Gamma(b)}\theta^{a-1}(1-\theta)^{b-1}\times\binom ny\theta^y(1-\theta)^{n-y}\\ &\propto \theta^{a+y-1}(1-\theta)^{b+n-y-1}, \end{aligned} and so, by the kernel method, θ∣y∼Beta⁡(a+y,  b+n−y).\theta\mid y\sim\operatorname{Beta}(a+y,\;b+n-y). The updating rule is simple: add the number of successes to aa, and the number of failures to bb.

This suggests a useful way to think about the prior. The Beta⁡(a,b)\operatorname{Beta}(a,b) prior behaves exactly as though we had begun with a uniform prior and had already observed a−1a-1 successes and b−1b-1 failures. Alternatively, and more commonly, one says that the prior carries the information of a sample of size a+ba+b in which the proportion of successes is a/(a+b)a/(a+b). The quantity a+ba+b is often called the prior sample size. This interpretation helps in choosing a prior: we can set the prior mean a/(a+b)a/(a+b) to our best guess for θ\theta, and then choose a+ba+b to reflect how many observations we think our prior knowledge is worth.

4.6.1 The posterior mean as a compromise

The posterior mean shows clearly how prior information and data combine: E⁡[θ∣y]=a+ya+b+n=a+ba+b+n⋅aa+b+na+b+n⋅yn.\operatorname{E}[\theta\mid y] = \frac{a+y}{a+b+n} = \frac{a+b}{a+b+n}\cdot\frac{a}{a+b} + \frac{n}{a+b+n}\cdot\frac yn. It is a weighted average of the prior mean a/(a+b)a/(a+b) and the sample proportion y/ny/n, and the weights are proportional to the prior sample size a+ba+b and the actual sample size nn. When nn is small compared with a+ba+b, the posterior mean stays close to the prior mean. As nn grows, the weight on the data increases and the posterior mean approaches the sample proportion. Similarly, the posterior variance, Var⁡[θ∣y]=E⁡[θ∣y] (1−E⁡[θ∣y])a+b+n+1,\operatorname{Var}[\theta\mid y] = \frac{\operatorname{E}[\theta\mid y]\,(1-\operatorname{E}[\theta\mid y])}{a+b+n+1}, decreases roughly in proportion to 1/n1/n as data accumulate. With plenty of data, then, the posterior is concentrated near the sample proportion whatever (reasonable) prior we began with. This is the precise sense in which the influence of the prior diminishes as evidence accumulates, and in which people with different priors are brought into agreement.

4.6.2 Sequential updating

Suppose the observations arrive one at a time. We may update after each, using the posterior from the first kk observations as the prior for the (k+1)(k+1)th. Starting from Beta⁡(a,b)\operatorname{Beta}(a,b), each success adds one to the first parameter and each failure one to the second, so after all nn observations we arrive at Beta⁡(a+y, b+n−y)\operatorname{Beta}(a+y,\,b+n-y), exactly as if we had processed the data all at once. This is a general property of Bayesian updating with conditionally independent observations, not a peculiarity of the Beta distribution (see Exercise 7.2). Today’s posterior is tomorrow’s prior.

4.6.3 Interpreting a posterior interval

A 95% central posterior interval for θ\theta is an interval (θ0.025,θ0.975)(\theta_{0.025},\theta_{0.975}) between the 2.5%2.5\% and 97.5%97.5\% quantiles of the posterior. Its interpretation is direct: given the model, the prior and the data, the probability that θ\theta lies in the interval is 0.950.95. This is the statement that people often wish to make about a frequentist confidence interval, but cannot, since in the frequentist framework θ\theta is fixed and the probability 0.950.95 refers to the procedure’s behaviour over repeated samples, not to the particular interval computed. The two kinds of interval are often numerically similar, particularly with large samples and vague priors, but they answer different questions.

4.7 Conjugacy

We have seen that a Beta prior combined with binomial data yields a Beta posterior. This property has a name.

Definition 4.2. A class P\mathcal P of prior distributions for θ\theta is conjugate for a sampling model p(y∣θ)p(y\mid\theta) if p(θ)∈P  ⟹  p(θ∣y)∈P.p(\theta)\in\mathcal P \implies p(\theta\mid y)\in\mathcal P.

The class of Beta distributions is thus conjugate for binomial data. Conjugate priors have obvious advantages. Computing the posterior amounts to updating a few parameters, and the properties of the posterior are those of a familiar distribution. They also make the contribution of the prior easy to interpret, as with the prior sample size above.

Their disadvantage is that a conjugate family may not be flexible enough to represent what we actually believe. A Beta density has at most one mode, for example, and cannot express the belief that θ\theta is probably either near 0.20.2 or near 0.70.7. A useful compromise is a mixture of conjugate priors. If the prior is a mixture of Beta distributions, p(θ)=∑kwk Beta⁡(θ∣ak,bk)p(\theta)=\sum_kw_k\,\operatorname{Beta}(\theta\mid a_k,b_k), then the posterior is also a mixture of Beta distributions, each component updated in the usual way, with the mixture weights themselves updated in proportion to how well each component predicted the data. Any continuous prior on (0,1)(0,1) can be approximated arbitrarily well by such a mixture.

In Chapter 10 we shall see that conjugate priors exist for a whole class of models, the exponential families, of which the binomial, Poisson, exponential and normal models are all members.

Exercises

Exercise 4.1

Of 25 items inspected, none is defective. Let θ\theta be the proportion of defective items in the day’s production, and give θ\theta a uniform prior.

  1. Find the posterior distribution of θ\theta.

  2. Find P(θ<0.1∣y)P(\theta<0.1\mid y) in closed form.

  3. Find the probability that the next item inspected is defective.

Show solutionHide solution

(a) With n=25n=25, y=0y=0 and a Beta⁡(1,1)\operatorname{Beta}(1,1) prior, θ∣y∼Beta⁡(1,26)\theta\mid y\sim\operatorname{Beta}(1,26), with density 26(1−θ)2526(1-\theta)^{25}.

(b) P(θ<0.1∣y)=∫00.126(1−θ)25 dθ=1−0.926≈0.935P(\theta<0.1\mid y)=\int_0^{0.1}26(1-\theta)^{25}\,\mathrm{d}\theta=1-0.9^{26}\approx0.935.

(c) E⁡[θ∣y]=1/27≈0.037\operatorname{E}[\theta\mid y]=1/27\approx0.037. Note that the maximum likelihood estimate, 00, would say defects are impossible.

Exercise 4.2

Suppose θ∼Beta⁡(a,b)\theta\sim\operatorname{Beta}(a,b) and Y∣θ∼Bin⁡(n,θ)Y\mid\theta\sim\operatorname{Bin}(n,\theta). Show that the posterior variance of θ\theta is smaller than the prior variance whenever the observed proportion y/ny/n equals the prior mean a/(a+b)a/(a+b). Is the posterior variance always smaller than the prior variance?

Show solutionHide solution

Write m=a/(a+b)m=a/(a+b). The prior variance is m(1−m)/(a+b+1)m(1-m)/(a+b+1). If y/n=my/n=m, then (a+y)/(a+b+n)=m(a+y)/(a+b+n)=m, so the posterior mean is also mm and the posterior variance is m(1−m)/(a+b+n+1)m(1-m)/(a+b+n+1), which is smaller.

The posterior variance is not always smaller. Take a=1a=1, b=20b=20, and observe one success in one trial. The prior variance is 121⋅2021⋅122≈0.0021\frac{1}{21}\cdot\frac{20}{21}\cdot\frac1{22}\approx0.0021. The posterior is Beta⁡(2,20)\operatorname{Beta}(2,20), with variance 222⋅2022⋅123≈0.0036\frac2{22}\cdot\frac{20}{22}\cdot\frac1{23}\approx0.0036. A surprising observation can increase uncertainty. On average, however, it cannot: by (2.2), Var⁡[θ]=E⁡[Var⁡[θ∣Y]]+Var⁡[E⁡[θ∣Y]]≥E⁡[Var⁡[θ∣Y]]\operatorname{Var}[\theta]=\operatorname{E}[\operatorname{Var}[\theta\mid Y]]+\operatorname{Var}[\operatorname{E}[\theta\mid Y]]\ge\operatorname{E}[\operatorname{Var}[\theta\mid Y]].

5 Poisson data

Reference: Hoff §3.2.

The binomial model describes the number of successes in a fixed number of trials. Many data sets instead record counts with no natural upper limit: the number of times something happened in a given period, or in a given region. The Poisson model is the standard starting point for such data, and its Bayesian analysis runs closely parallel to that of the previous chapter, with the Gamma distribution taking the place of the Beta.

5.1 Setting

We observe counts of events that occur independently of one another, over equal periods of time or within regions of equal size. Examples include the number of cases of a rare disease in a region in each year; the number of radioactive decays from a sample of uranium in each minute; the number of customers arriving at a campus shop between ten and eleven on weekday mornings; and the number of junk emails received in an hour.

Definition 5.1. A random variable YY has a Poisson distribution with mean θ>0\theta>0 if P(Y=y∣θ)=θyy!e−θ,y=0,1,2,…P(Y=y\mid\theta) = \frac{\theta^y}{y!}e^{-\theta}, \qquad y=0,1,2,\ldots

The sample space Y={0,1,2,…}\mathcal{Y}=\{0,1,2,\ldots\} is countable, so YY is discrete, and the parameter space is Θ=(0,∞)\Theta=(0,\infty). The mean and the variance are both equal to θ\theta: E⁡[Y∣θ]=Var⁡[Y∣θ]=θ.\operatorname{E}[Y\mid\theta]=\operatorname{Var}[Y\mid\theta]=\theta. This equality of mean and variance is a strong restriction, and one worth checking against data: counts whose variance is much larger than their mean (which is common) are said to be overdispersed relative to the Poisson model.

The Poisson distribution arises as a limit of the binomial. If Y∼Bin⁡(n,p)Y\sim\operatorname{Bin}(n,p), and n→∞n\to\infty and p→0p\to0 in such a way that np→θnp\to\theta, then the distribution of YY converges to Poi⁡(θ)\operatorname{Poi}(\theta). So the Poisson model is appropriate for the number of occurrences of a rare event among a very large number of independent opportunities, which is the situation in each of the examples above.

We use two data sets for illustration. The first records the number of fatal accidents on scheduled airline flights in each year from 1976 to 1985 (GCSR, Exercise 2.13):

Year 76 77 78 79 80 81 82 83 84 85
Accidents 24 25 31 31 22 21 26 20 16 22

The second records the number of ribwort plantains found in each of 100 equal-sized regions of a plot of land:

Plants 0 1 2 3 4 5 6 7 8 9+
Regions 7 17 22 17 15 9 5 4 4 0

5.2 Likelihood

Suppose Y1,…,Yn∣θ∼iidPoi⁡(θ)Y_1,\ldots,Y_n\mid\theta\stackrel{\text{iid}}{\sim}\operatorname{Poi}(\theta). The likelihood is p(y∣θ)=∏i=1nθyiyi!e−θ=(∏i=1n1yi!)θ∑iyie−nθ∝θ∑iyie−nθ.p(y\mid\theta) = \prod_{i=1}^n\frac{\theta^{y_i}}{y_i!}e^{-\theta} = \left(\prod_{i=1}^n\frac1{y_i!}\right)\theta^{\sum_i y_i}e^{-n\theta} \propto \theta^{\sum_i y_i}e^{-n\theta}. Two things are apparent. First, the likelihood depends on the data only through the total ∑iyi\sum_iy_i, which is therefore a sufficient statistic for θ\theta: the total number of events is all that matters, not how they were distributed among the periods. Second, as a function of θ\theta the likelihood has the form θce−dθ\theta^{c}e^{-d\theta}. This is the kernel of a Gamma density, which suggests that a Gamma prior will be conjugate.

Definition 5.2. A random variable XX has a Ga⁡(a,b)\operatorname{Ga}(a,b) distribution, where a>0a>0 (the shape) and b>0b>0 (the rate), if its density is p(x)=baΓ(a)xa−1e−bx,x>0.p(x) = \frac{b^a}{\Gamma(a)}x^{a-1}e^{-bx}, \qquad x>0. Its mean and variance are E⁡[X]=a/b\operatorname{E}[X]=a/b and Var⁡[X]=a/b2\operatorname{Var}[X]=a/b^2, and its mode is (a−1)/b(a-1)/b if a>1a>1 and 00 if a≤1a\le1.

5.3 Gamma prior, Gamma posterior

With a Ga⁡(a,b)\operatorname{Ga}(a,b) prior, p(θ)∝θa−1e−bθp(\theta)\propto\theta^{a-1}e^{-b\theta}, and the posterior is p(θ∣y)∝θa−1e−bθ×θ∑iyie−nθ=θa+∑iyi−1e−(b+n)θ.p(\theta\mid y) \propto \theta^{a-1}e^{-b\theta}\times\theta^{\sum_iy_i}e^{-n\theta} = \theta^{a+\sum_iy_i-1}e^{-(b+n)\theta}. By the kernel method, θ∣y∼Ga⁡(a+∑i=1nyi,  b+n),\theta\mid y\sim\operatorname{Ga}\Big(a+\sum_{i=1}^ny_i,\;b+n\Big), so the Gamma family is indeed conjugate for Poisson data. The updating rule is again simple: add the total count to aa, and the number of observation periods to bb.

The interpretation parallels that of the Beta prior. The Ga⁡(a,b)\operatorname{Ga}(a,b) prior carries the same information as bb earlier observation periods in which a total of aa events were seen. The prior mean a/ba/b is thus a prior estimate of the rate, and bb measures how much weight it carries. The posterior mean is again a weighted average of prior mean and sample mean, E⁡[θ∣y]=a+nyˉb+n=bb+n⋅ab+nb+n⋅yˉ,\operatorname{E}[\theta\mid y] = \frac{a+n\bar y}{b+n} = \frac{b}{b+n}\cdot\frac ab + \frac{n}{b+n}\cdot\bar y, with weights proportional to the prior sample size bb and the actual sample size nn. The posterior variance, (a+nyˉ)/(b+n)2(a+n\bar y)/(b+n)^2, is approximately yˉ/n\bar y/n when nn is large, and so shrinks in proportion to 1/n1/n.

5.4 Posterior predictive distribution

Suppose we wish to predict a future count Y~\tilde Y, for example the number of airline accidents in 1986. We take Y~\tilde Y to be exchangeable with the observed Y1,…,YnY_1,\ldots,Y_n, so that given θ\theta it has the same Poi⁡(θ)\operatorname{Poi}(\theta) distribution and is independent of them. Its posterior predictive distribution is obtained by averaging the sampling model over the posterior: p(y~∣y)=∫0∞p(y~∣θ,y) p(θ∣y) dθ=∫0∞Poi⁡(y~∣θ) Ga⁡(θ∣a+nyˉ, b+n) dθ.p(\tilde y\mid y) = \int_0^\infty p(\tilde y\mid\theta,y)\,p(\theta\mid y)\,\mathrm{d}\theta = \int_0^\infty\operatorname{Poi}(\tilde y\mid\theta)\,\operatorname{Ga}(\theta\mid a+n\bar y,\,b+n)\,\mathrm{d}\theta. The second equality uses the conditional independence of Y~\tilde Y and the data given θ\theta. The predictive distribution incorporates two distinct sources of uncertainty: the variability of a Poisson count about its mean even when the mean is known, and our uncertainty about the mean itself.

The integral can be evaluated directly, but there is a neater route that avoids integration altogether and that works in any conjugate model. Imagine that we have observed y~\tilde y as well as yy. Applying Bayes’ theorem to the new observation, with the current posterior p(θ∣y)p(\theta\mid y) playing the part of the prior, gives p(θ∣y~,y)=p(y~∣θ,y) p(θ∣y)p(y~∣y).p(\theta\mid\tilde y,y) = \frac{p(\tilde y\mid\theta,y)\,p(\theta\mid y)}{p(\tilde y\mid y)}. Rearranging, p(y~∣y)=p(y~∣θ) p(θ∣y)p(θ∣y~,y).p(\tilde y\mid y) = \frac{p(\tilde y\mid\theta)\,p(\theta\mid y)}{p(\theta\mid\tilde y,y)}. This identity holds for every value of θ\theta. The left-hand side does not involve θ\theta, so when we substitute the three densities on the right, every occurrence of θ\theta must cancel, and we may use whatever form of the densities is convenient. All three are known:

  • p(y~∣θ)=Poi⁡(y~∣θ)p(\tilde y\mid\theta)=\operatorname{Poi}(\tilde y\mid\theta), the sampling model;

  • p(θ∣y)=Ga⁡(θ∣a+nyˉ, b+n)p(\theta\mid y)=\operatorname{Ga}(\theta\mid a+n\bar y,\,b+n), the current posterior;

  • p(θ∣y~,y)=Ga⁡(θ∣a+nyˉ+y~, b+n+1)p(\theta\mid\tilde y,y)=\operatorname{Ga}(\theta\mid a+n\bar y+\tilde y,\,b+n+1), the posterior we would have after n+1n+1 observations, obtained from the updating rule.

Write an=a+nyˉa_n=a+n\bar y and bn=b+nb_n=b+n for the posterior parameters. Substituting, p(y~∣y)=θy~e−θy~!⋅bnanΓ(an)θan−1e−bnθ(bn+1)an+y~Γ(an+y~)θan+y~−1e−(bn+1)θ=Γ(an+y~)Γ(y~+1)Γ(an)(bnbn+1)an(1bn+1)y~,y~=0,1,2,…,\begin{aligned} p(\tilde y\mid y) &= \frac{\dfrac{\theta^{\tilde y}e^{-\theta}}{\tilde y!}\cdot\dfrac{b_n^{a_n}}{\Gamma(a_n)}\theta^{a_n-1}e^{-b_n\theta}}{\dfrac{(b_n+1)^{a_n+\tilde y}}{\Gamma(a_n+\tilde y)}\theta^{a_n+\tilde y-1}e^{-(b_n+1)\theta}}\\[0.5em] &= \frac{\Gamma(a_n+\tilde y)}{\Gamma(\tilde y+1)\Gamma(a_n)}\left(\frac{b_n}{b_n+1}\right)^{a_n}\left(\frac1{b_n+1}\right)^{\tilde y}, \qquad \tilde y=0,1,2,\ldots, \end{aligned} and, as promised, θ\theta has disappeared. This is a negative binomial distribution, which we write Y~∣y∼Neg-Bin⁡(an,bn)\tilde Y\mid y\sim\operatorname{Neg\text{-}Bin}(a_n,b_n) following Hoff’s parameterisation. You should be aware that there is no agreed parameterisation of the negative binomial; the R function dnbinom, for instance, uses a different one, and it is always worth checking which is in use. In Hoff’s form, the Neg-Bin⁡(a,b)\operatorname{Neg\text{-}Bin}(a,b) distribution has mean a/ba/b and variance a(b+1)/b2a(b+1)/b^2.

The predictive mean and variance can also be found without identifying the distribution, using the identities (2.1) and (2.2): E⁡[Y~∣y]=E⁡[E⁡[Y~∣θ]∣y]=E⁡[θ∣y]=anbn,Var⁡[Y~∣y]=E⁡[Var⁡[Y~∣θ]∣y]+Var⁡[E⁡[Y~∣θ]∣y]=E⁡[θ∣y]+Var⁡[θ∣y]=anbn+anbn2.\begin{aligned} \operatorname{E}[\tilde Y\mid y] &= \operatorname{E}\big[\operatorname{E}[\tilde Y\mid\theta]\mid y\big] = \operatorname{E}[\theta\mid y] = \frac{a_n}{b_n},\\ \operatorname{Var}[\tilde Y\mid y] &= \operatorname{E}\big[\operatorname{Var}[\tilde Y\mid\theta]\mid y\big]+\operatorname{Var}\big[\operatorname{E}[\tilde Y\mid\theta]\mid y\big] = \operatorname{E}[\theta\mid y]+\operatorname{Var}[\theta\mid y] = \frac{a_n}{b_n}+\frac{a_n}{b_n^2}. \end{aligned} The decomposition of the variance matches the two sources of uncertainty described above. The first term is the Poisson variability we would have if θ\theta were known exactly. The second is the extra variability due to our uncertainty about θ\theta. The predictive distribution is therefore overdispersed relative to a Poisson with the same mean. As more data are collected, bnb_n grows, the second term vanishes, and the predictive distribution approaches a Poisson distribution with mean equal to the estimated rate.

5.5 Sampling from the predictive distribution

For many models the predictive distribution cannot be found in closed form. It is then usually still easy to simulate from it, and the method is worth learning now because it is completely general. Since p(y~,θ∣y)=p(y~∣θ,y) p(θ∣y)=p(y~∣θ) p(θ∣y),p(\tilde y,\theta\mid y) = p(\tilde y\mid\theta,y)\,p(\theta\mid y) = p(\tilde y\mid\theta)\,p(\theta\mid y), we can draw from the joint distribution of (y~,θ)(\tilde y,\theta) in two stages:

  1. draw a value θ⋆\theta^\star from the posterior p(θ∣y)p(\theta\mid y);

  2. draw a value y~⋆\tilde y^\star from the sampling model p(y~∣θ⋆)p(\tilde y\mid\theta^\star), with the parameter set to θ⋆\theta^\star.

The pair (y~⋆,θ⋆)(\tilde y^\star,\theta^\star) is then a draw from p(y~,θ∣y)p(\tilde y,\theta\mid y), and so y~⋆\tilde y^\star on its own is a draw from the marginal distribution p(y~∣y)p(\tilde y\mid y). Repeating the procedure LL times gives a sample y~(1),…,y~(L)\tilde y^{(1)},\ldots,\tilde y^{(L)} from the predictive distribution, from which we can estimate any summary we like.

For the Poisson model with a Gamma prior, step (a) draws θ⋆\theta^\star from Ga⁡(a+nyˉ, b+n)\operatorname{Ga}(a+n\bar y,\,b+n) and step (b) draws y~⋆\tilde y^\star from Poi⁡(θ⋆)\operatorname{Poi}(\theta^\star). The resulting draws come from the Neg-Bin⁡(a+nyˉ, b+n)\operatorname{Neg\text{-}Bin}(a+n\bar y,\,b+n) distribution found above. The recipe needs nothing more than the ability to sample from the posterior and from the sampling model, which is why it will serve us in much more complicated models later.

5.6 Airline accidents

We now analyse the airline data. Let YiY_i be the number of fatal accidents in year ii, and suppose Yi∣θ∼iidPoi⁡(θ)Y_i\mid\theta\stackrel{\text{iid}}{\sim}\operatorname{Poi}(\theta). For the prior we take θ∼Ga⁡(1,0.05)\theta\sim\operatorname{Ga}(1,0.05). This has mean 1/0.05=201/0.05=20 and standard deviation 1/0.05=20\sqrt1/0.05=20, a vague statement that accidents occur at a rate of the order of twenty a year, carrying the weight of only 0.050.05 of a year’s data. With n=10n=10 years and a total of ∑iyi=238\sum_iy_i=238 accidents, θ∣y∼Ga⁡(1+238,  0.05+10)=Ga⁡(239,  10.05).\theta\mid y\sim\operatorname{Ga}(1+238,\;0.05+10) = \operatorname{Ga}(239,\;10.05). Sampling from this posterior in R:

y <- c(24, 25, 31, 31, 22, 21, 26, 20, 16, 22)
n <- length(y)
a <- 1; b <- 0.05; nsamp <- 100000
set.seed(678)
theta.draws <- rgamma(nsamp, a + sum(y), b + n)
quantile(theta.draws, probs = c(0.025, 0.25, 0.5, 0.75, 0.975))
#     2.5%      25%      50%      75%    97.5%
# 20.85750 22.72680 23.74727 24.80283 26.91193

The 95% central posterior interval for the accident rate is about (20.9,26.9)(20.9,26.9) accidents a year. In this case we could have computed it exactly with qgamma, which gives (20.86,26.89)(20.86,26.89), and the agreement is a useful check on the simulation.

To predict the number of accidents in 1986, we follow the two-stage recipe, drawing a Poisson count for each posterior draw of θ\theta:

postpred.draws <- rpois(nsamp, theta.draws)
quantile(postpred.draws, probs = c(0.025, 0.25, 0.5, 0.75, 0.975))
#  2.5%   25%   50%   75% 97.5%
#    14    20    24    27    34

So, according to this model, the number of fatal accidents in 1986 would lie between 14 and 34 with probability about 0.950.95. Notice how much wider this is than the interval for θ\theta. Ten years of data determine the underlying rate quite well, but a single year’s count varies considerably about the rate even when the rate is known. Confusing the two intervals, and reporting the narrower one as a prediction, is a common error.

Exercises

Exercise 5.1

For the airline data with the Ga⁡(1,0.05)\operatorname{Ga}(1,0.05) prior, find the exact mean and variance of the posterior predictive distribution of the 1986 count. Compare the variance with that of a Poisson distribution whose mean is fixed at the posterior mean of θ\theta.

Show solutionHide solution

The posterior is Ga⁡(an,bn)\operatorname{Ga}(a_n,b_n) with an=239a_n=239 and bn=10.05b_n=10.05. The predictive mean is an/bn≈23.78a_n/b_n\approx23.78. The predictive variance is anbn+anbn2=23.78+2.37≈26.15.\frac{a_n}{b_n}+\frac{a_n}{b_n^2} = 23.78+2.37 \approx 26.15. A Poisson distribution with mean 23.7823.78 has variance 23.7823.78. The extra 2.372.37 is the posterior variance of θ\theta. It is small here because ten years of data determine θ\theta fairly well.

Exercise 5.2

For the plantain data, take θ∼Ga⁡(a,b)\theta\sim\operatorname{Ga}(a,b), and find the limit of the posterior distribution of θ\theta as a,b→0a,b\to0. No region has nine or more plants.

Show solutionHide solution

There are n=100n=100 regions. The total count is ∑iyi=0⋅7+1⋅17+2⋅22+3⋅17+4⋅15+5⋅9+6⋅5+7⋅4+8⋅4=307.\sum_iy_i = 0\cdot7+1\cdot17+2\cdot22+3\cdot17+4\cdot15+5\cdot9+6\cdot5+7\cdot4+8\cdot4 = 307. The posterior is Ga⁡(a+307, b+100)\operatorname{Ga}(a+307,\,b+100), which tends to Ga⁡(307,100)\operatorname{Ga}(307,100), with mean 3.073.07 and standard deviation 307/100≈0.175\sqrt{307}/100\approx0.175. The limiting prior, p(θ)∝θ−1p(\theta)\propto\theta^{-1}, is improper, but the posterior is proper because the total count is positive.

6 Exponential data

The Poisson model describes how many events occur in a fixed period. The exponential model describes the complementary quantity: how long we wait between events. The two are closely linked, and we shall see that the same Gamma family serves as a conjugate prior for both. The chapter also introduces censored observations, which arise whenever a study ends before every waiting time has been observed.

6.1 Setting

We are interested in the lengths of intervals in time or space between events, and we treat the intervals as independent random variables. Examples include the time until a machine breaks down or a patient relapses, the time between successive visitors to a website, and the distance between potholes along a road.

Definition 6.1. A random variable YY has an exponential distribution with rate θ>0\theta>0 if its density is p(y∣θ)=θe−θy,y>0.p(y\mid\theta) = \theta e^{-\theta y}, \qquad y>0.

The sample space Y=(0,∞)\mathcal{Y}=(0,\infty) is an interval, so YY is continuous, and the parameter space is Θ=(0,∞)\Theta=(0,\infty). The mean and variance are E⁡[Y∣θ]=1θ,Var⁡[Y∣θ]=1θ2,\operatorname{E}[Y\mid\theta]=\frac1\theta, \qquad \operatorname{Var}[Y\mid\theta]=\frac1{\theta^2}, so a high rate means short waiting times. The Exp⁡(θ)\operatorname{Exp}(\theta) distribution is the special case Ga⁡(1,θ)\operatorname{Ga}(1,\theta) of the Gamma distribution.

The exponential distribution has a distinctive property: it is memoryless. If Y∼Exp⁡(θ)Y\sim\operatorname{Exp}(\theta), then P(Y>s+t∣Y>s)=P(Y>t)P(Y>s+t\mid Y>s)=P(Y>t) for all s,t>0s,t>0. Having already waited for time ss makes no difference to how much longer we expect to wait. This is appropriate for events that occur at random at a constant rate, but it may not be for, say, the failure of a machine that wears out with age.

6.2 Likelihood and posterior

Suppose Y1,…,Yn∣θ∼iidExp⁡(θ)Y_1,\ldots,Y_n\mid\theta\stackrel{\text{iid}}{\sim}\operatorname{Exp}(\theta). The likelihood is p(y∣θ)=∏i=1nθe−θyi=θne−θ∑iyi.p(y\mid\theta) = \prod_{i=1}^n\theta e^{-\theta y_i} = \theta^ne^{-\theta\sum_iy_i}. It depends on the data only through ∑iyi\sum_iy_i, the total waiting time, which is therefore a sufficient statistic. As a function of θ\theta it is again a Gamma kernel. With a Ga⁡(a,b)\operatorname{Ga}(a,b) prior, p(θ∣y)∝θa−1e−bθ×θne−θ∑iyi=θa+n−1e−(b+∑iyi)θ,p(\theta\mid y)\propto\theta^{a-1}e^{-b\theta}\times\theta^ne^{-\theta\sum_iy_i} = \theta^{a+n-1}e^{-(b+\sum_iy_i)\theta}, so θ∣y∼Ga⁡(a+n,  b+∑i=1nyi).\theta\mid y\sim\operatorname{Ga}\Big(a+n,\;b+\sum_{i=1}^ny_i\Big). The prior carries the information of aa earlier observations with a total waiting time of bb.

It is instructive to compare this with the Poisson case. There, the posterior was Ga⁡(a+total count, b+number of periods)\operatorname{Ga}(a+\text{total count},\,b+\text{number of periods}); here it is Ga⁡(a+number of events, b+total time)\operatorname{Ga}(a+\text{number of events},\,b+\text{total time}). In both cases the first parameter accumulates events and the second accumulates the time over which they were observed, which is often called the exposure. This is no coincidence. If events occur at random at rate θ\theta, the waiting times between them are Exp⁡(θ)\operatorname{Exp}(\theta) and the count in a period of length one is Poi⁡(θ)\operatorname{Poi}(\theta). Both models describe the same process, and both give information about θ\theta in the form of events per unit of exposure.

6.3 Posterior predictive distribution

The method of Section 5.4 applies without change. Let an=a+na_n=a+n and bn=b+nyˉb_n=b+n\bar y be the posterior parameters. The posterior after one further observation y~\tilde y would be Ga⁡(an+1, bn+y~)\operatorname{Ga}(a_n+1,\,b_n+\tilde y), so p(y~∣y)=Exp⁡(y~∣θ) Ga⁡(θ∣an,bn)Ga⁡(θ∣an+1, bn+y~)=θe−θy~⋅bnanΓ(an)θan−1e−bnθ(bn+y~)an+1Γ(an+1)θane−(bn+y~)θ=anbnan(bn+y~)an+1,y~>0,\begin{aligned} p(\tilde y\mid y) &= \frac{\operatorname{Exp}(\tilde y\mid\theta)\,\operatorname{Ga}(\theta\mid a_n,b_n)}{\operatorname{Ga}(\theta\mid a_n+1,\,b_n+\tilde y)} = \frac{\theta e^{-\theta\tilde y}\cdot\dfrac{b_n^{a_n}}{\Gamma(a_n)}\theta^{a_n-1}e^{-b_n\theta}}{\dfrac{(b_n+\tilde y)^{a_n+1}}{\Gamma(a_n+1)}\theta^{a_n}e^{-(b_n+\tilde y)\theta}}\\[0.5em] &= a_n\frac{b_n^{a_n}}{(b_n+\tilde y)^{a_n+1}}, \qquad\tilde y>0, \end{aligned} using Γ(an+1)=anΓ(an)\Gamma(a_n+1)=a_n\Gamma(a_n). This is a Pareto distribution of the second kind, also called the Lomax distribution. Its tail decreases only as a power of y~\tilde y, much more slowly than the exponential tail of any single Exp⁡(θ)\operatorname{Exp}(\theta) distribution. The reason is that the predictive distribution is a mixture of exponential distributions with different rates, and the mixture components with small rates, which are not ruled out by the data, contribute long waiting times. This is the continuous analogue of the overdispersion we saw in the Poisson case.

To simulate from the predictive distribution, draw θ⋆\theta^\star from Ga⁡(an,bn)\operatorname{Ga}(a_n,b_n) and then y~⋆\tilde y^\star from Exp⁡(θ⋆)\operatorname{Exp}(\theta^\star).

6.4 Leukaemia remission times

We now apply the model to the times of remission, in weeks, of leukaemia patients, from a study by Gehan (1965) reported by Cox and Oakes (1984). Half the patients were allocated at random to be treated with the drug 6-mercaptopurine (6-MP); the other half served as controls. An asterisk marks a censored time, which we discuss in the next section.

Drug (6-MP) 6*, 6, 6, 6, 7, 9*, 10*, 10, 11*, 13, 16, 17*, 19*, 20*, 22, 23, 25*, 32*, 32*, 34*, 35*
Control 1, 1, 2, 2, 3, 4, 4, 5, 5, 8, 8, 8, 8, 11, 11, 12, 12, 15, 17, 22, 23

We begin with the control group, which has no censored times. Before fitting a model it is sensible to check whether it is plausible. The slides show a histogram of the control times with the Exp⁡(1/yˉ)\operatorname{Exp}(1/\bar y) density superimposed, where yˉ=8.67\bar y=8.67 weeks, and a quantile–quantile plot of the sorted times against the corresponding quantiles of that exponential distribution. The points in the quantile–quantile plot lie reasonably close to a straight line, and the exponential model seems adequate.

We take Yi∣θ∼iidExp⁡(θ)Y_i\mid\theta\stackrel{\text{iid}}{\sim}\operatorname{Exp}(\theta) with prior θ∼Ga⁡(0.05,1)\theta\sim\operatorname{Ga}(0.05,1). This prior is equivalent to having seen 0.050.05 of an event in one week, and carries very little information. The control group has n=21n=21 patients with total remission time ∑iyi=182\sum_iy_i=182 weeks, so θ∣y∼Ga⁡(0.05+21,  1+182)=Ga⁡(21.05,  183).\theta\mid y\sim\operatorname{Ga}(0.05+21,\;1+182) = \operatorname{Ga}(21.05,\;183).

The rate θ\theta is not the most natural quantity to report. Clinicians would rather know the mean remission time, 1/θ1/\theta. We could find its posterior distribution with the transformation formula of Section 2.6, but there is a much easier way. If θ(1),…,θ(L)\theta^{(1)},\ldots,\theta^{(L)} are draws from the posterior of θ\theta, then 1/θ(1),…,1/θ(L)1/\theta^{(1)},\ldots,1/\theta^{(L)} are draws from the posterior of 1/θ1/\theta:

remistime <- c(1,1,2,2,3,4,4,5,5,8,8,8,8,11,11,12,12,15,17,22,23)
theta.draws <- rgamma(100000, 0.05 + 21, 1 + sum(remistime))
quantile(1/theta.draws, probs = c(0.025, 0.25, 0.5, 0.75, 0.975))
#     2.5%       25%       50%       75%     97.5%
# 5.914584  7.640598  8.832950 10.273225 14.056048

So the mean remission time for control patients lies between about 5.9 and 14.1 weeks with probability 0.950.95. Chapter 8 explains why this simple device of transforming the draws is valid in general.

We can also predict the remission time of a future control patient, by drawing θ⋆\theta^\star from the posterior and then y~⋆\tilde y^\star from Exp⁡(θ⋆)\operatorname{Exp}(\theta^\star). The slides show a histogram of such draws, and ask what is unsatisfactory about it. It is worth considering this before reading further. It may help to ask where the predictive distribution places most of its probability, and to compare that with the remission times actually recorded; and to recall what the memoryless property implies about patients who have just begun remission.

6.5 Censored observations

Twelve of the 21 times in the drug group are marked as censored. For these patients the study ended, or contact with the patient was lost, while they were still in remission. We do not know their remission times; we know only that each was at least as long as the recorded value.

It would be tempting simply to discard these observations, but that would be a serious mistake. The censored patients are precisely those whose remissions lasted longest, so discarding them would make the drug appear much less effective than it is (see Exercise 6.1). Nor can we treat the recorded values as if they were the true remission times, since that would understate them. The correct approach is to use exactly the information we have: that the remission time exceeded the recorded value.

Consider first a single observation, with Y∣θ∼Exp⁡(θ)Y\mid\theta\sim\operatorname{Exp}(\theta) and θ∼Ga⁡(a,b)\theta\sim\operatorname{Ga}(a,b), and suppose that we observe not YY itself but only the event Y≥yY\ge y. The likelihood is the probability of what we observed, which is now the probability of that event: P(Y≥y∣θ)=∫y∞θe−θz dz=[−e−θz]y∞=e−θy.P(Y\ge y\mid\theta) = \int_y^\infty\theta e^{-\theta z}\,\mathrm{d}z = \Big[-e^{-\theta z}\Big]_y^\infty = e^{-\theta y}. The posterior is therefore p(θ∣Y≥y)∝θa−1e−bθ×e−θy=θa−1e−(b+y)θ,soθ∣Y≥y∼Ga⁡(a,  b+y).p(\theta\mid Y\ge y)\propto\theta^{a-1}e^{-b\theta}\times e^{-\theta y} = \theta^{a-1}e^{-(b+y)\theta}, \qquad\text{so}\qquad \theta\mid Y\ge y\sim\operatorname{Ga}(a,\;b+y). Compare this with the posterior when the time is observed exactly, θ∣Y=y∼Ga⁡(a+1, b+y)\theta\mid Y=y\sim\operatorname{Ga}(a+1,\,b+y). In the language of events and exposure, a censored observation adds its time to the exposure, just as an exact observation does, but it does not add an event, since the event (the end of remission) has not been seen.

With several independent observations, some exact and some censored, the likelihood is the product of the densities θe−θyi\theta e^{-\theta y_i} for the exact observations and the survival probabilities e−θyie^{-\theta y_i} for the censored ones. If dd of the observations are exact, and TT is the sum of all the recorded times, exact and censored, the likelihood is θde−θT\theta^de^{-\theta T} and θ∣data∼Ga⁡(a+d,  b+T).\theta\mid\text{data}\sim\operatorname{Ga}(a+d,\;b+T). The general principle, which applies well beyond the exponential model, is that the likelihood should always be the probability of exactly what was observed, no more and no less.

Exercises

Exercise 6.1

For the drug group, use the Ga⁡(0.05,1)\operatorname{Ga}(0.05,1) prior and the result above.

  1. Find the posterior distribution of θT\theta_T, the rate for the drug group.

  2. Find E⁡[1/θT∣data]\operatorname{E}[1/\theta_T\mid\text{data}] and compare it with the same quantity for the control group.

  3. What would the posterior have been had the censored observations been discarded?

  4. Describe how to estimate P(θT<θC∣data)P(\theta_T<\theta_C\mid\text{data}) by simulation, assuming θT\theta_T and θC\theta_C are independent a priori.

Show solutionHide solution

(a) In the drug group there are d=9d=9 exact times, summing to 109109, and 1212 censored times, summing to 250250. So T=359T=359 and θT∣data∼Ga⁡(0.05+9,  1+359)=Ga⁡(9.05,  360).\theta_T\mid\text{data}\sim\operatorname{Ga}(0.05+9,\;1+359) = \operatorname{Ga}(9.05,\;360).

(b) By Exercise 6.2, E⁡[1/θT∣data]=360/8.05≈44.7\operatorname{E}[1/\theta_T\mid\text{data}]=360/8.05\approx44.7 weeks. For the control group, E⁡[1/θC∣y]=183/20.05≈9.1\operatorname{E}[1/\theta_C\mid y]=183/20.05\approx9.1 weeks.

(c) Discarding the censored times leaves Ga⁡(9.05, 110)\operatorname{Ga}(9.05,\,110), with E⁡[1/θT]=110/8.05≈13.7\operatorname{E}[1/\theta_T]=110/8.05\approx13.7 weeks. The drug would appear far less effective, because the discarded patients are those with the longest remissions.

(d) Draw θT(j)∼Ga⁡(9.05,360)\theta_T^{(j)}\sim\operatorname{Ga}(9.05,360) and θC(j)∼Ga⁡(21.05,183)\theta_C^{(j)}\sim\operatorname{Ga}(21.05,183) independently, for j=1,…,Lj=1,\ldots,L. Estimate the probability by the proportion of jj with θT(j)<θC(j)\theta_T^{(j)}<\theta_C^{(j)}. The posteriors are independent because the priors are independent and the likelihood factorises into a part for each group. With L=106L=10^6 the estimate is about 0.999980.99998.

Exercise 6.2

Show that if θ∼Ga⁡(a,b)\theta\sim\operatorname{Ga}(a,b) with a>1a>1, then E⁡[1/θ]=b/(a−1)\operatorname{E}[1/\theta]=b/(a-1).

Show solutionHide solution

E⁡[1/θ]=∫0∞1θbaΓ(a)θa−1e−bθ dθ=baΓ(a)⋅Γ(a−1)ba−1=ba−1,\operatorname{E}[1/\theta] = \int_0^\infty\frac1\theta\frac{b^a}{\Gamma(a)}\theta^{a-1}e^{-b\theta}\,\mathrm{d}\theta = \frac{b^a}{\Gamma(a)}\cdot\frac{\Gamma(a-1)}{b^{a-1}} = \frac b{a-1}, using the Gamma integral ∫0∞θc−1e−bθ dθ=Γ(c)/bc\int_0^\infty\theta^{c-1}e^{-b\theta}\,\mathrm{d}\theta=\Gamma(c)/b^c with c=a−1>0c=a-1>0, and Γ(a)=(a−1)Γ(a−1)\Gamma(a)=(a-1)\Gamma(a-1). Note that E⁡[1/θ]≠1/E⁡[θ]=b/a\operatorname{E}[1/\theta]\ne1/\operatorname{E}[\theta]=b/a.

7 Normal data

References: Hoff §5.2–5.3; GCSR §2.5–2.6, §3.2–3.4.

The normal distribution is the most widely used model for continuous data, and its Bayesian analysis introduces two new features. First, the model has two parameters, the mean and the variance, and we must think about prior beliefs on both and about how they interact. Second, the posterior for one parameter when the other is unknown is no longer of the same form as when the other is known, and this is our first encounter with the need to integrate out a parameter that is not of direct interest.

7.1 Setting

The normal model is suitable for quantities that can take any value on the real line and are roughly symmetric about a central value. Its wide applicability is explained, at least in part, by the central limit theorem: a quantity formed as the sum of many small independent contributions is approximately normally distributed, whatever the distributions of the contributions. Examples include the daily change in the logarithm of a stock price, the error in a measurement made by an optical instrument, and the position of an animal moving at random.

Definition 7.1. A random variable YY has a normal distribution with mean θ\theta and variance σ2\sigma^2, written Y∼N⁡(θ,σ2)Y\sim\operatorname{N}(\theta,\sigma^2), if its density is p(y∣θ,σ2)=12π σexp⁡{−12σ2(y−θ)2},−∞<y<∞.p(y\mid\theta,\sigma^2) = \frac1{\sqrt{2\pi}\,\sigma}\exp\left\{-\frac1{2\sigma^2}(y-\theta)^2\right\}, \qquad -\infty<y<\infty.

Our running example is a set of 100 measurements of the speed of light in air made by Michelson in the summer of 1879. The values are recorded in kilometres per second, with 299 000 subtracted. On this scale the currently accepted value is 734.5.

We consider three cases in turn, each building on the last: the mean unknown with the variance known; the mean known with the variance unknown; and both unknown. The first two are rarely realistic in themselves, since we seldom know one parameter exactly while being uncertain about the other. But they are the building blocks for the third case, and, as we shall see in Chapter 9, for the Gibbs sampler.

Throughout, it helps to think in terms of precision, the reciprocal of a variance. Precision measures how much information a distribution carries: a small variance means a high precision and a sharply defined quantity.

7.2 Mean unknown, variance known

7.2.1 A single observation

We start with the simplest case. Let Y∣θ∼N⁡(θ,σ2)Y\mid\theta\sim\operatorname{N}(\theta,\sigma^2), where σ2\sigma^2 is known, and give θ\theta a normal prior, θ∼N⁡(μ0,τ02)\theta\sim\operatorname{N}(\mu_0,\tau_0^2), with μ0\mu_0 and τ02\tau_0^2 fixed. The prior mean μ0\mu_0 is our best prior guess for θ\theta and the prior variance τ02\tau_0^2 expresses how uncertain we are about it.

The posterior is proportional to the product of prior and likelihood: p(θ∣y)∝exp⁡{−(θ−μ0)22τ02}×exp⁡{−(y−θ)22σ2}=exp⁡{−12[(θ−μ0)2τ02+(y−θ)2σ2]}.p(\theta\mid y) \propto \exp\left\{-\frac{(\theta-\mu_0)^2}{2\tau_0^2}\right\}\times\exp\left\{-\frac{(y-\theta)^2}{2\sigma^2}\right\} = \exp\left\{-\frac12\left[\frac{(\theta-\mu_0)^2}{\tau_0^2}+\frac{(y-\theta)^2}{\sigma^2}\right]\right\}. The expression in square brackets is a quadratic in θ\theta, so the posterior is the exponential of a quadratic, which is the form of a normal density. To identify its mean and variance we expand the squares and collect terms in θ\theta: θ2−2μ0θ+μ02τ02+θ2−2yθ+y2σ2=θ2(1τ02+1σ2)−2θ(μ0τ02+yσ2)+terms free of θ.\frac{\theta^2-2\mu_0\theta+\mu_0^2}{\tau_0^2}+\frac{\theta^2-2y\theta+y^2}{\sigma^2} = \theta^2\left(\frac1{\tau_0^2}+\frac1{\sigma^2}\right)-2\theta\left(\frac{\mu_0}{\tau_0^2}+\frac y{\sigma^2}\right)+\text{terms free of }\theta. The terms free of θ\theta contribute a constant factor to the posterior and may be dropped. Now define 1τ12=1τ02+1σ2,μ1=τ12(μ0τ02+yσ2).\frac1{\tau_1^2} = \frac1{\tau_0^2}+\frac1{\sigma^2}, \qquad \mu_1 = \tau_1^2\left(\frac{\mu_0}{\tau_0^2}+\frac y{\sigma^2}\right). With this notation the posterior is p(θ∣y)∝exp⁡{−12τ12(θ2−2θμ1)}∝exp⁡{−12τ12(θ2−2θμ1+μ12)}=exp⁡{−12τ12(θ−μ1)2}.\begin{aligned} p(\theta\mid y)&\propto\exp\left\{-\frac1{2\tau_1^2}\left(\theta^2-2\theta\mu_1\right)\right\}\\ &\propto\exp\left\{-\frac1{2\tau_1^2}\left(\theta^2-2\theta\mu_1+\mu_1^2\right)\right\} = \exp\left\{-\frac1{2\tau_1^2}(\theta-\mu_1)^2\right\}. \end{aligned} In the second step we completed the square by multiplying by exp⁡{−μ12/2τ12}\exp\{-\mu_1^2/2\tau_1^2\}, which is permissible because this factor does not involve θ\theta. The result is the kernel of a normal density, and so θ∣y∼N⁡(μ1,τ12).\theta\mid y\sim\operatorname{N}(\mu_1,\tau_1^2). The normal prior is therefore conjugate for the normal model with known variance.

The formulae for μ1\mu_1 and τ12\tau_1^2 have a clear interpretation.

  • The posterior precision is the sum of the prior precision and the precision of the observation: 1/τ12=1/τ02+1/σ21/\tau_1^2=1/\tau_0^2+1/\sigma^2. Information from the two sources simply adds, and the posterior is always more precise than either the prior or the observation alone.

  • The posterior mean is a weighted average of the prior mean and the observation, μ1=μ0/τ02+y/σ21/τ02+1/σ2,\mu_1 = \frac{\mu_0/\tau_0^2+y/\sigma^2}{1/\tau_0^2+1/\sigma^2}, in which each is weighted by its precision. If the prior is vague (τ02\tau_0^2 large), the posterior mean is close to the observation; if the measurement is imprecise (σ2\sigma^2 large), it stays close to the prior mean.

7.2.2 Several observations

Now suppose we have nn observations, Y1,…,Yn∣θ∼iidN⁡(θ,σ2)Y_1,\ldots,Y_n\mid\theta\stackrel{\text{iid}}{\sim}\operatorname{N}(\theta,\sigma^2), with the same prior. The posterior is p(θ∣y)∝exp⁡{−(θ−μ0)22τ02}∏i=1nexp⁡{−(yi−θ)22σ2}=exp⁡{−12[(θ−μ0)2τ02+1σ2(∑iyi2−2θ∑iyi+nθ2)]}∝exp⁡{−12[(θ−μ0)2τ02+nσ2(θ2−2θyˉ)]},\begin{aligned} p(\theta\mid y) &\propto \exp\left\{-\frac{(\theta-\mu_0)^2}{2\tau_0^2}\right\}\prod_{i=1}^n\exp\left\{-\frac{(y_i-\theta)^2}{2\sigma^2}\right\}\\ &= \exp\left\{-\frac12\left[\frac{(\theta-\mu_0)^2}{\tau_0^2}+\frac1{\sigma^2}\Big(\sum_iy_i^2-2\theta\sum_iy_i+n\theta^2\Big)\right]\right\}\\ &\propto \exp\left\{-\frac12\left[\frac{(\theta-\mu_0)^2}{\tau_0^2}+\frac{n}{\sigma^2}\big(\theta^2-2\theta\bar y\big)\right]\right\}, \end{aligned} where in the last step we dropped ∑iyi2\sum_iy_i^2, which does not involve θ\theta, and wrote ∑iyi=nyˉ\sum_iy_i=n\bar y.

The data now enter only through the sample mean yˉ\bar y, which is therefore sufficient for θ\theta. Moreover, the expression has exactly the form we had for a single observation, with yy replaced by yˉ\bar y and σ2\sigma^2 replaced by σ2/n\sigma^2/n. This makes sense: the sample mean Yˉ\bar Y has distribution N⁡(θ,σ2/n)\operatorname{N}(\theta,\sigma^2/n), so observing the whole sample is equivalent, as far as θ\theta is concerned, to observing its mean, a single observation with precision n/σ2n/\sigma^2. Making these substitutions in the single-observation result, θ∣y∼N⁡(μn,τn2),1τn2=1τ02+nσ2,μn=μ0/τ02+nyˉ/σ21/τ02+n/σ2.\theta\mid y\sim\operatorname{N}(\mu_n,\tau_n^2), \qquad \frac1{\tau_n^2}=\frac1{\tau_0^2}+\frac n{\sigma^2}, \qquad \mu_n=\frac{\mu_0/\tau_0^2+n\bar y/\sigma^2}{1/\tau_0^2+n/\sigma^2}. So the posterior precision is the prior precision plus the precision of the sample mean, and the posterior mean is a precision-weighted average of the prior mean and the sample mean.

As nn increases, the data precision n/σ2n/\sigma^2 grows without limit, while the prior precision stays fixed. Unless nn is very small, or the prior is very precise compared with a single observation (τ0≪σ\tau_0\ll\sigma), the data dominate and τn2≈σ2n,μn≈yˉ,soθ∣y  ∼˙  N⁡(yˉ,σ2n).\tau_n^2\approx\frac{\sigma^2}n, \qquad \mu_n\approx\bar y, \qquad\text{so}\qquad \theta\mid y\;\dot\sim\;\operatorname{N}\left(\bar y,\frac{\sigma^2}n\right). The corresponding 95% central posterior interval is approximately yˉ±1.96 σ/n\bar y\pm1.96\,\sigma/\sqrt n. This is numerically the same as the familiar frequentist confidence interval for a normal mean with known variance, a coincidence that recurs in many models when the prior is vague and the sample is large. As discussed in Chapter 4, the interpretation is different: here the interval is a probability statement about θ\theta given the observed data.

7.2.3 Prediction

Let Y~\tilde Y be a future observation. Given θ\theta, Y~∼N⁡(θ,σ2)\tilde Y\sim\operatorname{N}(\theta,\sigma^2), independently of the data. It is convenient to write Y~=θ+ε\tilde Y=\theta+\varepsilon, where ε∼N⁡(0,σ2)\varepsilon\sim\operatorname{N}(0,\sigma^2) is independent of both θ\theta and the data. Given the data, θ∼N⁡(μn,τn2)\theta\sim\operatorname{N}(\mu_n,\tau_n^2). So, given the data, Y~\tilde Y is the sum of two independent normal random variables, and is therefore itself normal. (Equivalently, (Y~,θ)(\tilde Y,\theta) is bivariate normal given yy, and so Y~\tilde Y is marginally normal.) It remains only to find its mean and variance, which we do with (2.1) and (2.2): E⁡[Y~∣y]=E⁡[E⁡[Y~∣θ,y]∣y]=E⁡[θ∣y]=μn,Var⁡[Y~∣y]=E⁡[Var⁡[Y~∣θ,y]∣y]+Var⁡[E⁡[Y~∣θ,y]∣y]=E⁡[σ2∣y]+Var⁡[θ∣y]=σ2+τn2.\begin{aligned} \operatorname{E}[\tilde Y\mid y] &= \operatorname{E}\big[\operatorname{E}[\tilde Y\mid\theta,y]\mid y\big] = \operatorname{E}[\theta\mid y] = \mu_n,\\ \operatorname{Var}[\tilde Y\mid y] &= \operatorname{E}\big[\operatorname{Var}[\tilde Y\mid\theta,y]\mid y\big]+\operatorname{Var}\big[\operatorname{E}[\tilde Y\mid\theta,y]\mid y\big] = \operatorname{E}[\sigma^2\mid y]+\operatorname{Var}[\theta\mid y] = \sigma^2+\tau_n^2. \end{aligned} Hence Y~∣y∼N⁡(μn,  σ2+τn2).\tilde Y\mid y\sim\operatorname{N}(\mu_n,\;\sigma^2+\tau_n^2). The predictive variance is the sum of the sampling variance σ2\sigma^2 and the posterior variance of the mean τn2\tau_n^2. As in the Poisson case, these reflect two different kinds of uncertainty. The posterior variance τn2\tau_n^2 can be reduced by collecting more data, and tends to zero as n→∞n\to\infty. The sampling variance cannot: however well we know θ\theta, a single new observation will vary about it with variance σ2\sigma^2.

7.3 Mean known, variance unknown

Next suppose the mean θ\theta is known and the variance σ2\sigma^2 is not: Y1,…,Yn∣σ2∼iidN⁡(θ,σ2)Y_1,\ldots,Y_n\mid\sigma^2\stackrel{\text{iid}}{\sim}\operatorname{N}(\theta,\sigma^2).

The likelihood, regarded as a function of σ2\sigma^2, is p(y∣σ2)∝(σ2)−n/2exp⁡{−12σ2∑i=1n(yi−θ)2}.p(y\mid\sigma^2)\propto(\sigma^2)^{-n/2}\exp\left\{-\frac1{2\sigma^2}\sum_{i=1}^n(y_i-\theta)^2\right\}. This involves a power of σ2\sigma^2 multiplied by the exponential of a constant divided by σ2\sigma^2. As a function of the precision 1/σ21/\sigma^2 it would be a Gamma kernel. As a function of σ2\sigma^2 itself it is the kernel of the distribution of the reciprocal of a Gamma variable.

Definition 7.2. If X∼Ga⁡(a,b)X\sim\operatorname{Ga}(a,b) and Z=1/XZ=1/X, we say that ZZ has an inverse-gamma distribution and write Z∼Inv-Ga⁡(a,b)Z\sim\operatorname{Inv\text{-}Ga}(a,b). By the transformation formula of Section 2.6, with ∣ dx/ dz∣=1/z2|\,\mathrm{d}x/\,\mathrm{d}z|=1/z^2, p(z)=baΓ(a)z−(a+1)e−b/z,z>0.p(z) = \frac{b^a}{\Gamma(a)}z^{-(a+1)}e^{-b/z}, \qquad z>0. Its mean is b/(a−1)b/(a-1) for a>1a>1 (Exercise 6.2).

We therefore take an inverse-gamma prior for σ2\sigma^2. For the sake of interpretation it is convenient to write its parameters as a=ν0/2a=\nu_0/2 and b=ν0σ02/2b=\nu_0\sigma_0^2/2: σ2∼Inv-Ga⁡(ν02,  ν0σ022).\sigma^2\sim\operatorname{Inv\text{-}Ga}\left(\frac{\nu_0}2,\;\frac{\nu_0\sigma_0^2}2\right). The reason for this choice will become clear in a moment. Let v=1n∑i=1n(yi−θ)2v=\frac1n\sum_{i=1}^n(y_i-\theta)^2 be the mean squared deviation of the observations from the known mean. Then p(σ2∣y)∝(σ2)−(ν0/2+1)exp⁡{−ν0σ022σ2}×(σ2)−n/2exp⁡{−nv2σ2}=(σ2)−((ν0+n)/2+1)exp⁡{−ν0σ02+nv2σ2},\begin{aligned} p(\sigma^2\mid y) &\propto (\sigma^2)^{-(\nu_0/2+1)}\exp\left\{-\frac{\nu_0\sigma_0^2}{2\sigma^2}\right\}\times(\sigma^2)^{-n/2}\exp\left\{-\frac{nv}{2\sigma^2}\right\}\\ &= (\sigma^2)^{-((\nu_0+n)/2+1)}\exp\left\{-\frac{\nu_0\sigma_0^2+nv}{2\sigma^2}\right\}, \end{aligned} which is an inverse-gamma kernel. Hence σ2∣y∼Inv-Ga⁡(ν0+n2,  ν0σ02+nv2),\sigma^2\mid y\sim\operatorname{Inv\text{-}Ga}\left(\frac{\nu_0+n}2,\;\frac{\nu_0\sigma_0^2+nv}2\right), and the inverse-gamma prior is conjugate.

The parameterisation now explains itself. In the likelihood, nn is the number of observations and nvnv is their sum of squared deviations. In the prior, ν0\nu_0 plays the part of a number of observations and ν0σ02\nu_0\sigma_0^2 that of a sum of squares. So the prior carries the information of ν0\nu_0 observations whose mean squared deviation is σ02\sigma_0^2, and updating consists of adding the actual sample size to the prior sample size and the actual sum of squares to the prior sum of squares: ν0  ⟶  ν0+n,ν0σ02  ⟶  ν0σ02+nv.\nu_0\;\longrightarrow\;\nu_0+n, \qquad \nu_0\sigma_0^2\;\longrightarrow\;\nu_0\sigma_0^2+nv.

7.4 Both mean and variance unknown

In practice both parameters are usually unknown, and we need a joint prior for (θ,σ2)(\theta,\sigma^2). Two choices are common. The conjugate prior, which we treat here, makes θ\theta and σ2\sigma^2 dependent a priori. The semi-conjugate prior, treated in Hoff §6.1–6.3, makes them independent a priori; its posterior is not of a standard form, and it is most easily handled with the Gibbs sampler of Chapter 9 (see Exercise 9.2).

7.4.1 The conjugate prior

The conjugate model is Y1,…,Yn∣θ,σ2∼iidN⁡(θ,σ2),θ∣σ2∼N⁡(μ0, σ2/κ0),σ2∼Inv-Ga⁡(ν0/2, ν0σ02/2).\begin{aligned} Y_1,\ldots,Y_n\mid\theta,\sigma^2 &\stackrel{\text{iid}}{\sim}\operatorname{N}(\theta,\sigma^2),\\ \theta\mid\sigma^2 &\sim \operatorname{N}(\mu_0,\,\sigma^2/\kappa_0),\\ \sigma^2 &\sim \operatorname{Inv\text{-}Ga}(\nu_0/2,\,\nu_0\sigma_0^2/2). \end{aligned} The prior is specified in two stages: a marginal prior for σ2\sigma^2 of the kind used in the previous section, and a conditional prior for θ\theta given σ2\sigma^2. Since the conditional prior for θ\theta involves σ2\sigma^2, the two parameters are dependent a priori: the larger the variance of the observations, the less certain we are about their mean.

The prior variance of θ\theta, σ2/κ0\sigma^2/\kappa_0, has been written as a multiple of the sampling variance. The effect is that our prior information about θ\theta is expressed in units of observations. Comparing with the result of Section 7.2, where the sample mean of nn observations had variance σ2/n\sigma^2/n, we see that the prior for θ\theta is equivalent to the information in κ0\kappa_0 observations with mean μ0\mu_0. Similarly the prior for σ2\sigma^2 is equivalent to ν0\nu_0 observations with mean squared deviation σ02\sigma_0^2. This is a natural and convenient way to specify a prior when our prior knowledge really can be thought of as coming from an earlier sample. It is less natural if, for example, we have firm knowledge about θ\theta from a source that has nothing to do with the variability of the measurements. Another way to see the structure is that the prior on θ\theta given σ2\sigma^2 implies θσ ∣ σ2∼N⁡(μ0σ,  1κ0),\left.\frac\theta\sigma\,\right|\,\sigma^2\sim\operatorname{N}\left(\frac{\mu_0}\sigma,\;\frac1{\kappa_0}\right), so the prior fixes our uncertainty about the mean measured in units of the standard deviation.

7.4.2 The posterior

Theorem 7.3. Under the conjugate prior, θ∣σ2,y∼N⁡(μn,  σ2κn),σ2∣y∼Inv-Ga⁡(νn2,  νnσn22),\theta\mid\sigma^2,y\sim\operatorname{N}\left(\mu_n,\;\frac{\sigma^2}{\kappa_n}\right), \qquad \sigma^2\mid y\sim\operatorname{Inv\text{-}Ga}\left(\frac{\nu_n}2,\;\frac{\nu_n\sigma_n^2}2\right), where κn=κ0+n,μn=κ0μ0+nyˉκ0+n,νn=ν0+n,νnσn2=ν0σ02+(n−1)s2+κ0nκ0+n(yˉ−μ0)2,\begin{gathered} \kappa_n=\kappa_0+n, \qquad \mu_n=\frac{\kappa_0\mu_0+n\bar y}{\kappa_0+n}, \qquad \nu_n=\nu_0+n,\\ \nu_n\sigma_n^2 = \nu_0\sigma_0^2+(n-1)s^2+\frac{\kappa_0n}{\kappa_0+n}(\bar y-\mu_0)^2, \end{gathered} with yˉ=1n∑iyi\bar y=\frac1n\sum_iy_i and s2=1n−1∑i(yi−yˉ)2s^2=\frac1{n-1}\sum_i(y_i-\bar y)^2.

Proof. The joint posterior can be written p(θ,σ2∣y)=p(θ∣σ2,y) p(σ2∣y)p(\theta,\sigma^2\mid y)=p(\theta\mid\sigma^2,y)\,p(\sigma^2\mid y), and we find the two factors in turn.

The conditional posterior of θ\theta. For fixed σ2\sigma^2, the model for θ\theta is exactly that of Section 7.2, with prior variance τ02=σ2/κ0\tau_0^2=\sigma^2/\kappa_0. The prior precision is κ0/σ2\kappa_0/\sigma^2 and the data precision is n/σ2n/\sigma^2. They add to give posterior precision κn/σ2\kappa_n/\sigma^2, and the posterior mean is the precision-weighted average (κ0μ0+nyˉ)/(κ0+n)=μn(\kappa_0\mu_0+n\bar y)/(\kappa_0+n)=\mu_n.

The marginal posterior of σ2\sigma^2. We need the identity ∑i=1n(yi−θ)2=∑i=1n(yi−yˉ)2+n(yˉ−θ)2=(n−1)s2+n(yˉ−θ)2,\sum_{i=1}^n(y_i-\theta)^2 = \sum_{i=1}^n(y_i-\bar y)^2+n(\bar y-\theta)^2 = (n-1)s^2+n(\bar y-\theta)^2, which follows by writing yi−θ=(yi−yˉ)+(yˉ−θ)y_i-\theta=(y_i-\bar y)+(\bar y-\theta) and noting that the cross term sums to zero. The joint posterior is proportional to prior times likelihood: p(θ,σ2∣y)∝(σ2)−(ν0/2+1)e−ν0σ02/2σ2⏟p(σ2)  (σ2)−1/2e−κ0(θ−μ0)2/2σ2⏟p(θ∣σ2)  (σ2)−n/2e−[(n−1)s2+n(yˉ−θ)2]/2σ2⏟p(y∣θ,σ2).p(\theta,\sigma^2\mid y)\propto\underbrace{(\sigma^2)^{-(\nu_0/2+1)}e^{-\nu_0\sigma_0^2/2\sigma^2}}_{p(\sigma^2)}\;\underbrace{(\sigma^2)^{-1/2}e^{-\kappa_0(\theta-\mu_0)^2/2\sigma^2}}_{p(\theta\mid\sigma^2)}\;\underbrace{(\sigma^2)^{-n/2}e^{-[(n-1)s^2+n(\bar y-\theta)^2]/2\sigma^2}}_{p(y\mid\theta,\sigma^2)}. The two quadratics in θ\theta can be combined by completing the square: κ0(θ−μ0)2+n(θ−yˉ)2=κn(θ−μn)2+κ0nκn(yˉ−μ0)2.\kappa_0(\theta-\mu_0)^2+n(\theta-\bar y)^2 = \kappa_n(\theta-\mu_n)^2+\frac{\kappa_0n}{\kappa_n}(\bar y-\mu_0)^2. (This can be checked by expanding both sides.) Substituting, the joint posterior becomes p(θ,σ2∣y)∝(σ2)−(νn/2+1)exp⁡{−νnσn22σ2}×(σ2)−1/2exp⁡{−κn(θ−μn)22σ2},p(\theta,\sigma^2\mid y)\propto(\sigma^2)^{-(\nu_n/2+1)}\exp\left\{-\frac{\nu_n\sigma_n^2}{2\sigma^2}\right\}\times(\sigma^2)^{-1/2}\exp\left\{-\frac{\kappa_n(\theta-\mu_n)^2}{2\sigma^2}\right\}, where νnσn2\nu_n\sigma_n^2 is as defined in the statement. To find the marginal posterior of σ2\sigma^2 we integrate over θ\theta. The second factor is, apart from the constant 2π/κn\sqrt{2\pi/\kappa_n}, a normal density in θ\theta with variance σ2/κn\sigma^2/\kappa_n, and so integrates to a constant that does not depend on σ2\sigma^2. What remains is the first factor, which is the stated inverse-gamma kernel. ◻

The updating rules are the natural extension of those we have seen. The prior sample sizes κ0\kappa_0 and ν0\nu_0 each increase by nn. The posterior mean μn\mu_n is the weighted average of prior mean and sample mean, with weights κ0\kappa_0 and nn. The posterior sum of squares νnσn2\nu_n\sigma_n^2 has three parts: the prior sum of squares ν0σ02\nu_0\sigma_0^2; the sum of squares within the sample, (n−1)s2(n-1)s^2; and a term that measures the disagreement between the prior mean and the sample mean. If the data turn out to be centred far from where the prior expected, this last term is large and our estimate of the variance increases, which is sensible, since such a discrepancy is itself evidence of greater variability than we supposed.

7.4.3 Simulating from the posterior

The factorisation p(θ,σ2∣y)=p(σ2∣y) p(θ∣σ2,y)p(\theta,\sigma^2\mid y)=p(\sigma^2\mid y)\,p(\theta\mid\sigma^2,y) tells us how to simulate from the joint posterior. For j=1,…,Lj=1,\ldots,L:

  1. draw σ(j)2\sigma^2_{(j)} from the marginal posterior Inv-Ga⁡(νn/2, νnσn2/2)\operatorname{Inv\text{-}Ga}(\nu_n/2,\,\nu_n\sigma_n^2/2), which we do by drawing from the Ga⁡(νn/2, νnσn2/2)\operatorname{Ga}(\nu_n/2,\,\nu_n\sigma_n^2/2) distribution and taking the reciprocal;

  2. draw θ(j)\theta_{(j)} from the conditional posterior N⁡(μn, σ(j)2/κn)\operatorname{N}(\mu_n,\,\sigma^2_{(j)}/\kappa_n), using the value of σ2\sigma^2 just drawn.

Each pair (θ(j),σ(j)2)(\theta_{(j)},\sigma^2_{(j)}) is then an independent draw from the joint posterior. In R:

normalconj <- function(nsamp, y, nu0, sigma0, mu0, kappa0) {
  n <- length(y); ybar <- mean(y); s2 <- var(y)
  kappan <- kappa0 + n
  mun <- (kappa0 * mu0 + n * ybar) / kappan
  nun <- nu0 + n
  sigman2 <- (nu0 * sigma0^2 + (n - 1) * s2 +
              (kappa0 * n / kappan) * (ybar - mu0)^2) / nun
  sigma2.draws <- 1 / rgamma(nsamp, nun / 2, nun * sigman2 / 2)
  theta.draws <- rnorm(nsamp, mun, sqrt(sigma2.draws / kappan))
  list(theta = theta.draws, sigma2 = sigma2.draws)
}

The draws θ(1),…,θ(L)\theta_{(1)},\ldots,\theta_{(L)} taken on their own are a sample from the marginal posterior of θ\theta, which is usually the distribution of main interest. Here σ2\sigma^2 is what is called a nuisance parameter: it must be included in the model, but we are not directly interested in it, and simulation lets us account for our uncertainty about it simply by ignoring the σ2\sigma^2 draws at the end.

7.4.4 The marginal posterior of θ\theta

In this model the marginal posterior of θ\theta can also be found exactly: θ−μnσn/κn ∣ y  ∼  tνn,\left.\frac{\theta-\mu_n}{\sigma_n/\sqrt{\kappa_n}}\,\right|\,y\;\sim\;t_{\nu_n}, a tt distribution with νn\nu_n degrees of freedom. To see this, integrate σ2\sigma^2 out of the joint posterior found in the proof above. As a function of σ2\sigma^2, the joint density is (σ2)−((νn+1)/2+1)exp⁡{−νnσn2+κn(θ−μn)22σ2},(\sigma^2)^{-((\nu_n+1)/2+1)}\exp\left\{-\frac{\nu_n\sigma_n^2+\kappa_n(\theta-\mu_n)^2}{2\sigma^2}\right\}, which is an inverse-gamma kernel with shape (νn+1)/2(\nu_n+1)/2 and scale B=12[νnσn2+κn(θ−μn)2]B=\frac12[\nu_n\sigma_n^2+\kappa_n(\theta-\mu_n)^2]. The integral of an Inv-Ga⁡(A,B)\operatorname{Inv\text{-}Ga}(A,B) kernel over (0,∞)(0,\infty) is Γ(A)/BA\Gamma(A)/B^A, so p(θ∣y)∝B−(νn+1)/2∝[1+1νn (θ−μn)2σn2/κn]−(νn+1)/2,p(\theta\mid y)\propto B^{-(\nu_n+1)/2}\propto\left[1+\frac1{\nu_n}\,\frac{(\theta-\mu_n)^2}{\sigma_n^2/\kappa_n}\right]^{-(\nu_n+1)/2}, which is the kernel of the stated tt distribution. So uncertainty about σ2\sigma^2 turns what would have been a normal posterior for θ\theta (if σ2\sigma^2 were known) into a tt posterior, which has heavier tails. With νn\nu_n large, as it is when nn is large, the tt distribution is close to the normal and the distinction matters little. The result is the Bayesian counterpart of the familiar tt interval for a normal mean, and with a vague prior (κ0,ν0→0\kappa_0,\nu_0\to0) the two coincide numerically.

7.4.5 Prediction

The posterior predictive density of a future observation Y~\tilde Y is p(y~∣y)=∬p(y~∣θ,σ2) p(θ,σ2∣y) dθ dσ2=∬N⁡(y~∣θ,σ2) p(θ,σ2∣y) dθ dσ2.p(\tilde y\mid y) = \iint p(\tilde y\mid\theta,\sigma^2)\,p(\theta,\sigma^2\mid y)\,\mathrm{d}\theta\,\mathrm{d}\sigma^2 = \iint\operatorname{N}(\tilde y\mid\theta,\sigma^2)\,p(\theta,\sigma^2\mid y)\,\mathrm{d}\theta\,\mathrm{d}\sigma^2. To draw from it we follow the usual procedure: draw (θ,σ2)(\theta,\sigma^2) from the joint posterior as above, and then draw y~\tilde y from N⁡(θ,σ2)\operatorname{N}(\theta,\sigma^2). In this model an exact result is also available: Y~−μnσn1+1/κn ∣ y  ∼  tνn.\left.\frac{\tilde Y-\mu_n}{\sigma_n\sqrt{1+1/\kappa_n}}\,\right|\,y\;\sim\;t_{\nu_n}. The argument is the same as for the marginal posterior of θ\theta. Given σ2\sigma^2, the result of Section 7.2.3 gives Y~∣σ2,y∼N⁡(μn,σ2+σ2/κn)\tilde Y\mid\sigma^2,y\sim\operatorname{N}(\mu_n,\sigma^2+\sigma^2/\kappa_n), and integrating over the inverse-gamma posterior of σ2\sigma^2 turns this normal into a tt.

7.4.6 Michelson’s data

For the Michelson data, n=100n=100, yˉ=852.4\bar y=852.4 and s=79.01s=79.01. The slides show 10 000 draws from the joint and marginal posteriors under two priors:

  • ν0=1\nu_0=1, σ0=1\sigma_0=1, μ0=1000\mu_0=1000, κ0=1\kappa_0=1;

  • ν0=1\nu_0=1, σ0=1\sigma_0=1, μ0=1\mu_0=1, κ0=10−6\kappa_0=10^{-6}.

The two priors differ considerably in their means, but in both cases κ0\kappa_0 and ν0\nu_0 are small compared with n=100n=100, so the prior carries the weight of at most one observation and the posteriors are dominated by the data. The two sets of plots are accordingly very similar. The posterior for θ\theta is concentrated around 850, with a 95% interval roughly 16 units either side (Exercise 7.1).

Both posteriors lie far from the accepted value of 734.5. This is not a failure of the Bayesian method but of the model. The model assumes that the measurements are centred on the true speed of light, and Michelson’s apparatus in 1879 was evidently subject to a systematic error of which he was unaware. The posterior faithfully reports what the data say under the assumptions made; it cannot correct assumptions that are wrong. The example is a useful reminder that a posterior distribution describes uncertainty within a model, and that the model itself deserves scrutiny.

Exercises

Exercise 7.1

For the Michelson data under the first prior above, compute κn\kappa_n, μn\mu_n, νn\nu_n and σn2\sigma_n^2. Hence give a 95% central posterior interval for θ\theta and a 95% predictive interval for a new measurement.

Show solutionHide solution

With κ0=1\kappa_0=1, μ0=1000\mu_0=1000, ν0=1\nu_0=1, σ0=1\sigma_0=1, n=100n=100, yˉ=852.4\bar y=852.4 and s=79.01s=79.01: κn=101,μn=1000+85 240101≈853.86,νn=101,νnσn2=1+99×79.012+100101(852.4−1000)2≈1+618 016+21 570=639 587,\begin{gathered} \kappa_n=101, \qquad \mu_n=\frac{1000+85\,240}{101}\approx853.86, \qquad \nu_n=101,\\ \nu_n\sigma_n^2 = 1+99\times79.01^2+\frac{100}{101}(852.4-1000)^2 \approx 1+618\,016+21\,570 = 639\,587, \end{gathered} so σn2≈6332.5\sigma_n^2\approx6332.5 and σn≈79.58\sigma_n\approx79.58. The posterior scale for θ\theta is σn/κn≈7.92\sigma_n/\sqrt{\kappa_n}\approx7.92, and t101,0.975≈1.984t_{101,0.975}\approx1.984. The 95% posterior interval for θ\theta is 853.86±1.984×7.92≈(838.2,  869.6).853.86\pm1.984\times7.92 \approx (838.2,\;869.6). For a new measurement the scale is σn1+1/101≈79.97\sigma_n\sqrt{1+1/101}\approx79.97, giving (695.2,  1012.5)(695.2,\;1012.5). The accepted value 734.5734.5 lies well outside the interval for θ\theta and inside the predictive interval: a single measurement near the true value would not be unusual, but the average of Michelson’s measurements is far from it.

Exercise 7.2

In the model with σ2\sigma^2 known, show that updating the prior with y1y_1, and then updating the resulting posterior with y2y_2, gives the same result as updating the prior with (y1,y2)(y_1,y_2) at once.

Show solutionHide solution

After y1y_1 the posterior has precision 1/τ02+1/σ21/\tau_0^2+1/\sigma^2 and mean (μ0/τ02+y1/σ2)/(1/τ02+1/σ2)(\mu_0/\tau_0^2+y_1/\sigma^2)/(1/\tau_0^2+1/\sigma^2). Using this as the prior for y2y_2 gives precision 1/τ02+2/σ21/\tau_0^2+2/\sigma^2 and mean μ0/τ02+y1/σ2+y2/σ21/τ02+2/σ2=μ0/τ02+2yˉ/σ21/τ02+2/σ2,\frac{\mu_0/\tau_0^2+y_1/\sigma^2+y_2/\sigma^2}{1/\tau_0^2+2/\sigma^2} = \frac{\mu_0/\tau_0^2+2\bar y/\sigma^2}{1/\tau_0^2+2/\sigma^2}, which is the posterior from (y1,y2)(y_1,y_2) with n=2n=2. The result holds for any model with conditionally independent observations, since p(θ∣y1,y2)∝p(y2∣θ) p(y1∣θ) p(θ)∝p(y2∣θ) p(θ∣y1)p(\theta\mid y_1,y_2)\propto p(y_2\mid\theta)\,p(y_1\mid\theta)\,p(\theta)\propto p(y_2\mid\theta)\,p(\theta\mid y_1).

8 Monte Carlo methods

Reference: GCSR §1.9; Hoff Chapter 4.

We have already used simulation several times: to compute posterior intervals, to find the posterior of 1/θ1/\theta, and to draw from predictive distributions. This chapter explains why these methods work and how accurate they are. The ideas are simple, but they are the foundation of nearly all modern Bayesian computation, since in realistic models the integrals required by Bayes’ theorem can almost never be evaluated exactly.

8.1 The idea

Suppose we want to know a number XX, and that computing it exactly is impossible, complicated or slow, while a good approximation would serve our purpose. If XX can be written as the expectation of some random variable, X=E⁡[ψ]X=\operatorname{E}[\psi], and if we can draw samples from the distribution of ψ\psi, then we can estimate XX by the sample mean X^=ψˉ=1L∑j=1Lψj,\hat X = \bar\psi = \frac1L\sum_{j=1}^L\psi_j, where ψ1,…,ψL\psi_1,\ldots,\psi_L are independent draws. This is the Monte Carlo method, named after the casino.

Two familiar results justify it. By the law of large numbers, ψˉ→E⁡[ψ]\bar\psi\to\operatorname{E}[\psi] as L→∞L\to\infty, so the estimate can be made as accurate as we like by taking enough draws. And by the central limit theorem, if Var⁡[ψ]\operatorname{Var}[\psi] is finite then, for large LL, ψˉ  ∼˙  N⁡(E⁡[ψ],  Var⁡[ψ]L).\bar\psi\;\dot\sim\;\operatorname{N}\left(\operatorname{E}[\psi],\;\frac{\operatorname{Var}[\psi]}L\right). This tells us how accurate the estimate is. Its standard deviation, the Monte Carlo standard error, is Var⁡[ψ]/L\sqrt{\operatorname{Var}[\psi]/L}, which we estimate by sψ/Ls_\psi/\sqrt L, where sψs_\psi is the standard deviation of the draws. An approximate 95% interval for XX is ψˉ±2sψ/L\bar\psi\pm2s_\psi/\sqrt L.

A simple illustration is the estimation of an area. Suppose a region AA of the plane, perhaps the union of several overlapping circles as on the slides, is awkward to measure, but lies inside a rectangle RR whose area we know. Draw points independently and uniformly at random in RR, and let ψj=1\psi_j=1 if the jjth point falls in AA and ψj=0\psi_j=0 otherwise. Then E⁡[ψ]=area⁡(A)/area⁡(R)\operatorname{E}[\psi]=\operatorname{area}(A)/\operatorname{area}(R), so the proportion of points falling in AA, multiplied by the area of RR, estimates the area of AA. All we need is a way to tell whether a given point lies in AA.

8.2 Simulation in Bayesian inference

Now consider a parameter vector θ=(θ1,…,θk)\boldsymbol\theta=(\theta_1,\ldots,\theta_k) with joint posterior p(θ∣y)p(\boldsymbol\theta\mid y). We are often interested in only one component, and so need its marginal posterior, p(θi∣y)=∫p(θ∣y) dθ1⋯ dθi−1 dθi+1⋯ dθk.p(\theta_i\mid y) = \int p(\boldsymbol\theta\mid y)\,\mathrm{d}\theta_1\cdots\,\mathrm{d}\theta_{i-1}\,\mathrm{d}\theta_{i+1}\cdots\,\mathrm{d}\theta_k. For most models this integral has no closed form, and for all but small kk even numerical integration is difficult, because the number of points needed on a grid grows exponentially with the dimension.

Suppose instead that we can draw a sample θ(1),θ(2),…,θ(L)\boldsymbol\theta^{(1)},\boldsymbol\theta^{(2)},\ldots,\boldsymbol\theta^{(L)} from the joint posterior. Then the iith components of these draws, θi(1),…,θi(L)\theta_i^{(1)},\ldots,\theta_i^{(L)}, form a sample from the marginal posterior p(θi∣y)p(\theta_i\mid y). This follows directly from what a marginal distribution is: the distribution of one coordinate of the random vector, whatever the others happen to be. So to marginalise a sample we simply ignore the coordinates we are not interested in. No integration is required. We used exactly this device for the nuisance parameter σ2\sigma^2 in the previous chapter.

From the sample we can estimate any summary of the marginal posterior:

  • the posterior mean E⁡[θi∣y]\operatorname{E}[\theta_i\mid y], by the sample mean θˉi=1L∑jθi(j)\bar\theta_i=\frac1L\sum_j\theta_i^{(j)};

  • the posterior median, the value mm such that P(θi<m∣y)=0.5P(\theta_i<m\mid y)=0.5, by the sample median of the draws;

  • a 95% central posterior interval (a,b)(a,b), with P(a<θi<b∣y)=0.95P(a<\theta_i<b\mid y)=0.95, by the 0.0250.025 and 0.9750.975 sample quantiles of the draws. With L=10 000L=10\,000, these are approximately the 250th and 9751st values when the draws are sorted into increasing order.

Each of these is itself a Monte Carlo estimate, with an error that decreases as LL increases.

8.3 Functions of parameters

Often the quantity of interest is not a parameter itself but some function of the parameters, ψ=g(θ)\psi=g(\boldsymbol\theta). The mean remission time 1/θ1/\theta was one example. Others are the difference between two rates, the ratio of a standard deviation to a mean, or the probability that one treatment is better than another. The formulation is very general. It includes ψ=θi\psi=\theta_i, and also indicator functions: if ψ=IA(θ)={1θ∈A,0θ∉A,\psi = I_A(\boldsymbol\theta) = \begin{cases}1 & \boldsymbol\theta\in A,\\ 0 & \boldsymbol\theta\notin A,\end{cases} then E⁡[ψ∣y]=P(θ∈A∣y)\operatorname{E}[\psi\mid y]=P(\boldsymbol\theta\in A\mid y), so posterior probabilities of events are also posterior expectations.

To find p(ψ∣y)p(\psi\mid y) analytically, one would transform from (θ1,θ2,…,θk)(\theta_1,\theta_2,\ldots,\theta_k) to new variables (ψ,θ2,…,θk)(\psi,\theta_2,\ldots,\theta_k), find their joint density using the Jacobian of the transformation, and integrate out θ2,…,θk\theta_2,\ldots,\theta_k. This is laborious even when it is possible.

By simulation it is almost trivial. Given draws θ(1),…,θ(L)\boldsymbol\theta^{(1)},\ldots,\boldsymbol\theta^{(L)} from p(θ∣y)p(\boldsymbol\theta\mid y), compute ψ(1)=g(θ(1)),ψ(2)=g(θ(2)),…,ψ(L)=g(θ(L)).\psi^{(1)} = g(\boldsymbol\theta^{(1)}),\quad \psi^{(2)} = g(\boldsymbol\theta^{(2)}),\quad\ldots,\quad \psi^{(L)} = g(\boldsymbol\theta^{(L)}). These are draws from p(ψ∣y)p(\psi\mid y), because each ψ(j)\psi^{(j)} is the value of g(θ)g(\boldsymbol\theta) at a random θ\boldsymbol\theta drawn from the posterior, which is exactly what it means for ψ\psi to have the distribution p(ψ∣y)p(\psi\mid y). Summaries of p(ψ∣y)p(\psi\mid y) are then estimated from the ψ(j)\psi^{(j)} as above.

8.4 An example

The following example, in which the exact answer is known, lets us check the method. Suppose θ1,…,θ20\theta_1,\ldots,\theta_{20} are independent Unif⁡(0,1)\operatorname{Unif}(0,1) random variables, and let ψ=g(θ)=−∑i=120log⁡θi.\psi = g(\boldsymbol\theta) = -\sum_{i=1}^{20}\log\theta_i. We draw L=10 000L=10\,000 samples of ψ\psi and use them to estimate E⁡[ψ]\operatorname{E}[\psi], the standard deviation of ψ\psi, P(ψ>30)P(\psi>30) and the interquartile range of ψ\psi. In R, we generate the θ\theta draws as a 10 000×2010\,000\times20 matrix, one row per draw of θ\boldsymbol\theta, and apply gg to each row:

theta.samp <- matrix(runif(10000 * 20, 0, 1), nrow = 10000)
psi.samp <- -rowSums(log(theta.samp))
mean(psi.samp)        # 20.01562
sd(psi.samp)          # 4.464726
mean(psi.samp > 30)   # 0.0204
IQR(psi.samp)         # 5.992474

Note that the estimate of P(ψ>30)P(\psi>30) is simply the proportion of draws exceeding 30, the sample mean of the indicator I(ψ>30)I(\psi>30).

In this example simulation is not really needed, since one can show that ψ∼Ga⁡(20,1)\psi\sim\operatorname{Ga}(20,1) (Exercise 8.1). The exact values of the four summaries are 2020, 4.4724.472, 0.021870.02187 and 5.9785.978, and the simulation estimates are close to all of them. A histogram of the draws with the Ga⁡(20,1)\operatorname{Ga}(20,1) density superimposed, shown on the slides, confirms the agreement.

If the estimates are not accurate enough, the remedy is to increase LL. Since Var⁡[ψˉ]=Var⁡[ψ]/L\operatorname{Var}[\bar\psi]=\operatorname{Var}[\psi]/L, the standard error decreases in proportion to 1/L1/\sqrt L, so halving it requires four times as many draws. Precision is bought slowly. On the other hand, it is entirely under our control, which the sample size of the data usually is not, and with modern computers LL in the tens or hundreds of thousands is seldom a burden.

Exercises

Exercise 8.1

Show that if U∼Unif⁡(0,1)U\sim\operatorname{Unif}(0,1) then −log⁡U∼Exp⁡(1)-\log U\sim\operatorname{Exp}(1). Deduce that ψ\psi in the example above has a Ga⁡(20,1)\operatorname{Ga}(20,1) distribution.

Show solutionHide solution

For w>0w>0, P(−log⁡U≤w)=P(U≥e−w)=1−e−wP(-\log U\le w)=P(U\ge e^{-w})=1-e^{-w}, the Exp⁡(1)\operatorname{Exp}(1) distribution function. So ψ\psi is a sum of 2020 independent Exp⁡(1)\operatorname{Exp}(1) variables, that is, of Ga⁡(1,1)\operatorname{Ga}(1,1) variables with a common rate. By the result of Exercise 2.2, applied repeatedly, sums of independent Gamma variables with a common rate are Gamma, with the shapes added. Hence ψ∼Ga⁡(20,1)\psi\sim\operatorname{Ga}(20,1).

Exercise 8.2

In the example above, find the Monte Carlo standard error of the estimate of P(ψ>30)P(\psi>30) when L=10 000L=10\,000. How large must LL be for the standard error to be below 0.00050.0005?

Show solutionHide solution

The estimate is a mean of LL Bernoulli(q)(q) variables with q≈0.022q\approx0.022. Its standard error is q(1−q)/L≈0.0215/10 000≈0.0015\sqrt{q(1-q)/L}\approx\sqrt{0.0215/10\,000}\approx0.0015. For a standard error below 0.00050.0005 we need L>q(1−q)/0.00052≈86 000L>q(1-q)/0.0005^2\approx86\,000.

9 Markov chain Monte Carlo

The methods of Chapter 8 require independent draws from the posterior distribution. For the conjugate models we have studied these are easy to obtain, because the posterior is a standard distribution for which sampling routines exist. For most other models they are not. The posterior is known only up to its normalising constant, as the product of prior and likelihood, and it is not of any recognisable form.

Markov chain Monte Carlo (MCMC) methods overcome this difficulty. Rather than drawing independent samples, they construct a sequence of dependent draws, each generated from the one before, in such a way that the distribution of the draws approaches the posterior as the sequence continues. Two features make them so widely useful. They require the posterior density only up to a constant, which is exactly what Bayes’ theorem provides without further work. And the individual steps are simple, usually involving only low-dimensional distributions, even when the posterior itself is high-dimensional. Once we have a long enough sequence of draws, we use it just as we used independent draws in Chapter 8.

9.1 Markov chains and stationary distributions

We recall a few facts from the theory of Markov chains, which you will have met in Applied Probability. A sequence of random variables X(0),X(1),X(2),…X^{(0)},X^{(1)},X^{(2)},\ldots is a Markov chain if the distribution of each X(k)X^{(k)}, given all the earlier values, depends only on the immediately preceding value X(k−1)X^{(k-1)}. The chain is described by its transition kernel P(y∣x)P(y\mid x), the probability of moving to state yy from state xx. (For continuous states, P(y∣x)P(y\mid x) is a density in yy.)

A distribution pp is stationary for the chain if, whenever X(k−1)X^{(k-1)} has distribution pp, so does X(k)X^{(k)}: ∑xP(y∣x) p(x)=p(y)for all y.\sum_xP(y\mid x)\,p(x) = p(y) \qquad\text{for all } y. Under mild conditions (roughly, that the chain can reach every region of the state space and does not cycle periodically), a chain with stationary distribution pp converges to it: whatever the starting value, the distribution of X(k)X^{(k)} approaches pp as k→∞k\to\infty, and averages along the chain converge to expectations under pp. So if we can design a chain whose stationary distribution is the posterior, and run it for long enough, its later values can be treated as draws from the posterior.

The following condition is the usual tool for checking that a given distribution is stationary.

Theorem 9.1 (Detailed balance). If a distribution pp and a transition kernel PP satisfy P(y∣x) p(x)=P(x∣y) p(y)for all x,y,P(y\mid x)\,p(x) = P(x\mid y)\,p(y)\qquad\text{for all }x,y, then pp is a stationary distribution of the chain with kernel PP.

Proof. Sum both sides over xx. The left-hand side becomes ∑xP(y∣x)p(x)\sum_xP(y\mid x)p(x). The right-hand side becomes p(y)∑xP(x∣y)=p(y)p(y)\sum_xP(x\mid y)=p(y), since the transition probabilities out of yy sum to one. ◻

One way to picture detailed balance is to imagine a large population of walkers distributed across the states according to pp, each moving according to PP. The left-hand side is the rate at which walkers flow from xx to yy, and the right-hand side the rate from yy to xx. If these flows balance for every pair of states, the overall distribution of walkers does not change. Detailed balance is sufficient for stationarity but not necessary; a chain can have pp as its stationary distribution without satisfying it, as we shall see.

9.2 The Gibbs sampler

Let p(θ1,…,θd)p(\theta_1,\ldots,\theta_d) be the distribution we wish to sample from, the target. In our applications it is a posterior, but the method does not depend on this. The Gibbs sampler proceeds as follows.

Gibbs sampler. Choose a starting point (θ1(0),…,θd(0))(\theta_1^{(0)},\ldots,\theta_d^{(0)}). For k=1,2,…,Kk=1,2,\ldots,K, and for i=1,…,di=1,\ldots,d in turn, draw θi(k)∼p(θi∣θ1(k),…,θi−1(k),θi+1(k−1),…,θd(k−1)).\theta_i^{(k)}\sim p\big(\theta_i\mid\theta_1^{(k)},\ldots,\theta_{i-1}^{(k)},\theta_{i+1}^{(k-1)},\ldots,\theta_d^{(k-1)}\big).

In words, we cycle through the components, and update each by drawing a new value from its full conditional distribution, its distribution given the current values of all the other components. Components already updated in the current sweep are conditioned on at their new values, and the rest at their old values. The claim is that the vectors θ(k)=(θ1(k),…,θd(k))\boldsymbol\theta^{(k)}=(\theta_1^{(k)},\ldots,\theta_d^{(k)}), for k=1,…,Kk=1,\ldots,K, form a Markov chain whose stationary distribution is pp. They are therefore, after an initial period, correlated draws from pp.

The method is useful because full conditional distributions are often easy to sample from even when the joint distribution is not. The full conditional of θi\theta_i is proportional to the joint density regarded as a function of θi\theta_i alone, with the other components held fixed, and in many models (particularly those built from conjugate pieces) it turns out to be a standard distribution. We shall see an example in the hierarchical model of Chapter 11, and another in Exercise 9.2.

9.2.1 Why the Gibbs sampler works

We give the argument for a discrete state space. The continuous case is the same, with sums replaced by integrals.

Define dd equivalence relations on the set of states. For each ii, say that x∼iyx\sim_iy if xj=yjx_j=y_j for all j≠ij\ne i: that is, xx and yy agree in every coordinate except possibly the iith. The equivalence class of xx under ∼i\sim_i is the set of states reachable from xx by changing its iith coordinate alone. Let Zi(x)=∑z∼ixp(z)Z_i(x)=\sum_{z\sim_ix}p(z) be the total probability of that class.

Consider a single update of coordinate ii. It moves from state xx to state yy with probability Pi(y∣x)={p(y)/Zi(x)if y∼ix,0otherwise.P_i(y\mid x) = \begin{cases} p(y)/Z_i(x) & \text{if } y\sim_ix,\\ 0 & \text{otherwise.}\end{cases} This is exactly a draw from the full conditional of θi\theta_i: the chain can move only within the equivalence class of its current state, and it chooses a state in that class with probability proportional to pp. The division by Zi(x)Z_i(x) makes the probabilities sum to one over the class.

We now check detailed balance for PiP_i. If y̸∼ixy\not\sim_ix then also x̸∼iyx\not\sim_iy, because an equivalence relation is symmetric, and both sides of the detailed balance equation are zero. If y∼ixy\sim_ix, then xx and yy lie in the same equivalence class, and so the class totals agree: Zi(x)=Zi(y)Z_i(x)=Z_i(y). Hence Pi(y∣x) p(x)=p(y) p(x)Zi(x)=p(x) p(y)Zi(y)=Pi(x∣y) p(y).P_i(y\mid x)\,p(x) = \frac{p(y)\,p(x)}{Z_i(x)} = \frac{p(x)\,p(y)}{Z_i(y)} = P_i(x\mid y)\,p(y). So each single-coordinate update satisfies detailed balance with respect to pp, and therefore leaves pp stationary.

One complete sweep of the Gibbs sampler applies the updates P1,P2,…,PdP_1,P_2,\ldots,P_d in turn. If the state at the start of the sweep has distribution pp, then after applying P1P_1 it still has distribution pp; after P2P_2 it still does; and so on to the end of the sweep. So pp is stationary for the sweep as a whole. It is worth noticing that the complete sweep does not in general satisfy detailed balance, since running the updates in reverse order is not the same as running them forwards. This does not matter, since detailed balance is only a sufficient condition and we have established stationarity directly.

An alternative is the random-scan Gibbs sampler, which at each step chooses one coordinate ii at random, with probability 1/d1/d, and updates only that coordinate. Its transition kernel is the average 1d∑iPi\frac1d\sum_iP_i, and since each PiP_i satisfies detailed balance, so does the average. The version on the lecture slides is of this kind, with the factor 1/d1/d absorbed into the normalising constants.

9.2.2 Using the output

The draws from a Gibbs sampler, or from any MCMC method, differ from the independent draws of Chapter 8 in two respects, and both need attention in practice.

First, the chain starts from an arbitrary point, which may lie in a region of low posterior probability, and it takes some time to reach the region where the posterior is concentrated. The early draws are therefore not representative of the posterior, and are usually discarded as burn-in. A plot of the draws against the iteration number, called a trace plot, is the simplest way to judge how long this takes: after burn-in the trace should wander around a stable level with no visible trend. Running several chains from widely separated starting points, and checking that they settle in the same place, is a more thorough check.

Second, successive draws are correlated, because each is generated from the one before. The law of large numbers still holds, so averages along the chain still converge to posterior expectations, but a correlated sample carries less information than an independent sample of the same size, and the Monte Carlo error is larger. The Gibbs sampler moves slowly when components of θ\boldsymbol\theta are strongly correlated in the target, since each update changes one coordinate while holding the others fixed, and so can take only small steps along a narrow ridge of high probability. Exercise 9.1 makes this precise for a bivariate normal target. When this happens, the chain must be run for much longer, or the model reparameterised to reduce the correlation.

The Gibbs sampler takes its name from the physicist Josiah Willard Gibbs, through its connection with the distributions of statistical mechanics. It was introduced to statistics by the brothers Stuart and Donald Geman in 1984, in work on the restoration of noisy images, and was brought to the attention of Bayesian statisticians generally by Gelfand and Smith in 1990.

9.3 The Metropolis–Hastings algorithm

The Gibbs sampler requires us to draw from every full conditional distribution. When some full conditional is not a standard distribution, we need another way to generate a move. The Metropolis–Hastings algorithm provides one. It requires only that we can evaluate the target density up to a constant, and that we can draw from some convenient proposal distribution of our choosing.

Let π\pi be the target density, known up to a constant, and let g(θ′∣θ)g(\theta'\mid\theta) be a proposal density, which suggests a new value θ′\theta' given the current value θ\theta.

Metropolis–Hastings. Choose θ(0)\theta^{(0)}. For k=1,2,…,Kk=1,2,\ldots,K:

  1. draw a proposal θ′\theta' from g(θ′∣θ(k−1))g(\theta'\mid\theta^{(k-1)});

  2. compute the acceptance probability α=min⁡(1,  g(θ(k−1)∣θ′)g(θ′∣θ(k−1))⋅π(θ′)π(θ(k−1)));\alpha = \min\left(1,\;\frac{g(\theta^{(k-1)}\mid\theta')}{g(\theta'\mid\theta^{(k-1)})}\cdot\frac{\pi(\theta')}{\pi(\theta^{(k-1)})}\right);

  3. with probability α\alpha accept the proposal and set θ(k)=θ′\theta^{(k)}=\theta'; otherwise reject it and set θ(k)=θ(k−1)\theta^{(k)}=\theta^{(k-1)}.

The target enters only through the ratio π(θ′)/π(θ(k−1))\pi(\theta')/\pi(\theta^{(k-1)}), in which any normalising constant cancels. This is the property that makes the algorithm so useful for Bayesian computation: we can take π(θ)=p(y∣θ) p(θ)\pi(\theta)=p(y\mid\theta)\,p(\theta), the unnormalised posterior, and never compute the marginal likelihood.

The rule has an intuitive reading, most easily seen when the proposal is symmetric, g(θ′∣θ)=g(θ∣θ′)g(\theta'\mid\theta)=g(\theta\mid\theta'). The ratio of proposal densities is then one, and α=min⁡(1,π(θ′)/π(θ))\alpha=\min(1,\pi(\theta')/\pi(\theta)). A proposal to move to a point of higher posterior density is always accepted. A proposal to move to a point of lower density is accepted with probability equal to the ratio of the densities, so that downhill moves are possible but become less likely the further downhill they go. The chain thus spends most of its time where the posterior density is high, but still visits regions of lower density in the correct proportion. This special case, with a symmetric proposal, is the original algorithm of Metropolis and his colleagues. A common choice is the random-walk proposal θ′=θ+ε\theta'=\theta+\varepsilon, with ε∼N⁡(0,c2)\varepsilon\sim\operatorname{N}(0,c^2). The ratio g(θ∣θ′)/g(θ′∣θ)g(\theta\mid\theta')/g(\theta'\mid\theta) in the general algorithm, introduced by Hastings in 1970, corrects for any asymmetry in the proposal, so that a proposal which tends to suggest moves in one direction does not bias the chain in that direction.

9.3.1 Why Metropolis–Hastings works

Detailed balance is again the tool. For θ′≠θ\theta'\ne\theta, the chain moves from θ\theta to θ′\theta' only by proposing θ′\theta' and then accepting it, so its transition density is P(θ′∣θ)=g(θ′∣θ) A(θ′,θ),P(\theta'\mid\theta) = g(\theta'\mid\theta)\,A(\theta',\theta), where A(θ′,θ)A(\theta',\theta) denotes the probability of accepting a proposed move from θ\theta to θ′\theta'. Substituting into the detailed balance equation, P(θ′∣θ)π(θ)=P(θ∣θ′)π(θ′)P(\theta'\mid\theta)\pi(\theta)=P(\theta\mid\theta')\pi(\theta'), we require g(θ′∣θ) A(θ′,θ) π(θ)=g(θ∣θ′) A(θ,θ′) π(θ′),g(\theta'\mid\theta)\,A(\theta',\theta)\,\pi(\theta) = g(\theta\mid\theta')\,A(\theta,\theta')\,\pi(\theta'), or equivalently A(θ′,θ)A(θ,θ′)=g(θ∣θ′) π(θ′)g(θ′∣θ) π(θ)=:r.\frac{A(\theta',\theta)}{A(\theta,\theta')} = \frac{g(\theta\mid\theta')\,\pi(\theta')}{g(\theta'\mid\theta)\,\pi(\theta)} =: r. Any acceptance rule satisfying this condition gives a chain with stationary distribution π\pi. The Metropolis–Hastings choice is A(θ′,θ)=min⁡(1,r)A(\theta',\theta)=\min(1,r). For the reverse move the ratio is 1/r1/r, so A(θ,θ′)=min⁡(1,1/r)A(\theta,\theta')=\min(1,1/r). If r≥1r\ge1, the ratio of the two acceptance probabilities is 1/(1/r)=r1/(1/r)=r; if r<1r<1, it is r/1=rr/1=r. In either case the condition holds. Moves in which the chain stays where it is (because a proposal was rejected) satisfy detailed balance trivially, since both sides involve the same state.

Other acceptance rules also satisfy the condition, but the Metropolis–Hastings rule accepts proposals as often as possible, since one of A(θ′,θ)A(\theta',\theta) and A(θ,θ′)A(\theta,\theta') is always equal to one. Frequent acceptance means the chain moves more and explores the target more quickly.

9.3.2 Choosing the proposal

The algorithm is correct for almost any proposal, but its efficiency depends strongly on the choice. With a random-walk proposal, the scale cc matters. If cc is very small, nearly every proposal is accepted, but each moves the chain only a short distance, and it explores the target slowly. If cc is very large, most proposals land in regions of low density and are rejected, and the chain stays in one place for long periods. Either way successive draws are highly correlated. A useful rule of thumb for random-walk proposals is to adjust cc until roughly a quarter to a half of proposals are accepted, and trace plots help to judge whether the chain is moving well.

The algorithm was published in 1953 by Nicholas Metropolis, Arianna and Marshall Rosenbluth, and Augusta and Edward Teller, in a paper on the simulation of the physical properties of materials. Historical accounts now credit much of the work to the Rosenbluths.

9.3.3 Metropolis within Gibbs

The two methods combine naturally. In a Gibbs sampler, a full conditional that cannot be sampled directly may be replaced by a single Metropolis–Hastings step that has that full conditional as its target. Since the Metropolis–Hastings step leaves its target stationary, it leaves the joint distribution stationary in the same way that an exact Gibbs update does, and the argument of the previous section goes through unchanged. This is called Metropolis-within-Gibbs, and it is how the hierarchical model of Chapter 11 is fitted in practice.

Exercises

Exercise 9.1

Let (θ1,θ2)(\theta_1,\theta_2) have a bivariate normal distribution with zero means, unit variances and correlation ρ\rho.

  1. Write down the full conditional distributions.

  2. Write R code for a Gibbs sampler.

  3. Show that, when the chain is stationary, successive draws of θ1\theta_1 have correlation ρ2\rho^2. What happens as ρ→1\rho\to1?

Show solutionHide solution

(a) θ1∣θ2∼N⁡(ρθ2, 1−ρ2)\theta_1\mid\theta_2\sim\operatorname{N}(\rho\theta_2,\,1-\rho^2) and θ2∣θ1∼N⁡(ρθ1, 1−ρ2)\theta_2\mid\theta_1\sim\operatorname{N}(\rho\theta_1,\,1-\rho^2).

(b)

gibbs.bvn <- function(K, rho, start = c(0, 0)) {
  out <- matrix(NA, K, 2)
  th <- start
  for (k in 1:K) {
    th[1] <- rnorm(1, rho * th[2], sqrt(1 - rho^2))
    th[2] <- rnorm(1, rho * th[1], sqrt(1 - rho^2))
    out[k, ] <- th
  }
  out
}
draws <- gibbs.bvn(5000, rho = 0.9)
plot(draws[, 1], type = "l")   # trace plot

(c) Given θ1(k−1)\theta_1^{(k-1)}, the next θ2\theta_2 has mean ρθ1(k−1)\rho\theta_1^{(k-1)}, and the next θ1\theta_1 has mean ρ\rho times that. So E⁡[θ1(k)∣θ1(k−1)]=ρ2θ1(k−1)\operatorname{E}[\theta_1^{(k)}\mid\theta_1^{(k-1)}]=\rho^2\theta_1^{(k-1)}. At stationarity both have variance one, so the correlation is Cov⁡(θ1(k),θ1(k−1))=E⁡[θ1(k−1)E⁡[θ1(k)∣θ1(k−1)]]=ρ2\operatorname{Cov}(\theta_1^{(k)},\theta_1^{(k-1)})=\operatorname{E}[\theta_1^{(k-1)}\operatorname{E}[\theta_1^{(k)}\mid\theta_1^{(k-1)}]]=\rho^2. As ρ→1\rho\to1 successive draws become nearly identical and the chain explores the distribution very slowly. Many more draws are needed for a given Monte Carlo precision.

Exercise 9.2

Consider the normal model with the semi-conjugate prior: Y1,…,Yn∣θ,σ2∼iidN⁡(θ,σ2)Y_1,\ldots,Y_n\mid\theta,\sigma^2\stackrel{\text{iid}}{\sim}\operatorname{N}(\theta,\sigma^2), with θ∼N⁡(μ0,τ02)\theta\sim\operatorname{N}(\mu_0,\tau_0^2) and σ2∼Inv-Ga⁡(ν0/2,ν0σ02/2)\sigma^2\sim\operatorname{Inv\text{-}Ga}(\nu_0/2,\nu_0\sigma_0^2/2) independent a priori. Find the full conditional distributions of θ\theta and σ2\sigma^2, and describe a Gibbs sampler for the joint posterior.

Show solutionHide solution

Given σ2\sigma^2, the model for θ\theta is the known-variance model of Section 7.2. So θ∣σ2,y∼N⁡(μn,τn2),1τn2=1τ02+nσ2,μn=τn2(μ0τ02+nyˉσ2).\theta\mid\sigma^2,y\sim\operatorname{N}(\mu_n,\tau_n^2), \qquad \frac1{\tau_n^2}=\frac1{\tau_0^2}+\frac n{\sigma^2}, \qquad \mu_n=\tau_n^2\left(\frac{\mu_0}{\tau_0^2}+\frac{n\bar y}{\sigma^2}\right). Given θ\theta, the model for σ2\sigma^2 is the known-mean model. So σ2∣θ,y∼Inv-Ga⁡(ν0+n2,  ν0σ02+∑i(yi−θ)22).\sigma^2\mid\theta,y\sim\operatorname{Inv\text{-}Ga}\left(\frac{\nu_0+n}2,\;\frac{\nu_0\sigma_0^2+\sum_i(y_i-\theta)^2}2\right). The Gibbs sampler starts from some σ2(0)\sigma^{2(0)}, perhaps s2s^2, and alternates: draw θ(k)\theta^{(k)} from the first distribution with σ2=σ2(k−1)\sigma^2=\sigma^{2(k-1)}; then draw σ2(k)\sigma^{2(k)} from the second with θ=θ(k)\theta=\theta^{(k)}. Note that μn\mu_n and τn2\tau_n^2 must be recomputed at every iteration, since they depend on the current σ2\sigma^2. This is Hoff’s example in §6.3.

10 More on priors

So far we have chosen priors largely for convenience, usually from a conjugate family, and have interpreted their parameters as the information in an imaginary earlier sample. This chapter looks more closely at the choice of prior. We first show that conjugate priors exist for a whole class of models and that the ‘imaginary data’ interpretation holds throughout that class. We then turn to the harder question of what prior to use when we have, or wish to claim, little prior information.

10.1 Exponential families and conjugate priors

Reference: Hoff §3.3.

A one-parameter exponential family is a model whose density or mass function can be written in the form p(y∣ϕ)=h(y) c(ϕ)exp⁡{ϕ t(y)}.p(y\mid\phi) = h(y)\,c(\phi)\exp\{\phi\,t(y)\}. Here ϕ\phi is called the natural parameter and t(y)t(y) the sufficient statistic. The essential feature is that the data and the parameter interact only through the product ϕ t(y)\phi\,t(y) in the exponent. Many standard models have this form once the parameter is suitably transformed.

Example 10.1 (Bernoulli). p(y∣θ)=θy(1−θ)1−y=(1−θ)(θ1−θ)y=(1−θ)exp⁡{ylog⁡θ1−θ}.p(y\mid\theta) = \theta^y(1-\theta)^{1-y} = (1-\theta)\left(\frac\theta{1-\theta}\right)^y = (1-\theta)\exp\left\{y\log\frac\theta{1-\theta}\right\}. So h(y)=1h(y)=1, t(y)=yt(y)=y, and the natural parameter is the log-odds ϕ=log⁡{θ/(1−θ)}\phi=\log\{\theta/(1-\theta)\}. Inverting, θ=eϕ/(1+eϕ)\theta=e^\phi/(1+e^\phi) and 1−θ=(1+eϕ)−11-\theta=(1+e^\phi)^{-1}, so c(ϕ)=(1+eϕ)−1c(\phi)=(1+e^\phi)^{-1}.

Example 10.2 (Poisson). p(y∣θ)=θyy!e−θ=1y!exp⁡{−elog⁡θ}exp⁡{ylog⁡θ}.p(y\mid\theta) = \frac{\theta^y}{y!}e^{-\theta} = \frac1{y!}\exp\{-e^{\log\theta}\}\exp\{y\log\theta\}. So h(y)=1/y!h(y)=1/y!, t(y)=yt(y)=y, ϕ=log⁡θ\phi=\log\theta and c(ϕ)=exp⁡{−eϕ}c(\phi)=\exp\{-e^\phi\}.

The exponential model, and the normal model with either the mean or the variance known, can be written in the same form (see Exercise 10.3 for the first of these).

10.1.1 The conjugate prior

For any one-parameter exponential family, consider a prior for the natural parameter of the form p(ϕ)∝c(ϕ)n0exp⁡{n0t0ϕ},p(\phi)\propto c(\phi)^{n_0}\exp\{n_0t_0\phi\}, where n0n_0 and t0t_0 are constants. After observing y1,…,yny_1,\ldots,y_n independently from the model, the posterior is p(ϕ∣y)∝c(ϕ)n0en0t0ϕ∏i=1n[h(yi) c(ϕ) eϕt(yi)]∝c(ϕ)n0+nexp⁡{(n0t0+ntˉ)ϕ},p(\phi\mid y)\propto c(\phi)^{n_0}e^{n_0t_0\phi}\prod_{i=1}^n\Big[h(y_i)\,c(\phi)\,e^{\phi t(y_i)}\Big] \propto c(\phi)^{n_0+n}\exp\{(n_0t_0+n\bar t)\phi\}, where tˉ=1n∑it(yi)\bar t=\frac1n\sum_it(y_i) is the average of the sufficient statistics. The posterior has the same form as the prior, with new constants, so this family of priors is conjugate. The updating rule is:

  • n0n_0 becomes n0+nn_0+n;

  • t0t_0 becomes (n0t0+ntˉ)/(n0+n)(n_0t_0+n\bar t)/(n_0+n), a weighted average of t0t_0 and tˉ\bar t with weights n0n_0 and nn.

The interpretation we have met in each particular model now appears in general. The prior carries the same information as n0n_0 imaginary observations whose sufficient statistics average t0t_0. This gives a practical recipe for choosing a prior: set t0t_0 to your best prior guess for the average value of t(Y)t(Y), and use n0n_0 to express how confident you are in that guess, in units of observations. A small value, say n0≤1n_0\le1, gives a vague prior whose influence on the posterior will soon be swamped by the data.

To see that this reproduces the conjugate priors we already know, we transform back from ϕ\phi to the original parameter θ\theta, remembering to include the Jacobian.

Example 10.3 (Bernoulli). The conjugate prior is p(ϕ)∝(1+eϕ)−n0en0t0ϕp(\phi)\propto(1+e^\phi)^{-n_0}e^{n_0t_0\phi}. With ϕ=log⁡{θ/(1−θ)}\phi=\log\{\theta/(1-\theta)\} we have eϕ=θ/(1−θ)e^\phi=\theta/(1-\theta) and  dϕ/ dθ=1/{θ(1−θ)}\,\mathrm{d}\phi/\,\mathrm{d}\theta=1/\{\theta(1-\theta)\}, so the induced prior on θ\theta is p(θ)=p(ϕ)∣ dϕ dθ∣∝(1+θ1−θ)−n0(θ1−θ)n0t01θ(1−θ)=(1−θ)n0 θn0t0(1−θ)−n0t0 θ−1(1−θ)−1=θn0t0−1(1−θ)n0(1−t0)−1.\begin{aligned} p(\theta) &= p(\phi)\left|\frac{\,\mathrm{d}\phi}{\,\mathrm{d}\theta}\right| \propto\left(1+\frac\theta{1-\theta}\right)^{-n_0}\left(\frac\theta{1-\theta}\right)^{n_0t_0}\frac1{\theta(1-\theta)}\\ &= (1-\theta)^{n_0}\,\theta^{n_0t_0}(1-\theta)^{-n_0t_0}\,\theta^{-1}(1-\theta)^{-1} = \theta^{n_0t_0-1}(1-\theta)^{n_0(1-t_0)-1}. \end{aligned} This is the Beta⁡(n0t0, n0(1−t0))\operatorname{Beta}(n_0t_0,\,n_0(1-t_0)) distribution. So we recover the Beta prior, with a+b=n0a+b=n_0 (the prior sample size) and a/(a+b)=t0a/(a+b)=t_0 (the prior mean).

Example 10.4 (Poisson). The conjugate prior is p(ϕ)∝exp⁡{−n0eϕ}en0t0ϕp(\phi)\propto\exp\{-n_0e^\phi\}e^{n_0t_0\phi}. With θ=eϕ\theta=e^\phi,  dϕ/ dθ=1/θ\,\mathrm{d}\phi/\,\mathrm{d}\theta=1/\theta, and the induced prior on θ\theta is p(θ)∝e−n0θ θn0t0⋅θ−1=θn0t0−1e−n0θ,p(\theta)\propto e^{-n_0\theta}\,\theta^{n_0t_0}\cdot\theta^{-1} = \theta^{n_0t_0-1}e^{-n_0\theta}, the Ga⁡(n0t0, n0)\operatorname{Ga}(n_0t_0,\,n_0) distribution. We recover the Gamma prior, with b=n0b=n_0 observation periods and total count a=n0t0a=n_0t_0.

10.2 Non-informative priors

References: GCSR Chapter 2; Lee §2.4.

Every Bayesian analysis requires a prior. Since the posterior is proportional to prior times likelihood, the prior can in principle have a large influence on the conclusions. In simple problems with a moderate amount of data this influence is usually small, as we have seen repeatedly. But it is not always small. With a parameter of moderate or high dimension, a prior that seems harmless for each component separately may be highly informative about some function of the components. And in some applications there is simply no agreed prior information, or there is a wish to present an analysis whose conclusions do not appear to depend on anyone’s particular beliefs.

This has motivated a long search for priors that play a minimal role in the analysis, and that ‘let the data speak for themselves’. Such priors go by many names: vague, flat, diffuse, reference, objective, non-informative. None of these names should be taken quite literally. As we shall see, there is no prior that represents complete ignorance in every respect, and the search is better understood as one for priors that are reasonable defaults.

10.3 Improper priors

A natural way to make a prior uninformative is to make it very spread out, and the limit of this process often leads to a ‘density’ that cannot be normalised.

A prior p(θ)p(\theta) is proper if it integrates to one. If ∫p(θ) dθ=k\int p(\theta)\,\mathrm{d}\theta=k with kk finite, then pp can be made proper by dividing by kk, so only the shape of a prior matters. If the integral is infinite, the prior is improper. An improper prior is not a probability distribution, and cannot literally represent anyone’s beliefs. It may nonetheless be used, provided the resulting posterior is proper, that is, provided ∫p(y∣θ) p(θ) dθ<∞\int p(y\mid\theta)\,p(\theta)\,\mathrm{d}\theta<\infty. The posterior can then be interpreted as an approximation to the posterior under a proper but very diffuse prior.

10.3.1 Binomial model

In the binomial model with a Beta⁡(a,b)\operatorname{Beta}(a,b) prior, we interpreted aa and bb as prior numbers of successes and failures. It seems natural, then, to express the absence of prior information by setting a=b=0a=b=0. This corresponds to p(θ)∝θ−1(1−θ)−1,0<θ<1,p(\theta)\propto\theta^{-1}(1-\theta)^{-1}, \qquad 0<\theta<1, which is called the Haldane prior. Its integral over (0,1)(0,1) is infinite, since near each end the integrand behaves like 1/θ1/\theta or 1/(1−θ)1/(1-\theta): ∫01 dθθ(1−θ)=∫01/2 dθθ(1−θ)+∫1/21 dθθ(1−θ)≥∫01/2 dθθ+∫1/21 dθ1−θ=2∫01/2 dθθ=2[log⁡θ]01/2=∞.\begin{aligned} \int_0^1\frac{\,\mathrm{d}\theta}{\theta(1-\theta)} &= \int_0^{1/2}\frac{\,\mathrm{d}\theta}{\theta(1-\theta)}+\int_{1/2}^1\frac{\,\mathrm{d}\theta}{\theta(1-\theta)}\\ &\ge \int_0^{1/2}\frac{\,\mathrm{d}\theta}\theta+\int_{1/2}^1\frac{\,\mathrm{d}\theta}{1-\theta} = 2\int_0^{1/2}\frac{\,\mathrm{d}\theta}\theta = 2\Big[\log\theta\Big]_0^{1/2} = \infty. \end{aligned} So the Haldane prior is improper. Nevertheless, setting a=b=0a=b=0 in the posterior formula gives Beta⁡(y, n−y)\operatorname{Beta}(y,\,n-y), which is a proper distribution provided both y>0y>0 and n−y>0n-y>0. If, however, every observation is a success, or every observation is a failure, the posterior is improper and the analysis breaks down. The quality control data of Exercise 4.1, with no defective items, are an example where the Haldane prior cannot be used.

This suggests a safe procedure for using improper priors:

  1. find the posterior under a proper conjugate prior;

  2. let the hyperparameters tend to the values that give the improper prior (a=b=0a=b=0 in this example);

  3. if the limit is a proper distribution, it may be used as the posterior.

10.3.2 Normal model with known mean

With σ2∼Inv-Ga⁡(ν0/2,ν0σ02/2)\sigma^2\sim\operatorname{Inv\text{-}Ga}(\nu_0/2,\nu_0\sigma_0^2/2), the parameter ν0\nu_0 is the number of observations the prior is worth. Letting ν0→0\nu_0\to0 seems the natural way to express ignorance. In the limit the prior density becomes p(σ2)∝(σ2)−1p(\sigma^2)\propto(\sigma^2)^{-1}, which has infinite integral over (0,∞)(0,\infty) (the integral diverges at both ends), so the limiting prior is improper. Setting ν0=0\nu_0=0 in the posterior, however, gives σ2∣y∼Inv-Ga⁡(n2,  nv2),\sigma^2\mid y\sim\operatorname{Inv\text{-}Ga}\left(\frac n2,\;\frac{nv}2\right), which is proper for any n≥1n\ge1 provided v>0v>0.

In both examples an improper prior led to a proper posterior, at least for most data sets. This does not always happen, and in complicated models it can fail in ways that are not obvious. An improper prior places on its user the burden of proving that the posterior is proper. MCMC methods will happily produce output from an improper posterior, and that output will be meaningless.

10.4 Jeffreys’ prior

The uniform prior has an obvious appeal as an expression of ignorance, but it has a serious defect, which we noted in Section 2.6. If we know nothing about a parameter θ\theta, then presumably we know nothing about any one-to-one function of it, ϕ=g(θ)\phi=g(\theta). A rule for choosing non-informative priors ought therefore to give consistent answers whether it is applied to θ\theta or to ϕ\phi. But the prior densities for θ\theta and ϕ\phi must be related by the transformation formula, p(ϕ)=p(θ)∣ dθ dϕ∣,p(\phi) = p(\theta)\left|\frac{\,\mathrm{d}\theta}{\,\mathrm{d}\phi}\right|, so a prior that is uniform in θ\theta is not uniform in ϕ\phi unless gg is linear. For example, a uniform prior on the success probability θ\theta of a binomial model is not uniform on the log-odds. The rule ‘use a uniform prior’ therefore gives different answers depending on which parameterisation we happen to start from, and this is unsatisfactory.

Jeffreys proposed a rule that does not suffer from this defect. It takes p(θ)∝J(θ)1/2,p(\theta)\propto J(\theta)^{1/2}, where J(θ)J(\theta) is the Fisher information of the model, J(θ)=E⁡[( d dθlog⁡p(Y∣θ))2 | θ]=−E⁡[ d2 dθ2log⁡p(Y∣θ) | θ].J(\theta) = \operatorname{E}\left[\left(\frac{\,\mathrm{d}}{\,\mathrm{d}\theta}\log p(Y\mid\theta)\right)^2\,\middle|\,\theta\right] = -\operatorname{E}\left[\frac{\,\mathrm{d}^2}{\,\mathrm{d}\theta^2}\log p(Y\mid\theta)\,\middle|\,\theta\right]. (The two expressions are equal under mild regularity conditions, and the second is usually easier to compute.) The Fisher information measures how sharply, on average, the likelihood discriminates between nearby values of θ\theta. Jeffreys’ prior is therefore larger where the data are expected to be more informative. The key property is that it transforms correctly.

Lemma 10.5. Let ϕ=h(θ)\phi=h(\theta) be a one-to-one, continuously differentiable transformation. Then J(ϕ)=J(θ)∣ dθ dϕ∣2.J(\phi) = J(\theta)\left|\frac{\,\mathrm{d}\theta}{\,\mathrm{d}\phi}\right|^2.

Proof. The model is the same whichever parameter we use to describe it, so log⁡p(y∣ϕ)=log⁡p(y∣θ)\log p(y\mid\phi)=\log p(y\mid\theta) when θ=h−1(ϕ)\theta=h^{-1}(\phi). Differentiating with respect to ϕ\phi by the chain rule,  d dϕlog⁡p(y∣ϕ)= d dθlog⁡p(y∣θ)⋅ dθ dϕ.\frac{\,\mathrm{d}}{\,\mathrm{d}\phi}\log p(y\mid\phi) = \frac{\,\mathrm{d}}{\,\mathrm{d}\theta}\log p(y\mid\theta)\cdot\frac{\,\mathrm{d}\theta}{\,\mathrm{d}\phi}. Now square both sides and take expectations over YY, conditional on ϕ\phi on the left and on the corresponding θ=h−1(ϕ)\theta=h^{-1}(\phi) on the right. The factor  dθ/ dϕ\,\mathrm{d}\theta/\,\mathrm{d}\phi is not random and comes outside the expectation, giving the result. ◻

Suppose, then, that we apply Jeffreys’ rule to θ\theta, taking p(θ)∝J(θ)p(\theta)\propto\sqrt{J(\theta)}. The prior this induces on ϕ\phi is, by the transformation formula and the lemma, p(ϕ)=p(θ)∣ dθ dϕ∣∝J(θ)∣ dθ dϕ∣=J(ϕ)∣ dθ dϕ∣−1∣ dθ dϕ∣=J(ϕ).p(\phi) = p(\theta)\left|\frac{\,\mathrm{d}\theta}{\,\mathrm{d}\phi}\right| \propto \sqrt{J(\theta)}\left|\frac{\,\mathrm{d}\theta}{\,\mathrm{d}\phi}\right| = \sqrt{J(\phi)}\left|\frac{\,\mathrm{d}\theta}{\,\mathrm{d}\phi}\right|^{-1}\left|\frac{\,\mathrm{d}\theta}{\,\mathrm{d}\phi}\right| = \sqrt{J(\phi)}. This is exactly what we would have obtained by applying Jeffreys’ rule to ϕ\phi directly. The rule gives the same answer whichever parameterisation we begin with, which is the consistency we asked for.

Example 10.6 (Normal variance). Let Y1,…,Yn∣ψ∼iidN⁡(θ,ψ)Y_1,\ldots,Y_n\mid\psi\stackrel{\text{iid}}{\sim}\operatorname{N}(\theta,\psi), with θ\theta known and ψ=σ2\psi=\sigma^2 unknown. Then log⁡p(y∣ψ)=const.−n2log⁡ψ−12ψ∑i(yi−θ)2, d dψlog⁡p(y∣ψ)=−n2ψ+12ψ2∑i(yi−θ)2, d2 dψ2log⁡p(y∣ψ)=n2ψ2−1ψ3∑i(yi−θ)2.\begin{aligned} \log p(y\mid\psi) &= \text{const.}-\frac n2\log\psi-\frac1{2\psi}\sum_i(y_i-\theta)^2,\\ \frac{\,\mathrm{d}}{\,\mathrm{d}\psi}\log p(y\mid\psi) &= -\frac n{2\psi}+\frac1{2\psi^2}\sum_i(y_i-\theta)^2,\\ \frac{\,\mathrm{d}^2}{\,\mathrm{d}\psi^2}\log p(y\mid\psi) &= \frac n{2\psi^2}-\frac1{\psi^3}\sum_i(y_i-\theta)^2. \end{aligned} Since E⁡[(Yi−θ)2∣ψ]=ψ\operatorname{E}[(Y_i-\theta)^2\mid\psi]=\psi, we have E⁡[∑i(Yi−θ)2∣ψ]=nψ\operatorname{E}[\sum_i(Y_i-\theta)^2\mid\psi]=n\psi, and so J(ψ)=−(n2ψ2−nψψ3)=n2ψ2.J(\psi) = -\left(\frac n{2\psi^2}-\frac{n\psi}{\psi^3}\right) = \frac n{2\psi^2}. Jeffreys’ prior is therefore p(σ2)∝J(σ2)∝1/σ2p(\sigma^2)\propto\sqrt{J(\sigma^2)}\propto1/\sigma^2. This is the same improper prior that we obtained in the previous section by letting ν0→0\nu_0\to0 in the conjugate prior, so two quite different lines of argument lead to the same default choice.

Jeffreys’ prior is widely used, but it should not be regarded as the final answer to the problem of representing ignorance. It is often improper, so the propriety of the posterior must be checked. Its invariance is a property of the rule, not a demonstration that the resulting prior expresses no information. And for parameters of more than one dimension, the natural generalisation, p(θ)∝det⁡J(θ)p(\boldsymbol\theta)\propto\sqrt{\det J(\boldsymbol\theta)}, can give unsatisfactory results; Jeffreys himself recommended against using it in some multi-parameter problems.

Exercises

Exercise 10.1

Find Jeffreys’ prior for:

  1. θ\theta in the Bin⁡(n,θ)\operatorname{Bin}(n,\theta) model;

  2. θ\theta in the Poi⁡(θ)\operatorname{Poi}(\theta) model, with nn observations.

In each case state whether the prior is proper, and give the posterior.

Show solutionHide solution

(a) log⁡p(y∣θ)=const.+ylog⁡θ+(n−y)log⁡(1−θ)\log p(y\mid\theta)=\text{const.}+y\log\theta+(n-y)\log(1-\theta), so − d2 dθ2log⁡p(y∣θ)=yθ2+n−y(1−θ)2.-\frac{\,\mathrm{d}^2}{\,\mathrm{d}\theta^2}\log p(y\mid\theta)=\frac y{\theta^2}+\frac{n-y}{(1-\theta)^2}. Taking expectations with E⁡[Y]=nθ\operatorname{E}[Y]=n\theta gives J(θ)=n/θ+n/(1−θ)=n/{θ(1−θ)}J(\theta)=n/\theta+n/(1-\theta)=n/\{\theta(1-\theta)\}. So p(θ)∝θ−1/2(1−θ)−1/2p(\theta)\propto\theta^{-1/2}(1-\theta)^{-1/2}, the Beta⁡(12,12)\operatorname{Beta}(\frac12,\frac12) distribution. It is proper. The posterior is Beta⁡(y+12, n−y+12)\operatorname{Beta}(y+\frac12,\,n-y+\frac12).

(b) For one observation, − d2 dθ2log⁡p(y∣θ)=y/θ2-\frac{\,\mathrm{d}^2}{\,\mathrm{d}\theta^2}\log p(y\mid\theta)=y/\theta^2, with expectation 1/θ1/\theta. For nn observations J(θ)=n/θJ(\theta)=n/\theta. So p(θ)∝θ−1/2p(\theta)\propto\theta^{-1/2}, which is improper on (0,∞)(0,\infty). It is the limit of Ga⁡(12,b)\operatorname{Ga}(\frac12,b) as b→0b\to0. The posterior is Ga⁡(∑iyi+12, n)\operatorname{Ga}(\sum_iy_i+\frac12,\,n), which is proper for any data.

Exercise 10.2

In the normal model with known mean, find Jeffreys’ prior for the standard deviation σ\sigma directly. Check that it agrees with the prior p(σ2)∝1/σ2p(\sigma^2)\propto1/\sigma^2 after a change of variable.

Show solutionHide solution

With log⁡p(y∣σ)=const.−nlog⁡σ−12σ2∑i(yi−θ)2\log p(y\mid\sigma)=\text{const.}-n\log\sigma-\frac1{2\sigma^2}\sum_i(y_i-\theta)^2, − d2 dσ2log⁡p(y∣σ)=−nσ2+3σ4∑i(yi−θ)2,-\frac{\,\mathrm{d}^2}{\,\mathrm{d}\sigma^2}\log p(y\mid\sigma) = -\frac n{\sigma^2}+\frac3{\sigma^4}\sum_i(y_i-\theta)^2, with expectation −n/σ2+3n/σ2=2n/σ2-n/\sigma^2+3n/\sigma^2=2n/\sigma^2. So p(σ)∝1/σp(\sigma)\propto1/\sigma. Starting instead from p(σ2)∝1/σ2p(\sigma^2)\propto1/\sigma^2 and changing variable, p(σ)=p(σ2) ∣ dσ2/ dσ∣∝σ−2⋅2σ∝1/σp(\sigma)=p(\sigma^2)\,|\,\mathrm{d}\sigma^2/\,\mathrm{d}\sigma|\propto\sigma^{-2}\cdot2\sigma\propto1/\sigma. They agree, as the lemma requires. The prior is also equivalent to a uniform prior on log⁡σ\log\sigma.

Exercise 10.3

Write the Exp⁡(θ)\operatorname{Exp}(\theta) model in exponential-family form. Find the prior on θ\theta induced by the conjugate prior for the natural parameter.

Show solutionHide solution

p(y∣θ)=θe−θyp(y\mid\theta)=\theta e^{-\theta y}. Take ϕ=−θ\phi=-\theta, so that p(y∣ϕ)=(−ϕ)exp⁡{ϕy}p(y\mid\phi)=(-\phi)\exp\{\phi y\}, with h(y)=1h(y)=1, c(ϕ)=−ϕc(\phi)=-\phi for ϕ<0\phi<0, and t(y)=yt(y)=y. The conjugate prior is p(ϕ)∝(−ϕ)n0en0t0ϕp(\phi)\propto(-\phi)^{n_0}e^{n_0t_0\phi}. Since ∣ dϕ/ dθ∣=1|\,\mathrm{d}\phi/\,\mathrm{d}\theta|=1, p(θ)∝θn0e−n0t0θ,p(\theta)\propto\theta^{n_0}e^{-n_0t_0\theta}, the Ga⁡(n0+1, n0t0)\operatorname{Ga}(n_0+1,\,n_0t_0) distribution. The shape is n0+1n_0+1 rather than n0n_0 because the natural parameter here is linear in θ\theta, so there is no Jacobian factor θ−1\theta^{-1} as in the Poisson case. The prior still carries n0n_0 imagined observations with mean t0t_0, since the posterior is Ga⁡(n0+n+1, n0t0+nyˉ)\operatorname{Ga}(n_0+n+1,\,n_0t_0+n\bar y).

11 Hierarchical models

Reference: GCSR §5.1–5.3; Hoff Chapter 8.

In every model so far, the data have come from a single population described by a single parameter, or a single vector of parameters. Many data sets are made up of several related groups: patients in different hospitals, pupils in different schools, experiments conducted in different laboratories. Each group has its own parameter, but the groups are similar enough that knowing about some of them tells us something about the others. Hierarchical models are the Bayesian way of expressing this, and they bring together nearly every idea in the course: exchangeability, conjugacy, prediction and MCMC.

11.1 Several binomial experiments

Suppose we have the results of JJ binomial experiments, Yj∣θj∼Bin⁡(nj,θj),j=1,…,J,Y_j\mid\theta_j\sim\operatorname{Bin}(n_j,\theta_j), \qquad j=1,\ldots,J, each with its own success probability θj\theta_j. How should we analyse them?

At one extreme, if we believe the θj\theta_j are completely unrelated, we can analyse each experiment separately, exactly as in Chapter 4. We would give each θj\theta_j its own prior, perhaps Beta⁡(aj,bj)\operatorname{Beta}(a_j,b_j) with fixed aja_j and bjb_j, or perhaps the same Beta⁡(a,b)\operatorname{Beta}(a,b) for every jj, and obtain p(θj∣yj)p(\theta_j\mid y_j) from the jjth experiment alone. At the other extreme, if we believe all the θj\theta_j are equal, we can pool the experiments into a single large one. Neither extreme is usually right.

Often the θj\theta_j are related but not identical, so that knowing one of them gives some information about the others. For example:

  • θj\theta_j might be the probability that drug jj produces a certain effect, where the drugs all belong to the same family of chemical compounds;

  • θj\theta_j might be the mortality rate from a certain disease in city jj;

  • θj\theta_j might be the probability that a laboratory rat in experiment jj develops a tumour when given a dose of a certain drug.

In such cases it is natural to regard the θj\theta_j as a sample from some common population of values, and to use what we learn about that population to inform our estimate of each individual θj\theta_j.

11.2 Rat tumour data

Our example comes from GCSR §5.1. In the current experiment, 4 of 14 laboratory rats developed a certain kind of tumour. The results of 70 earlier experiments with rats of the same strain are also available. The earlier tumour rates yj/njy_j/n_j have mean 0.1360.136 and standard deviation 0.1030.103, and in 14 of those experiments no tumours were observed.

Analysing the current experiment alone, with a uniform prior, gives θ71∣y71∼Beta⁡(5,11)\theta_{71}\mid y_{71}\sim\operatorname{Beta}(5,11), with mean 5/16≈0.315/16\approx0.31. This takes no account of the earlier experiments, which suggest that tumour rates are usually lower than this. How should we use the historical data to improve our inference about θ71\theta_{71}?

11.2.1 Empirical Bayes

One simple approach is to use the historical data to choose a prior for θ71\theta_{71}. We seek a Beta⁡(a,b)\operatorname{Beta}(a,b) distribution whose mean and variance match the sample mean mm and sample variance vv of the 70 earlier rates: m=aa+b,v=m(1−m)a+b+1.m = \frac a{a+b}, \qquad v = \frac{m(1-m)}{a+b+1}. Solving these two equations for aa and bb gives a=(1−m)m2v−m,b=a(1m−1).a = \frac{(1-m)m^2}v-m, \qquad b = a\left(\frac1m-1\right). With m=0.136m=0.136 and v=0.103\sqrt v=0.103 this yields a≈1.4a\approx1.4 and b≈8.6b\approx8.6. The prior Beta⁡(1.4,8.6)\operatorname{Beta}(1.4,8.6) has mean 0.140.14 and is worth about ten observations. Combining it with the current data gives the posterior Beta⁡(1.4+4, 8.6+10)=Beta⁡(5.4, 18.6)\operatorname{Beta}(1.4+4,\,8.6+10)=\operatorname{Beta}(5.4,\,18.6), with mean 0.2250.225. The estimate has been pulled well down from the raw rate of 4/14≈0.294/14\approx0.29, towards the historical average.

This approach, in which the prior is estimated from the data, is called empirical Bayes. It is practical and often gives sensible answers. But it has two weaknesses. It treats the estimated values of aa and bb as if they were known exactly, and so understates our uncertainty; with only 70 earlier experiments, aa and bb are in fact rather uncertain. And the moment-matching method, while simple, is somewhat arbitrary. The hierarchical model addresses both points.

11.3 The hierarchical model

The fully Bayesian approach is to treat the unknown aa and bb as parameters like any other, and to give them a prior distribution. In general terms, suppose Yj∣θj∼p(yj∣θj)independently,j=1,…,J.Y_j\mid\theta_j\sim p(y_j\mid\theta_j) \quad\text{independently}, \qquad j=1,\ldots,J. If we have no information that distinguishes the groups from one another before seeing the data, we should judge θ1,…,θJ\theta_1,\ldots,\theta_J exchangeable. De Finetti’s theorem then tells us to model them as conditionally i.i.d. given some further parameter ϕ\phi, called a hyperparameter: θj∣ϕ∼iidp(θ∣ϕ),j=1,…,J,\theta_j\mid\phi\stackrel{\text{iid}}{\sim}p(\theta\mid\phi), \qquad j=1,\ldots,J, and to give ϕ\phi a prior distribution of its own, ϕ∼p(ϕ)\phi\sim p(\phi). For the binomial experiments, ϕ=(a,b)\phi=(a,b) and θj∣a,b∼iidBeta⁡(a,b)\theta_j\mid a,b\stackrel{\text{iid}}{\sim}\operatorname{Beta}(a,b). The distribution p(θ∣ϕ)p(\theta\mid\phi) describes the population from which the group parameters are drawn, and ϕ\phi describes the characteristics of that population, such as its mean and spread.

The model is called hierarchical because it has several levels:

  1. the prior distribution of the hyperparameter ϕ\phi;

  2. the distribution of the group parameters θj\theta_j given ϕ\phi;

  3. the distribution of the data YjY_j given the group parameters θj\theta_j.

It can be represented by a directed acyclic graph (DAG), with a node for each random quantity and an arrow from ϕ\phi to each θj\theta_j, and from each θj\theta_j to the corresponding YjY_j. The arrows show the order in which the model generates the quantities: first ϕ\phi, then the θj\theta_j given ϕ\phi, then the data given the θj\theta_j.

11.3.1 Conditional independence and the sharing of information

The DAG also encodes conditional independence. In such a graph, each variable is conditionally independent of the variables that are not its descendants, given its parents (the variables with arrows pointing into it). So, given θj\theta_j, the observation YjY_j is independent of ϕ\phi and of all the other θ\thetas and YYs, and p(y∣θ,ϕ)=∏j=1Jp(yj∣θj).p(y\mid\theta,\phi) = \prod_{j=1}^Jp(y_j\mid\theta_j). Similarly, given ϕ\phi, the θj\theta_j are independent of one another.

But ϕ\phi is unknown, and this is what makes the model useful. If we were told the value of θ1\theta_1, say, this would change our beliefs about ϕ\phi, the characteristics of the population from which θ1\theta_1 was drawn. And through ϕ\phi it would change our beliefs about θ2,…,θJ\theta_2,\ldots,\theta_J. In the same way, the data from each experiment inform us about ϕ\phi, and hence about every other experiment. Information is shared across the experiments by way of the hyperparameter. This is exactly the dependence through an unknown common parameter that we met in Chapters 2 and 3, now operating one level higher.

It is worth stressing that the DAG describes the joint distribution before any data are observed. Once we condition on the data, the conditional independence relations it displays generally no longer hold for the posterior distribution of the parameters.

11.3.2 The posterior

The unknowns are now both the group parameters θ=(θ1,…,θJ)\theta=(\theta_1,\ldots,\theta_J) and the hyperparameter ϕ\phi. Their joint posterior is p(θ,ϕ∣y)∝p(ϕ) p(θ∣ϕ) p(y∣θ)=p(ϕ){∏j=1Jp(θj∣ϕ)}{∏j=1Jp(yj∣θj)},p(\theta,\phi\mid y)\propto p(\phi)\,p(\theta\mid\phi)\,p(y\mid\theta) = p(\phi)\left\{\prod_{j=1}^Jp(\theta_j\mid\phi)\right\}\left\{\prod_{j=1}^Jp(y_j\mid\theta_j)\right\}, where the factorisation follows the levels of the hierarchy. With J=71J=71 experiments, θ\theta has 71 components and the posterior has 73 dimensions. Direct integration is out of the question, but the structure of the posterior is well suited to the Gibbs sampler.

11.4 The rat tumour example

The full model is Yj∣θj∼Bin⁡(nj,θj)independently for j=1,…,71,θj∣a,b∼Beta⁡(a,b)independently,(a,b)∼p(a,b).\begin{aligned} Y_j\mid\theta_j &\sim \operatorname{Bin}(n_j,\theta_j) \quad\text{independently for } j=1,\ldots,71,\\ \theta_j\mid a,b &\sim \operatorname{Beta}(a,b) \quad\text{independently},\\ (a,b) &\sim p(a,b). \end{aligned}

11.4.1 The prior on (a,b)(a,b)

The parameters aa and bb are not easy to think about directly. It is more natural to work with n0=a+bn_0=a+b, the equivalent number of observations carried by the population distribution, and t0=a/(a+b)t_0=a/(a+b), the mean of the population distribution, which is the prior expectation of each θj\theta_j. The lecture example uses independent priors t0∼Unif⁡(0,1)t_0\sim\operatorname{Unif}(0,1) and n0∼Exp⁡(λ)n_0\sim\operatorname{Exp}(\lambda) with λ=1/20\lambda=1/20. This says that we have no prior preference for the average tumour rate, and that we expect the population of rates to be about as concentrated as a Beta distribution worth twenty or so observations, while allowing values of n0n_0 much larger or smaller. Exercise 11.1 derives the corresponding prior on (a,b)(a,b). GCSR use a different prior, p(a,b)∝(a+b)−5/2p(a,b)\propto(a+b)^{-5/2}, which is improper; see their §5.3 for the reasoning behind it.

11.4.2 A Gibbs sampler

We sample from the posterior with a Gibbs sampler, alternating between the group parameters and the hyperparameters.

  1. Update the θj\theta_j. The full conditional of θj\theta_j is proportional to the joint posterior regarded as a function of θj\theta_j, which involves only the factors p(θj∣a,b) p(yj∣θj)p(\theta_j\mid a,b)\,p(y_j\mid\theta_j). This is a Beta prior times a binomial likelihood, and by the conjugate result of Chapter 4, θj∣a,b,y∼Beta⁡(a+yj,  b+nj−yj).\theta_j\mid a,b,y\sim\operatorname{Beta}(a+y_j,\;b+n_j-y_j). The θj\theta_j are conditionally independent given aa, bb and the data, so all 71 can be drawn at once.

  2. Update (a,b)(a,b). The full conditional of (a,b)(a,b) involves the prior and the Beta densities of the current θj\theta_j: p(a,b∣θ,y)∝p(a,b)∏j=1JΓ(a+b)Γ(a)Γ(b)θja−1(1−θj)b−1.p(a,b\mid\theta,y)\propto p(a,b)\prod_{j=1}^J\frac{\Gamma(a+b)}{\Gamma(a)\Gamma(b)}\theta_j^{a-1}(1-\theta_j)^{b-1}. The data do not appear directly, because given the θj\theta_j the hyperparameters are independent of the data. This full conditional is not a standard distribution, so we replace the exact draw by a Metropolis–Hastings step, perhaps a random walk on (log⁡a,log⁡b)(\log a,\log b), as described at the end of Chapter 9.

In practice one rarely writes such samplers from scratch. Software such as Stan, JAGS and BUGS will construct an MCMC sampler automatically from a description of the model. The results on the slides were obtained with JAGS.

11.4.3 Results

The slides show draws from the joint posterior of (a,b)(a,b), and of (t0,log⁡n0)(t_0,\log n_0), and the marginal posterior of θ71\theta_{71}. The posterior of θ71\theta_{71} is close to the empirical Bayes posterior Beta⁡(5.4,18.6)\operatorname{Beta}(5.4,18.6) found earlier, but slightly wider. The extra width comes from our uncertainty about aa and bb, which the hierarchical model includes and the empirical Bayes analysis ignores.

The most instructive plot shows the posterior medians and 95% intervals for all 71 θj\theta_j, plotted against the raw rates yj/njy_j/n_j. A clear pattern appears. Experiments with low raw rates have posterior medians somewhat above those rates, and experiments with high raw rates have posterior medians somewhat below. Every estimate is pulled towards the overall mean.

This phenomenon is called shrinkage, and it is a consequence of the sharing of information described above, often called borrowing strength. The posterior for each θj\theta_j depends not only on the data from experiment jj but, through their influence on (a,b)(a,b), on the data from all the other experiments. An experiment that recorded an unusually high or low rate is judged, in effect, against the background of all the others, and its extreme result is partly attributed to chance. The amount of shrinkage is greatest for the experiments with the least data of their own, since for them the population information carries relatively more weight (Exercise 11.2).

Shrinkage is most striking for the experiments with no observed tumours. Their raw rate is zero, but it would be unreasonable to conclude that the true tumour rate in those experiments is zero when other experiments with the same strain of rat recorded tumours. The hierarchical model gives them small but positive posterior medians.

Finally, the slides compare the 95% intervals from the hierarchical model with those from separate analyses using independent uniform priors on each θj\theta_j. The hierarchical intervals are shorter, because each experiment benefits from the information in the others. In the separate analyses the posterior medians lie close to the raw rates, except where the raw rate is zero, where the uniform prior itself pulls the estimate away from zero. The hierarchical model has let the data decide how much the experiments should be pooled: somewhere between the two extremes of treating them as identical and treating them as unrelated.

Exercises

Exercise 11.1

Let t0∼Unif⁡(0,1)t_0\sim\operatorname{Unif}(0,1) and n0∼Exp⁡(λ)n_0\sim\operatorname{Exp}(\lambda) independently, and let a=t0n0a=t_0n_0 and b=(1−t0)n0b=(1-t_0)n_0. Find the joint density of (a,b)(a,b).

Show solutionHide solution

The inverse is t0=a/(a+b)t_0=a/(a+b), n0=a+bn_0=a+b. The map (t0,n0)↦(a,b)(t_0,n_0)\mapsto(a,b) has Jacobian determinant det⁡(n0t0−n01−t0)=n0,\det\begin{pmatrix} n_0 & t_0\\ -n_0 & 1-t_0\end{pmatrix} = n_0, so the inverse map has ∣det⁡J∣=1/n0=1/(a+b)|\det J|=1/n_0=1/(a+b). Hence p(a,b)=1⋅λe−λ(a+b)⋅1a+b,a,b>0.p(a,b) = 1\cdot\lambda e^{-\lambda(a+b)}\cdot\frac1{a+b}, \qquad a,b>0. Compare Exercise 2.2, where the same change of variables appears in reverse.

Exercise 11.2

In the hierarchical binomial model, suppose that aa and bb were known. Show that E⁡[θj∣y]\operatorname{E}[\theta_j\mid y] is a weighted average of yj/njy_j/n_j and a/(a+b)a/(a+b). Which experiments are shrunk most?

Show solutionHide solution

If aa and bb are known, the θj\theta_j are independent given the data, and θj∣y∼Beta⁡(a+yj, b+nj−yj)\theta_j\mid y\sim\operatorname{Beta}(a+y_j,\,b+n_j-y_j). So E⁡[θj∣y]=a+yja+b+nj=a+ba+b+nj⋅aa+b+nja+b+nj⋅yjnj.\operatorname{E}[\theta_j\mid y] = \frac{a+y_j}{a+b+n_j} = \frac{a+b}{a+b+n_j}\cdot\frac a{a+b}+\frac{n_j}{a+b+n_j}\cdot\frac{y_j}{n_j}. The weight on the common mean is (a+b)/(a+b+nj)(a+b)/(a+b+n_j). It is largest when njn_j is small. Small experiments are shrunk most. With a+b≈10a+b\approx10 as in the rat data, an experiment with nj=20n_j=20 puts about a third of its weight on the common mean.