New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bugfix: propagation of measurement uncertainties into parameter covariances #14519
Conversation
Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.
|
dof = len(y) - len(init_values) | ||
self.fit_info["param_cov"] = cov_x * sum_sqrs / dof | ||
self.fit_info["param_cov"] = cov_x | ||
if weights is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also give the option to toggle the behavior like the absolute_sigma
option that scipy.optimize.curve_fit
gives users for the case when no weights are passed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand giving people options is generally a good thing, and the generic name of the kwarg is weights
. But we say in the docstring that weights = 1 / sigma
, which is not true if absolute_sigma == False
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, so then the question becomes "should we add an option to disable this change". That is add an option which allows users to return the behavior of the weights to how they previously worked? My concern here lies with the fact that this change will technically change this computation's output in some cases. This can have knock-on effects for downstream users, who may wish to return to the previous behavior.
@pllim what is your opinion here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is add an option which allows users to return the behavior of the weights to how they previously worked?
Unless the old way is completely wrong, it is generally good to provide backward compatibility.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would argue the old way is completely wrong. We offered a keyword to adjust the weights, but did not compute the result based on the weights.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also – the user can have "perfect" backwards compatibility if they set weights = None
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I told Brett offline, API change is acceptable as long as it is not backported. And if an extra option lets people get the old results, then fine by me but ultimately it is up to @astropy/modeling .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In principle, I am okay with this change so long as it is not backported. However, we should probably advertise the change with a "whats-new". So that it is clear when reading the changes for astropy 5.3 that this behavior will be slightly different.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes look fine to me. Please add a "whats new" item detailing both how the results will change and the circumstance that the results will change under. I think this is necessary since this is a very subtle API change could be difficult to figure out.
8dcc11e
to
3fe6949
Compare
3fe6949
to
044b506
Compare
Thanks @WilliamJamieson! |
.. _whatsnew-5.3-modeling-measurement-uncertainties: | ||
|
||
Support for collapse operations on arbitrary axes in ``nddata`` | ||
=============================================================== |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too late to fix, but the whatsnew section title is wrong. (Please be more careful in future PRs)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yikes! My apologies 😱
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this is super confusing... Is there a way to fix just the doc without doing another release?
https://docs.astropy.org/en/stable/whatsnew/5.3.html#whatsnew-5-3-modeling-measurement-uncertainties
Also, while we're at it, is it normal to have dev tag displayed on the "stable" URL?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will be for the next bugfix release, 5.3 is done, but good point, we need to fix that in the v5.3.x branch.
About the dev tag, this is the goal of #14860. (Not a new issue, just managed to find a solution before forgetting about it ;)).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed it directly on v5.3.x 31f3290
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks !
Description
astropy.modeling
can fit nonlinear models to observations with uncertainties, and return best-fit parameters and their covariances. However, if you increase the measurement uncertainties and re-run the fit, you will find that the parameter covariances do not respond to the changing uncertainties. An example is collapsed below.Example
Example
The code below currently runs without error on
main
, meaning the parameter covariance does not respond to increases in the uncertainties in the observations:If the code were working correctly, the parameter uncertainties should increase.
This problem arises because
astropy.modeling.fitting
renormalizes the covariance matrix on these lines:astropy/astropy/modeling/fitting.py
Lines 1246 to 1248 in eccdccf
An analogous normalization is done in scipy's curve_fit but only in the case where
absolute_sigma == False
. We can see from the definition of this argument in the curve_fit docstring that this choice is equivalent to assuming that only the relative scale of the uncertainties matters, not their actual ("absolute") values.This is not how I (or most astronomers?) might expect the
weight = 1 / sigma
parameter inastropy.modeling
to behave. Usually when we bother to specify uncertainties, we are giving "absolute" ones, not "relative" ones.This PR preserves the current behavior if no measurement uncertainties are provided via the
weights
kwarg. Ifweights is not None
, the renormalization for "relative" sigmas is skipped, and the measurement uncertainties affect the covariance matrix.I've added a test with an ordinary least squares example with heteroscedastic errors, and check that the resulting covariances are correct.