## Project 2, Problem 2

1. Write you own R-function that fits a zero-mean AR(2)-model $Z_t=\phi_1 Z_{t-1} + \phi_2 Z_{t-2} + a_t$ to $n$ observations $z_1,z_2,\dots,z_n$. The function should take a vector z as its argument and return a vector containing the exact maximum likelihood estimates of $\phi_1,\phi_2$ and $\sigma_a^2$. You're allowed to use the functions optim and lm but not arima. Your code should compute a good choice of initial parameter values before making the call to optim. To derive the exact likelihood function you need to consider the distribution of $Z_1$, $Z_2|Z_1$ and $Z_t|Z_{t-1},Z_{t-2}$ for $t>2$.
2. Check that your function produce reasonable results by applying it to simulated data generated using arima.sim.
3. Write the above AR(2) model in state-space form with $Z_t$ and $Z_{t+1}$ as state variables assuming that $Z_t$ is observed with time dependent observation error variance $\sigma_{b,t}^2$. How does the Kalman recursions change when the variance of the observation error is time dependendent and when some observations are missing entirely? Assuming stationarity, find $V_{1|0}$. Write an R-function taking the model parameters, a vector of observed non-missing and missing observations and a vector containing known values for $\sigma_{b,t}^2$ as input and computes and returns forecasted, filtered and smoothed means and variance matrices. Construct numerical examples and run your function on these to verify that the computed quantities behaves as you expect. In particular, look at the behaviour of the filter if there is a large gap with missing values somewhere in the observed time series.
4. Change your function such that it optionally computes the log likelihood of the observed non-missing data. Use the optim R-function with carefully chosen starting values to compute maximum likelihood estimates of the parameters. Construct some numerical examples and verify that your code works by comparing with MLEs computed by the arima R-function. You should maximise the log likelihood only with respect to $\phi_1, \phi_2, \sigma_a^2$ while using your own chosen values for the $\sigma_{b,t}^2$'s.