How to resolve the algorithm Benford's law step by step in the Racket programming language
How to resolve the algorithm Benford's law step by step in the Racket programming language
Table of Contents
Problem Statement
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
{\displaystyle d}
(
d ∈ { 1 , … , 9 }
{\displaystyle d\in {1,\ldots ,9}}
) occurs with probability 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.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Benford's law step by step in the Racket programming language
Source code in the racket programming language
#lang racket
(define (log10 n) (/ (log n) (log 10)))
(define (first-digit n)
(quotient n (expt 10 (inexact->exact (floor (log10 n))))))
(define N 10000)
(define fibs
(let loop ([n N] [a 0] [b 1])
(if (zero? n) '() (cons b (loop (sub1 n) b (+ a b))))))
(define v (make-vector 10 0))
(for ([n fibs])
(define f (first-digit n))
(vector-set! v f (add1 (vector-ref v f))))
(printf "N OBS EXP\n")
(define (pct n) (~r (* n 100.0) #:precision 1 #:min-width 4))
(for ([i (in-range 1 10)])
(printf "~a: ~a% ~a%\n" i
(pct (/ (vector-ref v i) N))
(pct (log10 (+ 1 (/ i))))))
;; Output:
;; N OBS EXP
;; 1: 30.1% 30.1%
;; 2: 17.6% 17.6%
;; 3: 12.5% 12.5%
;; 4: 9.7% 9.7%
;; 5: 7.9% 7.9%
;; 6: 6.7% 6.7%
;; 7: 5.8% 5.8%
;; 8: 5.1% 5.1%
;; 9: 4.6% 4.6%
You may also check:How to resolve the algorithm Sort numbers lexicographically step by step in the APL programming language
You may also check:How to resolve the algorithm Go Fish step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm First-class functions step by step in the PostScript programming language
You may also check:How to resolve the algorithm Jewels and stones step by step in the APL programming language
You may also check:How to resolve the algorithm Loops/Foreach step by step in the Apex programming language