Skip to content

Commit

Permalink
week 7 seminar code and slides
Browse files Browse the repository at this point in the history
  • Loading branch information
robjhyndman committed Apr 19, 2024
1 parent 85587e0 commit b703353
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 72 deletions.
35 changes: 12 additions & 23 deletions week7/seminar_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
49 changes: 0 additions & 49 deletions week7/slides.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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}
56 changes: 56 additions & 0 deletions week8/slides.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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}$
Expand All @@ -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}{}
Expand Down Expand Up @@ -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.

Expand All @@ -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}
Expand Down

0 comments on commit b703353

Please sign in to comment.