Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 36 ยท Projects and Practice

Practice: Bayesian Problems with Solutions

Six practice problems on conjugate updates, shrinkage, Bayes factors, posterior predictive checks, regression and decisions, each with a runnable solution you can compare to yours.

The best way to make Bayesian ideas stick is to compute with them. This page has six problems drawn from the chapters on hierarchical models, model checking and regression. Try each one on paper or in your own code first, then run the solutions below. Every solution is seeded, so your output will match.

Problems 1 to 3

Problem 1: Poisson-Gamma. A call center receives 37 calls in 10 hours on a quiet shift. With a Gamma(2, 0.5) prior on the hourly rate (shape and rate), find the posterior, its mean, a 90% interval, and the probability that the rate exceeds 4 calls per hour. Then give the posterior predictive probability of getting 0 calls in the next hour.

Problem 2: Shrinkage by hand. Three schools report average gains y = (12, 4, -2) with standard errors s = (6, 3, 6). Assume mu = 5 and tau = 4. Compute each school's posterior mean and standard deviation, and say which school moves the most and why.

Problem 3: Bayes factor. In 20 trials a learner is right 15 times. Compare M1: theta = 0.5 with M2: theta ~ Uniform(0, 1). Compute the Bayes factor and the posterior probability of M1 if the prior model odds are 1.

Python Solutions to problems 1 to 3

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Reading the results: in problem 1 the posterior mean is around 3.7 and the probability of a rate above 4 is about 0.3. In problem 2, the schools with the largest standard errors (the first and third) are shrunk the most; the middle school, measured most precisely, stays close to its raw 4. In problem 3 the Bayes factor is about 0.31, so the data are roughly three times more likely under the uniform-prior model and the posterior probability of pure guessing is about 0.24: 15 out of 20 is only moderate evidence that the learner is not guessing.

Problems 4 to 6

Problem 4: Posterior predictive check. Data: y = [0, 0, 1, 0, 9, 0, 1, 0, 0, 8] (counts of insect bites). A Poisson model with a Gamma(1, 1) prior is fitted. Compute the posterior predictive p-value for the maximum and for the proportion of zeros. What do they say?

Problem 5: Regression and prediction. Data are x = 1..8 and y = [2.1, 3.9, 6.2, 7.8, 10.1, 12.2, 13.8, 16.1]. Fit a Bayesian linear regression with known sigma = 0.3 and a Normal(0, 10^2) prior on both coefficients. Give the posterior mean slope and a 95% predictive interval for a new observation at x = 10.

Problem 6: A decision. Variant A has 18 conversions out of 120, variant B has 25 out of 130. With Beta(1, 1) priors, compute P(B > A) and the expected loss of choosing B, in percentage points.

Python Solutions to problems 4 to 6

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For problem 4 the maximum of 9 is far larger than a Poisson with rate around 1.8 would usually produce, and the data contain more zeros than that rate predicts, so both checks flag the Poisson as a poor description: the counts are zero-heavy with occasional big values (overdispersion and zero inflation). In problem 5 the line is nearly perfect, so the predictive interval at x = 10 is narrow and only a little wider than the noise. In problem 6 the posterior probability that B is better is about 0.81, and the expected loss of choosing B is about 0.5 percentage points, not negligible next to rates of 15 to 19 percent: a cautious team would collect more data.

Extra challenges

  1. Replace the Gamma(1, 1) prior in problem 4 by a negative binomial model with a grid over its two parameters and repeat the posterior predictive checks. Does the maximum check pass now?
  2. In problem 2, treat tau as unknown with the grid method from the hierarchical lesson. How different are the school estimates?
  3. For problem 5, add a quadratic term and compare the two models with WAIC and LOO. Does the extra term help?
  4. For problem 6, simulate peeking: what stopping threshold on P(B > A) keeps false winners under 5% across 10 looks? Use the approach from the A/B test project.
  5. For problem 3, change the prior on theta to Beta(10, 10) and see how the Bayes factor changes.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In problem 1, why does the posterior use Gamma(a0 + S, b0 + T)?

    Total events add to the shape and total exposure adds to the rate.
  2. 2. In problem 2, which school is shrunk the most and why?

    The weight on own data is (1/s^2) / (1/s^2 + 1/tau^2), small when s is large.
  3. 3. A Bayes factor of 0.31 for M1 against M2 means...

    A Bayes factor below 1 favors M2; 1/0.31 is about 3.
  4. 4. In problem 4, failing the check on the maximum suggests...

    Replicated maxima are much smaller than the observed one, a sign of overdispersion.

Summary

  • Conjugate updates, shrinkage weights, Bayes factors, posterior predictive checks, regression predictions and expected-loss decisions all reduce to short computations with draws or closed forms.
  • Run the check before trusting the model: the Poisson fit in problem 4 is easy to compute and wrong.
  • Whenever you can, verify an answer two ways (closed form and simulation).
  • You now have the tools to build, check and compare Bayesian models end to end.

Next: Bayesian Statistics track page