Practice / Probability for ML

Bayesian inference: conjugate priors, posterior means and MAP estimates

Ten problems on Bayesian inference for a continuous parameter: a Beta posterior from a uniform prior and its MAP estimate, the posterior mean as a weighted average of the prior mean and the data, how the posterior narrows, updating one batch at a time, a Gamma prior on a Poisson error rate, the mean of a normal with known variance and its 95% credible interval, MAP regression with a Gaussian prior as ridge regression, the chance of the next two successes, a Beta prior built from a mean and a standard deviation, and add-one smoothing as a Dirichlet posterior mean, with worked solutions and the mistakes of squaring the posterior mean, forgetting the N in the data's precision, swapping the Gamma updates, inverting the ridge penalty and counting the first batch twice.

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. p(θ∣data)∝p(data∣θ) p(θ)p(\theta \mid \text{data}) \propto p(\text{data} \mid \theta)\,p(\theta): posterior is proportional to likelihood times prior. The missing constant makes the posterior integrate to 11, and it does not depend on θ\theta.
  • Beta. Beta⁡(α,β)\operatorname{Beta}(\alpha, \beta) on 0<θ<10 < \theta < 1 has density proportional to θα−1(1−θ)β−1\theta^{\alpha - 1}(1 - \theta)^{\beta - 1}. Mean αα+β\dfrac{\alpha}{\alpha + \beta}, mode α−1α+β−2\dfrac{\alpha - 1}{\alpha + \beta - 2} for α,β>1\alpha, \beta > 1, and variance αβ(α+β)2(α+β+1)\dfrac{\alpha\beta}{(\alpha + \beta)^2(\alpha + \beta + 1)}. Beta⁡(1,1)\operatorname{Beta}(1, 1) is the uniform prior.
  • Beta and binomial. With prior Beta⁡(a,b)\operatorname{Beta}(a, b) and kk successes in NN trials, the posterior is Beta⁡(a+k,b+N−k)\operatorname{Beta}(a + k, b + N - k).
  • Gamma and Poisson. Gamma⁡(α,β)\operatorname{Gamma}(\alpha, \beta), with shape α\alpha and rate β\beta, has density proportional to λα−1e−βλ\lambda^{\alpha - 1}e^{-\beta\lambda} on λ>0\lambda > 0, mean α/β\alpha/\beta, mode (α−1)/β(\alpha - 1)/\beta for α≥1\alpha \ge 1 and variance α/β2\alpha/\beta^2. With NN Poisson counts adding up to SS, the posterior is Gamma⁡(α+S,β+N)\operatorname{Gamma}(\alpha + S, \beta + N).
  • Normal mean, known variance. With prior μ∼N(μ0,τ02)\mu \sim \mathcal N(\mu_0, \tau_0^2) and NN observations from N(μ,σ2)\mathcal N(\mu, \sigma^2) with mean xˉ\bar x, precisions add: the posterior precision is 1/τ02+N/σ21/\tau_0^2 + N/\sigma^2, and the posterior mean is the precision-weighted average of μ0\mu_0 and xˉ\bar x.
  • Estimates. The MAP estimate is the posterior mode, the maximizer of log⁡p(data∣θ)+log⁡p(θ)\log p(\text{data} \mid \theta) + \log p(\theta). With a uniform prior it is the maximum-likelihood estimate. The posterior mean minimizes the expected squared error.
  • Dirichlet. Dirichlet⁡(α1,…,αK)\operatorname{Dirichlet}(\alpha_1, \dots, \alpha_K) is the Beta for KK categories, with mean αj/∑iαi\alpha_j/\sum_i \alpha_i. With category counts njn_j the posterior is Dirichlet⁡(α1+n1,…,αK+nK)\operatorname{Dirichlet}(\alpha_1 + n_1, \dots, \alpha_K + n_K).

Builds on: Bayes' rule and conditional probability, Maximum likelihood estimation: derivations by hand

