#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Forecast {
pub day: i64,
pub vix: Vec<f64>,
pub index_variance: Vec<f64>,
pub policy_rate: Vec<f64>,
pub oil: Vec<f64>,
pub name_variance: Vec<Vec<f64>>,
}
impl Forecast {
pub fn horizon(&self) -> usize {
self.vix.len()
}
pub fn to_words(&self) -> Vec<f64> {
let h = self.horizon();
let mut out = Vec::with_capacity(3 + 4 * h + self.name_variance.len() * (h + 1));
out.push(self.day as f64);
out.push(h as f64);
out.push(self.name_variance.len() as f64);
for series in [&self.vix, &self.index_variance, &self.policy_rate, &self.oil] {
out.extend_from_slice(series);
}
for row in &self.name_variance {
out.push(row.len() as f64);
out.extend_from_slice(row);
}
out
}
pub fn from_words(words: &[f64]) -> Result<Self, String> {
let bad = |why: &str| format!("a forecast's numbers do not parse: {why}");
let count = |v: f64, what: &str| -> Result<usize, String> {
if v.is_finite() && v >= 0.0 && v.fract() == 0.0 && v <= 1e9 {
Ok(v as usize)
} else {
Err(bad(&format!("{what} is {v}")))
}
};
if words.len() < 3 {
return Err(bad("fewer than three numbers"));
}
let day = words[0];
if !(day.is_finite() && day.fract() == 0.0) {
return Err(bad(&format!("the day is {day}")));
}
let h = count(words[1], "the horizon")?;
let n = count(words[2], "the slot count")?;
let mut at = 3;
let mut take = |len: usize| -> Result<Vec<f64>, String> {
if at + len > words.len() {
return Err(bad("it is shorter than its counts say"));
}
let v = words[at..at + len].to_vec();
at += len;
Ok(v)
};
let vix = take(h)?;
let index_variance = take(h)?;
let policy_rate = take(h)?;
let oil = take(h)?;
let mut name_variance = Vec::with_capacity(n);
for _ in 0..n {
let len = take(1)?[0];
let len = count(len, "a slot's length")?;
if len != 0 && len != h {
return Err(bad(&format!("a slot carries {len} numbers for a horizon of {h}")));
}
name_variance.push(take(len)?);
}
if at != words.len() {
return Err(bad("it is longer than its counts say"));
}
Ok(Forecast { day: day as i64, vix, index_variance, policy_rate, oil, name_variance })
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_words_round_trip_and_a_bad_list_is_refused() {
let f = Forecast {
day: 12,
vix: vec![20.0, 19.5],
index_variance: vec![1e-4, 1.1e-4],
policy_rate: vec![3.0, 3.0],
oil: vec![80.0, 80.5],
name_variance: vec![vec![2e-4, 2.1e-4], vec![], vec![3e-4, 3e-4]],
};
let words = f.to_words();
assert_eq!(Forecast::from_words(&words).unwrap(), f);
assert!(Forecast::from_words(&words[..words.len() - 1]).is_err());
let mut longer = words.clone();
longer.push(0.0);
assert!(Forecast::from_words(&longer).is_err());
let mut odd = words;
odd[1] = 2.5;
assert!(Forecast::from_words(&odd).is_err());
}
}