forked from TBrost/BYUI-Timeseries-Drafts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter_4_lesson_2_handout.qmd
349 lines (277 loc) · 6.9 KB
/
chapter_4_lesson_2_handout.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
---
title: "White Noise and Random Walks: Part 2 -- Handout"
subtitle: "Chapter 4: Lesson 2"
format: html
editor: source
sidebar: false
---
```{r}
#| include: false
source("common_functions.R")
```
```{=html}
<script type="text/javascript">
function showhide(id) {
var e = document.getElementById(id);
e.style.display = (e.style.display == 'block') ? 'none' : 'block';
}
function openTab(evt, tabName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(tabName).style.display = "block";
evt.currentTarget.className += " active";
}
</script>
```
#### Difference Operator
Let $\{x_t\}$ be a time series with the following values.
<center>
```{r, results='asis'}
#| echo: false
set.seed(6)
n <- 8
d_operator <- data.frame(t = c(1:n), x = sample(1:15, n, replace = FALSE)) |>
mutate(diff = t - n)
#cat( paste( paste0("$x_{t", ifelse(d_operator$t==n,"",d_operator$t-n), "} = ", d_operator$x, "$"), collapse = ",$~$ " ) )
cat( paste( paste0("$x_{", d_operator$t, "} = ", d_operator$x, "$"), collapse = ",$~$ " ) )
# Computes the value of the "power_on_d"^th difference from x_n
d_value <- function(power_on_d = 0) {
out <- d_operator |> #### Note the use of this global variable
filter(diff == -power_on_d) |>
dplyr::select(x) |>
pull()
return(out)
}
ts_val <- function(t_value) {
out <- d_operator |> #### Note the use of this global variable
filter(t == t_value) |>
dplyr::select(x) |>
pull()
return(out)
}
```
</center>
<!-- Beginning of two columns -->
::: columns
::: {.column width="45%"}
- Find the first differences, $\nabla x_t$
- Find the second differences, $\nabla^2 x_t$.
- Fill in the missing steps:
\begin{align*}
\nabla^2 x_8 &= (1-\mathbf{B} )^2 x_8 \\
&= (1-\mathbf{B} ) \left[ (1-\mathbf{B} ) x_8 \right] \\
& ~~~~~~~~~~~~~~~~~~~~~~ ⋮ \\
&= (x_8-x_7)-(x_7-x_6)
\end{align*}
and check that this is equal to the last term in the sequence of second differences.
:::
::: {.column width="10%"}
<!-- empty column to create gap -->
:::
::: {.column width="45%"}
```{r}
#| echo: false
d_operator |>
dplyr::select(-diff) |>
mutate(diff1 = x - lag(x)) |>
mutate(diff2 = diff1 - lag(diff1)) |>
rename(
"$$t$$" = t,
"$$x_t$$" = x,
"$$\\nabla x_t$$" = diff1,
"$$\\nabla^2 x_t$$" = diff2
) |>
replace_na_with_char("") |>
replace_cells_with_char(rows = 1:8, cols = 3:4) |>
display_table("0.5in")
```
:::
:::
<!-- End of two columns -->
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
#### Computing Differences
::: {.callout-tip appearance="minimal"}
<!-- ##### Linear Function -->
<!-- Beginning of two columns -->
::: columns
::: {.column width="45%"}
```{r}
#| echo: false
#| fig-asp: 1
#| fig-width: 3
linear_fcn <- function(t) { return(5 + 2.5 * t) }
df_linear <- data.frame(t = 1:9) |>
mutate(x = linear_fcn(t)) |>
mutate(diff1 = x - lag(x)) |>
mutate(diff2 = diff1 - lag(diff1)) |>
mutate(diff3 = diff2 - lag(diff2))
df_linear |>
ggplot(aes(x = t, y = x)) +
geom_function(fun = linear_fcn, color = "#D55E00", xlim=c(0.8, 9.2)) +
geom_point() +
scale_x_continuous(breaks=seq(1, 9, by = 1)) +
scale_y_continuous(breaks=seq(0, 30, by = 5)) +
coord_cartesian(ylim = c(0,30)) +
theme_bw() +
theme(panel.grid.minor = element_blank()) +
labs(
title = "Linear Function",
subtitle = "x = 2.5 t + 5"
) +
theme(
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5)
)
```
:::
::: {.column width="10%"}
<!-- empty column to create gap -->
:::
::: {.column width="45%"}
```{r}
#| echo: false
df_linear |>
rename(
"$$t$$" = t,
"$$x_t$$" = x,
"$$\\nabla x_t$$" = diff1,
" " = diff2,
" " = diff3
) |>
replace_na_with_char("") |>
replace_cells_with_char(rows = 1:9, cols = 3:5, new_char = "") |>
display_table("0.5in")
```
:::
:::
<!-- End of two columns -->
<!-- ##### Quadratic Function -->
<!-- Beginning of two columns -->
::: columns
::: {.column width="45%"}
```{r}
#| echo: false
#| fig-asp: 1
#| fig-width: 3
quadratic_fcn <- function(t) { return(2 * (t - 4.75)^2 - 13.125) }
df_quadratic <- data.frame(t = 1:9) |>
mutate(x = quadratic_fcn(t)) |>
mutate(diff1 = x - lag(x)) |>
mutate(diff2 = diff1 - lag(diff1)) |>
mutate(diff3 = diff2 - lag(diff2))
df_quadratic |>
ggplot(aes(x = t, y = x)) +
geom_function(fun = quadratic_fcn, color = "#D55E00", xlim=c(0.8, 9.2)) +
geom_point() +
scale_x_continuous(breaks=seq(1, 9, by = 1)) +
scale_y_continuous(breaks=seq(-15, 30, by = 5)) +
coord_cartesian(ylim = c(-14,26)) +
theme_bw() +
theme(panel.grid.minor = element_blank()) +
labs(
title = "Quadratic Function",
subtitle = "x = 2 (t - 4.75)^2 - 13.125"
) +
theme(
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5)
)
```
:::
::: {.column width="10%"}
<!-- empty column to create gap -->
:::
::: {.column width="45%"}
```{r}
#| echo: false
df_quadratic |>
rename(
"$$t$$" = t,
"$$x_t$$" = x,
"$$\\nabla x_t$$" = diff1,
"$$\\nabla^2 x_t$$" = diff2,
" " = diff3
) |>
replace_na_with_char("") |>
replace_cells_with_char(rows = 1:9, cols = 3:5, new_char = "") |>
display_table("0.5in")
```
:::
:::
<!-- End of two columns -->
<!-- ##### Cubic Function -->
<!-- Beginning of two columns -->
::: columns
::: {.column width="45%"}
```{r}
#| echo: false
#| fig-asp: 1
#| fig-width: 3
cubic_fcn <- function(t) { return((t - 5.5) * (t - 2) * (t - 7) / 10) }
df_cubic <- data.frame(t = 1:9) |>
mutate(x = cubic_fcn(t)) |>
mutate(diff1 = x - lag(x)) |>
mutate(diff2 = diff1 - lag(diff1)) |>
mutate(diff3 = diff2 - lag(diff2))
df_cubic |>
ggplot(aes(x = t, y = x)) +
geom_function(fun = cubic_fcn, color = "#D55E00", xlim=c(0.8, 9.2)) +
geom_point() +
scale_x_continuous(breaks=seq(1, 9, by = 1)) +
scale_y_continuous(breaks=seq(-5, 8, by = 1)) +
theme_bw() +
theme(panel.grid.minor = element_blank()) +
labs(
title = "Cubic Function",
subtitle = "x = (t - 2)(t - 7)(t - 5.5)/10"
) +
theme(
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5)
)
```
:::
::: {.column width="10%"}
<!-- empty column to create gap -->
:::
::: {.column width="45%"}
```{r}
#| echo: false
df_cubic |>
rename(
"$$t$$" = t,
"$$x_t$$" = x,
"$$\\nabla x_t$$" = diff1,
"$$\\nabla^2 x_t$$" = diff2,
"$$\\nabla^3 x_t$$" = diff3
) |>
replace_na_with_char("") |>
replace_cells_with_char(rows = 1:9, cols = 3:5, new_char = "") |>
display_table("0.5in")
```
:::
:::
<!-- End of two columns -->
:::
<!-- End of callout box -->