0% found this document useful (0 votes)
6 views2 pages

Stat Python

fichier python statistiques

Uploaded by

l00pingmexico
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Stat Python

fichier python statistiques

Uploaded by

l00pingmexico
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import numpy as np

import matplotlib.pyplot as plt


from math import pi, log

def lognorm(x, m, s):


fx = 1 / (x * s * (2 * pi)**0.5) * np.exp(-1 * (np.log(x) - m)**2 / (2 * s**2))
return fx

def Trapz(x,gx):
integ=np.sum((gx[1:])+gx[:-1])/2*(x[1:]-x[:-1])
return integ

def ecdf(x,gx):
ecdf=np.zeros(len(x),dtype=float)
for i in range(1,len(x)):
ecdf[i]=Trapz(x[0:i],gx[0:i])
return ecdf

def GenerateSample(x,CDF,nb):
u=np.random.uniform(0,1,nb)
Samplle=[]
for i in range(len(u)):
Idw=np.where(CDF<=u[i])[0][-1]
try:
Iup=np.where(CDF>u[i])[0][0]
except:
Iup=Idw
Sample.append(np.random.uniform(x[Idw],x[Iup]))
Sample=np.asarray(Sample)
return Sample

# Paramètres
m = 2
s = 1
x = np.linspace(0.01, 200, 1000) # Éviter x proche de 0

# Calcul de la fonction lognormale


fx = lognorm(x, m, s)

# Tracé de la courbe
plt.figure(1)
plt.plot(x, fx, 'k', linewidth=2)

# Afficher la courbe
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Log-Normale')
plt.grid(True)
plt.show()

print('integrale de fx = ' + str(np.round(Trapz(x,fx),5)))


print('moyenne de fx = ' + str(np.round(Trapz(x,x*fx),5)))
print('moyenne théorique de fx = ' + str(np.round(np.exp(m+(s**2)/2))))
print('moyenne de fx = ' + str(np.round(Trapz(x,x**2*fx)-Trapz(x,x*fx)**2,5)))
#print('moyenne de fx = ' + str(np.round(np.exp(s**2)-1)*np.exp(2*m+s**2),5))

ecdflognorm=ecdf(x,fx)
plt.figure(2)
plt.step(x,ecdflognorm,'k')

You might also like