benfordslaw is to test if an empirical (observed) distribution differs significantly from a theoretical (expected, Benfords) distribution.
Project description
benfordslaw
-
benfordslaw
is Python package to test if an empirical (observed) distribution differs significantly from a theoretical (expected, Benfords) distribution. The law states that in many naturally occurring collections of numbers, the leading significant digit is likely to be small. This method can be used if you want to test whether your set of numbers may be artificial (or manipulated). If a certain set of values follows Benford's Law then model's for the corresponding predicted values should also follow Benford's Law. Normal data (Unmanipulated) does trend with Benford's Law, whereas Manipulated or fraudulent data does not. -
Assumptions of the data:
- The numbers need to be random and not assigned, with no imposed minimums or maximums.
- The numbers should cover several orders of magnitude
- Dataset should preferably cover at least 1000 samples. Though Benford's law has been shown to hold true for datasets containing as few as 50 numbers.
Installation
- Install
benfordslaw
from PyPI (recommended). benfordslaw is compatible with Python 3.6+ and runs on Linux, MacOS X and Windows. - It is distributed under the MIT license.
Installation
pip install benfordslaw
- Alternatively, install benfordslaw from the GitHub source:
git clone https://github.com/erdogant/benfordslaw.git
cd benfordslaw
pip install -U .
Import benfordslaw package
from benfordslaw import benfordslaw
# Initialize
bl = benfordslaw(alpha=0.05)
# Load elections example
df = bl.import_example(data='USA')
# Extract election information.
X = df['votes'].loc[df['candidate']=='Donald Trump'].values
# Print
print(X)
# array([ 5387, 23618, 1710, ..., 16, 21, 0], dtype=int64)
# Make fit
results = bl.fit(X)
# Plot
bl.plot(title='Donald Trump')
Analyze second digit-distribution
from benfordslaw import benfordslaw
# Initialize and set to analyze the second digit postion
bl = benfordslaw(pos=2)
# USA example
df = bl.import_example(data='USA')
results = bl.fit(X)
# Plot
bl.plot(title='Donald Trump', barcolor=[0.5, 0.5, 0.5], fontsize=12, barwidth=0.4)
Analyze last digit-distribution
from benfordslaw import benfordslaw
# Initialize and set to analyze the last postion
bl = benfordslaw(pos=-1)
# USA example
df = bl.import_example(data='USA')
results = bl.fit(X)
# Plot
bl.plot(title='Donald Trump', barcolor=[0.5, 0.5, 0.5], fontsize=12, barwidth=0.4)
Analyze second last digit-distribution
from benfordslaw import benfordslaw
# Initialize and set to analyze the last postion
bl = benfordslaw(pos=-2)
# USA example
df = bl.import_example(data='USA')
results = bl.fit(X)
# Plot
bl.plot(title='Donald Trump', barcolor=[0.5, 0.5, 0.5], fontsize=12, barwidth=0.4)
Citation
Please cite benfordslaw in your publications if this is useful for your research.
References
- https://en.wikipedia.org/wiki/Benford%27s_law
- https://towardsdatascience.com/frawd-detection-using-benfords-law-python-code-9db8db474cf8
Maintainer
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for benfordslaw-1.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 81122bbd82424667a2fec52d601b65336c080ca20b619b32804cb211e9f136de |
|
MD5 | 20cf22795d0cdf0d5612bbad03e86b8e |
|
BLAKE2b-256 | c7ad20bf56127de4620818fd2eaf637815009acb36e6ec820e56a1a94308b158 |