My results did not match what was expected from Benford’s Law. Is there something wrong?

Asked

Viewed 48 times

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://www.youtube.com/watch?v=etx0k1nLn78

  • https://math.stackexchange.com/q/217869

  • 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.

  • 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.

  • 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.

1 answer

1


The problem was that for Benford’s Law to work, the upper and lower intervals of the variable change from individual to individual. For example: When a test is applied to a class of "n" students, it cannot be considered that all students have the same probability of getting the same grade, because it is a heterogeneous group. Thus, considering the range of possible grades for each student, different, we can observe the Benford Law functioning appropriately.

This time, I was less sloppy in writing the code:

using System;

namespace Teste_para_sempre
{
    class Program
    {
        static void Main(string[] args)
        {
            int population = 10000000;
            int pontuaçãoMinima_LimiteInferior = 0;
            int pontuaçãoMinima_LimiteSuperior = 100;
            int pontuaçãoMaxima_LimiteInferior = 1000;
            int pontuaçãoMaxima_LimiteSuperior = 10000;
            int[] termoInicial = new int[10];

            Random rand = new Random();

            for (int i = 0; i < population; i++)
            {
                int pontuaçãoMinima = rand.Next(pontuaçãoMinima_LimiteInferior, pontuaçãoMinima_LimiteSuperior);
                int pontuaçãoMaxima = rand.Next(pontuaçãoMaxima_LimiteInferior, pontuaçãoMaxima_LimiteSuperior);

                int random = rand.Next(pontuaçãoMinima, pontuaçãoMaxima);

                while (random >= 10)
                {
                    random /= 10;
                }

                termoInicial[random]++; 
            }

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Termo {0}: {1}",i, termoInicial[i]);
            }
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.