Exemples de comment calculer et tracer une fonction de répartition, ou fonction de distribution cumulative en python:
1 -- Générer des nombres aléatoires
Générons par exemple des nombres aléatoires depuis une loi normale:
import numpy as npimport matplotlib.pyplot as pltN = 100000data = np.random.randn(N)
2 -- Créer un histogramme avec matplotlib
hx, hy, _ = plt.hist(data, bins=50, normed=1,color="lightblue")plt.ylim(0.0,max(hx)+0.05)plt.title('Generate random numbers \n from a standard normal distribution with python')plt.grid()plt.savefig("cumulative_density_distribution_01.png", bbox_inches='tight')#plt.show()plt.close()

3 -- Option 1: Calculer une fonction de répartition a partir d'un histogramme
dx = hy[1] - hy[0]F1 = np.cumsum(hx)*dxplt.plot(hy[1:], F1)plt.title('How to calculate and plot a cumulative distribution function ?')plt.savefig("cumulative_density_distribution_02.png", bbox_inches='tight')plt.close()

4 -- Option 2: en triant les données
X2 = np.sort(data)F2 = np.array(range(N))/float(N)plt.plot(X2, F2)plt.title('How to calculate and plot a cumulative distribution function ?')plt.savefig("cumulative_density_distribution_03.png", bbox_inches='tight')plt.close()

4 -- Utiliser cdf pour une distribution normale (Gaussienne)
Pour une fonction normale standard il existe la fonction cdf():
from scipy.stats import normx = np.linspace(-10,10,100)y = norm.cdf(x)plt.plot(x, y)plt.title('How to calculate and plot a cumulative distribution function ?')plt.savefig("cumulative_density_distribution_04.png", bbox_inches='tight')plt.close()

