Exercise: Bayesian vs frequentist

Goal

By the end of this exercise you should be able to:

  • state, in your own words, how the frequentist and Bayesian views of probability differ,
  • state Bayes’ rule (in words or in symbols) and identify the prior, likelihood, and posterior,
  • use the working form posterior \(\propto\) likelihood \(\times\) prior,
  • update a Beta prior with counted cells by hand — add positives to \(\alpha\) and negatives to \(\beta\): \(\text{Beta}(\alpha+k,\ \beta+n-k)\),
  • read a posterior mean and a credible interval, and say what a credible interval means (and what a confidence interval does not mean),
  • explain the difference between a p-value and a posterior probability.

This is a concept day with light arithmetic. Do the by-hand parts first; the R section only confirms your numbers.

Background

Two views of probability. A frequentist treats a parameter \(p\) as a fixed unknown and probability as a long-run frequency; results are point estimates, confidence intervals, and p-values. A Bayesian treats \(p\) as uncertain and describes it with a probability distribution, updated by data.

Bayes’ rule. In words: what you believe after the data = what you believed before × how well each possibility explains the data, rescaled so the beliefs add up to 1. In symbols, for a hypothesis \(H\) (a value of \(p\)) and observed data \(D\),

\[P(H \mid D) = \frac{P(D \mid H)\,P(H)}{P(D)} \qquad\Longrightarrow\qquad \underbrace{P(H\mid D)}_{\text{posterior}} \;\propto\; \underbrace{P(D\mid H)}_{\text{likelihood}}\;\underbrace{P(H)}_{\text{prior}}.\]

The denominator \(P(D)\) does not depend on \(H\), so it is just a normaliser; the shape of the posterior is prior \(\times\) likelihood.

The Beta–binomial update (the one rule to remember). To estimate a proportion \(p\) from \(k\) positive cells out of \(n\):

  1. Likelihood: for each possible \(p\), how probable is “\(k\) positive out of \(n\)”? (The binomial count; dbinom(k, n, p) in R.) It is highest at \(p = k/n\).
  2. Prior: \(p \sim \text{Beta}(\alpha,\beta)\), a belief curve over \(p\) from 0 to 1. Read it as \(\alpha\) imagined positive cells and \(\beta\) imagined negative cells (prior pseudo-counts). \(\text{Beta}(1,1)\) is flat (“no opinion”).
  3. Posterior: add your positives to \(\alpha\) and your negatives to \(\beta\): \[p \mid \text{data} \sim \text{Beta}(\alpha + k,\ \beta + n - k).\]
  4. Summaries. Posterior mean \(= \dfrac{\alpha}{\alpha+\beta}\) (using the posterior \(\alpha,\beta\)). A 95% credible interval holds 95% of the posterior probability; you may say “there is a 95% probability that \(p\) lies inside.”

Why the tally rule works. The binomial likelihood is \(P(k\mid p) = \binom{n}{k} p^{k}(1-p)^{n-k} \propto p^{k}(1-p)^{n-k}\) and the Beta prior is \(P(p) \propto p^{\alpha-1}(1-p)^{\beta-1}\). Multiplying them adds the exponents, \(p^{(\alpha+k)-1}(1-p)^{(\beta+n-k)-1}\), which is again a Beta shape. A prior that gives a posterior in the same family is called conjugate. A tail probability such as \(P(p > 0.25\mid\text{data})\) is the area \(\int_{0.25}^{1}\text{Beta}(p;\alpha,\beta)\,dp\), which 1 - pbeta(0.25, a, b) computes.

Interpretation traps. A frequentist confidence interval is a statement about the long-run procedure, not a probability about the parameter. A p-value is \(P(\text{data this extreme}\mid H_0)\), not \(P(H_0\mid\text{data})\).

By hand

Throughout, the running example is counting marker-positive cells: \(k\) positive out of \(n\) examined.

B1. In one sentence each, contrast the frequentist and Bayesian meaning of the word “probability.” Then say which of the two is willing to write \(P(p = 0.3)\), and why.

B2. Label the three pieces of \(\text{posterior} \propto \text{likelihood} \times \text{prior}\) for the cell-counting problem: which ingredient comes from the data (your 6 of 20 cells), which comes from the earlier experiments, and which do you report at the end?

B3. You examine \(n = 20\) cells and find \(k = 6\) positive. Using a uniform prior \(\text{Beta}(1,1)\): (a) write the posterior distribution; (b) compute the posterior mean; (c) compare it to the frequentist estimate \(\hat p = k/n\). Why are they close but not identical?

B4. Repeat B3 with an informative prior \(\text{Beta}(2,8)\) (three earlier experiments suggested about 20% positive). Write the posterior and its mean. Does the estimate move up or down relative to B3, and why?

B5. Now suppose you had examined ten times as many cells, \(n = 200\) with \(k = 60\) (same fraction \(0.30\)). Redo the posterior mean under both priors \(\text{Beta}(1,1)\) and \(\text{Beta}(2,8)\). What happens to the gap between the two answers, and what general principle does this illustrate?

B6. A colleague reports a 95% confidence interval of \([0.14,\ 0.52]\) and says “so there’s a 95% probability the true fraction is between 0.14 and 0.52.” Is that interpretation correct for a confidence interval? State the correct meaning, and say under which framework the quoted sentence would be legitimate.

B7. You test whether the positive fraction exceeds 25%. A frequentist reports \(p\text{-value} = 0.08\) for \(H_0: p = 0.25\). A student concludes “there’s only an 8% chance the true fraction is 0.25 or below.” Explain what the p-value actually says, and what Bayesian quantity the student is really reaching for.

B8. (pseudo-counts) Compare \(\text{Beta}(2,8)\) with \(\text{Beta}(20,80)\) for the \(6/20\) observations in B3–B4. Both priors have mean \(0.2\). Why does the second pull the posterior mean further toward \(0.2\)? Relate your answer to prior pseudo-counts.

Explore

Do B3–B5 by hand first, then use the demo to check your numbers and to see the curves. The sliders set the prior Beta(\(\alpha,\beta\)) and the data (\(k\) positive of \(n\) cells); the presets load the lecture’s cases. The solid red curve is the posterior, with its 95% credible interval shaded; below the plot the Bayesian interval is drawn next to the frequentist \(k/n\) with its usual 95% confidence interval.1 The shaded area under the posterior is the part to the right of the “shade p >” slider (0.25 at the start); untick its box to hide it.

E1. Load “6/20 flat” and then “6/20 Beta(2,8)”. Check the posterior parameters and means against your B3 and B4 answers. Which of the two intervals below the plot moves when you change the prior, and which does not? Why?

E2. Load “6/20 wrong prior” (Beta(40,10), centred at 0.8). Where does the posterior end up? Now load “60/200 wrong prior”. Has the data “won” yet? Use the readout line prior share of counts to explain.

E3. Keep the prior at Beta(2,8) and \(k/n = 6/20\), but raise \(\alpha\) and \(\beta\) to Beta(20,80) (same centre 0.2). What happens to the posterior mean and the width of the credible interval? Relate this to B8.

E4. Set a flat prior, press “no data”, then “+1 negative” five times (\(k = 0\), \(n = 5\)). Compare the frequentist interval with the Bayesian credible interval. Which one gives a sensible statement of uncertainty, and why does the other one fail?

E5. (Tail area.) With “6/20 flat”, read \(P(p > 0.25)\) from the shaded area. Switch to “6/20 Beta(2,8)”. Which prior makes you more confident that the fraction exceeds a quarter? How does this area relate to the quantity the student in B7 was reaching for?

