Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import collections
- import random
- benford_percentages = {1: 0.334, 2: 0.185, 3: 0.124,
- 4: 0.075, 5: 0.071, 6: 0.065, 7: 0.055, 8: 0.049, 9: 0.042}
- def process(data):
- results = []
- first_digits_list = list(map(lambda n: str(n)[0], data))
- first_digit_frequencies = collections.Counter(first_digits_list)
- for n in range(1, 10):
- data_frequency = first_digit_frequencies[str(n)]
- data_frequency_percent = data_frequency / len(data) * 100
- benford_frequency = len(data) * benford_percentages[n]
- benford_frequency_percent = benford_percentages[n] * 100
- deviation = abs(data_frequency - benford_frequency)
- deviation_percent = abs(
- data_frequency_percent - benford_frequency_percent)
- results.append({"n": n,
- "dfp": data_frequency_percent,
- "bfp": benford_frequency_percent,
- "dp": deviation_percent})
- width = 36
- print("-" * width)
- print("| n | Data | Benford | Deviation |")
- print("-" * width)
- for item in results:
- print("| {} | {:6.2f} | {:6.2f} | {:6.2f} |".format(item["n"],
- item["dfp"],
- item["bfp"],
- item["dp"]))
- print("-" * width)
- random_data = [0] * 1000
- random_data = list(map(lambda n: n + random.randint(1, 1000), random_data))
- process(random_data)
Advertisement
Add Comment
Please, Sign In to add comment