I'm new to python and I am trying to draw a graph for the probability of normal distribution of 1000 samples with specific mean and variance. The probability function is:
I have generated 1000 samples with:
import numpy as np
import matplotlib.pyplot as plt
s_mean = 0
s_variance = 0.1
s_std_dev = np.sqrt(s_variance)
s = np.random.normal(s_mean, 100, 1000)
I have managed to follow the function as much as I can:
prob_s = (np.exp(-1*((s-s_mean)**2)/(2*s_variance)))/(np.sqrt(2*np.pi)*s_std_dev)
and draw the graph using matplotlib:
f, (ax1) = plt.subplots(1, figsize=(5, 5))
ax1.hist(prob_s, bins=50, color='r')
plt.show()
but the graph is no where close to what I have expected:
The graph I was expecting is the following:
I can't find what's wrong here. Any help?
np.random.normal
needs the mean and the standard deviation. Then s
can be used as input for the histogram. density=True
normalizes the histogram to be similar in size as a probability density function (pdf), so with an area of 1
.
To draw the pdf curve, you can use your function, using some regularly-spaced x-values.
import numpy as np
import matplotlib.pyplot as plt
s_mean = 0
s_variance = 0.1
s_std_dev = np.sqrt(s_variance)
s = np.random.normal(s_mean, s_std_dev, 1000)
fig, ax1 = plt.subplots(figsize=(5, 5))
ax1.hist(s, bins=50, color='crimson', density=True)
x = np.linspace(s.min(), s.max(), 200)
prob_s = (np.exp(-1 * ((x - s_mean) ** 2) / (2 * s_variance))) / (np.sqrt(2 * np.pi) * s_std_dev)
ax1.plot(x, prob_s, color='dodgerblue')
plt.show()