3

I fit some data using Scipy:

param=expon.fit(data)
pdf_fitted=expon.pdf(x,loc=param[-2],scale=param[-1])
plot(x,pdf_fitted,'r')
hist(data,normed=1,alpha=.3,histtype='stepfilled')

And I get a curve that looks like this:

Exponential fit to data

How do I check how good the fit is (is there a parameter for this?). I'd like a number so I can compare different ways of fitting.

Devinity
  • 377
  • 1
  • 5
  • 17

1 Answers1

4

The standard method, assuming your errors are normally distributed, is to use the sum of the squared residuals. This, you can turn into rigurours statistics using the chi² distribution.

values, edges = np.histogram(data, bins=np.sqrt(len(data)))
x = edges[:-1] + np.diff(edges)

pdf_fitted = expon.pdf(x, loc=param[0], scale=param[1])
residuals = values - pdf_fitted

print np.dot(residuals, residuals)

Or, if you prefer the RMS:

print np.dot(residuals, residuals) / len(residuals)
Davidmh
  • 3,797
  • 18
  • 35