# The setup

Let $$X = (x_1, \ldots, x_T)$$ denote a state variable that follows a Markov process, where $$x_t \in S$$. The transition distribution is denoted by $$$$p(x_{t}|x_{t-1}) .$$$$ The joint distribution for $$X$$ is given by $$$$p(X) = p(x_1)\prod_{t=2}^T p(x_{t}|x_{t-1}) ,$$$$ where $$p(x_1)$$ is the marginal distribution for $$x_1$$. This latter distribution may be expressed in terms of the marginal distribution for $$x_0$$: $$$$\label{x1_marg} p(x_1) = \int_S p(x_1|x_0)\,p(x_0)\,dx_0 .$$$$

Let $$Y = (y_1, \ldots, y_T)$$ denote a collection of observations that depend on the state variable. Let the observations depend on the states as follows: $$$$p(Y|X) = \prod_{t=1}^T p(y_t|x_t) ,$$$$ where $$$$p(y_t|x_t)$$$$ is sometimes known as the measurement distribution. Note $$x_0$$ does not have an observation associated with it.

# The problem

Use the forward-backward algorithm to make a random draw of the unobserved Markov process $$X$$ from the posterior distribution
$$$$p(X|Y) \propto p(Y|X)\,p(X) .$$$$ As part of the answer, illustrate the details of the algorithm in the context of the following two specific cases.

### Gaussian model

For the first example, suppose \begin{align} p(x_{t}|x_{t-1}) &= \textsf{N}(x_{t}|a + b\,x_{t-1},s^2) \\ p(y_t|x_t) &= \textsf{N}(y_t|\alpha + \beta\,x_t, \tau^2) . \end{align} In this example the state space is the real line: $$x_t \in S = \mathbb{R}$$.

### Hidden Markov model

For the next example, let $$S = \{1, \ldots, K\}$$. Let $$Q$$ be a $$K \times K$$ transition matrix where $$Q_{jk} \ge 0$$ and $$\sum_{k\in S} Q_{jk} = 1$$ for all $$j \in S$$. In particular, $$$$p(x_t=k|x_{t-1}=j) = Q_{jk} .$$$$ Let the observations depend on the state variable and on $$t$$ as well in the following way: $$$$p(y_t|x_t = j) = \textsf{N}(y_t|\alpha_j + \beta_j\,t,\sigma^2) .$$$$

## Parameter estimation

Finally, explain how to proceed if the transition and observation distributions depend on unknown parameters.

# The forward-backward algorithm

This presentation of the forward-backward algorithm is similar to the textbook treatment found in Greenberg (2013) Introduction to Bayesian Econometrics (pp. 193-195).

Let $$Y_t = (y_1, \ldots, y_t)$$. The forward part of the algorithm (sometimes called filtering) computes $$p(x_t|Y_t)$$ from $$p(x_{t-1}|Y_{t-1})$$ for $$t = 1, \ldots, T$$. This forward recursion is initialized with $$p(x_0|Y_0) = p(x_0)$$, which is the distribution for $$x_0$$ absent any observations. The recursion can be decomposed into two steps; namely, the prediction step $$$$p(x_t|Y_{t-1}) = \int_S p(x_t|x_{t-1})\,p(x_{t-1}|Y_{t-1})\,d x_{t-1} ,$$$$ followed by the observation step $$$$p(x_t|Y_t) \propto p(y_t|x_t)\,p(x_t|Y_{t-1}) .$$$$ The prediction step produces a prior distribution for the observation step in which the observation distribution is the likelihood. The filtered distributions $$p(x_t|Y_t)$$ play a central role in the backward part of the algorithm, which where the draw of $$X$$ is made.

