scirs2-series 0.1.5

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
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
//! Time series specific feature selection methods

use scirs2_core::ndarray::{Array1, Array2};
use std::collections::HashMap;

use super::wrapper::WrapperMethods;
use super::FeatureSelectionResult;
use crate::error::{Result, TimeSeriesError};
use crate::utils::cross_correlation;

/// Time series specific feature selection methods
pub struct TimeSeriesMethods;

impl TimeSeriesMethods {
    /// Lag-based feature selection
    ///
    /// Selects features based on their predictive power at different lags.
    ///
    /// # Arguments
    ///
    /// * `features` - Feature matrix (samples x features)
    /// * `target` - Target variable
    /// * `max_lag` - Maximum lag to consider
    /// * `n_features` - Number of top features to select
    ///
    /// # Returns
    ///
    /// * Feature selection result
    ///
    /// # Example
    ///
    /// ```
    /// use scirs2_core::ndarray::{Array1, Array2};
    /// use scirs2_series::feature_selection::TimeSeriesMethods;
    ///
    /// let features = Array2::from_shape_vec((100, 5), (0..500).map(|x| x as f64).collect()).expect("Operation failed");
    /// let target = Array1::from_vec((0..100).map(|x| x as f64).collect());
    ///
    /// let result = TimeSeriesMethods::lag_based_selection(&features, &target, 5, Some(3)).expect("Operation failed");
    /// println!("Selected {} features", result.selected_features.len());
    /// ```
    pub fn lag_based_selection(
        features: &Array2<f64>,
        target: &Array1<f64>,
        max_lag: usize,
        n_features: Option<usize>,
    ) -> Result<FeatureSelectionResult> {
        let (n_samples, n_feat) = features.dim();

        if n_samples != target.len() {
            return Err(TimeSeriesError::DimensionMismatch {
                expected: n_samples,
                actual: target.len(),
            });
        }

        if n_samples <= max_lag + 1 {
            return Err(TimeSeriesError::InsufficientData {
                message: "Insufficient samples for lag-based selection".to_string(),
                required: max_lag + 2,
                actual: n_samples,
            });
        }

        let mut feature_scores = Array1::zeros(n_feat);

        for i in 0..n_feat {
            let feature_col = features.column(i).to_owned();
            let mut max_correlation = 0.0f64;

            // Test correlations at different lags
            for lag in 1..=max_lag {
                if n_samples > lag {
                    let lagged_feature =
                        feature_col.slice(scirs2_core::ndarray::s![..n_samples - lag]);
                    let future_target = target.slice(scirs2_core::ndarray::s![lag..]);

                    let correlation =
                        Self::calculate_correlation_arrays(&lagged_feature, &future_target)?;
                    max_correlation = max_correlation.max(correlation.abs());
                }
            }

            feature_scores[i] = max_correlation;
        }

        // Select top _features
        let n_to_select = n_features.unwrap_or(n_feat / 2).min(n_feat);
        let mut indexed_scores: Vec<(usize, f64)> = feature_scores
            .iter()
            .enumerate()
            .map(|(i, &score)| (i, score))
            .collect();

        indexed_scores.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        let selected_features: Vec<usize> = indexed_scores
            .into_iter()
            .take(n_to_select)
            .map(|(idx_, _)| idx_)
            .collect();

        let mut metadata = HashMap::new();
        metadata.insert("max_lag".to_string(), max_lag as f64);
        metadata.insert("n_selected".to_string(), selected_features.len() as f64);

        Ok(FeatureSelectionResult {
            selected_features,
            feature_scores,
            method: "LagBased".to_string(),
            metadata,
        })
    }

    /// Seasonal feature importance
    ///
    /// Selects features based on their seasonal patterns.
    ///
    /// # Arguments
    ///
    /// * `features` - Feature matrix (samples x features)
    /// * `seasonal_period` - Seasonal period
    /// * `n_features` - Number of top features to select
    ///
    /// # Returns
    ///
    /// * Feature selection result
    pub fn seasonal_importance_selection(
        features: &Array2<f64>,
        seasonal_period: usize,
        n_features: Option<usize>,
    ) -> Result<FeatureSelectionResult> {
        let (n_samples, n_feat) = features.dim();

        if n_samples < seasonal_period * 2 {
            return Err(TimeSeriesError::InsufficientData {
                message: "Insufficient samples for seasonal analysis".to_string(),
                required: seasonal_period * 2,
                actual: n_samples,
            });
        }

        let mut feature_scores = Array1::zeros(n_feat);

        for i in 0..n_feat {
            let feature_col = features.column(i).to_owned();
            let seasonal_strength =
                Self::calculate_seasonal_strength(&feature_col, seasonal_period)?;
            feature_scores[i] = seasonal_strength;
        }

        // Select top _features
        let n_to_select = n_features.unwrap_or(n_feat / 2).min(n_feat);
        let mut indexed_scores: Vec<(usize, f64)> = feature_scores
            .iter()
            .enumerate()
            .map(|(i, &score)| (i, score))
            .collect();

        indexed_scores.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        let selected_features: Vec<usize> = indexed_scores
            .into_iter()
            .take(n_to_select)
            .map(|(idx_, _)| idx_)
            .collect();

        let mut metadata = HashMap::new();
        metadata.insert("seasonal_period".to_string(), seasonal_period as f64);
        metadata.insert("n_selected".to_string(), selected_features.len() as f64);

        Ok(FeatureSelectionResult {
            selected_features,
            feature_scores,
            method: "SeasonalImportance".to_string(),
            metadata,
        })
    }

