augurs_core/
forecast.rs

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
/// Forecast intervals.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ForecastIntervals {
    /// The confidence level for the intervals.
    pub level: f64,
    /// The lower prediction intervals.
    pub lower: Vec<f64>,
    /// The upper prediction intervals.
    pub upper: Vec<f64>,
}

impl ForecastIntervals {
    /// Return empty forecast intervals.
    pub fn empty(level: f64) -> ForecastIntervals {
        Self {
            level,
            lower: Vec::new(),
            upper: Vec::new(),
        }
    }

    /// Return empty forecast intervals with the specified capacity.
    pub fn with_capacity(level: f64, capacity: usize) -> ForecastIntervals {
        Self {
            level,
            lower: Vec::with_capacity(capacity),
            upper: Vec::with_capacity(capacity),
        }
    }
}

/// A forecast containing point forecasts and, optionally, prediction intervals.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Forecast {
    /// The point forecasts.
    pub point: Vec<f64>,
    /// The forecast intervals, if requested and supported
    /// by the trend model.
    pub intervals: Option<ForecastIntervals>,
}

impl Forecast {
    /// Return an empty forecast.
    pub fn empty() -> Forecast {
        Self {
            point: Vec::new(),
            intervals: None,
        }
    }

    /// Return an empty forecast with the specified capacity.
    pub fn with_capacity(capacity: usize) -> Forecast {
        Self {
            point: Vec::with_capacity(capacity),
            intervals: None,
        }
    }

    /// Return an empty forecast with the specified capacity and level.
    pub fn with_capacity_and_level(capacity: usize, level: f64) -> Forecast {
        Self {
            point: Vec::with_capacity(capacity),
            intervals: Some(ForecastIntervals::with_capacity(level, capacity)),
        }
    }

    /// Chain two forecasts together.
    ///
    /// This can be useful after producing in-sample and out-of-sample forecasts
    /// to combine them into a single forecast.
    pub fn chain(self, other: Self) -> Self {
        let mut out = Self::empty();
        out.point.extend(self.point);
        out.point.extend(other.point);
        if let Some(mut intervals) = self.intervals {
            if let Some(other_intervals) = other.intervals {
                intervals.lower.extend(other_intervals.lower);
                intervals.upper.extend(other_intervals.upper);
            }
            out.intervals = Some(intervals);
        } else if let Some(other_intervals) = other.intervals {
            out.intervals = Some(other_intervals);
        }
        out
    }
}