\chapter{Inference for a normal population}
This chapter shows how to make inferences for the mean and variance of
a normal population using a conjugate prior distribution.  First we
need the multi-parameter version of Bayes Theorem.

\section{Bayes Theorem for many parameters}
Suppose that now the probability (density) function we used to
describe the data depends on many parameters, that is,
$f(\xvec|\thetavec)$ where $\thetavece$. After observing the data, the
likelihood function for $\thetavec$ is $f(\xvec|\thetavec)$.  Prior 
beliefs about $\thetavec$ are represented through a probability
(density) function $\pi(\thetavec)$. Therefore, using Bayes Theorem,
the posterior probability (density) function for $\thetavec$ is
\begin{align*}
\pi(\thetavec|\xvec)&=\frac{\pi(\thetavec)\,f(\xvec|\thetavec)}{f(\xvec)} \\
\intertext{where}
f(\xvec)&=
\begin{cases}
\int_\Theta\pi(\thetavec)\,f(\xvec|\thetavec)\,d\thetavec & \text{if
$\thetavec$ is continuous}, \\ \\
\sum_\Theta\pi(\thetavec)\,f(\xvec|\thetavec) & \text{if
$\thetavec$ is discrete}.
\end{cases} \\
\intertext{As in Chapter~1, this can be rewritten as}
\pi(\thetavec|\xvec)&\propto \pi(\thetavec)\times f(\xvec|\thetavec) \\
i.e.~~\text{posterior}&\propto\text{prior}\times\text{likelihood}.
\end{align*}

Next we introduce a new distribution which will be useful later on.

%%Hack for vspace
\newpage

\begin{example}
  \label{ex:student}~\\
  If $X$ has a generalised $t_a(b,c)$ distribution (see formula sheet) then show that
  $Y=(X-b)/\sqrt{c}\sim t_a\equiv t_a(0,1)$.

Recall the general result: if $X$ is a random variable with
probability density function $f_X(x)$ and $g$ is a bijective (1--1)
function then the random variable $Y=g(X)$ has probability density
function
\begin{equation}
\label{eq:trans1}
f_Y(y)=f_X\left\{g^{-1}(y)\right\}\left|\frac{d}{dy}\,g^{-1}(y)\right|.
\end{equation}

\framebox[17cm]{ \parbox[t]{16cm}{

\vspace{0.4cm}

\huge
\ding{46} \normalsize 

\textcolor{white}{
  Here we take $Y=g(X)=(X-b)/\sqrt{c}$ from which we obtain
  $X=g^{-1}(Y)=b+\sqrt{c}\,Y$. Therefore using $\eqref{eq:trans1}$ we
  have
\begin{align*}
f_Y(y)
&=f_X\left\{g^{-1}(y)\right\}\left|\frac{d}{dy}\,g^{-1}(y)\right|\\
&=f_Y\left(b+\sqrt{c}\,y\right)\times\sqrt{c}\\
&=\frac{\Gamma\left(\frac{a+1}{2}\right)}
{\sqrt{ac\pi}\,\Gamma\left(\frac{a}{2}\right)}
\left(1+\frac{y^2}{a}\right)^{-\frac{a+1}{2}}\times\sqrt{c},\quad y\in\mathbb{R}\\
&=\frac{\Gamma\left(\frac{a+1}{2}\right)}
{\sqrt{a\pi}\,\Gamma\left(\frac{a}{2}\right)}
\left(1+\frac{y^2}{a}\right)^{-\frac{a+1}{2}},\quad y\in\mathbb{R}.
\end{align*}
This is the $t_a$ density and so $Y=(X-b)/\sqrt{c}\sim t_a$.}

\vspace{2cm}

}}
\end{example}

\vspace{-0.75cm}

\subsection*{Comment}
Values for the density function $f_Y(y)$ and the distribution function
$F_Y(y)$ can be obtained by using the \texttt{R} functions
\texttt{dgt} and \texttt{pgt} in the package \texttt{nclbayes}.

It is clear that $t_a(0,1)\equiv t_a$ by examining their densities.
Therefore, it makes sense to think of the $t_a$ distribution as the
standard $t_a$--distribution and make all calculations for the
generalised $t_a(b,c)$ distribution from this standard distribution.
The relationship between this standard and generalised version of the
$t$-distribution is directly analogous to that between the standard
normal $N(0,1)$ distribution and its more general version: the
$N(b,c)$ distribution.  In both cases the relationship is one of
location and scale:
\begin{center}
\begin{tabular}{lcl}
$Y\sim N(b,c)$ & $\Longrightarrow$ & $\dfrac{Y-b}{\sqrt{c}}\sim N(0,1)$\\
\\
$Y\sim t_a(b,c)$ & $\Longrightarrow$ & $\dfrac{Y-b}{\sqrt{c}}\sim t_a$.\\
\end{tabular}
\end{center}

