%29/11/10
\documentclass{beamer}
\usepackage{graphics}
\title{MAS8303: Modern Bayesian Inference}
\author{Malcolm Farrow}
\date{November 2010}
\newcommand{\E}{{\rm E}}
\newcommand{\var}{{\rm var}}
\newcommand{\covar}{{\rm covar}}
\newcommand{\muu}{\underline{\mu}}
\newcommand{\Mu}{\underline{M}}
\newcommand{\Xu}{\underline{X}}
\newcommand{\nnu}{\underline{n}}
\newcommand{\xu}{\underline{x}}
\newcommand{\yu}{\underline{y}}
\newcommand{\eps}{\varepsilon}
\newcommand{\piu}{\underline{\pi}}
\newcommand{\tauu}{\underline{\tau}}
\newcommand{\binco}[2]{\left( \begin{array}{c} #1 \\ #2 \end{array} \right) }
\begin{document}

\frame{\titlepage}

\begin{frame}
\frametitle{Lecture 4.1: Mixtures}

Two reasons why we might want to do this:
\begin{enumerate}
\item We might believe that there really are two or more sub-populations. 

\item Using a mixture distribution allows more flexibility in the sampling model. 
\end{enumerate}
\end{frame}


\begin{frame}
\frametitle{Lecture 4.1: Mixtures}
Consider a simple two-component mixture model. Our sampling model for observation $ Y_{i} $ has pdf

\[ f(y_{i};\ \pi, \theta_{1}, \theta_{2}) = \pi f_{1}(y_{i}; \theta_{1}) + (1-\pi) f_{2}(y_{i}; \theta_{2}). \]
Here $ f_{j}(y; \theta_{j}) $ is the pdf for component $ j $ and depends on parameters $ \theta_{j}. $ The component membership probabilities are $ \pi $ and $ 1-\pi, $ with $ 0 \leq \pi \leq 1. $

Suppose that we have $ n $ independent (given the parameters) observations $ y_{1}, \ldots , y_{n}. $ The likelihood 
is

\[ L = \prod_{i=1}^{n} \left\{ \pi f_{1}(y_{i}; \theta_{1}) + (1-\pi) f_{2}(y_{i}; \theta_{2})\right\}. \ \ \ \ (4.1) \]

This has a rather complicated form. For example, it is a polynomial of degree $ n $ in $ \pi. $ 
\end{frame}

\begin{frame}
\frametitle{Lecture 4.1: Mixtures}

More generally we could have $ J $ components with

\[ f(y_{i};\ \piu, \Theta) = \sum_{j=1}^{J}\pi_{j} f_{j}(y_{i}; \theta_{j}), \ \ \ \ (4.2) \]

where $ \sum_{j=1}^{J} \pi_{j} =1 $ and $ \pi_{j} \geq 0 $ for $ j=1, \ldots , J. $ In this case the likelihood is

\[ L = \prod_{i=1}^{n} \left\{\sum_{j=1}^{J}\pi_{j} f_{j}(y_{i}; \theta_{j}) \right\}. \ \ \ \ (4.3) \]

This could be very complicated.
\end{frame}

\begin{frame}
\frametitle{Lecture 4.1: Mixtures}

We can make things much simpler by introducing a group-membership variable which is unobserved. The values form auxiliary data so this is an example of data augmentation.

\end{frame}

\begin{frame}
\frametitle{Lecture 4.1: Mixtures}

We introduce, for observation $ i, $ an auxiliary variable $ c_{i}, $ which can take the values $ 1, \ldots , J. $ Then, given that $ c_{i}=j, $ the conditional pdf for observation $ i $ is simply $ f_{j}(y_{i};\ \theta_{j}). $ The corresponding conditional likelihood is then just

\[ L_{c} = \prod_{i=1}^{n} \pi_{c_{i}} f_{c_{i}}(y_{i};\ \theta_{c_{i}}). \]

\end{frame}

\begin{frame}
\frametitle{Lecture 4.1: Mixtures}

Now we give $ c_{i} $ a multinomial (or ``categorical'') distribution, in which $ \Pr(c_{i}=j) = \pi_{j}. $ We give the parameters $ \piu=(\pi_{1}, \ldots , \pi_{J})^{T} $ and $ \Theta = \{ \theta_{1}, \ldots , \theta_{J} \} $ a suitable prior distribution. Then, by ``integrating out'', i.e.\ ``averaging over'', $ c_{1}, \ldots , c_{n}, $ we obtain the correct posterior distribution.

\end{frame}

\begin{frame}
\frametitle{Lecture 4.1: Mixtures}

The joint probability (density) that $ c_{i}=j $ and $ Y_{i}=y_{i} $ is

\[ f(y_{i},c_{i}=j;\ \piu, \Theta) = \pi_{j} f_{j}(y_{i};\ \theta_{j}). \]
To find the marginal probability density of $ y_{i} $ we sum over $ j $ and obtain (4.2) as required.

\end{frame}

\begin{frame}
\frametitle{Lecture 4.1: MCMC and label-switching : MCMC}

Once we have the model set up with the auxiliary variables $ c_{1}, \ldots , c_{n} $ as above, we have a prior distribution with density $ f_{0}(\Theta, \piu) $ for the parameters and we have initial values for the unknowns, $ \Theta,\ \piu,\ c_{1}, \ldots , c_{n}, $ then we can proceed with MCMC as follows.

\end{frame}

\begin{frame}
\frametitle{Lecture 4.1: MCMC and label-switching : MCMC}

1. Sample a new value for $ \Theta. $ The fcd density is proportional to

\[ f_{0}(\Theta, \piu) \prod_{j=1}^{J} L_{c,j} \]
where
\[ L_{c,j} = \prod_{i \in C_{j}} f_{j}(y_{i};\ \theta_{j}) \]
and $ i \in C_{j} $ if $ c_{i}=j. $ That is $ C_{j} $ is the set of observations currently assigned to component $ j. $
We might well have $ f_{0}(\Theta, \piu) = f_{0,\theta}(\Theta) f_{0,\pi}(\piu) $ in which case the fcd density is proportional to

\[ f_{0}(\Theta) \prod_{j=1}^{J} L_{c,j} \]

\end{frame}

\begin{frame}
\frametitle{Lecture 4.1: MCMC and label-switching : MCMC}


2. Sample a new value for $ \piu. $ The fcd density is proportional to

\[ f_{0}(\Theta, \piu) \prod_{j=1}^{J} \pi_{j}^{n_{j}} \]
where $ n_{j} $ is the number of observations currently assigned to component $ j. $ If $ f_{0}(\Theta, \piu) = f_{0,\theta}(\Theta) f_{0,\pi}(\piu) $ then the fcd density is proportional to

\[ f_{0,\pi}(\piu) \prod_{j=1}^{J} \pi_{j}^{n_{j}}. \]
A popular choice for $ f_{0,\pi}(\piu) $ would be a Dirichlet density. In this case the fcd is also a Dirichlet distribution. Sampling from a Dirichlet distribution is quite easy.

\end{frame}

\begin{frame}
\frametitle{Lecture 4.1: MCMC and label-switching : MCMC}

3. Sample a new value for each of $ c_{1}, \ldots , c_{n}. $ The fcd is a categorical distribution with

\[ \Pr(c_{i}=j) \propto \pi_{j} f_{j}(y_{i};\ \theta_{j}). \]

4. Repeat.

\end{frame}

\begin{frame}
\frametitle{Lecture 4.1: MCMC and label-switching : Label-switching}


Consider the likelihood (4.1). 

Suppose that both component distributions are of the same family so that the likelihood is

\[  L = \prod_{i=1}^{n} \left\{ \pi f_{y}(y_{i}; \theta_{1}) + (1-\pi) f_{y}(y_{i}; \theta_{2}).\right\} \]

Suppose that we ``switch the labels'' and write

\[  \tilde{L} = \prod_{i=1}^{n} \left\{ \tilde{\pi} f_{y}(y_{i}; \tilde{\theta}_{1}) + (1-\tilde{\pi}) f_{y}(y_{i}; \tilde{\theta}_{2}) \right\} \]
where $ \tilde{\pi} = 1-\pi,\ \tilde{\theta}_{1}=\theta_{2} $ and $ \tilde{\theta}_{2}=\theta_{1}. $

\end{frame}

\begin{frame}
\frametitle{Lecture 4.1: MCMC and label-switching : Label-switching}


Clearly $ L = \tilde{L}. $ The likelihood is therefore bimodal and, in fact, the modes match each other. If the prior does not strongly favour one mode over the other then the posterior distribution will also be bimodal.

\end{frame}

\begin{frame}
\frametitle{Lecture 4.1: MCMC and label-switching : Label-switching}

\begin{itemize}
\item Constraints -- eg order constraints -- on parameters.

\item Mixtures with unknown numbers of components.

\end{itemize}

\end{frame}

\begin{frame}
\frametitle{Lecture 4.1: Multivariate mixtures}

It is, of course, possible to make a mixture model where the observation $ \yu $ is multivariate. For example, we might make several measurements on each of a sample of birds belonging to one species with the idea that there might be two or more subspecies. In two dimensions we might expect a plot of observations $ y_{1} $ against $ y_{2} $ to reveal ``clusters'' of observations.

\end{frame}

\begin{frame}
\frametitle{Lecture 4.1: Continuous mixtures}

As well as the finite mixtures described above it is possible to have a mixture model with an infinite number of components. It is also possible to have a {\em continuous mixture}. In a continuous mixture model, instead of (4.2), we have, for example,

\[  f(y_{i}) = \int_{\Omega} f_{\theta}(\theta) f_{y}(y_{i}; \theta, \lambda_{i}). \ \ \ \ (4.4) \]

\end{frame}

\begin{frame}
\frametitle{Lecture 4.1: Continuous mixtures}

\[  f(y_{i}) = \int_{\Omega} f_{\theta}(\theta) f_{y}(y_{i}; \theta, \lambda_{i}). \ \ \ \ (4.4) \]

Here $ \theta $ is a parameter with a continuous distribution specified by the {\em mixing density } $ f_{\theta}(\theta). $ The range of values of $ \theta $ is denoted by $ \Omega. $ There may be other parameters which do not vary in this way and these are denoted by $ \lambda_{i}. $

\end{frame}

\begin{frame}
\frametitle{Lecture 4.1: Continuous mixtures}

We saw an example of this in Section 3.3.3 where we used Student-$t$ errors in a regression. The model was

\begin{eqnarray*}
Y_{i} \mid \mu_{i},X_{i} & \sim & N(\mu_{i},\ X_{i}^{-1}), \\
 d \sigma^{2} X_{i} & \sim & \chi^{2}_{d}. 
\end{eqnarray*}

Here $ \mu_{i} $ corresponds to $ \lambda_{i} $ in (4.4) and $ X $ corresponds to $ \theta $ in (4.4). The mixing density is that of a scaled $ \chi^{2} $ distribution and $ f_{y}(y_{i}; \theta, \lambda_{i}) $ in (4.4) corresponds to $ \phi( X_{i}^{1/2}[y_{i}-\mu_{i}] ) $ where $ \phi() $ is the standard normal pdf.


\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Lecture 4.2

\frame{\titlepage}



\begin{frame}
\frametitle{Lecture 4.2: Mixture Examples: ``Old Faithful''}

\resizebox{\textwidth}{!}{\includegraphics{oldfaithful.jpg}}

\end{frame}

\begin{frame}
\frametitle{Lecture 4.2: Mixture Examples: ``Old Faithful''}




\resizebox{\textwidth}{!}{\includegraphics{graphs/oldfaith.pdf}}

\end{frame}

\begin{frame}
\frametitle{Lecture 4.2: Mixture Examples: Normal mixture}

Let us try using a two-component normal mixture model for the log intervals. So

\begin{eqnarray*}
\Pr(c_{i}=1) & = & \pi \\
\Pr(c_{i}=2) & = & 1-\pi \\
\pi & \sim & {\rm Beta}(a_{\pi},b_{\pi}) \\
y_{i} \mid \mu_{j}, \tau_{j}, c_{i}=j & \sim & N(\mu_{j},\ \tau_{j}^{-1}) \\
\mu_{j} \mid \mu_{0} & \sim & N(\mu_{0}+\delta_{j},\ \tau_{\mu}^{-1}) \\
\mu_{0} & \sim & N(M_{\mu},\ V_{\mu}) \\
\tau_{j} & \sim & {\rm Ga}(a_{\tau},b_{\tau})
\end{eqnarray*}

\end{frame}

\begin{frame}
\frametitle{Lecture 4.2: Mixture Examples: Normal mixture}


Notice that we have given $ \mu_{1},\ \mu_{2} $ a ``hierarchical prior.'' 
Each depends on $ \mu_{0} $ which then has a prior of its own. In order to avoid label switching we can impose the restriction $ \mu_{1}<\mu_{2}. $ We also push the conditional prior means of $ \mu_{1}, \mu_{2} $ apart by making them $ \mu_{0}+\delta_{1} $ and $ \mu_{0}+\delta_{2} $ respectively, where $ \delta_{1}=-\delta $ and $ \delta_{2}=\delta. $

\end{frame}

\begin{frame}
\frametitle{Lecture 4.2: Mixture Examples: Normal mixture}


We could also use a hierarchical prior for $ \tau_{1} $ and $ \tau_{2} $ although this is not quite as straightforward with gamma distributions as it is with normal distributions. I have just given them independent priors here. There is no need to impose an order constraint on $ \tau_{1},\ \tau_{2}. $

\end{frame}

\begin{frame}
\frametitle{Lecture 4.2: Mixture Examples: Normal mixture}


The specification of the prior is completed by giving numerical values to $ a_{\pi},b_{\pi},a_{\tau},b_{\tau},M_{\mu},
V_{\mu}, \tau_{\mu}. $ We will use the following values.
\[ a_{\pi}=4,\ \ \ b_{\pi}=4,\ \ \ a_{\tau}=4,\ \ \ b_{\tau}=0.04, \]
\[ M_{\mu} = 4.0 \approx \log(60),\ \ \ V_{\mu}=0.30 \approx (\log(3)/2)^{2},\]
\[ \tau_{\mu}=3.3 \approx (\log(3)/2)^{-2},\ \ \ \delta=0.2. \]

\end{frame}

\begin{frame}[fragile]
\frametitle{Lecture 4.2: Mixture Examples: Normal mixture}


\begin{verbatim}
model faithnorm

{for (i in 1:n)
   {c[i]~dcat(q[])
    y[i]~dnorm(mu[c[i]],tau[c[i]])
    }

 for (j in 1:2)
   {tau[j]~dgamma(4,0.04)
    }

\end{verbatim}

\end{frame}

\begin{frame}[fragile]
\frametitle{Lecture 4.2: Mixture Examples: Normal mixture}


\begin{verbatim}
 mumean[1]<-mu0-0.2
 mumean[2]<-mu0+0.2
 mu[1]~dnorm(mumean[1],3.3) I(,mu[2]) # This imposes the 
 mu[2]~dnorm(mumean[2],3.3) I(mu[1],) # order constraint.

 mu0~dgamma(4.0,p.mu)
 p.mu<-1/0.3

 pi~dbeta(3,3)
 q[1]<-pi
 q[2]<-1-pi
 }
\end{verbatim}

\end{frame}

\begin{frame}
\frametitle{Lecture 4.2: Mixture Examples: Gamma mixture}

As an alternative to the normal mixture for the log intervals, which is, of course, equivalent to a lognormal mixture for the intervals, we could try a gamma mixture for the intervals themselves.

\begin{eqnarray*}
\Pr(c_{i}=1) & = & \pi \\
\Pr(c_{i}=2) & = & 1-\pi \\
\pi & \sim & {\rm Beta}(a_{\pi},b_{\pi}) \\
t_{i} \mid \alpha_{j}, \beta_{j}, c_{i}=j & \sim & {\rm Ga}(\alpha_{j},\ \beta_{j}) \\
\beta_{j} & = & \alpha_{j}/\lambda_{j} \\
\lambda_{j} & = & \exp(\mu_{j}) \\
\mu_{j} \mid \mu_{0} & \sim & N(\mu_{0}+\delta_{j},\ \tau_{\mu}^{-1}) \\
\mu_{0} & \sim & N(M_{\mu},\ V_{\mu}) \\
\alpha_{j} & \sim & {\rm Ga}(a_{\alpha},b_{\alpha})
\end{eqnarray*}

\end{frame}

\begin{frame}
\frametitle{Lecture 4.2: Mixture Examples: Gamma mixture}



Since the mean of a Ga$(\alpha_{j},\beta_{j})$ distribution is $ \alpha_{j}/\beta_{j} $ and we set 
$ \beta_{j}=\alpha_{j}/\lambda_{j}, $ the mean interval, in component $ j, $ is $ \lambda_{j}. $  We then treat $ \mu_{j}=\log(\lambda_{j}) $ in the same way as we treated $ \mu_{j} $ in the lognormal mixture. Of course the log of the mean is not the same as the mean of the logs but, in this case, this difference has little effect. (To avoid this slight discrepancy we would have to make $ \lambda_{j} $ the median rather than the mean but this is not convenient with a gamma distribution).

\end{frame}

\begin{frame}
\frametitle{Lecture 4.2: Mixture Examples: Gamma mixture}

I have not used a hierarchical prior for $ \alpha_{1},\alpha_{2}. $ I have just given them independent priors here. There is no need to impose an order constraint on $ \alpha_{1},\ \alpha_{2}. $

\end{frame}

\begin{frame}
\frametitle{Lecture 4.2: Mixture Examples: Gamma mixture}

 We will use the following values to complete the prior specification.
\[ a_{\pi}=1,\ \ \ b_{\pi}=1,\ \ \ a_{\alpha}=3,\ \ \ b_{\alpha}=0.1, \]
\[ M_{\mu} = 4.0 \approx \log(60),\ \ \ V_{\mu}=0.30 \approx (\log(3)/2)^{2},\]
\[ \tau_{\mu}=3.3 \approx (\log(3)/2)^{-2},\ \ \ \delta=0.2. \]

\end{frame}

\begin{frame}[fragile]
\frametitle{Lecture 4.2: Mixture Examples: Gamma mixture}


\begin{verbatim}
model faithgamma

{for (i in 1:n)
   {c[i]~dcat(q[])
    t[i]~dgamma(alpha[c[i]],beta[c[i]])
    }

 for (j in 1:2)
   {alpha[j]~dgamma(3,0.1)
    beta[j]<-alpha[j]/lambda[j]
    lambda[j]<-exp(mu[j])
    }
\end{verbatim}

\end{frame}

\begin{frame}[fragile]
\frametitle{Lecture 4.2: Mixture Examples: Gamma mixture}

\begin{verbatim}
 mumean[1]<-mu0-0.2
 mumean[2]<-mu0+0.2
 mu[1]~dnorm(mumean[1],3.3) I(,mu[2]) # This imposes the 
 mu[2]~dnorm(mumean[2],3.3) I(mu[1],) # order constraint.

 mu0~dnorm(4.0,p.mu)
 p.mu<-1/0.3

 pi~dbeta(1,1)
 q[1]<-pi
 q[2]<-1-pi
 }
\end{verbatim}

\end{frame}

\begin{frame}
\frametitle{Lecture 4.2: Mixture Examples: Headways}


Time gaps, or ``headways'', between vehicles passing along a road.  The idea is that headways fall naturally into one of two sub-populations:
\begin{enumerate}
\item Headways where the following vehicle is not impeded by the vehicle in front.
\item ``Congested'' headways where the following vehicle is impeded by the vehicle in front.
\end{enumerate}

\end{frame}


\begin{frame}
\frametitle{Lecture 4.2: Mixture Examples: Headways}

\begin{description}
\item[1: non-congested headways]: Exponential distribution, that is  Ga$(1,\beta_{1})$. 
\item[2: Congested headways]: Ga$(\alpha_{2},\beta_{2})$ distribution with $ \alpha_{2}>1. $ 
\end{description}

\end{frame}

\begin{frame}
\frametitle{Lecture 4.2: Mixture Examples: Headways}

\begin{itemize}
\item The constraint that $ \alpha_{2}>1 $ is imposed by letting $ \alpha_{2}=1+A $ where $ A \sim {\rm Ga}(a_{A},b_{A}). $  We have $ a_{A}=2 $ and $ b_{A}=8. $ 
\item The mean headway in component 1 is $ \mu_{1}= \beta_{1}^{-1}. $ 
\item The mean headway in component 2 is $ \mu_{2}=\alpha_{2}/\beta_{2}. $ 
\item We set $ \beta_{2}=\alpha_{2}B $ where $ B \sim {\rm Ga}(a_{B}, b_{B}). $  We have $ a_{B}=1 $ and $ b_{B}=2. $ Thus $ \mu_{2}=B^{-1}. $ 
\item By imposing the constraint $ B>\beta_{1} $ we ensure that $ \mu_{1}>\mu_{2}. $ 
\end{itemize}

\end{frame}


\begin{frame}[fragile]
\frametitle{Lecture 4.2: Mixture Examples: Headways}

\begin{verbatim}
model headway;

{

 for (i in 1:N) 
     {c[i]~dcat(q[])
      t[i]~dgamma(alpha[c[i]],beta[c[i]])
      }
\end{verbatim}

\end{frame}

\begin{frame}[fragile]
\frametitle{Lecture 4.2: Mixture Examples: Headways}
\begin{verbatim} 
 alpha[1]<-1
 alpha[2]<-1+aa
 aa~dgamma(1,0.5)
 beta[1]~dgamma(2,8) I(,bb)
 beta[2]<-alpha[2]*bb
 bb~dgamma(1,2) I(beta[1],)
 pi~dbeta(1,2)
 q[1]<-pi
 q[2]<-1-pi

 mu[1]<-1/beta[1]
 mu[2]<-1/bb

 }
\end{verbatim}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Lecture 4.3

\frame{\titlepage}



\begin{frame}
\frametitle{Lecture 4.3: Hidden Markov Models}

\resizebox{\textwidth}{!}{\includegraphics{graphs/faith2.pdf}}

\end{frame}



\begin{frame}
\frametitle{Lecture 4.3: Hidden Markov Models}

We could model the sequence $ c_{1}, \ldots , c_{n} $ using a two-state
Markov chain with the folowing transition matrix, where $ q_{j,k}=\Pr(c_{i}=j \mid c_{i-1}=k). $

\[ \left( \begin{array}{cc} q_{1,1} & q_{1,2} \\ q_{2,1} & q_{2,2} \end{array} \right) = \left( \begin{array}{cc} 0 & \pi \\ 1 & 1-\pi \end{array} \right). \ \ \ \ (4.5) \]

\end{frame}

\begin{frame}
\frametitle{Lecture 4.3: Hidden Markov Models}

Of course, before we saw the data we would not know about this pattern so it could be argued that we should use a more general model in which we allow $ q_{1,1}>0. $ In this case we would have

\[ \left( \begin{array}{cc} q_{1,1} & q_{1,2} \\ q_{2,1} & q_{2,2} \end{array} \right) = \left( \begin{array}{cc} \pi_{1} & \pi_{2} \\ 1-\pi_{1} & 1-\pi_{2} \end{array} \right). \ \ \ \ (4.6) \]


\end{frame}


\begin{frame}
\frametitle{Lecture 4.3: Hidden Markov Models}


Examples of {\em hidden Markov models} or HMM:

\begin{itemize}
\item Time series, 
\item DNA sequences,
\item Linguistics,
\item etc.
\end{itemize}

\end{frame}

\begin{frame}
\frametitle{Lecture 4.3: Hidden Markov Models}

 In general, in a HMM, we have a sequence of (possibly vector) observations $ \ldots y_{i-1}, y_{i}, y_{i+1} \ldots $ where the distribution of $ y_{i} $ depends on the value of an unobserved (i.e.\ {\em latent}) (possibly vector) variable $ x_{i} $ and the sequence $ \ldots x_{i-1}, x_{i} , x_{i+1}, \ldots $ forms a Markov chain.

\end{frame}


\begin{frame}
\frametitle{Lecture 4.3: Hidden Markov Models: Figure 4.6}


\setlength{\unitlength}{0.1cm}
\thicklines

\begin{picture}(150,60)(25,0)
\put (45,15){\circle{20}}
\put (45,15){\makebox(0,0){$X_{i-1}$}}
\put (45,45){\circle{20}}
\put (45,45){\makebox(0,0){$Y_{i-1}$}}
\put (75,15){\circle{20}}
\put (75,15){\makebox(0,0){$X_{i}$}}
\put (75,45){\circle{20}}
\put (75,45){\makebox(0,0){$Y_{i}$}}
\put (105,15){\circle{20}}
\put (105,15){\makebox(0,0){$X_{i+1}$}}
\put (105,45){\circle{20}}
\put (105,45){\makebox(0,0){$Y_{i+1}$}}

\put (23,15){\vector(1,0){14}}
\put (53,15){\vector(1,0){14}}
\put (83,15){\vector(1,0){14}}
\put (113,15){\vector(1,0){14}}
\put (45,23){\vector(0,1){14}}
\put (75,23){\vector(0,1){14}}
\put (105,23){\vector(0,1){14}}

\end{picture}

\end{frame}

\begin{frame}
\frametitle{Lecture 4.3: Hidden Markov Models: Figure 4.7}


\setlength{\unitlength}{0.07cm}
\thicklines

\begin{picture}(150,120)(0,-30)
\put (45,15){\circle{20}}
\put (45,15){\makebox(0,0){$X_{i-1}$}}
\put (45,45){\circle{20}}
\put (45,45){\makebox(0,0){$Y_{i-1}$}}
\put (75,15){\circle{20}}
\put (75,15){\makebox(0,0){$X_{i}$}}
\put (75,45){\circle{20}}
\put (75,45){\makebox(0,0){$Y_{i}$}}
\put (105,15){\circle{20}}
\put (105,15){\makebox(0,0){$X_{i+1}$}}
\put (105,45){\circle{20}}
\put (105,45){\makebox(0,0){$Y_{i+1}$}}

\put (24,15){\vector(1,0){12}}
\put (54,15){\vector(1,0){12}}
\put (84,15){\vector(1,0){12}}
\put (114,15){\vector(1,0){12}}
\put (45,24){\vector(0,1){12}}
\put (75,24){\vector(0,1){12}}
\put (105,24){\vector(0,1){12}}

\put (75,-15){\circle{20}}
\put (75,-15){\makebox(0,0){$ \pi $}}
\put (75,-6){\vector(0,1){12}}
\put (68,-8){\vector(-1,1){16}}
\put (82,-8){\vector(1,1){16}}

\put (75,75){\circle{20}}
\put (75,75){\makebox(0,0){$ \muu,\tauu $}}
\put (75,66){\vector(0,-1){12}}
\put (68,68){\vector(-1,-1){16}}
\put (82,68){\vector(1,-1){16}}

\end{picture}

\end{frame}

\begin{frame}
\frametitle{Lecture 4.3: Hidden Markov Models: Old Faithful}

Stationary distribution of the Markov chain, in this case, is

\begin{eqnarray*}
 \Pr(c_{1}=1) & = & \frac{\pi}{1-\pi},\\
 \Pr(c_{1}=2) & = & \frac{1}{1-\pi}. 
\end{eqnarray*}

\end{frame}

\begin{frame}[fragile]
\frametitle{Lecture 4.3: Hidden Markov Models: Old Faithful}


\begin{verbatim}
model faithnormhmm

{p0[1]<-0.5
 p0[2]<-0.5
 cc[1]~dcat(p0[])
 
 for (i in 2:30) 
   {cc[i]~dcat(q[,cc[i-1]])   # This is the 
    }                         # "burn-in" section.

 c[1]~dcat(q[,cc[30]])     # This is for 
                           # the initial state.
\end{verbatim}

\end{frame}

\begin{frame}[fragile]
\frametitle{Lecture 4.3: Hidden Markov Models: Old Faithful}


\begin{verbatim}
 for (i in 2:n)
   {c[i]~dcat(q[,c[i-1]])        
    }

 for (i in 1:n)
   {y[i]~dnorm(mu[c[i]],tau[c[i]])
    }

 for (j in 1:2)
   {tau[j]~dgamma(4,0.04)
    }
\end{verbatim}

\end{frame}

\begin{frame}[fragile]
\frametitle{Lecture 4.3: Hidden Markov Models: Old Faithful}


\begin{verbatim}
 mumean[1]<-mu0-0.2
 mumean[2]<-mu0+0.2
 mu[1]~dnorm(mumean[1],3.3) I(,mu[2]) # This imposes the
 mu[2]~dnorm(mumean[2],3.3) I(mu[1],) # order constraint.

 mu0~dnorm(4.0,p.mu)
 p.mu<-1/0.3

 q[1,2]<-pi
 pi~dbeta(1,1)
 q[2,2]<-1-q[1,2]
 q[1,1]<-0.0
 q[2,1]<-1-q[1,1]
 
 }
\end{verbatim}
\end{frame}

\begin{frame}
\frametitle{Lecture 4.3: Hidden Markov Models: Road traffic headways}
 The stationary distribution of the Markov chain in this case has

\begin{eqnarray*}
 \Pr(c_{1}=1) & = & \frac{\pi_{2}}{1+\pi_{2}-\pi_{1}}, \\
 \Pr(c_{1}=2) & = & \frac{ 1- \pi_{1}}{1+\pi_{2}-\pi_{1}}. 
\end{eqnarray*}


\end{frame}

\begin{frame}[fragile]
\frametitle{Lecture 4.3: Hidden Markov Models: Road traffic headways}
\begin{verbatim}
model headway

{p0[1]<-0.5
 p0[2]<-0.5
 cc[1]~dcat(p0[])
 for (i in 2:30) 
     {cc[i]~dcat(q[,cc[i-1]])   # This is the 
      }                         # "burn-in" section.
 
 c[1]~dcat(q[,cc[30]])
 t[1]~dgamma(alpha[c[1]],beta[c[1]])
\end{verbatim}


\end{frame}

\begin{frame}[fragile]
\frametitle{Lecture 4.3: Hidden Markov Models: Road traffic headways}
\begin{verbatim}
 for (i in 2:N) 
     {c[i]~dcat(q[,c[i-1]])
      t[i]~dgamma(alpha[c[i]],beta[c[i]])
      }
 
\end{verbatim}


\end{frame}

\begin{frame}[fragile]
\frametitle{Lecture 4.3: Hidden Markov Models: Road traffic headways}
\begin{verbatim}
 alpha[1]<-1
 alpha[2]<-1+aa
 aa~dgamma(1,0.5)
 beta[1]~dgamma(2,8) I(,bb)
 beta[2]<-alpha[2]*bb
 bb~dgamma(1,2) I(beta[1],)
 pi[1]~dbeta(1,2)
 pi[2]~dbeta(1,2)
 q[1,1]<-pi[1]
 q[1,2]<-pi[2]
 q[2,1]<-1-pi[1]
 q[2,2]<-1-pi[2]
\end{verbatim}


\end{frame}

\begin{frame}[fragile]
\frametitle{Lecture 4.3: Hidden Markov Models: Road traffic headways}
\begin{verbatim}

 lrr<-log(pi[1]/pi[2])
 pos<-step(lrr)
 mu[1]<-1/beta[1]
 mu[2]<-1/bb
}
\end{verbatim}

\end{frame}
\end{document}