Problems

  1. ·

    A new feature is shown to 1212 users and 33 of them use it. With a uniform prior on the adoption rate θ\theta, find the posterior, its mean and its MAP estimate, and compare them with the maximum-likelihood estimate.

  2. ·

    A click-through rate has prior Beta⁡(6,14)\operatorname{Beta}(6, 14). In 3030 impressions there are 1818 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?

  3. ··

    Find the standard deviation of the prior Beta⁡(6,14)\operatorname{Beta}(6, 14) and of the posterior Beta⁡(24,26)\operatorname{Beta}(24, 26) from Problem 2.

  4. ··

    A conversion rate has prior Beta⁡(2,2)\operatorname{Beta}(2, 2). On day 1, 44 of 1010 visitors convert; on day 2, 77 of 1010. Update one day at a time, then all at once, and compare. Give the final posterior mean and MAP estimate.

  5. ··

    A service's errors per hour are Poisson with rate λ\lambda, and the prior is Gamma⁡(3,2)\operatorname{Gamma}(3, 2) (shape 33, rate 22). Over 55 hours the counts are 3,1,4,0,23, 1, 4, 0, 2. Find the posterior, its mean, mode and standard deviation, and compare with the MLE.

  6. ··

    A sensor's true offset μ\mu has prior N(50,16)\mathcal N(50, 16). Four readings, each N(μ,36)\mathcal N(\mu, 36), have mean xˉ=56\bar x = 56. Find the posterior mean and standard deviation of μ\mu and a 95%95\% credible interval.

  7. ···

    Take y=Xw+εy = Xw + \varepsilon with ε∼N(0,σ2I)\varepsilon \sim \mathcal N(0, \sigma^2 I) and prior w∼N(0,τ2I)w \sim \mathcal N(0, \tau^2 I). Show that the MAP estimate is ridge regression with λ=σ2/τ2\lambda = \sigma^2/\tau^2. Then compute it for σ2=1\sigma^2 = 1, τ2=12\tau^2 = \tfrac12 and

    X=[100111],y=[124].X = \begin{bmatrix} 1 & 0 \\ 0 & 1 \\ 1 & 1 \end{bmatrix}, \qquad y = \begin{bmatrix} 1 \\ 2 \\ 4 \end{bmatrix}.
  8. ··

    With the posterior Beta⁡(4,10)\operatorname{Beta}(4, 10) of Problem 1, find the probability that the next user adopts the feature, and the probability that the next two both do.

  9. ··

    An expert says a defect rate is "about 0.20.2, give or take 0.10.1". Find the Beta prior with mean 0.20.2 and standard deviation 0.10.1, then update it with 55 defects in 1010 items and give the posterior mean.

  10. ···

    A tiny language model sees the words cat, dog and eel 55, 00 and 33 times. Find the posterior mean of the word probabilities under a uniform Dirichlet⁡(1,1,1)\operatorname{Dirichlet}(1, 1, 1) prior, and compare it with the maximum-likelihood estimate.

Worked solutions

Problem 1

