pub const ONE_SIGMA: f64 = 1.0; pub const TWO_SIGMA: f64 = 2.0; pub const THREE_SIGMA: f64 = 3.0;
pub const FIFTY_TO_SIGMA: f64 = 0.675;
pub const SIXTY_TO_SIGMA: f64 = 0.842;
pub const SEVENTY_TO_SIGMA: f64 = 1.036;
pub const EIGHTY_TO_SIGMA: f64 = 1.282;
pub const NINETY_TO_SIGMA: f64 = 1.645;
pub const NINETY_FIVE_TO_SIGMA: f64 = 1.960;
pub const NINETY_NINE_TO_SIGMA: f64 = 2.576;
pub fn matches_sigma_environment(data: &Vec<f64>, sigma_room: f64, to_check: f64) -> bool {
let average = average(data);
let sigma = empiric_deviation(data);
matches_custom_sigma_environment(average, sigma, sigma_room, to_check)
}
pub fn matches_custom_sigma_environment(average: f64, sigma: f64, sigma_room: f64, to_check: f64) -> bool {
((average - (sigma_room * sigma)) < to_check) && (to_check < (average + (sigma_room * sigma)))
}
pub fn average(data: &Vec<f64>) -> f64 {
let mut sum: f64 = 0.0;
for i in data {
sum += *i;
}
return sum / data.len() as f64;
}
pub fn variance(data: &Vec<f64>) -> f64 {
let average = average(data);
let mut variance = 0.0;
for i in data {
let difference: f64 = *i - average;
variance += difference * difference;
}
return variance / data.len() as f64;
}
pub fn empiric_deviation(data: &Vec<f64>) -> f64 {
return variance(data).sqrt();
}
#[cfg(test)]
mod tests {
#[test]
fn test_average() {
let data = vec![1.0, 2.0, 3.0, 4.0, 2.0];
assert_eq!(::average(&data), 2.4);
}
#[test]
fn test_variance() {
let data = vec![1.0, 2.0, 3.0, 4.0, 2.0];
assert_eq!(::variance(&data), 1.04);
}
#[test]
fn test_empiric_deviation() {
let data = vec![1.0, 2.0, 3.0, 4.0, 2.0];
assert_eq!(::empiric_deviation(&data), 1.019803902718557);
}
#[test]
fn test_sigma_environment() {
let data = vec![1.0, 2.0, 3.0, 4.0, 2.0];
assert_eq!(::matches_sigma_environment(&data, ::ONE_SIGMA, 3.4), true);
assert_eq!(::matches_sigma_environment(&data, ::ONE_SIGMA, 1.4), true);
assert_eq!(::matches_sigma_environment(&data, ::ONE_SIGMA, 5.0), false);
assert_eq!(::matches_sigma_environment(&data, ::TWO_SIGMA, 3.4), true);
assert_eq!(::matches_sigma_environment(&data, ::TWO_SIGMA, 5.0), false);
}
#[test]
fn test_custom_sigma_environment() {
let sigma = 15.0;
let average = 100.0;
assert_eq!(::matches_custom_sigma_environment(average, sigma, ::ONE_SIGMA, 110.0), true);
assert_eq!(::matches_custom_sigma_environment(average, sigma, ::ONE_SIGMA, 120.0), false);
}
}