I've been surfing but haven't found the correct method to do the following.
I have a histogram done with matplotlib:
hist, bins, patches = plt.hist(distance, bins=100, normed='True')
From the plot, I can see that the distribution is more or less an exponential (Poisson distribution). How can I do the best fitting, taking into account my hist and bins arrays?
UPDATE
I am using the following approach:
x = np.float64(bins) # Had some troubles with data types float128 and float64
hist = np.float64(hist)
myexp=lambda x,l,A:A*np.exp(-l*x)
popt,pcov=opt.curve_fit(myexp,(x[1:]+x[:-1])/2,hist)
But I get
---> 41 plt.plot(stats.expon.pdf(np.arange(len(hist)),popt),'-')
ValueError: operands could not be broadcast together with shapes (100,) (2,)
What you described is a form of exponential distribution, and you want to estimate the parameters of the exponential distribution, given the probability density observed in your data. Instead of using non-linear regression method (which assumes the residue errors are Gaussian distributed), one correct way is arguably a MLE (maximum likelihood estimation).
scipy
provides a large number of continuous distributions in itsstats
library, and the MLE is implemented with the.fit()
method. Of course, exponential distribution is there:Your
distance
will replaceX
here.