numrs2 0.3.3

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
Documentation
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
//! ARIMA and SARIMA Time Series Models
//!
//! This module implements AutoRegressive Integrated Moving Average (ARIMA) models
//! and their seasonal variants (SARIMA) for univariate time series analysis.
//!
//! ## Model Specification
//!
//! ### ARIMA(p,d,q)
//!
//! The ARIMA model combines:
//! - **AR(p)**: AutoRegressive component of order p
//! - **I(d)**: Differencing of order d to achieve stationarity
//! - **MA(q)**: Moving Average component of order q
//!
//! The model equation for differenced series Y_t = ∇^d X_t is:
//!
//! φ(L) Y_t = θ(L) ε_t
//!
//! where:
//! - φ(L) = 1 - φ₁L - φ₂L² - ... - φₚLᵖ (AR polynomial)
//! - θ(L) = 1 + θ₁L + θ₂L² + ... + θ_qL^q (MA polynomial)
//! - ε_t ~ N(0, σ²) (white noise)
//!
//! ### SARIMA(p,d,q)(P,D,Q)s
//!
//! Seasonal ARIMA extends ARIMA with seasonal components:
//! - (P,D,Q): Seasonal orders
//! - s: Seasonal period (e.g., 12 for monthly data with yearly seasonality)
//!
//! ## References
//!
//! Box, G. E., Jenkins, G. M., Reinsel, G. C., & Ljung, G. M. (2015).
//! *Time series analysis: forecasting and control* (5th ed.). John Wiley & Sons.
//!
//! Hyndman, R. J., & Athanasopoulos, G. (2021).
//! *Forecasting: principles and practice* (3rd ed.). OTexts.

use crate::error::{NumRs2Error, Result};
use scirs2_core::ndarray::{s, Array1, Array2, ArrayView1};

/// ARIMA model parameters.
#[derive(Debug, Clone)]
pub struct ArimaParams {
    /// AR coefficients (φ₁, φ₂, ..., φₚ)
    pub ar_coefs: Array1<f64>,
    /// MA coefficients (θ₁, θ₂, ..., θ_q)
    pub ma_coefs: Array1<f64>,
    /// Intercept/constant term
    pub intercept: f64,
    /// Residual variance σ²
    pub sigma2: f64,
    /// Log-likelihood of fitted model
    pub log_likelihood: f64,
    /// Akaike Information Criterion
    pub aic: f64,
    /// Bayesian Information Criterion
    pub bic: f64,
}

/// SARIMA model parameters extending ARIMA with seasonal components.
#[derive(Debug, Clone)]
pub struct SarimaParams {
    /// Non-seasonal ARIMA parameters
    pub arima_params: ArimaParams,
    /// Seasonal AR coefficients (Φ₁, Φ₂, ..., Φₚ)
    pub seasonal_ar_coefs: Array1<f64>,
    /// Seasonal MA coefficients (Θ₁, Θ₂, ..., Θ_Q)
    pub seasonal_ma_coefs: Array1<f64>,
    /// Seasonal period
    pub seasonal_period: usize,
}

/// ARIMA model structure.
#[derive(Debug, Clone)]
pub struct Arima {
    /// AR order
    pub p: usize,
    /// Differencing order
    pub d: usize,
    /// MA order
    pub q: usize,
    /// Include intercept term
    pub include_intercept: bool,
}

impl Arima {
    /// Create a new ARIMA(p,d,q) model specification.
    ///
    /// # Arguments
    ///
    /// * `p` - AR order
    /// * `d` - Differencing order
    /// * `q` - MA order
    ///
    /// # Examples
    ///
    /// ```
    /// use numrs2::new_modules::timeseries::Arima;
    ///
    /// let model = Arima::new(1, 1, 1); // ARIMA(1,1,1)
    /// ```
    pub fn new(p: usize, d: usize, q: usize) -> Self {
        Self {
            p,
            d,
            q,
            include_intercept: true,
        }
    }

    /// Set whether to include an intercept term.
    pub fn with_intercept(mut self, include: bool) -> Self {
        self.include_intercept = include;
        self
    }

    /// Fit the ARIMA model to time series data.
    ///
    /// Uses maximum likelihood estimation via numerical optimization.
    ///
    /// # Arguments
    ///
    /// * `data` - Time series data
    ///
    /// # Returns
    ///
    /// Fitted model parameters
    pub fn fit(&self, data: &ArrayView1<f64>) -> Result<ArimaParams> {
        let n = data.len();

        if n < self.p + self.d + self.q + 1 {
            return Err(NumRs2Error::ValueError(format!(
                "Insufficient data: need at least {} observations",
                self.p + self.d + self.q + 1
            )));
        }

        // Apply differencing
        let diff_data = self.difference(data)?;

        // Estimate parameters using conditional sum of squares (CSS) for initialization
        let init_params = self.estimate_css(&diff_data.view())?;

        // Refine using maximum likelihood estimation (MLE)
        let mle_params = self.estimate_mle(&diff_data.view(), &init_params)?;

        Ok(mle_params)
    }

    /// Apply differencing to the time series.
    fn difference(&self, data: &ArrayView1<f64>) -> Result<Array1<f64>> {
        let mut result = data.to_owned();

        for _ in 0..self.d {
            if result.len() < 2 {
                return Err(NumRs2Error::ValueError(
                    "Series too short for differencing".to_string(),
                ));
            }

            let n = result.len();
            let mut diff = Array1::zeros(n - 1);

            for i in 0..(n - 1) {
                diff[i] = result[i + 1] - result[i];
            }

            result = diff;
        }

        Ok(result)
    }

    /// Estimate initial parameters using Conditional Sum of Squares.
    fn estimate_css(&self, data: &ArrayView1<f64>) -> Result<ArimaParams> {
        let n = data.len();
        let max_lag = self.p.max(self.q);

        if n <= max_lag {
            return Err(NumRs2Error::ValueError(
                "Insufficient data for CSS estimation".to_string(),
            ));
        }

        // Initialize coefficients
        let mut ar_coefs = Array1::zeros(self.p);
        let mut ma_coefs = Array1::zeros(self.q);
        let mut intercept = 0.0;

        // Compute mean for intercept
        if self.include_intercept {
            intercept = data.iter().sum::<f64>() / n as f64;
        }

        // For AR component, use Yule-Walker equations if p > 0
        if self.p > 0 {
            ar_coefs = self.estimate_ar_yule_walker(data)?;
        }

        // For MA component, use innovations algorithm if q > 0
        if self.q > 0 {
            ma_coefs = self.estimate_ma_innovations(data, &ar_coefs)?;
        }

        // Compute residuals and variance
        let residuals = self.compute_residuals(data, &ar_coefs, &ma_coefs, intercept)?;
        let sigma2 = residuals.iter().map(|&r| r * r).sum::<f64>() / residuals.len() as f64;

        // Compute log-likelihood
        let log_likelihood = self.compute_log_likelihood(&residuals, sigma2);

        // Compute information criteria
        let k = self.p + self.q + if self.include_intercept { 1 } else { 0 };
        let aic = -2.0 * log_likelihood + 2.0 * k as f64;
        let bic = -2.0 * log_likelihood + (k as f64) * (n as f64).ln();

        Ok(ArimaParams {
            ar_coefs,
            ma_coefs,
            intercept,
            sigma2,
            log_likelihood,
            aic,
            bic,
        })
    }

    /// Estimate AR coefficients using Yule-Walker equations.
    fn estimate_ar_yule_walker(&self, data: &ArrayView1<f64>) -> Result<Array1<f64>> {
        if self.p == 0 {
            return Ok(Array1::zeros(0));
        }

        use crate::new_modules::timeseries::autocorrelation;

        // Compute autocorrelations
        let acf = autocorrelation(data, self.p)?;

        // Set up Yule-Walker equations: Rφ = r
        // where R is Toeplitz matrix of autocorrelations
        let mut r_matrix = Array2::zeros((self.p, self.p));
        let mut r_vec = Array1::zeros(self.p);

        for i in 0..self.p {
            r_vec[i] = acf[i + 1];
            for j in 0..self.p {
                let lag = i.abs_diff(j);
                r_matrix[[i, j]] = acf[lag];
            }
        }

        // Solve for AR coefficients
        match scirs2_linalg::solve(&r_matrix.view(), &r_vec.view(), None) {
            Ok(phi) => Ok(phi),
            Err(_) => {
                // Fallback to least squares if direct solve fails
                Ok(Array1::zeros(self.p))
            }
        }
    }

    /// Estimate MA coefficients using innovations algorithm.
    fn estimate_ma_innovations(
        &self,
        data: &ArrayView1<f64>,
        ar_coefs: &Array1<f64>,
    ) -> Result<Array1<f64>> {
        if self.q == 0 {
            return Ok(Array1::zeros(0));
        }

        // Simplified estimation: use residuals from AR fit
        let intercept = if self.include_intercept {
            data.iter().sum::<f64>() / data.len() as f64
        } else {
            0.0
        };

        let residuals = if self.p > 0 {
            self.compute_ar_residuals(data, ar_coefs, intercept)?
        } else {
            data.iter().map(|&x| x - intercept).collect()
        };

        // Use autocorrelation of residuals for MA estimation
        use crate::new_modules::timeseries::autocorrelation;

        let res_array = Array1::from_vec(residuals);
        let res_acf = autocorrelation(&res_array.view(), self.q)?;

        // Simplified MA coefficient estimation
        let mut ma_coefs = Array1::zeros(self.q);
        for i in 0..self.q {
            ma_coefs[i] = -res_acf[i + 1]; // Negative of ACF
        }

        Ok(ma_coefs)
    }

