diff --git a/week7/seminar_code.R b/week7/seminar_code.R index 2e46d23..d111373 100644 --- a/week7/seminar_code.R +++ b/week7/seminar_code.R @@ -4,32 +4,35 @@ library(fpp3) # Facebook stock fb <- gafa_stock |> - filter(Symbol == "FB") |> - select(Date, Close) |> - fill_gaps() + filter(Symbol == "FB") fb |> gg_tsdisplay(Close) fb |> - gg_tsdisplay(Close |> difference()) + gg_tsdisplay(difference(Close)) # aus_production Bricks +aus_production |> + gg_tsdisplay(Bricks) aus_production |> gg_tsdisplay( box_cox(Bricks, lambda = 0.3) ) aus_production |> gg_tsdisplay( - box_cox(Bricks, lambda = 0.3) |> difference(lag = 4) + box_cox(Bricks, lambda = 0.3) |> + difference(lag = 4) ) aus_production |> gg_tsdisplay( - box_cox(Bricks, lambda = 0.3) |> difference(lag = 4) |> difference() + box_cox(Bricks, lambda = 0.3) |> + difference(lag = 4) |> + difference() ) # Unit root tests fb |> features(Close, unitroot_kpss) -fb |> features(Close |> difference(), unitroot_kpss) +fb |> features(difference(Close), unitroot_kpss) fb |> features(Close, unitroot_ndiffs) ## Seasonal strength @@ -43,24 +46,10 @@ aus_production |> box_cox(Bricks, lambda = 0.3), unitroot_nsdiffs ) - -## Unit root -aus_production |> - features( - box_cox(Bricks, lambda = 0.3) |> difference(lag = 4), - unitroot_kpss - ) aus_production |> features( - box_cox(Bricks, lambda = 0.3) |> difference(lag = 4), + box_cox(Bricks, lambda = 0.3) |> + difference(lag = 4), unitroot_ndiffs ) -# Random walks - -tsibble( - time = 1:500, - value = cumsum(rnorm(500) + 0.1), - index = time -) |> - gg_tsdisplay(value) diff --git a/week7/slides.qmd b/week7/slides.qmd index 0a235a7..54f12f6 100644 --- a/week7/slides.qmd +++ b/week7/slides.qmd @@ -89,52 +89,3 @@ If $F_s > 0.64$, do one seasonal difference. ## Your turn 3. Do the unit root tests for the `Gas` series from `aus_accommodation`. Do they give the same numbers of difference as you chose? - -# Random walks - -## Random walk model - -If differenced series is white noise with zero mean: - -\begin{block}{} -\centerline{$y_t-y_{t-1}=\varepsilon_t$ \hspace{0.4cm} or \hspace{0.4cm} $y_t=y_{t-1}+\varepsilon_t$} -\end{block}\vspace*{-0.3cm} -where $\varepsilon_t \sim NID(0,\sigma^2)$. - -* Model behind the \alert{naïve method}. -* Forecast are equal to the last observation (future movements up or down are equally likely). - -\vspace*{10cm} - -## Random walk model - -\begin{block}{} -\centerline{$y_t=y_{t-1}+\varepsilon_t$ \qquad -where $\varepsilon_t \sim NID(0,\sigma^2)$.} -\end{block}\pause -\vspace*{-1.3cm} -\begin{align*} -y_{T+h} &= y_{T+h-1} + \varepsilon_{T+h} \\ - &= y_{T+h-2} + \varepsilon_{T+h-1} + \varepsilon_{T+h} \\ - &= \dots \\ - &= y_T + \varepsilon_{T+1} + \dots + \varepsilon_{T+h} -\end{align*} -\pause\vspace*{-1cm} -\begin{align*} -\text{So}\qquad &\text{E}(y_{T+h}|y_1,\dots,y_T) = y_T \\ -\text{and}\qquad &\text{Var}(y_{T+h}|y_1,\dots,y_T) = h\sigma^2 -\end{align*}\vspace*{10cm} - -## Random walk with drift model - -If differenced series is white noise with non-zero mean: - -\begin{block}{} -\centerline{$y_t-y_{t-1}=c+\varepsilon_t$ \hspace{0.4cm} or \hspace{0.4cm} $y_t=c+y_{t-1}+\varepsilon_t$} -\end{block}\vspace*{-0.3cm} -where $\varepsilon_t \sim NID(0,\sigma^2)$. - -* $c$ is the \alert{average change} between consecutive observations. -* Model behind the \alert{drift method}. - -\vspace*{10cm} diff --git a/week8/slides.qmd b/week8/slides.qmd index 56c8e6c..1568312 100644 --- a/week8/slides.qmd +++ b/week8/slides.qmd @@ -23,6 +23,57 @@ library(purrr) ``` +# Random walks + +## Random walk model + +If differenced series is white noise with zero mean: + +\begin{block}{} +\centerline{$y_t-y_{t-1}=\varepsilon_t$ \hspace{0.4cm} or \hspace{0.4cm} $y_t=y_{t-1}+\varepsilon_t$} +\end{block}\vspace*{-0.3cm} +where $\varepsilon_t \sim NID(0,\sigma^2)$. + +* Model behind the \alert{naïve method}. +* Forecast are equal to the last observation (future movements up or down are equally likely). + +\vspace*{10cm} + +## Random walk model + +\begin{block}{} +\centerline{$y_t=y_{t-1}+\varepsilon_t$ \qquad +where $\varepsilon_t \sim NID(0,\sigma^2)$.} +\end{block}\pause +\vspace*{-1.3cm} +\begin{align*} +y_{T+h} &= y_{T+h-1} + \varepsilon_{T+h} \\ + &= y_{T+h-2} + \varepsilon_{T+h-1} + \varepsilon_{T+h} \\ + &= \dots \\ + &= y_T + \varepsilon_{T+1} + \dots + \varepsilon_{T+h} +\end{align*} +\pause\vspace*{-1cm} +\begin{align*} +\text{So}\qquad &\text{E}(y_{T+h}|y_1,\dots,y_T) = y_T \\ +\text{and}\qquad &\text{Var}(y_{T+h}|y_1,\dots,y_T) = h\sigma^2 +\end{align*}\vspace*{10cm} + +## Random walk with drift model + +If differenced series is white noise with non-zero mean: + +\begin{block}{} +\centerline{$y_t-y_{t-1}=c+\varepsilon_t$ \hspace{0.4cm} or \hspace{0.4cm} $y_t=c+y_{t-1}+\varepsilon_t$} +\end{block}\vspace*{-0.3cm} +where $\varepsilon_t \sim NID(0,\sigma^2)$. + +* $c$ is the \alert{average change} between consecutive observations. +* Model behind the \alert{drift method}. + +\vspace*{10cm} + +# Backshift operator notation + ## Backshift operator notation * $B$ shifts the data back one period. $B y_{t} = y_{t - 1}$ @@ -32,6 +83,8 @@ library(purrr) * A seasonal difference followed by a first difference can be written as $(1-B)(1-B^m)y_t$ +# Autoregressive (AR) models + ## AR(1) model \begin{block}{} @@ -73,6 +126,8 @@ We normally restrict autoregressive models to stationary data, and then some con * More complicated conditions hold for $p\ge3$. * fable takes care of this. +# Moving Average (MA) models + ## Moving Average (MA) models A multiple regression with \textbf{past \emph{errors}} as predictors. @@ -98,6 +153,7 @@ A multiple regression with \textbf{past \emph{errors}} as predictors. * More complicated conditions hold for $q\ge3$. * fable takes care of this. +# ARIMA models ## ARIMA models \fontsize{14}{14.5}\sf\vspace*{0.2cm}