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
//! Seasonal period candidates, by the rule of Nixtla's coreforecast
//! (`find_season_length`).
//!
//! The candidates are the lags in `[2, max_period]` at which the autocovariance of the first
//! differences peaks, ranked by it. A candidate is kept only if the series has a strong
//! seasonal pattern at that period: STL seasonal strength above
//! [`SEASONAL_STRENGTH_THRESHOLD`], the rule [`nsdiffs`](crate::nsdiffs) uses.
use ;
use Vec;
/// A seasonal period candidate of [`find_period`].
/// Ranks seasonal period candidates of `values` up to `max_period`, strongest first.
///
/// This follows coreforecast's `find_season_length`. It computes `c_k = sum_t dx_t dx_(t+k)`
/// for the first differences `dx` and each lag `k` in `[2, max_period]`; the products are
/// not demeaned or scaled, as coreforecast does it. The candidates are the lags where
/// `c_k` peaks (it is larger than at its neighbours within `[2, max_period]`), in
/// descending order of `c_k`, so the largest `c_k` comes first.
/// Only the 8 largest peaks are checked, and of those only candidates
/// whose STL seasonal strength is above [`SEASONAL_STRENGTH_THRESHOLD`] remain, which needs
/// two full seasons of data.
///
/// coreforecast returns only its single largest lag, and 0 if that lag fails the strength
/// check. When it finds a period, that period is the first candidate here too. Its STL
/// smooths the seasonal part with degree 0 and this crate's with degree 1, so strengths
/// near the threshold can differ.
///
/// The result is a heuristic and a ranking, not a test. R's `findfrequency`, coreforecast
/// and periodogram methods are heuristics, and R calls its own threshold arbitrary. No
/// established method detects several periods jointly. The candidates are integer periods,
/// and with a moderate `max_period` long periods such as 8766 on hourly data stay out of
/// reach.
///
/// Returns no candidate if a value is `NaN` or infinite, or fewer than 4 values are given,
/// and an empty list whenever no checked peak is seasonal enough: callers must handle it.
///
/// # Examples
///
/// ```
/// use backshift::find_period;
///
/// let y: Vec<f64> = (0..120)
/// .map(|t| [3.0, 1.0, -2.0, 0.5, -1.0, -1.5, 0.0][t % 7] + 0.05 * t as f64)
/// .collect();
/// let candidates = find_period(&y, 30);
/// assert_eq!(candidates[0].period, 7);
/// ```
/// How many of the largest autocovariance peaks [`find_period`] checks with a full STL.
///
/// shortcut: noise has hundreds of peaks and each STL costs about `n period`, which made
/// 2000 values take 40 s; raise it if real series need weaker peaks ranked further down.
const MAX_CHECKED_PEAKS: usize = 8;
/// `sum_t x_t x_(t+k)`.
/// Whether `c[k]` is larger than its neighbours among lags `2..c.len()`; the first and the
/// last lag have only one.