The backward part of the algorithm (sometimes called smoothing) is based on the following factorization of the joint posterior distribution for the state variable: $$$$p(X|Y) = p(x_T|Y) \prod_{t=1}^{T-1} p(x_t|Y,X^{t+1}) ,$$$$ where $$X^{t+1} = (x_{t+1}, \ldots, x_T)$$. The backward part of the algorithm draws states sequentially beginning with $$p(x_T|Y)$$ and then using $$p(x_{t}|Y,X^{t+1})$$ repeatedly. The result is a random draw from $$p(X|Y)$$.

In order to sample from $$p(x_t|Y,X^{t+1})$$ we simplify it and express it in a form that relies on the filtered distributions computed in the forward part. Begin by breaking up the conditioning variables in the distribution as follows: $$$$p(x_t|Y,X^{t+1}) \equiv p(x_t|Y_t,Y^{t+1},x_{t+1},X^{t+2}) .$$$$ As can be seen in the Figure, information contained in either $$Y^{t+1}$$ or $$X^{t+2}$$ must pass through $$x_{t+1}$$ to reach $$x_t$$. Therefore, $$x_t$$ is independent of both $$Y^{t+1}$$ and $$X^{t+2}$$ as long as $$x_{t+1}$$ is in the conditioning list. Consequently, $$$$p(x_{t}|Y,X^{t+1}) = p(x_t|Y_t,x_{t+1}) .$$$$ Finish by using Bayes' rule to write $$$$p(x_t|Y_t,x_{t+1}) \propto p(x_{t+1}|x_{t})\,p(x_{t}|Y_{t}) .$$$$ The right-hand side provides the key to sampling from $$p(X|Y)$$. Note in passing it is possible, given the draw of $$x_1$$, to go one step farther with the backward part of the algorithm and draw $$x_0 \sim p(x_0|x_1) \propto p(x_1|x_0)\,p(x_0)$$.

### Observable covariates

The second example given in the question requires a slight generalization of the setup because the observation distribution depends on an observable covariate, $$z_t$$. (In the example, $$z_t = t$$.) It is straightforward to include observable covariates in the observation distribution: $$$$p(y_t|x_t,z_t) ,$$$$ where $$z_t$$ is an observable variable (or a vector of observable variables). Then $$p(Y|X,Z) = \prod_{t=1}^T p(y_t|x_t,z_t)$$ and $$p(X|Y,Z) \propto p(Y|X,Z)\,p(X)$$.
The forward and backward equations are characterized by \begin{align} p(x_t|Y_t,Z_t) &\propto p(y_t|x_t,z_t) \int_S p(x_t|x_{t-1})\,p(x_{t-1}|Y_{t-1},Z_{t-1})\,dx_{t-1} \\ p(x_t|Y_t,Z_t,x_{t+1}) &\propto p(x_{t+1}|x_{t})\,p(x_t|Y_t,Z_t) , \end{align} where $$Z_t = (z_1, \ldots, z_t)$$.

## Illustration I: Gaussian model

To derive the forward recursion, begin by assuming $$$$p(x_{t-1}|Y_{t-1}) = \textsf{N}(x_{t-1}|\mu_{t-1}, \sigma_{t-1}^2) ,$$$$ where $$\mu_{t-1}$$ and $$\sigma_{t-1}^2$$ are sufficient statistics for $$Y_{t-1}$$. Then the prediction step implies $$$$p(x_t|Y_{t-1}) = \textsf{N}(x_t|M_{t-1}, V_{t-1})$$$$ where $$$$M_{t-1} = a+b\,\mu_{t-1} \qquad\text{and}\qquad V_{t-1} = s^2 + b^2\,\sigma_{t-1}^2 .$$$$ Consequently, the observation step delivers $$$$p(x_t|Y_t) = \textsf{N}(x_t|\mu_t,\sigma_t^2) ,$$$$ where $$$$\mu_t = \sigma_t^2 \left(\frac{M_{t-1}}{V_{t-1}} + \frac{(y_t - \alpha)/\beta}{\tau^2/\beta^2}\right) \qquad\text{and}\qquad \sigma_t^2 = \left(\frac{1}{V_{t-1}} + \frac{1}{\tau^2/\beta^2}\right)^{-1} .$$$$ The forward recursion is initialized with $$(\mu_0,\sigma_0^2)$$ and produces $$\{(\mu_t,\sigma_t^2)\}_{t=1}^T$$. This recursion is an example of the Kalman filter.

