# How to integrate a simple normal distribution in python ?

Published: February 09, 2019

To integrate a simple normal distribution in python, a solution is to use quad, example:

````from scipy.integrate import quad`

`import matplotlib.pyplot as plt`
`import scipy.stats`
`import numpy as np`

`#----------------------------------------------------------------------------------------#`
`# Normal Distribution`

`x_min = 0.0`
`x_max = 16.0`

`mean = 8.0 `
`std = 3.0`

`x = 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 x1`

`def normal_distribution_function(x):`
`    value = scipy.stats.norm.pdf(x,mean,std)`
`    return value`

`x1 = mean + std`
`x2 = mean + 2.0 * std`

`res, err = quad(normal_distribution_function, x1, x2)`

`print('Normal Distribution (mean,std):',mean,std)`
`print('Integration bewteen {} and {} --> '.format(x1,x2),res)`

`#----------------------------------------------------------------------------------------#`
`# plot integration surface`

`ptx = 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()`
```

with a mean and standard deviation (std) of 8.0 and 3.0 respectively, the integration between 1 * std and 2 * std
returns:

````>>> Normal Distribution (mean,std): 8.0 3.0`
`>>> Integration bewteen 11.0 and 14.0 -->  0.13590512198327787`
```

It is possible to integrate a function that takes several parameters with quad in python, example of syntax for a function f that takes two arguments: arg1 and arg2:

````quad( f, x_min, x_max, args=(arg1,arg2,))`
```

Example of code using quad with a function that takes multiple arguments:

````from scipy.integrate import quad`

`import matplotlib.pyplot as plt`
`import scipy.stats`
`import numpy as np`

`def normal_distribution_function(x,mean,std):`
`    value = scipy.stats.norm.pdf(x,mean,std)`
`    return value`

`x_min = 0.0`
`x_max = 30.0`

`mean = 15.0 `
`std = 4.0`

`ptx = np.linspace(x_min, x_max, 100)`
`pty = scipy.stats.norm.pdf(ptx,mean,std)`

`plt.plot(ptx,pty, color='gray')`

`plt.fill_between(ptx, pty, color='#e1b1b4', alpha='1.0')`

`plt.grid()`

`plt.title('How to integrate a function that takes parameteres in python ?', fontsize=10)`
`plt.xlabel('x', fontsize=8)`
`plt.ylabel('Probability Density Function', fontsize=8)`

`res, err = quad(normal_distribution_function, x_min, x_max, args=(mean,std,))`

`print(res)`

`plt.savefig("integrate_function_takes_parameters.png")`
`plt.show()`
```

Image

of