scirs2-series 0.4.0

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
//! DLinear and NLinear: Simple Linear Models for Time Series Forecasting
//!
//! Implementation of *"Are Transformers Effective for Time Series Forecasting?"*
//! (Zeng et al., 2022). Despite their simplicity, these linear models often
//! outperform complex Transformer-based methods on standard benchmarks.
//!
//! ## DLinear
//! Decomposes the series into trend and seasonal components via a moving average,
//! then applies two separate linear layers (one per component).
//!
//! ```text
//! trend    = moving_average(input, kernel_size)
//! seasonal = input - trend
//! forecast = Linear_trend(trend[-seq_len..]) + Linear_seasonal(seasonal[-seq_len..])
//! ```
//!
//! ## NLinear
//! Subtracts the last observed value (simple normalization), applies a linear
//! mapping, then adds the last value back.
//!
//! ```text
//! last       = input[-1]    (last time step, broadcast across channels)
//! normalized = input - last
//! forecast   = Linear(normalized) + last  (broadcast)
//! ```
//!
//! Both models support:
//! - **Shared weights** (`individual=false`): one linear layer shared across all channels
//! - **Per-channel weights** (`individual=true`): separate linear layer per channel

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

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

// ---------------------------------------------------------------------------
// DLinear Configuration
// ---------------------------------------------------------------------------

/// Configuration for the DLinear model.
#[derive(Debug, Clone)]
pub struct DLinearConfig {
    /// Input sequence length.
    pub seq_len: usize,
    /// Prediction horizon.
    pub pred_len: usize,
    /// Number of input variates (channels).
    pub n_channels: usize,
    /// Moving average kernel size for trend extraction (must be odd for symmetric MA).
    pub kernel_size: usize,
    /// If true, use per-channel linear weights; otherwise share weights across channels.
    pub individual: bool,
    /// Random seed for weight initialization.
    pub seed: u32,
}

impl Default for DLinearConfig {
    fn default() -> Self {
        Self {
            seq_len: 96,
            pred_len: 24,
            n_channels: 7,
            kernel_size: 25,
            individual: false,
            seed: 0,
        }
    }
}

// ---------------------------------------------------------------------------
// NLinear Configuration
// ---------------------------------------------------------------------------

/// Configuration for the NLinear model.
#[derive(Debug, Clone)]
pub struct NLinearConfig {
    /// Input sequence length.
    pub seq_len: usize,
    /// Prediction horizon.
    pub pred_len: usize,
    /// Number of input variates (channels).
    pub n_channels: usize,
    /// If true, use per-channel linear weights; otherwise share weights across channels.
    pub individual: bool,
}

impl Default for NLinearConfig {
    fn default() -> Self {
        Self {
            seq_len: 96,
            pred_len: 24,
            n_channels: 7,
            individual: false,
        }
    }
}

// ---------------------------------------------------------------------------
// Moving average helper
// ---------------------------------------------------------------------------

/// Compute symmetric (causal) moving average of a 1-D signal.
///
/// Uses a centred moving average of the given kernel size (must be >= 1).
/// Boundary effects are handled by clamping the window to valid indices,
/// equivalent to "same" padding with edge repetition.
fn moving_average<F: Float + FromPrimitive>(signal: &Array1<F>, kernel_size: usize) -> Array1<F> {
    let n = signal.len();
    if kernel_size <= 1 || n == 0 {
        return signal.clone();
    }
    let half = kernel_size / 2;
    let mut out = Array1::zeros(n);
    let ks_f = F::from(kernel_size as f64).unwrap_or_else(|| F::one());
    for i in 0..n {
        let start = if i >= half { i - half } else { 0 };
        let end = (i + half + 1).min(n);
        let window_len = end - start;
        let mut sum = F::zero();
        for j in start..end {
            sum = sum + signal[j];
        }
        // Normalize by actual kernel_size to stay consistent with centred MA
        // (divide by kernel_size, not actual window length, to avoid boundary scaling)
        let actual_ks = F::from(window_len as f64).unwrap_or_else(|| ks_f);
        out[i] = sum / actual_ks;
    }
    out
}

// ---------------------------------------------------------------------------
// DLinear Model
// ---------------------------------------------------------------------------