A new feature is shown to 1212 users and 33 of them use it. With a uniform prior on the adoption rate θ\theta, find the posterior, its mean and its MAP estimate, and compare them with the maximum-likelihood estimate.

  1. Prior Beta⁡(1,1)\operatorname{Beta}(1, 1), k=3k = 3, N=12N = 12The uniform density on (0,1)(0, 1) is θ0(1−θ)0\theta^0(1 - \theta)^0.
  2. Posterior Beta⁡(1+3,1+9)=Beta⁡(4,10)\operatorname{Beta}(1 + 3, 1 + 9) = \operatorname{Beta}(4, 10)Add the successes to α\alpha and the failures to β\beta.
  3. Mean 414=27≈0.286\tfrac{4}{14} = \tfrac27 \approx 0.286α/(α+β)\alpha/(\alpha + \beta).
  4. MAP 4−114−2=312=0.25\tfrac{4 - 1}{14 - 2} = \tfrac{3}{12} = 0.25The mode (α−1)/(α+β−2)(\alpha - 1)/(\alpha + \beta - 2).
  5. Posterior Beta⁡(4,10)\operatorname{Beta}(4, 10), mean 27≈0.286\tfrac27 \approx 0.286, MAP 0.250.25, which equals the MLE k/N=0.25k/N = 0.25A 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 Beta⁡(6,14)\operatorname{Beta}(6, 14). In 3030 impressions there are 1818 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?

  1. Posterior Beta⁡(6+18,14+12)=Beta⁡(24,26)\operatorname{Beta}(6 + 18, 14 + 12) = \operatorname{Beta}(24, 26)1212 impressions were not clicked.
  2. Posterior mean 2450=0.48\tfrac{24}{50} = 0.48α/(α+β)\alpha/(\alpha + \beta).
  3. Prior mean 620=0.3\tfrac{6}{20} = 0.3; observed rate 1830=0.6\tfrac{18}{30} = 0.6The two things being averaged.
  4. a+ka+b+N=w aa+b+(1−w) kN\dfrac{a + k}{a + b + N} = w\,\dfrac{a}{a + b} + (1 - w)\,\dfrac{k}{N}Holds with w=a+ba+b+Nw = \dfrac{a + b}{a + b + N}: the two terms are aa+b+N\dfrac{a}{a + b + N} and ka+b+N\dfrac{k}{a + b + N}.
  5. Posterior mean 0.48=0.4⋅0.3+0.6⋅0.60.48 = 0.4 \cdot 0.3 + 0.6 \cdot 0.6, with weight w=2050=0.4w = \tfrac{20}{50} = 0.4 on the priorThe prior counts as a+b=20a + b = 20 pseudo-observations against 3030 real ones. As NN grows, w→0w \to 0 and the data take over.

Problem 3

Find the standard deviation of the prior Beta⁡(6,14)\operatorname{Beta}(6, 14) and of the posterior Beta⁡(24,26)\operatorname{Beta}(24, 26) from Problem 2.

  1. Prior variance 6⋅14202⋅21=848400=0.01\dfrac{6 \cdot 14}{20^2 \cdot 21} = \dfrac{84}{8400} = 0.01αβ/((α+β)2(α+β+1))\alpha\beta/((\alpha + \beta)^2(\alpha + \beta + 1)).
  2. Prior standard deviation 0.10.1The square root.
  3. Posterior variance 24⋅26502⋅51=624127,500\dfrac{24 \cdot 26}{50^2 \cdot 51} = \dfrac{624}{127{,}500}The same formula with α+β=50\alpha + \beta = 50.
  4. ≈0.004894\approx 0.004894Less than half the prior variance.
  5. Prior sd 0.10.1, posterior sd ≈0.070\approx 0.070The variance shrinks roughly like 1/(α+β)1/(\alpha + \beta), the prior's 2020 pseudo-observations plus the 3030 real ones.

Problem 4