\section{Prior to posterior analysis}
\label{sec:nochtheory}
Suppose we have a random sample from a normal distribution in which
both the mean~$\mu$ and the precision~$\tau$ are unknown, that is,
$X_i|\mu,\tau\sim N(\mu,1/\tau)$, $i=1,2,\ldots,n$ (independent). We
shall adopt a (joint) prior distribution for $\mu$ and $\tau$ for
which
\begin{equation*}
\mu|\tau\sim N\left(b,\frac{1}{c\tau}\right) 
\quad\quad\text{and}\quad\quad
\tau\sim Ga(g,h)
\end{equation*}
for known values $b$, $c$, $g$ and $h$. This distribution has density function
\begin{align}
\label{eq:nogaprior}
\pi(\mu,\tau)&=
\pi(\mu|\tau)\pi(\tau) \notag \\
&=\left(\frac{c\tau}{2\pi}\right)^{1/2}\exp\left\{-\frac{c\tau}{2}(\mu-b)^2\right\}
\times
\frac{h^g\tau^{g-1}e^{-h\tau}}{\Gamma(g)},
\quad \mu\in\R, ~\tau>0 \notag \\
&\propto\tau^{g-\frac{1}{2}}\exp\left\{-\frac{\tau}{2}
\left[c(\mu-b)^2+2h\right]\right\},\quad \mu\in\R, ~\tau>0.
\end{align}
We will use the notation $NGa(b,c,g,h)$ for this distribution. Thus we
take the prior distribution
\[
\mutau\sim NGa(b,c,g,h).
\] 
Determine the posterior distribution for $\mutau$.

Hint:
\[
c(\mu-b)^2+n(\bar x-\mu)^2
=(c+n)\left\{\mu-\left(\frac{cb+n\bar x}{c+n}\right)\right\}^2
+\frac{nc(\bar x-b)^2}{c+n}.
\]

\framebox[17cm]{ \parbox[t]{16cm}{

\vspace{0.4cm}

\huge
\ding{46} \normalsize 

\textcolor{white}{
From \eqref{eq:p10}, the likelihood function is
\begin{align*}
f(\xvec|\mu,\tau)&=\left(\frac{\tau}{2\pi}\right)^{n/2}
\exp\left[-\frac{n\tau}{2}
\left\{s^2+(\bar x-\mu)^2\right\}\right].
%\end{align*}
%\begin{align}
\intertext{Using Bayes Theorem, the posterior density is}
\pi(\mu,\tau|\xvec)&\propto\pi(\mu,\tau)\,f(\xvec|\mu,\tau) 
\intertext{and so, for $\mu\in\R$, $\tau>0$}
\pi(\mu,\tau|\xvec)&\propto\tau^{g-\frac{1}{2}}\exp\left\{-\frac{\tau}{2}
\left[c(\mu-b)^2+2h\right]\right\}\\
&\hspace*{3cm}\times \tau^{\frac{n}{2}}
\exp\left[-\frac{n\tau}{2}
\left\{s^2+(\bar x-\mu)^2\right\}\right] \\
&\propto\tau^{g+\frac{n}{2}-\frac{1}{2}}\exp\left\{-\frac{\tau}{2}
\left[c(\mu-b)^2+n(\bar x-\mu)^2+2h+ns^2\right]\right\}\\
&\propto\tau^{g+\frac{n}{2}-\frac{1}{2}}\exp\left\{-\frac{\tau}{2}
\left[(c+n)\left\{\mu-\left(\frac{cb+n\bar x}{c+n}\right)\right\}^2+\frac{nc(\bar x-b)^2}{c+n}+2h+ns^2\right]\right\}
\end{align*}
}
}}

\framebox[17cm]{ \parbox[t]{16cm}{

\vspace{0.4cm}

\huge
\ding{46} \normalsize \textit{...Solution to Example 1.2}

\textcolor{white}{
using the hint.  Let
\begin{equation}
\begin{split}
\label{eq:nogapostpara}
B&=\frac{bc+n\bar x}{c+n}, \quad\quad C=c+n,\\
G&=g+\frac{n}{2}, \quad\quad
H=h+\frac{cn(\bar x-b)^2}{2(c+n)}+\frac{ns^2}{2}.
\end{split}
\end{equation}
Then the posterior density is
\begin{equation*}
%\label{eq:nogaposterior}
\begin{split}
\pi(\mu,\tau|\xvec)&\propto\tau^{G-\frac{1}{2}}\exp\left\{-\frac{\tau}{2}
\left[C(\mu-B)^2+2H\right]\right\},\\
&\hspace{7cm}\mu\in\R, \tau>0
\end{split}
\end{equation*}
Notice that this posterior density 
%$\eqref{eq:nogaposterior}$ 
is of the same form as the prior density $\eqref{eq:nogaprior}$.
Therefore, we can conclude that the posterior distribution is
\[
\left.\mutau\right|\xvec\sim NGa(B,C,G,H). 
\]
Thus, the $NGa$ distribution is conjugate to this data model.}

\vspace{10cm}

}}

%\end{soln}

\newpage

\subsection{Marginal distributions}
Suppose $(\mu,\tau)^T\sim NGa(b,c,g,h)$. From the definition of the
$NGa$ distribution we know that $\tau\sim Ga(g,h)$. This also means
that $\sigma=1/\sqrt\tau\sim\textit{Inv-Chi}(g,h)$; see formula sheet.  

The (marginal) density for $\mu$ is, for $\mu\in\R$
\begin{align*}
\pi(\mu)&=\int_0^\infty \pi(\mu,\tau)\,d\tau \\
&\propto\int_0^\infty \tau^{g-\frac{1}{2}}\exp\left\{-\frac{\tau}{2}
\left[c(\mu-b)^2+2h\right]\right\}\,d\tau. 
\end{align*}
Now, as the integral of a gamma density over its entire range is one,
we have
\[
\int_0^\infty\frac{b^a\theta^{a-1}e^{-b\theta}}{\Gamma(a)}\,d\theta=1
\quad\Longrightarrow\quad
\int_0^\infty\theta^{a-1}e^{-b\theta}\,d\theta
=\frac{\Gamma(a)}{b^a}.
\]
Therefore, for $\mu\in\R$
\begin{align*}
\pi(\mu)
&\propto\int_0^\infty \tau^{g+\frac{1}{2}-1}\exp\left\{-\frac{\tau}{2}
\left[c(\mu-b)^2+2h\right]\right\}\,d\tau\\
&\propto\frac{\Gamma\left(g+\frac{1}{2}\right)}
{\left[\left\{c(\mu-b)^2+2h\}/2\right\}\right]^{g+\frac{1}{2}}} \\
&\propto h^{-g-1/2}\left\{1+\frac{c(\mu-b)^2}{2h}\right\}^{-g-1/2}\\
&\propto\left\{1+\frac{c(\mu-b)^2}{2h}\right\}^{-\frac{2g+1}{2}}.
\end{align*}
Comparing this density with that of the generalised $t$--distribution
(see formula sheet) gives
\begin{equation}
\label{eq:margprior}
\mu\sim t_{2g}\left(b,\frac{h}{gc}\right).
\end{equation}
Thus, marginally, the prior distribution for $\mu$ is a
$t$--distribution.

Similar calculations can be used to determine the (marginal) posterior
distributions.

\subsection*{Summary of marginal distributions}
The prior $\mutau\sim NGa(b,c,g,h)$ has marginal distributions
\begin{itemize}
\item $\mu\sim t_{2g}\left(b,\frac{h}{gc}\right)$ 
\medskip
\item $\tau\sim Ga(g,h)$; also $\sigma=1/\sqrt\tau\sim\textit{Inv-Chi}(g,h)$.
\end{itemize}

\newpage 

The posterior $\left.\mutau\right|\xvec\sim NGa(B,C,G,H)$ has marginal
distributions
\begin{itemize}
\item $\mu|\xvec\sim t_{2G}\left(B,\frac{H}{GC}\right)$ 
\medskip
\item $\tau|\xvec\sim Ga(G,H)$; also $\sigma|\xvec\sim\textit{Inv-Chi}(G,H)$.
\end{itemize}

It can be shown that the posterior mean of $\mu$ is greater than its
prior mean if and only if the sample mean (likelihood mode) is greater
than its prior mean, that is,
\begin{equation*}
E(\mu|\xvec)>E(\mu)\quad\iff\quad \bar x >b.
\end{equation*}
The relationships between the prior and posterior variance of $\mu$
and mean and variance of $\tau$ and of $\sigma$ are rather more
complex.

\begin{example}~\\
  Recall Example~\ref{ex:cavendish} on the earth's density. Previously
\label{ex:cavbiv}
  we assumed that the measurements followed a $N(\mu,0.2^2)$
  distribution, that is, the standard deviation of the measurements
  was known to be~$0.2\,g/cm^3$. Now we consider the case where this
  standard deviation is unknown and determine posterior distributions
  using the theory in section~\ref{sec:nochtheory}.

Before we can proceed, we must specify the parameters in the
$NGa(b,c,g,h)$ prior distribution for $(\mu,\tau)$.  In the previous
analysis, we assumed that the population measurement precision was
$\tau=1/0.2^2=25$ and assumed a $N(5.41,0.4^2)$ prior distribution for
the population mean, that is, $\mu|\tau=25\sim N(5.41,0.4^2)$.

Choice of $b$ and $c$: the conditional prior distribution for $\mu$ is
$\mu|\tau\sim N\{b,1/(c\tau)\}$ and so matching the prior distributions
for~$\mu$ (when $\tau=25$) gives $b=5.41$ and $c=0.25$.

