This post was updated on .
Hi,
I am working with python 2.7 using last updated versions of scipy/numpy. I need to find the best parameters to minimize a function that is like this: f(x) = A.x + c.e^Bx, where A and B are parameters and c in constant. The function is non-linear, and i used to use the method scipy.optimize.leastsq to perform this optimization: xi = np.array([list]) yi = np.array([list]) p = [A0, B0] def error(params, xi, yi): y0 = f(params, x0) return yi - y0 best_p, ok = scipy.optimize.leastsq(error, p, args = (xi,yi)) print best_p But now I want optimize the parameters with a different function, not the sum of deviations squared. If I want to use, for example, the sum of the absolute values of the error, what function of scipy I would use? Thank you. |
If you know that leastsq squares and sums the return value from your error function, perhaps you could just modify the return value. def error(params, xi, yi): y0 = f(params, x0)
return ( np.abs(yi - y0) )**0.5 This is probably really bad from a statistical point of view, but I guess it does what you want. I don't know if any of the other functions will use the absolute deviation.
Ryan On Wed, Dec 4, 2013 at 11:58 AM, davidsousarj <[hidden email]> wrote: Hi, I am working with python 2.7 using last updated versions of scipy/numpy. I need to find the best parameters to minimize a function that is like this: f(x) = A.x + c.e^{Bx}, where A and B are parameters and c in constant. The function is non-linear, and i used to use the method scipy.optimize.leastsq to perform this optimization: xi = np.array([list]) yi = np.array([list]) p = [A0, B0] def error(params, xi, yi): y0 = f(params, x0) return yi - y0 best_p, ok = scipy.optimize.leastsq(error, p, args = (xi,yi)) print best_p But now I want optimize the parameters with a different function, not the sum of deviations squared. If I want to use, for example, the sum of the absolute values of the error, what function of scipy I would use? Thank you. _______________________________________________ SciPy-User mailing list [hidden email] http://mail.scipy.org/mailman/listinfo/scipy-user |
2013/12/5 Ryan Nelson <[hidden email]>:
> If you know that leastsq squares and sums the return value from your error > function, perhaps you could just modify the return value. > > def error(params, xi, yi): > y0 = f(params, x0) > return ( np.abs(yi - y0) )**0.5 > > This is probably really bad from a statistical point of view, but I guess it > does what you want. I don't know if any of the other functions will use the > absolute deviation. Least absolute deviation is a special case of quantile regression; I don't know of any function in SciPy to do this, but there is statsmodels.regression.quantile_regression.QuantReg. http://statsmodels.sourceforge.net/devel/generated/statsmodels.regression.quantile_regression.QuantReg.html -- G. D. McBain Theory of Lift - Introductory Computational Aerodynamics in MATLAB/Octave Out now - http://www.wileyeurope.com/remtitle.cgi?111995228X _______________________________________________ SciPy-User mailing list [hidden email] http://mail.scipy.org/mailman/listinfo/scipy-user |
Free forum by Nabble | Edit this page |