The smoothing distribution is given by $$$$p(x_t|Y_t,x_{t+1}) = \textsf{N}(x_t| \mathcal{M}_t(x_{t+1}),\mathcal{V}_t) ,$$$$ where $$$$\mathcal{M}_t(x_{t+1}) = \mathcal{V}_t \left(\frac{\mu_t}{\sigma_t^2} + \frac{(x_{t+1}-a)/b}{s^2/b^2}\right) \qquad\text{and}\qquad \mathcal{V}_t = \left(\frac{1}{\sigma_t^2} + \frac{1}{s^2/b^2}\right)^{-1} .$$$$ Let $$X^{(r)} = (x_1^{(r)},\ldots, x_T^{(r)})$$ denote the $$r$$-th draw of $$X$$. The backward part begins with $$$$x_T^{(r)} \sim \textsf{N}(x_T|\mu_T,\sigma_T^2)$$$$ followed by $$$$x_{T-1}^{(r)} \sim \textsf{N}(x_{T-1}|\mathcal{M}_{T-1}(x_{T}^{(r)}),\mathcal{V}_{T-1})$$$$ and so forth until finally $$$$x_1^{(r)} \sim \textsf{N}(x_1|\mathcal{M}_1(x_2^{(r)}),\mathcal{V}_1) .$$$$

### Side note: Special example

Let $$(a,b,s^2) = (0,1,0)$$ so that $$x_t = x_0$$ for all $$t$$. In addition let $$(\alpha,\beta,\tau^2) = (0,1,\tau^2)$$ so that $$p(y_t|x_t) = \textsf{N}(y_t|x_0,\tau^2)$$. Evidently $$x_0$$ is the common (unknown) mean of the observations. (The variance of the observations, $$\tau^2$$, is known.)

In this case the prediction step produces $$M_{t-1} = \mu_{t-1}$$ and $$V_{t-1} = \sigma_{t-1}^2$$, and the observation step delivers $$p(x_0|Y_t) = \textsf{N}(x_0|\mu_t,\sigma_t^2)$$, where $$$$\mu_t = \sigma_t^2 \left(\frac{\mu_{t-1}}{\sigma_{t-1}^2} + \frac{y_t}{\tau^2}\right) \qquad\text{and}\qquad \sigma_t^2 = \left(\frac{1}{\sigma_{t-1}^2} + \frac{1}{\tau^2}\right)^{-1} .$$$$ If we further assume $$(\mu_0,\sigma_0^2) = (\mu_0,\infty)$$, so that there is effectively no prior information regarding $$x_0$$, then $$$$\mu_t = \frac{1}{t} \sum_{s=1}^t y_s \qquad\text{and}\qquad \sigma_t^2 = \frac{\tau^2}{t} .$$$$

The initial step of the backward recursion is given by $$p(x_0|Y_T) = \textsf{N}(x_0|\mu_T,\sigma_T^2)$$. For additional steps, one may show (by taking the limit as $$s^2 \to 0$$) that $$\mathcal{M}_t(x_{t+1}) = x_{t+1}$$ and $$\mathcal{V}_t = 0$$.

## Illustration II: Hidden Markov model

I begin with two preliminary comments. First, given the discrete nature of $$S$$, some integrals become summations, such as $$$$\int_S p(x_t|x_{t-1})\,p(x_{t-1})\,dx_{t-1} \quad\rightarrow\quad \sum_{x_{t-1}\in S} p(x_t|x_{t-1})\,p(x_{t-1}) .$$$$ Second, the observations depend on an observable covariate, $$z_t = t$$, which is made explicit here: $$$$p(y_t|x_t = j, z_t = t) = \textsf{N}(y_t|\alpha_j + \beta_j\,t,\sigma^2) .$$$$

The forward-backward algorithm can be expressed compactly in this case. Let $$L$$ and $$G$$ denote $$T \times K$$ matrices where \begin{align} L_{tj} &= p(y_t|x_t = j, z_t = t) \\ G_{tj} &= p(x_t=j|Y_{t},Z_t) . \end{align} Let $$G_0$$ denote the (row) vector of probabilities for $$x_0$$ absent any observations, so that $$G_{0j} = p(x_0=j)$$. The input for the algorithm consists of $$(L,Q,G_0)$$.

The forward part of the algorithm can be expressed as the recursion $$$$G_{t} \propto L_t \circ (G_{t-1} Q) ,$$$$ where $$G_t$$ is the $$t$$-th row of $$G$$, $$L_t$$ is the $$t$$-th row of $$L$$, and "$$\circ$$" denotes the Hadamard product (component-by-component). Note the prediction step is embodied in the expression $$G_{t-1} Q$$.

Turning to the backward part, let $$(Q^\top)_k$$ denote the $$k$$-th row of the transpose of $$Q$$ and let $$H_t^k$$ denote the row vector of probabilities where $$$$H_t^k \propto (Q^\top)_k \circ G_t .$$$$ The probabilities for the backward part are given by $$$$p(x_T=j|Y) = G_{Tj} ,$$$$ and $$$$p(x_t = j|Y_t,Z_t,x_{t+1} = k) = H_{tj}^k .$$$$

# What to do if the parameters are not known?

Thus far we have assumed the parameters of the transition and observation distributions were known. How can one proceed if the parameters are not known? The answer involves using the Gibbs sampler.

We can include the parameters in the notation as follows: $$$$p(x_t|x_{t-1},\theta_x) \qquad\text{and}\qquad p(y_t|x_t,\theta_y) .$$$$ In the Gaussian example, $$\theta_x = (a,b,s^2)$$ and $$\theta_y = (\alpha,\beta,\tau^2)$$. In the HMM example, $$\theta_x = Q$$ and $$\theta_y = (\phi,\sigma^2)$$, where $$\phi = (\phi_1, \ldots, \phi_K)$$ and $$\phi_j = (\alpha_j, \beta_j)$$.

It is convenient to assume prior independence, where $$p(\theta_x,\theta_y) = p(\theta_x)\,p(\theta_y)$$. Then the posterior distribution for the complete set of unknowns is $$$$p(X,\theta_x,\theta_y|Y) \propto p(Y|X,\theta_y)\,p(X|\theta_x)\,p(\theta_x)\,p(\theta_y) .$$$$ Note that $$p(x_1|\theta_x) = \int p(x_1|x_0,\theta_x)\,p(x_0|\theta_x)\,dx_0$$ where the marginal distribution for $$x_0$$ may or may not depend on $$\theta_x$$. The joint distribution for the unobserved state variable and the unknown parameters can be characterized by the following collection of full conditional distributions: \begin{align} p(X|Y,\theta_x,\theta_y) &\propto p(Y|X,\theta_y)\,p(X|\theta_x) \\ p(\theta_x|X,Y,\theta_y) &\propto p(X|\theta_x)\,p(\theta_x) \\ p(\theta_y|X,Y,\theta_x) &\propto p(Y|X,\theta_y)\,p(\theta_y) . \end{align} The Gibbs sampler produces draws from the joint posterior distribution by sequentially drawing from the full conditional distributions. Given $$(X^{(r-1)}, \theta_x^{(r-1)}, \theta_y^{(r-1)})$$, the next draw in the chain can be computed as follows: \begin{align} X^{(r)} &\sim p(X|Y,\theta_x^{(r-1)}, \theta_y^{(r-1)}) \\ \theta_x^{(r)} &\sim p(\theta_x|Y,X^{(r)},\theta_y^{(r-1)}) \\ \theta_y^{(r)} &\sim p(\theta|Y,X^{(r)},\theta_x^{(r)}) . \end{align} The draws from $$X^{(r)}$$ are made using the forward-backward algorithm described above, treating $$(\theta_x^{(r-1)},\theta_y^{(r-1)})$$ as known. The way in which draws from the other two distributions are made will depend on the specific case.

