scirs2-series 0.3.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
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
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
//! Exponential Smoothing (ETS) Models
//!
//! Implements the Error-Trend-Seasonal (ETS) framework for time series forecasting:
//! - Simple Exponential Smoothing (SES) - no trend, no seasonality
//! - Holt's Double Exponential Smoothing - additive/multiplicative trend
//! - Holt-Winters Triple Exponential Smoothing - trend + seasonality
//!
//! Each model supports parameter estimation via maximum likelihood, forecasting
//! with confidence intervals, and information criteria for model selection.
//!
//! # References
//!
//! - Hyndman, R.J. & Athanasopoulos, G. (2021) "Forecasting: Principles and Practice"
//! - Hyndman, R.J., Koehler, A.B., Ord, J.K. & Snyder, R.D. (2008) "Forecasting with
//!   Exponential Smoothing: The State Space Approach"

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

use crate::error::{Result, TimeSeriesError};

/// Error type in the ETS framework
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ETSError {
    /// Additive errors (Normal distribution)
    Additive,
    /// Multiplicative errors (percentage errors)
    Multiplicative,
}

/// Trend type in the ETS framework
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ETSTrend {
    /// No trend component
    None,
    /// Additive trend
    Additive,
    /// Multiplicative trend (requires positive data)
    Multiplicative,
    /// Damped additive trend
    DampedAdditive,
    /// Damped multiplicative trend (requires positive data)
    DampedMultiplicative,
}

/// Seasonal type in the ETS framework
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ETSSeasonal {
    /// No seasonal component
    None,
    /// Additive seasonality
    Additive,
    /// Multiplicative seasonality (requires positive data)
    Multiplicative,
}

/// Configuration for ETS model
#[derive(Debug, Clone)]
pub struct ETSConfig {
    /// Error type
    pub error: ETSError,
    /// Trend type
    pub trend: ETSTrend,
    /// Seasonal type
    pub seasonal: ETSSeasonal,
    /// Seasonal period (required if seasonal != None)
    pub period: Option<usize>,
    /// Level smoothing parameter alpha (None = auto-estimate)
    pub alpha: Option<f64>,
    /// Trend smoothing parameter beta (None = auto-estimate)
    pub beta: Option<f64>,
    /// Seasonal smoothing parameter gamma (None = auto-estimate)
    pub gamma: Option<f64>,
    /// Trend damping parameter phi (None = auto-estimate, only for damped trends)
    pub phi: Option<f64>,
    /// Maximum iterations for parameter optimization
    pub max_iter: usize,
    /// Convergence tolerance for parameter optimization
    pub tolerance: f64,
}

impl Default for ETSConfig {
    fn default() -> Self {
        Self {
            error: ETSError::Additive,
            trend: ETSTrend::None,
            seasonal: ETSSeasonal::None,
            period: None,
            alpha: None,
            beta: None,
            gamma: None,
            phi: None,
            max_iter: 500,
            tolerance: 1e-8,
        }
    }
}

/// State components of an ETS model at each time step
#[derive(Debug, Clone)]
struct ETSState<F> {
    /// Level component
    level: Vec<F>,
    /// Trend component (empty if no trend)
    trend: Vec<F>,
    /// Seasonal component (empty if no seasonality)
    seasonal: Vec<F>,
}

/// Fitted ETS (Error-Trend-Seasonal) model
///
/// This struct contains the fitted model parameters, state components,
/// and provides methods for forecasting and diagnostics.
#[derive(Debug, Clone)]
pub struct ETSModel<F> {
    /// Model configuration
    pub config: ETSConfig,
    /// Level smoothing parameter
    pub alpha: F,
    /// Trend smoothing parameter
    pub beta: F,
    /// Seasonal smoothing parameter
    pub gamma: F,
    /// Damping parameter
    pub phi: F,
    /// Initial level
    pub initial_level: F,
    /// Initial trend
    pub initial_trend: F,
    /// Initial seasonal indices
    pub initial_seasonal: Array1<F>,
    /// Residual variance (sigma^2)
    pub sigma2: F,
    /// Log-likelihood
    pub log_likelihood: F,
    /// Number of observations
    pub n_obs: usize,
    /// Number of parameters estimated
    pub n_params: usize,
    /// Whether the model has been fitted
    pub is_fitted: bool,
    /// Fitted values (one-step-ahead predictions)
    fitted_values: Array1<F>,
    /// Residuals
    residuals: Array1<F>,
    /// Final state for forecasting
    final_level: F,
    final_trend: F,
    final_seasonal: Vec<F>,
}