    /// Cross-correlation based feature selection
    ///
    /// Selects features based on cross-correlation with target variable.
    ///
    /// # Arguments
    ///
    /// * `features` - Feature matrix (samples x features)
    /// * `target` - Target variable
    /// * `max_lag` - Maximum lag for cross-correlation
    /// * `threshold` - Minimum cross-correlation threshold
    ///
    /// # Returns
    ///
    /// * Feature selection result
    pub fn cross_correlation_selection(
        features: &Array2<f64>,
        target: &Array1<f64>,
        max_lag: usize,
        threshold: f64,
    ) -> Result<FeatureSelectionResult> {
        let (n_samples, n_feat) = features.dim();

        if n_samples != target.len() {
            return Err(TimeSeriesError::DimensionMismatch {
                expected: n_samples,
                actual: target.len(),
            });
        }

        if n_samples <= max_lag + 1 {
            return Err(TimeSeriesError::InsufficientData {
                message: "Insufficient samples for cross-correlation".to_string(),
                required: max_lag + 2,
                actual: n_samples,
            });
        }

        let mut selected_features = Vec::new();
        let mut feature_scores = Array1::zeros(n_feat);

        for i in 0..n_feat {
            let feature_col = features.column(i).to_owned();

            // Calculate cross-correlation
            let ccf = cross_correlation(&feature_col, target, Some(max_lag))?;

            // Find maximum absolute cross-correlation
            let max_ccf = ccf.iter().map(|&x| x.abs()).fold(0.0_f64, |a, b| a.max(b));

            feature_scores[i] = max_ccf;

            if max_ccf >= threshold {
                selected_features.push(i);
            }
        }

        let mut metadata = HashMap::new();
        metadata.insert("max_lag".to_string(), max_lag as f64);
        metadata.insert("threshold".to_string(), threshold);
        metadata.insert("n_selected".to_string(), selected_features.len() as f64);

        Ok(FeatureSelectionResult {
            selected_features,
            feature_scores,
            method: "CrossCorrelation".to_string(),
            metadata,
        })
    }

    /// Granger causality-based feature selection
    ///
    /// Selects features based on Granger causality test.
    ///
    /// # Arguments
    ///
    /// * `features` - Feature matrix (samples x features)
    /// * `target` - Target variable
    /// * `max_lag` - Maximum lag for Granger test
    /// * `alpha` - Significance level
    ///
    /// # Returns
    ///
    /// * Feature selection result
    pub fn granger_causality_selection(
        features: &Array2<f64>,
        target: &Array1<f64>,
        max_lag: usize,
        alpha: f64,
    ) -> Result<FeatureSelectionResult> {
        let (n_samples, n_feat) = features.dim();

        if n_samples != target.len() {
            return Err(TimeSeriesError::DimensionMismatch {
                expected: n_samples,
                actual: target.len(),
            });
        }

        if n_samples <= max_lag * 2 + 5 {
            return Err(TimeSeriesError::InsufficientData {
                message: "Insufficient samples for Granger causality test".to_string(),
                required: max_lag * 2 + 6,
                actual: n_samples,
            });
        }

        let mut selected_features = Vec::new();
        let mut feature_scores = Array1::zeros(n_feat);

        for i in 0..n_feat {
            let feature_col = features.column(i).to_owned();

            let (f_stat, p_value) = Self::granger_causality_test(&feature_col, target, max_lag)?;

            feature_scores[i] = f_stat;

            if p_value < alpha {
                selected_features.push(i);
            }
        }

        let mut metadata = HashMap::new();
        metadata.insert("max_lag".to_string(), max_lag as f64);
        metadata.insert("alpha".to_string(), alpha);
        metadata.insert("n_selected".to_string(), selected_features.len() as f64);

        Ok(FeatureSelectionResult {
            selected_features,
            feature_scores,
            method: "GrangerCausality".to_string(),
            metadata,
        })
    }

    // Helper methods

