scirs2-series 0.3.3

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
//! Auto-generated module
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)

use crate::error::{Result, TimeSeriesError};
use scirs2_core::ndarray::Array1;
use scirs2_core::numeric::{Float, FromPrimitive};
use std::fmt::Debug;

use super::functions::{
    const_f64, exponential_smoothing_forecast, holt_winters_forecast, moving_average_forecast,
};
use super::functions_2::arima_forecast;
use super::types::{ArimaParams, ExpSmoothingParams};

/// Ensemble forecasting methods
pub mod ensemble {
    use super::*;
    use scirs2_core::ndarray::Array1;
    use scirs2_core::numeric::{Float, FromPrimitive};
    use std::fmt::Debug;
    /// Configuration for ensemble forecasting
    #[derive(Debug, Clone)]
    pub struct EnsembleConfig {
        /// Include ARIMA model in ensemble
        pub use_arima: bool,
        /// Include exponential smoothing in ensemble
        pub use_exp_smoothing: bool,
        /// Include Holt-Winters in ensemble
        pub use_holt_winters: bool,
        /// Include moving average in ensemble
        pub use_moving_average: bool,
        /// Weights for models (if empty, uses equal weights)
        pub weights: Vec<f64>,
        /// Use adaptive weighting based on historical performance
        pub adaptive_weights: bool,
        /// Forecast horizon
        pub horizon: usize,
    }
    impl Default for EnsembleConfig {
        fn default() -> Self {
            Self {
                use_arima: true,
                use_exp_smoothing: true,
                use_holt_winters: true,
                use_moving_average: true,
                weights: vec![],
                adaptive_weights: false,
                horizon: 12,
            }
        }
    }
    /// Result of ensemble forecasting
    #[derive(Debug, Clone)]
    pub struct EnsembleResult<F> {
        /// Combined ensemble forecast
        pub ensemble_forecast: Array1<F>,
        /// Individual model forecasts
        pub individual_forecasts: Vec<Array1<F>>,
        /// Model names
        pub model_names: Vec<String>,
        /// Final weights used for combination
        pub weights: Vec<f64>,
        /// Lower confidence interval
        pub lower_ci: Array1<F>,
        /// Upper confidence interval
        pub upper_ci: Array1<F>,
    }
    /// Simple averaging ensemble
    pub fn simple_ensemble_forecast<F>(
        data: &Array1<F>,
        config: &EnsembleConfig,
    ) -> Result<EnsembleResult<F>>
    where
        F: Float + FromPrimitive + Debug + Clone,
    {
        if data.is_empty() {
            return Err(TimeSeriesError::InvalidInput(
                "Data cannot be empty".to_string(),
            ));
        }
        let mut individual_forecasts = Vec::new();
        let mut model_names = Vec::new();
        if config.use_moving_average {
            let window = std::cmp::min(12, data.len() / 2);
            if let Ok(forecast) = super::moving_average_forecast(data, window, config.horizon, 0.95)
            {
                individual_forecasts.push(forecast.forecast);
                model_names.push("MovingAverage".to_string());
            }
        }
        if config.use_exp_smoothing {
            let params = super::ExpSmoothingParams::default();
            if let Ok(forecast) =
                super::exponential_smoothing_forecast(data, params.alpha, config.horizon, 0.95)
            {
                individual_forecasts.push(forecast.forecast);
                model_names.push("ExponentialSmoothing".to_string());
            }
        }
        if config.use_holt_winters && data.len() >= 24 {
            let params = super::ExpSmoothingParams {
                alpha: 0.3,
                beta: Some(0.1),
                gamma: Some(0.1),
                seasonal_period: Some(12),
                ..Default::default()
            };
            if let Ok(forecast) = super::holt_winters_forecast(data, &params, config.horizon, 0.95)
            {
                individual_forecasts.push(forecast.forecast);
                model_names.push("HoltWinters".to_string());
            }
        }
        if config.use_arima && data.len() >= 20 {
            let params = super::ArimaParams {
                p: 1,
                d: 1,
                q: 1,
                ..Default::default()
            };
            if let Ok(forecast) = super::arima_forecast(data, &params, config.horizon, 0.95) {
                individual_forecasts.push(forecast.forecast);
                model_names.push("ARIMA".to_string());
            }
        }
        if individual_forecasts.is_empty() {
            return Err(TimeSeriesError::InvalidInput(
                "No valid forecasts could be generated".to_string(),
            ));
        }
        let weights =
            if config.weights.is_empty() || config.weights.len() != individual_forecasts.len() {
                vec![1.0 / individual_forecasts.len() as f64; individual_forecasts.len()]
            } else {
                let sum: f64 = config.weights.iter().sum();
                config.weights.iter().map(|w| w / sum).collect()
            };
        let mut ensemble_forecast = Array1::zeros(config.horizon);
        for (i, weight) in weights.iter().enumerate() {
            let w = F::from_f64(*weight).expect("Failed to convert weight to float");
            for j in 0..config.horizon {
                if j < individual_forecasts[i].len() {
                    ensemble_forecast[j] = ensemble_forecast[j] + w * individual_forecasts[i][j];
                }
            }
        }
        let mut lower_ci = Array1::zeros(config.horizon);
        let mut upper_ci = Array1::zeros(config.horizon);
        for j in 0..config.horizon {
            let mean = ensemble_forecast[j];
            let mut variance = F::zero();
            let mut count = 0;
            for forecast in &individual_forecasts {
                if j < forecast.len() {
                    let diff = forecast[j] - mean;
                    variance = variance + diff * diff;
                    count += 1;
                }
            }
            if count > 1 {
                variance =
                    variance / F::from_usize(count).expect("Failed to convert usize to float");
                let std_dev = variance.sqrt();
                let margin = std_dev * const_f64::<F>(1.96);
                lower_ci[j] = mean - margin;
                upper_ci[j] = mean + margin;
            } else {
                lower_ci[j] = mean;
                upper_ci[j] = mean;
            }
        }
        Ok(EnsembleResult {
            ensemble_forecast,
            individual_forecasts,
            model_names,
            weights,
            lower_ci,
            upper_ci,
        })
    }
    /// Weighted ensemble based on historical performance
    pub fn weighted_ensemble_forecast<F>(
        data: &Array1<F>,
        config: &EnsembleConfig,
    ) -> Result<EnsembleResult<F>>
    where
        F: Float + FromPrimitive + Debug + Clone,
    {
        if data.len() < 20 {
            return Err(TimeSeriesError::InsufficientData {
                message: "Need at least 20 observations for weighted ensemble".to_string(),
                required: 20,
                actual: data.len(),
            });
        }
        let split_point = data.len() - config.horizon;
        let train_data = data
            .slice(scirs2_core::ndarray::s![..split_point])
            .to_owned();
        let validation_data = data
            .slice(scirs2_core::ndarray::s![split_point..])
            .to_owned();
        let mut individual_forecasts = Vec::new();
        let mut model_names = Vec::new();
        let mut validation_errors = Vec::new();
        if config.use_moving_average {
            let window = std::cmp::min(12, train_data.len() / 2);
            if let Ok(forecast) =
                super::moving_average_forecast(&train_data, window, config.horizon, 0.95)
            {
                let error = calculate_mse(&forecast.forecast, &validation_data);
                individual_forecasts.push(forecast.forecast);
                model_names.push("MovingAverage".to_string());
                validation_errors.push(error);
            }
        }
        if config.use_exp_smoothing {
            let params = super::ExpSmoothingParams::default();
            if let Ok(forecast) = super::exponential_smoothing_forecast(
                &train_data,
                params.alpha,
                config.horizon,
                0.95,
            ) {
                let error = calculate_mse(&forecast.forecast, &validation_data);
                individual_forecasts.push(forecast.forecast);
                model_names.push("ExponentialSmoothing".to_string());
                validation_errors.push(error);
            }
        }
        if config.use_holt_winters && train_data.len() >= 24 {
            let params = super::ExpSmoothingParams {
                alpha: 0.3,
                beta: Some(0.1),
                gamma: Some(0.1),
                seasonal_period: Some(12),
                ..Default::default()
            };
            if let Ok(forecast) =
                super::holt_winters_forecast(&train_data, &params, config.horizon, 0.95)
            {
                let error = calculate_mse(&forecast.forecast, &validation_data);
                individual_forecasts.push(forecast.forecast);
                model_names.push("HoltWinters".to_string());
                validation_errors.push(error);
            }
        }
        if config.use_arima && train_data.len() >= 20 {
            let params = super::ArimaParams {
                p: 1,
                d: 1,
                q: 1,
                ..Default::default()
            };
            if let Ok(forecast) = super::arima_forecast(&train_data, &params, config.horizon, 0.95)
            {
                let error = calculate_mse(&forecast.forecast, &validation_data);
                individual_forecasts.push(forecast.forecast);
                model_names.push("ARIMA".to_string());
                validation_errors.push(error);
            }
        }
        if individual_forecasts.is_empty() {
            return Err(TimeSeriesError::InvalidInput(
                "No valid forecasts could be generated".to_string(),
            ));
        }
        let weights: Vec<f64> = if validation_errors.iter().all(|&e| e > 0.0) {
            let inv_errors: Vec<f64> = validation_errors.iter().map(|&e| 1.0 / e).collect();
            let sum: f64 = inv_errors.iter().sum();
            inv_errors.iter().map(|&w| w / sum).collect()
        } else {
            vec![1.0 / individual_forecasts.len() as f64; individual_forecasts.len()]
        };
        let mut final_forecasts = Vec::new();
        if config.use_moving_average && model_names.contains(&"MovingAverage".to_string()) {
            let window = std::cmp::min(12, data.len() / 2);
            if let Ok(forecast) = super::moving_average_forecast(data, window, config.horizon, 0.95)
            {
                final_forecasts.push(forecast.forecast);
            }
        }
        if config.use_exp_smoothing && model_names.contains(&"ExponentialSmoothing".to_string()) {
            let params = super::ExpSmoothingParams::default();
            if let Ok(forecast) =
                super::exponential_smoothing_forecast(data, params.alpha, config.horizon, 0.95)
            {
                final_forecasts.push(forecast.forecast);
            }
        }
        if config.use_holt_winters && model_names.contains(&"HoltWinters".to_string()) {
            let params = super::ExpSmoothingParams {
                alpha: 0.3,
                beta: Some(0.1),
                gamma: Some(0.1),
                seasonal_period: Some(12),
                ..Default::default()
            };
            if let Ok(forecast) = super::holt_winters_forecast(data, &params, config.horizon, 0.95)
            {
                final_forecasts.push(forecast.forecast);
            }
        }
        if config.use_arima && model_names.contains(&"ARIMA".to_string()) {
            let params = super::ArimaParams {
                p: 1,
                d: 1,
                q: 1,
                ..Default::default()
            };
            if let Ok(forecast) = super::arima_forecast(data, &params, config.horizon, 0.95) {
                final_forecasts.push(forecast.forecast);
            }
        }
        let mut ensemble_forecast = Array1::zeros(config.horizon);
        for (i, weight) in weights.iter().enumerate() {
            if i < final_forecasts.len() {
                let w = F::from_f64(*weight).expect("Failed to convert weight to float");
                for j in 0..config.horizon {
                    if j < final_forecasts[i].len() {
                        ensemble_forecast[j] = ensemble_forecast[j] + w * final_forecasts[i][j];
                    }
                }
            }
        }
        let mut lower_ci = Array1::zeros(config.horizon);
        let mut upper_ci = Array1::zeros(config.horizon);
        for j in 0..config.horizon {
            let mean = ensemble_forecast[j];
            let mut variance = F::zero();
            let mut count = 0;
            for forecast in &final_forecasts {
                if j < forecast.len() {
                    let diff = forecast[j] - mean;
                    variance = variance + diff * diff;
                    count += 1;
                }
            }
            if count > 1 {
                variance =
                    variance / F::from_usize(count).expect("Failed to convert usize to float");
                let std_dev = variance.sqrt();
                let margin = std_dev * const_f64::<F>(1.96);
                lower_ci[j] = mean - margin;
                upper_ci[j] = mean + margin;
            } else {
                lower_ci[j] = mean;
                upper_ci[j] = mean;
            }
        }
        Ok(EnsembleResult {
            ensemble_forecast,
            individual_forecasts: final_forecasts,
            model_names,
            weights,
            lower_ci,
            upper_ci,
        })
    }
    /// Stacked ensemble using simple linear combination
    pub fn stacked_ensemble_forecast<F>(
        data: &Array1<F>,
        config: &EnsembleConfig,
    ) -> Result<EnsembleResult<F>>
    where
        F: Float + FromPrimitive + Debug + Clone,
    {
        if data.len() < 30 {
            return Err(TimeSeriesError::InsufficientData {
                message: "Need at least 30 observations for stacked ensemble".to_string(),
                required: 30,
                actual: data.len(),
            });
        }
        let cv_folds = 3;
        let fold_size = data.len() / cv_folds;
        let mut all_predictions = Vec::new();
        let mut all_targets = Vec::new();
        let mut model_names = Vec::new();
        if config.use_moving_average {
            model_names.push("MovingAverage".to_string());
        }
        if config.use_exp_smoothing {
            model_names.push("ExponentialSmoothing".to_string());
        }
        if config.use_holt_winters {
            model_names.push("HoltWinters".to_string());
        }
        if config.use_arima {
            model_names.push("ARIMA".to_string());
        }
        for fold in 0..cv_folds {
            let test_start = fold * fold_size;
            let test_end = if fold == cv_folds - 1 {
                data.len()
            } else {
                (fold + 1) * fold_size
            };
            if test_start >= data.len() - 1 {
                continue;
            }
            let train_data = if test_start == 0 {
                data.slice(scirs2_core::ndarray::s![test_end..]).to_owned()
            } else {
                let train_part1 = data
                    .slice(scirs2_core::ndarray::s![..test_start])
                    .to_owned();
                let train_part2 = data.slice(scirs2_core::ndarray::s![test_end..]).to_owned();
                let mut combined = Array1::zeros(train_part1.len() + train_part2.len());
                combined
                    .slice_mut(scirs2_core::ndarray::s![..train_part1.len()])
                    .assign(&train_part1);
                combined
                    .slice_mut(scirs2_core::ndarray::s![train_part1.len()..])
                    .assign(&train_part2);
                combined
            };
            let test_data = data
                .slice(scirs2_core::ndarray::s![test_start..test_end])
                .to_owned();
            let horizon = test_data.len();
            if train_data.len() < 10 {
                continue;
            }
            let mut fold_predictions = Vec::new();
            if config.use_moving_average {
                let window = std::cmp::min(12, train_data.len() / 2);
                if let Ok(forecast) =
                    super::moving_average_forecast(&train_data, window, horizon, 0.95)
                {
                    fold_predictions.push(forecast.forecast);
                } else {
                    fold_predictions.push(Array1::zeros(horizon));
                }
            }
            if config.use_exp_smoothing {
                let params = super::ExpSmoothingParams::default();
                if let Ok(forecast) =
                    super::exponential_smoothing_forecast(&train_data, params.alpha, horizon, 0.95)
                {
                    fold_predictions.push(forecast.forecast);
                } else {
                    fold_predictions.push(Array1::zeros(horizon));
                }
            }
            if config.use_holt_winters && train_data.len() >= 24 {
                let params = super::ExpSmoothingParams {
                    alpha: 0.3,
                    beta: Some(0.1),
                    gamma: Some(0.1),
                    seasonal_period: Some(12),
                    ..Default::default()
                };
                if let Ok(forecast) =
                    super::holt_winters_forecast(&train_data, &params, horizon, 0.95)
                {
                    fold_predictions.push(forecast.forecast);
                } else {
                    fold_predictions.push(Array1::zeros(horizon));
                }
            }
            if config.use_arima && train_data.len() >= 20 {
                let params = super::ArimaParams {
                    p: 1,
                    d: 1,
                    q: 1,
                    ..Default::default()
                };
                if let Ok(forecast) = super::arima_forecast(&train_data, &params, horizon, 0.95) {
                    fold_predictions.push(forecast.forecast);
                } else {
                    fold_predictions.push(Array1::zeros(horizon));
                }
            }
            all_predictions.push(fold_predictions);
            all_targets.push(test_data);
        }
        let num_models = model_names.len();
        let mut weights = vec![1.0 / num_models as f64; num_models];
        if !all_predictions.is_empty() {
            let mut x = Vec::new();
            let mut y = Vec::new();
            for (fold_predictions, targets) in all_predictions.iter().zip(all_targets.iter()) {
                for i in 0..targets.len() {
                    let mut row = Vec::new();
                    for model_pred in fold_predictions {
                        if i < model_pred.len() {
                            row.push(model_pred[i].to_f64().unwrap_or(0.0));
                        } else {
                            row.push(0.0);
                        }
                    }
                    if row.len() == num_models {
                        x.push(row);
                        y.push(targets[i].to_f64().unwrap_or(0.0));
                    }
                }
            }
            if x.len() > num_models && x.iter().all(|row| row.len() == num_models) {
                weights = solve_linear_regression(&x, &y);
                let mut sum = weights.iter().sum::<f64>();
                if sum <= 0.0 {
                    weights = vec![1.0 / num_models as f64; num_models];
                } else {
                    for w in weights.iter_mut() {
                        *w = w.max(0.0);
                    }
                    sum = weights.iter().sum::<f64>();
                    if sum > 0.0 {
                        for w in weights.iter_mut() {
                            *w /= sum;
                        }
                    } else {
                        weights = vec![1.0 / num_models as f64; num_models];
                    }
                }
            }
        }
        let mut final_forecasts = Vec::new();
        if config.use_moving_average {
            let window = std::cmp::min(12, data.len() / 2);
            if let Ok(forecast) = super::moving_average_forecast(data, window, config.horizon, 0.95)
            {
                final_forecasts.push(forecast.forecast);
            }
        }
        if config.use_exp_smoothing {
            let params = super::ExpSmoothingParams::default();
            if let Ok(forecast) =
                super::exponential_smoothing_forecast(data, params.alpha, config.horizon, 0.95)
            {
                final_forecasts.push(forecast.forecast);
            }
        }
        if config.use_holt_winters && data.len() >= 24 {
            let params = super::ExpSmoothingParams {
                alpha: 0.3,
                beta: Some(0.1),
                gamma: Some(0.1),
                seasonal_period: Some(12),
                ..Default::default()
            };
            if let Ok(forecast) = super::holt_winters_forecast(data, &params, config.horizon, 0.95)
            {
                final_forecasts.push(forecast.forecast);
            }
        }
        if config.use_arima && data.len() >= 20 {
            let params = super::ArimaParams {
                p: 1,
                d: 1,
                q: 1,
                ..Default::default()
            };
            if let Ok(forecast) = super::arima_forecast(data, &params, config.horizon, 0.95) {
                final_forecasts.push(forecast.forecast);
            }
        }
        let mut ensemble_forecast = Array1::zeros(config.horizon);
        for (i, weight) in weights.iter().enumerate() {
            if i < final_forecasts.len() {
                let w = F::from_f64(*weight).expect("Failed to convert weight to float");
                for j in 0..config.horizon {
                    if j < final_forecasts[i].len() {
                        ensemble_forecast[j] = ensemble_forecast[j] + w * final_forecasts[i][j];
                    }
                }
            }
        }
        let mut lower_ci = Array1::zeros(config.horizon);
        let mut upper_ci = Array1::zeros(config.horizon);
        for j in 0..config.horizon {
            let mean = ensemble_forecast[j];
            let mut variance = F::zero();
            let mut count = 0;
            for forecast in &final_forecasts {
                if j < forecast.len() {
                    let diff = forecast[j] - mean;
                    variance = variance + diff * diff;
                    count += 1;
                }
            }
            if count > 1 {
                variance =
                    variance / F::from_usize(count).expect("Failed to convert usize to float");
                let std_dev = variance.sqrt();
                let margin = std_dev * const_f64::<F>(1.96);
                lower_ci[j] = mean - margin;
                upper_ci[j] = mean + margin;
            } else {
                lower_ci[j] = mean;
                upper_ci[j] = mean;
            }
        }
        Ok(EnsembleResult {
            ensemble_forecast,
            individual_forecasts: final_forecasts,
            model_names,
            weights,
            lower_ci,
            upper_ci,
        })
    }
    /// Calculate Mean Squared Error between forecast and actual values
    fn calculate_mse<F: Float>(forecast: &Array1<F>, actual: &Array1<F>) -> f64 {
        let min_len = forecast.len().min(actual.len());
        if min_len == 0 {
            return f64::INFINITY;
        }
        let mut sum_sq_error = 0.0;
        for i in 0..min_len {
            let error = forecast[i].to_f64().unwrap_or(0.0) - actual[i].to_f64().unwrap_or(0.0);
            sum_sq_error += error * error;
        }
        sum_sq_error / min_len as f64
    }
    /// Simple linear regression solver for stacking weights
    fn solve_linear_regression(x: &[Vec<f64>], y: &[f64]) -> Vec<f64> {
        let n = x.len();
        let m = if n > 0 { x[0].len() } else { 0 };
        if n == 0 || m == 0 || n != y.len() {
            return vec![1.0 / m.max(1) as f64; m.max(1)];
        }
        let mut weights = vec![1.0 / m as f64; m];
        let learning_rate = 0.01;
        let iterations = 100;
        for _ in 0..iterations {
            let mut gradients = vec![0.0; m];
            for (x_row, &y_val) in x.iter().zip(y.iter()) {
                let prediction = weights
                    .iter()
                    .zip(x_row.iter())
                    .map(|(w, x)| w * x)
                    .sum::<f64>();
                let error = prediction - y_val;
                for (grad, &x_val) in gradients.iter_mut().zip(x_row.iter()) {
                    *grad += (2.0 / n as f64) * error * x_val;
                }
            }
            for j in 0..m {
                weights[j] -= learning_rate * gradients[j];
                weights[j] = weights[j].max(0.0);
            }
            let sum: f64 = weights.iter().sum();
            if sum > 0.0 {
                for w in weights.iter_mut() {
                    *w /= sum;
                }
            }
        }
        weights
    }
}