scirs2-series 0.1.2

Time series analysis module for SciRS2 (scirs2-series)
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
//! STL (Seasonal-Trend decomposition using LOESS) and MSTL implementation

use scirs2_core::ndarray::Array1;
use scirs2_core::numeric::{Float, FromPrimitive};
use std::fmt::Debug;

use super::common::DecompositionResult;
use crate::error::{Result, TimeSeriesError};

/// Options for STL decomposition
#[derive(Debug, Clone)]
pub struct STLOptions {
    /// Trend window size (must be odd)
    pub trend_window: usize,
    /// Seasonal window size (must be odd)
    pub seasonal_window: usize,
    /// Number of inner loop iterations
    pub n_inner: usize,
    /// Number of outer loop iterations
    pub n_outer: usize,
    /// Whether to use robust weighting
    pub robust: bool,
}

impl Default for STLOptions {
    fn default() -> Self {
        Self {
            trend_window: 21,
            seasonal_window: 13,
            n_inner: 2,
            n_outer: 1,
            robust: false,
        }
    }
}

/// Options for Multiple Seasonal-Trend decomposition using LOESS (MSTL)
#[derive(Debug, Clone)]
pub struct MSTLOptions {
    /// Seasonal periods (e.g., [7, 30, 365] for weekly, monthly, and yearly seasonality)
    pub seasonal_periods: Vec<usize>,
    /// Trend window size (must be odd)
    pub trend_window: usize,
    /// Seasonal window sizes for each seasonal period (must be odd)
    pub seasonal_windows: Option<Vec<usize>>,
    /// Number of inner loop iterations
    pub n_inner: usize,
    /// Number of outer loop iterations
    pub n_outer: usize,
    /// Whether to use robust weighting
    pub robust: bool,
}

impl Default for MSTLOptions {
    fn default() -> Self {
        Self {
            seasonal_periods: Vec::new(),
            trend_window: 21,
            seasonal_windows: None,
            n_inner: 2,
            n_outer: 1,
            robust: false,
        }
    }
}

/// Result of multiple seasonal time series decomposition
#[derive(Debug, Clone)]
pub struct MultiSeasonalDecompositionResult<F> {
    /// Trend component
    pub trend: Array1<F>,
    /// Multiple seasonal components
    pub seasonal_components: Vec<Array1<F>>,
    /// Residual component
    pub residual: Array1<F>,
    /// Original time series
    pub original: Array1<F>,
}

