Benford’s law describes the relative frequence of the lead digit of numbers from real-world data-sets that contain data across several orders of magnitude. Numbers will start with the digit 1 about 30% of the time.
The following code uses the Wolfram Language command BenfordDistribution to compare his prediction to the sizes of files in a filesystem.
The following code uses the Wolfram Language command BenfordDistribution to compare his prediction to the sizes of files in a filesystem.
First collect all the file names that you are interested in.
files=FileNames["*","~/Documents/",∞];
Now calculated the lead digit of the FileByteCount for each file (discarding folders and empty files).
digitCounts=Counts[First[IntegerDigits[#]]&/@DeleteCases[Quiet[FileByteCount/@files],$Failed|0]];digitFrequencies=digitCounts/Total[digitCounts];
Now use BenfordDistribution to generate the predicted proportions.
predictions=PDF[BenfordDistribution[10],Range[9]];
Finally visualize the two data sets
Show[ListLinePlot[Legended[predictions,"Benford`s\nPrediction"],PlotStyleLightBlue,MeshStyleBlue,MeshAll,AxesLabel{"Lead digit","Frequency"},PlotLabel"Benford's Law Prediction of File Sizes",Ticks{Range[9],Table[{i,PercentForm[i]},{i,0.05,0.3,0.05}]}],ListLinePlot[Legended[KeySort[digitFrequencies],"Actual"],PlotStyleLighter[Green,0.7],MeshStyleDarker[Green],MeshAll],ImageSize500]
Number of files considered:
Total[digitCounts]
85496