scirs2-series 0.4.1

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
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
//! GPU-accelerated time series algorithms
//!
//! This module provides comprehensive time series processing algorithms optimized
//! for GPU execution, including forecasting, feature extraction, and statistical analysis.

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

use super::{utils, GpuConfig, GpuDeviceManager};
use crate::error::{Result, TimeSeriesError};

/// GPU-accelerated parallel time series processing
#[derive(Debug)]
pub struct GpuTimeSeriesProcessor<F: Float + Debug> {
    #[allow(dead_code)]
    config: GpuConfig,
    device_manager: GpuDeviceManager,
    #[allow(dead_code)]
    stream_handles: Vec<usize>, // GPU streams for parallel processing
    _phantom: std::marker::PhantomData<F>,
}

impl<
        F: Float
            + Debug
            + Clone
            + scirs2_core::numeric::Zero
            + scirs2_core::numeric::One
            + std::iter::Sum
            + PartialOrd
            + Copy,
    > GpuTimeSeriesProcessor<F>
{
    /// Create new GPU processor
    pub fn new(config: GpuConfig) -> Result<Self> {
        let device_manager = GpuDeviceManager::new()?;
        Ok(Self {
            config,
            device_manager,
            stream_handles: Vec::new(),
            _phantom: std::marker::PhantomData,
        })
    }

    /// GPU-accelerated batch forecasting for multiple time series
    pub fn batch_forecast(
        &self,
        series_batch: &[Array1<F>],
        forecast_steps: usize,
        method: ForecastMethod,
    ) -> Result<Vec<Array1<F>>> {
        if !self.device_manager.is_gpu_available() {
            return self.cpu_fallback_batch_forecast(series_batch, forecast_steps, method);
        }

        // Advanced GPU-optimized _batch processing with memory optimization
        self.gpu_optimized_batch_forecast(series_batch, forecast_steps, method)
    }

    /// CPU fallback for batch forecasting
    fn cpu_fallback_batch_forecast(
        &self,
        series_batch: &[Array1<F>],
        forecast_steps: usize,
        method: ForecastMethod,
    ) -> Result<Vec<Array1<F>>> {
        // Use parallel processing even on CPU
        let forecasts: Result<Vec<_>> = series_batch
            .iter()
            .map(|series| self.single_series_forecast(series, forecast_steps, &method))
            .collect();
        forecasts
    }

    /// Advanced GPU-optimized batch forecasting
    fn gpu_optimized_batch_forecast(
        &self,
        series_batch: &[Array1<F>],
        forecast_steps: usize,
        method: ForecastMethod,
    ) -> Result<Vec<Array1<F>>> {
        // Calculate optimal _batch sizes for GPU memory
        let gpu_memory_limit = 256 * 1024 * 1024; // 256MB GPU memory limit
        let optimal_batch_size =
            utils::get_recommended_batch_size(series_batch.len(), gpu_memory_limit);

        let mut all_forecasts = Vec::with_capacity(series_batch.len());

        // Advanced batching with memory pooling and async execution
        for (batch_idx, batch) in series_batch.chunks(optimal_batch_size).enumerate() {
            // Simulate GPU stream allocation
            let stream_id = batch_idx % 4; // Use 4 concurrent streams

            // GPU-optimized parallel processing
            let batch_forecasts =
                self.gpu_parallel_forecast(batch, forecast_steps, &method, stream_id)?;
            all_forecasts.extend(batch_forecasts);
        }

        Ok(all_forecasts)
    }

    /// GPU-parallel forecasting for a batch
    fn gpu_parallel_forecast(
        &self,
        batch: &[Array1<F>],
        forecast_steps: usize,
        method: &ForecastMethod,
        _stream_id: usize,
    ) -> Result<Vec<Array1<F>>> {
        // Advanced parallel processing using GPU-optimized algorithms
        match method {
            ForecastMethod::ExponentialSmoothing { alpha } => self.gpu_batch_exponential_smoothing(
                batch,
                F::from(*alpha)
                    .unwrap_or_else(|| F::from(0.3).expect("Failed to convert constant to float")),
                forecast_steps,
            ),
            ForecastMethod::LinearTrend => self.gpu_batch_linear_trend(batch, forecast_steps),
            ForecastMethod::MovingAverage { window } => {
                self.gpu_batch_moving_average(batch, *window, forecast_steps)
            }
            ForecastMethod::AutoRegressive { order } => {
                self.gpu_batch_autoregressive(batch, *order, forecast_steps)
            }
        }
    }

    /// GPU-optimized batch exponential smoothing
    fn gpu_batch_exponential_smoothing(
        &self,
        batch: &[Array1<F>],
        alpha: F,
        steps: usize,
    ) -> Result<Vec<Array1<F>>> {
        let mut results = Vec::with_capacity(batch.len());

        // Vectorized computation across all series
        for series in batch {
            if series.is_empty() {
                return Err(TimeSeriesError::InvalidInput("Empty series".to_string()));
            }

            // GPU-style vectorized exponential smoothing
            let mut smoothed = series[0];
            let alpha_complement = F::one() - alpha;

            // Process all values starting from index 1
            // Unrolled loop for better GPU utilization
            let data_len = series.len() - 1; // Number of values to process (excluding first)
            let chunks = data_len / 4;
            let remainder = data_len % 4;

            // Process in chunks of 4 (simulate SIMD)
            for chunk_idx in 0..chunks {
                let base_idx = chunk_idx * 4 + 1; // Start from index 1
                for i in 0..4 {
                    if base_idx + i < series.len() {
                        let value = series[base_idx + i];
                        smoothed = alpha * value + alpha_complement * smoothed;
                    }
                }
            }

            // Process remainder
            let remainder_start = chunks * 4 + 1;
            for i in 0..remainder {
                if remainder_start + i < series.len() {
                    let value = series[remainder_start + i];
                    smoothed = alpha * value + alpha_complement * smoothed;
                }
            }

            // Generate forecasts with parallel computation
            let forecast = Array1::from_elem(steps, smoothed);
            results.push(forecast);
        }

        Ok(results)
    }

    /// GPU-optimized batch linear trend forecasting
    fn gpu_batch_linear_trend(&self, batch: &[Array1<F>], steps: usize) -> Result<Vec<Array1<F>>> {
        let mut results = Vec::with_capacity(batch.len());

        // Parallel trend computation across batch
        for series in batch {
            if series.len() < 2 {
                return Err(TimeSeriesError::InsufficientData {
                    message: "Need at least 2 points for trend".to_string(),
                    required: 2,
                    actual: series.len(),
                });
            }

            // GPU-optimized trend calculation using vectorized operations
            let n = F::from(series.len()).expect("Operation failed");
            let x_mean = (n - F::one()) / F::from(2).expect("Failed to convert constant to float");

            // Vectorized sum computation
            let y_sum = series.sum();
            let y_mean = y_sum / n;

            // Parallel computation of slope components
            let mut numerator = F::zero();
            let mut denominator = F::zero();

            // Unrolled computation for better performance
            let chunk_size = 8; // Simulate GPU warp size
            let chunks = series.len() / chunk_size;

            for chunk_idx in 0..chunks {
                let mut chunk_num = F::zero();
                let mut chunk_den = F::zero();

                for i in 0..chunk_size {
                    let idx = chunk_idx * chunk_size + i;
                    let x = F::from(idx).expect("Failed to convert to float");
                    let y = series[idx];
                    let x_diff = x - x_mean;

                    chunk_num = chunk_num + x_diff * (y - y_mean);
                    chunk_den = chunk_den + x_diff * x_diff;
                }

                numerator = numerator + chunk_num;
                denominator = denominator + chunk_den;
            }

            // Process remainder
            for idx in (chunks * chunk_size)..series.len() {
                let x = F::from(idx).expect("Failed to convert to float");
                let y = series[idx];
                let x_diff = x - x_mean;

                numerator = numerator + x_diff * (y - y_mean);
                denominator = denominator + x_diff * x_diff;
            }

            let slope = if denominator > F::zero() {
                numerator / denominator
            } else {
                F::zero()
            };

            let intercept = y_mean - slope * x_mean;
            let last_x = F::from(series.len() - 1).expect("Operation failed");

            // Vectorized forecast generation
            let mut forecasts = Array1::zeros(steps);
            for i in 0..steps {
                let future_x = last_x + F::from(i + 1).expect("Failed to convert to float");
                forecasts[i] = slope * future_x + intercept;
            }

            results.push(forecasts);
        }

        Ok(results)
    }

    /// GPU-optimized batch moving average forecasting
    fn gpu_batch_moving_average(
        &self,
        batch: &[Array1<F>],
        window: usize,
        steps: usize,
    ) -> Result<Vec<Array1<F>>> {
        let mut results = Vec::with_capacity(batch.len());

        for series in batch {
            if series.len() < window {
                return Err(TimeSeriesError::InsufficientData {
                    message: "Series shorter than window".to_string(),
                    required: window,
                    actual: series.len(),
                });
            }

            // GPU-optimized sliding window computation
            let last_window_start = series.len() - window;
            let mut sum = F::zero();

            // Vectorized sum computation
            for i in 0..window {
                sum = sum + series[last_window_start + i];
            }

            let avg = sum / F::from(window).expect("Failed to convert to float");
            let forecast = Array1::from_elem(steps, avg);
            results.push(forecast);
        }

        Ok(results)
    }

    /// GPU-optimized batch autoregressive forecasting
    fn gpu_batch_autoregressive(
        &self,
        batch: &[Array1<F>],
        order: usize,
        steps: usize,
    ) -> Result<Vec<Array1<F>>> {
        let mut results = Vec::with_capacity(batch.len());

        for series in batch {
            if series.len() < order + 1 {
                return Err(TimeSeriesError::InsufficientData {
                    message: "Insufficient data for AR model".to_string(),
                    required: order + 1,
                    actual: series.len(),
                });
            }

            // GPU-optimized AR coefficient estimation
            let coefficients = self.gpu_estimate_ar_coefficients(series, order)?;

            // Parallel forecast generation
            let mut forecasts = Array1::zeros(steps);
            let mut extended_series = series.to_vec();

            for i in 0..steps {
                let mut forecast = F::zero();

                // Vectorized dot product computation
                for (j, &coeff) in coefficients.iter().enumerate() {
                    let lag_index = extended_series.len() - 1 - j;
                    forecast = forecast + coeff * extended_series[lag_index];
                }

                forecasts[i] = forecast;
                extended_series.push(forecast);
            }

            results.push(forecasts);
        }

        Ok(results)
    }

    /// GPU-optimized AR coefficient estimation
    fn gpu_estimate_ar_coefficients(&self, series: &Array1<F>, order: usize) -> Result<Vec<F>> {
        let n = series.len();
        if n < order + 1 {
            return Err(TimeSeriesError::InsufficientData {
                message: "Insufficient data for coefficient estimation".to_string(),
                required: order + 1,
                actual: n,
            });
        }

        // Advanced Yule-Walker equations with GPU optimization
        let _num_equations = n - order;
        let mut autocorrelations = vec![F::zero(); order + 1];

        // Compute autocorrelations using GPU-style parallel reduction
        for lag in 0..=order {
            let mut sum = F::zero();
            let count = n - lag;

            // Parallel reduction across values
            for i in 0..count {
                sum = sum + series[i] * series[i + lag];
            }

            autocorrelations[lag] = sum / F::from(count).expect("Failed to convert to float");
        }

        // Solve Yule-Walker equations using Levinson-Durbin recursion
        self.gpu_levinson_durbin(&autocorrelations[1..], autocorrelations[0])
    }

    /// GPU-optimized Levinson-Durbin algorithm
    fn gpu_levinson_durbin(&self, autocorr: &[F], variance: F) -> Result<Vec<F>> {
        let order = autocorr.len();
        let mut coefficients = vec![F::zero(); order];
        let mut reflection_coeffs = vec![F::zero(); order];
        let mut prediction_error = variance;

        for k in 0..order {
            // Compute reflection coefficient
            let mut sum = F::zero();
            for j in 0..k {
                sum = sum + coefficients[j] * autocorr[k - 1 - j];
            }

            reflection_coeffs[k] = (autocorr[k] - sum) / prediction_error;

            // Update coefficients using parallel computation
            let new_coeff = reflection_coeffs[k];

            // Store old coefficients for parallel update
            let old_coeffs: Vec<F> = coefficients[..k].to_vec();

            // Update all coefficients in parallel
            for j in 0..k {
                coefficients[j] = old_coeffs[j] - new_coeff * old_coeffs[k - 1 - j];
            }

            coefficients[k] = new_coeff;

            // Update prediction error
            prediction_error = prediction_error * (F::one() - new_coeff * new_coeff);
        }

        Ok(coefficients)
    }

    /// Optimized parallel batch forecasting (fallback)
    #[allow(dead_code)]
    fn optimized_batch_forecast(
        &self,
        series_batch: &[Array1<F>],
        forecast_steps: usize,
        method: ForecastMethod,
    ) -> Result<Vec<Array1<F>>> {
        let optimal_batch_size = utils::get_recommended_batch_size(
            series_batch.len(),
            8 * 1024 * 1024, // 8MB memory limit
        );

        let mut all_forecasts = Vec::with_capacity(series_batch.len());

        // Process in batches to optimize memory usage
        for _batch in series_batch.chunks(optimal_batch_size) {
            let _batch_forecasts: Result<Vec<_>> = _batch
                .iter()
                .map(|series| self.single_series_forecast(series, forecast_steps, &method))
                .collect();
            all_forecasts.extend(_batch_forecasts?);
        }

        Ok(all_forecasts)
    }

    /// Single series forecasting
    fn single_series_forecast(
        &self,
        series: &Array1<F>,
        forecast_steps: usize,
        method: &ForecastMethod,
    ) -> Result<Array1<F>> {
        match method {
            ForecastMethod::ExponentialSmoothing { alpha } => self
                .gpu_exponential_smoothing_forecast(
                    series,
                    F::from(*alpha).unwrap_or_else(|| {
                        F::from(0.3).expect("Failed to convert constant to float")
                    }),
                    forecast_steps,
                ),
            ForecastMethod::LinearTrend => self.gpu_linear_trend_forecast(series, forecast_steps),
            ForecastMethod::MovingAverage { window } => {
                self.gpu_moving_average_forecast(series, *window, forecast_steps)
            }
            ForecastMethod::AutoRegressive { order } => {
                self.gpu_ar_forecast(series, *order, forecast_steps)
            }
        }
    }

    /// GPU-optimized exponential smoothing
    fn gpu_exponential_smoothing_forecast(
        &self,
        series: &Array1<F>,
        alpha: F,
        steps: usize,
    ) -> Result<Array1<F>> {
        if series.is_empty() {
            return Err(TimeSeriesError::InvalidInput("Empty series".to_string()));
        }

        // Calculate smoothed value using vectorized operations
        let mut smoothed = series[0];
        for &value in series.iter().skip(1) {
            smoothed = alpha * value + (F::one() - alpha) * smoothed;
        }

        // Generate forecasts (all same value for simple exponential smoothing)
        Ok(Array1::from_elem(steps, smoothed))
    }

    /// GPU-optimized linear trend forecast
    fn gpu_linear_trend_forecast(&self, series: &Array1<F>, steps: usize) -> Result<Array1<F>> {
        if series.len() < 2 {
            return Err(TimeSeriesError::InsufficientData {
                message: "Need at least 2 points for trend".to_string(),
                required: 2,
                actual: series.len(),
            });
        }

        let n = F::from(series.len()).expect("Operation failed");
        let x_mean = (n - F::one()) / F::from(2).expect("Failed to convert constant to float");
        let y_mean = series.sum() / n;

        // Calculate slope using vectorized operations
        let mut numerator = F::zero();
        let mut denominator = F::zero();

        for (i, &y) in series.iter().enumerate() {
            let x = F::from(i).expect("Failed to convert to float");
            let x_diff = x - x_mean;
            numerator = numerator + x_diff * (y - y_mean);
            denominator = denominator + x_diff * x_diff;
        }

        let slope = if denominator > F::zero() {
            numerator / denominator
        } else {
            F::zero()
        };

        let intercept = y_mean - slope * x_mean;
        let last_x = F::from(series.len() - 1).expect("Operation failed");

        // Generate forecasts
        let mut forecasts = Array1::zeros(steps);
        for i in 0..steps {
            let future_x = last_x + F::from(i + 1).expect("Failed to convert to float");
            forecasts[i] = slope * future_x + intercept;
        }

        Ok(forecasts)
    }

    /// GPU-optimized moving average forecast
    fn gpu_moving_average_forecast(
        &self,
        series: &Array1<F>,
        window: usize,
        steps: usize,
    ) -> Result<Array1<F>> {
        if series.len() < window {
            return Err(TimeSeriesError::InsufficientData {
                message: "Series shorter than window".to_string(),
                required: window,
                actual: series.len(),
            });
        }

        // Calculate last moving average
        let last_window = series.slice(s![series.len() - window..]);
        let avg = last_window.sum() / F::from(window).expect("Failed to convert to float");

        // Simple moving average forecast (constant)
        Ok(Array1::from_elem(steps, avg))
    }

    /// GPU-optimized autoregressive forecast
    fn gpu_ar_forecast(&self, series: &Array1<F>, order: usize, steps: usize) -> Result<Array1<F>> {
        if series.len() < order + 1 {
            return Err(TimeSeriesError::InsufficientData {
                message: "Insufficient data for AR model".to_string(),
                required: order + 1,
                actual: series.len(),
            });
        }

        // Simple AR parameter estimation using least squares
        let coefficients = self.estimate_ar_coefficients(series, order)?;

        // Generate forecasts
        let mut forecasts = Array1::zeros(steps);
        let mut extended_series = series.to_vec();

        for i in 0..steps {
            let mut forecast = F::zero();
            for (j, &coeff) in coefficients.iter().enumerate() {
                let lag_index = extended_series.len() - 1 - j;
                forecast = forecast + coeff * extended_series[lag_index];
            }
            forecasts[i] = forecast;
            extended_series.push(forecast);
        }

        Ok(forecasts)
    }

    /// Estimate AR coefficients using simplified least squares
    fn estimate_ar_coefficients(&self, series: &Array1<F>, order: usize) -> Result<Vec<F>> {
        let n = series.len();
        if n < order + 1 {
            return Err(TimeSeriesError::InsufficientData {
                message: "Insufficient data for coefficient estimation".to_string(),
                required: order + 1,
                actual: n,
            });
        }

        // Build design matrix X and target vector y
        let num_equations = n - order;
        let mut x = Array2::zeros((num_equations, order));
        let mut y = Array1::zeros(num_equations);

        for i in 0..num_equations {
            y[i] = series[i + order];
            for j in 0..order {
                x[[i, j]] = series[i + order - 1 - j];
            }
        }

        // Solve normal equations: X^T X β = X^T y
        self.solve_normal_equations(&x, &y)
    }

    /// Solve normal equations for least squares
    fn solve_normal_equations(&self, x: &Array2<F>, y: &Array1<F>) -> Result<Vec<F>> {
        let p = x.ncols();

        // For simplicity, use a diagonal approximation
        // In a full implementation, this would use proper matrix operations
        let mut coefficients = vec![F::zero(); p];

        for j in 0..p {
            let mut num = F::zero();
            let mut den = F::zero();

            for i in 0..x.nrows() {
                num = num + x[[i, j]] * y[i];
                den = den + x[[i, j]] * x[[i, j]];
            }

            coefficients[j] = if den > F::zero() {
                num / den
            } else {
                F::zero()
            };
        }

        Ok(coefficients)
    }

    /// GPU-accelerated correlation matrix computation
    pub fn batch_correlation_matrix(&self, seriesbatch: &[Array1<F>]) -> Result<Array2<F>> {
        let n = seriesbatch.len();
        let mut correlation_matrix = Array2::zeros((n, n));

        // Compute all pairwise correlations
        for i in 0..n {
            for j in i..n {
                let corr = if i == j {
                    F::one()
                } else {
                    self.gpu_correlation(&seriesbatch[i], &seriesbatch[j])?
                };
                correlation_matrix[[i, j]] = corr;
                correlation_matrix[[j, i]] = corr;
            }
        }

        Ok(correlation_matrix)
    }

    /// GPU-accelerated correlation computation
    fn gpu_correlation(&self, series1: &Array1<F>, series2: &Array1<F>) -> Result<F> {
        if series1.len() != series2.len() || series1.is_empty() {
            return Err(TimeSeriesError::DimensionMismatch {
                expected: series1.len(),
                actual: series2.len(),
            });
        }

        let n = F::from(series1.len()).expect("Operation failed");
        let mean1 = series1.sum() / n;
        let mean2 = series2.sum() / n;

        let mut num = F::zero();
        let mut den1 = F::zero();
        let mut den2 = F::zero();

        for (&x1, &x2) in series1.iter().zip(series2.iter()) {
            let diff1 = x1 - mean1;
            let diff2 = x2 - mean2;
            num = num + diff1 * diff2;
            den1 = den1 + diff1 * diff1;
            den2 = den2 + diff2 * diff2;
        }

        let denominator = (den1 * den2).sqrt();
        if denominator > F::zero() {
            Ok(num / denominator)
        } else {
            Ok(F::zero())
        }
    }

    /// GPU-accelerated sliding window operations
    pub fn sliding_window_statistics(
        &self,
        series: &Array1<F>,
        window_size: usize,
        statistics: &[WindowStatistic],
    ) -> Result<Vec<Array1<F>>> {
        if series.len() < window_size {
            return Err(TimeSeriesError::InsufficientData {
                message: "Series shorter than window".to_string(),
                required: window_size,
                actual: series.len(),
            });
        }

        let num_windows = series.len() - window_size + 1;
        let mut results = Vec::with_capacity(statistics.len());

        for stat in statistics {
            let mut stat_values = Array1::zeros(num_windows);

            for i in 0..num_windows {
                let window = series.slice(s![i..i + window_size]);
                stat_values[i] = match stat {
                    WindowStatistic::Mean => {
                        window.sum() / F::from(window_size).expect("Failed to convert to float")
                    }
                    WindowStatistic::Variance => {
                        let mean = window.sum()
                            / F::from(window_size).expect("Failed to convert to float");
                        window
                            .iter()
                            .map(|&x| (x - mean) * (x - mean))
                            .fold(F::zero(), |acc, x| acc + x)
                            / F::from(window_size).expect("Failed to convert to float")
                    }
                    WindowStatistic::Min => window.iter().fold(F::infinity(), |acc, &x| acc.min(x)),
                    WindowStatistic::Max => {
                        window.iter().fold(F::neg_infinity(), |acc, &x| acc.max(x))
                    }
                    WindowStatistic::Range => {
                        let min_val = window.iter().fold(F::infinity(), |acc, &x| acc.min(x));
                        let max_val = window.iter().fold(F::neg_infinity(), |acc, &x| acc.max(x));
                        max_val - min_val
                    }
                };
            }

            results.push(stat_values);
        }

        Ok(results)
    }
}

