You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I haven't checked through all the open modeling issues, but I think model classes should have a fit method (that would default to one of the fitters, but could also take an argument for the fitter to use). I think the following 'beginner' example could be made simpler if it looked like:
I'm not sure this is a good idea to select one of the Fitters as a default and different fitters have different input data, so the signature of this method would be complex.
Can we hold off deciding whether to add this until we have a few more fitters in astropy?
(I will do #1056 in the next days)
Having just played around with modeling some more this morning, what I find unintuitive is that if one has a model m and a fitter associated with that model, doing:
fit(x, y)
changes the state of m, which doesn't feel right. I would have thought that:
m.fit(x,y)
would change the state of m, but:
fit(x,y)
would just return a dictionary of parameters or something similar.
This looks like one of those features which is easy to add and would be hard to remove or change. And I think developing a flexible way to construct a fitter with user-defined optimization method and statistic (and eventually error estimation method) should be done before adding a convenience method m.fit() since this will change the signature of the method. So I would push this for after 0.3 unless there's a strong opinion that it is needed now.
But to be sure I understand what you have in mind, is this how you see it working:
Also, since I see m.fit() as a convenience, I would prefer fitter(x, y) to return a model which is ready to be evaluated, as this makes it more flexible and easier in the non-default case and in the case of a custom model.
It may be good to get other opinions on this @eteq@keflavich Anyone else?
I agree the fit method can wait until later. For now, what bugs me is that fitter(x,y) changes m but there is no mention of m in that line. What about (I think you are suggesting something similar):
m_fitted = fitter(m, x, y)
i.e. m is the original model, m_fitted is the model with the fitted parameters, then everything is explicit and one can re-use the same model as initial conditions to fit multiple datasets.
(it's also conceptually simpler that the fitter takes the model and the data as inputs)
I agree this behaviour can be a bit unexpected for users and returning a copy of the modified model may be better. Of course one has to be careful about reusing the initial values with different data sets.
I'm not so sure about passing the model to fitter.__call__. Currently the fitting is a two step process:
and the state of the fitter is set in the first step. The most important part of this is determining the constraints and which parameters are supposed to be fit (frozen and tied parameters are not fit).
It's good to keep the __call__ method with as little overhead as possible (although I haven't actually timed a fitter with a model passed to __call__ and setting the state there..
One way around this may be to have a model.initial_parameters list and model._initialize() method which fitter.__call__ calls before actually doing the fit. So the syntax will be:
A modified copy of the model is returned and the model is initialized before each fitting.
I understand this is still not as explicit as fitter(m, x, y). Do you think this would be sufficient? If not I can play with how fitters work now and see the performance of something like:
delaying for the next version is fine - I see the point that it's more important to get it right the first time, and the "create a new model" paradigm does sound safer
@astrofrog@nden@embray@cdeil - is this now "resolved" in that we've accepted the current system of new_model = fitter(initial_model, x, y), or should it be left open?
No I think they are a bit different - this is saying models should have a fit method, whereas #979 (I think) is saying there should be a reference to the most recent fitter used.
Let's close this - we have way too many issues of 'let's keep open just in case' these days, and I agree with @eteq that we've settled on an accepted way to use the fitters.
Activity
embray commentedon Aug 8, 2013
I like this idea. The default fitter to use could even be defined on a per-model basis (though also overridable).
cdeil commentedon Aug 13, 2013
I'm not sure this is a good idea to select one of the Fitters as a default and different fitters have different input data, so the signature of this method would be complex.
Can we hold off deciding whether to add this until we have a few more fitters in astropy?
(I will do #1056 in the next days)
astrofrog commentedon Aug 13, 2013
@cdeil - then we could consider at least supporting the most common fitting algorithm.
astrofrog commentedon Sep 29, 2013
Having just played around with modeling some more this morning, what I find unintuitive is that if one has a model
mand a fitter associated with that model, doing:changes the state of
m, which doesn't feel right. I would have thought that:would change the state of
m, but:would just return a dictionary of parameters or something similar.
nden commentedon Sep 30, 2013
This looks like one of those features which is easy to add and would be hard to remove or change. And I think developing a flexible way to construct a fitter with user-defined optimization method and statistic (and eventually error estimation method) should be done before adding a convenience method
m.fit()since this will change the signature of the method. So I would push this for after 0.3 unless there's a strong opinion that it is needed now.But to be sure I understand what you have in mind, is this how you see it working:
Also, since I see
m.fit()as a convenience, I would preferfitter(x, y)to return a model which is ready to be evaluated, as this makes it more flexible and easier in the non-default case and in the case of a custom model.It may be good to get other opinions on this @eteq @keflavich Anyone else?
astrofrog commentedon Sep 30, 2013
I agree the
fitmethod can wait until later. For now, what bugs me is thatfitter(x,y)changesmbut there is no mention ofmin that line. What about (I think you are suggesting something similar):i.e.
mis the original model,m_fittedis the model with the fitted parameters, then everything is explicit and one can re-use the same model as initial conditions to fit multiple datasets.(it's also conceptually simpler that the fitter takes the model and the data as inputs)
nden commentedon Sep 30, 2013
I agree this behaviour can be a bit unexpected for users and returning a copy of the modified model may be better. Of course one has to be careful about reusing the initial values with different data sets.
I'm not so sure about passing the model to
fitter.__call__. Currently the fitting is a two step process:and the state of the fitter is set in the first step. The most important part of this is determining the constraints and which parameters are supposed to be fit (frozen and tied parameters are not fit).
It's good to keep the
__call__method with as little overhead as possible (although I haven't actually timed a fitter with a model passed to__call__and setting the state there..One way around this may be to have a
model.initial_parameterslist andmodel._initialize()method whichfitter.__call__calls before actually doing the fit. So the syntax will be:A modified copy of the model is returned and the model is initialized before each fitting.
I understand this is still not as explicit as
fitter(m, x, y). Do you think this would be sufficient? If not I can play with how fitters work now and see the performance of something like:eteq commentedon Oct 16, 2013
delaying for the next version is fine - I see the point that it's more important to get it right the first time, and the "create a new model" paradigm does sound safer
initial changes for astropy#1330
eteq commentedon May 1, 2014
@astrofrog @nden @embray @cdeil - is this now "resolved" in that we've accepted the current system of
new_model = fitter(initial_model, x, y), or should it be left open?astrofrog commentedon May 1, 2014
Maybe we can simply close this and see if anyone else raises this in future?
embray commentedon May 1, 2014
I'm -0 on closing it.
This is definitely not a priority right now. But it's not an altogether bad idea either. Putting this in "Future"
pllim commentedon Jun 15, 2017
Is this a duplicate of #979?
astrofrog commentedon Jun 15, 2017
No I think they are a bit different - this is saying models should have a fit method, whereas #979 (I think) is saying there should be a reference to the most recent fitter used.
astrofrog commentedon Mar 21, 2019
Let's close this - we have way too many issues of 'let's keep open just in case' these days, and I agree with @eteq that we've settled on an accepted way to use the fitters.