Skip to content
Myr

Remove statistical dependency

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Peter · committed by David Peter · parent e9892ea · Browse files · 3 files+165 added−210 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+165 added−210 removed

  • other:+165 added−210 removed

3 files+165 added−210 removed

modified:src/quantity/statistics.rs+130 added-61 removed
31 unmodified lines
32 Information::new::<byte>(value)33 }34}3536/// A max function that assumes no NaNs and at least one element37pub fn max<Q: PartialOrd>(values: impl IntoIterator<Item = Q>) -> Q {38 values39 .into_iter()40 .max_by(|a, b| a.partial_cmp(b).expect("No NaN values"))41 .expect("'max' requires at least one element")42}4336/// A min function that assumes no NaNs and at least one element37pub fn min<Q: PartialOrd>(values: impl IntoIterator<Item = Q>) -> Q {38 values39 .into_iter()40 .min_by(|a, b| a.partial_cmp(b).expect("No NaN values"))41 .expect("'min' requires at least one element")42}4344/// A max function that assumes no NaNs and at least one element45pub fn max<Q: PartialOrd>(values: impl IntoIterator<Item = Q>) -> Q {46 values47 .into_iter()48 .max_by(|a, b| a.partial_cmp(b).expect("No NaN values"))49 .expect("'max' requires at least one element")50}5152/// A mean function that assumes at least one element53pub fn mean<Q, P>(values: impl IntoIterator<Item = Q>) -> Q54where55 Q: AddAssign + num_traits::Zero + Div<Ratio, Output = P>,
28 unmodified lines
84 values[len / 2]85 }86}8788fn standard_deviation_f64(values: &[f64]) -> f64 {89 let mean_value = mean(values.iter().copied());9091 let mut squared_deviations = 0.;92 let mut count = 0;93 for value in values {94 let deviation = value - mean_value;95 squared_deviations += deviation * deviation;96 count += 1;97 }9899 (1. / ((count - 1) as f64) * squared_deviations).sqrt()100}101102pub fn standard_deviation<Q: UnsafeRawValue>(values: &[Q]) -> Q {103 let values: Vec<_> = values.iter().map(|q| q.unsafe_raw_value()).collect();90 let result_value = {91 let mean_value = statistical::mean(&values);92 statistical::standard_deviation(&values, Some(mean_value))93 };94 Q::unsafe_from_raw_value(result_value)104 let result = standard_deviation_f64(&values);105 Q::unsafe_from_raw_value(result)106}107108/// Compute modifized Z-scores for a given sample. A (unmodified) Z-score is defined by109/// `(x_i - x_mean)/x_stddev` whereas the modified Z-score is defined by `(x_i - x_median)/MAD`110/// where MAD is the median absolute deviation.111///112/// References:113/// - <https://en.wikipedia.org/wiki/Median_absolute_deviation>114pub fn modified_zscores_f64(xs: &[f64]) -> Vec<f64> {115 assert!(!xs.is_empty());116117 // Compute sample median:118 let x_median = median(xs.iter().copied());119120 // Compute the absolute deviations from the median:121 let deviations: Vec<f64> = xs.iter().map(|x| (x - x_median).abs()).collect();122123 // Compute median absolute deviation:124 let mad = median(deviations.iter().copied());125126 // Handle MAD == 0 case127 let mad = if mad > 0.0 { mad } else { f64::EPSILON };128129 // Compute modified Z-scores (x_i - x_median) / MAD130 xs.iter().map(|&x| (x - x_median) / mad).collect()131}132133pub fn modified_zscores<Q: UnsafeRawValue>(values: &[Q]) -> Vec<f64> {134 let values: Vec<_> = values.iter().map(|q| q.unsafe_raw_value()).collect();99 crate::outlier_detection::modified_zscores(&values)135 modified_zscores_f64(&values)136}137102#[test]103fn test_max() {104 assert_eq!(1.0, max([1.0]));105 assert_eq!(-1.0, max([-1.0]));106 assert_eq!(-1.0, max([-2.0, -1.0]));107 assert_eq!(1.0, max([-1.0, 1.0]));108 assert_eq!(1.0, max([-1.0, 1.0, 0.0]));109}138#[cfg(test)]139mod tests {140 use super::*;141 use approx::assert_relative_eq;142 use uom::si::information::kibibyte;143 use uom::si::time::{microsecond, millisecond};144111#[test]112fn test_mean() {113 use crate::quantity::{FormatQuantity, TimeUnit};114 use uom::si::time::millisecond;145 #[test]146 fn test_min() {147 assert_eq!(1.0, min([1.0]));148 assert_eq!(-1.0, min([-1.0]));149 assert_eq!(-2.0, min([-2.0, -1.0]));150 assert_eq!(-1.0, min([-1.0, 1.0]));151 assert_eq!(-1.0, min([1.0, -1.0, 0.0]));152116 let values = vec![117 Time::new::<millisecond>(123.4),118 Time::new::<millisecond>(234.5),119 ];120 let result = mean(values.into_iter());121 assert_eq!(result.format(TimeUnit::MilliSecond), "178.9 ms");122}153 let values = vec![154 Information::new::<kibibyte>(1.0),155 Information::new::<byte>(2.0),156 Information::new::<byte>(3.0),157 ];158 assert_eq!(min(&values).get::<byte>(), 2.0);159 }160124#[test]125fn statistics() {126 use crate::quantity::{FormatQuantity, InformationUnit, TimeUnit};161 #[test]162 fn test_max() {163 assert_eq!(1.0, max([1.0]));164 assert_eq!(-1.0, max([-1.0]));165 assert_eq!(-1.0, max([-2.0, -1.0]));166 assert_eq!(1.0, max([-1.0, 1.0]));167 assert_eq!(1.0, max([-1.0, 1.0, 0.0]));168128 let values = vec![129 Time::new::<second>(1.0),130 Time::new::<second>(2.0),131 Time::new::<second>(3.0),132 ];169 let values = vec![170 Information::new::<byte>(1.0),171 Information::new::<kibibyte>(2.0),172 Information::new::<byte>(3.0),173 ];174 assert_eq!(max(&values).get::<kibibyte>(), 2.0);175 }176134 assert_eq!(135 mean(values.iter().copied()).format(TimeUnit::Second),136 "2.000 s"137 );138 assert_eq!(139 median(values.iter().copied()).format(TimeUnit::Second),140 "2.000 s"141 );142 assert_eq!(min(&values).format(TimeUnit::Second), "1.000 s");143 assert_eq!(max(&values).format(TimeUnit::Second), "3.000 s");144 assert_eq!(145 standard_deviation(&values).format(TimeUnit::Second),146 "1.000 s"147 );177 #[test]178 fn test_mean() {179 assert_eq!(1.0, mean([1.0]));180 assert_relative_eq!(2.0, mean([1.0, 3.0]));181149 let values = vec![150 Information::new::<byte>(1.0),151 Information::new::<byte>(2.0),152 Information::new::<byte>(3.0),153 ];154 mean(values.iter().copied()).format(InformationUnit::Byte);182 let values = [183 Time::new::<millisecond>(100.0),184 Time::new::<millisecond>(200.0),185 Time::new::<microsecond>(600_000.0),186 ];187 let result = mean(values);188 assert_relative_eq!(result.get::<millisecond>(), 300.0);189 }190191 #[test]192 fn test_median() {193 assert_eq!(1.0, median([1.0]));194 assert_relative_eq!(2.0, median([1.0, 3.0]));195196 let values = [197 Time::new::<millisecond>(100.0),198 Time::new::<millisecond>(200.0),199 Time::new::<microsecond>(600_000.0),200 ];201 let result = median(values);202 assert_relative_eq!(result.get::<millisecond>(), 200.0);203204 let values = [205 Time::new::<millisecond>(100.0),206 Time::new::<millisecond>(200.0),207 Time::new::<microsecond>(300_000.0),208 Time::new::<microsecond>(600_000.0),209 ];210 let result = median(values);211 assert_relative_eq!(result.get::<millisecond>(), 250.0);212 }213214 #[test]215 fn test_standard_deviation() {216 let values = [217 Time::new::<millisecond>(100.0),218 Time::new::<millisecond>(200.0),219 Time::new::<microsecond>(300_000.0),220 ];221 let result = standard_deviation(&values);222 assert_relative_eq!(result.get::<millisecond>(), 100.0);223 }224}
modified:Cargo.lock+34 added-148 removed
97 unmodified lines
98]99100[[package]]101name = "autocfg"102version = "1.5.1"102version = "1.4.0"103source = "registry+https://github.com/rust-lang/crates.io-index"104checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"104checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"105106[[package]]107name = "bitflags"108version = "2.13.2"108version = "2.6.0"109source = "registry+https://github.com/rust-lang/crates.io-index"110checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06"110checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"111112[[package]]113name = "bitvec"114version = "1.0.1"115source = "registry+https://github.com/rust-lang/crates.io-index"116checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c"117dependencies = [118 "funty",119 "radium",120 "tap",121 "wyz",122]123124[[package]]125name = "borsh"126version = "1.8.1"127source = "registry+https://github.com/rust-lang/crates.io-index"128checksum = "553c5d846a6ba5150c65e3b1b8ec073bcf1abc20f9b7220de384a4443ea4e20a"
96 unmodified lines
225[[package]]226name = "clap_lex"227version = "1.1.1"228source = "registry+https://github.com/rust-lang/crates.io-index"217checksum = "1c133bc6a41be0d194c306b5506d15e6feeea7b1d6604bd3f8310dfb2ca96486"229checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6"230231[[package]]232name = "colorchoice"233version = "1.0.5"
106 unmodified lines
340 "num-traits",341]342343[[package]]344name = "funty"345version = "2.0.0"346source = "registry+https://github.com/rust-lang/crates.io-index"347checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c"348349[[package]]350name = "getrandom"351version = "0.2.17"352source = "registry+https://github.com/rust-lang/crates.io-index"353checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0"
50 unmodified lines
404 "libc",405 "nix",406 "once_cell",407 "predicates",390 "rand 0.10.3",408 "rand",409 "rust_decimal",410 "serde",411 "serde_json",412 "shell-words",395 "statistical",413 "tempfile",414 "thiserror",415 "uom",416 "windows-sys 0.59.0",
105 unmodified lines
522source = "registry+https://github.com/rust-lang/crates.io-index"523checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be"524525[[package]]509name = "num"510version = "0.2.1"511source = "registry+https://github.com/rust-lang/crates.io-index"512checksum = "b8536030f9fea7127f841b45bb6243b27255787fb4eb83958aa1ef9d2fdc0c36"513dependencies = [514 "num-bigint",515 "num-complex",516 "num-integer",517 "num-iter",518 "num-rational",519 "num-traits",520]521522[[package]]523name = "num-bigint"524version = "0.2.6"525source = "registry+https://github.com/rust-lang/crates.io-index"526checksum = "090c7f9998ee0ff65aa5b723e4009f7b217707f1fb5ea551329cc4d6231fb304"527dependencies = [528 "autocfg 1.4.0",529 "num-integer",530 "num-traits",531]532533[[package]]534name = "num-complex"535version = "0.2.4"536source = "registry+https://github.com/rust-lang/crates.io-index"537checksum = "b6b19411a9719e753aff12e5187b74d60d3dc449ec3f4dc21e3989c3f554bc95"538dependencies = [539 "autocfg 1.4.0",540 "num-traits",541]542543[[package]]544name = "num-integer"545version = "0.1.46"546source = "registry+https://github.com/rust-lang/crates.io-index"547checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"548dependencies = [549 "num-traits",550]551552[[package]]553name = "num-iter"554version = "0.1.45"555source = "registry+https://github.com/rust-lang/crates.io-index"556checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf"557dependencies = [558 "autocfg 1.4.0",559 "num-integer",560 "num-traits",561]562563[[package]]564name = "num-rational"565version = "0.2.4"566source = "registry+https://github.com/rust-lang/crates.io-index"567checksum = "5c000134b5dbf44adc5cb772486d335293351644b801551abe8f75c84cfa4aef"568dependencies = [569 "autocfg 1.4.0",570 "num-bigint",571 "num-integer",572 "num-traits",573]574575[[package]]526name = "num-traits"527version = "0.2.19"528source = "registry+https://github.com/rust-lang/crates.io-index"529checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
104 unmodified lines
634checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"635636[[package]]637name = "rand"688version = "0.8.8"638version = "0.8.5"639source = "registry+https://github.com/rust-lang/crates.io-index"690checksum = "e058c7de0b26af77780c769414d6257830bb240f3c38477dbc2c16e5f54d6d4c"640checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"641dependencies = [642 "libc",693 "rand_chacha 0.3.1",694 "rand_core 0.6.4",643 "rand_chacha",644 "rand_core",645]646647[[package]]698name = "rand"699version = "0.9.5"700source = "registry+https://github.com/rust-lang/crates.io-index"701checksum = "b9ef1d0d795eb7d84685bca4f72f3649f064e6641543d3a8c415898726a57b41"702dependencies = [703 "rand_chacha 0.9.0",704 "rand_core 0.9.5",705]706707[[package]]708name = "rand"709version = "0.10.3"710source = "registry+https://github.com/rust-lang/crates.io-index"711checksum = "65c9fb96cbc91e3478eaae79a69fcd3f1ae4ad052e471fe6732fff548984b4af"712dependencies = [713 "chacha20",714 "getrandom 0.4.3",715 "rand_core 0.10.1",716]717718[[package]]648name = "rand_chacha"649version = "0.3.1"650source = "registry+https://github.com/rust-lang/crates.io-index"651checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"652dependencies = [653 "ppv-lite86",725 "rand_core 0.6.4",654 "rand_core",655]656657[[package]]729name = "rand_chacha"730version = "0.9.0"731source = "registry+https://github.com/rust-lang/crates.io-index"732checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"733dependencies = [734 "ppv-lite86",735 "rand_core 0.9.5",736]737738[[package]]658name = "rand_core"659version = "0.6.4"660source = "registry+https://github.com/rust-lang/crates.io-index"661checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
1 unmodified line
663 "getrandom 0.2.17",664]665666[[package]]748name = "rand_core"749version = "0.9.5"750source = "registry+https://github.com/rust-lang/crates.io-index"751checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c"752dependencies = [753 "getrandom 0.3.4",754]755756[[package]]757name = "rand_core"758version = "0.10.1"759source = "registry+https://github.com/rust-lang/crates.io-index"760checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69"761762[[package]]667name = "regex"668version = "1.13.1"669source = "registry+https://github.com/rust-lang/crates.io-index"670checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d"
30 unmodified lines
701 "arrayvec",702 "borsh",703 "bytes",704 "num-traits",801 "rand 0.8.8",802 "rand 0.9.5",705 "rand",706 "rkyv",707 "serde",708 "serde_json",709 "wasm-bindgen",710]
78 unmodified lines
789source = "registry+https://github.com/rust-lang/crates.io-index"790checksum = "bbbb5d9659141646ae647b42fe094daf6c6192d1620870b449d9557f748b2daa"791792[[package]]889name = "statistical"890version = "1.0.0"891source = "registry+https://github.com/rust-lang/crates.io-index"892checksum = "2a8f8038e7e7969abb3f1b7c2a811225e9296da208539e0f79c5251d6cac0025"893dependencies = [894 "vte",895]896897[[package]]793name = "strsim"794version = "0.11.1"795source = "registry+https://github.com/rust-lang/crates.io-index"796checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
163 unmodified lines
960source = "registry+https://github.com/rust-lang/crates.io-index"961checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"962963[[package]]1069name = "wasip2"1070version = "1.0.4+wasi-0.2.12"964name = "windows-sys"965version = "0.48.0"966source = "registry+https://github.com/rust-lang/crates.io-index"1072checksum = "b67efb37e106e55ce722a510d6b5f9c17f083e5fc79afc2badeb12cc313d9487"967checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"968dependencies = [1074 "wit-bindgen",1075]10761077[[package]]1078name = "wasm-bindgen"1079version = "0.2.129"1080source = "registry+https://github.com/rust-lang/crates.io-index"1081checksum = "9bb54f33acc68fd454578d9820b0bde1a1a3d17aa17bb7b6595806d02886d409"1082dependencies = [969 "cfg-if",970 "once_cell",971 "rustversion",972 "serde",
164 unmodified lines
modified:Cargo.toml+1 added-1 removed