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
Provenance
- Commit
c64e69f- Tree
76c146e- Author
David Peter <sharkdp@users.noreply.github.com>- Committer
David Peter <mail@david-peter.de>- Parent
e9892ea
3 files changed+165 added−210 removed
modified:src/quantity/statistics.rs+130 added-61 removed
31 unmodified lines32 Information::new::<byte>(value)33 }34 }35 36-/// A max function that assumes no NaNs and at least one element37-pub 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-}43-36 /// A min function that assumes no NaNs and at least one element37 pub 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 }43 44+/// A max function that assumes no NaNs and at least one element45+pub 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+}51+52 /// A mean function that assumes at least one element53 pub fn mean<Q, P>(values: impl IntoIterator<Item = Q>) -> Q54 where55 Q: AddAssign + num_traits::Zero + Div<Ratio, Output = P>,28 unmodified lines84 values[len / 2]85 }86 }87 88+fn standard_deviation_f64(values: &[f64]) -> f64 {89+ let mean_value = mean(values.iter().copied());90+91+ 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+ }98+99+ (1. / ((count - 1) as f64) * squared_deviations).sqrt()100+}101+102 pub 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 }107 108+/// 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>114+pub fn modified_zscores_f64(xs: &[f64]) -> Vec<f64> {115+ assert!(!xs.is_empty());116+117+ // Compute sample median:118+ let x_median = median(xs.iter().copied());119+120+ // Compute the absolute deviations from the median:121+ let deviations: Vec<f64> = xs.iter().map(|x| (x - x_median).abs()).collect();122+123+ // Compute median absolute deviation:124+ let mad = median(deviations.iter().copied());125+126+ // Handle MAD == 0 case127+ let mad = if mad > 0.0 { mad } else { f64::EPSILON };128+129+ // Compute modified Z-scores (x_i - x_median) / MAD130+ xs.iter().map(|&x| (x - x_median) / mad).collect()131+}132+133 pub 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 }137 102-#[test]103-fn 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)]139+mod tests {140+ use super::*;141+ use approx::assert_relative_eq;142+ use uom::si::information::kibibyte;143+ use uom::si::time::{microsecond, millisecond};144 111-#[test]112-fn 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]));152 116- 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+ }160 124-#[test]125-fn 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]));168 128- 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+ }176 134- 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]));181 149- 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+ }190+191+ #[test]192+ fn test_median() {193+ assert_eq!(1.0, median([1.0]));194+ assert_relative_eq!(2.0, median([1.0, 3.0]));195+196+ 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);203+204+ 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+ }213+214+ #[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 lines98 ]99 100 [[package]]101 name = "autocfg"102-version = "1.5.1"102+version = "1.4.0"103 source = "registry+https://github.com/rust-lang/crates.io-index"104-checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"104+checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"105 106 [[package]]107 name = "bitflags"108-version = "2.13.2"108+version = "2.6.0"109 source = "registry+https://github.com/rust-lang/crates.io-index"110-checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06"110+checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"111 112 [[package]]113+name = "bitvec"114+version = "1.0.1"115+source = "registry+https://github.com/rust-lang/crates.io-index"116+checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c"117+dependencies = [118+ "funty",119+ "radium",120+ "tap",121+ "wyz",122+]123+124+[[package]]125 name = "borsh"126 version = "1.8.1"127 source = "registry+https://github.com/rust-lang/crates.io-index"128 checksum = "553c5d846a6ba5150c65e3b1b8ec073bcf1abc20f9b7220de384a4443ea4e20a"96 unmodified lines225 [[package]]226 name = "clap_lex"227 version = "1.1.1"228 source = "registry+https://github.com/rust-lang/crates.io-index"217-checksum = "1c133bc6a41be0d194c306b5506d15e6feeea7b1d6604bd3f8310dfb2ca96486"229+checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6"230 231 [[package]]232 name = "colorchoice"233 version = "1.0.5"106 unmodified lines340 "num-traits",341 ]342 343 [[package]]344+name = "funty"345+version = "2.0.0"346+source = "registry+https://github.com/rust-lang/crates.io-index"347+checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c"348+349+[[package]]350 name = "getrandom"351 version = "0.2.17"352 source = "registry+https://github.com/rust-lang/crates.io-index"353 checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0"50 unmodified lines404 "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 lines522 source = "registry+https://github.com/rust-lang/crates.io-index"523 checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be"524 525 [[package]]509-name = "num"510-version = "0.2.1"511-source = "registry+https://github.com/rust-lang/crates.io-index"512-checksum = "b8536030f9fea7127f841b45bb6243b27255787fb4eb83958aa1ef9d2fdc0c36"513-dependencies = [514- "num-bigint",515- "num-complex",516- "num-integer",517- "num-iter",518- "num-rational",519- "num-traits",520-]521-522-[[package]]523-name = "num-bigint"524-version = "0.2.6"525-source = "registry+https://github.com/rust-lang/crates.io-index"526-checksum = "090c7f9998ee0ff65aa5b723e4009f7b217707f1fb5ea551329cc4d6231fb304"527-dependencies = [528- "autocfg 1.4.0",529- "num-integer",530- "num-traits",531-]532-533-[[package]]534-name = "num-complex"535-version = "0.2.4"536-source = "registry+https://github.com/rust-lang/crates.io-index"537-checksum = "b6b19411a9719e753aff12e5187b74d60d3dc449ec3f4dc21e3989c3f554bc95"538-dependencies = [539- "autocfg 1.4.0",540- "num-traits",541-]542-543-[[package]]544-name = "num-integer"545-version = "0.1.46"546-source = "registry+https://github.com/rust-lang/crates.io-index"547-checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"548-dependencies = [549- "num-traits",550-]551-552-[[package]]553-name = "num-iter"554-version = "0.1.45"555-source = "registry+https://github.com/rust-lang/crates.io-index"556-checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf"557-dependencies = [558- "autocfg 1.4.0",559- "num-integer",560- "num-traits",561-]562-563-[[package]]564-name = "num-rational"565-version = "0.2.4"566-source = "registry+https://github.com/rust-lang/crates.io-index"567-checksum = "5c000134b5dbf44adc5cb772486d335293351644b801551abe8f75c84cfa4aef"568-dependencies = [569- "autocfg 1.4.0",570- "num-bigint",571- "num-integer",572- "num-traits",573-]574-575-[[package]]526 name = "num-traits"527 version = "0.2.19"528 source = "registry+https://github.com/rust-lang/crates.io-index"529 checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"104 unmodified lines634 checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"635 636 [[package]]637 name = "rand"688-version = "0.8.8"638+version = "0.8.5"639 source = "registry+https://github.com/rust-lang/crates.io-index"690-checksum = "e058c7de0b26af77780c769414d6257830bb240f3c38477dbc2c16e5f54d6d4c"640+checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"641 dependencies = [642 "libc",693- "rand_chacha 0.3.1",694- "rand_core 0.6.4",643+ "rand_chacha",644+ "rand_core",645 ]646 647 [[package]]698-name = "rand"699-version = "0.9.5"700-source = "registry+https://github.com/rust-lang/crates.io-index"701-checksum = "b9ef1d0d795eb7d84685bca4f72f3649f064e6641543d3a8c415898726a57b41"702-dependencies = [703- "rand_chacha 0.9.0",704- "rand_core 0.9.5",705-]706-707-[[package]]708-name = "rand"709-version = "0.10.3"710-source = "registry+https://github.com/rust-lang/crates.io-index"711-checksum = "65c9fb96cbc91e3478eaae79a69fcd3f1ae4ad052e471fe6732fff548984b4af"712-dependencies = [713- "chacha20",714- "getrandom 0.4.3",715- "rand_core 0.10.1",716-]717-718-[[package]]648 name = "rand_chacha"649 version = "0.3.1"650 source = "registry+https://github.com/rust-lang/crates.io-index"651 checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"652 dependencies = [653 "ppv-lite86",725- "rand_core 0.6.4",654+ "rand_core",655 ]656 657 [[package]]729-name = "rand_chacha"730-version = "0.9.0"731-source = "registry+https://github.com/rust-lang/crates.io-index"732-checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"733-dependencies = [734- "ppv-lite86",735- "rand_core 0.9.5",736-]737-738-[[package]]658 name = "rand_core"659 version = "0.6.4"660 source = "registry+https://github.com/rust-lang/crates.io-index"661 checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"1 unmodified line663 "getrandom 0.2.17",664 ]665 666 [[package]]748-name = "rand_core"749-version = "0.9.5"750-source = "registry+https://github.com/rust-lang/crates.io-index"751-checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c"752-dependencies = [753- "getrandom 0.3.4",754-]755-756-[[package]]757-name = "rand_core"758-version = "0.10.1"759-source = "registry+https://github.com/rust-lang/crates.io-index"760-checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69"761-762-[[package]]667 name = "regex"668 version = "1.13.1"669 source = "registry+https://github.com/rust-lang/crates.io-index"670 checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d"30 unmodified lines701 "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 lines789 source = "registry+https://github.com/rust-lang/crates.io-index"790 checksum = "bbbb5d9659141646ae647b42fe094daf6c6192d1620870b449d9557f748b2daa"791 792 [[package]]889-name = "statistical"890-version = "1.0.0"891-source = "registry+https://github.com/rust-lang/crates.io-index"892-checksum = "2a8f8038e7e7969abb3f1b7c2a811225e9296da208539e0f79c5251d6cac0025"893-dependencies = [894- "vte",895-]896-897-[[package]]793 name = "strsim"794 version = "0.11.1"795 source = "registry+https://github.com/rust-lang/crates.io-index"796 checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"163 unmodified lines960 source = "registry+https://github.com/rust-lang/crates.io-index"961 checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"962 963 [[package]]1069-name = "wasip2"1070-version = "1.0.4+wasi-0.2.12"964+name = "windows-sys"965+version = "0.48.0"966 source = "registry+https://github.com/rust-lang/crates.io-index"1072-checksum = "b67efb37e106e55ce722a510d6b5f9c17f083e5fc79afc2badeb12cc313d9487"967+checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"968 dependencies = [1074- "wit-bindgen",1075-]1076-1077-[[package]]1078-name = "wasm-bindgen"1079-version = "0.2.129"1080-source = "registry+https://github.com/rust-lang/crates.io-index"1081-checksum = "9bb54f33acc68fd454578d9820b0bde1a1a3d17aa17bb7b6595806d02886d409"1082-dependencies = [969 "cfg-if",970 "once_cell",971 "rustversion",972 "serde",164 unmodified linesmodified:Cargo.toml+1 added-1 removed
18 unmodified lines1920212223232425262760 unmodified lines18 unmodified lines[dependencies]colored = "3.1"# Newer versions regress ETA estimates: https://github.com/console-rs/indicatif/issues/580indicatif = "=0.17.4"csv = "1.4"csv = "1.3"serde = { version = "1.0", features = ["derive"] }serde_json = "1.0"rust_decimal = "1.43"rand = "0.10"60 unmodified lines