Created
September 6, 2013 00:58
-
-
Save tclarke/6458240 to your computer and use it in GitHub Desktop.
A fun little example of Benford's Law
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
def fib(a,b): | |
"Generator for the fibonacci sequence starting at position a,b" | |
while True: | |
yield b | |
a,b=b,a+b | |
def ld(v): | |
"Determine the left-most digit in v" | |
return (v%10) if (v<10) else ld(v//10) | |
def calc_hist(n,a=0,b=1): | |
"Calculate the histogram of leading digits for n numbers in the fibonacci sequence starting at position a,b" | |
hist=numpy.zeros(9, dtype="float") | |
f=fib(a,b) | |
for i in xrange(n): | |
hist[ld(f.next())-1] += 1 | |
return hist/hist.sum() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment