---
title: "Lab 5 - Part 2: Key"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(warning = FALSE)
library(datasets)
library(ggplot2)
library(dplyr)
```

This lab is an in-class demo for 536 students. It should be turned in along with Lab 5.

We previously studied recursive Bayesian estimation for an unmoving object. Now we will build upon that for a moving object (such as a price).


#### Recursive Bayesian Modeling with a Moving Object

- The classic example of a state-space model is concerned with tracking the position of an object that is observed with errors. The same framework can be used in several other cases, such as the price of avocados that evolve in time.

##### Sampling Model

- The sampling model now includes a $\theta_t$ term rather than $\theta$, to denote that $\theta$ evolves in time. The observations are then modeled as:
$$y_t = \theta_t + \epsilon_t, \; \; \; \epsilon_t \sim N(0,\sigma_{\epsilon}^2) \; \text{and  $\epsilon_t$ are iid.}$$
and 
$$\theta_t = \theta_{t-1} + \nu + w_t, \; \; \; w_t \sim N(0,\sigma_w^2) \; \text{and  $\nu$ is a speed term}.$$

- The first equation, $y_t = \theta_t + \epsilon_t$ is known as the observation equation.

- The second equation, $\theta_t = \theta_{t-1} + \nu + w_t$ is known as the evolution equation.

- With the standard priors, at each time point ($t$) we have:

1.  *A Predictive Step*: to predict the next observed response 
    - This is attained by first finding the distribution of the latent state $\theta_{t+1}|y_{1:t}$ at time $t+1$, which propogates $\theta_t$ forward to $\theta_{t+1}$.
    - Then, $\theta_{t+1}$ is used to make a prediction at time point $t+1$, which is formally the distribution $y_{t+1}|y_{1:t}$
  
2. *An Estimation Step:* to estimate the value of the latent state $\theta_{t+1}$ after observed the data at time time $t+1$
    - After observing $y_{t+1}$ the distribution of $\theta_{t+1}|y_{1:t+1}$ can be updated.
  
  
  
##### Questions

1. Write out the following distributions and define paramters: $\theta_{t+1}|y_{1:t}$, $y_{t+1}|y_{1:t}$, and $\theta_{t+1}|y_{1:t+1}$. This should be defined in a recursive fashion.

2. Simulate a model with this process, where $\theta$ evolves in time. Find a way to graphical display both the unobserved (latent) $\theta$ values and the observed responses.