How to resolve the algorithm Benford's law step by step in the Oberon-2 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Benford's law step by step in the Oberon-2 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 Oberon-2 programming language

Source code in the oberon-2 programming language

MODULE BenfordLaw;
IMPORT 
  LRealStr,
  LRealMath,
  Out := NPCT:Console;

VAR
  r: ARRAY 1000 OF LONGREAL;
  d: ARRAY 10 OF LONGINT;
  a: LONGREAL;
  i: LONGINT;

PROCEDURE Fibb(VAR r: ARRAY OF LONGREAL);
VAR
  i: LONGINT;
BEGIN
  r[0] := 1.0;r[1] := 1.0;
  FOR i := 2 TO LEN(r) - 1 DO
    r[i] := r[i - 2] + r[i - 1]
  END
END Fibb;

PROCEDURE Dist(r [NO_COPY]: ARRAY OF LONGREAL; VAR d: ARRAY OF LONGINT);
VAR
  i: LONGINT;
  str: ARRAY 256 OF CHAR;
BEGIN
  FOR i := 0 TO LEN(r) - 1 DO
    LRealStr.RealToStr(r[i],str);
    INC(d[ORD(str[0]) - ORD('0')])
  END
END Dist;

BEGIN 
  Fibb(r);
  Dist(r,d);
  Out.String("First 1000 fibonacci numbers: ");Out.Ln;
  Out.String(" digit ");Out.String(" observed ");Out.String(" predicted ");Out.Ln;
  FOR i := 1 TO LEN(d) - 1 DO
    a := LRealMath.ln(1.0 + 1.0 / i ) / LRealMath.ln(10);
    Out.Int(i,5);Out.LongRealFix(d[i] / 1000.0,9,3);Out.LongRealFix(a,10,3);Out.Ln
  END
END BenfordLaw.

  

You may also check:How to resolve the algorithm Selectively replace multiple instances of a character within a string step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Old lady swallowed a fly step by step in the Elena programming language
You may also check:How to resolve the algorithm Literals/Integer step by step in the zkl programming language
You may also check:How to resolve the algorithm Averages/Root mean square step by step in the Oz programming language
You may also check:How to resolve the algorithm Word wheel step by step in the Haskell programming language