/// Performs STL (Seasonal and Trend decomposition using LOESS) on a time series
///
/// STL decomposition uses locally weighted regression (LOESS) to extract trend
/// and seasonal components.
///
/// # Arguments
///
/// * `ts` - The time series to decompose
/// * `period` - The seasonal period
/// * `options` - Options for STL decomposition
///
/// # Returns
///
/// * Decomposition result containing trend, seasonal, and residual components
///
/// # Example
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_series::decomposition::{stl_decomposition, STLOptions};
///
/// let ts = array![1.0, 2.0, 3.0, 2.0, 1.0, 2.0, 3.0, 2.0, 1.0, 2.0, 3.0, 2.0];
/// let options = STLOptions::default();
/// let result = stl_decomposition(&ts, 4, &options).expect("Operation failed");
/// println!("Trend: {:?}", result.trend);
/// println!("Seasonal: {:?}", result.seasonal);
/// println!("Residual: {:?}", result.residual);
/// ```
#[allow(dead_code)]
pub fn stl_decomposition<F>(
    ts: &Array1<F>,
    period: usize,
    options: &STLOptions,
) -> Result<DecompositionResult<F>>
where
    F: Float + FromPrimitive + Debug,
{
    if ts.len() < 2 * period {
        return Err(TimeSeriesError::DecompositionError(format!(
            "Time series length ({}) must be at least twice the seasonal period ({})",
            ts.len(),
            period
        )));
    }

    // Validate options
    if options.trend_window.is_multiple_of(2) {
        return Err(TimeSeriesError::InvalidParameter {
            name: "trend_window".to_string(),
            message: "Trend window size must be odd".to_string(),
        });
    }
    if options.seasonal_window.is_multiple_of(2) {
        return Err(TimeSeriesError::InvalidParameter {
            name: "seasonal_window".to_string(),
            message: "Seasonal window size must be odd".to_string(),
        });
    }

    // Working arrays
    let n = ts.len();
    let mut seasonal = Array1::zeros(n);
    let mut trend = Array1::zeros(n);
    let mut weights = Array1::from_elem(n, F::one());
    let original = ts.clone();

    // STL Outer Loop
    for _ in 0..options.n_outer {
        // STL Inner Loop
        for _ in 0..options.n_inner {
            // 1. Detrend
            let detrended = if trend.iter().all(|&x| x == F::zero()) {
                // First iteration, trend is zero
                original.clone()
            } else {
                // Subsequent iterations, remove trend
                original.clone() - &trend
            };

            // 2. Cycle-subseries Smoothing for Seasonal Component
            let mut cycle_subseries = vec![Vec::new(); period];
            let mut smoothed_seasonal = Array1::zeros(n);

            // Group by seasonal position
            for i in 0..n {
                cycle_subseries[i % period].push((i, detrended[i]));
            }

            // Process each subseries
            for subseries in cycle_subseries.iter() {
                if subseries.is_empty() {
                    continue;
                }

                // Extract subseries values
                let mut indices = Vec::with_capacity(subseries.len());
                let mut values = Vec::with_capacity(subseries.len());
                let mut subseries_weights = Vec::with_capacity(subseries.len());

                for &(idx, val) in subseries {
                    indices.push(idx);
                    values.push(val);
                    subseries_weights.push(weights[idx]);
                }

                // Convert to ndarray
                let indices_array = Array1::from_vec(indices);
                let values_array = Array1::from_vec(values);
                let weights_array = Array1::from_vec(subseries_weights);

                // Calculate locally weighted smoothed value for each index
                // (In production code, replace with actual LOESS implementation)
                // Simplified for this example:
                let mut smoothed_values = Array1::zeros(indices_array.len());
                // ... smoothing algorithm would go here ...

                // For now, just use a simple moving average as a placeholder
                for i in 0..indices_array.len() {
                    let mut count = 0;
                    let mut sum = F::zero();
                    let window = options.seasonal_window / 2;

                    for j in 0..indices_array.len() {
                        if i >= window
                            && i < indices_array.len() - window
                            && j >= i - window
                            && j <= i + window
                        {
                            sum = sum + values_array[j] * weights_array[j];
                            count += 1;
                        }
                    }

                    if count > 0 {
                        smoothed_values[i] = sum / F::from_usize(count).expect("Operation failed");
                    } else {
                        smoothed_values[i] = values_array[i];
                    }
                }

                // Assign smoothed values back to the seasonal component
                for (idx, val) in indices_array.iter().zip(smoothed_values.iter()) {
                    smoothed_seasonal[*idx] = *val;
                }
            }

            // 3. Low-Pass Filter of Seasonal
            // (Actual implementation would use a proper low-pass filter)
            // This is a simplified placeholder
            let filtered_seasonal = smoothed_seasonal.clone();
            // ... filtering would go here ...

            // 4. Deseasonalize
            let deseasonalized = original.clone() - &filtered_seasonal;

            // 5. Trend Smoothing
            // (In production code, replace with actual LOESS implementation)
            // Simplified for this example:
            let mut new_trend = Array1::zeros(n);
            // ... smoothing algorithm would go here ...

            // For now, use simple moving average as placeholder
            for i in 0..n {
                let mut count = 0;
                let mut sum = F::zero();
                let window = options.trend_window / 2;

                for j in 0..n {
                    if i >= window && i < n - window && j >= i - window && j <= i + window {
                        sum = sum + deseasonalized[j] * weights[j];
                        count += 1;
                    }
                }

                if count > 0 {
                    new_trend[i] = sum / F::from_usize(count).expect("Operation failed");
                } else {
                    new_trend[i] = deseasonalized[i];
                }
            }

            // 6. Update components
            trend = new_trend;
            seasonal = filtered_seasonal;
        }

        // Update robustness weights (if using robust method)
        if options.robust {
            // Calculate residuals
            let residual = original.clone() - &trend - &seasonal;

            // Calculate robust weights
            // (In production code, implement proper bisquare weights)
            // Simplified for this example:
            let abs_residuals = residual.mapv(|x| x.abs());
            let max_residual = abs_residuals.fold(F::zero(), |a, &b| if a > b { a } else { b });

            if max_residual > F::zero() {
                for i in 0..n {
                    let r = abs_residuals[i] / max_residual;
                    if r < F::from_f64(0.5).expect("Operation failed") {
                        weights[i] = F::one();
                    } else if r < F::one() {
                        let tmp = F::one() - r * r;
                        weights[i] = tmp * tmp;
                    } else {
                        weights[i] = F::zero();
                    }
                }
            }
        }
    }

    // Calculate final residual
    let residual = original.clone() - &trend - &seasonal;

    // Return result
    Ok(DecompositionResult {
        trend,
        seasonal,
        residual,
        original,
    })
}

