Python curve fit optimize using relative deviation instead of absolute deviation

Viewed 495

I am fitting curve using the scipy.optimize.curve_fit. From what I notice, the curve fitting is performed by minimizing the sum of the squared residuals of f(xdata, *popt) - ydata, whereas I want to minimize the squared residuals of relative error: (f(xdata, *popt) - ydata)/ydata since my ydata order of magnitude varies a lot. How to optimize using the relative deviation? I do not need to necessarily use curve_fit function. Any python function to achieve this is fine.

PS: I am aware of another approach of converting the ydata into logspace and fitting the resulting data. But I do not want to do this approach.

2 Answers

An error weighting with the inverse y data is equivalent to a transformation into log-space. This is for the following reason: In a standard least square fit the data y deviates from the "true" function value yt by the error sa. The least-square fit minimizes the sum of y - yt = sa over all data. When going to log-space this reads log(y) - log(yt) = log( yt + sa) - log( yt ) = log( 1 + sa / yt ) = log( y / yt ) using the standard rules for logarithms.

This gives, hence, the relative error as also pointed out by JJacquelin .

BTW, as the least square makes the square, of course, this is also ( log( y ) - log( yt ) )**2 = ( -log( y ) + log( yt ) )**2 = log( yt / y )**2

Related