1

I am currently using scipy.optimize's curve_fit to fit data to a Gaussian. Fit etc. worked perfectly fine, and I get my parameters and uncertainty of those. But is there any way to calculate the quality of the fit?

Thank you for your answers

2 Answers2

2

You would generate predictions using your function and the parameters returned by curve_fit after optimization with some metric (MSE, RMSE, R-sq, etc) for quantifying quality of fit.

For example:

popt, pcov = curve_fit(func, xdata, ydata)

# Use optimized parameters with your function
predicted = func(popt) 

# Use some metric to quantify fit quality
sklearn.metrics.mean_squared_error(ydata, predicted)
Derek
  • 61
  • 3
1

You can use some metrics like MSE or RMSE:

# Suppose:
# popt, pcov = curve_fit(func, xdata, ydata)
# ypred = func(xdata, *popt)

mse = np.mean((ydata - ypred)**2)
rmse = np.sqrt(mse)

Or with linalg module of numpy:

mse = np.linalg.norm(ydata - ypred)**2 / len(ydata)
rmse = np.linalg.norm(ydata - ypred) / np.sqrt(len(ydata))

More information about RMSE: https://stackoverflow.com/a/37861832/15239951

Corralien
  • 109,409
  • 8
  • 28
  • 52