[SciPy-user] optimization question

Matthieu Brucher matthieu.brucher@gmail....
Wed Jul 4 02:48:03 CDT 2007

2007/7/4, Volker Lorrmann <lorrmann@physik.uni-wuerzburg.de>:
> Hi Robert,
> > All leastsq() really adds to fmin*() is that you return the residuals
> > instead of summing up the squares of the residuals. You can do that
> > manually and use whichever minimizer you need.
> Can you give me a short example, how to do so? I´ve asked this also
> Matthieu, so if he will answer, you probably don´t need to. But two
> examples would be better than one ;)

My view is heavily influenced by my own optimizers, but here is the thing :

you have points y (possibly multidimensional) and points x (multidimensional
too) and a fitting function f(x, parameters)
You construct a new class (for instance) that take x and y as parameters :
class cost_fun(object:
  def __init__(self, x, y):
    self.x = x
    self.y = y

and you define a call method that will be used by fmin, for instance :
  def __call__(self, parameters):
    cost = 0
    for (xi, yi) in (x, y):
      cost += numpy.sum((yi - f(xi, parameters))**2) # you can use some
parameters instead of all parameters, just iterate over the part you need
    return cost

And voilà, your basic cost function is created.
Here a quadratic cost is used (this is what leastsq may do), but you can use
a robust cost instead.

I hope I was clear.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20070704/507e3338/attachment.html 

More information about the SciPy-user mailing list