Practice / Probability for ML
Bayesian inference: conjugate priors, posterior means and MAP estimates
Before you start
Maximum likelihood gives one number for a parameter. Bayesian inference gives a distribution: start with a prior density for the parameter, multiply by the likelihood of the data, and normalize. For a few prior and likelihood pairs, called conjugate, the posterior is in the same family as the prior, and the update is just arithmetic on the parameters. These ten problems work through the three conjugate pairs that matter most in practice, the Beta for a probability, the Gamma for a Poisson rate and the normal for a mean, then connect the posterior to the estimates that machine learning actually uses: the MAP estimate, which turns a Gaussian prior into ridge regression, and the posterior mean, which turns a uniform prior into add-one smoothing.
- Bayes' rule for a parameter. : posterior is proportional to likelihood times prior. The missing constant makes the posterior integrate to , and it does not depend on .
- Beta. on has density proportional to . Mean , mode for , and variance . is the uniform prior.
- Beta and binomial. With prior and successes in trials, the posterior is .
- Gamma and Poisson. , with shape and rate , has density proportional to on , mean , mode for and variance . With Poisson counts adding up to , the posterior is .
- Normal mean, known variance. With prior and observations from with mean , precisions add: the posterior precision is , and the posterior mean is the precision-weighted average of and .
- Estimates. The MAP estimate is the posterior mode, the maximizer of . With a uniform prior it is the maximum-likelihood estimate. The posterior mean minimizes the expected squared error.
- Dirichlet. is the Beta for categories, with mean . With category counts the posterior is .
Builds on: Bayes' rule and conditional probability, Maximum likelihood estimation: derivations by hand
Problems
- ·
A new feature is shown to users and of them use it. With a uniform prior on the adoption rate , find the posterior, its mean and its MAP estimate, and compare them with the maximum-likelihood estimate.
- ·
A click-through rate has prior . In impressions there are clicks. Find the posterior mean, and show it is a weighted average of the prior mean and the observed rate. What is the weight on the prior?
- ··
Find the standard deviation of the prior and of the posterior from Problem 2.
- ··
A conversion rate has prior . On day 1, of visitors convert; on day 2, of . Update one day at a time, then all at once, and compare. Give the final posterior mean and MAP estimate.
- ··
A service's errors per hour are Poisson with rate , and the prior is (shape , rate ). Over hours the counts are . Find the posterior, its mean, mode and standard deviation, and compare with the MLE.
- ··
A sensor's true offset has prior . Four readings, each , have mean . Find the posterior mean and standard deviation of and a credible interval.
- ···
Take with and prior . Show that the MAP estimate is ridge regression with . Then compute it for , and
- ··
With the posterior of Problem 1, find the probability that the next user adopts the feature, and the probability that the next two both do.
- ··
An expert says a defect rate is "about , give or take ". Find the Beta prior with mean and standard deviation , then update it with defects in items and give the posterior mean.
- ···
A tiny language model sees the words cat, dog and eel , and times. Find the posterior mean of the word probabilities under a uniform prior, and compare it with the maximum-likelihood estimate.
Answers
- Posterior , mean , MAP , which equals the MLE
- Posterior mean , with weight on the prior
- Prior sd , posterior sd
- Both routes give , with mean and MAP
- Posterior : mean , mode , sd ; the MLE is
- Posterior , sd ; credible interval
- , ridge with
- ,
- Prior ; posterior with mean
- Posterior mean ; the MLE gives dog probability
Worked solutions
Problem 1
A new feature is shown to users and of them use it. With a uniform prior on the adoption rate , find the posterior, its mean and its MAP estimate, and compare them with the maximum-likelihood estimate.
- Prior , , The uniform density on is .
- Posterior Add the successes to and the failures to .
- Mean .
- MAP The mode .
- Posterior , mean , MAP , which equals the MLE A flat prior adds nothing to the log-likelihood, so the posterior peaks where the likelihood does. The mean sits higher because the posterior is skewed to the right.
Problem 2
A click-through rate has prior . In impressions there are clicks. Find the posterior mean, and show it is a weighted average of the prior mean and the observed rate. What is the weight on the prior?
- Posterior impressions were not clicked.
- Posterior mean .
- Prior mean ; observed rate The two things being averaged.
- Holds with : the two terms are and .
- Posterior mean , with weight on the priorThe prior counts as pseudo-observations against real ones. As grows, and the data take over.
Problem 3
Find the standard deviation of the prior and of the posterior from Problem 2.
- Prior variance .
- Prior standard deviation The square root.
- Posterior variance The same formula with .
- Less than half the prior variance.
- Prior sd , posterior sd The variance shrinks roughly like , the prior's pseudo-observations plus the real ones.
Problem 4
A conversion rate has prior . On day 1, of visitors convert; on day 2, of . Update one day at a time, then all at once, and compare. Give the final posterior mean and MAP estimate.
- After day 1: visitors did not convert.
- After day 2: Day 1's posterior is day 2's prior.
- All at once: of , so The same .
- Mean ; MAP and .
- Both routes give , with mean and MAP Multiplying the likelihoods of two independent batches is the same as multiplying by one and then the other. Each observation must be used exactly once (Mistake 5).
Problem 5
A service's errors per hour are Poisson with rate , and the prior is (shape , rate ). Over hours the counts are . Find the posterior, its mean, mode and standard deviation, and compare with the MLE.
- errors in hoursOnly the total and the number of hours enter the likelihood .
- Posterior The likelihood times the prior density.
- , so Shape , rate .
- Mean ; mode and .
- Posterior : mean , mode , sd ; the MLE is The prior mean is , worth hours of data, so the posterior mean sits between and , closer to the data's hours.
Problem 6
A sensor's true offset has prior . Four readings, each , have mean . Find the posterior mean and standard deviation of and a credible interval.
- Prior precision One over the prior variance.
- Data precision The mean of readings has variance .
- Posterior precision , variance Precisions add; .
- Mean Weights are the precisions over their sum: .
- Posterior , sd ; credible interval The posterior sd is smaller than both the prior's and the data's . The interval is a probability statement about given the data and the prior.
Problem 7
Take with and prior . Show that the MAP estimate is ridge regression with . Then compute it for , and
- The log-likelihood and the log-prior, with the terms that do not depend on collected in .
- Multiply by : minimize A positive factor does not move the minimizer; .
- Set the gradient to .
- ; and is .
- The inverse, with determinant .
- , ridge with Least squares gives ; the prior pulls both weights toward . A tighter prior (smaller ) means a larger .
Problem 8
With the posterior of Problem 1, find the probability that the next user adopts the feature, and the probability that the next two both do.
- Average over the posterior.
- Given the two are independent with probability ; average that over the posterior.
- The second moment of a Beta.
- With , .
- , Larger than , because (Mistake 1).
Problem 9
An expert says a defect rate is "about , give or take ". Find the Beta prior with mean and standard deviation , then update it with defects in items and give the posterior mean.
- With and : The Beta variance rewritten in terms of the mean and the total .
- , so .
- : , , .
- Posterior defects and good items.
- Prior ; posterior with mean The expert's opinion is worth items, so items at a rate of move the mean from only to .
Problem 10
A tiny language model sees the words cat, dog and eel , and times. Find the posterior mean of the word probabilities under a uniform prior, and compare it with the maximum-likelihood estimate.
- Posterior Add each count to its parameter.
- Posterior mean Each parameter over their sum .
- MLE Counts over the total .
- with , The posterior mean adds one to every count and to the total.
- Posterior mean ; the MLE gives dog probability This is add-one (Laplace) smoothing. Under the MLE an unseen word has probability , so any sentence containing it has likelihood and log-likelihood .
Where this goes wrong
1. Squaring the posterior mean for two future successes
Problem 8 asks for two adoptions in a row, and two independent events multiply.
- Right so far.
- “The next two are independent, so square it.”They are independent only given . Unconditionally, a first adoption is evidence that is high, which raises the chance of the second.
- The right value is . The gap is , the posterior's own uncertainty.
2. Forgetting the N in the data's precision
Problem 6 has a prior variance of and a reading variance of , and the two variances are the numbers on the page.
- Prior precision Right so far.
- “The data precision is .”That is the precision of a single reading. The mean of readings has variance , so the data precision is .
- Posterior variance , mean The right answer is variance and mean . Dropping treats four readings as one, so the posterior stays too close to the prior and too wide (sd instead of ).
3. Swapping the Gamma updates
The Poisson update adds two numbers to the two Gamma parameters, and it is easy to pair them the wrong way.
- Prior , errors in hoursRight so far: Problem 5.
- “Add the hours to the shape and the errors to the rate.”The likelihood is : the count is a power of , which belongs with the shape, and the exposure multiplies in the exponent, which belongs with the rate.
- Posterior , mean The right posterior is with mean . A rate of errors an hour is below both the prior mean and the observed , which a posterior mean can never be.
4. Inverting the ridge penalty
Problem 7's prior variance is and the noise variance is , and the ratio is easy to write upside down.
- Minimize Right so far.
- “.”Multiplying through by leaves in front of . A tight prior, small , has to mean a strong penalty.
- , so With the answer is . The inverted ratio makes a confident prior shrink the weights less than a vague one would.
5. Counting the first batch twice
In Problem 4, day 1's posterior becomes day 2's prior, and then all the data so far are used for the update.
- After day 1: Right so far.
- “Update with everything seen: of .”Day 1's of is already inside . The update should use only the new data, of .
- Posterior , mean The right posterior is , mean . Counting data twice also makes the posterior too confident: sd instead of .
Print this set: bayesian-inference-and-conjugate-priors.pdf (problems, answers, and worked solutions on separate pages).
More at eulerpad.app. This is one of 82 free practice sets with worked solutions at eulerpad.app/practice, alongside guides to the topics behind them.
EulerPad, the app, reads your handwritten math on iPad, iPhone and Android and checks it line by line. It's free to start.
This set is also Chapter 23 of Probability & Statistics for Machine Learning, a 250-problem workbook with worked solutions: eulerpad.app/workbooks