Reputation: 65
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?
Upvotes: 0
Views: 397
Reputation: 80279
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()
Upvotes: 1
Reputation: 187
That won't work. The normal distribution is specific, because it can not be expressed in terms of elementary functions. My advice is to just use scipy.stats
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
import math
mu = 0
variance = 1
sigma = math.sqrt(variance)
x = np.linspace(mu - 3*sigma, mu + 3*sigma, 100)
plt.plot(x, stats.norm.pdf(x, mu, sigma))
plt.show()
if you want to draw the distribution out of those 1000 samples you should use
hist, bins = np.histogram(s, bins=50)
plt.plot(bins[1:], hist)
plt.show()
Upvotes: 1