0
Hello, I’m a beginner programmer and I decided to write a "napkin draft" to test the Benford Law (Benford’s Law). Although the code is not good quality and poorly optimized, I believe that the mathematical part is correct.
The random variables were calculated in a range between 0 and 1500, thus meeting the criteria of the law, and the sample group was 10000000. However, despite this, the values do not approach so much the expected curve, mainly from the third onwards.
using System;
namespace Teste_para_sempre
{
class Program
{
static void Main(string[] args)
{
int numero0 = 0;
int numero1s = 0;
int numero2s = 0;
int numero3s = 0;
int numero4s = 0;
int numero5s = 0;
int numero6s = 0;
int numero7s = 0;
int numero8s = 0;
int numero9s = 0;
int[] numero = new int[10000000];
var teste = new Random();
for (int i = 0; i < 10000000; i++)
{
numero[i] = teste.Next(0, 1500);
for(int a=1; a<= 10000000; a++)
{
if (numero[i] == 0)
{
numero0++;
a = 10000000;
}
if(numero[i]/a == 1)
{
numero1s++;
}
if (numero[i] / a == 2)
{
numero2s++;
}
if (numero[i] / a == 3)
{
numero3s++;
}
if (numero[i] / a == 4)
{
numero4s++;
}
if (numero[i] / a == 5)
{
numero5s++;
}
if (numero[i] / a == 6)
{
numero6s++;
}
if (numero[i] / a == 7)
{
numero7s++;
}
if (numero[i] / a == 8)
{
numero8s++;
}
if (numero[i] / a == 9)
{
numero9s++;
}
a = (a * 10) - 1;
}
}
int total = numero0 + numero1s + numero2s + numero3s + numero4s + numero5s + numero6s + numero7s + numero8s + numero9s;
Console.WriteLine("Numeros que começam com 1: {0}\nNumeros que começam com 2: {1}\nNumeros que começam com 3: {2}\nNumeros que começam com 4: {3}\nNumeros que começam com 5: {4}\nNumeros que começam com 6: {5}\nNumeros que começam com 7: {6}\nNumeros que começam com 8: {7}\nNumeros que começam com 9: {8}\n", numero1s, numero2s, numero3s, numero4s, numero5s, numero6s, numero7s, numero8s, numero9s);
Console.WriteLine(total);
}
}
}
Here’s a video talking about Benford’s Law and its role in fraud detection:
https://math.stackexchange.com/q/217869
– hkotsubo
Newcomb-Benford is just useful in finding randomly generated values, maybe your generation is having a bias , try to download some real series from the Internet (IBGE etc) and try to apply.
– Motta
Thank you very much for your answers. I think my distribution range is too small to observe these differences using the randomness function. Maybe using a real database or adjusting the distribution range for a logarithmic scale can work.
– Dhonai
When I read about NB the first time I did a test based on the company I work for, the result was consistent with the theory.
– Motta