\chapter*{Exercises}
\markboth{}{EXERCISES}
\addcontentsline{toc}{chapter}{\numberline{}Exercises}

\begin{enumerate}
\item \label{qn:1} Suppose you have a random sample \exxvec. In the
  following models, derive the posterior density and name the
  posterior distribution (and its parameters).
\begin{enumerate}[(i)]
\item $f(x|\theta)=\theta^{x-1}(1-\theta )$, $x = 1,2,\ldots$ with a
  $Beta(3,2)$ prior distribution for~$\theta$.
\item $f(x|\theta)=\bigfrac{e^{-\theta}\theta^x}{x!}$, $x =
  0,1,\ldots$ with a $Exp(2)$ prior distribution for $\theta$.
\item $f(x|\theta)=\begin{pmatrix}4\\x\\ \end{pmatrix}
  \theta^x(1-\theta)^{4-x}$, $x=0,1,2,3,4$ with a $U(0,1)$ prior
  distribution for $\theta$.
\end{enumerate}

\item \label{qn:1a} Suppose that a random sample
  $x_1,x_2,\ldots,x_{10}$ is obtained from a uniform $U(0,\theta)$
  distribution. Derive the posterior density for~$\theta$ assuming a
  gamma $Ga(20,1)$ prior distribution $\theta$. Hint: this
  distribution will depend on the maximum observed $x$-value
  $x_{\max}$.

\item \label{qn:1b} Suppose that a random sample $x_1,x_2,\ldots,x_n$
  is obtained from a truncated unit exponential distribution, with
  density
\[
f(x|\theta)=\begin{cases} e^{\theta-x},&x>\theta\\
                          0,&\text{otherwise}, \end{cases}
\]
where $\theta\in\mathbb{R}$.  Derive the posterior density
for~$\theta$ assuming a normal $N(b,1/d)$ prior distribution.  Hint:
this distribution will depend on the minimum observed $x$-value
$x_{\min}$.

\item \label{qn:2} The dimensions of a component from a long
  production run vary according to a $N(\mu ,1)$ distribution, and the
  mean dimension $\mu$ varies from production run to production run
  according to a $N(10,1/4)$ distribution. From one production run 12
  components are drawn at random and their average dimension is found
  to be $10\frac{1}{3}$.  On this information what is the probability
  that the mean component dimension is at least 10?

\item \label{qn:4} A trucking company owns a large fleet of
  well-maintained trucks.  Suppose that breakdowns occur at random
  times. The owner of the company is interested in learning about the
  daily rate $\theta$ at which breakdowns occur. It is known that the
  number of breakdowns $X$ on a typical day has a Poisson distribution
  with mean~$\theta$.  The owner has some knowledge about the rate
  parameter~$\theta$ based on the observed number of breakdowns in
  previous years and expresses these prior beliefs using a $Ga(4,2)$
  distribution. The daily number of truck breakdowns are obtained on
  five consecutive days as 3, 2, 3, 1, and 2. Assuming these data are
  a random sample, determine the posterior distribution of $\theta$.

\item \label{qn:5} Suppose that the time in minutes required to serve
  a customer at a certain facility has an exponential distribution for
  which the parameter $\theta$ is unknown, and that the prior
  distribution of $\theta $ is a gamma distribution with mean 0.2 and
  standard deviation 1. If the average time required to serve a random
  sample of 20 customers is observed to be 3.8 minutes, determine the
  posterior distribution of $\theta$.

\item \label{qn:6} The following data is the time intervals (in
  minutes) between eruptions of the ``Old Faithful'' geyser. Note that
  the average time between eruptions is 67.38 minutes, that is, 1.123
  hours. If the eruptions occur randomly in time according to a
  Poisson process with a rate of $\theta$ eruptions per hour then the
  times between eruptions will form a random sample from an
  exponential distribution with rate~$\theta$. Making this assumption,
  establish a prior distribution for $\theta$ with $E(\theta) = 1$ and
  $Var(\theta) = 1$. Determine the posterior distribution for
  $\theta$.  Comment on your analysis.
