TP1-Correction
Posted on Sat 16 February 2019 in posts
In [1]:
# import
import numpy as np
import matplotlib.pyplot as pp
In [2]:
# Générer des nombres aléatoires uniformes [0,1]
va = np.random.random((100000))
In [3]:
# faire un histogramme
def count_elements(seq, BInf,BSup, NBins):
δ = (BSup-BInf)/NBins
hist = np.zeros((NBins))
for i in seq:
idx = int(np.floor((i-BInf)/δ))
if((idx<0)|(idx>=NBins)):
print("range error!")
break;
hist[idx] += 1
norm = 0
for i in hist:
norm += i*δ
return hist,norm
# on fait l'histogramme des v.a. uniformes
h1,norm = count_elements(va,0,1,100)
print(h1)
print(h1/norm)
pp.plot(0)
pp.plot(2)
pp.plot(h1/norm)
Out[3]:
In [4]:
# variables gaussiennes
va_normal = np.random.normal(size=(100000))
δ = (max(va_normal)+0.01-(min(va_normal)-0.01))/100
xr = np.arange(min(va_normal)-0.01,max(va_normal)+0.01,δ)
h2,norm=count_elements(va_normal,min(va_normal)-0.01,max(va_normal)+0.01,100)
# on definit la pdf
def gauss(x):
return 1/np.sqrt(2*np.pi) * np.exp(-x**2 / 2.0)
# compararaison entre pdf et histogramme
pp.plot(xr,h2/norm)
pp.plot(xr,gauss(xr))
Out[4]:
In [5]:
# numpy sait le faire
# vérifier la fonction de répartition : faire un histogramme
f,ax = pp.subplots(1,3,figsize=(15,5))
ax[0].hist(va, range=(0,1), bins=10)
ax[1].hist(va_normal,bins=50)
ax[2].hist(va_normal,bins=50,density=True)
ax[2].plot(xr,gauss(xr))
Out[5]:
We generate correlated variables : if (y<0) we put a gaussian distribution with negative mean, and positive mean for (y>0) Using Baysian rule : $p(x,y) = p(x|y)p(y)$ with $p(y) \sim \cal{U}[-1;1]$
We obtain $p(x|y) = \cal{N}(-5,1)\theta(y) + \cal{N}(5,1)\theta(-y)$
In [6]:
# p(x,y) = p(x|y)p(y)
# p(y) ~ unif(-1,1)
# p(x|y) = Normal(-5,1)\theta(y) + Normal(5,1)\theta(-y)
va_y = np.random.random(size=(100000))*2-1
va_x = np.zeros(va_y.shape[0])
yneg = np.where(va_y < 0)
ypos = np.where(va_y >= 0)
va_x[yneg[0]] = np.random.normal(-5,size=(len(yneg[0])))
va_x[ypos[0]] = np.random.normal(5,size=(len(ypos[0])))
pp.scatter(va_x,va_y)
Out[6]:
In [7]:
fig = pp.figure(figsize=(10, 5))
pp.hist2d(va_x,va_y,bins=100,density=True)
pp.colorbar(orientation='horizontal')
Out[7]:
In [10]:
# Loi des gds nombres et convergence
# commençons par des variables de bernoulli
# Nva number of variables to generate
Nva = 10**np.arange(1,8,0.2)
print(Nva)
# number of time to repeat the process
NRESH = 1
arr_mean = []
for i in Nva:
m=0
for j in range(NRESH):
v_b = np.random.randint(2,size=(int(i)))
m += np.mean(v_b)
arr_mean = np.append(arr_mean,m/NRESH)
# allure des courbes vs loi de puissance en racine carrée
pp.loglog(Nva,np.abs(arr_mean-0.5))
pp.loglog(Nva,Nva**(-0.5)/Nva[0]**(-0.5)*np.abs(arr_mean-0.5)[0])
Out[10]:
In [9]:
# somme de variables aléatoire, convergence vers la loi gaussienne
n_var = 10000
n_sum = 10000
est_m = []
for i in range(n_var):
est_m = np.append(est_m,np.sum(np.random.randint(2,size=(n_sum))))
Z = np.sqrt(n_sum)*(est_m/n_sum - 0.5)/0.5;
pp.hist(Z,bins=40,density=True)
pp.plot(xr,gauss(xr))
Out[9]:
In [ ]: