-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathPeriodic-trig.qmd
More file actions
371 lines (319 loc) · 12.9 KB
/
Periodic-trig.qmd
File metadata and controls
371 lines (319 loc) · 12.9 KB
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
---
pagetitle: "Feature Engineering A-Z | Trigonometric"
aliases:
- circular-trig.html
image: social-cards/periodic-trig.png
---
# Trigonometric {#sec-periodic-trig}
::: {style="visibility: hidden; height: 0px;"}
## Trigonometric
:::
One of the most common ways of dealing with periodic or cyclical data is by applying the trigonometric functions `sin()` and `cos()` to these variables. This is not a trained method.
You will note that many occurrences of periodic data are datetime variables in nature.
Please read the [Datetime Circular](datetime-circular) chapter,
for the additional considerations to take into account when working with datetime variables specifically.
The terminology we will be using in this chapter when talking about sine curves is **period** and **offset**.
A sine curve normally has a period of $2\pi$.
A period of $2\pi$ is unlikely to be useful,
we can set this value to be an arbitrarily different value by dividing by both that value and by $2\pi$.
$$
\sin\left(\dfrac{x}{\text{period} \cdot 2 \pi}\right)
$$
This sine curve has a period of `period`.
The second thing we might wanna do is to slide this curve one way or another.
The offset handles that by sliding the sine curve that many units back or forwards.
$$
\sin\left(\dfrac{x - \text{offset}}{\text{period} \cdot 2 \pi}\right)
$$
I think that this is easier to see with some toy visualizations.
The following toy data set contains a highly significant periodic effect.
```{r}
#| label: toy_data
#| echo: false
#| message: false
library(tidyverse)
set.seed(1234)
toy_data <- tibble(
predictor = c(1, 1000, sample(1:1000, 300))
) |>
mutate(target = pmax(sin(predictor / 365 * pi * 2), 0) ^ 4 +
rnorm(n(), sd = 0.1) - 0.5) |>
arrange(target)
```
```{r}
#| label: fig-trig-predictor-outcome
#| echo: false
#| message: false
#| fig-cap: |
#| Strong periodic signal every 365 values along predictor.
#| fig-alt: |
#| Scatter chart. Predictor (0-1000) on x-axis, target on y-axis. Data shows
#| periodic pattern with peaks every 365 units. Target stays near -0.5 between
#| peaks, rising sharply to about 0.5 at regular intervals.
toy_data |>
ggplot(aes(predictor, target)) +
geom_point() +
theme_minimal()
```
If we were to take the `sin()` and `cos()` of the predictor,
using appropriate period and offset we can overlay them on the previous chart
```{r}
#| label: fig-trig-predictor-outcome-sin-cos
#| echo: false
#| message: false
#| fig-cap: |
#| Sin() curve perfectly captures peaks.
#| fig-alt: |
#| Scatter chart with sine and cosine curves overlaid. Black points show
#| periodic target data. The sine curve (period 365) peaks align perfectly
#| with the data peaks. The cosine curve is phase-shifted by 1/4 cycle,
#| reaching zero when sine peaks.
library(recipes)
recipe(target ~ predictor, toy_data) |>
step_harmonic(predictor, cycle_size = 365, frequency = 1, keep_original_cols = TRUE) |>
prep() |>
bake(new_data = NULL) |>
rename(`sin(predictor)` = predictor_sin_1, `cos(predictor)` = predictor_cos_1) |>
pivot_longer(-c(predictor, target)) |>
ggplot(aes(predictor, value, color = name)) +
geom_point(aes(predictor, target), color = "black") +
geom_line() +
labs(color = NULL) +
theme_minimal() +
theme(legend.position = "bottom")
```
We notice that the `sin()` transformation somehow captures the periodic nature of the data.
It obviously can't fully capture the effect since the relationship isn't a perfect sine curve.
The effect might be more noticeable if we change the way we visualize this effect.
```{r}
#| label: fig-trig-predictor-outcome-circle
#| echo: false
#| message: false
#| fig-cap: |
#| Strong singal for sin(predictor)
#| fig-alt: |
#| Scatter chart in sin-cos space forming a circle. Points colored by target
#| value. High target values cluster on the right side of the circle where
#| sin(predictor) is high. Low values fill the rest. Jitter added to show
#| overlapping points. Demonstrates that sin alone captures the signal.
library(recipes)
recipe(target ~ predictor, toy_data) |>
step_harmonic(predictor, cycle_size = 365, frequency = 1) |>
prep() |>
bake(new_data = NULL) |>
ggplot(aes(predictor_sin_1, predictor_cos_1, color = target)) +
geom_jitter(width = 0.05, height = 0.05, alpha = 0.5) +
scale_color_viridis_c() +
labs(
x = "sin(predictor)",
y = "cos(predictor)",
caption = "Jittering artificially added to show overlapping points"
) +
theme_minimal()
```
In this simple case, the `predictor_sin_1` becomes an ideal predictor.
On the other hand, `predictor_cos_1` doesn't do as well since it doesn't allow for a clean separation between high and low `target` values.
All the high values appear when `predictor_cos_1` is close to 0.
However, not all observations where `predictor_cos_1` is close to 0 have high target values.
Above we got lucky that we picked the period and offset.
if you are offeven by a bit you can get uninformative transformations like the one below.
```{r}
#| label: fig-trig-predictor-outcome-circle-wrong
#| echo: false
#| message: false
#| fig-cap: |
#| No signal detected
#| fig-alt: |
#| Scatter chart in sin-cos space with wrong period (160 instead of 365).
#| Points form a circle but target colors are scattered randomly around it
#| with no spatial pattern.
library(recipes)
recipe(target ~ predictor, toy_data) |>
step_harmonic(predictor, cycle_size = 160, frequency = 1) |>
prep() |>
bake(new_data = NULL) |>
ggplot(aes(predictor_sin_1, predictor_cos_1, color = target)) +
geom_jitter(width = 0.05, height = 0.05, alpha = 0.5) +
scale_color_viridis_c() +
labs(
x = "sin(predictor)",
y = "cos(predictor)",
caption = "Jittering artificially added to show overlapping points"
) +
theme_minimal()
```
The offset is also important.
We got lucky that it gave 1 good predictor and one bad predictor.
Even if the period is good,
but the offset is off you end up with two decent predictors.
These two predictors are still good enough, but they are less ideal than the first example we got.
```{r}
#| label: fig-trig-predictor-outcome-circle-offset
#| echo: false
#| message: false
#| fig-cap: |
#| No signal detected
#| fig-alt: |
#| Scatter chart in sin-cos space with correct period but offset starting_val.
#| Points form a circle with high target values (yellow) concentrated in
#| the upper-right quadrant rather than purely on the right.
library(recipes)
recipe(target ~ predictor, toy_data) |>
step_harmonic(predictor, cycle_size = 365, frequency = 1, starting_val = 45) |>
prep() |>
bake(new_data = NULL) |>
ggplot(aes(predictor_sin_1, predictor_cos_1, color = target)) +
geom_jitter(width = 0.05, height = 0.05, alpha = 0.5) +
scale_color_viridis_c() +
labs(
x = "sin(predictor)",
y = "cos(predictor)",
caption = "Jittering artificially added to show overlapping points"
) +
theme_minimal()
```
One of the issues with this approach is that each variable contains double information.
E.i. you need both variables to detect "summer".
One of the downsides to taking both the `sin()` and `cos()` of a predictor,
is that it is unlikely that both of them capture the effect well.
You might get one good and one bad, or both decent.
The good thing about calling both `sin()` and `cos()` is that you don't have to worry about the offset too much.
As long as you get the period right you get a signal in at least one of the curves.
If you spend some manual time finding the right period **and** offset,
then you only need to calculate `sin()`.
For a sufficiently periodic effect and proper placement,
a single `sin()` is going to be more effective than both `sin()` and `cos()`.
This is especially true since `cos()` is a shift of `sin()` by definition.
Propose now we have multiple periodic signals.
They could be shifted versions of each other,
think Halloween, Christmas, and Valentine's day.
Or they could have different periods.
This would be hard to handle using a single set of curves,
and we thus have to create one for each periodic pattern.
This is also where the decision to only do `sin()` instead of both helps,
as we are trying to limit redundant or ineffective predictors from being created.
```{r}
#| label: toy_data_two
#| echo: false
toy_data_two <- tibble(
predictor = c(1, 1000, sample(1:1000, 300))
) |>
mutate(target = pmax(sin(predictor / 365 * pi * 2), 0) ^ 4 +
rnorm(n(), sd = 0.1) - 0.5) |>
mutate(target = target + pmax(sin((predictor-125)/ 200 * pi * 2), 0) ^ 4 * 0.6) |>
arrange(target)
```
```{r}
#| label: fig-trig-predictor-outcome-two
#| echo: false
#| message: false
#| fig-cap: |
#| Strong periodic signal every 365 and 200 values along predictor.
#| fig-alt: |
#| Scatter chart showing data with two overlapping periodic signals. Predictor
#| on x-axis, target on y-axis. Peaks occur at two different frequencies:
#| every 365 units (larger peaks) and every 200 units (smaller peaks, offset
#| by 125). Some peaks overlap, creating higher combined values.
toy_data_two |>
ggplot(aes(predictor, target)) +
geom_point() +
theme_minimal()
```
```{r}
#| label: fig-trig-predictor-outcome-sin-cos-two
#| echo: false
#| message: false
#| fig-cap: |
#| the two sin() curve perfectly captures peaks.
#| fig-alt: |
#| Faceted chart with two panels, one per sine wave frequency. Black points
#| show the dual-periodic target data in both panels. Top panel: sin(365)
#| curve captures the larger, slower periodic peaks. Bottom panel: sin(200)
#| curve (offset by 125) captures the smaller, faster peaks.
recipe(target ~ predictor, toy_data_two) |>
step_harmonic(predictor, cycle_size = 365, frequency = 1, keep_original_cols = TRUE) |>
step_rename(predictor_sin_one = predictor_sin_1) |>
step_rm(predictor_cos_1) |>
step_harmonic(predictor, cycle_size = 200, frequency = 1, starting_val = 125, keep_original_cols = TRUE) |>
step_rename(predictor_sin_two = predictor_sin_1) |>
step_rm(predictor_cos_1) |>
prep() |>
bake(new_data = NULL) |>
rename(`sin(365)` = predictor_sin_one, `sin(200)` = predictor_sin_two) |>
pivot_longer(-c(predictor, target)) |>
ggplot(aes(predictor, value, color = name)) +
geom_point(aes(predictor, target), color = "black") +
geom_line() +
theme_minimal() +
theme(legend.position = "bottom") +
facet_grid(name ~ .)
```
These two newly created predictors now hit the peaks of each of the periods.
A glaring issue right now is that all signals are not perfect sine curves.
This method explained in this chapter will thus work less well for sharp spikes and asymmetric signals.
The more advanced methods such as [periodic splines](periodic-splines) and [periodic indicators](periodic-indicators) handle these types of data better.
## Pros and Cons
### Pros
- Easy and fast to calculate
- Calculating both `sin()` and `cos()` is fail-safe if you have the right period
### Cons
- Doesn't fit all periodic data shapes
- Sensitive to should of period
## R Examples
We will be using the animalshelter data set for this.
```{r}
#| label: show-data
library(recipes)
library(animalshelter)
longbeach |>
select(outcome_type, intake_date)
```
The `step_harmonic()` will calculate the
```{r}
#| label: step_harmonic
trig_rec <- recipe(outcome_type ~ intake_date, data = longbeach) |>
step_harmonic(intake_date,
cycle_size = 365,
frequency = 1,
starting_val = 0,
keep_original_cols = TRUE)
trig_rec |>
prep() |>
bake(new_data = NULL)
```
## Python Examples
```{python}
#| label: python-setup
#| echo: false
import pandas as pd
from sklearn import set_config
set_config(transform_output="pandas")
pd.set_option('display.precision', 3)
```
We are using the `shelter_cats` data set for examples.
Since there isn't a built-in transformer for this transformation, we can create our own using `FunctionTransformer()` and `numpy.sin()` and `numpy.cos()`.
```{python}
#| label: functiontransformer-sin-cos
from feazdata import shelter_cats
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import FunctionTransformer
import numpy as np
import pandas as pd
# transforming `intake_date` to integer.
# Should properly be done in sin_transformer() and cos_transformer()
shelter_cats["intake_date"] = pd.to_datetime(shelter_cats["intake_date"])
shelter_cats["intake_date"] = shelter_cats["intake_date"].astype(int)
def sin_transformer(period, offset):
return FunctionTransformer(lambda x: np.sin((x - offset) / period * 2 * np.pi))
def cos_transformer(period, offset):
return FunctionTransformer(lambda x: np.cos((x - offset) / period * 2 * np.pi))
ct = ColumnTransformer(
transformers=[
("intake_sin", sin_transformer(365, 0), ["intake_date"]),
("intake_cos", cos_transformer(365, 0), ["intake_date"]),
],
remainder="passthrough"
)
ct.fit(shelter_cats)
ct.transform(shelter_cats).filter(regex=("^intake_(sin|cos).*"))
```