This repository has been archived by the owner on Nov 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Improving the encapsulation (chunking) algorithm #456
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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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 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
Binary file not shown.
This file contains 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 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 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,109 @@ | ||
//! This module holds implementations of some basic statistical properties, such as mean and standard deviation. | ||
|
||
pub(crate) fn mean(data: &[u64]) -> Option<f64> { | ||
if data.is_empty() { | ||
None | ||
} else { | ||
Some(data.iter().sum::<u64>() as f64 / data.len() as f64) | ||
} | ||
} | ||
|
||
pub(crate) fn std_deviation(data: &[u64]) -> Option<f64> { | ||
match (mean(data), data.len()) { | ||
(Some(data_mean), count) if count > 0 => { | ||
let variance = data | ||
.iter() | ||
.map(|value| { | ||
let diff = data_mean - (*value as f64); | ||
diff * diff | ||
}) | ||
.sum::<f64>() | ||
/ count as f64; | ||
Some(variance.sqrt()) | ||
} | ||
_ => None, | ||
} | ||
} | ||
|
||
//Assumed sorted | ||
pub(crate) fn median_absolute_deviation(data: &mut [u64]) -> Option<(f64, f64)> { | ||
if data.is_empty() { | ||
None | ||
} else { | ||
//Sort data | ||
//data.sort_by(|a, b| a.partial_cmp(b).unwrap()); | ||
|
||
//Find median of data | ||
let median_data: f64 = match data.len() % 2 { | ||
1 => data[data.len() / 2] as f64, | ||
_ => 0.5 * (data[data.len() / 2 - 1] + data[data.len() / 2]) as f64, | ||
}; | ||
|
||
//Absolute deviations | ||
let mut absolute_deviations = Vec::new(); | ||
for size in data { | ||
absolute_deviations.push(f64::abs(*size as f64 - median_data)) | ||
} | ||
|
||
absolute_deviations.sort_by(|a, b| a.partial_cmp(b).unwrap()); | ||
let l = absolute_deviations.len(); | ||
let mad: f64 = match l % 2 { | ||
1 => absolute_deviations[l / 2], | ||
_ => 0.5 * (absolute_deviations[l / 2 - 1] + absolute_deviations[l / 2]), | ||
}; | ||
|
||
Some((median_data, mad)) | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_mean() { | ||
assert_eq!(mean(&[]), None); | ||
for v in [0u64, 1, 5, 100] { | ||
assert_eq!(mean(&[v]), Some(v as f64)); | ||
} | ||
assert_eq!(mean(&[0, 1]), Some(0.5)); | ||
assert_eq!(mean(&[0, 5, 100]), Some(35.0)); | ||
assert_eq!(mean(&[7, 4, 30, 14]), Some(13.75)); | ||
} | ||
|
||
#[test] | ||
fn test_std_deviation() { | ||
assert_eq!(std_deviation(&[]), None); | ||
for v in [0u64, 1, 5, 100] { | ||
assert_eq!(std_deviation(&[v]), Some(0 as f64)); | ||
} | ||
assert_eq!(std_deviation(&[1, 4]), Some(1.5)); | ||
assert_eq!(std_deviation(&[2, 2, 2, 2]), Some(0.0)); | ||
assert_eq!( | ||
std_deviation(&[1, 20, 300, 4000, 50000, 600000, 7000000, 80000000]), | ||
Some(26193874.56387471) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_median_absolute_deviation() { | ||
RishabhSaini marked this conversation as resolved.
Show resolved
Hide resolved
|
||
//Assumes sorted | ||
assert_eq!(median_absolute_deviation(&mut []), None); | ||
for v in [0u64, 1, 5, 100] { | ||
assert_eq!(median_absolute_deviation(&mut [v]), Some((v as f64, 0.0))); | ||
} | ||
assert_eq!(median_absolute_deviation(&mut [1, 4]), Some((2.5, 1.5))); | ||
assert_eq!( | ||
median_absolute_deviation(&mut [2, 2, 2, 2]), | ||
Some((2.0, 0.0)) | ||
); | ||
assert_eq!( | ||
median_absolute_deviation(&mut [ | ||
1, 2, 3, 3, 4, 4, 4, 5, 5, 6, 6, 6, 7, 7, 7, 8, 9, 12, 52, 90 | ||
]), | ||
Some((6.0, 2.0)) | ||
); | ||
|
||
//if more than half of the data has the same value, MAD = 0, thus any | ||
//value different from the residual median is classified as an outlier | ||
assert_eq!( | ||
median_absolute_deviation(&mut [0, 1, 1, 1, 1, 1, 1, 1, 0]), | ||
Some((1.0, 0.0)) | ||
); | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding the semver break label for this