Skip to content

Commit

Permalink
Fix panic on unwrapping None
Browse files Browse the repository at this point in the history
  • Loading branch information
pkolaczk committed Aug 13, 2024
1 parent 031d976 commit 188b8dc
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/stats/timeseries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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()
Expand All @@ -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
}
Expand Down

0 comments on commit 188b8dc

Please sign in to comment.