Crate benford

source ·
Expand description

This crate provides a way to test of 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())

Structs

Extracts the first digit of a number.

Enums

represents all digits that are Benford’s law copes with.

Traits

Maps a number to a Digit.