A conversion rate has prior Beta⁡(2,2)\operatorname{Beta}(2, 2). On day 1, 44 of 1010 visitors convert; on day 2, 77 of 1010. Update one day at a time, then all at once, and compare. Give the final posterior mean and MAP estimate.

  1. After day 1: Beta⁡(2+4,2+6)=Beta⁡(6,8)\operatorname{Beta}(2 + 4, 2 + 6) = \operatorname{Beta}(6, 8)66 visitors did not convert.
  2. After day 2: Beta⁡(6+7,8+3)=Beta⁡(13,11)\operatorname{Beta}(6 + 7, 8 + 3) = \operatorname{Beta}(13, 11)Day 1's posterior is day 2's prior.
  3. All at once: 1111 of 2020, so Beta⁡(2+11,2+9)\operatorname{Beta}(2 + 11, 2 + 9)The same Beta⁡(13,11)\operatorname{Beta}(13, 11).
  4. Mean 1324≈0.542\tfrac{13}{24} \approx 0.542; MAP 1222≈0.545\tfrac{12}{22} \approx 0.545α/(α+β)\alpha/(\alpha + \beta) and (α−1)/(α+β−2)(\alpha - 1)/(\alpha + \beta - 2).
  5. Both routes give Beta⁡(13,11)\operatorname{Beta}(13, 11), with mean ≈0.542\approx 0.542 and MAP ≈0.545\approx 0.545Multiplying 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 λ\lambda, and the prior is Gamma⁡(3,2)\operatorname{Gamma}(3, 2) (shape 33, rate 22). Over 55 hours the counts are 3,1,4,0,23, 1, 4, 0, 2. Find the posterior, its mean, mode and standard deviation, and compare with the MLE.

  1. S=3+1+4+0+2=10S = 3 + 1 + 4 + 0 + 2 = 10 errors in N=5N = 5 hoursOnly the total and the number of hours enter the likelihood ∏e−λλxi∝λSe−Nλ\prod e^{-\lambda}\lambda^{x_i} \propto \lambda^S e^{-N\lambda}.
  2. Posterior ∝λ10e−5λ⋅λ2e−2λ\propto \lambda^{10}e^{-5\lambda} \cdot \lambda^{2}e^{-2\lambda}The likelihood times the prior density.
  3. =λ12e−7λ= \lambda^{12}e^{-7\lambda}, so Gamma⁡(13,7)\operatorname{Gamma}(13, 7)Shape α+S\alpha + S, rate β+N\beta + N.
  4. Mean 137≈1.857\tfrac{13}{7} \approx 1.857; mode 127≈1.714\tfrac{12}{7} \approx 1.714α/β\alpha/\beta and (α−1)/β(\alpha - 1)/\beta.
  5. Posterior Gamma⁡(13,7)\operatorname{Gamma}(13, 7): mean ≈1.857\approx 1.857, mode ≈1.714\approx 1.714, sd 13/7≈0.515\sqrt{13}/7 \approx 0.515; the MLE is S/N=2S/N = 2The prior mean is 3/2=1.53/2 = 1.5, worth 22 hours of data, so the posterior mean sits between 1.51.5 and 22, closer to the data's 55 hours.

Problem 6

A sensor's true offset μ\mu has prior N(50,16)\mathcal N(50, 16). Four readings, each N(μ,36)\mathcal N(\mu, 36), have mean xˉ=56\bar x = 56. Find the posterior mean and standard deviation of μ\mu and a 95%95\% credible interval.

  1. Prior precision 116=0.0625\tfrac{1}{16} = 0.0625One over the prior variance.
  2. Data precision Nσ2=436≈0.1111\tfrac{N}{\sigma^2} = \tfrac{4}{36} \approx 0.1111The mean of 44 readings has variance 36/4=936/4 = 9.
  3. Posterior precision 0.17360.1736, variance 5.765.76Precisions add; 1/0.1736=5.761/0.1736 = 5.76.
  4. Mean 0.36⋅50+0.64⋅56=53.840.36 \cdot 50 + 0.64 \cdot 56 = 53.84Weights are the precisions over their sum: 0.0625/0.1736=0.360.0625/0.1736 = 0.36.
  5. Posterior N(53.84,5.76)\mathcal N(53.84, 5.76), sd 2.42.4; 95%95\% credible interval 53.84±1.96⋅2.4≈(49.14,58.54)53.84 \pm 1.96 \cdot 2.4 \approx (49.14, 58.54)The posterior sd is smaller than both the prior's 44 and the data's 33. The interval is a probability statement about μ\mu given the data and the prior.

Problem 7