E6. (What “95% confidence” means.) The second demo repeats a 20-cell experiment 100 times, with true rate 0.3, and draws each experiment’s 95% confidence interval.

  1. How many of the 100 intervals contain the true rate? Press “repeat 100 more experiments” a few times. What does “95%” promise, and about what? (b) Load “n 20, rate 0.05”. What goes wrong, and why? (c) Tick “also show credible intervals (flat prior)” and compare. Relate your answers to B6 and E4.

E1. Flat prior: posterior Beta(7, 15), mean 0.318, 95% credible interval about [0.146, 0.522]. With Beta(2,8): posterior Beta(8, 22), mean 0.267, interval about [0.127, 0.435]. Only the Bayesian interval moves. The frequentist estimate \(k/n = 0.30\) and its confidence interval [0.10, 0.50] use the data alone, so the prior has no way to enter them.

E2. With 6/20 the posterior is Beta(46, 24), mean 0.657 — much closer to the prior’s 0.8 than to the data’s 0.3, and its credible interval [0.54, 0.76] does not even contain 0.3. The prior carries 50 pseudo-counts against 20 real cells (prior share 71%). With 60/200 the posterior is Beta(100, 150), mean 0.40: better, but still well above 0.30, since the prior still makes up 20% of the counts. A confident prior in the wrong place needs a lot of data to overcome, which is why priors have to be defended and checked (sensitivity analysis).

E3. Beta(20,80) + 6/20 gives Beta(26, 94), mean 0.217 (versus 0.267 with Beta(2,8)), and a narrower interval, about [0.15, 0.29] instead of [0.13, 0.44]. Both priors are centred at 0.2, but Beta(20,80) is worth 100 pseudo-cells, five times the data, so it pulls the estimate almost all the way to 0.2. The strength \(\alpha+\beta\), not only the centre, decides how much a prior matters (B8).

E4. The frequentist estimate is \(0/5 = 0\) and the formula gives SE \(= 0\), so the “95% interval” collapses to the single point [0, 0] — it claims certainty from five cells. The flat-prior posterior is Beta(1, 6), mean 0.143, with credible interval about [0.004, 0.46]: small fractions are most plausible, but values up to almost one half are not ruled out. The simple \(\pm 1.96\,\text{SE}\) interval is an approximation that breaks down for small \(n\) or when \(k\) is 0 or \(n\) (better frequentist intervals exist, e.g. binom.test in R); the Bayesian posterior handles these cases without a special rule.

E5. Flat prior: the shaded area is 0.74 (posterior Beta(7, 15)). With Beta(2,8) it drops to 0.56 (posterior Beta(8, 22)), since that prior leans toward 0.2. So the flat prior makes you more confident that \(p > 0.25\), matching R2. This area, \(P(p > 0.25\mid\text{data})\) (or its complement \(P(p \le 0.25\mid\text{data}) = 0.26\) for the flat prior), is the posterior probability the student in B7 wanted; the p-value is a different quantity.

E6. (a) At the start 96 of the 100 intervals contain 0.3; repeated forever it would be 94.7%, and further batches wobble around 95. “95%” is a promise about the procedure over many experiments; any single interval either contains 0.3 or does not (B6). (b) With rate 0.05 only about 61 of 100 contain the truth (63.9% in the long run): about a third of the experiments see no positive cell at all, and their interval collapses to the point [0, 0], the E4 failure. The simple interval is an approximation that breaks down for rare events and small \(n\). (c) The flat-prior credible intervals contain the true rate 94 times in 100 here (92.5% in the long run), because a zero count still gives a wide credible interval. With \(n = 200\) at rate 0.3 both kinds contain the truth about 94–95% of the time: with plenty of data the two frameworks agree.

In R

Use R only to confirm your by-hand answers. Everything here is base R — no package needed.

The Beta–binomial update

k <- 6; n <- 20

# frequentist point estimate
p_hat <- k / n                       # 0.30