## Gibbs sampler for the Gaussian example

Recall $$\theta_x = (a,b,s^2)$$ and $$\theta_y = (\alpha,\beta,\tau^2)$$. Let $$p(\theta_x) \propto 1/s^2$$ and let $$p(\theta_y) \propto 1/\tau^2$$.

It is convenient to augment the list of unknowns with $$x_0$$. In this case we have $$$$p(X, x_0,\theta_x,\theta_y|Y) \propto p(Y|X,\theta_y)\, p(X|x_0,\theta_x)\,p(x_0)\,p(\theta_x)\,p(\theta_y) .$$$$ We have already discussed the possibility of using the forward-backward algorithm to draw from $$$$p(X,x_0|-) \propto p(Y|X,\theta_y)\, p(X|x_0,\theta_x)\,p(x_0)$$$$ by taking one additional backward step.
Then $$$$p(\theta_x|-) \propto p(X|x_0,\theta_x)\,p(\theta_x) = \frac{1}{s^2} \prod_{t=1}^T \textsf{N}(x_t|a+b\,x_{t-1}, s^2) .$$$$ If either $$\mu_0$$ or $$\sigma_0^2$$ depends on $$\theta_x$$, then the factor $$p(x_0|\theta_x)$$ must be included in the kernel for $$p(\theta_x|-)$$. Finally, $$$$p(\theta_y|-) \propto p(Y|X,\theta_y)\,p(\theta_y) = \frac{1}{\tau^2} \prod_{t=1}^T \textsf{N}(y_t|\alpha + \beta\,x_t, \tau^2) .$$$$ One can make standard Gibbs-sampler draws of $$\theta_x$$ and $$\theta_y$$.

## Gibbs sampler for HMM example

Recall $$\theta_x = Q$$ and $$\theta_y = (\phi, \sigma^2)$$. We begin with the draws of $$Q$$. Let $$T_j$$ denote the number of times $$x_t = j$$ for $$t = 1, \ldots, T-1$$. Let $$C$$ denote a $$K\times K$$ matrix where $$C_{jk}$$ is the number of times $$(x_t, x_{t+1}) = (j,k)$$ for $$t = 1, \ldots, T-1$$, and let $$C_j$$ denote the $$j$$-th row of $$C$$. Then $$$$p(C_j|Q_j,T_j) = \textsf{Multinomial}(C_j|T_j,Q_j) .$$$$ If $$p(Q_j) = \textsf{Dirichlet}(Q_j|\alpha_j)$$, then $$$$p(\theta_x|-) = \prod_{j=1}^K p(Q_j|C_j) ,$$$$ where $$$$p(Q_j|C_j) = \textsf{Dirichlet}(Q_j|\alpha_j + C_j) .$$$$

We now turn to the draws of $$\theta_y$$. Let $$$$\mathcal{Y}_j = \{y_t : x_t = j\} .$$$$ (Note $$\cup_{j=1}^K \mathcal{Y}_j = Y$$.) Let $$$$p(\mathcal{Y}_j|\phi_j,\sigma^2) = \prod_{y_t \in \mathcal{Y}_j} p(y_t|\phi_j,\sigma^2) .$$$$ Then $$$$p(\phi|-) = \prod_{j=1}^K p(\phi_j|-)$$$$ where $$$$p(\phi_j|-) \propto p(\phi_j) \prod_{t\in\tau_j} p(y_t|x_t,\theta) .$$$$ For $$\sigma^2$$ we have $$$$p(\sigma^2|-) \propto p(Y|X,\theta_y)\,p(\sigma^2) .$$$$