\begin{center}
\begin{tabular}{|ccccccccccccccc|}
\hline
70&64&72&76&80&48&88&53&71&56&69&72&76&54&76\\
65&54&86&40&87&49&76&51&77&49&71&78&80&51&82\\
49&80&43&83&49&75&47&78&71&69&63&64&82&68&71\\
71&63&79&66&75&56&83&67&65&77&72&79&73&53&69\\
53&78&55&67&68&73&53&70&69&66&79&48&90&49&78\\
52&79&49&75&75&50&87&40&76&57&71&70&69&72&51\\
84&43&73&73&70&84&71&79&58&73&  &  &  &  &  \\
\hline
\end{tabular}
\end{center}

\item \label{qn:7} The negative binomial distribution is used to model
  scenarios in which we observe the number of independent
  (success-fail) trials needed before we see a successful trial; the
  trials must have the same success probability. Suppose \exxvec are a
  random sample from a negative binomial $NegBin(k,\theta)$
  distribution, where $k$ is known.
\begin{itemize}
\item[(i)] Verify that the congugate prior distribution is a Beta
  distribution.
\item[(ii)] Determine the choice of parameters $g$ and $h$ for the
  $Beta(g,h)$ distribution that give it maximal variance.\\ Hint:
  reparameterise the distribution in terms of its mean $m=g/(g+h)$ and
  $s=g+h$; determine the choice of $m$ and $s$ that maximises the
  variance of the distribution, and hence the choice of $g$ and $h$.
\end{itemize}
Determine the posterior distribution
for $\theta$ assuming
\begin{itemize}
\item[(iii)] vague prior knowledge;
\item[(iv)] a very large sample.
\end{itemize}

\item \label{qn:7a} The Rayleigh distribution is often used to measure
  variability in magnetic resonance imaging (MRI). Suppose \exxvec are
  a random sample from a Rayleigh $R(\theta)$ distribution. Determine
  the posterior distribution for $\theta$ assuming
\begin{enumerate}[(i)]
\item vague prior knowledge;
\item a very large sample.
\end{enumerate}

\item\label{qn:7b} The Pareto distribution is often used to model data
  in many areas, ranging from the wealth of individuals to the sizes
  of meteorites. Suppose \exxvec are a random sample from a Pareto
  $Pa(1,\theta)$ distribution. Determine the posterior distribution
  for $\theta$ assuming
\begin{enumerate}[(i)]
\item vague prior knowledge;
\item a very large sample.
\end{enumerate}

\item \label{qn:10} Suppose that a random sample of size $n=10$ from an
$N(\mu,1)$ distribution has mean $\bar x=2.5$. The prior distribution
for $\mu$ is the mixture distribution
\[
\mu\sim 0.2\,N(3.3,0.37^2)+0.8\,N(1.1,0.47^2).
\]
\begin{enumerate}[(i)]
\item Determine the posterior distribution for $\mu$. Note that,
  for this model
\begin{align*}
f_i(\xvec)&=\frac{\pi_i(\mu)\,f(\xvec|\mu)}{\pi_i(\mu|\xvec)}
\propto\frac{\sqrt{d_i}}{\sqrt{D_i}}
\exp\left\{\frac{1}{2}\left[D_iB_i^2-d_ib_i^2\right]\right\},
\end{align*}
where the constant of proportionality doesn't depend on component
prior~$i$. In order to get accurate values for the posterior weights,
you should calculate posterior component means and standard deviations
to at least 4 dp.
\item Calculate the prior and posterior mean and standard deviation.
\item Plot the prior and posterior densities for $\mu$.
\item Calculate the prior and posterior probability that $\mu$
exceeds 2.5. Comment on the effect of incorporating the data.
\end{enumerate}

\item \label{qn:10a} Consider the exponential model and gamma mixture
  prior distribution described in Example~\ref{ex:gammix}. Define your
  own \texttt{R} functions to calculate the first component weight
  ($p_1^*$) and the posterior mean and standard deviation as functions
  of the sample mean~$\bar x$.  Investigate the behaviour of these
  functions and give an intuitive explanation of their general
  properties.

