Supplementary Exercises#
Warning
This page contains solutions! We recommend attempting each problem before peeking.
1. Laplace’s Method for Binomial-Beta#
Approximate the posterior by a normal model using Laplace’s method. The likelihood is binomial \(X \mid p \sim \text{Bin}(n, p)\) and the prior is \(p \sim \text{Be}(\alpha, \beta)\) with parameters:
\(\alpha = \beta = 1\) (flat prior)
\(\alpha = \beta = 1/2\) (Jeffreys’ prior)
\(\alpha = \beta = 2\)
How well does Laplace’s method approximate the 95% credible set (CS) for \(p\)? Compare exact equi-tailed CS’s with the approximations. Use \(n = 20\) and \(X = 8\).
Solution
See 3. Laplace’s Method for the steps for Laplace’s method. Most of the code below is from there.
Our model:
This is conjugate, with an exact posterior of \(\text{Be}(\alpha + x, \beta + n - x)\).
The log-posterior (with fixed \(n\)) and one datapoint:
We could simplify further and find the mode but I’m just going to use code.
\(\alpha = \beta = 1\)
Exact posterior is \(\text{Be}(9, 13)\).
\(\alpha = \beta = \frac{1}{2}\)
Exact posterior is \(\text{Be}(8.5, 12.5)\).
\(\alpha = \beta = 2\).
Exact posterior is \(\text{Be}(10, 14)\).
The code in the hidden cell below solves all three problems.
2. Coin and Probability of Heads#
A coin is flipped an unknown number of times, represented by \( n \), and \( X \) heads are observed. We are interested in the probability of heads \( p \) and assume the following model:
The model is completed with priors on \(n\) and \( p \). Specifically, we assume \( n \) is Poisson-distributed with mean \( \lambda \) and \( p \) follows a beta distribution \( \text{Be}(\alpha, \beta) \). The priors on \( n \) and \( p \) are independent.
Assume \(X=6\) is observed. Sample from the posterior using:
Metropolis.
Gibbs.
Assume \(\alpha=\beta =20, \lambda=18\). Take as proposal beta Be(X + α, β) distribution. Note that this is an independence proposal which conveniently simplifies acceptance probability in Metropolis algorithm.
Solution
Metropolis solution (Aaron)
Our full model is:
Our binomial likelihood is:
When they say integrate out \(n\), it would have been better stated as averaging over \(n\). For a discrete distribution, we need to use a sum instead of integrating:
Then our posterior will be proportional to the Poisson * Beta distributions. We only need proportionality for the Metropolis target function.
The hint suggests a beta proposal, specifically \(\text{Be}(X + \alpha, \beta)\). This will cover the full support of the posterior for \(p\). For Metropolis-Hastings, we need to find our acceptance ratio, where \(q(\cdot)\) is our proposal, \(\pi(\cdot)\) is our target, \(p\) is our current value, and \(p_*\) is our proposed value.
Code is below.
Gibbs solution (Greg) The likelihood and priors are as follows:
We have the following:
The joint distribution of \(X\), \(p\), \(n\) is as follows:
To find the conditional distribution \(p \mid n, X = x\), we have:
which is the kernel of a \(\text{Beta}(x + \alpha, n - x + \beta)\) distribution.
To find the conditional distribution \(n \mid p, X = x\), we have:
To see what kernel this is, we first let \(Y \sim \text{Poi}(\lambda)\). Then \(f(y) = \frac{e^{-\lambda} \lambda^y}{y!}\). If \(z = y + a\), then
Thus, \(n \mid X = x, p \sim x + \text{Poi}((1 - p)\lambda)\). We set up the Gibbs sampler and have included a histogram for \(p\).
3. Amanita muscaria#
With its bright red, sometimes dinner-plate-sized caps, the fly agaric (Amanita muscaria) is one of the most striking of all mushrooms (Fig. 1a). The white warts that adorn the cap, the white gills, a well-developed ring, and the distinctive volva of concentric rings distinguish the fly agaric from all other red mushrooms. The spores of the mushroom print white, are elliptical, and have a larger axis in the range of 7 to 13 µm (Fig. 1b).
Measurements of the diameter \(X\) of spores for \(n = 51\) mushrooms are given in the following table:
Assume that the measurements are normally distributed with mean \(\mu\) and variance \(\sigma^2\), but both parameters are unknown and of interest. Suppose that the prior on \(\mu\) is normal \(N (12, 2^2)\) and the prior on \(\tau = 1/\sigma^2\) is gamma \(\text{Ga}(2, 4)\).
Develop a Gibbs sampling algorithm and find Bayes estimators of \(\mu\) and \(\tau\).
If an inverse gamma \(\text{InvGa}(4, 2)\) is placed on \(\sigma^2\), would the solution be different? Develop a Gibbs sampling for this case keeping the prior on \(\mu\) as in 1.
Develop a Metropolis algorithm for the priors as in 1. Choice of proposal distributions is up to you.
Solution
Solution provided by TA Jason Naramore.
Likelihood:
\(\mathcal{N}(\mu,\sigma^2)\)
Prior for \(\mu\):
\(\mathcal{N}(12,2^2\))
Prior for \(1/\sigma^2\) or \(\tau\):
\(\mathcal{Ga}(2,4)\)
Joint conditional:
Recognize these full conditionals are same ase those derived in GIBBS.pdf and Greg’s Office Hours 2.20.2024. See those documents for detailed steps.
\(\mu | y, \sigma^2\):
\(\tau | y, \mu\):
See hidden code below for Metropolis and PyMC answers as well.
4. Jeremy via Metropolis#
The Jeremy example was solved exactly (as a conjugate problem) and by using WinBUGS. Recall that Jeremy’s IQ test score \(X\) was modeled as normal \(N(\theta, 80)\), while the location parameter \(\theta\) had prior \(N(110, 120)\). We could think of \(\theta\) as Jeremy’s intrinsic IQ level. On his IQ test Jeremy scored \(X = 98\).
Develop a Metropolis MCMC scheme for sampling from the posterior.
Sample the proposal \(\theta_p\) from a Cauchy distribution centered at the current state of the chain \(\theta\).
theta_prop = theta + 10 * np.tan(np.pi * np.random.rand() - np.pi / 2)
Since the proposal distribution depends on \((\theta_p - \theta)^2\), it does not factor into the acceptance ratio \(\rho\). Your target is the product of the likelihood and prior and does not need to be normalized to be a distribution. Start with \(\theta = 100\) and burn in 500 simulations. Save 50,000 simulations (not including the burn-in).
Estimate the posterior mean, posterior standard deviation, find the median, and 95% equal-tailed credible set.
Plot the normalized histogram and superimpose the theoretical posterior density.
Solution
Solution to be added. Feel free to share yours on Ed Discussion!
5. Jeremy via Gibbs Sampling#
Translate the following WinBUGS code into a custom-made Gibbs Sampling program in MATLAB.
model {
x ~ dnorm(mu, tau)
pprec <- 1/120
mu ~ dnorm(110, pprec)
tau ~ dgamma(0.01, 1)
sig2 <- 1/tau
}
DATA
list(x = 98)
INITS
list(mu = 100, tau = 0.01)
Find full conditionals for \(\mu\) and \(\tau\). Compare Bayes summaries (posterior mean, median, 2.5 and 97.5 percentiles, histograms) between WinBUGS and Gibbs outputs.
Solution
Solution to be added. Feel free to share yours on Ed Discussion!
6. Gibbs with Metropolis Step (Georgia Deaths from Kidney Cancer 1985-1989 by Counties)#
When in a Gibbs sampling scheme, one or more full conditionals are not tractable, their function can be replaced by a so-called Metropolis step. An example with a Metropolis step is given below.
The data set contains death counts from Kidney Cancer for 159 Georgia counties as well as the county population size. The data are from years 1985-1989. It is of interest to estimate the death rate (per 100,000) for each county, as well as the all-Georgia death rate.
The model is Poisson \(\text{Poi}(\lambda_i n_i)\), \(i = 1, \ldots, k\) where \(n_i\) is the population size divided by 100,000 for county \(i\); here \(k = 169\). Also, \(n = n_1 + \ldots + n_k\) is the population of Georgia in units of 100,000. The model is as follows:
for specified constants \(A\) and \(B\). The joint distribution is:
Therefore,
Therefore,
The Metropolis Step for generating \(\alpha\): Proposal \(\alpha' \sim N(\alpha, \sigma^2)\). The Metropolis ratio depends on the target only,
for \(C = \beta^k \prod_{i=1}^k \lambda_i\).
Solution
7. Exponential Survival Times#
Survival times of \(n\) post-surgery patients assigned to a treatment group are given as \(T_1, \ldots, T_n\). In a control group of \(m\) post-surgery patients the survival times are \(C_1, \ldots, C_m\). The proposed model is
where \(T_i\)’s and \(C_i\)’s are assumed independent. To complete the model assume the following priors on \(\lambda\) and \(\theta\),
where gamma is parameterized via the rate parameter.
Show that the likelihood and posterior depend on \(T = \sum_{i=1}^n T_i\) and \(C = \sum_{i=1}^m C_i\).
Identify full conditionals for \([\lambda \mid \theta, T, C]\) and \([\theta \mid \lambda, T, C]\).
For \(n = 10\), \(m = 12\), \(T = 18.26\), and \(C = 26.78\), set up a Gibbs sampling scheme.
From a Gibbs sample of size 10000 estimate \(\theta\) and find a 95% equi-tailed credible set. Is the credible set containing 1? Discuss.
How would you deal with the analysis if some times are censored? Repeat the analysis if \(T_{10} = 2.12\), \(C_{11} = 1.54\), and \(C_{12} = 1.98\) are in fact censored times.
Solution
Solution to be added. Feel free to share yours on Ed Discussion! This problem is challenging, since we haven’t learned about censoring yet.
Hint: The posterior is proportional to
The above product is proportional to
when \(\theta\) is constant, and to
when \(\lambda\) is constant. Thus,
8. Testing the Effectiveness of a Seasonal Flu Shot#
Assume 30 individuals are given a flu shot at the start of winter. At the end of winter, follow up to see whether they contracted flu. Let
Suppose the 30th individual was unavailable for follow-up. Define \(y = \sum_{i=1}^{29} x_i\). If \(p\) is the probability the shot is effective, then
With the complete data (\(y\) plus \(x_{30}\)),
With a uniform prior on \(p\) and \([x_{30} \mid p] \sim \text{Ber}(p)\) the joint distribution of \([p, x_{30} \mid y]\) is proportional to \(f(y, x_{30} \mid p)\). Thus, the full conditionals are
and
If \(y = 21\), estimate \(p\) using a Gibbs sampler that uses these two full conditionals.
Solution
Solution to be added. Feel free to share yours on Ed Discussion!