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


import matplotlib.pyplot as plt
import numpy as np
from statistics import stdev, mean
plt.rcParams['font.size'] = 12
plt.close('all')

## mesures

# distances
d = 20 #m
u_d = .1  #m  incertitude
t = 100 #ns
u_t = 1  #ns incertitude

#pour affichage
nom_resultat = "$v$" # coefficient de frottement
unite = "m/s" #

## Méthode de Monté-Carlo
N =  10000 # nombre de tirages
d = np.random.uniform(d-u_d,d+u_d,N)
t = np.random.uniform(t-u_t,t+u_t,N)

v = d/t*1e9 # m/s

## représentation graphique
plt.figure(figsize=(10,6))

_,bins,_ = plt.hist(v, 50,
                            density = True,
                            color ='green',
                            alpha = 0.3)
# Répartition gaussienne
sigma, mu = np.std(v), np.mean(v)
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
plt.plot([mu,mu],[0,max(y)],'--r')
texte = "$\mu$ = {:.3e} ".format(mu) +  unite
plt.text(mu+sigma/10,max(y)/20,texte,color='r')
# Affichage écart type
plt.plot([mu,mu+sigma],2*[0.6*max(y)],'k',linewidth=3)
texte = "$\sigma =  {:.1e} $".format(sigma) + unite
plt.text(mu+sigma/10,max(y)*.56,texte)
# noms des axes et titre
plt.xlabel(nom_resultat + ' ('+unite + ')')
plt.ylabel('Frequence')
plt.title('Methode de Monte-Carlo')
plt.show()