--- title: "STAT 436 / 536 - Lecture 7: Key" date: September 26, 2018 output: pdf_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) knitr::opts_chunk$set(warning = FALSE) knitr::opts_chunk$set(message = FALSE) knitr::opts_chunk$set(fig.align= 'center') knitr::opts_chunk$set(fig.height= 3) knitr::opts_chunk$set(fig.width = 5) ``` ## Stochastic Models - Thus far we have seen two approaches for estimating a time series. 1. The `decompose` function estimates the trend and seasonal patterns for a time series. \vfill 2. The `HoltWinters` function uses exponentially weighted averages to estimate the mean, trend, and seasonal components. \vfill - When fitting time series models, most of the deterministic features of the time series can be captured in various ways, but regardless of the approach, we still have a \vfill - Sometimes the deterministic features capture the time series behavior, so that the residual error series is \vfill - Otherwise, if the residual error series contains \vfill #### White Noise - If the time series model is defined for value $y$, then the residual time series can be defined as: \vfill - A time series exhibits white noise if $x_t = w_t$, where $w_t$ are \vfill ```{r} set.seed(09192018) library(dplyr) library(ggfortify) rnorm(100) %>% as.ts() %>% autoplot() + ggtitle('White Noise') ``` \vfill \newpage -The second order properties for white noise are: the mean term \vfill - the covariance $\gamma_k(w_t, w_{t+k}) = 0$ \vfill - the correlation $\rho_k(w_t, w_{t+k}) =0$ \vfill - Q: will the sample correlation necessarily be zero from a simulation? \vfill ```{r, eval = F} w <- ts(rnorm(100)) acf.obj <- acf(w) acf.obj ``` \vfill \vfill - When fitting this model, what parameters would we need to estimate? \vfill \newpage #### Random Walks - Let $\{x_t\}$ be a time series object, then this is a random walk if... \vfill \vfill - Using back substitution, this series can be written as: \vfill \vfill - The textbook defines **B** \vfill - The second order properties of the random walk are $\mu_x = 0$ and $\gamma_k(t) = t \sigma^2$ (Note this is on HW4). \vfill - The autocorrelation $\rho_k(t) = \frac{1}{\sqrt{1+k/t}}$ \vfill - Note this results in a non-stationary time series as the covariance depends on $t$. \vfill - A common approach with a non-stationary time series, such as a random walk, is to take the difference between consecutive time points. This is denoted as \vfill - **Q:** what is the resulting time series after applying the differencing operator to a random walk time series $\{x_t\}$? \vfill - Sketch out pseudocode to simulate a random walk. \vfill \newpage ```{r} time.pts <- 100 random.walk <- rep(0,time.pts) sigma.w <- 1 for (t in 2:time.pts){ random.walk[t] <- random.walk[t-1] + rnorm(sigma.w) } random.walk %>% as.ts() %>% autoplot() + ggtitle('Simulated Random Walk') ``` \vfill - ACF plots for random walk and differenced random walk. ```{r, fig.height=4, fig.width=6} par(mfcol=c(1,2)) acf(random.walk); acf(diff(random.walk)) ``` \newpage - In some situations, a purely random walk model may not be appropriate. Consider the following figure. ```{r, echo = FALSE} par(mfcol=c(1,1)) time.pts <- 100 random.walk.drift <- rep(0,time.pts) const <- .5 sigma.w <- 1 for (t in 2:time.pts){ random.walk.drift[t] <- random.walk.drift[t-1] + rnorm(sigma.w) + const } random.walk.drift %>% as.ts() %>% autoplot() + ggtitle('Simulated Random Walk?') ``` \vfill - This is a random walk \vfill \vfill - The \vfill ```{r} drift.est <- random.walk.drift %>% diff() %>% mean() %>% round(digits = 2) ``` where the estimate of $\delta$ is `r drift.est`. \vfill - The Holt-Winters function can be used to estimate both of these time series data sets. **Random Walk** ```{r} HW.rw <- HoltWinters(random.walk, gamma = FALSE, beta = FALSE) HW.rw$alpha random.walk[time.pts] ``` \vfill \newpage ```{r, eval = F} rw.pred <- predict(HW.rw, n.ahead = 5, prediction.interval = T); rw.pred plot(HW.rw, rw.pred) ``` **Random Walk with Drift** ```{r} HW.rw.drift <- HoltWinters(random.walk.drift, gamma = FALSE) HW.rw.drift$alpha; HW.rw.drift$coefficients['b'] rw.drift.pred <- predict(HW.rw.drift, n.ahead = 5, prediction.interval = T) rw.drift.pred plot(HW.rw.drift, rw.drift.pred) ```