How to calculate a Gaussian density probability function at a given point in python ?

How to calculate a Gaussian density probability function at a given point in python ?

Daidalos April 30, 2020


Let's consider the normal (Gaussian) distribution with mean equal to 8 and standard deviation equal to 2:

How to calculate a Gaussian density probability function at a given point in python ?

To calculate a Gaussian density probability function at a given point in python, a solution is to do:

scipy.stats.norm.pdf(6,8,2)

returns:

0.13

Source code to create the plot:

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

x = np.linspace(0, 16.0, 100)

plt.plot(x,scipy.stats.norm.pdf(x,8,2))

plt.plot([6,6],[0,scipy.stats.norm.pdf(6,8,2)],c='k')
plt.plot([0,6],[scipy.stats.norm.pdf(6,8,2),scipy.stats.norm.pdf(6,8,2)],c='k')
plt.scatter(6,scipy.stats.norm.pdf(6,8,2),c='k')

plt.grid()
plt.xlim(0,16)
plt.ylim(0,0.25)
plt.title('Loi normale (gaussienne)')
plt.savefig("probability_normal_distribution.png")
plt.show()

References