pub struct Series<'a> { /* private fields */ }Expand description
A view over contiguous observations, oldest first.
Besides the values, a series knows its seasonal period (12 for monthly
data with a yearly cycle, 4 for quarterly, 1 for none) and where it sits in
the original data: slicing keeps the absolute position, so the season of
each observation and the alignment with external data (a price index, a
regressor) survive any head, tail or
slice.
Implementations§
Source§impl<'a> Series<'a>
impl<'a> Series<'a>
Sourcepub fn new(values: &'a [f64], period: usize) -> Self
pub fn new(values: &'a [f64], period: usize) -> Self
A series with the given seasonal period (0 is read as 1: no seasonality).
The first observation is taken to be at season 0; see
with_phase.
Sourcepub fn non_seasonal(values: &'a [f64]) -> Self
pub fn non_seasonal(values: &'a [f64]) -> Self
A series without seasonality.
Sourcepub fn monthly(values: &'a [f64], first_month: usize) -> Self
pub fn monthly(values: &'a [f64], first_month: usize) -> Self
Monthly data with a yearly cycle; first_month is the calendar month
of the first observation, 0 = January.
Examples found in repository?
9fn main() {
10 let data = include_str!("../data/piaui_revenue.csv");
11 let rows: Vec<Vec<f64>> = data
12 .lines()
13 .filter(|l| !l.starts_with('#') && !l.starts_with("month"))
14 .map(|l| l.split(',').skip(1).map(|v| v.parse().unwrap()).collect())
15 .collect();
16
17 for (column, name) in [(0, "ICMS"), (1, "FPE")] {
18 let values: Vec<f64> = rows.iter().map(|r| r[column]).collect();
19 // the first observation is March 2017
20 let y = Series::monthly(&values, 2);
21 let report = Backtest::default()
22 .run(y, &models::thorough())
23 .expect("series long enough");
24
25 println!(
26 "\n{name}: {} origins, {} months ahead",
27 report.origins, report.horizon
28 );
29 let mut ranking: Vec<_> = report.candidates.iter().collect();
30 ranking.sort_by(|a, b| a.score.total_cmp(&b.score));
31 for c in ranking {
32 let h = |i: usize| c.horizons[i].mape.unwrap_or(f64::NAN);
33 println!(
34 " {:<45} MAPE {:5.2}% h=1 {:5.2}% h=12 {:5.2}%",
35 c.name,
36 c.score,
37 h(0),
38 h(11)
39 );
40 }
41 let best = report.chosen();
42 let half = best.cumulative(6).expect("six months");
43 let i = half.interval(0.80).expect("80% interval");
44 println!(
45 " next 6 months with {}: {:.2} bi (80%: {:.2} to {:.2})",
46 best.name,
47 half.mean / 1e9,
48 i.lower / 1e9,
49 i.upper / 1e9
50 );
51 }
52}Sourcepub fn quarterly(values: &'a [f64], first_quarter: usize) -> Self
pub fn quarterly(values: &'a [f64], first_quarter: usize) -> Self
Quarterly data with a yearly cycle; first_quarter is 0 for Q1.
Sourcepub fn with_phase(self, phase: usize) -> Self
pub fn with_phase(self, phase: usize) -> Self
Sets the season of the first observation of this view.
pub fn values(&self) -> &'a [f64]
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn period(&self) -> usize
Sourcepub fn index(&self, i: usize) -> usize
pub fn index(&self, i: usize) -> usize
Absolute index of observation i. i may be past the end: len() is
the first period to be forecast.
Sourcepub fn season(&self, i: usize) -> usize
pub fn season(&self, i: usize) -> usize
Season (0..period) of observation i; like index,
i may be past the end.
Sourcepub fn with_values<'b>(&self, values: &'b [f64]) -> Option<Series<'b>>
pub fn with_values<'b>(&self, values: &'b [f64]) -> Option<Series<'b>>
The same series (period, season and position) over other values, such
as a transformation of the original ones. None if the lengths differ.
Sourcepub fn is_positive(&self) -> bool
pub fn is_positive(&self) -> bool
True when every value is finite and greater than zero (what multiplicative and logarithmic models need).