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
#[cfg(test)]
mod test_estimate {
use more_asserts::assert_lt;
const AR3: [f64; 20] = [
149.8228533548,
86.8388399871,
42.3116899484,
76.6796578536,
60.3665347774,
66.7733563129,
-5.1144504108,
14.0294086329,
76.2517878809,
121.2898170491,
74.65663878,
69.9331198692,
46.7476543397,
26.2225173663,
-32.0638217183,
2.8335240789,
31.5182582874,
76.4827451823,
36.6122657518,
-33.430444607,
];
const AR3_RES: [f64; 20] = [
0.0,
0.0,
0.0,
46.2603808,
-7.7972931,
28.510325,
-57.7569706,
14.2417414,
31.2183008,
48.5090956,
-2.716499,
38.8984537,
-5.402662,
-8.4669355,
-62.7063041,
4.5063279,
-14.4924325,
31.271378,
-29.2554603,
-54.8047308,
];
#[test]
fn residuals_ar3_f64() {
let x = AR3;
let (y, _mean) = arima::util::center(&x);
let intercept = -5.954353;
let phi = [0.67715294, -0.44171525, 0.08249936];
let residuals_real = AR3_RES;
let residuals = arima::estimate::residuals(&y, intercept, Some(&phi), None).unwrap();
assert_eq!(residuals.len(), residuals_real.len());
for i in 0..residuals_real.len() {
// the residuals were collected from R's arima() routine. allow for some variance.
assert_lt!((residuals_real[i] - residuals[i] as f64).abs(), 1.0e-3);
}
}
#[test]
fn residuals_arima_102_f64() {
let x = AR3;
let (y, _mean) = arima::util::center(&x);
let intercept = -23.64706;
let phi = [0.48359302];
let theta = [1.05643909, 1.51029256];
let residuals_real = [
0.0,
12.5024401,
-14.7741471,
51.0605505,
-10.2274033,
-30.6143332,
8.4998564,
51.8766267,
-0.0576161,
4.2438554,
9.7222585,
15.2661396,
-19.7658293,
-0.442378,
-16.3084314,
34.3532355,
16.6032739,
-10.0661619,
-16.6988839,
-20.1747913,
];
let residuals =
arima::estimate::residuals(&y, intercept, Some(&phi), Some(&theta)).unwrap();
assert_eq!(residuals.len(), residuals_real.len());
for i in 0..residuals_real.len() {
// the residuals were collected from R's arima() routine. allow for some variance.
assert_lt!((residuals_real[i] - residuals[i] as f64).abs(), 1.0e-3);
}
}
#[test]
fn fit_arima_2002_f64() {
let x = AR3;
let coef = arima::estimate::fit(&x, 2, 0, 0).unwrap();
// Results obtained from R with
// `cf <- arima(x, order=c(2, 0, 0), method="CSS", optim.method="L-BFGS-B")$coef`
// R's intercept must be transformed with `cf["intercept"] * (1 - sum(cf[1:2]))`
assert_lt!((coef[0] - 29.3546).abs(), 1.0e-4); // Intercept
assert_lt!((coef[1] - 0.6465575).abs(), 1.0e-4); // AR 1
assert_lt!((coef[2] - -0.3452993).abs(), 1.0e-4); // AR 2
}
#[test]
fn fit_arima_101_f64() {
let x = AR3;
let coef = arima::estimate::fit(&x, 1, 0, 1).unwrap();
// Results obtained from R with
// `cf <- arima(x, order=c(1, 0, 1), method="CSS", optim.method="L-BFGS-B")$coef`
// R's intercept must be transformed with `cf["intercept"] * (1 - sum(cf[1]))`
assert_lt!((coef[0] - 24.18111).abs(), 1.0e-3); // Intercept
assert_lt!((coef[1] - 0.3596548).abs(), 1.0e-4); // AR 1
assert_lt!((coef[2] - 0.2880067).abs(), 1.0e-4); // MA 1
}
#[test]
fn fit_arima_102_f64() {
let x = AR3;
let coef = arima::estimate::fit(&x, 1, 0, 2).unwrap();
println!("{:?}", coef);
// Results obtained from R with
// `cf <- arima(x, order=c(1, 0, 2), method="CSS", optim.method="L-BFGS-B")$coef`
// R's intercept must be transformed with `cf["intercept"] * (1 - sum(cf[1]))`
// With MA > 1, we often find multiple similarly good models that mostly differ in
// the intercept. Thus, we ignore it in this test.
//assert_lt!((coef[0] - 1.884872).abs(), 1.0e-7); // Intercept
assert_lt!((coef[1] - 0.4835826).abs(), 1.0e-2); // AR 1
assert_lt!((coef[2] - 1.0564438).abs(), 1.0e-2); // MA 1
assert_lt!((coef[3] - 1.5102864).abs(), 1.0e-2); // MA 2
}
}