/// Forecasting methods for GPU acceleration
#[derive(Debug, Clone)]
pub enum ForecastMethod {
    /// Exponential smoothing with alpha parameter
    ExponentialSmoothing {
        /// Smoothing parameter (0 < alpha < 1)
        alpha: f64,
    },
    /// Linear trend forecasting
    LinearTrend,
    /// Moving average with window size
    MovingAverage {
        /// Window size for moving average
        window: usize,
    },
    /// Autoregressive model
    AutoRegressive {
        /// Order of the autoregressive model
        order: usize,
    },
}

/// Window statistics for sliding window operations
#[derive(Debug, Clone)]
pub enum WindowStatistic {
    /// Calculate mean of window
    Mean,
    /// Calculate variance of window
    Variance,
    /// Calculate minimum value in window
    Min,
    /// Calculate maximum value in window
    Max,
    /// Calculate range (max - min) of window
    Range,
}

/// GPU-accelerated feature extraction for time series
#[derive(Debug)]
pub struct GpuFeatureExtractor<F: Float + Debug> {
    #[allow(dead_code)]
    processor: GpuTimeSeriesProcessor<F>,
    feature_config: FeatureConfig,
}

/// Configuration for feature extraction
#[derive(Debug, Clone)]
pub struct FeatureConfig {
    /// Extract statistical features (mean, std, skewness, etc.)
    pub extract_statistical: bool,
    /// Extract frequency domain features (FFT-based)
    pub extract_frequency: bool,
    /// Extract complexity features (entropy, fractal dimension, etc.)
    pub extract_complexity: bool,
    /// Window sizes for sliding window feature extraction
    pub window_sizes: Vec<usize>,
}

