use time::Duration;
#[derive(Debug, Clone, PartialEq)]
pub struct HorizonBands {
step: Duration,
bands: Vec<(f64, f64)>,
}
impl HorizonBands {
pub fn from_residuals(step: Duration, residuals_by_index: &[Vec<f64>], q: f64) -> Self {
let bands = residuals_by_index
.iter()
.map(|errors| {
if errors.is_empty() {
(0.0, 0.0)
} else {
let mut sorted = errors.clone();
sorted.sort_by(|a, b| a.total_cmp(b));
(quantile(&sorted, q), quantile(&sorted, 1.0 - q))
}
})
.collect();
Self { step, bands }
}
pub fn step(&self) -> Duration {
self.step
}
pub fn len(&self) -> usize {
self.bands.len()
}
pub fn is_empty(&self) -> bool {
self.bands.is_empty()
}
pub fn at(&self, dt: Duration) -> Option<(f64, f64)> {
let step_secs = self.step.whole_seconds();
if self.bands.is_empty() || step_secs <= 0 {
return None;
}
let idx = (dt.whole_seconds() / step_secs).max(0) as usize;
Some(self.bands[idx.min(self.bands.len() - 1)])
}
}
fn quantile(sorted: &[f64], q: f64) -> f64 {
if sorted.len() == 1 {
return sorted[0];
}
let rank = q.clamp(0.0, 1.0) * (sorted.len() - 1) as f64;
let lo = rank.floor() as usize;
let hi = rank.ceil() as usize;
let frac = rank - lo as f64;
sorted[lo] + (sorted[hi] - sorted[lo]) * frac
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_residuals_give_zero_band() {
let b = HorizonBands::from_residuals(Duration::minutes(15), &[vec![], vec![]], 0.1);
assert_eq!(b.at(Duration::ZERO), Some((0.0, 0.0)));
}
#[test]
fn band_widens_with_horizon() {
let near: Vec<f64> = (-2..=2).map(|x| x as f64).collect(); let far: Vec<f64> = (-20..=20).map(|x| x as f64).collect(); let b = HorizonBands::from_residuals(Duration::hours(1), &[near, far], 0.1);
let (lo0, hi0) = b.at(Duration::ZERO).unwrap();
let (lo1, hi1) = b.at(Duration::hours(1)).unwrap();
assert!(hi1 - lo1 > hi0 - lo0, "h+1 doit être plus large que h+0");
}
#[test]
fn index_is_clamped_to_last_horizon() {
let b = HorizonBands::from_residuals(Duration::hours(1), &[vec![0.0], vec![5.0]], 0.1);
assert_eq!(b.at(Duration::hours(99)), b.at(Duration::hours(1)));
}
#[test]
fn band_quantiles_bracket_the_error() {
let errors: Vec<f64> = (-10..=10).map(|x| x as f64).collect();
let b = HorizonBands::from_residuals(Duration::hours(1), &[errors], 0.1);
let (low, high) = b.at(Duration::ZERO).unwrap();
assert!(low < 0.0 && high > 0.0, "low={low}, high={high}");
}
}