# Bayesian update, uniform prior Beta(1, 1)
a0 <- 1; b0 <- 1
a  <- a0 + k                         # posterior alpha = 7
b  <- b0 + (n - k)                   # posterior beta  = 15

a / (a + b)                          # posterior mean          -> 0.318

Credible interval and a tail probability

# 95% credible interval = 2.5% and 97.5% quantiles of the posterior
qbeta(c(0.025, 0.975), a, b)         # ~ 0.15 .. 0.52

# posterior probability that p exceeds 0.25
1 - pbeta(0.25, a, b)                # ~ 0.74

Swapping in an informative prior

a0 <- 2; b0 <- 8                     # centred near 0.2, weak
a  <- a0 + k                         # 8
b  <- b0 + (n - k)                   # 22
a / (a + b)                          # posterior mean          -> 0.267
qbeta(c(0.025, 0.975), a, b)         # narrower, shifted down

Tasks

R1. Reproduce your B3 posterior mean and add a 95% credible interval with qbeta. Confirm the mean matches \(\alpha/(\alpha+\beta)\).

R2. Reproduce your B4 informative-prior mean, and compute 1 - pbeta(0.25, a, b) under both priors. Which prior makes you more confident that \(p > 0.25\), and does that match your intuition?

R3. Reproduce B5 in R: set n <- 200; k <- 60 and recompute the posterior mean under both priors. Verify numerically that the two answers are much closer than in the \(n = 20\) case.

R4. (Seeing the shift.) Evaluate the two posterior densities on a grid and eyeball where they peak:

p <- seq(0, 1, length.out = 501)
plot(p, dbeta(p, 7, 15),  type = "l",              # uniform-prior posterior
     xlab = "p", ylab = "posterior density")
lines(p, dbeta(p, 8, 22), lty = 2)                 # informative-prior posterior
abline(v = 6/20, col = "grey")                     # frequentist estimate 0.30

Which curve sits a little to the left, and why?

Hints

Frequentist probability is a long-run frequency of repeatable events, so a fixed parameter has no distribution — a frequentist will not write \(P(p = 0.3)\). Bayesian probability is a degree of belief, so it can describe uncertainty about a fixed-but-unknown parameter. See the Wikipedia summary of the two interpretations.

Counting \(k\) positives out of \(n\) is binomial; the matching prior for the proportion is the Beta distribution, worth \(\alpha\) imagined positives and \(\beta\) imagined negatives. Pool the imagined and the real cells: add positives to \(\alpha\), negatives to \(\beta\), giving \(\text{Beta}(\alpha+k,\ \beta+n-k)\). (Optional, why it works: multiplying \(p^{\alpha-1}(1-p)^{\beta-1}\) by \(p^{k}(1-p)^{n-k}\) just adds the exponents; see “Optional: the maths” in the Background.)

The mean of \(\text{Beta}(\alpha,\beta)\) is \(\alpha/(\alpha+\beta)\). Use the posterior parameters (after adding the data), not the prior ones.

Think of \(\alpha,\beta\) as pretend prior counts. The total “evidence” is \(\alpha+\beta+n\); the prior’s share is \((\alpha+\beta)/(\alpha+\beta+n)\). When \(n\) is small the prior is a big share (it moves the answer); when \(n\) is large the data dominates and the prior fades.

A credible interval is a probability statement about the parameter (“95% probability \(p\) is inside”). A confidence interval is a statement about the procedure: repeat the experiment many times and 95% of the intervals you build would contain the true fixed \(p\). A single realised interval either contains \(p\) or not — no probability attached. See confidence interval vs credible interval.

A p-value is \(P(\text{data at least this extreme}\mid H_0)\) — the probability of the data assuming the null is true. It is not \(P(H_0\mid\text{data})\). The quantity the student wants (the probability the hypothesis is true given the data) is a posterior probability, obtainable with pbeta on the posterior.

