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
//! 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::types::{ExpSmoothingParams, ForecastResult};
use crate::decomposition::common::DecompositionModel;
use crate::decomposition::exponential::exponential_decomposition;

/// Helper to convert f64 constants to generic Float type
#[inline(always)]
pub(super) fn const_f64<F: Float + FromPrimitive>(value: f64) -> F {
    F::from(value).expect("Failed to convert constant to target float type")
}
/// Forecasts future values using simple moving average
///
/// # Arguments
///
/// * `ts` - The time series data
/// * `window_size` - Size of the moving window
/// * `horizon` - Number of future points to forecast
/// * `conf_level` - Confidence level (0.0-1.0) for prediction intervals
///
/// # Returns
///
/// * Forecast result containing point forecasts and confidence intervals
///
/// # Example
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_series::forecasting::moving_average_forecast;
///
/// let ts = array![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
/// let result = moving_average_forecast(&ts, 3, 5, 0.95).expect("Example/test failed");
/// println!("Forecast: {:?}", result.forecast);
/// println!("Lower CI: {:?}", result.lower_ci);
/// println!("Upper CI: {:?}", result.upper_ci);
/// ```
#[allow(dead_code)]
pub fn moving_average_forecast<F>(
    ts: &Array1<F>,
    window_size: usize,
    horizon: usize,
    conf_level: f64,
) -> Result<ForecastResult<F>>
where
    F: Float + FromPrimitive + Debug,
{
    if ts.len() < window_size {
        return Err(TimeSeriesError::ForecastingError(format!(
            "Time series length ({}) must be at least equal to window _size ({})",
            ts.len(),
            window_size
        )));
    }
    if conf_level <= 0.0 || conf_level >= 1.0 {
        return Err(TimeSeriesError::InvalidInput(
            "Confidence _level must be between 0 and 1 (exclusive)".to_string(),
        ));
    }
    let mut sum = F::zero();
    for i in ts.len() - window_size..ts.len() {
        sum = sum + ts[i];
    }
    let avg = sum / F::from_usize(window_size).expect("Failed to convert usize to float");
    let mut forecast = Array1::zeros(horizon);
    let mut lower_ci = Array1::zeros(horizon);
    let mut upper_ci = Array1::zeros(horizon);
    let mut sq_errors = Array1::zeros(ts.len() - window_size);
    for i in window_size..ts.len() {
        let mut window_sum = F::zero();
        for j in i - window_size..i {
            window_sum = window_sum + ts[j];
        }
        let window_avg =
            window_sum / F::from_usize(window_size).expect("Failed to convert usize to float");
        sq_errors[i - window_size] = (ts[i] - window_avg).powi(2);
    }
    let mse = sq_errors.iter().fold(F::zero(), |acc, &x| acc + x)
        / F::from_usize(sq_errors.len()).expect("Example/test failed");
    let std_err = mse.sqrt();
    let z_score = match conf_level {
        c if c >= 0.99 => const_f64::<F>(2.576),
        c if c >= 0.98 => const_f64::<F>(2.326),
        c if c >= 0.95 => const_f64::<F>(1.96),
        c if c >= 0.90 => const_f64::<F>(1.645),
        c if c >= 0.85 => const_f64::<F>(1.44),
        c if c >= 0.80 => const_f64::<F>(1.282),
        _ => const_f64::<F>(1.0),
    };
    for i in 0..horizon {
        forecast[i] = avg;
        let adjustment = F::one()
            + const_f64::<F>(0.1) * F::from_usize(i).expect("Failed to convert usize to float");
        let ci_width = z_score * std_err * adjustment;
        lower_ci[i] = avg - ci_width;
        upper_ci[i] = avg + ci_width;
    }
    Ok(ForecastResult {
        forecast,
        lower_ci,
        upper_ci,
    })
}
/// Forecasts future values using simple exponential smoothing
///
/// # Arguments
///
/// * `ts` - The time series data
/// * `alpha` - Smoothing parameter (0.0-1.0)
/// * `horizon` - Number of future points to forecast
/// * `conf_level` - Confidence level (0.0-1.0) for prediction intervals
///
/// # Returns
///
/// * Forecast result containing point forecasts and confidence intervals
///
/// # Example
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_series::forecasting::exponential_smoothing_forecast;
///
/// let ts = array![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
/// let result = exponential_smoothing_forecast(&ts, 0.3, 5, 0.95).expect("Example/test failed");
/// println!("Forecast: {:?}", result.forecast);
/// println!("Lower CI: {:?}", result.lower_ci);
/// println!("Upper CI: {:?}", result.upper_ci);
/// ```
#[allow(dead_code)]
pub fn exponential_smoothing_forecast<F>(
    ts: &Array1<F>,
    alpha: f64,
    horizon: usize,
    conf_level: f64,
) -> Result<ForecastResult<F>>
where
    F: Float + FromPrimitive + Debug,
{
    if ts.len() < 2 {
        return Err(TimeSeriesError::ForecastingError(
            "Time series must have at least 2 points for exponential smoothing".to_string(),
        ));
    }
    if alpha <= 0.0 || alpha >= 1.0 {
        return Err(TimeSeriesError::InvalidInput(
            "Smoothing parameter (alpha) must be between 0 and 1 (exclusive)".to_string(),
        ));
    }
    if conf_level <= 0.0 || conf_level >= 1.0 {
        return Err(TimeSeriesError::InvalidInput(
            "Confidence _level must be between 0 and 1 (exclusive)".to_string(),
        ));
    }
    let mut _level = Array1::zeros(ts.len() + 1);
    let mut sq_errors = Array1::zeros(ts.len() - 1);
    _level[0] = ts[0];
    for i in 0..ts.len() {
        _level[i + 1] = F::from_f64(alpha).expect("Failed to convert alpha to float") * ts[i]
            + F::from_f64(1.0 - alpha).expect("Failed to convert (1.0 - alpha) to float")
                * _level[i];
        if i > 0 {
            sq_errors[i - 1] = (ts[i] - _level[i]).powi(2);
        }
    }
    let mse = sq_errors.iter().fold(F::zero(), |acc, &x| acc + x)
        / F::from_usize(sq_errors.len()).expect("Example/test failed");
    let std_err = mse.sqrt();
    let z_score = match conf_level {
        c if c >= 0.99 => const_f64::<F>(2.576),
        c if c >= 0.98 => const_f64::<F>(2.326),
        c if c >= 0.95 => const_f64::<F>(1.96),
        c if c >= 0.90 => const_f64::<F>(1.645),
        c if c >= 0.85 => const_f64::<F>(1.44),
        c if c >= 0.80 => const_f64::<F>(1.282),
        _ => const_f64::<F>(1.0),
    };
    let mut forecast = Array1::zeros(horizon);
    let mut lower_ci = Array1::zeros(horizon);
    let mut upper_ci = Array1::zeros(horizon);
    for i in 0..horizon {
        forecast[i] = _level[ts.len()];
        let h_adjustment = (F::from_usize(i + 1).expect("Failed to convert usize to float")).sqrt();
        let ci_width = z_score * std_err * h_adjustment;
        lower_ci[i] = forecast[i] - ci_width;
        upper_ci[i] = forecast[i] + ci_width;
    }
    Ok(ForecastResult {
        forecast,
        lower_ci,
        upper_ci,
    })
}
/// Forecasts future values using Holt-Winters exponential smoothing
///
/// # Arguments
///
/// * `ts` - The time series data
/// * `params` - Exponential smoothing parameters
/// * `horizon` - Number of future points to forecast
/// * `conf_level` - Confidence level (0.0-1.0) for prediction intervals
///
/// # Returns
///
/// * Forecast result containing point forecasts and confidence intervals
///
/// # Example
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_series::forecasting::{holt_winters_forecast, ExpSmoothingParams};
///
/// let ts = array![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 9.0, 8.0];
///
/// let mut params = ExpSmoothingParams::default();
/// params.alpha = 0.3;
/// params.beta = Some(0.1);
/// params.gamma = Some(0.2);
/// params.seasonal_period = Some(4);
///
/// let result = holt_winters_forecast(&ts, &params, 8, 0.95).expect("Example/test failed");
/// println!("Forecast: {:?}", result.forecast);
/// ```
#[allow(dead_code)]
pub fn holt_winters_forecast<F>(
    ts: &Array1<F>,
    params: &ExpSmoothingParams,
    horizon: usize,
    conf_level: f64,
) -> Result<ForecastResult<F>>
where
    F: Float + FromPrimitive + Debug,
{
    if params.alpha <= 0.0 || params.alpha >= 1.0 {
        return Err(TimeSeriesError::InvalidInput(
            "Alpha must be between 0 and 1 (exclusive)".to_string(),
        ));
    }
    if let Some(beta) = params.beta {
        if beta <= 0.0 || beta >= 1.0 {
            return Err(TimeSeriesError::InvalidInput(
                "Beta must be between 0 and 1 (exclusive)".to_string(),
            ));
        }
    }
    if let Some(gamma) = params.gamma {
        if gamma <= 0.0 || gamma >= 1.0 {
            return Err(TimeSeriesError::InvalidInput(
                "Gamma must be between 0 and 1 (exclusive)".to_string(),
            ));
        }
        if params.seasonal_period.is_none() {
            return Err(TimeSeriesError::InvalidInput(
                "Seasonal period must be provided when gamma is specified".to_string(),
            ));
        }
    }
    if conf_level <= 0.0 || conf_level >= 1.0 {
        return Err(TimeSeriesError::InvalidInput(
            "Confidence _level must be between 0 and 1 (exclusive)".to_string(),
        ));
    }
    if let Some(period) = params.seasonal_period {
        if ts.len() < 2 * period {
            return Err(TimeSeriesError::ForecastingError(format!(
                "Time series length ({}) must be at least twice the seasonal period ({})",
                ts.len(),
                period
            )));
        }
    } else if ts.len() < 3 {
        return Err(TimeSeriesError::ForecastingError(
            "Time series must have at least 3 points for non-seasonal models".to_string(),
        ));
    }
    if params.damped_trend {
        if let Some(phi) = params.phi {
            if phi <= 0.0 || phi >= 1.0 {
                return Err(TimeSeriesError::InvalidInput(
                    "Damping parameter (phi) must be between 0 and 1 (exclusive)".to_string(),
                ));
            }
        } else {
            return Err(TimeSeriesError::InvalidInput(
                "Damping parameter (phi) must be provided for damped trend models".to_string(),
            ));
        }
    }
    let has_trend = params.beta.is_some();
    let has_seasonal = params.gamma.is_some() && params.seasonal_period.is_some();
    if (params.multiplicative_trend || params.multiplicative_seasonality)
        && ts.iter().any(|&x| x <= F::zero())
    {
        return Err(TimeSeriesError::InvalidInput(
            "Multiplicative models require strictly positive data".to_string(),
        ));
    }
    let n = ts.len();
    let mut _level = Array1::zeros(n + 1);
    let mut trend = Array1::zeros(n + 1);
    let mut seasonal = Array1::zeros(n + params.seasonal_period.unwrap_or(1));
    let mut forecast_errors = Array1::zeros(n);
    if has_seasonal {
        let period = params.seasonal_period.expect("Seasonal period required");
        let decomp = exponential_decomposition(
            ts,
            period,
            params.alpha,
            params.beta.unwrap_or(0.1),
            params.gamma.expect("Gamma parameter required"),
            if params.multiplicative_seasonality {
                DecompositionModel::Multiplicative
            } else {
                DecompositionModel::Additive
            },
        )?;
        _level[n] = decomp.trend[n - 1];
        if has_trend {
            if n >= 2 {
                trend[n] = decomp.trend[n - 1] - decomp.trend[n - 2];
            }
        }
        for i in 0..period {
            seasonal[i] = decomp.seasonal[n - period + i];
        }
        for i in period..n {
            let pred = decomp.trend[i - 1] + decomp.seasonal[i];
            forecast_errors[i] = ts[i] - pred;
        }
    } else if has_trend {
        _level[0] = ts[0];
        if n > 1 {
            trend[0] = ts[1] - ts[0];
        }
        let alpha = F::from_f64(params.alpha).expect("Failed to convert alpha to float");
        let beta = F::from_f64(params.beta.expect("Beta parameter required"))
            .expect("Failed to convert beta to float");
        let phi = F::from_f64(params.phi.unwrap_or(1.0)).expect("Failed to convert phi to float");
        for i in 1..=n {
            let expected = _level[i - 1]
                + if params.damped_trend {
                    phi * trend[i - 1]
                } else {
                    trend[i - 1]
                };
            if i < n {
                _level[i] = alpha * ts[i - 1] + (F::one() - alpha) * expected;
                trend[i] = beta * (_level[i] - _level[i - 1])
                    + (F::one() - beta)
                        * if params.damped_trend {
                            phi * trend[i - 1]
                        } else {
                            trend[i - 1]
                        };
                forecast_errors[i - 1] = ts[i - 1] - expected;
            }
        }
    } else {
        return exponential_smoothing_forecast(ts, params.alpha, horizon, conf_level);
    }
    let mse = forecast_errors
        .iter()
        .skip(if has_seasonal {
            params.seasonal_period.expect("Seasonal period required")
        } else {
            1
        })
        .fold(F::zero(), |acc, &x| acc + x.powi(2))
        / F::from_usize(if has_seasonal {
            n - params.seasonal_period.expect("Seasonal period required")
        } else {
            n - 1
        })
        .expect("Example/test failed");
    let std_err = mse.sqrt();
    let z_score = match conf_level {
        c if c >= 0.99 => const_f64::<F>(2.576),
        c if c >= 0.98 => const_f64::<F>(2.326),
        c if c >= 0.95 => const_f64::<F>(1.96),
        c if c >= 0.90 => const_f64::<F>(1.645),
        c if c >= 0.85 => const_f64::<F>(1.44),
        c if c >= 0.80 => const_f64::<F>(1.282),
        _ => const_f64::<F>(1.0),
    };
    let mut forecast = Array1::zeros(horizon);
    let mut lower_ci = Array1::zeros(horizon);
    let mut upper_ci = Array1::zeros(horizon);
    for h in 0..horizon {
        let mut pred = _level[n];
        if has_trend {
            let phi =
                F::from_f64(params.phi.unwrap_or(1.0)).expect("Failed to convert phi to float");
            if params.damped_trend {
                let mut sum = F::one();
                let mut term = F::one();
                for _ in 1..h + 1 {
                    term = term * phi;
                    sum = sum + term;
                }
                pred = pred + trend[n] * sum;
            } else {
                pred = pred
                    + F::from_usize(h + 1).expect("Failed to convert usize to float") * trend[n];
            }
        }
        if has_seasonal {
            let period = params.seasonal_period.expect("Seasonal period required");
            let season_idx = (h + n) % period;
            if params.multiplicative_seasonality {
                pred = pred * seasonal[season_idx];
            } else {
                pred = pred + seasonal[season_idx];
            }
        }
        forecast[h] = pred;
        let h_adjustment = (F::from_usize(h + 1).expect("Failed to convert usize to float")).sqrt();
        let ci_width = z_score * std_err * h_adjustment;
        lower_ci[h] = pred - ci_width;
        upper_ci[h] = pred + ci_width;
    }
    Ok(ForecastResult {
        forecast,
        lower_ci,
        upper_ci,
    })
}