Choice of $g$ and $h$: the marginal prior distribution for $\tau$ is
$\tau\sim Ga(g,h)$.  Previously, we assumed $\tau=25$ (with
$Var(\tau)=0$) and so take this value as the prior mean: $E(\tau)=25$.
Suppose we also decide that $Var(\tau)=250$. These two requirements
give $g=2.5$ and $h=0.1$. Therefore, we will assume the prior
distribution
\[
\mutau\sim NGa(5.41,0.25,2.5,0.1).
\]
We have seen that if $(\mu,\tau)^T\sim NGa(b,c,g,h)$ then the marginal
distribution of $\mu$ is $\mu\sim t_{2g}\bigl\{b,h/(gc)\bigr\}$.
Therefore, with this choice of prior distribution, the marginal prior
distribution for $\mu$ is
\[
\mu\sim t_5(5.41,0.16).
\]
Figure~\ref{fig:cav-prior} shows the close match between the new
(marginal) prior distribution for $\mu$ and that used previously.

\begin{figure}[ht]
\centering
\includegraphics[width=0.65\textwidth]{cav-prior-mu}
%\includegraphics[width=0.5\textwidth]{cav-prior-tau}
%\includegraphics[width=0.5\textwidth]{cav-prior-sigma}
%\caption{Marginal prior densities for $\mu$ (new version (solid) and previous version (dashed)), $\tau$ and $\sigma$}
\caption{Marginal prior density for $\mu$: new version (solid) and previous version (dashed)}
\label{fig:cav-prior}
\end{figure}
Determine the posterior distribution for $(\mu,\tau)^T$. Also
determine the marginal prior distribution for $\tau$ and for $\sigma$,
and the marginal posterior distribution for each of $\mu$, $\tau$
and~$\sigma$.
\end{example}

\framebox[17cm]{ \parbox[t]{16cm}{

\vspace{0.4cm}

\huge
\ding{46} \normalsize 

\textcolor{white}{
We can combine the information in the $NGa(5.41,0.25,2.5,0.1)$ prior
distribution for $(\mu,\tau)^T$ with that in the data ($n=23$, $\bar
x=5.4848$, $s=0.1882$) using the results in section~\ref{sec:nochtheory} to
obtain a $NGa(B,C,G,H)$ posterior distribution, where
\begin{align*}
B&=\frac{bc+n\bar x}{c+n}
=\frac{(5.41\times 0.25)+(23\times 5.4848)}{23.25}=5.4840,\\
C&=c+n=23.25,\\
G&=g+\frac{n}{2}=14,\\
H&=h+\frac{cn(\bar x-b)^2}{2(c+n)}+\frac{ns^2}{2}
=0.1+\frac{5.75}{46.5}(5.4848-5.41)^2+11.5\times 0.1882^2=0.5080.
\end{align*}
The marginal prior distributions for $\tau$ and $\sigma$ are
\begin{align*}
\tau&\sim Ga(g,h)\equiv Ga(2.5,0.1)\\
\noalign{\medskip}
\sigma&\sim\textit{Inv-Chi}(g,h)\equiv\textit{Inv-Chi}(2.5,0.1)
\end{align*}
Also the marginal posterior distributions for $\mu$, $\tau$ and
$\sigma$ are
\begin{align*}
\mu|\xvec&\sim t_{2G}\left(B,\frac{H}{GC}\right)\equiv t_{28}(5.4840,0.001561)\\
\tau|\xvec&\sim Ga(G,H)\equiv Ga(14,0.5080)\\
\noalign{\medskip}
\sigma|\xvec&\sim\textit{Inv-Chi}(G,H)\equiv\textit{Inv-Chi}(14,0.5080)
\end{align*}}
%%Hack for vspace
\vspace{2cm}
}}



Plots of the (marginal) prior and posterior distributions of $\mu$,
$\tau$ and $\sigma$ are given in Figure~\ref{fig:cav-post}. Note that
the (marginal) prior and posterior distributions for $\sigma$ can be
determined from that of $\tau$.  We can also examine the joint prior
and posterior distributions for $(\mu,\tau)^T$ via the contour plots
of their densities to see if there is any change in the dependence
structure; see Figure~\ref{fig:cav-post-joint}. This figure is
produced by using the \texttt{R} command \texttt{NGacontour} in the
\texttt{nclbayes} package as follows:
\begin{verbatim}
mu=seq(4.5,6.5,len=1000)
tau=seq(0,71,len=1000)
NGacontour(mu,tau,b,c,g,h,lty=3)
NGacontour(mu,tau,B,C,G,H,add=TRUE)
\end{verbatim}
in which the variables \texttt{b,c,g,h,B,C,G,H} have already been set
to their prior/posterior values.  A careful look at the values of the
contour levels plotted shows that the highest contour level plotted
for the prior density is 0.024 and the lowest level for the posterior
density is 0.05. From this we can conclude that the posterior
distribution is far more concentrated than the prior distribution.
Also the contours for the posterior distribution are much more
elliptical than those for the prior distribution.  This indicates a
change in the dependence structure. However, the main changes shown by
the figure are in the mean and variability of $\mu$ and~$\tau$.
\begin{figure}[t!]
\includegraphics[width=0.5\textwidth]{cav-post-mu}\\
\includegraphics[width=0.5\textwidth]{cav-post-tau}
\includegraphics[width=0.5\textwidth]{cav-post-sigma}
\caption{Prior (dashed) and posterior (solid) densities for $\mu$,
  $\tau$ and $\sigma$}