Build the posterior parameters by adding counts, then use dbeta (density), qbeta (quantiles → credible interval), and pbeta (cumulative → tail probabilities). qbeta(c(0.025, 0.975), a, b) gives a 95% credible interval; 1 - pbeta(x, a, b) gives \(P(p > x \mid \text{data})\).

Solutions

B1. Frequentist: probability is the long-run relative frequency of an outcome over many repetitions of an experiment. Bayesian: probability is a degree of belief about a proposition, given what you know. Only the Bayesian will write \(P(p = 0.3)\), because Bayesian probability may describe uncertainty about a fixed-but-unknown parameter; for a frequentist the parameter is fixed (not random), so it has no probability distribution.

B2. The likelihood comes from the data: for each value of \(p\), how probable the observed 6 positives out of 20 are (the binomial count). The prior carries the earlier experiments: the Beta curve, \(\alpha\) imagined positives and \(\beta\) imagined negatives. What you report is the posterior, \(\text{Beta}(\alpha+6,\ \beta+14)\): prior and data pooled, rescaled to add up to 1.

B3. (a) Add successes to \(\alpha\) and failures to \(\beta\): \(\text{Beta}(1+6,\ 1+14) = \text{Beta}(7,\ 15)\). (b) Posterior mean \(= \dfrac{7}{7+15} = \dfrac{7}{22} \approx 0.318\). (c) The frequentist estimate is \(\hat p = 6/20 = 0.30\). They are close because the uniform prior carries almost no information (just one pseudo-success and one pseudo-failure). They are not identical because those pseudo-counts nudge the estimate very slightly toward \(0.5\) (the centre of the uniform prior).

B4. \(\text{Beta}(2+6,\ 8+14) = \text{Beta}(8,\ 22)\), mean \(= \dfrac{8}{30} \approx 0.267\). The estimate moves down relative to B3 (0.318 → 0.267) because the informative prior \(\text{Beta}(2,8)\) is centred near \(0.2\) (its mean is \(2/10 = 0.2\)) and pulls the posterior toward the earlier experiments. With only \(n = 20\) cells that pull is noticeable.

B5. With \(n = 200\), \(k = 60\), failures \(= 140\):

  • Uniform \(\text{Beta}(1,1)\): posterior \(\text{Beta}(61,\ 141)\), mean \(= 61/202 \approx 0.302\).
  • Informative \(\text{Beta}(2,8)\): posterior \(\text{Beta}(62,\ 148)\), mean \(= 62/210 \approx 0.295\).

The gap shrinks from \(0.318\) vs \(0.267\) (a difference of \(0.051\) at \(n=20\)) to \(0.302\) vs \(0.295\) (a difference of \(0.007\) at \(n=200\)). Principle: as data accumulates, the likelihood dominates and the choice of prior matters less — the two posteriors converge on the data estimate \(0.30\). Priors matter most when data is scarce.

B6. The interpretation is not correct for a confidence interval. Correct meaning: the interval was produced by a procedure that, over many repeated experiments, yields intervals containing the true fixed \(p\) 95% of the time. For this one realised interval \([0.14, 0.52]\), \(p\) is either inside it or not — there is no 95% probability attached. The quoted sentence would be legitimate for a credible interval (a Bayesian interval), because there \(p\) has a probability distribution.

B7. The p-value \(0.08\) is \(P(\text{data at least this extreme}\mid H_0: p = 0.25)\) — the probability of seeing data this far from the null if the null were exactly true. It says nothing directly about how probable \(H_0\) itself is. The student is reaching for a posterior probability, \(P(p \le 0.25 \mid \text{data})\), which is a Bayesian quantity: you get it from the posterior with pbeta(0.25, a, b). Confusing \(P(\text{data}\mid H_0)\) with \(P(H_0\mid\text{data})\) is the classic p-value misreading.

