cpx
cpx

Reputation: 17557

Probability density function in SciPy behaves differently than expected

I am trying to plot normal distribution curve using Python. First I did it manually by using the normal probability density function and then I found there's an exiting function pdf in scipy under stats module. However, the results I get are quite different.

Below is the example that I tried:

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

mean = 5
std_dev = 2
num_dist = 50

# Draw random samples from a normal (Gaussion) distribution
normalDist_dataset = np.random.normal(mean, std_dev, num_dist)

# Sort these values.
normalDist_dataset = sorted(normalDist_dataset)

# Create the bins and histogram
plt.figure(figsize=(15,7))
count, bins, ignored = plt.hist(normalDist_dataset, num_dist, density=True)

new_mean = np.mean(normalDist_dataset)
new_std = np.std(normalDist_dataset)

normal_curve1 = stats.norm.pdf(normalDist_dataset, new_mean, new_std)
normal_curve2 = (1/(new_std *np.sqrt(2*np.pi))) * (np.exp(-(bins - new_mean)**2 / (2 * new_std**2)))

plt.plot(normalDist_dataset, normal_curve1, linewidth=4, linestyle='dashed')
plt.plot(bins, normal_curve2, linewidth=4, color='y')

The result shows how the two curves I get are very different from each other.

enter image description here

My guess is that it is has something to do with bins or pdf behaves differently than usual formula. I have used the same and new mean and standard deviation for both the plots. So, how do I change my code to match what stats.norm.pdf is doing?

I don't know yet which curve is correct.

Upvotes: 0

Views: 180

Answers (1)

DYZ
DYZ

Reputation: 57033

Function plot simply connects the dots with line segments. Your bins do not have enough dots to show a smooth curve. Possible solution:

....
normal_curve1 = stats.norm.pdf(normalDist_dataset, new_mean, new_std)
bins = normalDist_dataset # Add this line
normal_curve2 = (1/(new_std *np.sqrt(2*np.pi))) * (np.exp(-(bins - new_mean)**2 / (2 * new_std**2)))
....

Upvotes: 2

Related Questions