/// DLinear model: decomposition-based linear forecasting.
///
/// The model decomposes the input into trend and seasonal components,
/// then uses two separate linear projections to forecast each component.
///
/// # Input/Output
/// - Input: `[seq_len, n_channels]`
/// - Output: `[pred_len, n_channels]`
#[derive(Debug)]
pub struct DLinearModel {
    config: DLinearConfig,
    /// Shared trend linear weight: (pred_len, seq_len)
    w_trend_shared: Option<Array2<f64>>,
    b_trend_shared: Option<Array1<f64>>,
    /// Shared seasonal linear weight: (pred_len, seq_len)
    w_seasonal_shared: Option<Array2<f64>>,
    b_seasonal_shared: Option<Array1<f64>>,
    /// Per-channel trend weights: Vec<(pred_len, seq_len)>  — one per channel
    w_trend_individual: Option<Vec<Array2<f64>>>,
    b_trend_individual: Option<Vec<Array1<f64>>>,
    /// Per-channel seasonal weights
    w_seasonal_individual: Option<Vec<Array2<f64>>>,
    b_seasonal_individual: Option<Vec<Array1<f64>>>,
}

impl DLinearModel {
    /// Create a new DLinear model from configuration.
    ///
    /// # Errors
    ///
    /// Returns error if configuration parameters are invalid.
    pub fn new(config: DLinearConfig) -> Result<Self> {
        if config.seq_len == 0 {
            return Err(TimeSeriesError::InvalidInput(
                "seq_len must be positive".to_string(),
            ));
        }
        if config.pred_len == 0 {
            return Err(TimeSeriesError::InvalidInput(
                "pred_len must be positive".to_string(),
            ));
        }
        if config.n_channels == 0 {
            return Err(TimeSeriesError::InvalidInput(
                "n_channels must be positive".to_string(),
            ));
        }
        if config.kernel_size == 0 {
            return Err(TimeSeriesError::InvalidInput(
                "kernel_size must be positive".to_string(),
            ));
        }

        let seed = config.seed;
        let sl = config.seq_len;
        let pl = config.pred_len;
        let nc = config.n_channels;

        if config.individual {
            let mut wt = Vec::with_capacity(nc);
            let mut bt = Vec::with_capacity(nc);
            let mut ws = Vec::with_capacity(nc);
            let mut bs = Vec::with_capacity(nc);
            for ch in 0..nc {
                let ch_seed = seed.wrapping_add(ch as u32 * 1000);
                wt.push(nn_utils::xavier_matrix(pl, sl, ch_seed));
                bt.push(nn_utils::zero_bias(pl));
                ws.push(nn_utils::xavier_matrix(pl, sl, ch_seed.wrapping_add(500)));
                bs.push(nn_utils::zero_bias(pl));
            }
            Ok(Self {
                config,
                w_trend_shared: None,
                b_trend_shared: None,
                w_seasonal_shared: None,
                b_seasonal_shared: None,
                w_trend_individual: Some(wt),
                b_trend_individual: Some(bt),
                w_seasonal_individual: Some(ws),
                b_seasonal_individual: Some(bs),
            })
        } else {
            Ok(Self {
                config,
                w_trend_shared: Some(nn_utils::xavier_matrix(pl, sl, seed)),
                b_trend_shared: Some(nn_utils::zero_bias(pl)),
                w_seasonal_shared: Some(nn_utils::xavier_matrix(pl, sl, seed.wrapping_add(500))),
                b_seasonal_shared: Some(nn_utils::zero_bias(pl)),
                w_trend_individual: None,
                b_trend_individual: None,
                w_seasonal_individual: None,
                b_seasonal_individual: None,
            })
        }
    }

    /// Decompose a channel's time series into (trend, seasonal) components.
    pub fn decompose(&self, channel: &Array1<f64>) -> (Array1<f64>, Array1<f64>) {
        let trend = moving_average(channel, self.config.kernel_size);
        let seasonal = channel - &trend;
        (trend, seasonal)
    }

