-
Notifications
You must be signed in to change notification settings - Fork 43
consensus metrics: avoid Histogram in favor of Welford stats
#699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| use num_traits::NumCast; | ||
|
|
||
| /// Welford's online algorithm for computing running mean, variance, and standard deviation. | ||
| #[derive(Debug, Clone, Default)] | ||
| pub(crate) struct WelfordStats { | ||
| /// Number of samples added. | ||
| count: u64, | ||
| /// Running mean, updated incrementally with each sample. | ||
| mean: f64, | ||
| /// Sum of squared differences from the current mean (used to compute variance). | ||
| m2: f64, | ||
| /// Maximum value seen. | ||
| max: u64, | ||
| } | ||
|
|
||
| impl WelfordStats { | ||
| /// Adds a sample and updates all running statistics. | ||
| pub(crate) fn add_sample(&mut self, value: u64) { | ||
| self.count = self.count.checked_add(1).unwrap(); | ||
| let v = value as f64; | ||
| let d = v - self.mean; | ||
| self.mean += d / self.count as f64; | ||
| self.m2 += d * (v - self.mean); | ||
| self.max = self.max.max(value); | ||
| } | ||
|
|
||
| /// Returns the number of samples added. | ||
| pub(crate) fn count(&self) -> u64 { | ||
| self.count | ||
| } | ||
|
|
||
| /// Returns the mean, or `None` if no samples have been added. | ||
| pub(crate) fn mean<T: NumCast>(&self) -> Option<T> { | ||
| match self.count { | ||
| 0 => None, | ||
| _ => NumCast::from(self.mean), | ||
| } | ||
| } | ||
|
|
||
| /// Returns the sample standard deviation, or `None` if fewer than 2 samples. | ||
| pub(crate) fn stddev<T: NumCast>(&self) -> Option<T> { | ||
| match self.count { | ||
| 0 | 1 => None, | ||
| n => { | ||
| let var = self.m2 / n.saturating_sub(1) as f64; | ||
| NumCast::from(var.sqrt()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Returns the maximum value seen, or `None` if no samples have been added. | ||
| pub(crate) fn maximum<T: NumCast>(&self) -> Option<T> { | ||
| match self.count { | ||
| 0 => None, | ||
| _ => NumCast::from(self.max), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use {super::*, test_case::test_matrix}; | ||
|
|
||
| const EPSILON: f64 = 1e-10; | ||
|
|
||
| fn make_stats(values: &[u64]) -> WelfordStats { | ||
| let mut stats = WelfordStats::default(); | ||
| values.iter().for_each(|&v| stats.add_sample(v)); | ||
| stats | ||
| } | ||
|
|
||
| fn expected_sequential_stddev(n: u64) -> f64 { | ||
| let num = n.saturating_mul(n.saturating_add(1)); | ||
| (num as f64 / 12.0).sqrt() | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_empty_returns_none() { | ||
| let stats = WelfordStats::default(); | ||
| assert_eq!(stats.count(), 0); | ||
| assert_eq!(stats.mean::<f64>(), None); | ||
| assert_eq!(stats.stddev::<f64>(), None); | ||
| assert_eq!(stats.maximum::<u64>(), None); | ||
| } | ||
|
|
||
| #[test_matrix( | ||
| [1usize, 5, 10, 100_000], | ||
| [false, true] | ||
| )] | ||
| fn test_sample_counts(n: usize, use_sequential: bool) { | ||
| let values: Vec<u64> = if use_sequential { | ||
| (1..=n as u64).collect() | ||
| } else { | ||
| std::iter::repeat_n(42, n).collect() | ||
| }; | ||
| let stats = make_stats(&values); | ||
|
|
||
| assert_eq!(stats.count(), n as u64); | ||
| assert!(stats.mean::<f64>().is_some()); | ||
| assert!(stats.maximum::<u64>().is_some()); | ||
| assert_eq!(stats.stddev::<f64>().is_some(), n > 1); | ||
| } | ||
|
|
||
| #[test_matrix([1usize, 5, 10, 100_000])] | ||
| fn test_sequential_stats(n: usize) { | ||
| let stats = make_stats(&(1..=n as u64).collect::<Vec<_>>()); | ||
|
|
||
| let expected_mean = (n as f64 + 1.0) / 2.0; | ||
| assert!((stats.mean::<f64>().unwrap() - expected_mean).abs() < EPSILON); | ||
| assert_eq!(stats.maximum::<u64>(), Some(n as u64)); | ||
|
|
||
| if n > 1 { | ||
| let expected_stddev = expected_sequential_stddev(n as u64); | ||
| assert!((stats.stddev::<f64>().unwrap() - expected_stddev).abs() < EPSILON); | ||
| } | ||
| } | ||
|
|
||
| #[test_matrix([2usize, 5, 10, 100_000])] | ||
| fn test_constant_has_zero_stddev(n: usize) { | ||
| let stats = make_stats(&vec![999; n]); | ||
| assert_eq!(stats.mean::<i64>(), Some(999)); | ||
| assert_eq!(stats.stddev::<f64>(), Some(0.0)); | ||
| assert_eq!(stats.maximum::<u64>(), Some(999)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_numerical_stability_large_values() { | ||
| let base = 1_000_000_000_000u64; | ||
| let stats = make_stats(&[base, base + 1, base + 2, base + 3, base + 4]); | ||
|
|
||
| assert_eq!(stats.mean::<i64>(), Some((base + 2) as i64)); | ||
| assert!((stats.stddev::<f64>().unwrap() - expected_sequential_stddev(5)).abs() < EPSILON); | ||
| assert_eq!(stats.maximum::<u64>(), Some(base + 4)); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.