\item \label{qn:13} Suppose that you have a random sample \exxvec from
  an $N(\mu,\sigma^2)$ distribution and take a $NGa(b,c,g,h)$ prior
  distribution for $(\mu,\tau)^T$, where $\tau=1/\sigma^2$. Verify
  that the posterior mean for $\mu$ is greater than the prior mean if
  and only if the sample mean is greater than the prior mean.

\item \label{qn:14} Suppose that you have a random sample %\exxvec
  from an $N(\mu,1/\tau)$ distribution and believe that the conjugate
  $NGa(b,c,g,h)$ prior distribution is appropriate for $(\mu,\tau)^T$.
  Let $t_{\nu,\alpha}$ and $\chi^2_{\nu,\alpha}$ denote the upper
  $\alpha$-points of the $t_\nu$ and $\chi^2_\nu$ distributions
  respectively.  Determine
\begin{enumerate}[(i)]
\item the equi-tailed 95\% Posterior confidence interval for $\mu$;
\item the equi-tailed 95\% Posterior confidence interval for $\tau$ (hint: if 
$W\sim Ga(a,b)$ then $2bW\sim\chi^2_{2a}$);
\item a 95\% Posterior confidence interval for $\sigma=1/\sqrt{\tau}$;
\item the 95\% Posterior HDI for $\mu$.
\item Why is it not straightforward to determine the 95\% HDI for $\tau$?
Determine the 95\% HDI for $\tau$ when the size of the random sample
is large. 
\end{enumerate}
Compare your answers with the equivalent 95\% frequentist confidence
intervals:
\begin{itemize}
\item[$\mu$:] $(\bar x-t_{n-1,0.025}s_u/\sqrt{n},\bar x+t_{n-1,0.025}s_u/\sqrt{n}\,)$
\item[$\tau$:] $(\chi^2_{n-1,0.975}/\{(n-1)s_u^2\},\chi^2_{n-1,0.025}/\{(n-1)s_u^2\})$
\item[$\sigma$:] $\left(\sqrt{\{(n-1)s_u^2\}/\chi^2_{n-1,0.025}},\,\sqrt{\{(n-1)s_u^2\}/\chi^2_{n-1,0.975}}\,\right)$.
\end{itemize}

\item \label{qn:15} Suppose that you have a random sample \exxvec from
  an $N(\mu,1/\tau)$ distribution and the sample size $n$ is
  sufficiently large that the posterior distribution for
  $(\mu,\tau)^T$ is close to its asymptotic form. Determine
