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
//! Regressors for a piecewise-linear trend, the trend of Prophet (Taylor and Letham 2018).
//!
//! Prophet's trend `k t + m + sum_j delta_j (t - s_j)^+` changes its slope by `delta_j` at
//! each changepoint `s_j`. With the hinge columns `(t - s_j)^+` as regressors, plus `t`
//! itself, a regression with ARIMA errors ([`Arima::fit_exog`](crate::Arima::fit_exog))
//! estimates the slope changes as coefficients; Fourier terms ([`fourier_row`]) and holiday
//! dummies complete the recipe. Prophet regularises the changes with a sparse prior and
//! places 25 changepoints in the first 80% of the history; a maximum-likelihood fit has no
//! prior, so it wants fewer, chosen by the information criteria.
//!
//! [`fourier_row`]: crate::fourier_row
/// Writes the trend regressors of time `t` into `row`: `t` itself, then the hinge
/// `max(0, t - s)` of each changepoint `s` of `knots`, in order. The function needs no heap,
/// so a device can build the row of each new value as it arrives.
///
/// # Panics
///
/// Panics if `row` does not hold `knots.len() + 1` values.
///
/// # Examples
///
/// ```
/// use backshift::trend_row;
///
/// let mut row = [0.0; 3];
/// trend_row(50, &[20, 80], &mut row);
/// assert_eq!(row, [50.0, 30.0, 0.0]);
/// ```
/// The trend regressors of [`trend_row`] for `rows` consecutive times from `t0`, row-major
/// with `knots.len() + 1` columns. Pass them to
/// [`Arima::fit_exog`](crate::Arima::fit_exog) with `t0 = 0`, and the rows from `t0 = n` on
/// to the forecasts: the trend continues at its last slope.
///
/// # Panics
///
/// Panics if `rows` times the column count overflows `usize`, as an allocation that size
/// would.
///
/// # Examples
///
/// ```
/// use backshift::trend_regressors;
///
/// let x = trend_regressors(&[2], 0, 4);
/// assert_eq!(x, [0.0, 0.0, 1.0, 0.0, 2.0, 0.0, 3.0, 1.0]);
/// ```