    /// Compute residuals from AR model.
    fn compute_ar_residuals(
        &self,
        data: &ArrayView1<f64>,
        ar_coefs: &Array1<f64>,
        intercept: f64,
    ) -> Result<Vec<f64>> {
        let n = data.len();
        let mut residuals = Vec::with_capacity(n - self.p);

        for t in self.p..n {
            let mut prediction = intercept;
            for i in 0..self.p {
                prediction += ar_coefs[i] * (data[t - i - 1] - intercept);
            }
            residuals.push(data[t] - prediction);
        }

        Ok(residuals)
    }

    /// Compute residuals from full ARIMA model.
    fn compute_residuals(
        &self,
        data: &ArrayView1<f64>,
        ar_coefs: &Array1<f64>,
        ma_coefs: &Array1<f64>,
        intercept: f64,
    ) -> Result<Array1<f64>> {
        let n = data.len();
        let max_lag = self.p.max(self.q);
        let mut residuals = Array1::zeros(n);

        // Initialize residuals for first max_lag observations
        for t in 0..max_lag {
            residuals[t] = data[t] - intercept;
        }

        // Compute residuals for remaining observations
        for t in max_lag..n {
            let mut prediction = intercept;

            // AR component
            for i in 0..self.p {
                if t > i {
                    prediction += ar_coefs[i] * (data[t - i - 1] - intercept);
                }
            }

            // MA component
            for i in 0..self.q {
                if t > i {
                    prediction += ma_coefs[i] * residuals[t - i - 1];
                }
            }

            residuals[t] = data[t] - prediction;
        }

        Ok(residuals.slice(s![max_lag..]).to_owned())
    }

    /// Refine parameters using Maximum Likelihood Estimation.
    fn estimate_mle(
        &self,
        data: &ArrayView1<f64>,
        init_params: &ArimaParams,
    ) -> Result<ArimaParams> {
        // For now, return CSS estimates
        // Full MLE would require numerical optimization (e.g., Nelder-Mead, BFGS)
        // which we can implement later
        Ok(init_params.clone())
    }

    /// Compute log-likelihood for given residuals and variance.
    fn compute_log_likelihood(&self, residuals: &Array1<f64>, sigma2: f64) -> f64 {
        let n = residuals.len() as f64;
        let ss = residuals.iter().map(|&r| r * r).sum::<f64>();

        -0.5 * n * (2.0 * std::f64::consts::PI * sigma2).ln() - 0.5 * ss / sigma2
    }

    /// Forecast future values.
    ///
    /// # Arguments
    ///
    /// * `data` - Historical time series data
    /// * `params` - Fitted model parameters
    /// * `steps` - Number of steps ahead to forecast
    ///
    /// # Returns
    ///
    /// Array of forecasted values
    pub fn forecast(
        &self,
        data: &ArrayView1<f64>,
        params: &ArimaParams,
        steps: usize,
    ) -> Result<Array1<f64>> {
        // Apply differencing
        let diff_data = self.difference(data)?;
        let n = diff_data.len();

        // Extend series with forecasts
        let mut extended = diff_data.to_owned();
        let mut forecasts = Array1::zeros(steps);

        for h in 0..steps {
            let mut prediction = params.intercept;

            // AR component
            for i in 0..self.p {
                let idx = n + h - i - 1;
                if idx < extended.len() {
                    prediction += params.ar_coefs[i] * (extended[idx] - params.intercept);
                }
            }

            // MA component (assumes future errors are 0)
            // In practice, this is the expected value

            forecasts[h] = prediction;
            extended = concatenate_arrays(&extended, &Array1::from_vec(vec![prediction]));
        }

        // Integrate forecasts back if differencing was applied
        let integrated = self.integrate(&forecasts, data)?;

        Ok(integrated)
    }

    /// Integrate differenced forecasts back to original scale.
    fn integrate(
        &self,
        forecasts: &Array1<f64>,
        original_data: &ArrayView1<f64>,
    ) -> Result<Array1<f64>> {
        if self.d == 0 {
            return Ok(forecasts.clone());
        }

        let mut result = forecasts.clone();
        let n_orig = original_data.len();

        for _ in 0..self.d {
            let mut integrated = Array1::zeros(result.len());
            let last_value = if n_orig > 0 {
                original_data[n_orig - 1]
            } else {
                0.0
            };

            integrated[0] = last_value + result[0];
            for i in 1..result.len() {
                integrated[i] = integrated[i - 1] + result[i];
            }

            result = integrated;
        }

        Ok(result)
    }
}

/// Concatenate two 1D arrays.
fn concatenate_arrays(a: &Array1<f64>, b: &Array1<f64>) -> Array1<f64> {
    let mut result = Array1::zeros(a.len() + b.len());
    result.slice_mut(s![..a.len()]).assign(a);
    result.slice_mut(s![a.len()..]).assign(b);
    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_relative_eq;
    use scirs2_core::ndarray::Array1;

    #[test]
    fn test_arima_creation() {
        let model = Arima::new(1, 1, 1);
        assert_eq!(model.p, 1);
        assert_eq!(model.d, 1);
        assert_eq!(model.q, 1);
        assert!(model.include_intercept);
    }

    #[test]
    fn test_differencing() {
        let data = Array1::from_vec(vec![1.0, 2.0, 4.0, 7.0, 11.0]);
        let model = Arima::new(0, 1, 0);

        let diff = model
            .difference(&data.view())
            .expect("differencing should succeed");
        assert_eq!(diff.len(), 4);
        assert_relative_eq!(diff[0], 1.0, epsilon = 1e-10);
        assert_relative_eq!(diff[1], 2.0, epsilon = 1e-10);
        assert_relative_eq!(diff[2], 3.0, epsilon = 1e-10);
        assert_relative_eq!(diff[3], 4.0, epsilon = 1e-10);
    }

    #[test]
    fn test_double_differencing() {
        let data = Array1::from_vec(vec![1.0, 2.0, 4.0, 7.0, 11.0, 16.0]);
        let model = Arima::new(0, 2, 0);

        let diff = model
            .difference(&data.view())
            .expect("double differencing should succeed");
        assert_eq!(diff.len(), 4);
        // First diff: [1, 2, 3, 4, 5]
        // Second diff: [1, 1, 1, 1]
        assert_relative_eq!(diff[0], 1.0, epsilon = 1e-10);
        assert_relative_eq!(diff[1], 1.0, epsilon = 1e-10);
    }

    #[test]
    fn test_arima_fit_simple() {
        // Simple AR(1) process
        let data = Array1::from_vec(vec![1.0, 1.5, 2.0, 2.3, 2.5, 2.7, 2.8, 2.9, 3.0, 3.05]);
        let model = Arima::new(1, 0, 0);

        let params = model.fit(&data.view()).expect("ARIMA fit should succeed");
        assert!(params.ar_coefs.len() == 1);
        assert!(params.ma_coefs.is_empty());
        assert!(params.sigma2 > 0.0);
    }

    #[test]
    fn test_arima_forecast() {
        // Use data with trend + small noise (more realistic than perfect linear trend)
        // Avoid perfect linear trend which becomes constant after differencing
        let data = Array1::from_vec(vec![1.0, 2.1, 2.9, 4.2, 4.8, 6.1, 6.9, 8.0, 9.1, 9.8]);
        let model = Arima::new(1, 1, 0);

        let params = model.fit(&data.view()).expect("fit should succeed");
        let forecast = model
            .forecast(&data.view(), &params, 3)
            .expect("forecast should succeed");

        assert_eq!(forecast.len(), 3);
        // For trend with noise, forecasts should continue the general upward trend
        // Allow wide range for numerical estimation variability
        assert!(
            forecast[0] > 8.0 && forecast[0] < 13.0,
            "First forecast {} should be reasonable",
            forecast[0]
        );
    }

    #[test]
    fn test_information_criteria() {
        let data = Array1::from_vec(vec![1.0, 1.5, 2.2, 2.8, 3.1, 3.5, 3.8, 4.0, 4.3, 4.5]);
        let model = Arima::new(1, 0, 1);

        let params = model.fit(&data.view()).expect("fit should succeed");

        // AIC and BIC should be finite
        assert!(params.aic.is_finite());
        assert!(params.bic.is_finite());
        // BIC penalizes complexity more than AIC
        assert!(params.bic > params.aic);
    }

    #[test]
    fn test_insufficient_data_error() {
        let data = Array1::from_vec(vec![1.0, 2.0]);
        let model = Arima::new(2, 1, 2);

        let result = model.fit(&data.view());
        assert!(result.is_err());
    }
}