    /// Forecast future values for a multivariate input.
    ///
    /// # Arguments
    /// * `input` - Input array of shape `[seq_len, n_channels]`
    ///
    /// # Returns
    /// Forecast array of shape `[pred_len, n_channels]`
    ///
    /// # Errors
    ///
    /// Returns error if input shape doesn't match configuration.
    pub fn forecast(&self, input: &Array2<f64>) -> Result<Array2<f64>> {
        let (seq_len, n_ch) = input.dim();
        if seq_len != self.config.seq_len {
            return Err(TimeSeriesError::DimensionMismatch {
                expected: self.config.seq_len,
                actual: seq_len,
            });
        }
        if n_ch != self.config.n_channels {
            return Err(TimeSeriesError::DimensionMismatch {
                expected: self.config.n_channels,
                actual: n_ch,
            });
        }

        let mut output = Array2::zeros((self.config.pred_len, n_ch));

        for ch in 0..n_ch {
            // Extract channel
            let channel: Array1<f64> = (0..seq_len).map(|t| input[[t, ch]]).collect();

            // Decompose into trend and seasonal
            let (trend, seasonal) = self.decompose(&channel);

            if self.config.individual {
                // Per-channel weights
                let wt = self.w_trend_individual.as_ref()
                    .expect("individual weights must be set");
                let bt = self.b_trend_individual.as_ref()
                    .expect("individual bias must be set");
                let ws = self.w_seasonal_individual.as_ref()
                    .expect("individual weights must be set");
                let bse = self.b_seasonal_individual.as_ref()
                    .expect("individual bias must be set");

                let trend_pred = nn_utils::dense_forward_vec(&trend, &wt[ch], &bt[ch]);
                let seasonal_pred = nn_utils::dense_forward_vec(&seasonal, &ws[ch], &bse[ch]);

                for t in 0..self.config.pred_len {
                    output[[t, ch]] = trend_pred[t] + seasonal_pred[t];
                }
            } else {
                // Shared weights
                let wt = self.w_trend_shared.as_ref()
                    .expect("shared trend weights must be set");
                let bt = self.b_trend_shared.as_ref()
                    .expect("shared trend bias must be set");
                let ws = self.w_seasonal_shared.as_ref()
                    .expect("shared seasonal weights must be set");
                let bse = self.b_seasonal_shared.as_ref()
                    .expect("shared seasonal bias must be set");

                let trend_pred = nn_utils::dense_forward_vec(&trend, wt, bt);
                let seasonal_pred = nn_utils::dense_forward_vec(&seasonal, ws, bse);

                for t in 0..self.config.pred_len {
                    output[[t, ch]] = trend_pred[t] + seasonal_pred[t];
                }
            }
        }

        Ok(output)
    }

    /// Get a reference to the configuration.
    pub fn config(&self) -> &DLinearConfig {
        &self.config
    }
}

// ---------------------------------------------------------------------------
// NLinear Model
// ---------------------------------------------------------------------------

/// NLinear model: last-value normalization + linear forecasting.
///
/// Subtracts the last observed value (per channel), applies a linear layer,
/// then adds the last value back — acting as a simple distribution-shift
/// correction.
///
/// # Input/Output
/// - Input: `[seq_len, n_channels]`
/// - Output: `[pred_len, n_channels]`
#[derive(Debug)]
pub struct NLinearModel {
    config: NLinearConfig,
    /// Shared linear weight: (pred_len, seq_len)
    w_shared: Option<Array2<f64>>,
    b_shared: Option<Array1<f64>>,
    /// Per-channel linear weights
    w_individual: Option<Vec<Array2<f64>>>,
    b_individual: Option<Vec<Array1<f64>>>,
}

impl NLinearModel {
    /// Create a new NLinear model from configuration.
    ///
    /// # Errors
    ///
    /// Returns error if configuration parameters are invalid.
    pub fn new(config: NLinearConfig) -> Result<Self> {
        if config.seq_len == 0 {
            return Err(TimeSeriesError::InvalidInput(
                "seq_len must be positive".to_string(),
            ));
        }
        if config.pred_len == 0 {
            return Err(TimeSeriesError::InvalidInput(
                "pred_len must be positive".to_string(),
            ));
        }
        if config.n_channels == 0 {
            return Err(TimeSeriesError::InvalidInput(
                "n_channels must be positive".to_string(),
            ));
        }

        let sl = config.seq_len;
        let pl = config.pred_len;
        let nc = config.n_channels;

        // NLinear: weights initialized near identity/zero mapping
        // seed=0 means zero-like initialization; we use xavier for the shared case
        if config.individual {
            let mut w_ind = Vec::with_capacity(nc);
            let mut b_ind = Vec::with_capacity(nc);
            for ch in 0..nc {
                let seed = ch as u32 * 1000 + 1;
                w_ind.push(nn_utils::xavier_matrix(pl, sl, seed));
                b_ind.push(nn_utils::zero_bias(pl));
            }
            Ok(Self {
                config,
                w_shared: None,
                b_shared: None,
                w_individual: Some(w_ind),
                b_individual: Some(b_ind),
            })
        } else {
            Ok(Self {
                config,
                w_shared: Some(nn_utils::xavier_matrix(pl, sl, 42)),
                b_shared: Some(nn_utils::zero_bias(pl)),
                w_individual: None,
                b_individual: None,
            })
        }
    }