    fn calculate_correlation_arrays(
        x: &scirs2_core::ndarray::ArrayView1<f64>,
        y: &scirs2_core::ndarray::ArrayView1<f64>,
    ) -> Result<f64> {
        let n = x.len().min(y.len());

        if n < 2 {
            return Ok(0.0);
        }

        let x_mean = x.iter().take(n).sum::<f64>() / n as f64;
        let y_mean = y.iter().take(n).sum::<f64>() / n as f64;

        let mut numerator = 0.0;
        let mut x_var = 0.0;
        let mut y_var = 0.0;

        for i in 0..n {
            let x_dev = x[i] - x_mean;
            let y_dev = y[i] - y_mean;

            numerator += x_dev * y_dev;
            x_var += x_dev * x_dev;
            y_var += y_dev * y_dev;
        }

        let denominator = (x_var * y_var).sqrt();

        if denominator == 0.0 {
            Ok(0.0)
        } else {
            Ok(numerator / denominator)
        }
    }

    fn calculate_seasonal_strength(ts: &Array1<f64>, period: usize) -> Result<f64> {
        let n = ts.len();

        if n < period * 2 {
            return Ok(0.0);
        }

        // Calculate seasonal differences
        let mut seasonal_diff = Vec::new();
        for i in period..n {
            seasonal_diff.push(ts[i] - ts[i - period]);
        }

        if seasonal_diff.is_empty() {
            return Ok(0.0);
        }

        // Calculate variance of original series
        let mean = ts.sum() / n as f64;
        let var_original = ts.mapv(|x| (x - mean).powi(2)).sum() / n as f64;

        // Calculate variance of seasonal differences
        let diff_mean = seasonal_diff.iter().sum::<f64>() / seasonal_diff.len() as f64;
        let var_seasonal = seasonal_diff
            .iter()
            .map(|&x| (x - diff_mean).powi(2))
            .sum::<f64>()
            / seasonal_diff.len() as f64;

        if var_original == 0.0 {
            Ok(0.0)
        } else {
            // Seasonal strength: 1 - var(seasonal_diff) / var(original)
            let strength = 1.0 - var_seasonal / var_original;
            Ok(strength.clamp(0.0, 1.0))
        }
    }

    fn granger_causality_test(
        x: &Array1<f64>,
        y: &Array1<f64>,
        max_lag: usize,
    ) -> Result<(f64, f64)> {
        let n = x.len();

        if n <= max_lag * 2 + 1 {
            return Ok((0.0, 1.0));
        }

        // Prepare lagged data
        let effective_n = n - max_lag;
        let mut y_lagged = Array2::zeros((effective_n, max_lag));
        let mut x_lagged = Array2::zeros((effective_n, max_lag));
        let mut y_current = Array1::zeros(effective_n);

        for i in 0..effective_n {
            y_current[i] = y[i + max_lag];

            for lag in 0..max_lag {
                y_lagged[[i, lag]] = y[i + max_lag - lag - 1];
                x_lagged[[i, lag]] = x[i + max_lag - lag - 1];
            }
        }

        // Restricted model: y_t = α + Σ β_i * y_{t-i} + ε_t
        let rss_restricted = Self::fit_autoregressive_model(&y_lagged, &y_current)?;

        // Unrestricted model: y_t = α + Σ β_i * y_{t-i} + Σ γ_i * x_{t-i} + ε_t
        let mut combined_features = Array2::zeros((effective_n, max_lag * 2));
        for i in 0..effective_n {
            for j in 0..max_lag {
                combined_features[[i, j]] = y_lagged[[i, j]];
                combined_features[[i, j + max_lag]] = x_lagged[[i, j]];
            }
        }

        let rss_unrestricted = Self::fit_autoregressive_model(&combined_features, &y_current)?;

        // F-statistic
        let df1 = max_lag as f64;
        let df2 = (effective_n - max_lag * 2 - 1) as f64;

        if df2 <= 0.0 || rss_unrestricted <= 0.0 {
            return Ok((0.0, 1.0));
        }

        let f_stat = ((rss_restricted - rss_unrestricted) / df1) / (rss_unrestricted / df2);

        // Approximate p-value (simplified)
        let p_value = if f_stat > 3.0 {
            0.01
        } else if f_stat > 2.0 {
            0.05
        } else {
            0.1
        };

        Ok((f_stat.max(0.0), p_value))
    }

    fn fit_autoregressive_model(features: &Array2<f64>, target: &Array1<f64>) -> Result<f64> {
        let predictions = WrapperMethods::fit_predict_linear(features, target)?;

        let rss = target
            .iter()
            .zip(predictions.iter())
            .map(|(y_true, y_pred)| (y_true - y_pred).powi(2))
            .sum::<f64>();

        Ok(rss)
    }
}