-
Notifications
You must be signed in to change notification settings - Fork 3
/
chapter_5_lesson_2.qmd
650 lines (479 loc) · 25 KB
/
chapter_5_lesson_2.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
---
title: "Harmonic Seasonal Variables - Part 1"
subtitle: "Chapter 5: 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>
```
```{r}
#| echo: false
#| label: functions_for_sine_and_cosine_plots
# Okabe-Ito color palette
okabe_ito_colors <- c("#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7", "#E69F00", "#56B4E9")
plot_sine <- function(cycle_length = 12, i_values = c(1:6), amplitude = rep(1, length(i_values)), spacing = 4, title = "Sine Functions with Different Frequencies", min_t = 0, max_t = cycle_length) {
create_sine_df <- function(i, cycle_length, amplitude) {
df <- tibble(
t = seq(from = min_t, to = max_t, length.out = 501),
value = amplitude[which(i_values == i)] * sin(2 * pi * i * t / cycle_length),
i = right(paste0(" ", as.character(i)), 2)
)
}
sine_df <- tibble(t = as.integer(), value = as.numeric(), i = as.character())
for (i in i_values) {
sine_df <- sine_df |> bind_rows(create_sine_df(i, cycle_length, amplitude))
}
ggplot(sine_df, aes(x = t, y = value - spacing * as.numeric(i), color = i)) +
geom_line(linewidth = 1) +
scale_y_continuous(
breaks = -spacing * (i_values),
minor_breaks = NULL,
labels = NULL
) +
scale_x_continuous(
breaks = c(-12:12),
minor_breaks = NULL
) +
scale_color_manual(values = okabe_ito_colors[1:length(i_values)], name = "i") +
labs(x = "t", y = "Sine Value", title = title) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5)
)
}
plot_cosine <- function(cycle_length = 12, i_values = c(1:6), amplitude = rep(1, length(i_values)), spacing = 4, title = "Cosine Functions with Different Frequencies", min_t = 0, max_t = cycle_length) {
create_cosine_df <- function(i, cycle_length, amplitude) {
df <- tibble(
t = seq(from = min_t, to = max_t, length.out = 501),
value = amplitude[which(i_values == i)] * cos(2 * pi * i * t / cycle_length),
i = right(paste0(" ", as.character(i)), 2)
)
}
cosine_df <- tibble(t = as.integer(), value = as.numeric(), i = as.character())
for (i in i_values) {
cosine_df <- cosine_df |> bind_rows(create_cosine_df(i, cycle_length, amplitude))
}
ggplot(cosine_df, aes(x = t, y = value - spacing * as.numeric(i), color = i)) +
geom_line(linewidth = 1) +
scale_y_continuous(
breaks = -spacing * (i_values),
minor_breaks = NULL,
labels = NULL
) +
scale_x_continuous(
breaks = c(-12:12),
minor_breaks = NULL
) +
scale_color_manual(values = okabe_ito_colors[1:length(i_values)], name = "i") +
labs(x = "t", y = "Cosine Value", title = title) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5)
)
}
```
## Learning Outcomes
{{< include outcomes/_chapter_5_lesson_2_outcomes.qmd >}}
## Preparation
- Read Section 5.6
## Learning Journal Exchange (10 min)
- Review another student's journal
- What would you add to your learning journal after reading another student's?
- What would you recommend the other student add to their learning journal?
- Sign the Learning Journal review sheet for your peer
## Class Activity: Joys of Trigonometry (15 min)
In the previous lesson, we learned how to incorporate an indicator (or dummy) variable for each season in a period. If there are twelve months in a year, this requires having twelve parameters in the model. Given that many seasonal changes are gradual and can be modeled by a continuous function, we can use sines and cosines to approximate the seasonal variation. This can lead to a smaller number of parameters than is required for the indicator variable approach.
Consider the sine wave with the following parameters:
- $A$:$~~$ the amplitude,
- $f$:$~~~$ the frequency or the number of cycles per sampling interval, and
- $\phi$:$~~~$ the phase shift.
$$
A \sin ( 2 \pi f t + \phi )
$$
Here is an interactive plot of this function. Adjust the values of $A$, $f$, and $\phi$ to see their effect on the function.
<iframe src="https://www.desmos.com/calculator/sgbxo2c0hx" width="750" height="500" style="border: 1px solid #ccc" frameborder=0></iframe>
Notice that this sine function is not linear in the parameters $A$ and $\phi$.
$$
A \sin ( 2 \pi f t + \phi )
$$
One of the trigonometric sum and difference identities is:
$$
\sin(\theta + \phi)
= \cos(\phi) \sin(\theta) + \sin(\phi) \cos(\theta)
$$
We apply this to our sine function.
\begin{align*}
A \sin ( 2 \pi f t + \phi )
&= \underbrace{A \cos( \phi )}_{\alpha_s} \cdot \sin ( 2 \pi f t ) + \underbrace{A \sin( \phi )}_{\alpha_c} \cdot \cos ( 2 \pi f t ) \\
&= \alpha_s \cdot \sin ( 2 \pi f t ) + \alpha_c \cdot \cos ( 2 \pi f t )
\end{align*}
We have transformed this from something that is not linear in the parameters $A$ and $\phi$ to an expression that is linear in the parameters $\alpha_s$ and $\alpha_c$.
We can denote the frequency of a sine function as $f = \frac{i}{s}$, where $s$ is the number of seasons in a cycle and $i$ is some integer. This leads the following representation:
\begin{align*}
A \sin \left( \frac{2 \pi i t}{s} + \phi \right)
&= \underbrace{A \cos( \phi )}_{\alpha_s} \cdot \sin \left( \frac{2 \pi i t}{s} \right) + \underbrace{A \sin( \phi )}_{\alpha_c} \cdot \cos \left( \frac{2 \pi i t}{s} \right) \\
&= \alpha_s \cdot \sin \left( \frac{2 \pi i t}{s} \right) + \alpha_c \cdot \cos \left( \frac{2 \pi i t}{s} \right)
\end{align*}
@fig-sineFrequencies and @fig-cosineFrequencies illustrate these sine and cosine functions with various values of $i$, where $s = 12$.
<!-- Beginning of two columns -->
::: columns
::: {.column width="45%"}
```{r}
#| code-fold: true
#| code-summary: "Show the code"
#| label: fig-sineFrequencies
#| fig-cap: "Sine Functions with Various Frequencies"
plot_sine()
```
:::
::: {.column width="10%"}
<!-- empty column to create gap -->
:::
::: {.column width="45%"}
```{r}
#| code-fold: true
#| code-summary: "Show the code"
#| label: fig-cosineFrequencies
#| fig-cap: "Cosine Functions with Various Frequencies"
plot_cosine()
```
:::
:::
<!-- End of two columns -->
One key objective of this lesson is to use a linear combination of functions like those above to model the seasonal component of a time series.
## Class Activity: Fourier Series (15 min)
### What is a Fourier Series?
We now explore an important mathematical concept that allows us to approximate any periodic function. If we have an infinite number of terms, the Fourier Series described below gives an exact representation of the function.
::: {.callout-note title="Fourier Series"}
The **Fourier Series** is an infinite series representation of a smooth function $f(t)$ with period $s$:
$$
f(t) = \frac{A_{s_0}}{2} + \sum_{i=1}^{\infty} \left\{ \alpha_{s_i} \sin \left( \frac{2\pi i t}{s} \right) + \alpha_{c_i} \cos \left( \frac{2\pi i t}{s} \right) \right\}
$$
The coefficients $\alpha_{s_i}$ and $\alpha_{c_i}$ are defined by the integrals:
$$
\alpha_{s_i} = \frac{2}{s} \int_0^s f(t) \cos \left( \frac{2\pi i t}{s} \right) \; dt
~~~~~~~~~~~~~~~~~~
\alpha_{c_i} = \frac{2}{s} \int_0^s f(t) \sin \left( \frac{2\pi i t}{s} \right) \; dt
$$
(You will not need to compute any of these integrals.)
:::
<!-- Sample Fourier Series -->
As an example, we will approximate the periodic function illustrated here with a Fourier series.
```{r}
#| echo: false
#| label: use_this_chunk_to_generate_fourier_data
step_size <- 0.001
fourier_df <- tibble(x = seq(-1.75, 1.75, step_size)) |>
mutate(
terms_1 = sin(pi * x),
terms_2 = terms_1 + sin(3 * pi * x) / 3,
terms_3 = terms_2 + sin(5 * pi * x) / 5,
terms_4 = terms_3 + sin(7 * pi * x) / 7,
terms_5 = terms_4 + sin(9 * pi * x) / 9,
terms_6 = terms_5 + sin(11 * pi * x) / 11,
terms_7 = terms_6 + sin(13 * pi * x) / 13,
terms_8 = terms_7 + sin(15 * pi * x) / 15,
terms_9 = terms_8 + sin(17 * pi * x) / 17,
terms_10 = terms_9 + sin(19 * pi * x) / 19,
terms_15 = terms_10 + sin(21 * pi * x) / 21 + sin(23 * pi * x) / 23
+ sin(25 * pi * x) / 25 + sin(27 * pi * x) / 27
+ sin(29 * pi * x) / 29,
terms_25 = terms_15 + sin(31 * pi * x) / 31 + sin(33 * pi * x) / 33
+ sin(35 * pi * x) / 35 + sin(37 * pi * x) / 37
+ sin(39 * pi * x) / 39 + sin(41 * pi * x) / 41
+ sin(43 * pi * x) / 43 + sin(45 * pi * x) / 45
+ sin(47 * pi * x) / 47 + sin(49 * pi * x) / 49,
terms_50 = terms_25 + sin(51 * pi * x) / 51 + sin(53 * pi * x) / 53
+ sin(55 * pi * x) / 55 + sin(57 * pi * x) / 57 + sin(59 * pi * x) / 59
+ sin(61 * pi * x) / 61 + sin(63 * pi * x) / 63 + sin(65 * pi * x) / 65
+ sin(67 * pi * x) / 67 + sin(69 * pi * x) / 69 + sin(71 * pi * x) / 71
+ sin(73 * pi * x) / 73 + sin(75 * pi * x) / 75 + sin(77 * pi * x) / 77
+ sin(79 * pi * x) / 79 + sin(81 * pi * x) / 81 + sin(83 * pi * x) / 83
+ sin(85 * pi * x) / 85 + sin(87 * pi * x) / 87 + sin(89 * pi * x) / 89
+ sin(91 * pi * x) / 91 + sin(93 * pi * x) / 93 + sin(95 * pi * x) / 95
+ sin(97 * pi * x) / 97 + sin(99 * pi * x) / 99,
terms_100 = terms_50 + sin(101 * pi * x) / 101 + sin(103 * pi * x) / 103 + sin(105 * pi * x) / 105 + sin(107 * pi * x) / 107 + sin(109 * pi * x) / 109 + sin(111 * pi * x) / 111 + sin(113 * pi * x) / 113 + sin(115 * pi * x) / 115 + sin(117 * pi * x) / 117 + sin(119 * pi * x) / 119 + sin(121 * pi * x) / 121 + sin(123 * pi * x) / 123 + sin(125 * pi * x) / 125 + sin(127 * pi * x) / 127 + sin(129 * pi * x) / 129 + sin(131 * pi * x) / 131 + sin(133 * pi * x) / 133 + sin(135 * pi * x) / 135 + sin(137 * pi * x) / 137 + sin(139 * pi * x) / 139 + sin(141 * pi * x) / 141 + sin(143 * pi * x) / 143 + sin(145 * pi * x) / 145 + sin(147 * pi * x) / 147 + sin(149 * pi * x) / 149 + sin(151 * pi * x) / 151 + sin(153 * pi * x) / 153 + sin(155 * pi * x) / 155 + sin(157 * pi * x) / 157 + sin(159 * pi * x) / 159 + sin(161 * pi * x) / 161 + sin(163 * pi * x) / 163 + sin(165 * pi * x) / 165 + sin(167 * pi * x) / 167 + sin(169 * pi * x) / 169 + sin(171 * pi * x) / 171 + sin(173 * pi * x) / 173 + sin(175 * pi * x) / 175 + sin(177 * pi * x) / 177 + sin(179 * pi * x) / 179 + sin(181 * pi * x) / 181 + sin(183 * pi * x) / 183 + sin(185 * pi * x) / 185 + sin(187 * pi * x) / 187 + sin(189 * pi * x) / 189 + sin(191 * pi * x) / 191 + sin(193 * pi * x) / 193 + sin(195 * pi * x) / 195 + sin(197 * pi * x) / 197 + sin(199 * pi * x) / 199
) |>
pivot_longer(names_to = "terms", values_to = "Value", cols = starts_with("terms")) |>
mutate(terms = str_remove(terms, "terms_"))
```
```{r}
#| echo: false
#| label: fourier_plot_function
plot_fourier <- function(df = fourier_df, num_terms) {
plot_df <- df |>
filter(terms == num_terms)
my_plot <- plot_df |>
ggplot(aes(x = x, y = Value)) +
geom_segment(aes(x = -1.75, y = 0, xend = 1.75, yend = 0), linewidth = 1, colour = "black") +
geom_segment(aes(x = -1.75, y = 0.785, xend = -1, yend = 0.785, colour = "Target Function"), linewidth = 1) +
geom_segment(aes(x = -1, y = 0.785, xend = -1, yend = -0.785, colour = "Target Function"), linewidth = 1) +
geom_segment(aes(x = -1, y = -0.785, xend = 0, yend = -0.785, colour = "Target Function"), linewidth = 1) +
geom_segment(aes(x = 0, y = -0.785, xend = 0, yend = 0.785, colour = "Target Function"), linewidth = 1) +
geom_segment(aes(x = 0, y = 0.785, xend = 1, yend = 0.785, colour = "Target Function"), linewidth = 1) +
geom_segment(aes(x = 1, y = 0.785, xend = 1, yend = -0.785, colour = "Target Function"), linewidth = 1) +
geom_segment(aes(x = 1, y = -0.785, xend = 1.75, yend = -0.785, colour = "Target Function"), linewidth = 1) +
geom_line(aes(colour = "Approximation"), linewidth = 1, alpha = 0.75) +
scale_color_manual(values = c("Target Function" = "#E69F00", "Approximation" = "#56B4E9")) +
theme_minimal() +
coord_cartesian(ylim = c(-1,1)) +
labs(title = paste0("Fourier Series Approximation with ", plot_df$terms |> head(1), " Terms")) +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text.y = element_blank(),
legend.position = c(0.9, 0.8),
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5)
)
return(my_plot)
}
```
::: panel-tabset
#### Target
```{r}
#| echo: false
#| label: plot_target_function
fourier_df |>
filter(terms == 1) |>
filter(x == floor(x)) |> # To speed up the processing of this chunk
ggplot(aes(x = x, y = Value)) +
geom_segment(aes(x = -1.75, y = 0, xend = 1.75, yend = 0), linewidth = 1, colour = "black") +
geom_segment(aes(x = -1.75, y = 0.785, xend = -1, yend = 0.785, colour = "Target Function"), linewidth = 1) +
geom_segment(aes(x = -1, y = 0.785, xend = -1, yend = -0.785, colour = "Target Function"), linewidth = 1) +
geom_segment(aes(x = -1, y = -0.785, xend = 0, yend = -0.785, colour = "Target Function"), linewidth = 1) +
geom_segment(aes(x = 0, y = -0.785, xend = 0, yend = 0.785, colour = "Target Function"), linewidth = 1) +
geom_segment(aes(x = 0, y = 0.785, xend = 1, yend = 0.785, colour = "Target Function"), linewidth = 1) +
geom_segment(aes(x = 1, y = 0.785, xend = 1, yend = -0.785, colour = "Target Function"), linewidth = 1) +
geom_segment(aes(x = 1, y = -0.785, xend = 1.75, yend = -0.785, colour = "Target Function"), linewidth = 1) +
scale_color_manual(values = c("Target Function" = "#E69F00")) +
theme_minimal() +
coord_cartesian(ylim = c(-1,1)) +
labs(
title = "Periodic Target Function",
x = "x",
y = "Values"
) +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text.y = element_blank(),
legend.position = "none",
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5)
)
```
#### 1 term
```{r}
#| echo: false
#| label: plot_fourier_approx_1
plot_fourier(num_terms = 1)
```
$$
f(x) \approx sin(\pi x)
$$
```{r}
#| echo: false
plot_sine(cycle_length = 2, i_values = c(1), amplitude = c(1), spacing = 0, title = "Functions Summed for this Approximation", min_t = -1.75, max_t = 1.75) +
theme(legend.position = c(0.95, 0.85))
```
#### 2 terms
```{r}
#| echo: false
#| label: plot_fourier_approx_2
plot_fourier(num_terms = 2)
```
$$
f(x) \approx \sin(\pi x) + \frac{1}{3} \sin(3 \pi x)
$$
```{r}
#| echo: false
plot_sine(cycle_length = 2, i_values = c(1,3), amplitude = c(1, 1/3), spacing = 0, title = "Functions Summed for this Approximation", min_t = -1.75, max_t = 1.75) +
theme(legend.position = c(0.95, 0.85))
```
#### 3 terms
```{r}
#| echo: false
#| label: plot_fourier_approx_3
plot_fourier(num_terms = 3)
```
$$
f(x) \approx \sin(\pi x) + \frac{1}{3} \sin(3 \pi x) + \frac{1}{5} \sin(5 \pi x)
$$
```{r}
#| echo: false
plot_sine(cycle_length = 2, i_values = c(1,3,5), amplitude = c(1, 1/3, 1/5), spacing = 0, title = "Functions Summed for this Approximation", min_t = -1.75, max_t = 1.75) +
theme(legend.position = c(0.95, 0.85))
```
#### 4 terms
```{r}
#| echo: false
#| label: plot_fourier_approx_4
plot_fourier(num_terms = 4)
```
$$
f(x) \approx \sin(\pi x) + \frac{1}{3} \sin(3 \pi x) + \frac{1}{5} \sin(5 \pi x) + \frac{1}{7} \sin(7 \pi x)
$$
```{r}
#| echo: false
plot_sine(cycle_length = 2, i_values = c(1,3,5,7), amplitude = c(1, 1/3, 1/5, 1/7), spacing = 0, title = "Functions Summed for this Approximation", min_t = -1.75, max_t = 1.75) +
theme(legend.position = c(0.95, 0.85))
```
#### 5 terms
```{r}
#| echo: false
#| label: plot_fourier_approx_5
plot_fourier(num_terms = 5)
```
$$
f(x) \approx sin(\pi x) + \frac{1}{3} \sin(3 \pi x) + \frac{1}{5} \sin(5 \pi x) + \frac{1}{7} \sin(7 \pi x) + \frac{1}{9} \sin(9 \pi x)
$$
```{r}
#| echo: false
plot_sine(cycle_length = 2, i_values = c(1,3,5,7,9), amplitude = c(1, 1/3, 1/5, 1/7, 1/9), spacing = 0, title = "Functions Summed for this Approximation", min_t = -1.75, max_t = 1.75) +
theme(legend.position = c(0.95, 0.85))
```
#### 10 terms
```{r}
#| echo: false
#| label: plot_fourier_approx_10
plot_fourier(num_terms = 10)
```
$$
f(x) \approx sin(\pi x) + \frac{1}{3} \sin(3 \pi x) + \frac{1}{5} \sin(5 \pi x) + \cdots + \frac{1}{17} \sin(17 \pi x) + \frac{1}{19} \sin(19 \pi x)
$$
#### 15 terms
```{r}
#| echo: false
#| label: plot_fourier_approx_15
plot_fourier(num_terms = 15)
```
$$
f(x) \approx sin(\pi x) + \frac{1}{3} \sin(3 \pi x) + \frac{1}{5} \sin(5 \pi x) + \cdots + \frac{1}{27} \sin(27 \pi x) + \frac{1}{29} \sin(29 \pi x)
$$
#### 25 terms
```{r}
#| echo: false
#| label: plot_fourier_approx_25
plot_fourier(num_terms = 25)
```
$$
f(x) \approx sin(\pi x) + \frac{1}{3} \sin(3 \pi x) + \frac{1}{5} \sin(5 \pi x) + \cdots + \frac{1}{47} \sin(47 \pi x) + \frac{1}{49} \sin(49 \pi x)
$$
#### 50 terms
```{r}
#| echo: false
#| label: plot_fourier_approx_50
plot_fourier(num_terms = 50)
```
$$
f(x) \approx sin(\pi x) + \frac{1}{3} \sin(3 \pi x) + \frac{1}{5} \sin(5 \pi x) + \cdots + \frac{1}{97} \sin(97 \pi x) + \frac{1}{99} \sin(99 \pi x)
$$
#### 100 terms
```{r}
#| echo: false
#| label: plot_fourier_approx_100
plot_fourier(num_terms = 100)
```
$$
f(x) \approx sin(\pi x) + \frac{1}{3} \sin(3 \pi x) + \frac{1}{5} \sin(5 \pi x) + \cdots + \frac{1}{197} \sin(197 \pi x) + \frac{1}{199} \sin(199 \pi x)
$$
:::
<!-- End of Fourier series example -->
We can continue to get more and more precise estimates of the function by just adding more terms to the sum.
So, the function $f(t)$ can be approximated to any desired level of precision by truncating the series after a sufficient number of terms. For the purpose of this discussion, we will define "one term" as an expression of the form
$$
\left\{ \alpha_{s_i} \sin \left( \frac{2\pi i t}{s} \right) + \alpha_{c_i} \cos \left( \frac{2\pi i t}{s} \right) \right\}
$$
### Fitting a Seasonal Component
The Fourier series exists for any smooth (continuously differentiable) function. Note theoretically, this allows us to obtain the value of the function at any real value by using this series.
However, for a discrete time series with $s$ seasons, we only need to evaluate the function at a finite number of points:
$t = 1, 2, 3, \ldots, s-1, s$.
For example, in the previous lesson, we used twelve indicator variables to pass through twelve points in a monthly seasonal component to a time series with annual cycles.
It turns out, that we only need six terms (which involves twelve coefficients) to fit monthly data with annual cycles.
In general, we only need to obtain $\left\lfloor \frac{s}{2} \right\rfloor$ terms of this sum to fit the seasonal values perfectly.
| Pattern | Number of Seasons, $s$ | Maximum terms in the sum, $\left\lfloor \frac{s}{2} \right\rfloor$ |
|--------------------|------------------------|------------------------|
| Days in a Week | 7 | 3 |
| Quarters in a Year | 4 | 2 |
| Months in a Year | 12 | 6 |
: A few examples of seasonal patterns and the corresponding values of $s$ and $\left\lfloor \frac{s}{2} \right\rfloor$ {#tbl-sAndFloorFunction}
Note that if $s$ is even and $i=\frac{s}{2}$,
$$
\sin \left( \frac{2\pi i t}{s} \right) = \sin \left( \frac{2\pi \cdot \frac{s}{2} \cdot t}{s} \right) = \sin \left( \pi t \right) = 0
$$
for all integer values of $t$. So, this term must be omitted from the model. If we try to include it in the model, the coefficient will be rediculously large, as R trys to make the product of something very close to 0 (the value from the sine function) and the coefficient multiply to some reasonably small number.
The method for fitting seaonal components using indicator variables does not assume any relationship between successive seasons. However, values observed in January are often highly correlated with values observed in February, etc. Fitting a seasonal component using terms in the Fourier Series can often yield a good approximation for the periodic cycles with only a few terms.
For a time series with $s$ seasons per cycle, our additive model can be written as:
\begin{align*}
x_t
&= m_t + s_t + z_t \\
&= m_t +
\sum_{i=1}^{\left\lfloor \frac{s}{2} \right\rfloor} \left\{ \alpha_{s_i} \sin \left( \frac{2\pi i t}{s} \right) + \alpha_{c_i} \cos \left( \frac{2\pi i t}{s} \right) \right\}
+ z_t
\end{align*}
The term $m_t$ can take a variety of forms, including:
- Linear: $~~~~~~~~~~~$ $m_t = \alpha_0 + \alpha_1 t$
- Quadratic: $~~~~~~$ $m_t = \alpha_0 + \alpha_1 t + \alpha_2 t^2$
- Exponential: $~~~$ $m_t = \alpha_0 e^{\alpha_1 t}$
- Any other functional form
The term $z_t$ is a (possibly autocorrelated) time series with mean zero.
We will now focus on the seasonal term, $s_t$.
The full seasonal term when considering 12 months in a year is:
\begin{align*}
s_t
&= \sum_{i=1}^{\left\lfloor \frac{s}{2} \right\rfloor} \left\{ \alpha_{s_i} \sin \left( \frac{2\pi i t}{s} \right) + \alpha_{c_i} \cos \left( \frac{2\pi i t}{s} \right) \right\} \\
&= \sum_{i=1}^{6} \left\{ \alpha_{s_i} \sin \left( \frac{2\pi i t}{12} \right) + \alpha_{c_i} \cos \left( \frac{2\pi i t}{12} \right) \right\} \\
&=~~~~ \left\{ \alpha_{s_1} \sin \left( \frac{2\pi \cdot 1 t}{12} \right) + \alpha_{c_1} \cos \left( \frac{2\pi \cdot 1 t}{12} \right) \right\} & \leftarrow i = 1 \\
&~~~~~+ \left\{ \alpha_{s_2} \sin \left( \frac{2\pi \cdot 2 t}{12} \right) + \alpha_{c_2} \cos \left( \frac{2\pi \cdot 2 t}{12} \right) \right\} & \leftarrow i = 2 \\
&~~~~~+ \left\{ \alpha_{s_3} \sin \left( \frac{2\pi \cdot 3 t}{12} \right) + \alpha_{c_3} \cos \left( \frac{2\pi \cdot 3 t}{12} \right) \right\} & \leftarrow i = 3 \\
&~~~~~+ \left\{ \alpha_{s_4} \sin \left( \frac{2\pi \cdot 4 t}{12} \right) + \alpha_{c_4} \cos \left( \frac{2\pi \cdot 4 t}{12} \right) \right\} & \leftarrow i = 4 \\
&~~~~~+ \left\{ \alpha_{s_5} \sin \left( \frac{2\pi \cdot 5 t}{12} \right) + \alpha_{c_5} \cos \left( \frac{2\pi \cdot 5 t}{12} \right) \right\} & \leftarrow i = 5 \\
&~~~~~+ \left\{ \alpha_{s_6} \sin \left( \frac{2\pi \cdot 6 t}{12} \right) + \alpha_{c_6} \cos \left( \frac{2\pi \cdot 6 t}{12} \right) \right\} & \leftarrow i = 6 \\
&=~~~~ \left\{ \alpha_{s_1} \sin \left( \frac{2\pi \cdot 1 t}{12} \right) + \alpha_{c_1} \cos \left( \frac{2\pi \cdot 1 t}{12} \right) \right\} \\
&~~~~~+ \left\{ \alpha_{s_2} \sin \left( \frac{2\pi \cdot 2 t}{12} \right) + \alpha_{c_2} \cos \left( \frac{2\pi \cdot 2 t}{12} \right) \right\} \\
&~~~~~+ \left\{ \alpha_{s_3} \sin \left( \frac{2\pi \cdot 3 t}{12} \right) + \alpha_{c_3} \cos \left( \frac{2\pi \cdot 3 t}{12} \right) \right\} \\
&~~~~~+ \left\{ \alpha_{s_4} \sin \left( \frac{2\pi \cdot 4 t}{12} \right) + \alpha_{c_4} \cos \left( \frac{2\pi \cdot 4 t}{12} \right) \right\} \\
&~~~~~+ \left\{ \alpha_{s_5} \sin \left( \frac{2\pi \cdot 5 t}{12} \right) + \alpha_{c_5} \cos \left( \frac{2\pi \cdot 5 t}{12} \right) \right\} \\
&~~~~~+ \left\{ \phantom{\alpha_{s_6} \sin \left( \frac{2\pi \cdot 6 t}{12} \right) +}~~ \alpha_{c_6} \cos \left( \frac{2\pi \cdot 6 t}{12} \right) \right\} \\
\end{align*}
Note that $\sin \left( \frac{2\pi \cdot 6 t}{12} \right) = 0$ for all integer values of $t$, so we can omit the term $\alpha_{s_6} \sin \left( \frac{2\pi \cdot 6 t}{12} \right)$.
As noted above, we can often use a relatively small subset of these terms to get a good approximation of the seasonal component.
### Simulation
The following simulation illustrates harmonic seasonal terms.
The values S1, S2, S3, $\ldots$ represent the coefficients on the sine functions: $\alpha_{s_1}, ~ \alpha_{s_2}, ~ \alpha_{s_3}, ~ \ldots, ~ \alpha_{s_6}$. Similarly, the values C1, C2, C3, $\ldots$ represent the coefficients on the corresponding cosine terms: $\alpha_{c_1}, ~ \alpha_{c_2}, ~ \alpha_{c_3}, ~ \ldots, ~ \alpha_{c_6}$.
Adjust the values of the parameters to create different seasonal patterns. Note that this is just a sum of sine and cosine function with various frequencies and amplitudes.
```{=html}
<iframe id="harmonicSeasons" src="https://posit.byui.edu/content/3fcf5813-76fe-44ee-ab7f-b4a99884c855" style="border: none; width: 100%; height: 950px" frameborder="0"></iframe>
```
## Homework Preview (5 min)
- Review upcoming homework assignment
- Clarify questions
::: {.callout-note icon=false}
## Download Homework
<a href="https://byuistats.github.io/timeseries/homework/homework_5_2.qmd" download="homework_5_2.qmd"> homework_5_2.qmd </a>
:::