1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
//! single_vector module provides a set of functions for performing statistical calculations on vectors of 128-bit signed integers and 64-bit floating-point numbers.
//!
//! The module includes functions for calculating the following statistics:
//! - Sum
//! - Product
//! - Mean
//! - Median
//! - Mode
//! - Range
//! - Interquartile Range
//! - Variance
//! - Standard Deviation
//! - Quartiles (Q1, Q2, and Q3)
//!
pub mod single_vector {
/// Returns the sum of all elements in a vector of 128-bit signed integers.
///
/// # Arguments
///
/// * `vector` - A vector of signed 128-bit integers
///
/// # Returns
///
/// * i128 - The sum of all elements in the input vector
pub fn vector_sum(vector: Vec<i128>) -> i128 {
vector.iter().sum()
}
/// Computes the sum of all elements in a given vector of floating-point numbers.
///
/// # Arguments
///
/// * `vector` - A vector of 64-bit floating-point numbers
///
/// # Returns
///
/// * f64 - The sum of all elements in the input vector
pub fn vector_sum_float(vector: Vec<f64>) -> f64 {
vector.iter().sum()
}
/// Returns the product of all elements in a vector of 128-bit signed integers.
///
/// # Arguments
///
/// * `vector` - A vector of signed 128-bit integers
///
/// # Returns
///
/// * i128 - The product of all elements in the input vector
pub fn vector_product(vector: Vec<i128>) -> i128 {
vector.iter().fold(1, |acc, &x| acc * x)
}
/// Computes the product of all elements in a given vector of floating-point numbers.
///
/// # Arguments
///
/// * `vector` - A vector of 64-bit floating-point numbers
///
/// # Returns
///
/// * f64 - The product of all elements in the input vector
pub fn vector_product_float(vector: Vec<f64>) -> f64 {
vector.iter().fold(1.0, |acc, &x| acc * x)
}
/// Calculates the mean of a given vector of 128-bit signed integers.
///
/// # Arguments
///
/// * `vector` - A vector of signed 128-bit integers
///
/// # Returns
///
/// * i128 - The mean of all elements in the input vector
pub fn vector_mean(vector: Vec<i128>) -> i128 {
vector_sum(vector.clone()) / vector.len() as i128
}
/// Calculates the mean of a given vector of 64-bit floating-point numbers.
///
/// # Arguments
///
/// * `vector` - A vector of 64-bit floating-point numbers
///
/// # Returns
///
/// * f64 - The mean of all elements in the input vector
pub fn vector_mean_float(vector: Vec<f64>) -> f64 {
vector_sum_float(vector.clone()) / vector.len() as f64
}
/// Calculates the median value of a vector containing 128-bit signed integers.
///
/// # Arguments
///
/// * `vector` - A vector of signed 128-bit integers
///
/// # Returns
///
/// * i128 - The median value of the input vector
pub fn vector_median(vector: Vec<i128>) -> i128 {
let mut sorted_vector = vector.clone();
sorted_vector.sort();
if sorted_vector.len() % 2 == 0 {
(sorted_vector[sorted_vector.len() / 2] + sorted_vector[sorted_vector.len() / 2 - 1]) / 2
} else {
sorted_vector[sorted_vector.len() / 2]
}
}
/// Calculates the median value of a given vector of 64-bit floating-point numbers.
///
/// # Arguments
///
/// * `vector` - A vector of 64-bit floating-point numbers
///
/// # Returns
///
/// * f64 - The median value of the input vector
pub fn vector_median_float(vector: Vec<f64>) -> f64 {
let mut sorted_vector = vector.clone();
sorted_vector.sort_by(|a, b| a.partial_cmp(b).unwrap());
if sorted_vector.len() % 2 == 0 {
(sorted_vector[sorted_vector.len() / 2] + sorted_vector[sorted_vector.len() / 2 - 1]) / 2.0
} else {
sorted_vector[sorted_vector.len() / 2]
}
}
/// Returns the mode of all elements in a vector
use std::collections::HashMap;
pub fn vector_mode(vector: Vec<i128>) -> i128 {
let mut counts = HashMap::new();
for &number in vector.iter() {
let count = counts.entry(number).or_insert(0);
*count += 1;
}
counts.into_iter()
.max_by_key(|&(_key, value)| value)
.map(|(key, _value)| key)
.unwrap_or(0)
}
/// Returns the mode of all elements in a vector of floating points.
use ordered_float::OrderedFloat;
pub fn vector_mode_float(vector: Vec<f64>) -> f64 {
let mut counts: HashMap<OrderedFloat<f64>, usize> = HashMap::new();
let epsilon = 1e-9; // Adjust this value according to your desired precision
for &number in vector.iter() {
let key = counts
.keys()
.find(|&key| ((**key) - number).abs() < epsilon)
.cloned();
match key {
Some(existing_key) => {
let count = counts.get_mut(&existing_key).unwrap();
*count += 1;
}
None => {
counts.insert(OrderedFloat(number), 1);
}
}
}
counts.into_iter()
.max_by_key(|&(_key, value)| value)
.map(|(key, _value)| *key)
.unwrap_or(f64::NAN)
}
/// Calculates the range of a given vector of 128-bit signed integers, which is the difference between the maximum and
/// minimum values in the vector.
///
/// # Arguments
///
/// * `vector` - A vector of signed 128-bit integers
///
/// # Returns
///
/// * i128 - The range of the input vector, calculated as the difference between the maximum and minimum values
pub fn vector_range(vector: Vec<i128>) -> i128 {
let mut sorted = vector.clone();
sorted.sort();
sorted[sorted.len() - 1] - sorted[0]
}
/// Calculates the range of a given vector of 64-bit floating-point numbers, which is the difference between the maximum and
/// minimum values in the vector.
///
/// # Arguments
///
/// * `vector` - A vector of 64-bit floating-point numbers
///
/// # Returns
///
/// * f64 - The range of the input vector, calculated as the difference between the maximum and minimum values
pub fn vector_range_float(vector: Vec<f64>) -> f64 {
let mut sorted = vector.clone();
sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());
sorted[sorted.len() - 1] - sorted[0]
}
/// Computes the interquartile range (IQR) of a given vector of 128-bit signed integers.
///
/// # Arguments
///
/// * `vector` - A vector of signed 128-bit integers
///
/// # Returns
///
/// * i128 - The interquartile range of the input vector, calculated as the difference between the third (Q3) and first (Q1) quartiles
pub fn vector_interquartile_range(vector: Vec<i128>) -> i128 {
let mut sorted = vector.clone();
sorted.sort();
let q1 = vector_median(sorted[0..sorted.len() / 2].to_vec());
let q3 = vector_median(sorted[sorted.len() / 2..sorted.len()].to_vec());
q3 - q1
}
/// Computes the interquartile range (IQR) of a given vector of 64-bit floating-point numbers.
///
/// # Arguments
///
/// * `vector` - A vector of 64-bit floating-point numbers
///
/// # Returns
///
/// * f64 - The interquartile range of the input vector, calculated as the difference between the third (Q3) and first (Q1) quartiles
pub fn vector_interquartile_range_float(vector: Vec<f64>) -> f64 {
let mut sorted = vector.clone();
sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());
let q1 = vector_median_float(sorted[0..sorted.len() / 2].to_vec());
let q3 = vector_median_float(sorted[sorted.len() / 2..sorted.len()].to_vec());
q3 - q1
}
/// Calculates the variance of a given vector of 128-bit signed integers.
///
/// # Arguments
///
/// * `vector` - A vector of signed 128-bit integers
///
/// # Returns
///
/// * String - The variance of the input vector, formatted as a string with two decimal places
pub fn vector_variance(vector: Vec<i128>) -> String {
let mean = vector_mean(vector.clone());
let mut sum = 0;
for i in 0..vector.len() {
sum += (vector[i] - mean).pow(2);
}
format!("{:.2}", sum as f64 / vector.len() as f64)
}
/// Calculates the variance of a given vector of 64-bit floating-point numbers.
///
/// # Arguments
///
/// * `vector` - A vector of 64-bit floating-point numbers
///
/// # Returns
///
/// * f64 - The variance of the input vector
pub fn vector_variance_float(vector: Vec<f64>) -> f64 {
let mean = vector_mean_float(vector.clone());
let mut sum = 0.0;
for i in 0..vector.len() {
sum += (vector[i] - mean).powf(2.0);
}
sum / vector.len() as f64
}
/// Calculates the standard deviation of a given vector of 128-bit signed integers.
///
/// # Arguments
///
/// * `vector` - A vector of signed 128-bit integers
///
/// # Returns
///
/// * String - The standard deviation of the input vector, formatted as a string with two decimal places
pub fn vector_standard_deviation(vector: Vec<i128>) -> String {
let variance = vector_variance(vector.clone());
format!("{:.2}", variance.parse::<f64>().unwrap().sqrt())
}
/// Calculates the standard deviation of a given vector of 64-bit floating-point numbers.
///
/// # Arguments
///
/// * `vector` - A vector of 64-bit floating-point numbers
///
/// # Returns
///
/// * f64 - The standard deviation of the input vector
pub fn vector_standard_deviation_float(vector: Vec<f64>) -> f64 {
let variance = vector_variance_float(vector.clone());
variance.sqrt()
}
/// Computes the first (Q1), second (Q2), and third (Q3) quartiles of a given vector of 128-bit signed integers.
///
/// # Arguments
///
/// * `vector` - A vector of signed 128-bit integers
///
/// # Returns
///
/// * String - A formatted string displaying the first, second, and third quartiles as "Q1: {}, Q2: {}, Q3: {}"
pub fn vector_quartiles(vector: Vec<i128>) -> String {
let mut sorted = vector.clone();
sorted.sort();
let q1 = sorted[sorted.len() / 4];
let q2 = sorted[sorted.len() / 2];
let q3 = sorted[sorted.len() * 3 / 4];
format!("Q1: {}, Q2: {}, Q3: {}", q1, q2, q3)
}
/// Returns a formatted string containing the first (Q1), second (Q2), and third (Q3) quartiles of a given vector of 64-bit
/// floating-point numbers.
///
/// # Arguments
///
/// * `vector` - A vector of 64-bit floating-point numbers
///
/// # Returns
///
/// * String - A formatted string containing the calculated Q1, Q2, and Q3 quartiles, in the format "Q1: {Q1}, Q2: {Q2}, Q3: {Q3}"
pub fn vector_quartiles_float(vector: Vec<f64>) -> String {
let mut sorted = vector.clone();
sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());
let q1 = sorted[sorted.len() / 4];
let q2 = sorted[sorted.len() / 2];
let q3 = sorted[sorted.len() * 3 / 4];
format!("Q1: {}, Q2: {}, Q3: {}", q1, q2, q3)
}
}