Skip to content

Instantly share code, notes, and snippets.

@gbushnell
Last active October 1, 2020 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbushnell/4bfed60adcfeef972c3956073d205150 to your computer and use it in GitHub Desktop.
Save gbushnell/4bfed60adcfeef972c3956073d205150 to your computer and use it in GitHub Desktop.
Expected Frequency of digit occurrences in leading 2 digits per Benford's Law
# Probability of digit (d) occurring in nth position
def calc_expected_probability(d: int, n: int) -> float:
# generalization of Benford's Law ~ Summation( log[ 1 + (1/(10k+d)] ) where d = digit, k = (10^(n-2), 10^(n-1))
# source: https://en.wikipedia.org/wiki/Benford%27s_law
prob = 0
if (d == 0) and (n == 1):
prob = 0
elif n == 1:
prob = math.log10(1 + (1 / d))
else:
l_bound = 10 ** (n - 2)
u_bound = 10 ** (n - 1)
for k in range(l_bound, u_bound):
prob += math.log10(1 + (1 / (10 * k + d)))
return round(prob, 3)
# populate matrix with Benford's Law (10x2 matrix for r= digit, c=location - 1)
expected_prob_matrix = np.zeros((10, 2))
for d_i in range(expected_prob_matrix.shape[0]):
for n_i in range(expected_prob_matrix.shape[1]):
expected_prob_matrix[d_i, n_i] = calc_expected_probability(d_i, n_i + 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment