CalcPath
ProgressProblem setsFeedback
← Question sheet

Statistics · Solutions

Bayesian Inference

12 questions · about 102 minutes

Work this sheet on paper and show every step — the solutions are written as methods so you can check your route, not just your final number. Name every result you use (“by Beta-binomial conjugacy”, “by the weighted-average form of the posterior mean”, “by detailed balance”) and keep every answer as an exact fraction: no calculator is needed anywhere, and the only decimal you will want is z0.975​=1.96. Full worked solutions are on a separate page; each question also has its own reveal, so you can check one answer without seeing the rest.

  1. 1.Computation

    A screening test is given to a population in which 1 person in 10 has a certain condition D. The test has sensitivity P(+∣D)=54​ and specificity P(−∣Dc)=109​. Work in exact fractions throughout.

    1. (a)A person tests positive. Compute P(D∣+) by Bayes' theorem, writing out the marginal probability P(+) explicitly.
    2. (b)Redo (a) on the odds scale: state the prior odds, the likelihood ratio of a positive result, and the posterior odds, and confirm that the answer agrees.
    3. (c)A different person tests negative. Compute P(D∣−). Which result — a positive or a negative — moves the odds by the larger factor, and what feature of the test explains that?

    Solution

    (a) Bayes' theorem with the marginal written out. The prior is P(D)=101​, P(Dc)=109​. The likelihoods of a positive result are P(+∣D)=54​ and P(+∣Dc)=1−109​=101​. The marginal probability of a positive is

    P(+)=P(+∣D)P(D)+P(+∣Dc)P(Dc)=54​⋅101​+101​⋅109​=1008​+1009​=10017​.

    Hence

    P(D∣+)=P(+)P(+∣D)P(D)​=17/1008/100​=178​.

    A positive result moves the probability from 101​ to a little under one half: of every 17 positives, 9 are false, because the healthy nine-tenths of the population generate almost as many false positives as the sick tenth generates true ones.

    (b) The odds route. The prior odds of disease are P(D):P(Dc)=1:9, that is 91​. The likelihood ratio of a positive result is

    P(+∣Dc)P(+∣D)​=1/104/5​=8,

    so the posterior odds are 91​×8=98​. Converting back, P(D∣+)=1+8/98/9​=178​, agreeing with (a). The marginal P(+) never appears on the odds scale — it cancels in the ratio — which is why sequential updating is so convenient there.

    (c) A negative result. Now the likelihoods are P(−∣D)=1−54​=51​ and P(−∣Dc)=109​, so the likelihood ratio is 9/101/5​=92​ and the posterior odds are 91​⋅92​=812​. Therefore

    P(D∣−)=1+2/812/81​=832​.

    Check by the marginal route: P(−)=51​⋅101​+109​⋅109​=1002​+10081​=10083​, and P(D∣−)=83/1002/100​=832​. ✓

    A positive multiplies the odds by 8; a negative multiplies them by 92​, that is, divides them by 421​. The positive result is the larger move. The two ratios say why: a positive is 8 times as likely under disease as under health because the false-positive rate 101​ is small, whereas a negative is only 421​ times as likely under health as under disease because the false-negative rate 51​ is not so small. A test's two error rates contribute separately, one to each direction of update.

  2. 2.Computation

    A coin is known to be one of three types, with probability of heads θ∈{41​,21​,43​}, and the prior is π(41​)=41​, π(21​)=21​, π(43​)=41​. The coin is tossed three times and shows three heads.

    1. (a)Write the posterior in the form “posterior ∝ likelihood × prior”, compute the three unnormalised values, and hence the marginal likelihood f(x).
    2. (b)State the posterior distribution and check that it sums to one. For each value of θ, give the factor by which its probability changed, and explain why that factor is f(x∣θ)/f(x).
    3. (c)Suppose instead the prior had been π(41​)=21​, π(21​)=21​, π(43​)=0. Find the posterior after three heads, and describe the posterior after a hundred heads in a hundred tosses. What lesson about priors does this illustrate?

    Solution

    (a) Likelihood times prior. Three heads in three tosses have probability f(x∣θ)=θ3, so π(θ∣x)∝θ3π(θ). The unnormalised values are

    θ=41​: 641​⋅41​=2561​,θ=21​: 81​⋅21​=161​=25616​,θ=43​: 6427​⋅41​=25627​.

    The marginal likelihood is the sum, f(x)=∑θ​f(x∣θ)π(θ)=2561+16+27​=25644​=6411​: the overall probability of seeing three heads, averaged over what the prior believed about the coin.

    (b) The posterior. Dividing each unnormalised value by 25644​,

    π(41​∣x)=441​,π(21​∣x)=4416​=114​,π(43​∣x)=4427​,

    and 441+16+27​=1. ✓

    The change factors are posterior over prior: 1/41/44​=111​ for θ=41​, 1/24/11​=118​ for θ=21​, and 1/427/44​=1127​ for θ=43​. Two values lost probability and one gained; only θ=43​ explains three heads better than average. The factor is f(x∣θ)/f(x) because Bayes' theorem reads π(θ∣x)=π(θ)⋅f(x)f(x∣θ)​: a value is promoted exactly when it predicts the data better than the prior-averaged prediction f(x)=6411​. Check: 11/641/64​=111​, 11/641/8​=118​, 11/6427/64​=1127​. ✓

    (c) A prior that excludes a value. The unnormalised values are now 641​⋅21​=1281​, 81​⋅21​=1288​ and 6427​⋅0=0, with sum 1289​, so the posterior is

    π(41​∣x)=91​,π(21​∣x)=98​,π(43​∣x)=0.

    After a hundred heads the unnormalised values are (41​)100⋅21​, (21​)100⋅21​ and 0, in the ratio 1:2100:0. So π(21​∣x)=2100+12100​, essentially 1, while π(43​∣x) is still exactly 0 — even though θ=43​ is by far the best explanation of the data among the three, and θ=21​ predicts a hundred heads with probability 2−100.

    The lesson: the posterior is a product, and a factor of zero in the prior survives every multiplication. Data cannot revive a value the prior excluded; the mass is pushed instead onto the best of the values that were allowed. A prior that assigns zero probability to a region is therefore a permanent commitment, and should be made only when the region is genuinely impossible.

  3. 3.Proof

    Let x1​ and x2​ be two observations that are conditionally independent given θ, so that f(x1​,x2​∣θ)=f(x1​∣θ)f(x2​∣θ), and let π(θ) be a prior.

    1. (a)Prove that π(θ∣x1​,x2​)∝f(x2​∣θ)π(θ∣x1​) — updating on both observations at once gives the same posterior as updating on x1​ and then using that posterior as the prior for x2​. Say explicitly which constants you drop and why dropping them loses nothing.
    2. (b)Deduce that the order of processing is irrelevant: π(θ∣x1​,x2​)=π(θ∣x2​,x1​).
    3. (c)Verify (a) directly for the Beta-binomial model: θ∼Beta(a,b), with x1​ successes in n1​ trials followed by x2​ successes in n2​ trials.

    Solution

    (a) Proof. By the definition of the posterior,

    π(θ∣x1​,x2​)=f(x1​,x2​)f(x1​,x2​∣θ)π(θ)​=f(x1​,x2​)f(x1​∣θ)f(x2​∣θ)π(θ)​,

    using conditional independence in the numerator. The denominator f(x1​,x2​)=∫f(x1​,x2​∣θ)π(θ)dθ is a number, not a function of θ, so

    π(θ∣x1​,x2​) ∝ f(x1​∣θ)f(x2​∣θ)π(θ).

    Now apply the definition to the first observation alone: π(θ∣x1​)=f(x1​∣θ)π(θ)/f(x1​), so f(x1​∣θ)π(θ)=f(x1​)π(θ∣x1​), where f(x1​) is again a constant in θ. Substituting,

    π(θ∣x1​,x2​) ∝ f(x1​)f(x2​∣θ)π(θ∣x1​) ∝ f(x2​∣θ)π(θ∣x1​).

    The two constants dropped are f(x1​,x2​) and f(x1​), both marginal likelihoods and both free of θ. Dropping them loses nothing because a density is determined by its kernel: the left side integrates to one, so its normalising constant is forced to be 1/∫f(x2​∣θ)π(θ∣x1​)dθ, and

    π(θ∣x1​,x2​)=∫f(x2​∣θ)π(θ∣x1​)dθf(x2​∣θ)π(θ∣x1​)​,

    which is exactly Bayes' theorem with π(θ∣x1​) in the role of the prior. ■

    (b) Order. The kernel f(x1​∣θ)f(x2​∣θ)π(θ) is unchanged if the labels 1 and 2 are swapped, so the argument in (a) with the roles reversed gives π(θ∣x2​,x1​)∝f(x1​∣θ)π(θ∣x2​) with the same kernel. Two densities with the same kernel are equal, so the two posteriors coincide. Yesterday's posterior is today's prior, and it does not matter which day came first.

    (c) Beta-binomial. By Beta-binomial conjugacy, after the first experiment θ∣x1​∼Beta(a+x1​,b+n1​−x1​). Using this as the prior for the second experiment and applying the same theorem,

    θ∣x1​,x2​ ∼ Beta(a+x1​+x2​, b+n1​−x1​+n2​−x2​)=Beta(a+(x1​+x2​), b+(n1​+n2​)−(x1​+x2​)),

    which is precisely the posterior from a single experiment with x1​+x2​ successes in n1​+n2​ trials. At the level of kernels the same thing is visible at once:

    θx2​(1−θ)n2​−x2​⋅θa+x1​−1(1−θ)b+n1​−x1​−1=θa+x1​+x2​−1(1−θ)b+n1​+n2​−x1​−x2​−1.

    The updating rule “add successes to a, failures to b” is additive, and addition does not care about order.

  4. 4.Computation

    Before a new production line starts, its acceptable-item rate θ is believed to be somewhere around 21​, and this is encoded as a Beta(3,3) prior. The first 20 items include 12 acceptable ones.

    1. (a)State the posterior distribution and its mean, naming the theorem used.
    2. (b)Write the posterior mean in the form wp^​+(1−w)μ0​, identify w, and check the arithmetic.
    3. (c)Production continues and the acceptable proportion stays at exactly 53​. Find the smallest total number of items n for which the prior's weight 1−w falls below 501​.
    4. (d)How many observations is the Beta(3,3) prior worth? Repeat (a) for a Beta(30,30) prior with the same 12 of 20, and comment on the two posterior means.

    Solution

    (a) Update by conjugacy. By the theorem that the Beta family is conjugate to the binomial, add the 12 successes to a and the 8 failures to b:

    θ∣x ∼ Beta(3+12, 3+8)=Beta(15,11),E(θ∣x)=15+1115​=2615​.

    (b) Weighted-average form. With a+b=6 and n=20, the weight on the data is

    w=n+a+bn​=2620​=1310​,

    and p^​=2012​=53​, μ0​=63​=21​. Then

    wp^​+(1−w)μ0​=1310​⋅53​+133​⋅21​=136​+263​=2612​+263​=2615​. ✓

    The posterior mean sits between 21​ and 53​, ten-thirteenths of the way toward the data.

    (c) When the prior stops mattering. The prior's weight is 1−w=n+a+ba+b​=n+66​, and

    n+66​<501​⟺300<n+6⟺n>294,

    so the smallest such n is 295. (At that point the posterior mean is 3013+53​⋅295​=301180​, against a sample proportion of 301180.6​ — the prior is nudging the third decimal.) The weight is computable before any data arrive, which is what makes the prior an assumption one can report rather than hide.

    (d) Prior strength in units of data. A Beta(a,b) prior behaves like a previously seen successes and b failures, so Beta(3,3) is worth a+b=6 observations and Beta(30,30) is worth 60. With the stronger prior the posterior is Beta(42,38) with mean

    8042​=4021​,w=8020​=41​.

    The same 20 items move the weak prior's mean from 21​ to 2615​≈0.58 but the strong prior's only to 4021​=0.525: the data are outweighed three to one by sixty pseudo-observations. Both are legitimate posteriors; what differs is how much prior information the analyst has claimed, and a report should say which it was.

  5. 5.Sketch

    The prior on a success probability is θ∼Beta(2,2), with density π(θ)=6θ(1−θ) on [0,1]. Three trials are run and all three succeed.

    1. (a)Write down the likelihood L(θ) and the posterior kernel, identify the posterior as a named Beta distribution, and find its normalising constant by direct integration.
    2. (b)Find the maximum likelihood estimate, the posterior mode and the posterior mean, and put the three in order.
    3. (c)On one set of axes over 0≤θ≤1, sketch the prior, the likelihood rescaled to integrate to one, and the posterior. Justify the value of each curve at θ=0 and θ=1, the location and height of each peak, and which side of the likelihood's peak the posterior's peak lies. Explain why the posterior is zero at θ=1 even though θ=1 is the maximum likelihood value.

    Solution

    (a) Posterior kernel. Three successes in three trials have likelihood L(θ)=θ3. Multiplying by the prior,

    π(θ∣x) ∝ θ3⋅θ(1−θ)=θ4(1−θ),

    which is the kernel θ5−1(1−θ)2−1 of a Beta(5,2) — in agreement with the updating rule Beta(2+3,2+0). Integrating the kernel,

    ∫01​θ4(1−θ)dθ=51​−61​=301​,

    so the posterior density is π(θ∣x)=30θ4(1−θ).

    (b) Three summaries. The likelihood θ3 is increasing on [0,1], so the MLE is θ^=1. For the mode, differentiate the kernel: dθd​θ4(1−θ)=4θ3−5θ4=θ3(4−5θ), which is positive for θ<54​ and negative beyond, so the mode is 54​ (the Beta mode formula α+β−2α−1​=54​ agrees). The mean of a Beta(5,2) is 75​. The order is

    mean 75​ < mode 54​ < MLE 1,

    the mean pulled furthest toward the prior's centre 21​ because it averages over the whole posterior, including its long left shoulder, while the mode tracks the likelihood most closely.

    (c) The sketch. Three curves on [0,1].

    Prior 6θ(1−θ): zero at both ends, a symmetric hump peaking at θ=21​ with height 6⋅41​=23​.

    Rescaled likelihood 4θ3 (since ∫01​θ3dθ=41​): zero at θ=0, increasing and convex throughout, reaching its maximum 4 at the right-hand endpoint θ=1. Its “peak” is the endpoint — the data alone say the coin might as well always succeed.

    Posterior 30θ4(1−θ): zero at θ=0, where it leaves the axis very flat (order θ4), rises to its peak at θ=54​ of height 30⋅625256​⋅51​=6251536​≈2.46, then drops steeply to zero at θ=1. For reference it passes through height 30⋅161​⋅21​=1615​ at θ=21​, already below the prior there, and it is visibly narrower than the prior — combining two sources of information sharpens the result.

    The posterior's peak lies to the left of the likelihood's, at 54​ rather than 1, because the prior factor (1−θ) penalises values near 1 and the compromise lands between the prior's centre and the data's endpoint. It lies far closer to the data end because three certain successes are strong evidence against small θ, whereas the prior, worth only four observations, is weak.

    The posterior is exactly zero at θ=1 because the prior is: π(1)=6⋅1⋅0=0, and the posterior is the product likelihood × prior, so a zero in either factor is a zero in the product. The likelihood being maximal there cannot help — 1×0=0. This is the prior's veto at work; it is tolerable here only because a prior density that vanishes at a single endpoint still gives positive probability to every neighbourhood of it, so the posterior can pile up arbitrarily close to 1 as successes accumulate.

  6. 6.Computation

    The mean resistance θ (in ohms) of a batch of components has prior θ∼N(50,25). Individual measurements are N(θ,100) with the variance known. Twenty-one measurements give xˉ=60. Take z0.975​=1.96.

    1. (a)Compute the posterior precision, variance and mean, naming the theorem used.
    2. (b)Write the posterior mean as a weighted average of the prior mean and the sample mean, state the two weights, and check that they reproduce the answer to (a).
    3. (c)Give the 95% equal-tailed credible interval and state in words exactly what it licenses. Then describe the 95% confidence interval that the same 21 observations give, say which of the two is wider and why, and state what the confidence interval does not license.
    4. (d)How many measurements in total would make the posterior standard deviation at most 1? And what does the posterior become as the prior variance τ2→∞ — to which frequentist object does that limiting credible interval correspond?

    Solution

    (a) Precisions add. By normal-normal conjugacy the posterior is normal with precision equal to the sum of the prior and data precisions:

    τn2​1​=τ21​+σ2n​=251​+10021​=1004​+10021​=10025​=41​,

    so the posterior variance is τn2​=4 and the posterior standard deviation is 2. The posterior mean is

    μn​=τn2​(τ2μ0​​+σ2nxˉ​)=4(2550​+10021⋅60​)=4(2+563​)=4⋅573​=5292​=58.4.

    So θ∣x∼N(58.4,4).

    (b) Precision-weighted average. Each source is weighted by its share of the total precision: the data get 25/10021/100​=2521​ and the prior gets 25/1004/100​=254​. Then

    2521​⋅60+254​⋅50=251260​+25200​=251460​=58.4. ✓

    The answer is pulled 254​ of the way from the sample mean back toward the prior mean, because the twenty-one measurements carry more than five times the prior's precision.

    (c) Credible against confidence. The 95% equal-tailed credible interval is

    58.4±1.96×2=58.4±3.92=(54.48, 62.32).

    It licenses the direct statement: given the prior and these data, P(54.48<θ<62.32∣x)=0.95.

    The confidence interval treats θ as fixed and uses only the data: it is centred at xˉ=60 with half-width 1.96σ/n​=1.96×21​10​. Since 21100​>4, the standard error 21​10​ exceeds the posterior standard deviation 2, so the confidence interval is the wider of the two — the posterior has borrowed precision from the prior, and its standard deviation is smaller than both the prior's 5 and the data's 21​10​, as precision addition guarantees. What the confidence interval does not license is the sentence above: it either contains θ or it does not, and the 95% describes the coverage of the procedure across repeated samples, not this interval.

    (d) Enough data, and no prior at all. The posterior standard deviation is at most 1 exactly when the posterior precision is at least 1:

    251​+100n​≥1⟺100n​≥2524​⟺n≥96.

    So 96 measurements in total, that is 75 more.

    As τ2→∞ the prior precision τ21​→0, so the posterior precision tends to σ2n​ and the mean to xˉ: the posterior becomes N(60,21100​), and the credible interval becomes 60±1.9621​10​ — numerically identical to the classical z-interval for a normal mean with known variance. This is the case in which the two frameworks agree exactly in their arithmetic and differ only in what the interval is said to mean.

  7. 7.Computation

    A flat Beta(1,1) prior is placed on the success probability θ of a process, and 4 successes are observed in 6 independent trials. Where you need it, you may quote that the number of successes in the next m trials, under a Beta(α,β) posterior with mean θˉ, has variance mθˉ(1−θˉ)⋅α+β+1α+β+m​.

    1. (a)Find the posterior, its mean and its mode, and the maximum likelihood estimate. Two of the three summaries coincide; say which and cite the result that guarantees it.
    2. (b)Find the probability that the next trial succeeds, naming the theorem.
    3. (c)Find the probability that the next two trials both succeed, first by plugging the posterior mean into a binomial, and then correctly by sequential updating. Which is larger? Explain the difference, and show that it equals the posterior variance of θ.
    4. (d)For the number of successes in the next 3 trials, compute the plug-in variance and the posterior predictive variance, and the factor between them.

    Solution

    (a) Posterior and its summaries. By Beta-binomial conjugacy the posterior is Beta(1+4,1+2)=Beta(5,3). Its mean is 85​ and its mode is α+β−2α−1​=64​=32​. The MLE is the sample proportion 64​=32​.

    The mode and the MLE coincide, by the proposition that the posterior mode is the MLE under a flat prior: with π(θ) constant the posterior is proportional to the likelihood, and proportional functions peak at the same place. The mean does not coincide — the flat prior is worth two pseudo-observations, one success and one failure, and the mean is pulled from 32​ toward 21​, to 85​. “Flat” is not “no influence”.

    (b) The next trial. By the theorem on predicting the next Bernoulli trial, P(X~=1∣x)=E(θ∣x)=85​.

    (c) Two future trials. The plug-in answer fixes θ=85​ and multiplies: (85​)2=6425​.

    Correctly, the two trials share the same unknown θ, so use sequential updating. The first succeeds with probability 85​ by (b). Conditional on that success the posterior becomes Beta(6,3), whose mean is 96​=32​, so the second then succeeds with probability 32​. Hence

    P(both)=85​⋅32​=2410​=125​.

    Comparing over the common denominator 192: 125​=19280​ and 6425​=19275​, so the predictive answer is larger. The reason is that a first success is evidence that θ is high, which raises the chance of the second; the two future trials are positively correlated once θ is unknown, and plug-in destroys that by pretending θ is known. Formally P(both)=E(θ2∣x) while plug-in gives E(θ∣x)2, and the difference is Var(θ∣x). Check: 19280−75​=1925​, and the variance of a Beta(5,3) is (α+β)2(α+β+1)αβ​=64⋅915​=57615​=1925​. ✓

    (d) Three future trials. Plug-in: Binomial(3,85​) has variance 3⋅85​⋅83​=6445​. Posterior predictive, with α+β=8 and m=3:

    6445​⋅8+18+3​=6445​⋅911​=6455​.

    The predictive variance is larger by the factor 911​, about 22%: it carries the uncertainty about θ on top of the trial-to-trial noise. The factor tends to 1 as α+β grows, that is, as the posterior sharpens; with six observations it is far from 1.

  8. 8.Critique

    A student observes 2 successes in 4 trials and writes the following.

    Step 1. I use the flat prior Beta(1,1). It is uninformative, so the posterior Beta(3,3) reflects the data alone and no prior assumption needs reporting.

    Step 2. The posterior mode is 3+3−23−1​=21​, equal to the MLE 42​. So with a flat prior the Bayesian and frequentist answers coincide, and the 95% confidence interval I computed for θ may equally be read as a 95% credible interval: there is a 95% probability that θ lies in it.

    Step 3. The posterior mean is also 21​, so the next trial succeeds with probability 21​, and the number of successes in the next 4 trials is Binomial(4,21​), with variance 1.

    Each step contains at least one correct sentence and at least one false one.

    1. (a)Go through the three steps. For each, say what is correct, identify exactly what is false, and name the result that is being misread or overreached.
    2. (b)Repair the argument: state what can correctly be said about the prior's influence, about the interval, and about the next four trials, including the correct predictive variance.

    Solution

    (a) The flaws.

    Step 1. The posterior Beta(3,3) is correct by Beta-binomial conjugacy. The rest is false. A Beta(1,1) prior is not without influence: it is worth a+b=2 observations, and with n=4 the data's weight in the posterior mean is only w=4+24​=32​ — a third of the answer is prior. Nor is flatness a neutral choice: a prior flat in θ is not flat in logθ or in the odds 1−θθ​, so “know nothing on this scale” is a definite opinion on every other. The result being overreached is the definition of an uninformative prior — “intended to let the data speak” is an intention, not a property — and the conclusion that nothing need be reported is exactly backwards for n=4.

    Step 2. The mode computation is correct, and mode = MLE is exactly what the proposition on the posterior mode under a flat prior guarantees. But that proposition is about one point summary, and the step stretches it to “the Bayesian and frequentist answers coincide”, which it does not say and which is false in general (the mean and the intervals are different objects). The second sentence then commits the central misreading of the whole subject: a confidence interval is a statement about a procedure's long-run coverage, and once computed it either contains θ or does not. Only a credible interval — the region between the posterior's 0.025 and 0.975 quantiles — licenses “95% probability that θ lies in it”. The two intervals are numerically close for a flat prior, which is why the error so rarely shows, but closeness of numbers is not identity of meaning, and the student has not even computed the credible interval.

    Step 3. The first claim is correct: by the theorem on predicting the next Bernoulli trial, P(X~=1∣x)=E(θ∣x)=21​. The second is the plug-in error: fixing θ at 21​ treats it as known. The number of successes in the next 4 trials has the posterior predictive distribution, which is beta-binomial and over-dispersed relative to any binomial, because all four future trials share the same uncertain θ and are therefore positively correlated.

    (b) The repair.

    Prior. Report it. The posterior mean 21​ is 32​⋅21​+31​⋅21​ — here the prior and the data happen to agree, so the influence is invisible in the mean, but it is visible in the spread and would be visible in the mean for any other data. A sensitivity analysis under, say, Beta(21​,21​) (the Jeffreys prior, posterior Beta(25​,25​)) would show how little or much the choice matters.

    Interval. Either compute the credible interval from the Beta(3,3) quantiles and make the probability statement, or keep the confidence interval and make the procedural one: “an interval built this way contains θ in 95% of repeated samples”. Not both from one interval.

    Next four trials. The success probability of the next single trial is 21​. The count Y over the next m=4 trials has mean 4⋅21​=2 but variance

    mθˉ(1−θˉ)⋅α+β+1α+β+m​=4⋅21​⋅21​⋅6+16+4​=710​,

    not 1: some 43% more spread than the binomial the student wrote down. The inflation is large precisely because the posterior is wide — six pseudo-observations in all — which is when the plug-in error costs the most.

  9. 9.Computation

    A coin is tossed to decide between M0​: θ=21​ exactly, and M1​: θ∼Uniform(0,1). You may use the Beta integral ∫01​θp(1−θ)qdθ=(p+q+1)!p!q!​.

    1. (a)Four tosses all show heads. Compute P(x∣M0​), P(x∣M1​) and BF10​, and say in words what the number means.
    2. (b)Show that when all n tosses show heads, BF10​=n+12n​, and evaluate it for n=8.
    3. (c)An analyst's prior odds are 1:4 against M1​. Find the posterior probability of M1​ after four heads, and after eight heads.
    4. (d)Now suppose 3 of the 4 tosses show heads. Compute BF10​ and explain, in terms of where M1​ places its prior mass, why the evidence now runs the other way.

    Solution

    (a) Four heads. Under M0​ the four heads have probability P(x∣M0​)=(21​)4=161​. Under M1​ the marginal likelihood averages θ4 over the uniform prior:

    P(x∣M1​)=∫01​θ4dθ=5!4!0!​=51​.

    Hence

    BF10​=1/161/5​=516​=351​.

    The data are 351​ times as probable under the biased-coin model as under the fair coin: whatever odds a reader held between the two models beforehand, four straight heads multiply them by 516​ in favour of M1​. That is moderate, not decisive, evidence.

    (b) All heads in n tosses. Under M0​ the probability is 2−n; under M1​ it is ∫01​θndθ=(n+1)!n!0!​=n+11​. So

    BF10​=2−n1/(n+1)​=n+12n​,

    which for n=8 is 9256​≈28. The fair coin's prediction shrinks geometrically while the uniform alternative's shrinks only like n1​, because M1​ keeps a fixed share of its prior mass near θ=1, where an unbroken run is not surprising.

    (c) Combining with prior odds. Posterior odds are BF10​× prior odds, with prior odds P(M0​)P(M1​)​=41​.

    After four heads: posterior odds 516​⋅41​=54​, so P(M1​∣x)=1+4/54/5​=94​. The sceptic still leans, just, toward fairness.

    After eight heads: posterior odds 9256​⋅41​=964​, so P(M1​∣x)=1+64/964/9​=7364​. The prior of P(M1​)=51​ has been moved to almost 0.88. Note the division of labour: the Bayes factor is the same for every reader, and each reader supplies their own prior odds.

    (d) Three heads in four. Under M0​: (34​)(21​)4=164​=41​. Under M1​:

    P(x∣M1​)=(34​)∫01​θ3(1−θ)dθ=4⋅5!3!1!​=4⋅1206​=51​.

    So BF10​=1/41/5​=54​: the data favour the fair coin, five to four, even though three heads in four is above one half.

    The reason is where M1​ has committed its prior mass. A uniform prior spends as much of its prediction on θ near 0 or 0.1 — values under which three heads in four are nearly impossible — as on values near 43​ that fit well. Its marginal likelihood is an average over all of them, and the badly fitting values drag it down to 51​, below the point null's 41​. That is the automatic penalty for a diffuse model: it must fit substantially better than the simple one to overcome the cost of having hedged so widely. Four heads in four escaped the penalty only because the data were extreme enough that M0​'s own prediction had collapsed to 161​.

  10. 10.Proof

    Let X be the number of heads in n tosses. Under the alternative M1​ the coin's probability of heads has the uniform prior θ∼Uniform(0,1); under the null M0​, θ=21​. You may use ∫01​θp(1−θ)qdθ=(p+q+1)!p!q!​.

    1. (a)Prove that P(X=x∣M1​)=n+11​ for every x=0,1,…,n, and interpret the result.
    2. (b)Deduce that BF10​=(n+1)(xn​)2n​.
    3. (c)Let n be even and take x=2n​, the data that agree with the null as well as data possibly can. Write bn​ for BF10​ in that case. Compute b2​,b4​,b6​,b8​ exactly, prove that bn​bn+2​​=n+3n+2​, and conclude that bn​ decreases strictly with n. Interpret: what happens to the evidence about fairness as a perfectly balanced sample grows, and why is the change per toss so small?

    Solution

    (a) Every count is equally likely under the uniform alternative. The marginal likelihood of the count x under M1​ averages the binomial probability over the prior:

    P(X=x∣M1​)=∫01​(xn​)θx(1−θ)n−xdθ=(xn​)⋅(n+1)!x!(n−x)!​=x!(n−x)!n!​⋅(n+1)!x!(n−x)!​=(n+1)!n!​=n+11​.

    The factorials from the binomial coefficient cancel exactly against those from the Beta integral, and what remains does not depend on x. ■

    Interpretation: before seeing the coin, a uniform prior on θ makes every possible number of heads from 0 to n equally probable — the prior predictive distribution of the count is uniform on {0,1,…,n}. This is what “spreading the prior thinly” means concretely: M1​ has hedged across all n+1 outcomes equally, so it can never predict any single outcome with probability better than n+11​.

    (b) The Bayes factor. Under M0​, P(X=x∣M0​)=(xn​)2−n. Dividing,

    BF10​=P(X=x∣M0​)P(X=x∣M1​)​=(xn​)2−n1/(n+1)​=(n+1)(xn​)2n​.

    (As checks, x=n gives n+12n​, and n=4, x=3 gives 5⋅416​=54​.)

    (c) Balanced data. With x=2n​,

    bn​=(n+1)(n/2n​)2n​.

    The first four values: b2​=3⋅24​=32​; b4​=5⋅616​=158​; b6​=7⋅2064​=3516​; b8​=9⋅70256​=315128​.

    The ratio. Write k=2n​. The central binomial coefficients are related by

    (k+1n+2​)=(k+1)!(k+1)!(n+2)!​=(k+1)2(n+2)(n+1)​⋅k!k!n!​=(2n+2​)2(n+2)(n+1)​(kn​)=n+24(n+1)​(kn​).

    Therefore

    bn​bn+2​​=(n+3)(k+1n+2​)2n+2​⋅2n(n+1)(kn​)​=(n+3)4(n+1)​⋅4(n+1)n+2​=n+3n+2​. ■

    Check against the values: b2​b4​​=2/38/15​=54​=2+32+2​ ✓, b4​b6​​=8/1516/35​=76​ ✓, b6​b8​​=16/35128/315​=98​ ✓.

    Since n+3n+2​<1 for every n, each bn+2​<bn​: the sequence is strictly decreasing, and telescoping the ratios gives bn​=32​⋅54​⋅76​⋯n+1n​, a product of factors each below 1.

    Interpretation. BF10​<1 throughout, so perfectly balanced data always favour the fair coin, and the support for M0​ strengthens as the balanced sample grows — a Bayes factor, unlike a p-value, can accumulate evidence for a null. But it accumulates slowly: two more balanced tosses multiply the odds for fairness by only n+2n+3​, a factor that tends to 1. The reason is that M1​ also predicts balanced data tolerably well, since its prior keeps mass near θ=21​; the point null wins only by the margin between “exactly 21​” and “somewhere near 21​”, and that margin, measured by the ratio of M0​'s sharp prediction to M1​'s flat n+11​, grows like n​ rather than geometrically. Contrast an unbroken run of heads, where the Bayes factor n+12n​ grows exponentially against the null: evidence against a point null comes fast, evidence for it comes slowly.

  11. 11.Computation

    A posterior on θ∈{1,2,3} is known only up to a constant: π(θ∣x)∝g(θ) with g(1)=1, g(2)=2, g(3)=3. A Metropolis sampler at state θ proposes θ′=θ+1 or θ′=θ−1 with probability 21​ each. A proposal outside {1,2,3} has g(θ′)=0 and is therefore rejected, the chain staying where it is.

    1. (a)Write down the acceptance probability for each of the four possible moves between neighbouring states, and hence the full 3×3 transition matrix P.
    2. (b)Verify detailed balance g(i)P(i,j)=g(j)P(j,i) for every pair i=j, using only g. Explain why the normalising constant of the posterior never had to be computed, and why this is the point of the algorithm.
    3. (c)Normalise the posterior, compute πP directly, and confirm that π is stationary.
    4. (d)The chain is run for 10,000 iterations after burn-in and the posterior mean of θ is estimated by the average of the draws. Find the exact posterior mean and standard deviation. If the effective sample size is 400, what is the Monte Carlo standard error of the estimate, and what would treating the draws as independent have given?

    Solution

    (a) Acceptance probabilities and the matrix. By the Metropolis rule, a proposal θ′ is accepted with probability α=min{1,g(θ′)/g(θ)} — the ratio of posteriors, in which any constant cancels, so g may stand in for π.

    From 1: propose 2 with probability 21​, accept with min{1,12​}=1, so P(1,2)=21​; propose 0 with probability 21​, rejected. Hence P(1,1)=21​.

    From 2: propose 1, accept with min{1,21​}=21​, so P(2,1)=21​⋅21​=41​; propose 3, accept with min{1,23​}=1, so P(2,3)=21​. Hence P(2,2)=1−41​−21​=41​.

    From 3: propose 2, accept with min{1,32​}=32​, so P(3,2)=21​⋅32​=31​; propose 4, rejected. Hence P(3,3)=32​.

    P=​21​41​0​21​41​31​​021​32​​​.

    Each row sums to one, and the diagonal entries are where rejected proposals went: the chain repeats its current value, and that repeat counts as a draw.

    (b) Detailed balance with the unnormalised posterior. For the pair {1,2}: g(1)P(1,2)=1⋅21​=21​ and g(2)P(2,1)=2⋅41​=21​. ✓ For {2,3}: g(2)P(2,3)=2⋅21​=1 and g(3)P(3,2)=3⋅31​=1. ✓ For {1,3}: P(1,3)=P(3,1)=0, so both sides are 0. ✓

    Detailed balance holds for g, and multiplying every g by the same constant c multiplies both sides of each equation by c, so it holds for the normalised π=g/∑g as well. The constant was never needed because the algorithm only ever asks for ratios g(θ′)/g(θ), from which ∑θ​g(θ) cancels. Here the constant is a sum of three numbers; for a real model it is an integral over a high-dimensional parameter space with no closed form, and being able to sample without ever evaluating it is exactly what made Bayesian computation practical.

    (c) Stationarity directly. The normalised posterior is π=(61​,62​,63​)=(61​,31​,21​). Multiplying,

    (πP)1​=61​⋅21​+31​⋅41​+21​⋅0=121​+121​=61​,

    (πP)2​=61​⋅21​+31​⋅41​+21​⋅31​=121​+121​+61​=31​,

    (πP)3​=61​⋅0+31​⋅21​+21​⋅32​=61​+31​=21​.

    So πP=π: the posterior is stationary, as the Metropolis theorem promises via detailed balance. Because the chain can reach every state from every other and can stay put, it is also irreducible and aperiodic, so long-run draws are distributed as π whatever the start.

    (d) Monte Carlo error. The exact posterior mean is

    E(θ∣x)=61⋅1+2⋅2+3⋅3​=614​=37​,

    and E(θ2∣x)=61+8+27​=6, so the posterior variance is 6−949​=95​ and the standard deviation is 35​​.

    The Monte Carlo standard error of the estimated mean uses the effective sample size, not the number of iterations:

    MCSE=400​5​/3​=605​​.

    Treating all 10,000 correlated draws as independent would give 1005​/3​=3005​​, five times too small — the factor is 10,000/400​=5. Even the correct figure is only 201​ of the posterior standard deviation, so the simulation is adding little to the uncertainty the data leave; that comparison, not the raw iteration count, is what decides whether the chain has run long enough.

  12. 12.Critique

    A report on an MCMC analysis of a single parameter θ reads as follows.

    “We ran one Metropolis chain of 10,000 iterations, started at the maximum likelihood estimate so that no burn-in was needed, and kept every draw. The proposal step was made very small, and as a result 98% of proposals were accepted, which confirms that the sampler was working well. The sample standard deviation of the 10,000 draws was 3, so the Monte Carlo standard error of the posterior mean is 3/10,000​=0.03, and we report the 95% credible interval for θ as the posterior mean ±0.06.”

    Where a number is needed, suppose that the autocorrelation of this chain makes its effective sample size 100.

    1. (a)Identify every flaw in the report. For each, say precisely which two things are being confused and which check or result is being ignored.
    2. (b)Give the corrected procedure and the corrected numbers: the Monte Carlo standard error of the posterior mean, and the credible interval that should have been reported.

    Solution

    (a) The flaws, in the order they occur.

    One chain, one start, no burn-in. Starting at the MLE is a sensible choice of starting point, but it does not establish that the chain has forgotten it, and “no burn-in was needed” is asserted rather than checked. More seriously, a single chain cannot show convergence at all: a chain stuck in one region of the posterior looks perfectly stable from the inside. The ignored check is to run several chains from dispersed starting points, confirm that they mix into the same region, and compute the Gelman–Rubin statistic R^, which compares between-chain and within-chain variance and should be near 1. What is confused is stability of one chain with convergence to the posterior.

    Tiny steps and a 98% acceptance rate. A high acceptance rate is not evidence of a healthy sampler. With a very small proposal step every proposed θ′ is close to θ, so the posterior ratio is close to 1 and nearly everything is accepted — but each move is tiny, consecutive draws are nearly identical, and the chain crawls across the posterior. That is a chain with very high autocorrelation and hence a small effective sample size; the report reads a symptom of the problem as a sign of success. What is confused is accepting proposals with exploring the posterior.

    The Monte Carlo standard error. The formula s/N​ requires N independent draws. Draws from a Markov chain are not independent, and the effective number of independent draws is smaller than the number of iterations by a factor depending on the autocorrelation — here, by the report's own admission of tiny steps, a large factor. The correct denominator is ESS​. What is confused is iterations with effective sample size.

    The credible interval. This is the gravest error. A credible interval describes where θ lies, so its width is set by the posterior's own spread, the standard deviation 3. The Monte Carlo standard error describes how accurately the simulation has located the posterior mean, and it shrinks to zero as the chain runs longer, whereas the posterior's spread does not shrink at all — it reflects genuine uncertainty that no amount of computing removes. Reporting mean ±0.06 claims to know θ to within a tenth of a unit when the data themselves leave it uncertain to within several units. What is confused is uncertainty about θ with uncertainty about a simulated summary of it — the same distinction as between a posterior and its Monte Carlo estimate.

    (b) The repair.

    Procedure. Run at least three or four chains from widely dispersed starting values, discard the initial stretch of each as burn-in, and check that R^≈1 and that the trace plots overlap. Increase the proposal step until the chain moves in appreciable jumps — an acceptance rate well below 98% is the price of a chain that actually travels — and estimate the effective sample size from the autocorrelation of the pooled draws. Then report the posterior summaries with their Monte Carlo errors alongside.

    Numbers. With effective sample size 100 the Monte Carlo standard error of the posterior mean is

    100​3​=103​=0.3,

    ten times the reported 0.03. (The posterior standard deviation 3 is itself estimated from correlated draws; it remains a consistent estimate, but its own Monte Carlo error is likewise inflated.)

    The credible interval is built from the posterior spread, not from the Monte Carlo error. If the posterior is approximately normal it is

    posterior mean±1.96×3=posterior mean±5.88,

    and in general it should be read off as the 2.5% and 97.5% sample quantiles of the post-burn-in draws — nearly a hundred times wider than the ±0.06 the report gave. The honest statement is then: “the posterior mean is about such-and-such, known to a Monte Carlo accuracy of ±0.3; and θ itself lies in the interval above with posterior probability 0.95.” The two uncertainties are different in kind, and both belong in the report.

← Back to the question sheet

CalcPath·Free university mathematics

SubjectsProblem setsProgressFeedbackAbout