3 releases
0.1.2 | Jan 31, 2023 |
---|---|
0.1.1 | Jan 29, 2023 |
0.1.0 | Jan 29, 2023 |
#2160 in Algorithms
21KB
286 lines
benford
This crate provides a way to test if some given set of numbers conforms to Benford's law
Currently, only the first digit is being used. You can use your own method
of mapping a number to a digit, by implementing BenfordClass
Example 1: Fibonacci numbers are Benford ...
use benford::{BenfordTester, FirstDigitBase10, Fibonacci};
let mut tester = BenfordTester::default();
let mut fibonacci = Fibonacci::<u64>::default();
for val in fibonacci {
if val != 0 {
tester.add_sample::<FirstDigitBase10>(val.into());
}
}
assert!(tester.is_benford());
Example 2: ... but only with limited confidence
if your test set is too small
use benford::{Alpha, BenfordTester, FirstDigitBase10, Fibonacci};
let mut tester = BenfordTester::default();
let mut fibonacci = Fibonacci::<u16>::default();
for val in fibonacci {
if val != 0 {
tester.add_sample::<FirstDigitBase10>(val.into());
}
}
assert!(! tester.is_benford());
assert!(tester.is_benford_with_alpha(Alpha::Point9));
Example 3: Natural numbers are not Benford
use benford::{BenfordTester, FirstDigitBase10};
let mut tester = BenfordTester::default();
for val in 1..u16::MAX {
tester.add_sample::<FirstDigitBase10>(val.into());
}
assert!(! tester.is_benford())
License: GPL-3.0
Dependencies
~2MB
~47K SLoC