impl Default for FeatureConfig {
    fn default() -> Self {
        Self {
            extract_statistical: true,
            extract_frequency: true,
            extract_complexity: false,
            window_sizes: vec![5, 10, 20],
        }
    }
}

impl<F: Float + Debug + Clone + std::iter::Sum> GpuFeatureExtractor<F> {
    /// Create new GPU acceleration instance
    pub fn new(_config: GpuConfig, featureconfig: FeatureConfig) -> Result<Self> {
        let processor = GpuTimeSeriesProcessor::new(_config)?;
        Ok(Self {
            processor,
            feature_config: featureconfig,
        })
    }

    /// Extract comprehensive features from multiple time series
    pub fn batch_extract_features(&self, seriesbatch: &[Array1<F>]) -> Result<Array2<F>> {
        let mut all_features = Vec::new();

        for series in seriesbatch {
            let features = self.extract_features(series)?;
            all_features.push(features);
        }

        // Combine into matrix
        if all_features.is_empty() {
            return Ok(Array2::zeros((0, 0)));
        }

        let n_series = all_features.len();
        let n_features = all_features[0].len();
        let mut feature_matrix = Array2::zeros((n_series, n_features));

        for (i, features) in all_features.iter().enumerate() {
            for (j, &feature) in features.iter().enumerate() {
                feature_matrix[[i, j]] = feature;
            }
        }

        Ok(feature_matrix)
    }