    /// Forecast future values for a multivariate input.
    ///
    /// # Arguments
    /// * `input` - Input array of shape `[seq_len, n_channels]`
    ///
    /// # Returns
    /// Forecast array of shape `[pred_len, n_channels]`
    ///
    /// # Errors
    ///
    /// Returns error if input shape doesn't match configuration.
    pub fn forecast(&self, input: &Array2<f64>) -> Result<Array2<f64>> {
        let (seq_len, n_ch) = input.dim();
        if seq_len != self.config.seq_len {
            return Err(TimeSeriesError::DimensionMismatch {
                expected: self.config.seq_len,
                actual: seq_len,
            });
        }
        if n_ch != self.config.n_channels {
            return Err(TimeSeriesError::DimensionMismatch {
                expected: self.config.n_channels,
                actual: n_ch,
            });
        }

        let mut output = Array2::zeros((self.config.pred_len, n_ch));

        for ch in 0..n_ch {
            // Extract channel as 1-D array
            let channel: Array1<f64> = (0..seq_len).map(|t| input[[t, ch]]).collect();

            // Last-value normalization: subtract input[-1]
            let last_val = channel[seq_len - 1];
            let normalized = channel.mapv(|v| v - last_val);

            // Linear projection
            let pred = if self.config.individual {
                let w = self.w_individual.as_ref()
                    .expect("individual weights must be set");
                let b = self.b_individual.as_ref()
                    .expect("individual bias must be set");
                nn_utils::dense_forward_vec(&normalized, &w[ch], &b[ch])
            } else {
                let w = self.w_shared.as_ref()
                    .expect("shared weights must be set");
                let b = self.b_shared.as_ref()
                    .expect("shared bias must be set");
                nn_utils::dense_forward_vec(&normalized, w, b)
            };

            // Add last value back (de-normalize)
            for t in 0..self.config.pred_len {
                output[[t, ch]] = pred[t] + last_val;
            }
        }

        Ok(output)
    }

