Assigning a prefix to a model after it is declared changes the values of its parameters #773
-
Hi all, I have encountered some surprising behavior (might be a bug or a feature) when assigning a prefix to a model after it is declared. If I run: A = PseudoVoigtModel(prefix='A_')
A.make_params()
A.set_param_hint('fraction', value=1)
for par in A.make_params().values():
print(par) The result is:
However, if I assign the prefix at a later time: A = PseudoVoigtModel()
A.make_params()
A.set_param_hint('fraction', value=1)
A.prefix = 'A_'
for par in A.make_params().values():
print(par) I get a different result:
Is this the expected behavior? Cheers, |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 4 replies
-
@jrr-us Hm, I am sort of OK with inconsistent or even non-sensical behavior when one does not provide initial values for parameters. There are no situations where that should be expected to work. If given that choice, I would suggest the better fix is to adopt the second set of values. |
Beta Was this translation helpful? Give feedback.
-
@newville Thank you for your reply. I see your point, however there are situations where default initial values might be fine. For example an initial amplitude of 1.0 would be a good starting value when fitting a peak that has been previously normalized to have unit area. Also, you can see in the second part of my example that A = PseudoVoigtModel()
A.make_params()
A.set_param_hint('amplitude', value=5.0)
A.set_param_hint('center', value=-1.0)
A.set_param_hint('sigma', value=0.1)
A.set_param_hint('fraction', value=1.0)
A.prefix = 'A_'
for par in A.make_params().values():
print(par) Gives:
|
Beta Was this translation helpful? Give feedback.
-
@jrr-us I don't think the way you use It is not intended to set initial parameters and in my opinion you shouldn't use it like that. If you want to set initial parameters, you can do that when making the parameters, for example by doing Having said that, it's not very obvious to me where the difference is coming from in the example you're showing, but since I don't think we actually support your use of |
Beta Was this translation helpful? Give feedback.
-
If a user expects that unit normalization is indeed reasonable for their data, then they should be able to state that explicitly. The fitting algorithms (yes, the algorithms, not our code or the implementation of the algorithms we are using) require initial values for all variable parameters. There really are no exceptions.
Again, in the absence of initial values provided by the user, I don't really see any values selected by default as wrong or better, except insofar as -Inf (or Nan) is sort of better because it clearly signals an unreasonable starting value that will fail early. |
Beta Was this translation helpful? Give feedback.
-
@jrr-us so was the question "why do the values change" after setting a prefix or "why was the parameter hint ignored"? That not respecting the parameter hint does seem odd to me, but I completely did not get that from your initial report. If it isn't obvious, it's always good to ask the question that you actually want to be answered ;). |
Beta Was this translation helpful? Give feedback.
@jrr-us I don't think the way you use
set_param_hint
is really what you're supposed to do. That is, theset_param_hint
belongs to the model and is intended to set, for example, parameter bounds for a certain model to make sure that it physically makes sense (e.g., the linewidth should be positive) and they will be used when doingmake_params()
. Since you're using one of the built-in models where we already set-up the parameter_hints correctly (i.e., forPseudoVoigModel
we set hints forsigma
andgama
) so there is no reason to call this method again yourself.It is not intended to set initial parameters and in my opinion you shouldn't use it like that. If you want to set initial parameters…