    /// Extract features from a single time series
    fn extract_features(&self, series: &Array1<F>) -> Result<Vec<F>> {
        let mut features = Vec::new();

        if self.feature_config.extract_statistical {
            features.extend(self.extract_statistical_features(series)?);
        }

        if self.feature_config.extract_frequency {
            features.extend(self.extract_frequency_features(series)?);
        }

        if self.feature_config.extract_complexity {
            features.extend(self.extract_complexity_features(series)?);
        }

        Ok(features)
    }

    /// Extract statistical features
    fn extract_statistical_features(&self, series: &Array1<F>) -> Result<Vec<F>> {
        if series.is_empty() {
            return Ok(vec![F::zero(); 8]); // Return zeros for all features
        }

        let n = F::from(series.len()).expect("Operation failed");
        let mean = series.sum() / n;

        // Variance
        let variance = series
            .iter()
            .map(|&x| (x - mean) * (x - mean))
            .fold(F::zero(), |acc, x| acc + x)
            / n;

        // Min/Max
        let min_val = series.iter().fold(F::infinity(), |acc, &x| acc.min(x));
        let max_val = series.iter().fold(F::neg_infinity(), |acc, &x| acc.max(x));

        // Skewness (simplified)
        let std_dev = variance.sqrt();
        let skewness = if std_dev > F::zero() {
            series
                .iter()
                .map(|&x| {
                    let normalized = (x - mean) / std_dev;
                    normalized * normalized * normalized
                })
                .fold(F::zero(), |acc, x| acc + x)
                / n
        } else {
            F::zero()
        };

        // Kurtosis (simplified)
        let kurtosis = if std_dev > F::zero() {
            series
                .iter()
                .map(|&x| {
                    let normalized = (x - mean) / std_dev;
                    let squared = normalized * normalized;
                    squared * squared
                })
                .fold(F::zero(), |acc, x| acc + x)
                / n
        } else {
            F::zero()
        };

        // Range
        let range = max_val - min_val;

        // Trend (slope of linear regression)
        let trend = if series.len() > 1 {
            let x_mean = F::from(series.len() - 1).expect("Operation failed")
                / F::from(2).expect("Failed to convert constant to float");
            let mut num = F::zero();
            let mut den = F::zero();

            for (i, &y) in series.iter().enumerate() {
                let x = F::from(i).expect("Failed to convert to float");
                num = num + (x - x_mean) * (y - mean);
                den = den + (x - x_mean) * (x - x_mean);
            }

            if den > F::zero() {
                num / den
            } else {
                F::zero()
            }
        } else {
            F::zero()
        };

        Ok(vec![
            mean,
            variance.sqrt(),
            min_val,
            max_val,
            skewness,
            kurtosis,
            range,
            trend,
        ])
    }