    /// Get a reference to the configuration.
    pub fn config(&self) -> &NLinearConfig {
        &self.config
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn make_input(seq_len: usize, n_channels: usize) -> Array2<f64> {
        let mut arr = Array2::zeros((seq_len, n_channels));
        for t in 0..seq_len {
            for c in 0..n_channels {
                arr[[t, c]] = t as f64 * 0.1 + c as f64;
            }
        }
        arr
    }

    // DLinear tests

    #[test]
    fn test_dlinear_default_config() {
        let cfg = DLinearConfig::default();
        assert_eq!(cfg.seq_len, 96);
        assert_eq!(cfg.pred_len, 24);
        assert_eq!(cfg.n_channels, 7);
        assert_eq!(cfg.kernel_size, 25);
        assert!(!cfg.individual);
        assert_eq!(cfg.seed, 0);
    }

    #[test]
    fn test_dlinear_model_creation() {
        let model = DLinearModel::new(DLinearConfig::default());
        assert!(model.is_ok());
    }

    #[test]
    fn test_dlinear_forecast_shape_shared() {
        let cfg = DLinearConfig {
            seq_len: 48,
            pred_len: 12,
            n_channels: 4,
            kernel_size: 5,
            individual: false,
            seed: 42,
        };
        let model = DLinearModel::new(cfg).expect("model creation failed");
        let input = make_input(48, 4);
        let output = model.forecast(&input).expect("forecast failed");
        assert_eq!(output.dim(), (12, 4));
    }

    #[test]
    fn test_dlinear_forecast_shape_individual() {
        let cfg = DLinearConfig {
            seq_len: 48,
            pred_len: 12,
            n_channels: 4,
            kernel_size: 5,
            individual: true,
            seed: 42,
        };
        let model = DLinearModel::new(cfg).expect("model creation failed");
        let input = make_input(48, 4);
        let output = model.forecast(&input).expect("forecast failed");
        assert_eq!(output.dim(), (12, 4));
    }

    #[test]
    fn test_dlinear_decomposition_correctness() {
        let cfg = DLinearConfig {
            seq_len: 20,
            pred_len: 4,
            n_channels: 1,
            kernel_size: 3,
            individual: false,
            seed: 0,
        };
        let model = DLinearModel::new(cfg).expect("model creation failed");

        // Create a signal where trend = constant = 5.0
        // so seasonal should be ~ 0
        let constant = Array1::from_elem(20, 5.0_f64);
        let (trend, seasonal) = model.decompose(&constant);

        // For a constant signal, moving average = constant
        for t in 0..20 {
            assert!(
                (trend[t] - 5.0).abs() < 1e-10,
                "trend[{t}] = {} != 5.0",
                trend[t]
            );
            assert!(
                seasonal[t].abs() < 1e-10,
                "seasonal[{t}] = {} != 0.0",
                seasonal[t]
            );
        }
    }

    #[test]
    fn test_dlinear_wrong_seq_len_error() {
        let cfg = DLinearConfig {
            seq_len: 32,
            pred_len: 8,
            n_channels: 2,
            kernel_size: 3,
            individual: false,
            seed: 0,
        };
        let model = DLinearModel::new(cfg).expect("model creation failed");
        let bad_input = make_input(24, 2); // wrong seq_len
        assert!(model.forecast(&bad_input).is_err());
    }

    #[test]
    fn test_dlinear_output_finite() {
        let cfg = DLinearConfig {
            seq_len: 32,
            pred_len: 8,
            n_channels: 3,
            kernel_size: 7,
            individual: false,
            seed: 1,
        };
        let model = DLinearModel::new(cfg).expect("model creation failed");
        let input = make_input(32, 3);
        let output = model.forecast(&input).expect("forecast failed");
        for t in 0..8 {
            for c in 0..3 {
                assert!(output[[t, c]].is_finite(), "non-finite at [{t},{c}]");
            }
        }
    }

    // NLinear tests

    #[test]
    fn test_nlinear_default_config() {
        let cfg = NLinearConfig::default();
        assert_eq!(cfg.seq_len, 96);
        assert_eq!(cfg.pred_len, 24);
        assert_eq!(cfg.n_channels, 7);
        assert!(!cfg.individual);
    }

    #[test]
    fn test_nlinear_model_creation() {
        let model = NLinearModel::new(NLinearConfig::default());
        assert!(model.is_ok());
    }

    #[test]
    fn test_nlinear_forecast_shape_shared() {
        let cfg = NLinearConfig {
            seq_len: 48,
            pred_len: 12,
            n_channels: 4,
            individual: false,
        };
        let model = NLinearModel::new(cfg).expect("model creation failed");
        let input = make_input(48, 4);
        let output = model.forecast(&input).expect("forecast failed");
        assert_eq!(output.dim(), (12, 4));
    }

    #[test]
    fn test_nlinear_forecast_shape_individual() {
        let cfg = NLinearConfig {
            seq_len: 48,
            pred_len: 12,
            n_channels: 4,
            individual: true,
        };
        let model = NLinearModel::new(cfg).expect("model creation failed");
        let input = make_input(48, 4);
        let output = model.forecast(&input).expect("forecast failed");
        assert_eq!(output.dim(), (12, 4));
    }

    #[test]
    fn test_nlinear_normalization_and_denorm() {
        // A constant-value series: after normalization (subtract last=C) all values are 0.
        // The linear layer output on zeros (plus bias=0) is 0.
        // Adding back C gives C everywhere in the forecast.
        // This tests the de-normalization logic.
        let cfg = NLinearConfig {
            seq_len: 10,
            pred_len: 3,
            n_channels: 1,
            individual: false,
        };
        let model = NLinearModel::new(cfg).expect("model creation failed");

        let mut constant_input = Array2::zeros((10, 1));
        for t in 0..10 {
            constant_input[[t, 0]] = 7.0;
        }
        let output = model.forecast(&constant_input).expect("forecast failed");
        // With weight=xavier (non-zero) and bias=0, the prediction on all-zero input
        // should be 0 (since dense(zeros)=bias=0). Adding back last_val=7 → all 7.
        for t in 0..3 {
            let expected = 7.0; // last value should be added back
            let actual = output[[t, 0]];
            // The linear output on all-zeros is just the bias (=0), so result = 0 + 7 = 7
            assert!(
                (actual - expected).abs() < 1e-10,
                "nlinear de-norm failed at t={t}: got {actual}, expected {expected}"
            );
        }
    }

    #[test]
    fn test_nlinear_wrong_seq_len_error() {
        let cfg = NLinearConfig {
            seq_len: 32,
            pred_len: 8,
            n_channels: 2,
            individual: false,
        };
        let model = NLinearModel::new(cfg).expect("model creation failed");
        let bad_input = make_input(16, 2);
        assert!(model.forecast(&bad_input).is_err());
    }

    #[test]
    fn test_nlinear_output_finite() {
        let cfg = NLinearConfig {
            seq_len: 24,
            pred_len: 6,
            n_channels: 5,
            individual: true,
        };
        let model = NLinearModel::new(cfg).expect("model creation failed");
        let input = make_input(24, 5);
        let output = model.forecast(&input).expect("forecast failed");
        for t in 0..6 {
            for c in 0..5 {
                assert!(output[[t, c]].is_finite(), "non-finite at [{t},{c}]");
            }
        }
    }
}