Curve Fiting Of Normal Distribution In Python
I want to calculate the percentiles of normal distribution data, so I first fit the data to the normal distribution, here is the example: from scipy.stats import norm import numpy
Solution 1:
depends on what you plotted, these look OK to me:
plt.plot(x,y)
Out[3]: [<matplotlib.lines.Line2D at 0xb9cef98>]
popt,popc
Out[4]:
(array([ 8.41765250e+04, 1.98651581e+00, 3.15537860e+00]),
array([[ 5.64670700e+05, 1.12782889e-05, 1.15455042e+01],
[ 1.12782889e-05, 2.91058556e-06, 2.73909077e-10],
[ 1.15455042e+01, 2.73909077e-10, 2.88523818e-04]]))
plt.plot(x,datafit(x,*popt))
Out[5]: [<matplotlib.lines.Line2D at 0xb990080>]
my guess is that you've got an error in sig, scale and *,/ in your datafit def vs norm()
I rewrote datafit to match the scipy norm.pdf
and still have a factor of ~pi issue which may be just definitional: https://en.wikipedia.org/wiki/Normal_distribution
oops, looks like the "factor of pi" was just coincidence of your particular data rereading the norm.pdf def suggest the whole is rescaled by the 'scale" factor so now I think it should be:
'''
norm.pdf(x) = exp(-x**2/2)/sqrt(2*pi)
norm.pdf(x, loc, scale) == norm.pdf(y) / scale with y = (x - loc) / scale
'''defdatafit(x,N,u,sig):
# y = N/(np.sqrt(2*np.pi)*sig)*np.exp(-(x-u)**2/2*sig**2)
y = N*np.exp(-((x-u)/sig)**2/2)/(np.sqrt(2*np.pi))
return y
popt,popc = curve_fit(datafit,x,y,p0=[np.max(y),2,2])
# scipy norm.pdf with scaling factors to match datafit()
Normal_distribution = popt[0]*popt[2]*norm.pdf(x, popt[1], popt[2])
plt.plot(x,y, 'b')
plt.plot(x, datafit(x+.1, *popt), 'g')
plt.plot(x, Normal_distribution, 'r')
Post a Comment for "Curve Fiting Of Normal Distribution In Python"