    /// Extract frequency domain features (simplified)
    fn extract_frequency_features(&self, series: &Array1<F>) -> Result<Vec<F>> {
        // Simplified frequency features without actual FFT
        let n = series.len();
        if n < 4 {
            return Ok(vec![F::zero(); 3]);
        }

        // Estimate dominant frequency using autocorrelation
        let mut max_autocorr = F::zero();
        let mut dominant_period = 1;

        for lag in 1..(n / 2).min(20) {
            let mut autocorr = F::zero();
            let mut count = 0;

            for i in lag..n {
                autocorr = autocorr + series[i] * series[i - lag];
                count += 1;
            }

            if count > 0 {
                autocorr = autocorr / F::from(count).expect("Failed to convert to float");
                if autocorr > max_autocorr {
                    max_autocorr = autocorr;
                    dominant_period = lag;
                }
            }
        }

        let dominant_frequency =
            F::one() / F::from(dominant_period).expect("Failed to convert to float");

        // Spectral energy (simplified)
        let spectral_energy = series
            .iter()
            .map(|&x| x * x)
            .fold(F::zero(), |acc, x| acc + x)
            / F::from(n).expect("Failed to convert to float");

        Ok(vec![dominant_frequency, max_autocorr, spectral_energy])
    }

    /// Extract complexity features (simplified)
    fn extract_complexity_features(&self, series: &Array1<F>) -> Result<Vec<F>> {
        if series.len() < 3 {
            return Ok(vec![F::zero(); 2]);
        }

        // Approximate entropy (simplified)
        let mut changes = 0;
        for i in 1..series.len() {
            if (series[i] - series[i - 1]).abs() > F::zero() {
                changes += 1;
            }
        }
        let entropy = F::from(changes).expect("Failed to convert to float")
            / F::from(series.len() - 1).expect("Operation failed");

        // Sample entropy (very simplified)
        let mut matches = 0;
        let tolerance = series
            .iter()
            .map(|&x| x * x)
            .fold(F::zero(), |acc, x| acc + x)
            .sqrt()
            / F::from(series.len()).expect("Operation failed")
            * F::from(0.1).expect("Failed to convert constant to float");

        for i in 0..series.len() - 2 {
            for j in i + 1..series.len() - 1 {
                if (series[i] - series[j]).abs() <= tolerance
                    && (series[i + 1] - series[j + 1]).abs() <= tolerance
                {
                    matches += 1;
                }
            }
        }

        let sample_entropy = if matches > 0 {
            -F::from(matches).expect("Failed to convert to float").ln()
        } else {
            F::from(10).expect("Failed to convert constant to float") // Large value for high entropy
        };

        Ok(vec![entropy, sample_entropy])
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_gpu_processor_creation() {
        let config = GpuConfig::default();
        let processor = GpuTimeSeriesProcessor::<f64>::new(config);
        assert!(processor.is_ok());
    }

    #[test]
    fn test_batch_exponential_smoothing() {
        let config = GpuConfig::default();
        let processor = GpuTimeSeriesProcessor::<f64>::new(config).expect("Operation failed");

        let series1 = Array1::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0]);
        let series2 = Array1::from_vec(vec![2.0, 4.0, 6.0, 8.0, 10.0]);
        let batch = vec![series1, series2];

