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
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
//! Filter-based feature selection methods

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

use super::FeatureSelectionResult;
use crate::error::{Result, TimeSeriesError};
use crate::utils::autocorrelation;

/// Filter-based feature selection methods
pub struct FilterMethods;

impl FilterMethods {
    /// Variance-based feature selection
    ///
    /// Removes features with low variance (quasi-constant features).
    ///
    /// # Arguments
    ///
    /// * `features` - Feature matrix (samples x features)
    /// * `threshold` - Minimum variance threshold
    ///
    /// # Returns
    ///
    /// * Feature selection result
    ///
    /// # Example
    ///
    /// ```
    /// use scirs2_core::ndarray::Array2;
    /// use scirs2_series::feature_selection::FilterMethods;
    ///
    /// let features = Array2::from_shape_vec((100, 5), (0..500).map(|x| x as f64).collect()).expect("Operation failed");
    /// let result = FilterMethods::variance_threshold(&features, 0.1).expect("Operation failed");
    /// println!("Selected {} features", result.selected_features.len());
    /// ```
    pub fn variance_threshold(
        features: &Array2<f64>,
        threshold: f64,
    ) -> Result<FeatureSelectionResult> {
        let (n_samples, n_features) = features.dim();

        if n_samples < 2 {
            return Err(TimeSeriesError::InsufficientData {
                message: "Need at least 2 samples for variance calculation".to_string(),
                required: 2,
                actual: n_samples,
            });
        }

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

        for i in 0..n_features {
            let feature_col = features.column(i);
            let mean = feature_col.sum() / n_samples as f64;
            let variance = feature_col.mapv(|x| (x - mean).powi(2)).sum() / (n_samples - 1) as f64;

            feature_scores[i] = variance;

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

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

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

    /// Correlation-based feature selection
    ///
    /// Selects features based on their correlation with the target variable.
    ///
    /// # Arguments
    ///
    /// * `features` - Feature matrix (samples x features)
    /// * `target` - Target variable
    /// * `threshold` - Minimum absolute correlation threshold
    ///
    /// # Returns
    ///
    /// * Feature selection result
    pub fn correlation_selection(
        features: &Array2<f64>,
        target: &Array1<f64>,
        threshold: f64,
    ) -> Result<FeatureSelectionResult> {
        let (n_samples, n_features) = features.dim();

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

        if n_samples < 3 {
            return Err(TimeSeriesError::InsufficientData {
                message: "Need at least 3 samples for correlation calculation".to_string(),
                required: 3,
                actual: n_samples,
            });
        }

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

        let target_mean = target.sum() / n_samples as f64;

        for i in 0..n_features {
            let feature_col = features.column(i);
            let feature_mean = feature_col.sum() / n_samples as f64;

            let correlation =
                Self::calculate_correlation(&feature_col, target, feature_mean, target_mean)?;
            let abs_correlation = correlation.abs();

            feature_scores[i] = abs_correlation;

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

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

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

    /// Mutual information-based feature selection for time series
    ///
    /// Estimates mutual information between features and target using histogram-based approach.
    ///
    /// # Arguments
    ///
    /// * `features` - Feature matrix (samples x features)
    /// * `target` - Target variable
    /// * `n_bins` - Number of bins for histogram estimation
    /// * `n_features` - Number of top features to select
    ///
    /// # Returns
    ///
    /// * Feature selection result
    pub fn mutual_information_selection(
        features: &Array2<f64>,
        target: &Array1<f64>,
        n_bins: 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 < n_bins * 2 {
            return Err(TimeSeriesError::InsufficientData {
                message: "Insufficient samples for mutual information estimation".to_string(),
                required: n_bins * 2,
                actual: n_samples,
            });
        }

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

        for i in 0..n_feat {
            let feature_col = features.column(i);
            let mi = Self::calculate_mutual_information(&feature_col, target, n_bins)?;
            feature_scores[i] = mi;
        }

        // 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("n_bins".to_string(), n_bins as f64);
        metadata.insert("n_selected".to_string(), selected_features.len() as f64);

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

    /// F-test based feature selection
    ///
    /// Performs F-test for feature relevance.
    ///
    /// # Arguments
    ///
    /// * `features` - Feature matrix (samples x features)
    /// * `target` - Target variable
    /// * `alpha` - Significance level
    ///
    /// # Returns
    ///
    /// * Feature selection result
    pub fn f_test_selection(
        features: &Array2<f64>,
        target: &Array1<f64>,
        alpha: f64,
    ) -> Result<FeatureSelectionResult> {
        let (n_samples, n_features) = features.dim();

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

        if n_samples < 4 {
            return Err(TimeSeriesError::InsufficientData {
                message: "Need at least 4 samples for F-test".to_string(),
                required: 4,
                actual: n_samples,
            });
        }

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

        let target_mean = target.sum() / n_samples as f64;
        let sst = target.mapv(|y| (y - target_mean).powi(2)).sum();

        for i in 0..n_features {
            let feature_col = features.column(i);
            let f_stat = Self::calculate_f_statistic(&feature_col, target, target_mean, sst)?;

            feature_scores[i] = f_stat;

            // Critical value for F(1, n-2) distribution at significance level alpha
            // Simplified approximation - in practice, use proper F-distribution
            let critical_value = Self::f_critical_value(alpha, n_samples);

            if f_stat > critical_value {
                selected_features.push(i);
            }
        }

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

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

    /// Autocorrelation-based feature filtering for time series
    ///
    /// Selects features based on their autocorrelation structure.
    ///
    /// # Arguments
    ///
    /// * `features` - Feature matrix (samples x features)
    /// * `max_lag` - Maximum lag to consider
    /// * `threshold` - Minimum autocorrelation threshold
    ///
    /// # Returns
    ///
    /// * Feature selection result
    pub fn autocorrelation_filter(
        features: &Array2<f64>,
        max_lag: usize,
        threshold: f64,
    ) -> Result<FeatureSelectionResult> {
        let (n_samples, n_features) = features.dim();

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

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

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

            // Calculate autocorrelation function, handle constant series
            let max_acf = match autocorrelation(&feature_col, Some(max_lag)) {
                Ok(acf) => {
                    // Score is the maximum significant autocorrelation
                    acf.slice(scirs2_core::ndarray::s![1..])
                        .iter()
                        .map(|&x| x.abs())
                        .fold(0.0f64, |a, b| a.max(b))
                }
                Err(_) => {
                    // Constant series or other error - assign zero autocorrelation
                    0.0
                }
            };

            feature_scores[i] = max_acf;

            if max_acf >= 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: "AutocorrelationFilter".to_string(),
            metadata,
        })
    }

    // Helper methods

    fn calculate_correlation(
        x: &scirs2_core::ndarray::ArrayView1<f64>,
        y: &Array1<f64>,
        x_mean: f64,
        y_mean: f64,
    ) -> Result<f64> {
        let n = x.len();

        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_mutual_information(
        x: &scirs2_core::ndarray::ArrayView1<f64>,
        y: &Array1<f64>,
        n_bins: usize,
    ) -> Result<f64> {
        let n = x.len();

        // Create _bins
        let x_min = x.iter().fold(f64::INFINITY, |a, &b| a.min(b));
        let x_max = x.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));
        let y_min = y.iter().fold(f64::INFINITY, |a, &b| a.min(b));
        let y_max = y.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));

        if x_max == x_min || y_max == y_min {
            return Ok(0.0);
        }

        let x_bin_size = (x_max - x_min) / n_bins as f64;
        let y_bin_size = (y_max - y_min) / n_bins as f64;

        // Count occurrences
        let mut joint_counts = vec![vec![0; n_bins]; n_bins];
        let mut x_counts = vec![0; n_bins];
        let mut y_counts = vec![0; n_bins];

        for i in 0..n {
            let x_bin = ((x[i] - x_min) / x_bin_size).floor() as usize;
            let y_bin = ((y[i] - y_min) / y_bin_size).floor() as usize;

            let x_bin = x_bin.min(n_bins - 1);
            let y_bin = y_bin.min(n_bins - 1);

            joint_counts[x_bin][y_bin] += 1;
            x_counts[x_bin] += 1;
            y_counts[y_bin] += 1;
        }

        // Calculate mutual information
        let mut mi = 0.0;

        for (i_, _) in x_counts.iter().enumerate().take(n_bins) {
            for (j_, _) in y_counts.iter().enumerate().take(n_bins) {
                if joint_counts[i_][j_] > 0 && x_counts[i_] > 0 && y_counts[j_] > 0 {
                    let p_xy = joint_counts[i_][j_] as f64 / n as f64;
                    let p_x = x_counts[i_] as f64 / n as f64;
                    let p_y = y_counts[j_] as f64 / n as f64;

                    mi += p_xy * (p_xy / (p_x * p_y)).ln();
                }
            }
        }

        Ok(mi)
    }

    fn calculate_f_statistic(
        x: &scirs2_core::ndarray::ArrayView1<f64>,
        y: &Array1<f64>,
        y_mean: f64,
        sst: f64,
    ) -> Result<f64> {
        let n = x.len();

        // Calculate regression coefficients
        let x_mean = x.sum() / n as f64;

        let mut num = 0.0;
        let mut den = 0.0;

        for i in 0..n {
            let x_dev = x[i] - x_mean;
            num += x_dev * (y[i] - y_mean);
            den += x_dev * x_dev;
        }

        if den == 0.0 {
            return Ok(0.0);
        }

        let beta = num / den;
        let alpha = y_mean - beta * x_mean;

        // Calculate sum of squared residuals
        let mut ssr = 0.0;
        for i in 0..n {
            let y_pred = alpha + beta * x[i];
            ssr += (y[i] - y_pred).powi(2);
        }

        let sse = sst - ssr;

        if ssr == 0.0 {
            return Ok(f64::INFINITY);
        }

        // F-statistic = (SSE/1) / (SSR/(n-2))
        let f_stat = (sse * (n - 2) as f64) / ssr;

        Ok(f_stat)
    }

    fn f_critical_value(alpha: f64, n: usize) -> f64 {
        // Simplified approximation for F(1, n-2) critical values
        // In practice, use proper F-distribution tables or functions
        match alpha {
            a if a <= 0.01 => 6.635 + 10.0 / (n as f64),
            a if a <= 0.05 => 3.841 + 5.0 / (n as f64),
            a if a <= 0.10 => 2.706 + 3.0 / (n as f64),
            _ => 1.0,
        }
    }
}