From 188b8dc8e70ecfa4df3aa6688c8e764269d32f85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ko=C5=82aczkowski?= Date: Tue, 13 Aug 2024 14:28:09 +0200 Subject: [PATCH] Fix panic on unwrapping None --- src/stats/timeseries.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/stats/timeseries.rs b/src/stats/timeseries.rs index 4f481b2..3d59169 100644 --- a/src/stats/timeseries.rs +++ b/src/stats/timeseries.rs @@ -83,7 +83,8 @@ impl TimeSeriesStats { } pub fn effective_sample_size(&self) -> u64 { - if self.n <= 1 { + // variances and means must be defined and level-0 must exist + if self.n < 2 { return self.n; } @@ -96,6 +97,8 @@ impl TimeSeriesStats { // - the number of batches should be also large enough for the variance // of the mean be accurate let sample_variance = self.levels[0].stats.variance(); + assert!(!sample_variance.is_nan()); + let autocorrelation_time = self .levels .iter() @@ -107,8 +110,7 @@ impl TimeSeriesStats { }) .take_while(|(batch_len, time)| *time > 0.2 * *batch_len as f64) .map(|(_, time)| time) - .reduce(f64::max) - .unwrap(); + .fold(1.0, f64::max); (self.n as f64 / autocorrelation_time) as u64 }