Solution (source code)

= Solution

Up to normalization, the joint posterior is
$$
p(M_{1:N},M_0,\tau^2\mid D)
\propto(\tau^2)^k
\prod_{s=1}^N
\exp\left[-\frac{(D_s-M_s)^2}{2\sigma^2}
-\frac{(M_s-M_0)^2}{2\tau^2}\right](\tau^2)^{-N/2}.
$$
A <Gibbs sampler> cycle consists of:

* independently draw every $M_s$ from the normal conditional in part a;
* draw $M_0\mid M_{1:N},\tau^2\sim N(\bar M,\tau^2/N)$;
* draw
  $$
  \tau^2\mid M_{1:N},M_0
  \sim\operatorname{Inv\text{-}Gamma}\left(
  \frac N2-k-1,\frac12\sum_s(M_s-M_0)^2\right).
  $$

Each proposal is the exact full conditional and is therefore accepted. If the cycle begins with density $p(\theta^t\mid D)$, integrating the product of the current posterior and successive conditional kernels over all overwritten coordinates leaves
$$
p(\theta^{t+1}\mid D),
$$
so a full Gibbs sweep preserves the joint posterior.