/* Benford's law (Rosetta code) in Picat. http://rosettacode.org/wiki/Benford's_law """ Benford's law, also called the first-digit law, refers to the frequency distribution of digits in many (but not all) real-life sources of data. In this distribution, the number 1 occurs as the first digit about 30% of the time, while larger numbers occur in that position less frequently: 9 as the first digit less than 5% of the time. This distribution of first digits is the same as the widths of gridlines on a logarithmic scale. Benford's law also concerns the expected distribution for digits beyond the first, which approach a uniform distribution. This result has been found to apply to a wide variety of data sets, including electricity bills, street addresses, stock prices, population numbers, death rates, lengths of rivers, physical and mathematical constants, and processes described by power laws (which are very common in nature). It tends to be most accurate when values are distributed across multiple orders of magnitude. A set of numbers is said to satisfy Benford's law if the leading digit d (d \in \{1, \ldots, 9\}) occurs with probability P(d) = \log_{10}(d+1)-\log_{10}(d) = \log_{10}\left(1+\frac{1}{d}\right) For this task, write (a) routine(s) to calculate the distribution of first significant (non-zero) digits in a collection of numbers, then display the actual vs. expected distribution in the way most convenient for your language (table / graph / histogram / whatever). Use the first 1000 numbers from the Fibonacci sequence as your data set. No need to show how the Fibonacci numbers are obtained. You can generate them or load them from a file; whichever is easiest. Display your actual vs expected distribution. For extra credit: Show the distribution for one other set of numbers from a page on Wikipedia. State which Wikipedia page it can be obtained from and what the set enumerates. Again, no need to display the actual list of numbers or the code to load them. """ Also see: https://en.wikipedia.org/wiki/Benford's_law This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import util. % import cp. main => go. go => N = 1000, Fib = [fib(I) : I in 1..N], check_benford(Fib), nl. % go2 => N =1000, println("Tribonacci:"), check_benford([tri(I) : I in 1..N]), println("Lucas numbers:"), check_benford([lucas(I) : I in 1..N]), println("Triangle numbers:"), check_benford([triangle(I) : I in 1..N]), println("Pentagonal numbers:"), check_benford([pent(I) : I in 1..N]), println("Hexagonal numbers:"), check_benford([hex(I) : I in 1..N]), println("Number of pairs:"), check_benford([pairs(I) : I in 1..N]), println("Powers of 2:"), check_benford([2**I : I in 1..N]), println("Powers of 3:"), check_benford([3**I : I in 1..N]), println("Primes <= 1_000_000:"), check_benford(primes(1_000_000)), println("Random (mod 1000):"), _ = random2(), check_benford([abs(random()) mod 1000 : _ in 1..N]), nl. % From https://en.wikipedia.org/wiki/Land_use_statistics_by_country % "Cultivated land (km^2)" % go3 => Land = [1669302, 1535063, 1504350, 1192300, 661299, 474681, 471550, 333847, 330037, 329334, 284342, 268072, 255893, 227155, 222394, 221400, 196860, 184981, 176407, 157246, 144911, 139000, 125590, 119358, 117793, 106751, 106357, 104377, 101233, 95384, 93723, 88075, 82170, 79902, 44658, 75567, 75511, 70698, 68020, 61800, 60891, 59336, 56821, 56604, 53871, 52073, 51119, 48955, 47943, 47757, 47684, 46993, 46810, 44850, 37985, 37900, 37881, 37835, 37122, 35905, 35510, 35199, 35183, 35108, 34044, 33500, 33395, 32900, 32317, 32206, 31524, 30632, 29849, 29124, 28971, 28651, 25186, 24480, 24409, 24226, 23107, 22839, 22376, 21466, 21134, 20566, 20406, 20247, 20001, 19999, 19974, 18904, 18254, 18212, 17481, 16684, 15845, 15801, 15567, 14378, 14255, 13923, 13907, 13066, 12489, 11816, 11608, 11008, 10925, 10664, 10574, 10539, 10040, 8962, 8559, 8511, 8301, 8255, 8065, 7702, 6999, 6660, 6446, 5916, 5835, 5464, 5358, 5347, 5211, 5203, 4767, 4666, 4265, 4172, 4138, 3930, 3864, 3339, 3080, 2851, 2832, 2799, 2542, 2300, 2164, 2023, 2007, 1917, 1902, 1831, 1398, 1333, 1284, 1283, 1220, 1055, 1050, 1013, 823, 733, 727, 678, 573, 552, 490, 411, 361, 249, 228, 219, 211, 180, 180, 176, 170, 156, 140, 130, 121, 118, 118, 109, 101, 100, 100, 96, 87, 83, 70, 70, 69, 64, 60, 60, 58, 56, 53, 51, 50, 41, 40, 30, 30, 30, 20, 20, 20, 17, 11, 10, 10, 10, 10, 9], check_benford(Land), nl. % % Check a list of numbers for Benford's law % check_benford(L) => Len = L.len, println(len=Len), Count = [F[1].to_integer() : Num in L, F=Num.to_string()].occurrences(), P = new_map([I=D/Len : I=D in Count]), println("Benford (percent):"), foreach(D in 1..9) B = benford(D)*100, PI = P.get(D,0)*100, Diff = abs(PI - B), printf("%d: count=%5d observed: %0.2f%% Benford: %0.2f%% diff=%0.3f\n", D,Count.get(D,0),PI,B,Diff) end, nl. benford(D) = log10(1+1/D). % % create an occurrences map of a list % occurrences(List) = Map => Map = new_map(), foreach(E in List) Map.put(E, cond(Map.has_key(E),Map.get(E)+1,1)) end. % Fibonacci table fib(0) = 0. fib(1) = 1. fib(N) = fib(N-1) + fib(N-2). % Tribonacci table tri(0) = 0. tri(1) = 1. tri(2) = 1. tri(N) = tri(N-1) + tri(N-2) + tri(N-3). % Lucas numbers lucas(N) = fib(N-1)+fib(N+1). % triangle numbers triangle(N) = N*(N+1) div 2. % pentagonal numbers pent(N) = N*(3*N-1) div 2. % hexagonal numbers hex(N) = N*(2*N-1). % number of pairs pairs(N) = N*(N-1) div 2.