impl<F> ETSModel<F>
where
    F: Float + FromPrimitive + Debug + Display + ScalarOperand,
{
    /// Create a new unfitted ETS model with the given configuration
    pub fn new(config: ETSConfig) -> Result<Self> {
        // Validate configuration
        if config.seasonal != ETSSeasonal::None && config.period.is_none() {
            return Err(TimeSeriesError::InvalidParameter {
                name: "period".to_string(),
                message: "Seasonal period must be specified for seasonal models".to_string(),
            });
        }

        if let Some(period) = config.period {
            if period < 2 {
                return Err(TimeSeriesError::InvalidParameter {
                    name: "period".to_string(),
                    message: "Seasonal period must be at least 2".to_string(),
                });
            }
        }

        // Validate that alpha/beta/gamma/phi are in (0,1) if provided
        if let Some(alpha) = config.alpha {
            if alpha <= 0.0 || alpha >= 1.0 {
                return Err(TimeSeriesError::InvalidParameter {
                    name: "alpha".to_string(),
                    message: "Alpha must be in (0, 1)".to_string(),
                });
            }
        }
        if let Some(beta) = config.beta {
            if beta <= 0.0 || beta >= 1.0 {
                return Err(TimeSeriesError::InvalidParameter {
                    name: "beta".to_string(),
                    message: "Beta must be in (0, 1)".to_string(),
                });
            }
        }
        if let Some(gamma) = config.gamma {
            if gamma <= 0.0 || gamma >= 1.0 {
                return Err(TimeSeriesError::InvalidParameter {
                    name: "gamma".to_string(),
                    message: "Gamma must be in (0, 1)".to_string(),
                });
            }
        }
        if let Some(phi) = config.phi {
            if phi <= 0.0 || phi >= 1.0 {
                return Err(TimeSeriesError::InvalidParameter {
                    name: "phi".to_string(),
                    message: "Phi must be in (0, 1)".to_string(),
                });
            }
        }

        Ok(Self {
            config,
            alpha: F::zero(),
            beta: F::zero(),
            gamma: F::zero(),
            phi: F::one(),
            initial_level: F::zero(),
            initial_trend: F::zero(),
            initial_seasonal: Array1::zeros(0),
            sigma2: F::one(),
            log_likelihood: F::neg_infinity(),
            n_obs: 0,
            n_params: 0,
            is_fitted: false,
            fitted_values: Array1::zeros(0),
            residuals: Array1::zeros(0),
            final_level: F::zero(),
            final_trend: F::zero(),
            final_seasonal: Vec::new(),
        })
    }

    /// Fit the ETS model to data
    ///
    /// Estimates the smoothing parameters and initial states via maximum likelihood.
    pub fn fit(&mut self, data: &Array1<F>) -> Result<()> {
        let n = data.len();
        let period = self.config.period.unwrap_or(1);

        // Validate data length
        let min_length = match self.config.seasonal {
            ETSSeasonal::None => 3,
            _ => 2 * period + 1,
        };

        if n < min_length {
            return Err(TimeSeriesError::InsufficientData {
                message: "Insufficient data for ETS model fitting".to_string(),
                required: min_length,
                actual: n,
            });
        }

        // Check for positive data if multiplicative components
        if self.config.trend == ETSTrend::Multiplicative
            || self.config.trend == ETSTrend::DampedMultiplicative
            || self.config.seasonal == ETSSeasonal::Multiplicative
            || self.config.error == ETSError::Multiplicative
        {
            if data.iter().any(|&x| x <= F::zero()) {
                return Err(TimeSeriesError::InvalidInput(
                    "Multiplicative components require strictly positive data".to_string(),
                ));
            }
        }

        self.n_obs = n;

        // Initialize state values
        self.initialize_states(data)?;

        // Optimize parameters
        self.optimize_parameters(data)?;

        // Compute final fitted values and residuals using optimal parameters
        let (_ll, fitted, resid, state) = self.evaluate(data)?;

        self.fitted_values = fitted;
        self.residuals = resid.clone();
        self.sigma2 = resid.iter().fold(F::zero(), |acc, &r| acc + r * r)
            / F::from(n).ok_or_else(|| {
                TimeSeriesError::NumericalInstability("Failed to convert n".to_string())
            })?;

        // Store final state for forecasting
        self.final_level = *state.level.last().unwrap_or(&F::zero());
        self.final_trend = *state.trend.last().unwrap_or(&F::zero());
        self.final_seasonal = state.seasonal.clone();

        // Calculate log-likelihood
        let two_pi = F::from(2.0 * std::f64::consts::PI).ok_or_else(|| {
            TimeSeriesError::NumericalInstability("Failed to convert 2pi".to_string())
        })?;
        let n_f = F::from(n).ok_or_else(|| {
            TimeSeriesError::NumericalInstability("Failed to convert n".to_string())
        })?;
        let two = F::from(2.0).ok_or_else(|| {
            TimeSeriesError::NumericalInstability("Failed to convert constant".to_string())
        })?;

        self.log_likelihood = -n_f / two * (F::one() + two_pi.ln()) - n_f / two * self.sigma2.ln();

        // Count parameters
        self.n_params = 1; // alpha
        if self.config.trend != ETSTrend::None {
            self.n_params += 1; // beta
        }
        if self.config.seasonal != ETSSeasonal::None {
            self.n_params += 1; // gamma
        }
        if self.config.trend == ETSTrend::DampedAdditive
            || self.config.trend == ETSTrend::DampedMultiplicative
        {
            self.n_params += 1; // phi
        }
        // Initial states
        self.n_params += 1; // initial level
        if self.config.trend != ETSTrend::None {
            self.n_params += 1; // initial trend
        }
        if self.config.seasonal != ETSSeasonal::None {
            self.n_params += period - 1; // initial seasonal indices (one is constrained)
        }

        self.is_fitted = true;
        Ok(())
    }

    /// Initialize state values from the data
    fn initialize_states(&mut self, data: &Array1<F>) -> Result<()> {
        let period = self.config.period.unwrap_or(1);

        match self.config.seasonal {
            ETSSeasonal::None => {
                // Simple initialization: level = first value, trend = first difference
                self.initial_level = data[0];
                if self.config.trend != ETSTrend::None {
                    // Average of first few differences
                    let n_diff = data.len().min(5);
                    let mut sum = F::zero();
                    for i in 1..n_diff {
                        sum = sum + (data[i] - data[i - 1]);
                    }
                    self.initial_trend = sum
                        / F::from(n_diff - 1).ok_or_else(|| {
                            TimeSeriesError::NumericalInstability("Failed to convert".to_string())
                        })?;
                }
                self.initial_seasonal = Array1::zeros(0);
            }
            ETSSeasonal::Additive => {
                // Initialize using first complete seasons
                let n_seasons = (data.len() / period).min(3);
                let mut season_means = Vec::with_capacity(n_seasons);

                for s in 0..n_seasons {
                    let start = s * period;
                    let end = start + period;
                    let mut sum = F::zero();
                    for i in start..end.min(data.len()) {
                        sum = sum + data[i];
                    }
                    season_means.push(
                        sum / F::from(period).ok_or_else(|| {
                            TimeSeriesError::NumericalInstability(
                                "Failed to convert period".to_string(),
                            )
                        })?,
                    );
                }

                self.initial_level = season_means[0];

                if self.config.trend != ETSTrend::None && n_seasons >= 2 {
                    self.initial_trend = (season_means[1] - season_means[0])
                        / F::from(period).ok_or_else(|| {
                            TimeSeriesError::NumericalInstability(
                                "Failed to convert period".to_string(),
                            )
                        })?;
                }

                // Initialize seasonal indices
                let mut seasonal = Array1::zeros(period);
                for j in 0..period {
                    let mut sum = F::zero();
                    let mut count = 0;
                    for s in 0..n_seasons {
                        let idx = s * period + j;
                        if idx < data.len() {
                            sum = sum + data[idx] - season_means[s];
                            count += 1;
                        }
                    }
                    if count > 0 {
                        seasonal[j] = sum
                            / F::from(count).ok_or_else(|| {
                                TimeSeriesError::NumericalInstability(
                                    "Failed to convert count".to_string(),
                                )
                            })?;
                    }
                }
                self.initial_seasonal = seasonal;
            }
            ETSSeasonal::Multiplicative => {
                // Initialize using first complete seasons
                let n_seasons = (data.len() / period).min(3);
                let mut season_means = Vec::with_capacity(n_seasons);

                for s in 0..n_seasons {
                    let start = s * period;
                    let end = start + period;
                    let mut sum = F::zero();
                    for i in start..end.min(data.len()) {
                        sum = sum + data[i];
                    }
                    season_means.push(
                        sum / F::from(period).ok_or_else(|| {
                            TimeSeriesError::NumericalInstability(
                                "Failed to convert period".to_string(),
                            )
                        })?,
                    );
                }

                self.initial_level = season_means[0];

                if self.config.trend != ETSTrend::None && n_seasons >= 2 {
                    self.initial_trend = (season_means[1] - season_means[0])
                        / F::from(period).ok_or_else(|| {
                            TimeSeriesError::NumericalInstability(
                                "Failed to convert period".to_string(),
                            )
                        })?;
                }

                // Initialize seasonal indices as ratios
                let mut seasonal = Array1::from_elem(period, F::one());
                for j in 0..period {
                    let mut sum = F::zero();
                    let mut count = 0;
                    for s in 0..n_seasons {
                        let idx = s * period + j;
                        if idx < data.len() && season_means[s] > F::zero() {
                            sum = sum + data[idx] / season_means[s];
                            count += 1;
                        }
                    }
                    if count > 0 {
                        seasonal[j] = sum
                            / F::from(count).ok_or_else(|| {
                                TimeSeriesError::NumericalInstability(
                                    "Failed to convert count".to_string(),
                                )
                            })?;
                    }
                }
                self.initial_seasonal = seasonal;
            }
        }

        Ok(())
    }

    /// Optimize smoothing parameters via grid search + Nelder-Mead refinement
    fn optimize_parameters(&mut self, data: &Array1<F>) -> Result<()> {
        let mut best_sse = F::infinity();
        let mut best_alpha = F::from(0.3).ok_or_else(|| {
            TimeSeriesError::NumericalInstability("Conversion failed".to_string())
        })?;
        let mut best_beta = F::from(0.1).ok_or_else(|| {
            TimeSeriesError::NumericalInstability("Conversion failed".to_string())
        })?;
        let mut best_gamma = F::from(0.1).ok_or_else(|| {
            TimeSeriesError::NumericalInstability("Conversion failed".to_string())
        })?;
        let mut best_phi = F::from(0.98).ok_or_else(|| {
            TimeSeriesError::NumericalInstability("Conversion failed".to_string())
        })?;

        // Grid search over parameter space
        let alpha_grid: Vec<f64> = if let Some(a) = self.config.alpha {
            vec![a]
        } else {
            vec![0.05, 0.1, 0.2, 0.3, 0.5, 0.7, 0.9]
        };

        let beta_grid: Vec<f64> = if self.config.trend == ETSTrend::None {
            vec![0.0]
        } else if let Some(b) = self.config.beta {
            vec![b]
        } else {
            vec![0.01, 0.05, 0.1, 0.2, 0.3]
        };

        let gamma_grid: Vec<f64> = if self.config.seasonal == ETSSeasonal::None {
            vec![0.0]
        } else if let Some(g) = self.config.gamma {
            vec![g]
        } else {
            vec![0.01, 0.05, 0.1, 0.2, 0.3]
        };

        let phi_grid: Vec<f64> = match self.config.trend {
            ETSTrend::DampedAdditive | ETSTrend::DampedMultiplicative => {
                if let Some(p) = self.config.phi {
                    vec![p]
                } else {
                    vec![0.8, 0.9, 0.95, 0.98]
                }
            }
            _ => vec![1.0],
        };

        for &a in &alpha_grid {
            for &b in &beta_grid {
                for &g in &gamma_grid {
                    for &p in &phi_grid {
                        let a_f = F::from(a).ok_or_else(|| {
                            TimeSeriesError::NumericalInstability("Conversion failed".to_string())
                        })?;
                        let b_f = F::from(b).ok_or_else(|| {
                            TimeSeriesError::NumericalInstability("Conversion failed".to_string())
                        })?;
                        let g_f = F::from(g).ok_or_else(|| {
                            TimeSeriesError::NumericalInstability("Conversion failed".to_string())
                        })?;
                        let p_f = F::from(p).ok_or_else(|| {
                            TimeSeriesError::NumericalInstability("Conversion failed".to_string())
                        })?;

                        self.alpha = a_f;
                        self.beta = b_f;
                        self.gamma = g_f;
                        self.phi = p_f;

                        if let Ok((_, _, resid, _)) = self.evaluate(data) {
                            let sse = resid.iter().fold(F::zero(), |acc, &r| acc + r * r);
                            if sse < best_sse {
                                best_sse = sse;
                                best_alpha = a_f;
                                best_beta = b_f;
                                best_gamma = g_f;
                                best_phi = p_f;
                            }
                        }
                    }
                }
            }
        }

        // Refine with local search (simplified Nelder-Mead-like coordinate descent)
        self.alpha = best_alpha;
        self.beta = best_beta;
        self.gamma = best_gamma;
        self.phi = best_phi;

        let step = F::from(0.02).ok_or_else(|| {
            TimeSeriesError::NumericalInstability("Conversion failed".to_string())
        })?;
        let min_val = F::from(0.001).ok_or_else(|| {
            TimeSeriesError::NumericalInstability("Conversion failed".to_string())
        })?;
        let max_val = F::from(0.999).ok_or_else(|| {
            TimeSeriesError::NumericalInstability("Conversion failed".to_string())
        })?;

        for _ in 0..self.config.max_iter {
            let prev_sse = best_sse;

            // Refine alpha
            if self.config.alpha.is_none() {
                for direction in [-1.0_f64, 1.0_f64] {
                    let delta = F::from(direction).ok_or_else(|| {
                        TimeSeriesError::NumericalInstability("Conversion failed".to_string())
                    })? * step;
                    let candidate = self.alpha + delta;
                    if candidate > min_val && candidate < max_val {
                        let saved = self.alpha;
                        self.alpha = candidate;
                        if let Ok((_, _, resid, _)) = self.evaluate(data) {
                            let sse = resid.iter().fold(F::zero(), |acc, &r| acc + r * r);
                            if sse < best_sse {
                                best_sse = sse;
                                best_alpha = candidate;
                            } else {
                                self.alpha = saved;
                            }
                        } else {
                            self.alpha = saved;
                        }
                    }
                }
                self.alpha = best_alpha;
            }

            // Refine beta
            if self.config.trend != ETSTrend::None && self.config.beta.is_none() {
                for direction in [-1.0_f64, 1.0_f64] {
                    let delta = F::from(direction).ok_or_else(|| {
                        TimeSeriesError::NumericalInstability("Conversion failed".to_string())
                    })? * step;
                    let candidate = self.beta + delta;
                    if candidate > min_val && candidate < max_val {
                        let saved = self.beta;
                        self.beta = candidate;
                        if let Ok((_, _, resid, _)) = self.evaluate(data) {
                            let sse = resid.iter().fold(F::zero(), |acc, &r| acc + r * r);
                            if sse < best_sse {
                                best_sse = sse;
                                best_beta = candidate;
                            } else {
                                self.beta = saved;
                            }
                        } else {
                            self.beta = saved;
                        }
                    }
                }
                self.beta = best_beta;
            }

            // Refine gamma
            if self.config.seasonal != ETSSeasonal::None && self.config.gamma.is_none() {
                for direction in [-1.0_f64, 1.0_f64] {
                    let delta = F::from(direction).ok_or_else(|| {
                        TimeSeriesError::NumericalInstability("Conversion failed".to_string())
                    })? * step;
                    let candidate = self.gamma + delta;
                    if candidate > min_val && candidate < max_val {
                        let saved = self.gamma;
                        self.gamma = candidate;
                        if let Ok((_, _, resid, _)) = self.evaluate(data) {
                            let sse = resid.iter().fold(F::zero(), |acc, &r| acc + r * r);
                            if sse < best_sse {
                                best_sse = sse;
                                best_gamma = candidate;
                            } else {
                                self.gamma = saved;
                            }
                        } else {
                            self.gamma = saved;
                        }
                    }
                }
                self.gamma = best_gamma;
            }

            // Check convergence
            let tol = F::from(self.config.tolerance).ok_or_else(|| {
                TimeSeriesError::NumericalInstability("Conversion failed".to_string())
            })?;
            if (prev_sse - best_sse).abs() < tol {
                break;
            }
        }

        Ok(())
    }

    /// Evaluate the model on data with current parameters, returning (log_likelihood, fitted, residuals, state)
    fn evaluate(&self, data: &Array1<F>) -> Result<(F, Array1<F>, Array1<F>, ETSState<F>)> {
        let n = data.len();
        let period = self.config.period.unwrap_or(1);

        let mut level = Vec::with_capacity(n + 1);
        let mut trend_vec = Vec::with_capacity(n + 1);
        let mut seasonal_vec: Vec<F> = Vec::new();
        let mut fitted = Array1::zeros(n);
        let mut residuals = Array1::zeros(n);

        // Set initial states
        level.push(self.initial_level);

        if self.config.trend != ETSTrend::None {
            trend_vec.push(self.initial_trend);
        }

        if self.config.seasonal != ETSSeasonal::None {
            for j in 0..period {
                if j < self.initial_seasonal.len() {
                    seasonal_vec.push(self.initial_seasonal[j]);
                } else {
                    seasonal_vec.push(F::one());
                }
            }
        }

        let mut total_ll = F::zero();

        for t in 0..n {
            let l = *level.last().unwrap_or(&F::zero());
            let b = *trend_vec.last().unwrap_or(&F::zero());

            // Get seasonal component for this time step
            let s = if self.config.seasonal != ETSSeasonal::None {
                let s_idx = t % period;
                if s_idx < seasonal_vec.len() {
                    seasonal_vec[s_idx]
                } else {
                    F::one()
                }
            } else {
                F::zero()
            };

            // Compute one-step-ahead forecast
            let forecast = self.compute_point(l, b, s, 1)?;
            fitted[t] = forecast;

            let error = data[t] - forecast;
            residuals[t] = error;

            // Update states
            let (new_l, new_b, new_s) = self.update_states(l, b, s, data[t], error)?;

            level.push(new_l);
            if self.config.trend != ETSTrend::None {
                trend_vec.push(new_b);
            }
            if self.config.seasonal != ETSSeasonal::None {
                // Extend seasonal_vec by storing new seasonal value at end
                // For the circular buffer approach:
                let s_idx = t % period;
                if s_idx < seasonal_vec.len() {
                    seasonal_vec[s_idx] = new_s;
                }
            }

            // Add to log-likelihood (Gaussian errors)
            total_ll = total_ll - error * error;
        }

        let state = ETSState {
            level,
            trend: trend_vec,
            seasonal: seasonal_vec,
        };

        Ok((total_ll, fitted, residuals, state))
    }

    /// Compute the point forecast h steps ahead given state components
    fn compute_point(&self, level: F, trend: F, seasonal: F, h: usize) -> Result<F> {
        let h_f = F::from(h).ok_or_else(|| {
            TimeSeriesError::NumericalInstability("Failed to convert h".to_string())
        })?;

        let trend_component = match self.config.trend {
            ETSTrend::None => F::zero(),
            ETSTrend::Additive => h_f * trend,
            ETSTrend::Multiplicative => level * (trend.powf(h_f) - F::one()),
            ETSTrend::DampedAdditive => {
                // sum_{i=1}^{h} phi^i * b
                let mut phi_sum = F::zero();
                let mut phi_power = self.phi;
                for _ in 0..h {
                    phi_sum = phi_sum + phi_power;
                    phi_power = phi_power * self.phi;
                }
                phi_sum * trend
            }
            ETSTrend::DampedMultiplicative => {
                let mut phi_sum = F::zero();
                let mut phi_power = self.phi;
                for _ in 0..h {
                    phi_sum = phi_sum + phi_power;
                    phi_power = phi_power * self.phi;
                }
                level * (trend.powf(phi_sum) - F::one())
            }
        };

        let base = match self.config.trend {
            ETSTrend::Multiplicative | ETSTrend::DampedMultiplicative => level + trend_component,
            _ => level + trend_component,
        };

        let result = match self.config.seasonal {
            ETSSeasonal::None => base,
            ETSSeasonal::Additive => base + seasonal,
            ETSSeasonal::Multiplicative => base * seasonal,
        };

        Ok(result)
    }

    /// Update state components given observation and error
    fn update_states(
        &self,
        level: F,
        trend: F,
        seasonal: F,
        observation: F,
        _error: F,
    ) -> Result<(F, F, F)> {
        // New level
        let new_level = match (self.config.trend, self.config.seasonal) {
            (ETSTrend::None, ETSSeasonal::None) => {
                self.alpha * observation + (F::one() - self.alpha) * level
            }
            (ETSTrend::None, ETSSeasonal::Additive) => {
                self.alpha * (observation - seasonal) + (F::one() - self.alpha) * level
            }
            (ETSTrend::None, ETSSeasonal::Multiplicative) => {
                if seasonal > F::zero() {
                    self.alpha * (observation / seasonal) + (F::one() - self.alpha) * level
                } else {
                    level
                }
            }
            (ETSTrend::Additive, ETSSeasonal::None) => {
                self.alpha * observation + (F::one() - self.alpha) * (level + trend)
            }
            (ETSTrend::Additive, ETSSeasonal::Additive) => {
                self.alpha * (observation - seasonal) + (F::one() - self.alpha) * (level + trend)
            }
            (ETSTrend::Additive, ETSSeasonal::Multiplicative) => {
                if seasonal > F::zero() {
                    self.alpha * (observation / seasonal)
                        + (F::one() - self.alpha) * (level + trend)
                } else {
                    level + trend
                }
            }
            (ETSTrend::DampedAdditive, ETSSeasonal::None) => {
                self.alpha * observation + (F::one() - self.alpha) * (level + self.phi * trend)
            }
            (ETSTrend::DampedAdditive, ETSSeasonal::Additive) => {
                self.alpha * (observation - seasonal)
                    + (F::one() - self.alpha) * (level + self.phi * trend)
            }
            (ETSTrend::DampedAdditive, ETSSeasonal::Multiplicative) => {
                if seasonal > F::zero() {
                    self.alpha * (observation / seasonal)
                        + (F::one() - self.alpha) * (level + self.phi * trend)
                } else {
                    level + self.phi * trend
                }
            }
            (ETSTrend::Multiplicative, ETSSeasonal::None) => {
                self.alpha * observation + (F::one() - self.alpha) * level * trend
            }
            (ETSTrend::Multiplicative, ETSSeasonal::Additive) => {
                self.alpha * (observation - seasonal) + (F::one() - self.alpha) * level * trend
            }
            (ETSTrend::Multiplicative, ETSSeasonal::Multiplicative) => {
                if seasonal > F::zero() {
                    self.alpha * (observation / seasonal) + (F::one() - self.alpha) * level * trend
                } else {
                    level * trend
                }
            }
            (ETSTrend::DampedMultiplicative, ETSSeasonal::None) => {
                self.alpha * observation + (F::one() - self.alpha) * level * trend.powf(self.phi)
            }
            (ETSTrend::DampedMultiplicative, ETSSeasonal::Additive) => {
                self.alpha * (observation - seasonal)
                    + (F::one() - self.alpha) * level * trend.powf(self.phi)
            }
            (ETSTrend::DampedMultiplicative, ETSSeasonal::Multiplicative) => {
                if seasonal > F::zero() {
                    self.alpha * (observation / seasonal)
                        + (F::one() - self.alpha) * level * trend.powf(self.phi)
                } else {
                    level * trend.powf(self.phi)
                }
            }
        };

        // New trend
        let new_trend = match self.config.trend {
            ETSTrend::None => F::zero(),
            ETSTrend::Additive => self.beta * (new_level - level) + (F::one() - self.beta) * trend,
            ETSTrend::DampedAdditive => {
                self.beta * (new_level - level) + (F::one() - self.beta) * self.phi * trend
            }
            ETSTrend::Multiplicative => {
                if level > F::zero() {
                    self.beta * (new_level / level) + (F::one() - self.beta) * trend
                } else {
                    trend
                }
            }
            ETSTrend::DampedMultiplicative => {
                if level > F::zero() {
                    self.beta * (new_level / level) + (F::one() - self.beta) * trend.powf(self.phi)
                } else {
                    trend.powf(self.phi)
                }
            }
        };

        // New seasonal
        let new_seasonal = match self.config.seasonal {
            ETSSeasonal::None => F::zero(),
            ETSSeasonal::Additive => {
                self.gamma * (observation - new_level) + (F::one() - self.gamma) * seasonal
            }
            ETSSeasonal::Multiplicative => {
                if new_level > F::zero() {
                    self.gamma * (observation / new_level) + (F::one() - self.gamma) * seasonal
                } else {
                    seasonal
                }
            }
        };

        Ok((new_level, new_trend, new_seasonal))
    }

    /// Forecast future values
    ///
    /// # Arguments
    /// * `steps` - Number of steps to forecast
    ///
    /// # Returns
    /// Array of point forecasts
    pub fn forecast(&self, steps: usize) -> Result<Array1<F>> {
        if !self.is_fitted {
            return Err(TimeSeriesError::ModelNotFitted(
                "Model must be fitted before forecasting".to_string(),
            ));
        }

        if steps == 0 {
            return Ok(Array1::zeros(0));
        }

        let period = self.config.period.unwrap_or(1);
        let mut forecasts = Array1::zeros(steps);

        for h in 0..steps {
            let s = if self.config.seasonal != ETSSeasonal::None && !self.final_seasonal.is_empty()
            {
                let s_idx = h % period;
                if s_idx < self.final_seasonal.len() {
                    self.final_seasonal[s_idx]
                } else {
                    F::one()
                }
            } else {
                F::zero()
            };

            forecasts[h] = self.compute_point(self.final_level, self.final_trend, s, h + 1)?;
        }

        Ok(forecasts)
    }

    /// Forecast with confidence intervals
    ///
    /// # Arguments
    /// * `steps` - Number of steps to forecast
    /// * `confidence_level` - Confidence level (e.g., 0.95 for 95% intervals)
    ///
    /// # Returns
    /// Tuple of (point_forecast, lower_ci, upper_ci)
    pub fn forecast_with_confidence(
        &self,
        steps: usize,
        confidence_level: f64,
    ) -> Result<(Array1<F>, Array1<F>, Array1<F>)> {
        if confidence_level <= 0.0 || confidence_level >= 1.0 {
            return Err(TimeSeriesError::InvalidParameter {
                name: "confidence_level".to_string(),
                message: "Must be between 0 and 1".to_string(),
            });
        }

        let point_forecast = self.forecast(steps)?;

        let alpha_half = (1.0 - confidence_level) / 2.0;
        let z = crate::arima_models::quantile_normal(1.0 - alpha_half);
        let z_f = F::from(z).ok_or_else(|| {
            TimeSeriesError::NumericalInstability("Failed to convert z-score".to_string())
        })?;

        let mut lower_ci = Array1::zeros(steps);
        let mut upper_ci = Array1::zeros(steps);

        for h in 0..steps {
            let h_f = F::from(h + 1).ok_or_else(|| {
                TimeSeriesError::NumericalInstability("Failed to convert h".to_string())
            })?;
            let se = self.sigma2.sqrt() * h_f.sqrt();
            lower_ci[h] = point_forecast[h] - z_f * se;
            upper_ci[h] = point_forecast[h] + z_f * se;
        }

        Ok((point_forecast, lower_ci, upper_ci))
    }

    /// Get fitted values
    pub fn fitted_values(&self) -> Result<&Array1<F>> {
        if !self.is_fitted {
            return Err(TimeSeriesError::ModelNotFitted(
                "Model must be fitted first".to_string(),
            ));
        }
        Ok(&self.fitted_values)
    }

    /// Get residuals
    pub fn residuals(&self) -> Result<&Array1<F>> {
        if !self.is_fitted {
            return Err(TimeSeriesError::ModelNotFitted(
                "Model must be fitted first".to_string(),
            ));
        }
        Ok(&self.residuals)
    }

    /// Calculate AIC
    pub fn aic(&self) -> F {
        let k = F::from(self.n_params).unwrap_or(F::one());
        let two = F::from(2.0).unwrap_or(F::one());
        two * k - two * self.log_likelihood
    }

    /// Calculate BIC
    pub fn bic(&self) -> F {
        let k = F::from(self.n_params).unwrap_or(F::one());
        let n = F::from(self.n_obs).unwrap_or(F::one());
        let two = F::from(2.0).unwrap_or(F::one());
        k * n.ln() - two * self.log_likelihood
    }

    /// Calculate AICc (corrected AIC for small samples)
    pub fn aicc(&self) -> F {
        let k = F::from(self.n_params).unwrap_or(F::one());
        let n = F::from(self.n_obs).unwrap_or(F::one());
        let two = F::from(2.0).unwrap_or(F::one());
        self.aic() + two * k * (k + F::one()) / (n - k - F::one())
    }

    /// Get the model name in ETS notation (e.g., "ETS(A,A,N)")
    pub fn model_name(&self) -> String {
        let error = match self.config.error {
            ETSError::Additive => "A",
            ETSError::Multiplicative => "M",
        };
        let trend = match self.config.trend {
            ETSTrend::None => "N",
            ETSTrend::Additive => "A",
            ETSTrend::Multiplicative => "M",
            ETSTrend::DampedAdditive => "Ad",
            ETSTrend::DampedMultiplicative => "Md",
        };
        let seasonal = match self.config.seasonal {
            ETSSeasonal::None => "N",
            ETSSeasonal::Additive => "A",
            ETSSeasonal::Multiplicative => "M",
        };
        format!("ETS({},{},{})", error, trend, seasonal)
    }
}

/// Convenience function to fit an ETS model
///
/// # Arguments
/// * `data` - Time series data
/// * `trend` - Trend type specification ("none", "additive", "multiplicative", "damped_additive")
/// * `seasonal` - Seasonal type specification ("none", "additive", "multiplicative")
/// * `period` - Seasonal period (required if seasonal != "none")
///
/// # Returns
/// A fitted ETSModel ready for forecasting
///
/// # Example
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_series::ets::ets;
///
/// let data = array![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
/// let model = ets(&data, "additive", "none", None).expect("Failed to fit ETS");
/// let forecast = model.forecast(5).expect("Failed to forecast");
/// assert_eq!(forecast.len(), 5);
/// ```
pub fn ets<F>(
    data: &Array1<F>,
    trend: &str,
    seasonal: &str,
    period: Option<usize>,
) -> Result<ETSModel<F>>
where
    F: Float + FromPrimitive + Debug + Display + ScalarOperand,
{
    let trend_type = match trend.to_lowercase().as_str() {
        "none" | "n" => ETSTrend::None,
        "additive" | "add" | "a" => ETSTrend::Additive,
        "multiplicative" | "mul" | "m" => ETSTrend::Multiplicative,
        "damped_additive" | "damped_add" | "da" | "ad" => ETSTrend::DampedAdditive,
        "damped_multiplicative" | "damped_mul" | "dm" | "md" => ETSTrend::DampedMultiplicative,
        _ => {
            return Err(TimeSeriesError::InvalidParameter {
                name: "trend".to_string(),
                message: format!(
                    "Unknown trend type '{}'. Use: none, additive, multiplicative, damped_additive",
                    trend
                ),
            })
        }
    };

    let seasonal_type = match seasonal.to_lowercase().as_str() {
        "none" | "n" => ETSSeasonal::None,
        "additive" | "add" | "a" => ETSSeasonal::Additive,
        "multiplicative" | "mul" | "m" => ETSSeasonal::Multiplicative,
        _ => {
            return Err(TimeSeriesError::InvalidParameter {
                name: "seasonal".to_string(),
                message: format!(
                    "Unknown seasonal type '{}'. Use: none, additive, multiplicative",
                    seasonal
                ),
            })
        }
    };

    let config = ETSConfig {
        error: ETSError::Additive,
        trend: trend_type,
        seasonal: seasonal_type,
        period,
        ..Default::default()
    };

    let mut model = ETSModel::new(config)?;
    model.fit(data)?;
    Ok(model)
}

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

    #[test]
    fn test_ses_fit_and_forecast() {
        let data = array![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
        let model = ets(&data, "none", "none", None).expect("Failed to fit SES");

        assert!(model.is_fitted);
        assert_eq!(model.model_name(), "ETS(A,N,N)");

        let forecast = model.forecast(5).expect("Failed to forecast");
        assert_eq!(forecast.len(), 5);

        // SES forecast should be a constant (all same value)
        for i in 1..5 {
            assert!(
                (forecast[i] - forecast[0]).abs() < 1e-10,
                "SES forecasts should be constant"
            );
        }
    }

    #[test]
    fn test_holt_double_exponential() {
        // Linear trend data
        let data = array![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
        let model = ets(&data, "additive", "none", None).expect("Failed to fit Holt");

        assert!(model.is_fitted);
        assert_eq!(model.model_name(), "ETS(A,A,N)");

        let forecast = model.forecast(3).expect("Failed to forecast");
        assert_eq!(forecast.len(), 3);

        // Forecasts should be increasing for upward-trending data
        for i in 1..3 {
            assert!(
                forecast[i] > forecast[i - 1],
                "Holt forecasts should be increasing for upward trend"
            );
        }
    }

    #[test]
    fn test_holt_winters_additive() {
        // Seasonal data with period 4
        let data = array![
            10.0, 15.0, 12.0, 8.0, 11.0, 16.0, 13.0, 9.0, 12.0, 17.0, 14.0, 10.0, 13.0, 18.0, 15.0,
            11.0
        ];
        let model =
            ets(&data, "additive", "additive", Some(4)).expect("Failed to fit Holt-Winters");

        assert!(model.is_fitted);
        assert_eq!(model.model_name(), "ETS(A,A,A)");

        let forecast = model.forecast(4).expect("Failed to forecast");
        assert_eq!(forecast.len(), 4);

        // Forecasts should be finite
        for i in 0..4 {
            assert!(
                forecast[i].is_finite(),
                "Forecast at step {} should be finite",
                i
            );
        }
    }

    #[test]
    fn test_holt_winters_multiplicative() {
        // Positive seasonal data with multiplicative pattern
        let data = array![
            10.0, 15.0, 12.0, 8.0, 11.0, 16.0, 13.0, 9.0, 12.0, 17.0, 14.0, 10.0, 13.0, 18.0, 15.0,
            11.0
        ];
        let model = ets(&data, "additive", "multiplicative", Some(4))
            .expect("Failed to fit multiplicative Holt-Winters");

        assert!(model.is_fitted);
        assert_eq!(model.model_name(), "ETS(A,A,M)");

        let forecast = model.forecast(4).expect("Failed to forecast");
        assert_eq!(forecast.len(), 4);

        for i in 0..4 {
            assert!(
                forecast[i].is_finite(),
                "Forecast at step {} should be finite",
                i
            );
        }
    }

    #[test]
    fn test_damped_trend() {
        let data = array![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
        let model =
            ets(&data, "damped_additive", "none", None).expect("Failed to fit damped trend");

        assert!(model.is_fitted);
        assert_eq!(model.model_name(), "ETS(A,Ad,N)");

        let forecast = model.forecast(10).expect("Failed to forecast");
        assert_eq!(forecast.len(), 10);

        // Damped trend should level off — later differences should be smaller
        if forecast.len() >= 3 {
            let diff_early = forecast[1] - forecast[0];
            let diff_late = forecast[forecast.len() - 1] - forecast[forecast.len() - 2];
            assert!(
                diff_late.abs() <= diff_early.abs() + 1e-6,
                "Damped trend increments should decrease over time"
            );
        }
    }

    #[test]
    fn test_confidence_intervals() {
        let data = array![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
        let model = ets(&data, "additive", "none", None).expect("Failed to fit ETS");

        let (point, lower, upper) = model
            .forecast_with_confidence(5, 0.95)
            .expect("Failed to forecast with CI");

        assert_eq!(point.len(), 5);
        assert_eq!(lower.len(), 5);
        assert_eq!(upper.len(), 5);

        for i in 0..5 {
            assert!(lower[i] <= point[i], "Lower CI should be <= point forecast");
            assert!(upper[i] >= point[i], "Upper CI should be >= point forecast");
        }

        // Intervals should widen over time
        for i in 1..5 {
            let width_prev = upper[i - 1] - lower[i - 1];
            let width_curr = upper[i] - lower[i];
            assert!(
                width_curr >= width_prev - 1e-10,
                "CI width should not decrease over time"
            );
        }
    }

    #[test]
    fn test_information_criteria() {
        let data = array![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
        let model = ets(&data, "none", "none", None).expect("Failed to fit SES");

        let aic = model.aic();
        let bic = model.bic();
        let aicc = model.aicc();

        assert!(aic.is_finite(), "AIC should be finite");
        assert!(bic.is_finite(), "BIC should be finite");
        assert!(aicc.is_finite(), "AICc should be finite");
    }

    #[test]
    fn test_fitted_and_residuals() {
        let data = array![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
        let model = ets(&data, "additive", "none", None).expect("Failed to fit ETS");

        let fitted = model.fitted_values().expect("Failed to get fitted values");
        let resid = model.residuals().expect("Failed to get residuals");

        assert_eq!(fitted.len(), data.len());
        assert_eq!(resid.len(), data.len());

        // Fitted + Residual should equal actual
        for i in 0..data.len() {
            let reconstructed = fitted[i] + resid[i];
            assert!(
                (reconstructed - data[i]).abs() < 1e-6,
                "fitted + residual should equal actual at index {}",
                i
            );
        }
    }

    #[test]
    fn test_multiplicative_requires_positive() {
        let data = array![1.0, -2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
        let result = ets(&data, "multiplicative", "none", None);
        assert!(
            result.is_err(),
            "Multiplicative model should fail with negative data"
        );
    }

    #[test]
    fn test_seasonal_requires_period() {
        let config = ETSConfig {
            seasonal: ETSSeasonal::Additive,
            period: None,
            ..Default::default()
        };
        let result = ETSModel::<f64>::new(config);
        assert!(result.is_err(), "Seasonal model without period should fail");
    }

    #[test]
    fn test_model_not_fitted_errors() {
        let config = ETSConfig::default();
        let model = ETSModel::<f64>::new(config).expect("Failed to create model");

        assert!(model.forecast(5).is_err());
        assert!(model.fitted_values().is_err());
        assert!(model.residuals().is_err());
    }

    #[test]
    fn test_ets_convenience_invalid_trend() {
        let data = array![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
        let result = ets(&data, "invalid_trend_type", "none", None);
        assert!(result.is_err());
    }

    #[test]
    fn test_ets_model_name_formats() {
        let ses = ETSConfig::default();
        let model = ETSModel::<f64>::new(ses).expect("Failed to create");
        assert_eq!(model.model_name(), "ETS(A,N,N)");

        let holt = ETSConfig {
            trend: ETSTrend::Additive,
            ..Default::default()
        };
        let model = ETSModel::<f64>::new(holt).expect("Failed to create");
        assert_eq!(model.model_name(), "ETS(A,A,N)");

        let hw = ETSConfig {
            trend: ETSTrend::Additive,
            seasonal: ETSSeasonal::Additive,
            period: Some(4),
            ..Default::default()
        };
        let model = ETSModel::<f64>::new(hw).expect("Failed to create");
        assert_eq!(model.model_name(), "ETS(A,A,A)");
    }
}