\begin{enumerate}[(i)]
\item the 95\% HDI for $\mu$;
\item the 95\% HDI for $\tau$;
\item the 95\% HDI for $\sigma=1/\sqrt{\tau}$.
\end{enumerate}
Compare your answers with the equivalent 95\% frequentist confidence
intervals:
\begin{itemize}
\item[$\mu$:] $(\bar x-1.96s/\sqrt{n},\bar x+1.96s/\sqrt{n}\,)$
\item[$\tau$:] $(1/s^2-1.96\sqrt{2}/(\sqrt{n}s^2),1/s^2+1.96\sqrt{2}/(\sqrt{n}s^2)$
\item[$\sigma$:] $(s-1.96s/\sqrt{2n},s+1.96s/\sqrt{2n}\,)$.
\end{itemize}

\item\label{qn:16} Suppose that you have a random sample
  \ from a $Ga(\alpha,\lambda)$ distribution.
\begin{enumerate}[(i)]
\item Determine the asymptotic posterior distribution for
  $\thetavec=(\alpha,\lambda)^T$. Hint: you should define the maximum
  likelihood estimates $\hat\alpha$ and $\hat\lambda$ in terms of the
  equations they must satisfy, with these involving the sample mean
  $\bar x$, the geometric mean $\bar x_g$ and the digamma function
  $\psi(x)=\Gamma'(x)/\Gamma(x)$.
\item Suppose a sample of size $n=100$ gives sample mean $\bar x=3.0$
  and geometric mean $\bar x_g=2.5$ leading to maximum likelihood
  estimates $\hat\alpha=2.8983$ and $\hat\lambda=0.9661$. Determine
  the asymptotic posterior distribution for $\thetavec$. Also
  calculate the asymptotic posterior correlation between $\alpha$ and
  $\lambda$.  Hint: you will need to use the \texttt{R} functions
  \texttt{digamma} for $\psi(\cdot)$ and \texttt{trigamma} for
  $\psi'(\cdot)$.
\end{enumerate}

\item \label{qn:asym} In a calibration experiment, the times
  between successive emissions from a radioactive source were measured
  using two techniques: one ($X$) is very accurate and the other ($Y$)
  less precise. Suppose that the observed times
  $(x_1,y_1),(x_2,y_2),\ldots,(x_n,y_n)$ are a (bivariate) random
  sample from a distribution in which $X\sim Exp(\lambda)$ and
  $Y|X=x\sim N(x,\sigma^2)$. Determine the posterior distribution for
  $(\lambda,\sigma)^T$ when the sample size is large.

\item \label{qn:metrop4} Consider the Metropolis--Hastings scheme
  described in Example~\ref{ex:metrop} which simulates realisations
  from the standard normal distribution. 
\begin{enumerate}[(i)]
\item Investigate the relationship between the acceptance rate of the
  proposals ($acc$) and the lag 1 autocorrelation ($r_1$) of the
  output as the size of the innovation~$a$ takes values
  $1,2,\ldots,8$.
\item How accurate are your acceptance rates (for each $a$)? Hint:
  consider the Bernoulli observations which describe whether a
  proposal is accepted at each iteration. This model scenario was
  considered in one of your MAS2903 exercises. Use the asymptotic
  distribution of the true acceptance probability based on these
  underlying  Bernoulli observations.
\item For each value of~$a$, obtain a sample of 1000 realisations
  which are almost uncorrelated (with $|r_1|<0.02$) and assess whether
  each sample is plausibly from the standard normal distribution using
  a Q--Q plot.
\end{enumerate}

\item \label{qn:metrop5} Suppose the posterior distribution is a
  standard normal distribution, with density $\phi(\cdot)$. Construct
  a Metropolis--Hastings algorithm which samples this posterior
  distribution by using a normal random walk proposal with standard
  deviation~$k$.

\item \label{qn:metrop1} Suppose the posterior distribution is
  $\theta|\xvec\sim Ga(G,H)$ distribution, with $G$ and $H$ known.
  Suppose you want to construct a Metropolis--Hastings algorithm which
  samples this posterior distribution by using a (skewed)
  $Exp(a/\theta)$ proposal distribution, where $a>0$, i.e an exponential distribution with its parameter $\lambda=a/\theta$. 
\begin{enumerate}[(i)]
\item Determine the mean of the proposal distribution. 
\item What good feature does this proposal distribution have? 
\item Write down the steps in this Metropolis--Hastings algorithm to
  simulate realisations from the posterior distribution.
\end{enumerate}

\item \label{qn:metrop2} Suppose the posterior distribution is
  $\theta|\xvec\sim \textit{Inv-Chi}(G,H)$ distribution, with $G$ and
  $H$ known.  Suppose you want to construct a Metropolis--Hastings
  algorithm which samples this posterior distribution by using a
  (skewed) log normal $LN(\log\theta,a^2)$ proposal distribution,
  where $a>0$.
\begin{enumerate}[(i)]
\item Determine the mean of the proposal distribution. 
\item What good feature does this proposal distribution have? 
\item Write down the steps in this Metropolis--Hastings algorithm to
  simulate realisations from the posterior distribution.
\end{enumerate}

\item \label{qn:metrop8} Rework Qn~\ref{qn:metrop2} for the case where
  interest is about $\beta=\log\theta$: suppose the posterior
  distribution is $\theta|\xvec\sim \textit{Inv-Chi}(G,H)$
  distribution, with $G$ and $H$ known.
\begin{enumerate}[(i)]
\item Use equation~\eqref{eq:trans1} to determine the posterior
  density of $\beta=\log\theta$.
\end{enumerate}
Suppose you want to construct a
  Metropolis--Hastings algorithm which samples the posterior
  distribution for $\beta$ by using a normal random walk with
  variance~$a^2$.
\begin{enumerate}[(i)]
\item[(ii)] Write down the steps in this Metropolis--Hastings
  algorithm to simulate realisations from the posterior distribution.
\item[(iii)] Show that the acceptance probabilities in these two
  Metropolis--Hastings algorithms are the same, that is
  $\alpha(\theta,\theta^*)=\alpha(\beta,\beta^*)$, where
  $\beta=\log\theta$ and $\beta^*=\log\theta^*$.
\end{enumerate}

\item \label{qn:metrop3} The Weibull distribution is commonly used to
  model lifetime data. Suppose we have a random sample \exxvec from a
  Weibull $Wei(\beta,\lambda)$ distribution, with parameters $\beta>0$
  and $\lambda>0$.  Suppose that the prior distribution has $\beta\sim
  Ga(a,b)$ and $\lambda\sim Ga(c,d)$, independently, for known values
  $a$, $b$, $c$ and $d$.
\begin{enumerate}[(i)]
\item Determine the posterior density for $(\beta,\lambda)^T$ up to a
  multiplicative constant.
\item Determine the posterior conditional densities for
  $\beta|\lambda$ and $\lambda|\beta$. 
\item Write down the steps in a Metropolis--Hastings algorithm to
  simulate realisations from the posterior distribution. Your
  algorithm should have separate steps for each parameter and use
  normal random walks with variances $\Sigma_\beta$ and
  $\Sigma_\lambda$ respectively.
\end{enumerate}

\item \label{qn:metrop6} The von Mises distribution is commonly used
  to model circular data, that is, data on the circle such as wind
  directions. Suppose we have a random sample \exxvec from a von Mises
  $vM(\mu,\lambda)$ distribution with mean direction $\mu\in(0,2\pi)$
  and concentration parameter $\lambda>0$. Suppose that the prior
  distribution has $\mu \sim U(0,2\pi)$ and $\lambda \sim Ga(g,h)$,
  independently, for known values of $g$ and $h$.
\begin{enumerate} [(i)]
\item Determine the posterior density for $(\mu,\lambda)^T$ up to a
  multiplicative constant.
\item Determine the posterior densities for $\mu|\lambda$ and
  $\lambda|\mu$.
\item Write down the steps in Metropolis-Hastings algorithm to
  simulate realisations from the posterior distribution. Your
  algorithm should have separate steps for each parameter and use
  normal random walks with variances $\Sigma_\mu$ and $\Sigma_\lambda$
  respectively.
\item Using the trigonometric identities $\cos(A-B)=\cos A\cos B+\sin
  A\sin B$ and $A\cos x+B\sin x=C\cos(x-D)$ where $C=\sqrt{A^2+B^2}$
  and $D=\arctan(B/A)$, show that the update for $\mu$ can be achieved
  using a (more efficient) Gibbs step.
\end{enumerate}

\item \label{qn:metrop7} A drug company wants to assess the level of
  side effects from a new drug. A random sample of $n$ people are
  given the drug and note is taken on the dose level ($X$) and whether
  they suffer side effects ($Y=1$ if yes and $Y=0$ if no). It is
  decided that the relationship between dose level and side effects
  can be described using a linear probit regression model in which
\[
Pr(Y=1|X=x)=\Phi(\beta+\theta x),
\]
where $\Phi(\cdot)$ is the standard normal distribution function.
Suppose that the prior distribution has $\beta\sim N(a,1/b^2)$ and
$\theta\sim N(c,1/d^2)$, independently for known values of $a$, $b$,
$c$ and $d$.
\begin{enumerate} [(i)]
\item Using the data $(y_1,x_1),\ldots,(y_n,x_n)$, determine the
  likelihood function $f(\yvec|\beta,\theta)$ and hence the posterior
  density for $(\beta,\theta)^T$ up to a multiplicative constant.
\item Determine the posterior densities for $\beta|\theta$ and
  $\theta|\beta$.
\item Write down the steps in Metropolis-Hastings algorithm to
  simulate realisations from the posterior distribution. Your
  algorithm should have separate steps for each parameter and use
  normal random walks with variances $\Sigma_\beta$ and
  $\Sigma_\theta$ respectively.
\end{enumerate}

\end{enumerate}

