Skip to content

Commit

Permalink
Add Interquartile Range (TheAlgorithms#633)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrixninja authored Dec 11, 2023
1 parent f6ff829 commit 1fd579c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
* [Huber Loss](https://github.com/TheAlgorithms/Rust/blob/master/src/math/huber_loss.rs)
* [Interest](https://github.com/TheAlgorithms/Rust/blob/master/src/math/interest.rs)
* [Interpolation](https://github.com/TheAlgorithms/Rust/blob/master/src/math/interpolation.rs)
* [Interquartile Range](https://github.com/TheAlgorithms/Rust/blob/master/src/math/interquartile_range.rs)
* [Karatsuba Multiplication](https://github.com/TheAlgorithms/Rust/blob/master/src/math/karatsuba_multiplication.rs)
* [Lcm Of N Numbers](https://github.com/TheAlgorithms/Rust/blob/master/src/math/lcm_of_n_numbers.rs)
* [Leaky Relu](https://github.com/TheAlgorithms/Rust/blob/master/src/math/leaky_relu.rs)
Expand Down
85 changes: 85 additions & 0 deletions src/math/interquartile_range.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Author : cyrixninja
// Interquartile Range : An implementation of interquartile range (IQR) which is a measure of statistical
// dispersion, which is the spread of the data.
// Wikipedia Reference : https://en.wikipedia.org/wiki/Interquartile_range

use std::cmp::Ordering;

pub fn find_median(numbers: &[f64]) -> f64 {
let mut numbers = numbers.to_vec();
numbers.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal));

let length = numbers.len();
let mid = length / 2;

if length % 2 == 0 {
(numbers[mid - 1] + numbers[mid]) / 2.0
} else {
numbers[mid]
}
}

pub fn interquartile_range(numbers: &[f64]) -> f64 {
if numbers.is_empty() {
panic!("Error: The list is empty. Please provide a non-empty list.");
}

let mut numbers = numbers.to_vec();
numbers.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal));

let length = numbers.len();
let mid = length / 2;
let (q1, q3) = if length % 2 == 0 {
let first_half = &numbers[0..mid];
let second_half = &numbers[mid..length];
(find_median(first_half), find_median(second_half))
} else {
let first_half = &numbers[0..mid];
let second_half = &numbers[mid + 1..length];
(find_median(first_half), find_median(second_half))
};

q3 - q1
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_find_median() {
let numbers1 = vec![1.0, 2.0, 2.0, 3.0, 4.0];
assert_eq!(find_median(&numbers1), 2.0);

let numbers2 = vec![1.0, 2.0, 2.0, 3.0, 4.0, 4.0];
assert_eq!(find_median(&numbers2), 2.5);

let numbers3 = vec![-1.0, 2.0, 0.0, 3.0, 4.0, -4.0];
assert_eq!(find_median(&numbers3), 1.0);

let numbers4 = vec![1.1, 2.2, 2.0, 3.3, 4.4, 4.0];
assert_eq!(find_median(&numbers4), 2.75);
}

#[test]
fn test_interquartile_range() {
let numbers1 = vec![4.0, 1.0, 2.0, 3.0, 2.0];
assert_eq!(interquartile_range(&numbers1), 2.0);

let numbers2 = vec![-2.0, -7.0, -10.0, 9.0, 8.0, 4.0, -67.0, 45.0];
assert_eq!(interquartile_range(&numbers2), 17.0);

let numbers3 = vec![-2.1, -7.1, -10.1, 9.1, 8.1, 4.1, -67.1, 45.1];
assert_eq!(interquartile_range(&numbers3), 17.2);

let numbers4 = vec![0.0, 0.0, 0.0, 0.0, 0.0];
assert_eq!(interquartile_range(&numbers4), 0.0);
}

#[test]
#[should_panic(expected = "Error: The list is empty. Please provide a non-empty list.")]
fn test_interquartile_range_empty_list() {
let numbers: Vec<f64> = vec![];
interquartile_range(&numbers);
}
}
2 changes: 2 additions & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ mod greatest_common_divisor;
mod huber_loss;
mod interest;
mod interpolation;
mod interquartile_range;
mod karatsuba_multiplication;
mod lcm_of_n_numbers;
mod leaky_relu;
Expand Down Expand Up @@ -112,6 +113,7 @@ pub use self::greatest_common_divisor::{
pub use self::huber_loss::huber_loss;
pub use self::interest::{compound_interest, simple_interest};
pub use self::interpolation::{lagrange_polynomial_interpolation, linear_interpolation};
pub use self::interquartile_range::interquartile_range;
pub use self::karatsuba_multiplication::multiply;
pub use self::lcm_of_n_numbers::lcm;
pub use self::leaky_relu::leaky_relu;
Expand Down

0 comments on commit 1fd579c

Please sign in to comment.