Take y=Xw+εy = Xw + \varepsilon with ε∼N(0,σ2I)\varepsilon \sim \mathcal N(0, \sigma^2 I) and prior w∼N(0,τ2I)w \sim \mathcal N(0, \tau^2 I). Show that the MAP estimate is ridge regression with λ=σ2/τ2\lambda = \sigma^2/\tau^2. Then compute it for σ2=1\sigma^2 = 1, τ2=12\tau^2 = \tfrac12 and

X=[100111],y=[124].X = \begin{bmatrix} 1 & 0 \\ 0 & 1 \\ 1 & 1 \end{bmatrix}, \qquad y = \begin{bmatrix} 1 \\ 2 \\ 4 \end{bmatrix}.
  1. −log⁡p(w∣y)=12σ2∥y−Xw∥2+12τ2∥w∥2+c-\log p(w \mid y) = \tfrac{1}{2\sigma^2}\lVert y - Xw\rVert^2 + \tfrac{1}{2\tau^2}\lVert w\rVert^2 + cThe log-likelihood and the log-prior, with the terms that do not depend on ww collected in cc.
  2. Multiply by 2σ22\sigma^2: minimize ∥y−Xw∥2+λ∥w∥2\lVert y - Xw\rVert^2 + \lambda\lVert w\rVert^2A positive factor does not move the minimizer; λ=σ2/τ2\lambda = \sigma^2/\tau^2.
  3. wMAP=(X⊤X+λI)−1X⊤yw_{\text{MAP}} = (X^\top X + \lambda I)^{-1}X^\top ySet the gradient −2X⊤(y−Xw)+2λw-2X^\top(y - Xw) + 2\lambda w to 00.
  4. λ=2\lambda = 2; X⊤X+2I=[4114]X^\top X + 2I = \begin{bmatrix} 4 & 1 \\ 1 & 4 \end{bmatrix} and X⊤y=[56]X^\top y = \begin{bmatrix} 5 \\ 6 \end{bmatrix}X⊤XX^\top X is [2112]\begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix}.
  5. 115[4−1−14][56]=115[1419]\tfrac{1}{15}\begin{bmatrix} 4 & -1 \\ -1 & 4 \end{bmatrix}\begin{bmatrix} 5 \\ 6 \end{bmatrix} = \tfrac{1}{15}\begin{bmatrix} 14 \\ 19 \end{bmatrix}The 2×22 \times 2 inverse, with determinant 1515.
  6. wMAP=(1415,1915)≈(0.933,1.267)w_{\text{MAP}} = (\tfrac{14}{15}, \tfrac{19}{15}) \approx (0.933, 1.267), ridge with λ=σ2/τ2=2\lambda = \sigma^2/\tau^2 = 2Least squares gives (43,73)(\tfrac43, \tfrac73); the prior pulls both weights toward 00. A tighter prior (smaller τ2\tau^2) means a larger λ\lambda.

Problem 8

With the posterior Beta⁡(4,10)\operatorname{Beta}(4, 10) of Problem 1, find the probability that the next user adopts the feature, and the probability that the next two both do.

  1. P(next)=E[θ]=414=27P(\text{next}) = E[\theta] = \tfrac{4}{14} = \tfrac27Average P(success∣θ)=θP(\text{success} \mid \theta) = \theta over the posterior.
  2. P(next two)=E[θ2]P(\text{next two}) = E[\theta^2]Given θ\theta the two are independent with probability θ2\theta^2; average that over the posterior.
  3. E[θ2]=α(α+1)(α+β)(α+β+1)E[\theta^2] = \dfrac{\alpha(\alpha + 1)}{(\alpha + \beta)(\alpha + \beta + 1)}The second moment of a Beta.
  4. =4⋅514⋅15=20210= \dfrac{4 \cdot 5}{14 \cdot 15} = \dfrac{20}{210}With α=4\alpha = 4, β=10\beta = 10.
  5. P(next)=27≈0.286P(\text{next}) = \tfrac27 \approx 0.286, P(next two)=221≈0.095P(\text{next two}) = \tfrac{2}{21} \approx 0.095Larger than (27)2≈0.082(\tfrac27)^2 \approx 0.082, because E[θ2]=(Eθ)2+Var⁡(θ)E[\theta^2] = (E\theta)^2 + \operatorname{Var}(\theta) (Mistake 1).

Problem 9

An expert says a defect rate is "about 0.20.2, give or take 0.10.1". Find the Beta prior with mean 0.20.2 and standard deviation 0.10.1, then update it with 55 defects in 1010 items and give the posterior mean.

  1. With m=αα+βm = \tfrac{\alpha}{\alpha + \beta} and n=α+βn = \alpha + \beta: Var⁡=m(1−m)n+1\operatorname{Var} = \tfrac{m(1 - m)}{n + 1}The Beta variance rewritten in terms of the mean and the total nn.
  2. 0.2⋅0.8n+1=0.01\tfrac{0.2 \cdot 0.8}{n + 1} = 0.01, so n+1=16n + 1 = 160.12=0.010.1^2 = 0.01.
  3. n=15n = 15: α=0.2⋅15=3\alpha = 0.2 \cdot 15 = 3, β=12\beta = 12α=mn\alpha = mn, β=(1−m)n\beta = (1 - m)n.
  4. Posterior Beta⁡(3+5,12+5)=Beta⁡(8,17)\operatorname{Beta}(3 + 5, 12 + 5) = \operatorname{Beta}(8, 17)55 defects and 55 good items.
  5. Prior Beta⁡(3,12)\operatorname{Beta}(3, 12); posterior Beta⁡(8,17)\operatorname{Beta}(8, 17) with mean 825=0.32\tfrac{8}{25} = 0.32The expert's opinion is worth 1515 items, so 1010 items at a rate of 0.50.5 move the mean from 0.20.2 only to 0.320.32.

Problem 10

A tiny language model sees the words cat, dog and eel 55, 00 and 33 times. Find the posterior mean of the word probabilities under a uniform Dirichlet⁡(1,1,1)\operatorname{Dirichlet}(1, 1, 1) prior, and compare it with the maximum-likelihood estimate.

  1. Posterior Dirichlet⁡(1+5,1+0,1+3)=Dirichlet⁡(6,1,4)\operatorname{Dirichlet}(1 + 5, 1 + 0, 1 + 3) = \operatorname{Dirichlet}(6, 1, 4)Add each count to its parameter.
  2. Posterior mean 111(6,1,4)\tfrac{1}{11}(6, 1, 4)Each parameter over their sum 1111.
  3. MLE 18(5,0,3)=(0.625,0,0.375)\tfrac18(5, 0, 3) = (0.625, 0, 0.375)Counts over the total 88.
  4. nj+1N+K\dfrac{n_j + 1}{N + K} with N=8N = 8, K=3K = 3The posterior mean adds one to every count and KK to the total.
  5. Posterior mean (611,111,411)≈(0.545,0.091,0.364)(\tfrac{6}{11}, \tfrac{1}{11}, \tfrac{4}{11}) \approx (0.545, 0.091, 0.364); the MLE gives dog probability 00This is add-one (Laplace) smoothing. Under the MLE an unseen word has probability 00, so any sentence containing it has likelihood 00 and log-likelihood −∞-\infty.

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.

  1. P(next)=E[θ]=27P(\text{next}) = E[\theta] = \tfrac27Right so far.
  2. “The next two are independent, so square it.”They are independent only given θ\theta. Unconditionally, a first adoption is evidence that θ\theta is high, which raises the chance of the second.
  3. P(next two)=(27)2=449≈0.082P(\text{next two}) = (\tfrac27)^2 = \tfrac{4}{49} \approx 0.082The right value is E[θ2]=221≈0.095E[\theta^2] = \tfrac{2}{21} \approx 0.095. The gap is Var⁡(θ)=221−449≈0.014\operatorname{Var}(\theta) = \tfrac{2}{21} - \tfrac{4}{49} \approx 0.014, the posterior's own uncertainty.

