$X$ est la variable aléatoire donnant le premier chiffre significatif d'un nombre choisi au hasard dans un ensemble de données.
Pour $c \in \{ 1,2,3,4,5,6,7,8,9 \}$, $$P(X=c) = \log \left( 1+ \frac{1}{c} \right) = \log(c+1) - \log(c)$$ où $\log$ est le logarithme décimal.
from math import log
# NB : en python, log désigne le logarithme neperien ln
# le logarithme decimal vaut log(x) = ln(x)/ln(10)
print("c","log(c)")
for c in range(1,10):
print(c , log(1+1/c)/log(10))
def benford(k):
return log(1+1/k)/log(10)
for i in range(1,10):
print(benford(i))
import pandas as pd
import matplotlib.pyplot as plt
def comparaisonBenford(L):
# comparaison de la liste L avec la liste generee par benford()
data = []
for i in range(1,10):
data = data + [[i,L[i],benford(i)]]
df = pd.DataFrame(data,columns=["chiffre","L","Benford"])
df.plot(x="chiffre", y=["L", "Benford"], kind="bar",figsize=(9,8))
plt.show()
def chiffreSignificatif(n):
#retourne le premier (en partant de la gauche) chiffre non nul du nombre n
#n peut être un décimal positif ou négatif
# si n=0, retourne 0
chiffres = {'1','2','3','4','5','6','7','8','9'}
p = '0'
k = 0
nchaine = str(n)
lchaine = len(nchaine)
while (k < lchaine) and (p not in chiffres):
p = nchaine[k]
if p in chiffres:
p = nchaine[k]
else:
p = 0 #résout le cas de 0 seul suivi d un autre caractere comme /n
k = k+1
return int(p)
chiffreSignificatif(700),chiffreSignificatif(5.578),chiffreSignificatif(-124785.700),chiffreSignificatif(0.0075700),chiffreSignificatif(0.00000002),chiffreSignificatif(0)
from random import randint
def frequenceChiffresSignificatifs(n):
# retourne la liste L des frequences d apparitions de chaque chiffre significatif
L = [0]*10
for i in range(n):
ri = randint(1,n)
for j in range(n):
p = chiffreSignificatif(ri * randint(1,n))
L[p] = L[p] + 1
for k in range(10):
L[k] = L[k]/(n**2)
return L
frequenceChiffresSignificatifs(1000)
comparaisonBenford(frequenceChiffresSignificatifs(1000))
from random import randint
def frequenceChiffresSignificatifs(n):
# retourne la liste L des frequences d apparitions de chaque chiffre significatif
L = [0]*10
N = 10000*n
for i in range(n):
p = chiffreSignificatif(randint(1,N))
L[p] = L[p] + 1
for k in range(10):
L[k] = L[k]/(n)
return L
print(frequenceChiffresSignificatifs(1000))
comparaisonBenford(frequenceChiffresSignificatifs(1000))
source : https://www.insee.fr/fr/statistiques/4265439?sommaire=4265511
fichierDonnees = open("communesLoireAtlantique2017.txt", "r")
L = [0]*10
nombreDonnees = 0
for ligne in fichierDonnees:
p = chiffreSignificatif(ligne)
L[p] = L[p] + 1
nombreDonnees = nombreDonnees + 1
fichierDonnees.close()
for k in range(10):
L[k] = L[k]/(nombreDonnees)
print(nombreDonnees)
print(L)
comparaisonBenford(L)
source : https://www.insee.fr/fr/statistiques/4265429?sommaire=4265511
fichierDonnees = open("communesFrance2017.txt", "r")
L = [0]*10
nombreDonnees = 0
for ligne in fichierDonnees:
p = chiffreSignificatif(ligne)
L[p] = L[p] + 1
nombreDonnees = nombreDonnees + 1
fichierDonnees.close()
for k in range(10):
L[k] = L[k]/(nombreDonnees)
print(nombreDonnees)
print(L)
comparaisonBenford(L)
Cette suite $(u_n)$ est définie pour tout entier naturel $n$ par : \begin{align*} & u_0 = 1\\ &u_1 = 1\\ &\text{et pour tout }n \in \mathbb{N},\qquad u_{n+2} = u_{n+1} + u_n \end{align*}
(en clair : chaque terme est la somme des deux précédents et on part de 1 et 1).
Ecrire une fonction qui prend n en entrée et retourne la fréquence des chiffres significatifs des n premiers termes de la suite de Fibonacci.
def fibo(n):
L = [0]*10
un = 1
unplus1 = 1
L[1] = 2
for i in range(n):
unplus2 = un + unplus1
p = chiffreSignificatif(unplus2)
L[p] = L[p] + 1
un = unplus1
unplus1 = unplus2
for k in range(10):
L[k] = L[k]/(n)
return L
aux = fibo(10001)
print(aux)
# print(benford())
comparaisonBenford(aux)
Pour tout entier naturel $n$, $$n ! = 1 \times 2 \times 3 \times 4 \times \dots \times n$$ $n!$ se lit "factorielle $n$".
Exemples : \begin{align*} &1 ! =1\\ &2 ! = 1 \times 2 = 2\\ &3! = 1 \times 2 \times 3 = 6\\ &5! = 1 \times 2 \times 3 \times 4 \times 5 = 120 \end{align*}
def fact(n):
L = [0]*10
f = 1
L[1] = 1
for i in range(2,n):
f = f*i
p = chiffreSignificatif(f)
L[p] = L[p] + 1
for k in range(10):
L[k] = L[k]/(n)
return L
aux = fact(10000)
print(aux)
# print(benford())
comparaisonBenford(aux)
$(u_n)$ est définie pour tout entier naturel $n$ par : \begin{align*} & u_0 = 3\\ &\text{et pour tout }n \in \mathbb{N},\qquad u_{n+1} = 1.2 \times u_{n} \end{align*}
def geom(n):
L = [0]*10
u = 3
L[3] = 1
for i in range(1,n):
u = u*1.2
p = chiffreSignificatif(u)
L[p] = L[p] + 1
for k in range(10):
L[k] = L[k]/(n)
return L
aux = geom(1000)
print(aux)
# print(benford())
comparaisonBenford(aux)