/// Performs Multiple STL decomposition on a time series with multiple seasonal components
///
/// MSTL (Multiple Seasonal-Trend decomposition using LOESS) extends STL to handle
/// multiple seasonal periods.
///
/// # Arguments
///
/// * `ts` - The time series to decompose
/// * `options` - Options for MSTL decomposition
///
/// # Returns
///
/// * MultiSeasonalDecompositionResult containing trend, multiple seasonal components, and residual
///
/// # Example
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_series::decomposition::{mstl_decomposition, MSTLOptions};
///
/// let ts = array![1.0, 2.0, 3.0, 2.0, 1.0, 2.0, 3.0, 2.0, 1.0, 2.0, 3.0, 2.0,
///                 1.5, 2.5, 3.5, 2.5, 1.5, 2.5, 3.5, 2.5, 1.5, 2.5, 3.5, 2.5];
///
/// let mut options = MSTLOptions::default();
/// options.seasonal_periods = vec![4, 12]; // Weekly and monthly patterns
///
/// let result = mstl_decomposition(&ts, &options).expect("Operation failed");
/// println!("Trend: {:?}", result.trend);
/// println!("Seasonal Components: {}", result.seasonal_components.len());
/// println!("Residual: {:?}", result.residual);
/// ```
#[allow(dead_code)]
pub fn mstl_decomposition<F>(
    ts: &Array1<F>,
    options: &MSTLOptions,
) -> Result<MultiSeasonalDecompositionResult<F>>
where
    F: Float + FromPrimitive + Debug,
{
    // Validate options
    if options.seasonal_periods.is_empty() {
        return Err(TimeSeriesError::InvalidParameter {
            name: "seasonal_periods".to_string(),
            message: "At least one seasonal period must be specified".to_string(),
        });
    }

    let n_seasons = options.seasonal_periods.len();
    if let Some(ref windows) = options.seasonal_windows {
        if windows.len() != n_seasons {
            return Err(TimeSeriesError::InvalidParameter {
                name: "seasonal_windows".to_string(),
                message: format!(
                    "Number of seasonal windows ({}) must match number of seasonal periods ({})",
                    windows.len(),
                    n_seasons
                ),
            });
        }
    }

    // For each seasonal period, check that the time series is long enough
    for &period in &options.seasonal_periods {
        if ts.len() < 2 * period {
            return Err(TimeSeriesError::DecompositionError(format!(
                "Time series length ({}) must be at least twice the seasonal period ({})",
                ts.len(),
                period
            )));
        }
    }

    let n = ts.len();
    let original = ts.clone();
    let mut seasonal_components = Vec::with_capacity(n_seasons);
    let _weights = Array1::from_elem(n, F::one());
    let mut deseasonal = original.clone();

    // For each seasonal component
    for (i, &period) in options.seasonal_periods.iter().enumerate() {
        let seasonal_window = if let Some(ref windows) = options.seasonal_windows {
            windows[i]
        } else {
            // Default to 7 or (period / 2), whichever is larger
            std::cmp::max(7, period / 2) | 1 // Ensure odd
        };

        let stl_options = STLOptions {
            trend_window: options.trend_window,
            seasonal_window,
            n_inner: options.n_inner,
            n_outer: options.n_outer,
            robust: options.robust,
        };

        // Apply STL with current data
        let result = stl_decomposition(&deseasonal, period, &stl_options)?;

        // Save this seasonal component
        seasonal_components.push(result.seasonal);

        // Remove this seasonal component
        deseasonal = deseasonal - &seasonal_components[i];
    }

    // The remaining deseasonal series is the trend
    let trend = deseasonal.clone();

    // Calculate final residual
    let mut residual = original.clone();
    residual = residual - &trend;
    for seasonal in &seasonal_components {
        residual = residual - seasonal;
    }

    Ok(MultiSeasonalDecompositionResult {
        trend,
        seasonal_components,
        residual,
        original,
    })
}