Exemple de comment intégrer une distribution (gaussienne) normale simple avec python en utilisant quad:

from scipy.integrate import quadimport matplotlib.pyplot as pltimport scipy.statsimport numpy as np#----------------------------------------------------------------------------------------## Normal Distributionx_min = 0.0x_max = 16.0mean = 8.0std = 3.0x = np.linspace(x_min, x_max, 100)y = scipy.stats.norm.pdf(x,mean,std)plt.plot(x,y, color='black')#----------------------------------------------------------------------------------------## integration between x1 and x1def normal_distribution_function(x):value = scipy.stats.norm.pdf(x,mean,std)return valuex1 = mean + stdx2 = mean + 2.0 * stdres, err = quad(normal_distribution_function, x1, x2)print('Normal Distribution (mean,std):',mean,std)print('Integration bewteen {} and {} --> '.format(x1,x2),res)#----------------------------------------------------------------------------------------## plot integration surfaceptx = np.linspace(x1, x2, 10)pty = scipy.stats.norm.pdf(ptx,mean,std)plt.fill_between(ptx, pty, color='#0b559f', alpha='1.0')#----------------------------------------------------------------------------------------#plt.grid()plt.xlim(x_min,x_max)plt.ylim(0,0.25)plt.title('How to integrate a normal distribution in python ?',fontsize=10)plt.xlabel('x')plt.ylabel('Normal Distribution')plt.savefig("integrate_normal_distribution.png")plt.show()
avec une moyenne et une déviation standard (std) de 8.0 et 3.0 respectivement, l'integration entre 1 * std et 2 * std donne:
>>> Normal Distribution (mean,std): 8.0 3.0>>> Integration bewteen 11.0 and 14.0 --> 0.13590512198327787
Références
| Links | Site |
|---|---|
| Best way to write a Python function that integrates a gaussian? | stackoverflow |
| quad | ocs.scipy.org |
| Integration using the quad method python | stackoverflow |
| SciPy - Integrate | tutorialspoint |