\label{fig:cav-post}
\end{figure}

\begin{figure}[t]
\centering\bigskip
\includegraphics[width=0.7\textwidth]{cav-post-joint}
%\includegraphics[width=0.7\textwidth]{cav-post-joint-sigma}
\caption{Contour plot of the prior (dashed) and posterior (solid)
densities for $(\mu,\tau)^T$.} 
\label{fig:cav-post-joint}
\end{figure}

Wikipedia tells us that the actual mean density of the earth is
$5.515\,g/cm^3$.  We can determine the (posterior) probability that
the mean density is within 0.1 of this value as follows. We already
know that $\mu|\xvec\sim t_{28}(5.484,0.001561)$ and so we can
calculate
\[
Pr(5.415<\mu<5.615|\xvec)=0.9529
\]
using \verb$pgt(5.615,28,5.484,0.001561)-pgt(5.415,28,5.484,0.001561)$.

Without the data, the only basis for determining the earth's density
is via the prior distribution. Here the prior distribution is $\mu\sim
t_5(5.41,0.16)$ and so the (prior) probability that the mean density
is within 0.1 of the (now known) true value is
\[
Pr(5.415<\mu<5.615)=0.1802,
\]
calculated using \verb$pgt(5.615,5,5.41,0.16)-pgt(5.415,5,5.41,0.16)$.

These probability calculations demonstrate that the data have been
very informative and changed our beliefs about the earth's density.

\newpage

\section{Confidence intervals and regions}
\label{sec:confint}
\begin{example}~\\
  Determine the $100(1-\alpha)\%$ highest density interval (HDI) for
  the population mean~$\mu$ in terms of quantiles of the standard
  $t$-distribution.
\end{example}

\framebox[17cm]{ \parbox[t]{16cm}{

\vspace{0.4cm}

\huge
\ding{46} \normalsize 

\textcolor{white}{
  The marginal posterior distribution is $\mu|\xvec\sim
  t_{2G}\left(B,\frac{H}{GC}\right)$. This is a symmetric distribution
  and so the HDI is an equi-tailed interval. Therefore the HDI
  $(\ell,u)$ for $\mu$ must satisfy
\[
Pr(\mu<\ell|\xvec)=\alpha/2\qquad\text{and}\qquad Pr(\mu>u|\xvec)=\alpha/2.
\]
Now, given the data $\xvec$
\begin{align*}
\frac{\mu-B}{\sqrt{H/(GC)}}&\sim t_{2G}
\intertext{and so}
Pr(\mu>u|\xvec)=\alpha/2\quad&\Rightarrow\quad
Pr\left(\left.\frac{\mu-B}{\sqrt{H/(GC)}}>\frac{u-B}{\sqrt{H/(GC)}}\right|\xvec\right)=\alpha/2\\
&\Rightarrow\quad \frac{u-B}{\sqrt{H/(GC)}}=t_{2G;\alpha/2}
\intertext{where $t_{2G;p}$ is the upper $p$ point of the $t_{2G}$
  distribution. Therefore}
u&=B+t_{2G;\alpha/2}\sqrt{\frac{H}{GC}}.
\intertext{Similar calculations give}
\ell&=B+t_{2G;1-\alpha/2}\sqrt{\frac{H}{GC}}=B-t_{2G;\alpha/2}\sqrt{\frac{H}{GC}}
\end{align*}
since the $t$ distribution is symmetric about zero.  Thus the
$100(1-\alpha)\%$ HDI for $\mu$ is
\[
\left(B-t_{2G;\alpha/2}\sqrt{\frac{H}{GC}},~B+t_{2G;\alpha/2}\sqrt{\frac{H}{GC}}\right).
\]}

\vspace{2cm}

}}

\newpage

These intervals can be calculated easily using the \verb$R$ function
\texttt{qgt} in the package \texttt{nclbayes}.  For example, the prior
and posterior 95\% HDIs for $\mu$ can be calculated using
\begin{center}
\texttt{c(qgt(0.025,2*g,b,h/(g*c)),qgt(0.975,2*g,b,h/(g*c)))}\\
\texttt{c(qgt(0.025,2*G,B,H/(G*C)),qgt(0.975,2*G,B,H/(G*C)))}
\end{center}

Determining a highest density interval (HDI) for the population
precision~$\tau$ or standard deviation~$\sigma$ is more complicated as
their posterior distributions are not symmetric. The (marginal)
posterior for $\tau$ is $\tau|\xvec\sim Ga(G,H)$ and the (marginal)
posterior for $\sigma$ is $\sigma|\xvec\sim\textit{Inv-Chi}(G,H)$.
HDIs can be found by using the \texttt{R} functions \texttt{hdiGamma}
and \texttt{hdiInvchi} in the package \texttt{nclbayes}. More standard
equi-tailed confidence intervals can be found using the functions
\texttt{qgamma} and \texttt{qinvchi}. 

For example, the prior and posterior 95\% HDIs for $\tau$ can be
calculated using \texttt{R} commands \texttt{hdiGamma(0.95,g,h)} and
\texttt{hdiGamma(0.95,G,H)}, and those for $\sigma$ using commands
\texttt{hdiInvchi(0.95,g,h)} and \texttt{hdiInvchi(0.95,G,H)}. The
95\% equi-tailed confidence intervals are calculated in a similar way
to the HDIs for $\mu$ above.  So for $\tau$, the prior and posterior
intervals are calculated using
\begin{center}
\texttt{c(qgamma(0.025,g,h),qgamma(0.975,g,h))}\\
\texttt{c(qgamma(0.025,G,H),qgamma(0.975,G,H))}
\end{center}
and those for $\sigma$ using
\begin{center}
\texttt{c(qinvchi(0.025,g,h),qinvchi(0.975,g,h))}\\
\texttt{c(qinvchi(0.025,G,H),qinvchi(0.975,G,H))}
\end{center}

The numerical values for the prior and posterior 95\% intervals for
the analysis in Example~\ref{ex:cavbiv} are given in
Table~\ref{tab:cavbivint}. Notice that there is little difference
between the posterior HDI and equi-tailed intervals for~$\tau$ and
for~$\sigma$, whereas the prior intervals are fairly different. This
is because the prior distributions are quite skewed but the posterior
distributions are fairly symmetric; see Figure~\ref{fig:cav-post}.

\begin{table}
\begin{center}
\begin{tabular}{cccl}
& Prior & Posterior \\
$\mu$: & $(4.3818,6.4382)$ & $(5.4031,5.5649)$ \\
\noalign{\medskip}
$\tau$: & {\d}$(1.4812,55.9573)$ & $(14.0193,42.2530)$ & $\gets$ HDI\\
& {\d}$(4.1561,64.1625)$ & $(15.0674,43.7625)$ \\
\noalign{\medskip}
$\sigma$: & $(0.1062,0.4246)$ & $(0.1466,0.2505)$ & $\gets$ HDI\\
& $(0.1248,0.4905)$ & $(0.1512,0.2576)$
\end{tabular}
\end{center}
\caption{Prior and posterior 95\% intervals for the analysis in Example~\ref{ex:cavbiv}}
\label{tab:cavbivint}
\end{table}

In Bayesian inference it can also be useful to determine (joint)
confidence regions for several parameters, in this case, for
$(\mu,\tau)^T$. In general this is a difficult problem to solve
mathematically, and it is in this case.

%%Hack for vspace
\newpage
\begin{example}~\\
  Determine a joint confidence region for $(\mu,\tau)^T$.
\end{example}