B8. \(\text{Beta}(2,8)\) and \(\text{Beta}(20,80)\) both have mean \(0.2\), but carry different amounts of prior information, measured by \(\alpha+\beta\). \(\text{Beta}(2,8)\) has 10 pseudo-counts against \(n = 20\) observations, giving posterior \(\text{Beta}(8,22)\) with mean \(8/30 \approx 0.267\). \(\text{Beta}(20,80)\) has 100 pseudo-counts, five times the data, giving posterior \(\text{Beta}(26,94)\) with mean \(26/120 \approx 0.217\). The stronger prior pulls further from the data fraction \(0.30\) toward \(0.2\). The width/strength of a prior, not just its centre, controls how much it moves the answer — the concentration \(\alpha+\beta\) is an effective prior sample size. By contrast, the flat \(\text{Beta}(1,1)\) prior has mean \(0.5\), so it would not isolate strength in this comparison.

R1.

k <- 6; n <- 20
a <- 1 + k; b <- 1 + (n - k)         # Beta(7, 15)
a / (a + b)                          # 0.3181818  -> matches 7/22
qbeta(c(0.025, 0.975), a, b)         # ~ 0.1459 .. 0.5218

The mean equals \(\alpha/(\alpha+\beta) = 7/22\); the 95% credible interval is roughly \([0.15, 0.52]\).

R2.

k <- 6; n <- 20

# uniform prior
a <- 1 + k; b <- 1 + (n - k)         # Beta(7, 15)
1 - pbeta(0.25, a, b)                # ~ 0.74

# informative prior Beta(2,8)
a <- 2 + k; b <- 8 + (n - k)         # Beta(8, 22)
a / (a + b)                          # 0.2667
1 - pbeta(0.25, a, b)                # ~ 0.55

The uniform prior gives a higher \(P(p > 0.25)\) (~0.74) than the informative prior (~0.55). That matches intuition: the informative prior is centred at \(0.2\), so it pulls belief below \(0.25\) and makes you less sure the fraction exceeds a quarter.

R3.

n <- 200; k <- 60

a <- 1 + k; b <- 1 + (n - k)         # Beta(61, 141)
a / (a + b)                          # 0.3020

a <- 2 + k; b <- 8 + (n - k)         # Beta(62, 148)
a / (a + b)                          # 0.2952

The two means (0.302 and 0.295) differ by under \(0.01\), far less than the \(n = 20\) gap (0.318 vs 0.267). With ten times the data the prior barely matters.

R4. The dashed curve, the \(\text{Beta}(8,22)\) posterior from the informative prior, sits slightly to the left of the solid \(\text{Beta}(7,15)\) curve, because the informative prior is centred near \(0.2\) and pulls the posterior toward smaller \(p\). The flat-prior posterior Beta(7,15) peaks exactly on the grey line at \(0.30\), the data estimate; the informative-prior posterior Beta(8,22) peaks below it, at \(0.25\), pulled toward the prior’s \(0.2\). (Optional: the peak of \(\text{Beta}(\alpha,\beta)\) sits at \((\alpha-1)/(\alpha+\beta-2)\), which gives \(6/20\) and \(7/28\).) With more data both curves would narrow and slide onto \(0.30\).

Where this leads

  • Bayesian inference in R: for non-conjugate models there is no closed-form posterior, and we sample it with MCMC (e.g. Stan / rstan, brms). The Beta–binomial here is the rare case where adding counts gives the posterior and qbeta/pbeta do the rest.
  • Hierarchical / empirical-Bayes shrinkage: the per-gene estimates in differential-expression tools (limma, DESeq2) borrow strength across genes exactly in the spirit of a prior — a hierarchical Bayesian idea, even when the software calls it something else.
  • Count distributions: the Poisson and negative binomial distributions (introduced with overdispersion in the count-data material) are the likelihoods you would pair with priors when modelling sequencing counts the Bayesian way.

Footnotes

  1. The usual (Wald) interval is \(\hat p \pm 1.96\sqrt{\hat p(1-\hat p)/n}\) with \(\hat p = k/n\); you do not need the formula.↩︎