#############################
######### Z - score #########
############ J.B ############
########### 2021  ###########
#############################
#############################

from random import random
import matplotlib.pyplot as plt
import numpy as np
from statistics import stdev, mean
plt.close('all')

## mesures
# distance
d = 1 #m
u_d = .01  # incertitude
# temps
t = 0.01
u_t = 1e-4 #incertitude


## Méthode de Monté-Carlo
N =  10000
liste_res = []
for _ in range(N):
    # tirage aléatoire
    t_alea = t + u_t*random()
    d_alea = d + u_d*random()
    v = d_alea / t_alea
    liste_res.append(v)


## représentation graphique
plt.figure(figsize=(12,8))
n, bins, patches = plt.hist(liste_res, 50,
                            density = 1,
                            color ='green',
                            alpha = 0.3)
# Répartition gaussienne
sigma, mu = stdev(liste_res), mean(liste_res)
y = ((1 / (np.sqrt(2 * np.pi) * sigma)) *
     np.exp(-0.5 * (1 / sigma * (bins - mu))**2))
plt.plot(bins, y, '--', color ='black')
# Affichage moyenne et écart type
plt.plot([mu,mu],[0,1],'--r')
texte = "< v > = {:.2f}".format(mu) +  "$m.s^{-1}$"
plt.text(mu,0.1,texte,color='r')
plt.arrow(mu,0.5,sigma,0, head_width = 0.02, head_length = 0.05)
texte = "$\sigma =  {:.2f} $".format(sigma) +  "$m.s^{-1}$"
plt.text(mu+sigma/10,0.52,texte)
# noms des axes et titre
plt.xlabel('vitesse ($m.s^{-1}$)')
plt.ylabel('Fréquence')
plt.title('Méthode de Monté-Carlo')
plt.show()




