Let $X_1, \ldots, X_n$ be a random sample from the $\mathrm{N}\left(\mu, \sigma^2\right)$ distribution. Consider the following Bayesian model:
- $f\left(\mu, \sigma^2\right)=1 / \sigma^2$ :
- ( $\mathbf{x}_i \mid \mu, \sigma$ ) ~ $\mathrm{N}\left(\mu, \sigma^2\right), i=1, \ldots, n$ independently.
Note that the prior for ( $\mu, \sigma^2$ ) is improper. That is, it is not a pdf in itself, but by obstinately applying Bayes' formula, it does yield a proper posterior pdf. In some sense it conveys the least amount of information about $\mu$ and $\sigma^2$. Let $\mathbf{x}=\left(x_1, \ldots, x_n\right)$ represent the data. The posterior pdf is given by
$$
f\left(\mu, \sigma^2 \mid \mathbf{x}\right)=\left(2 \pi \sigma^2\right)^{-n / 2} \exp \left\{-\frac{1}{2} \frac{\sum_i\left(x_i-\mu\right)^2}{\sigma^2}\right\} \frac{1}{\sigma^2} .
$$
We wish to sample from this distribution via the Gibbs sampler.
a) Show that $\left(\mu \mid \sigma^2, \mathbf{x}\right) \sim N\left(\bar{x}, \sigma^2 / n\right)$, where $\bar{x}$ is the sample mean.
b) Prove that
$$
f\left(\sigma^2 \mid \mu, \mathbf{x}\right) \propto \frac{1}{\left(\sigma^2\right)^{n / 2+1}} \exp \left(-\frac{n}{2} \frac{V_\mu}{\sigma^2}\right),
$$
where $V_\mu=\sum_i\left(x_i-\mu\right)^2 / n$ is the classical sample variance for known $\mu$. In other words, $\left(1 / \sigma^2 \mid \mu, \mathbf{x}\right) \sim \operatorname{Gamma}\left(n / 2, n V_\mu / 2\right)$.
c) Implement a Gibbs sampler to sample from the posterior distribution, taking $n=100$. Run the sampler for $10^5$ iterations. Plot the histograms of $f(\mu \mid \mathbf{x})$ and $f\left(\sigma^2 \mid \mathbf{x}\right)$ and find the sample means of these posteriors. Compare them with the classical estimates.
d) Show that the true posterior pdf of $\mu$ given the data is given by
$$
f(\mu \mid \mathbf{x}) \propto\left((\mu-\bar{x})^2+V\right)^{-n / 2},
$$
where $V=\sum_i\left(x_i-\bar{x}\right)^2 / n$. (Hint: in order to evaluate the integral
$$
f(\mu \mid \mathbf{x})=\int_0^{\infty} f\left(\mu, \sigma^2 \mid \mathbf{x}\right) d \sigma^2
$$
write it first as $(2 \pi)^{-n / 2} \int_0^{\infty} t^{n / 2-1} \exp \left(-\frac{1}{2} t c\right) d t$, where $c=n V_\mu$, by applying the change of variable $t=1 / \sigma^2$. Show that the latter integral is proportional to $c^{-n / 2}$. Finally, apply the decomposition $V_\mu=(\bar{x}-\mu)^2+V$.)