2. Forgetting the N in the data's precision

Problem 6 has a prior variance of 1616 and a reading variance of 3636, and the two variances are the numbers on the page.

  1. Prior precision 116\tfrac{1}{16}Right so far.
  2. “The data precision is 136\tfrac{1}{36}.”That is the precision of a single reading. The mean of 44 readings has variance 36/4=936/4 = 9, so the data precision is 4/364/36.
  3. Posterior variance 1/(116+136)≈11.081/(\tfrac{1}{16} + \tfrac{1}{36}) \approx 11.08, mean ≈51.85\approx 51.85The right answer is variance 5.765.76 and mean 53.8453.84. Dropping NN treats four readings as one, so the posterior stays too close to the prior and too wide (sd ≈3.33\approx 3.33 instead of 2.42.4).

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.

  1. Prior Gamma⁡(3,2)\operatorname{Gamma}(3, 2), S=10S = 10 errors in N=5N = 5 hoursRight so far: Problem 5.
  2. “Add the hours to the shape and the errors to the rate.”The likelihood is λSe−Nλ\lambda^S e^{-N\lambda}: the count SS is a power of λ\lambda, which belongs with the shape, and the exposure NN multiplies λ\lambda in the exponent, which belongs with the rate.
  3. Posterior Gamma⁡(3+5,2+10)=Gamma⁡(8,12)\operatorname{Gamma}(3 + 5, 2 + 10) = \operatorname{Gamma}(8, 12), mean ≈0.667\approx 0.667The right posterior is Gamma⁡(13,7)\operatorname{Gamma}(13, 7) with mean ≈1.857\approx 1.857. A rate of 0.6670.667 errors an hour is below both the prior mean 1.51.5 and the observed 22, which a posterior mean can never be.

4. Inverting the ridge penalty

Problem 7's prior variance is τ2=12\tau^2 = \tfrac12 and the noise variance is σ2=1\sigma^2 = 1, and the ratio is easy to write upside down.

  1. Minimize 12σ2∥y−Xw∥2+12τ2∥w∥2\tfrac{1}{2\sigma^2}\lVert y - Xw\rVert^2 + \tfrac{1}{2\tau^2}\lVert w\rVert^2Right so far.
  2. “λ=τ2/σ2\lambda = \tau^2/\sigma^2.”Multiplying through by 2σ22\sigma^2 leaves σ2/τ2\sigma^2/\tau^2 in front of ∥w∥2\lVert w\rVert^2. A tight prior, small τ2\tau^2, has to mean a strong penalty.
  3. λ=12\lambda = \tfrac12, so w≈(1.238,1.905)w \approx (1.238, 1.905)With λ=2\lambda = 2 the answer is (0.933,1.267)(0.933, 1.267). 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.

  1. After day 1: Beta⁡(6,8)\operatorname{Beta}(6, 8)Right so far.
  2. “Update with everything seen: 1111 of 2020.”Day 1's 44 of 1010 is already inside Beta⁡(6,8)\operatorname{Beta}(6, 8). The update should use only the new data, 77 of 1010.
  3. Posterior Beta⁡(6+11,8+9)=Beta⁡(17,17)\operatorname{Beta}(6 + 11, 8 + 9) = \operatorname{Beta}(17, 17), mean 0.50.5The right posterior is Beta⁡(13,11)\operatorname{Beta}(13, 11), mean ≈0.542\approx 0.542. Counting data twice also makes the posterior too confident: sd ≈0.085\approx 0.085 instead of ≈0.100\approx 0.100.

Print this set: bayesian-inference-and-conjugate-priors.pdf (problems, answers, and worked solutions on separate pages).