Model.fit vs minimize objective?

798 views
Skip to first unread message

Sebastian Schafer

unread,
Aug 24, 2017, 1:37:12 PM8/24/17
to lmfit-py
Hi,
I've been using lmfit for a while now and this is by far the most concise and easiest to use fit tool I saw, thanks to everyone involved!
I was wondering about the difference between minimizing an objective function and fitting a model function beyond the convenience of the wrapper and the returned objects. I don't expect (or saw) any efficiency difference but I'd like to ask whether I'm missing something?
My typical use case involves fitting an arbitrary number of spectra or similar data sets that share some of their parameters (e.g., if it were Gaussians, all widths were the same but amplitude and center can vary). Using the expression attribute when setting a parameter I can easily compare cases of shared vs. independent parameters. Since I don't know the number of of parameters beforehand, they are just created programmatically and in the function I want to minimize I do a groupby over the datasets and finally append the residuals.
In case of the model.fit, I still can work with arbitrary parameters, 'hiding' them in **params and unpacking as above.  Since the advantage of assigning parameters automatically is gone and calculating the residual is really just one line taking the difference between the function and data, I was wondering whether there's another advantage (maybe some optimization because the model 'sees' the underlying function whereas the minimizer only ever gets the residuals handed back)?

Cheers

  Sebastian

Matt Newville

unread,
Aug 25, 2017, 2:51:15 PM8/25/17
to lmfit-py
Hi Sebastian,

On Thu, Aug 24, 2017 at 12:37 PM, Sebastian Schafer <seb.m....@gmail.com> wrote:
Hi,
I've been using lmfit for a while now and this is by far the most concise and easiest to use fit tool I saw, thanks to everyone involved!
I was wondering about the difference between minimizing an objective function and fitting a model function beyond the convenience of the wrapper and the returned objects. I don't expect (or saw) any efficiency difference but I'd like to ask whether I'm missing something?

The main differences are really of use and philosophy, not of the underlying algorithms.  With Minimization/Optimization, you write an objective function to be minimized, often in the sense of "least squares" but sometimes using other metrics or "cost functions".   That is applicable to a range of problems that aren't strictly "curve fitting" or even trying to finding parameters that best make a flexible model match some set of data.

The Model class is definitely useful only for the restricted (but very common) case of curve-fitting.  In fact, Model and its related classes just help you set up the minimization problem, run it for you, and help parse the results.   The additional overhead for Model.fit() vs minimize() should be very small.  Well, unless the fit is very fast, in which case you probably don't care about the time to set up the Model.


My typical use case involves fitting an arbitrary number of spectra or similar data sets that share some of their parameters (e.g., if it were Gaussians, all widths were the same but amplitude and center can vary). Using the expression attribute when setting a parameter I can easily compare cases of shared vs. independent parameters. Since I don't know the number of of parameters beforehand, they are just created programmatically and in the function I want to minimize I do a groupby over the datasets and finally append the residuals.
In case of the model.fit, I still can work with arbitrary parameters, 'hiding' them in **params and unpacking as above.  Since the advantage of assigning parameters automatically is gone and calculating the residual is really just one line taking the difference between the function and data, I was wondering whether there's another advantage (maybe some optimization because the model 'sees' the underlying function whereas the minimizer only ever gets the residuals handed back)?

I don't think there will be much difference.  Model.fit() creates a ModelResult object, which creates a Minimizer object, and then the `minimize()` method is called.  For most problems (that take more than a dozen iterations), that will spend most of the time evaluating the model, and somewhat more time than we'd like updating the parameter constraints. But that really won't be different using Model or Minimizer.

Which is all to say: if you have a curve-fitting problem, use Model.  If it's not curve-fitting, or the model is really complex, you might as well use Minimizer. 

--Matt
Reply all
Reply to author
Forward
0 new messages