Reputation: 568
I'm trying to fit a lognormal distribution:
import numpy as np
import scipy.stats as sp
from scipy.optimize import curve_fit
def pdf(x, mu, sigma):
return (np.exp(-(np.log(x) - mu)**2 / (2 * sigma**2)) / (x * sigma * np.sqrt(2 * np.pi)))
x_axis = [5e5,1e6,2e6,5e6,6e6]
y_axis = [0,0.2,0.4,0.6,0.8]
curve_fit(pdf,x_axis,y_axis,maxfev=10000,)
This returns the following:
C:\Anaconda3\Lib\site-packages\scipy\optimize\minpack.py:604: OptimizeWarning: Covariance of the parameters could not be estimated
category=OptimizeWarning)
Out[66]:
(array([ 1., 1.]), array([[ inf, inf],
[ inf, inf]]))
These results don't really seem like a great fit. I know there are only five datapoints but when I use solver in excel I get the parameters of 0.1536 and 3.1915, which isn't perfect, but it is much closer.
edit: trying this with a cdf
def cdf(x,mu,sigma):
return sp.norm.cdf((np.log(x)-mu)/sigma)
curve_fit(cdf,x_axis,y_axis,)
this returns the same error as above
Upvotes: 0
Views: 810
Reputation: 253
Have you visualized the data?
The given values for x_axis
and y_axis
look like this:
If you use the given x_axis
values and your Excel solver values for mu=0.1536
and sigma=3.1915
and then visualize the pdf
you get this:
So I wonder what result you want to get?
Actually the data in the first picture does not really look like a Log-normal pdf, does it?
Upvotes: 2