LinearLSQFitter cannot fit compound models
See original GitHub issueUndo the doc modifications in #6041 before closing this as resolved.
@nden tells me that “I think in general a compound model should be fitted with a non-linear fitter because at least currently the LinearFitter cannot construct the Vandermonde matrix for compound models”. It would be useful to have this working if possible, as I believe non-linear fitters are not guaranteed to converge on the solution without a good starting guess, which may not always be available. Also, the documentation should mention that it currently doesn’t work (which I’ll fix in the meantime). This is just for the record and in case someone wants to work on it before one of us gets to it.
Running LinearLSQFitter on a couple of compound models currently gives errors like the following:
>>> LinearLSQFitter()(Mapping((0,)) | Chebyshev1D(2), range(0,10), range(0,10))
Traceback (innermost last):
File "<console>", line 1, in <module>
File "/home/jturner/anaconda2/envs/peylian/lib/python2.7/site-packages/astropy-2.0.dev18162-py2.7-linux-x86_64.egg/astropy/modeling/fitting.py", line 254, in __call__
raise ModelLinearityError('Model is not linear in parameters, '
ModelLinearityError: Model is not linear in parameters, linear fit methods should not be used.
>>> LinearLSQFitter()(Chebyshev1D(2) | Chebyshev1D(2), range(0,10), range(0,10))
Traceback (innermost last):
File "<console>", line 1, in <module>
File "/home/jturner/anaconda2/envs/peylian/lib/python2.7/site-packages/astropy-2.0.dev18162-py2.7-linux-x86_64.egg/astropy/modeling/fitting.py", line 279, in __call__
lhs = model_copy.fit_deriv(x, *model_copy.parameters)
TypeError: 'NoneType' object is not callable
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (5 by maintainers)

Top Related StackOverflow Question
Thanks. I was wondering if I should leave this for some new contributor at tomorrow’s sprints, but there are lots of “effort-low/package-novice” issues, so I’ll just do the docs (which could conceivably help people sprinting) 😄.
Just a thought for later: For this to work, I believe we need a reliable way of determining whether a compound model is linear after combining its constituent models, which may or may not be the case. There is some such logic in
_CompoundModelMeta, but, at a glance, it seems quite simplistic and I think the true outcome will depend on the individual models, as well as on the operator. I’m not sure whether this is feasible to do rigorously with a reasonable effort, as alluded to by an existing comment. Currently, however, the|operator produceslinear=Truewhen both operands are linear, which doesn’t seem like it can be generally correct (since the parameters of the first model may get coupled to multiple terms of the second one)?