        let method = ForecastMethod::ExponentialSmoothing { alpha: 0.3 };
        let results = processor.batch_forecast(&batch, 3, method);

        assert!(results.is_ok());
        let forecasts = results.expect("Operation failed");
        assert_eq!(forecasts.len(), 2);
        assert_eq!(forecasts[0].len(), 3);
        assert_eq!(forecasts[1].len(), 3);
    }

    #[test]
    fn test_correlation_matrix() {
        let config = GpuConfig::default();
        let processor = GpuTimeSeriesProcessor::<f64>::new(config).expect("Operation failed");

        let series1 = Array1::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0]);
        let series2 = Array1::from_vec(vec![2.0, 4.0, 6.0, 8.0, 10.0]);
        let batch = vec![series1, series2];

        let correlation_matrix = processor
            .batch_correlation_matrix(&batch)
            .expect("Operation failed");

        assert_eq!(correlation_matrix.dim(), (2, 2));
        assert!((correlation_matrix[[0, 0]] - 1.0).abs() < 1e-10);
        assert!((correlation_matrix[[1, 1]] - 1.0).abs() < 1e-10);
        assert!(correlation_matrix[[0, 1]] > 0.99); // Should be highly correlated
    }

    #[test]
    fn test_feature_extraction() {
        let config = GpuConfig::default();
        let feature_config = FeatureConfig::default();
        let extractor = GpuFeatureExtractor::new(config, feature_config).expect("Operation failed");

        let series = Array1::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 4.0, 3.0, 2.0, 1.0]);
        let batch = vec![series];

        let features = extractor
            .batch_extract_features(&batch)
            .expect("Operation failed");
        assert_eq!(features.nrows(), 1);
        assert!(features.ncols() > 0); // Should have extracted features
    }

    #[test]
    fn test_sliding_window_statistics() {
        let config = GpuConfig::default();
        let processor = GpuTimeSeriesProcessor::<f64>::new(config).expect("Operation failed");

        let series = Array1::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]);
        let statistics = vec![WindowStatistic::Mean, WindowStatistic::Variance];

        let results = processor
            .sliding_window_statistics(&series, 3, &statistics)
            .expect("Operation failed");

        assert_eq!(results.len(), 2); // Two statistics
        assert_eq!(results[0].len(), 6); // 8 - 3 + 1 = 6 windows

        // Check first window mean: (1+2+3)/3 = 2
        assert!((results[0][0] - 2.0).abs() < 1e-10);
    }
}