timers_rs/
ses.rs

1use crate::trend;
2
3pub struct ExponentialSmoothing{
4    pub alpha: f64,
5    pub beta: Option<f64>,
6}
7
8impl ExponentialSmoothing{
9    pub fn single_exponential_smoothing(&self, data: &[f64]) -> Vec<f64>{
10        let alpha = self.alpha;
11        let mut smoothed = Vec::new();
12        let mut x = data[0];
13
14        for &s in data{
15            x = alpha * s + (1.0 - alpha) * s;
16            println!("X Calculation: {}", x);
17            smoothed.push(x);
18        }
19
20        return smoothed
21    }
22
23    pub fn holt_linear_model(&self, data: &[f64]) -> (Vec<f64>, Vec<f64>){
24        let alpha = self.alpha;
25        let beta = self.beta.expect("Beta param is required for Holt Winter Model");
26
27        let mut smoothed = Vec::new();
28        let mut trend_smoothed = Vec::new();
29        let mut s = data[0];
30        let mut t = data[1] - data[0];
31
32        for &x in data{
33            let prev_s = s;
34            s = alpha * x + (1.0 - alpha) * (s + t); //Level estimation equation
35            t = beta * (s - prev_s) + (1.0 - beta) * t; //Trend Estimation Equation
36            smoothed.push(s);
37            trend_smoothed.push(t);
38        }
39
40        return (smoothed, trend_smoothed)
41    }
42}