\framebox[17cm]{ \parbox[t]{16cm}{

\vspace{0.4cm}

\huge
\ding{46} \normalsize 

\textcolor{white}{
  We know that the (joint) prior distribution for these parameters is
\[
\mutau\sim NGa(b,c,g,h).
\]  
Therefore an HDI--type confidence region takes the form
\begin{align*}
&\left\{\mutau:~\pi(\mu,\tau)>k\right\}\\
&=\left\{\mutau:~\tau^{g-\frac{1}{2}}\exp\left\{-\frac{\tau}{2}\left[c(\mu-b)^2+2h\right]\right\}>k'\right\}\\
%\end{align*}
%\begin{align*}
&=\left\{\mutau:~\left(g-\frac{1}{2}\right)\log\tau
-\frac{\tau}{2}\left[c(\mu-b)^2+2h\right]>k''\right\}\\
&=\left\{\mutau:~\frac{\tau c(\mu-b)^2}{2}+h\tau
-\left(g-\frac{1}{2}\right)\log\tau<k_\alpha\right\}
\end{align*}
where $k_\alpha$ will depend on the confidence level of the region.
These regions are not difficult to draw. The difficult part is
determining the appropriate value for $k_\alpha$ to get say a 95\%
confidence region. If we could determine the distribution of
\begin{align*}
Y&=\frac{\tau c(\mu-b)^2}{2}+h\tau-\left(g-\frac{1}{2}\right)\log\tau
\intertext{when}
\mutau&\sim NGa(b,c,g,h)
\end{align*}
then we could get the value for~$k_\alpha$.  Unfortunately it is quite
difficult to do this mathematically. However, we can use simulation
methods to get a pretty accurate value for $k_\alpha$ (for a given
confidence level).}

\vspace{3cm}}}

\newpage

Using an additional argument in the \texttt{R} function
\texttt{NGacontour} produces plots of confidence regions. For example
\begin{verbatim}
mu=seq(3.5,7.5,len=1000)
tau=seq(0,80,len=1000)
NGacontour(mu,tau,b,c,g,h,p=c(0.95,0.9,0.8),lty=3)
NGacontour(mu,tau,B,C,G,H,p=c(0.95,0.9,0.8),add=TRUE)
\end{verbatim}
produces a plot containing the 95\%, 90\% and 80\% prior and posterior
confidence regions for $(\mu,\tau)^T$ for the prior and posterior
distributions in Example~\ref{ex:cavbiv}; see
Figure~\ref{fig:cav-conf}.
\begin{figure}[p!]
\centering
\includegraphics[width=0.7\textwidth]{cav-conf1}
\includegraphics[width=0.7\textwidth]{cav-conf2}
\caption{95\%, 90\% and 80\% prior (dashed) and posterior (solid)
  confidence regions for $(\mu,\tau)^T$}
\label{fig:cav-conf}
\end{figure}
The upper plot shows contours of both prior and posterior densities.
The numbers within the plot are the contour levels. The largest prior
confidence region is the 95\% region. The next largest is the 90\%
prior confidence region and the smallest is the 80\% prior confidence
region. The same ordering holds for the posterior confidence regions.
The posterior contours are so concentrated in the middle of the plot
that there is no room to put in the contour levels. However, these can
be see on the lower plot which also shows the contours but focuses the
parameter range to highlight the contours of the posterior density.
The values of the contours in this lower plot show that the posterior
density is much more peaked, that is, the posterior has a much reduced
variability. The location of the centre of the central contour for
both the prior and posterior densities shows that there has been
little change in the mean/mode.

\section{Predictive distribution}
\label{sec:predrs}
Suppose we sample another value $y$ randomly from the population.
What values is it likely to take? This is described by its predictive
distribution. We can determine this distribution by using the
definition of the predictive density
\[
f(y|\xvec)=\int f(y|\mu,\tau)\,\pi(\mu,\tau|\xvec)\,d\mu\,d\tau
\]
or by using Candidate's formula (as this is a conjugate analysis).
However, for this model/prior, there is a more straightforward method
to determine the predictive distribution in this model.

  As $Y$ is a random value from the population, we have that
  $Y|\mu,\tau\sim N(\mu,1/\tau)$. We also know that the posterior
  distribution is $(\mu,\tau)^T|\xvec\sim NGa(B,C,G,H)$. Therefore,
  we can write
\begin{align*}
  Y&=\mu+\eps,
\intertext{where} 
\eps|\tau&\sim N(0,1/\tau)\qquad\text{and}\qquad
\mu|\xvec,\tau\sim N\left(B,\frac{1}{C\tau}\right).
\intertext{Hence $Y$ is the sum of two independent normal random
  quantities, and so} 
Y|\xvec,\tau&\sim N\left(B,\,\frac{1}{\tau}+\frac{1}{C\tau}\right)
\equiv N\left(B,\,\frac{C+1}{C\tau}\right).
\end{align*}
Thus, as $\tau|\xvec\sim Ga(G,H)$
\[
\left.\Ytau\right|\xvec\sim NGa\left(B,\frac{C}{C+1},G,H\right)
\]
and so, using \eqref{eq:margprior}
\[
Y|\xvec\sim t_{2G}\left\{B,\frac{H(C+1)}{GC}\right\}.
\]

We can determine $100(1-\alpha)\%$ predictive intervals by noting that
the predictive distribution is symmetric about its mean and therefore
the HDI is
\[
\left(B-t_{2G;\alpha/2}\sqrt{\frac{H(C+1)}{GC}},\,
B+t_{2G;\alpha/2}\sqrt{\frac{H(C+1)}{GC}}\right).
\]



These predictive intervals can be calculated easily using the \verb$R$
function \texttt{qgt}. For example, in Example~\ref{ex:cavbiv}, the
prior and posterior predictive HDIs for a new value $Y$ from the
population are $(4.2604,6.5596)$ and $(5.0855,5.8825)$ respectively,
calculated using
\begin{center}
\texttt{c(qgt(0.025,2*g,b,h*(c+1)/(g*c)),qgt(0.975,2*g,b,h*(c+1)/(g*c)))}\\
\texttt{c(qgt(0.025,2*G,B,H*(C+1)/(G*C)),qgt(0.975,2*G,B,H*(C+1)/(G*C)))}
\end{center}

\begin{comment}
\subsection*{Comment}
We can determine the predictive distribution of $V$ as $m\to\infty$ as
follows. In fact we will determine the predictive distribution of
$1/V$ as $m\to\infty$. Now
\[
\left.\frac{1}{V}\right|\xvec\sim\frac{mG}{(m-1)H}\,\frac{1}{F_{m-1,2G}}
\equiv\frac{mG}{(m-1)H}\,F_{2G,m-1}
\]
since $1/F_{\nu_1,\nu_2}\equiv F_{\nu_2,\nu_1}$. Also it can be shown
that
$\lim_{\nu_2\to\infty}F_{\nu_1,\nu_2}=Ga(\nu_1/2,\nu_1/2)$. Therefore,
as $m\to\infty$
\[
\left.\frac{1}{V}\right|\xvec\sim\frac{mG}{(m-1)H}\,F_{2G,m-1}\to
\frac{G}{H}\,Ga(G,G)\equiv Ga(G,H),
\]
which is the posterior distribution for the population
precision~$\tau$. Not surprising!
\end{comment}

\section{Summary}
Suppose we have a normal random sample with $X_i|\mu,\tau\sim
N(\mu,1/\tau)$, $i=1,2,\ldots,n$ (independent). 
\begin{enumerate}[(i)]
\item $(\mu,\tau)^T\sim NGa(b,c,g,h)$ is a conjugate prior
  distribution. 
\item The posterior distribution is $(\mu,\tau)^T|\xvec\sim
  NGa(B,C,G,H)$ where the posterior parameters are given by
  \eqref{eq:nogapostpara}.
\item The marginal prior distributions are $\mu\sim t_{2g}\{b,h/(gc)\}$,
  $\tau\sim Ga(g,h)$, $\sigma=1/\sqrt\tau\sim\textit{Inv-Chi}(g,h)$.
\item The marginal posterior distributions are $\mu|\xvec\sim
  t_{2G}\{B,H/(GC)\}$, $\tau|\xvec\sim Ga(G,H)$,
  $\sigma|\xvec\sim\textit{Inv-Chi}(G,H)$.
\item Prior and posterior means and standard deviations for $\mu$,
  $\tau$ and $\sigma$ can be calculated from the properties of the
  $t$, $Gamma$ and \textit{Inv-Chi} distributions.
\item Prior and posterior probabilities and densities for $\mu$,
  $\tau$ and $\sigma$ can be calculated using the \texttt{R} functions
  \texttt{pgt, dgt, pgamma, dgamma, pinvchi, dinvchi}.
\item HDIs or equi-tailed CIs for $\mu$, $\tau$ and $\sigma$ can be
  calculated using \texttt{qgt, hdiGamma, hdiInvchi, qgamma, qinvchi}.
\item Contour plots of the prior and posterior densities for
  $(\mu,\tau)^T$ can be plotted using the \texttt{NGacontour} function.
\item Prior and posterior confidence regions for $(\mu,\tau)^T$ can be
  plotted using the \texttt{NGacontour} function.
\item The predictive distribution for a new observation $Y$ from the
  population is $Y|\xvec\sim t_{2G}\{B,H(C+1)/(GC)\}$ and its HDI can be
  calculated using the \texttt{qgt} function.
\end{enumerate}


\section{Why do we have so many different distributions?}
So far we have used many distributions, some you will have met before
and some will be new. After a while the variety and sheer number of
different distributions can become overwhelming. Why do we need so
many distributions and why do we name so many of them?

Statistics studies the random variation in experiments, samples and
processes. The variety of applications leads to their randomness being
described by many different distributions. In many applications,
bespoke distributions will need to be formulated. However, some
distributions come up time and time again for modelling random
variation in data and for describing prior beliefs. It is helpful for
us to be able to refer to these distributions -- and so we give each
one a name -- and also to be able to quote known results for these
distributions such as their mean and variance. In this chapter you
have been introduced to a generalisation of the $t$-distribution and
the inverse chi distribution, and we have been able to use results for
their mean and variance to study prior and posterior distributions and
have been able to plot these distributions using functions in the
\texttt{R} package.

You will meet several other new distributions in the remainder of the
module. You won't be surprised to hear that it is useful to have a
working knowledge of each of these distributions but perhaps not vital
to remember all their properties listed in these notes. To help in
this regard, the exam paper will contain a list of all the
distributions used in the exam, together with their density (or
probability function) and any useful results such as their mean and
variance (as needed for the exam); see the specimen exam paper at the
back of this booklet.

%%Hack for vspace
\newpage

\newnotes
\section{Learning objectives}
By the end of this chapter, you should be able to:
\begin{myitemize}
\item determine the posterior distribution for $(\mu,\tau)^T$ 
\item determine and use the univariate prior and posterior distributions
\item determine confidence intervals, HDIs and confidence regions
\item determine the predictive distribution of another value from the
  population, and its predictive interval
\item determine the predictive distribution of the mean of another
  random sample from the population
\end{myitemize}
both in general and for a particular prior and data set. Also you
should be able to:
\begin{myitemize}
\item appreciate the benefit of naming distributions and for having
  lists of properties for these distributions
\end{myitemize}
