loi normale

ZENO · updated October 13, 2023
import numpy as np
import numpy.random as rd
import matplotlib.pyplot as plt
import time

def Loi_normale_centrée_réduite(nb_sim):
    plt.close()

#loi théorique
    B=np.linspace(-5,5,nb_sim)
    A=(1/np.sqrt(np.pi*2))*np.exp(-(B*B)/2)

#loi normale
    start=time.time()
    x=rd.normal(0,1,nb_sim)
    end=time.time()
    duree=str("{:.2f}".format(1000*(end-start)))+"ms"
    plt.subplot(1,3,1)
    plt.hist(x,15,density="True",label="empirique")
    plt.plot(B,A,label="théorique")
    plt.title("rd.normal, t="+duree)
    plt.legend()
    


#Box-Muller
    start=time.time()
    U=rd.normal(0,1,nb_sim)
    V=rd.normal(0,1,nb_sim)
    X=np.sqrt(-2*np.log(U))*np.sin(2*np.pi*V)
    end=time.time()
    duree=str("{:.2f}".format(1000*(end-start)))+"ms"
    
    plt.subplot(1,3,2)
    plt.hist(X,15,density="True",label="empirique")
    plt.plot(B,A,label="théorique")
    plt.title("Box-Muller, t="+duree)
    plt.legend()



#Les_douze_uniformes

    
    
    start=time.time()
    u=rd.random([12,nb_sim])
    x=np.sum(u,0)-6
    end=time.time()
    duree=str("{:.2f}".format(1000*(end-start)))+"ms"
    
    plt.subplot(1,3,3)
    plt.hist(x,15,density="True")
    plt.plot(B,A,label="théorique")
    plt.title("les douze uniformes, t="+duree)
    plt.legend()
    
    
    plt.show()
Loi_normale_centrée_réduite(100000)
    

Output

Comments

Please sign up or log in to contribute to the discussion.