Skip to content

DM-40487: Add uncertainty to the output of DipoleFitTask#399

Merged
parejkoj merged 7 commits intomainfrom
tickets/DM-40487
May 30, 2025
Merged

DM-40487: Add uncertainty to the output of DipoleFitTask#399
parejkoj merged 7 commits intomainfrom
tickets/DM-40487

Conversation

@parejkoj
Copy link
Contributor

No description provided.

@parejkoj parejkoj force-pushed the tickets/DM-40487 branch from 776784f to 42a6576 Compare May 12, 2025 21:32
Copy link
Contributor

@ebellm ebellm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A math error in uncertainty propagation, and I think it wouldn't be hard to add the covariance.

fitResult.params['ycenNeg'].stderr)
centroid = measBase.CentroidResult((fitResult.params['xcenPos'] + fitResult.params['xcenNeg']) / 2,
(fitResult.params['ycenPos'] + fitResult.params['ycenNeg']) / 2.,
math.sqrt(posCentroid.xErr**2 + negCentroid.xErr**2),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

incorrect error propagation (see e.g. here)

Suggested change
math.sqrt(posCentroid.xErr**2 + negCentroid.xErr**2),
math.sqrt(posCentroid.xErr**2 + negCentroid.xErr**2)/2,

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you can access covar (have to confirm indices):

Suggested change
math.sqrt(posCentroid.xErr**2 + negCentroid.xErr**2),
math.sqrt(posCentroid.xErr**2 + negCentroid.xErr**2 + 2*fitResult.covar[0,1])/2.,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dang it, just forgot the multiplier! Thanks for the catch, though it makes the overall uncertainty even smaller compared to that from SdssCentroid.

I'd missed covar. It looks like scale_covar=True by default, so the covariance is scaled by the reduced chi2 see here, but I think all the uncertainties are similarly scaled, so it should be ok.

I was surprised that the x pos/neg and y pos/neg covariances are both negative (in the "really a dipole" test data), thus further reducing the overall centroid uncertainty compared with SdssCentroid. I don't have a good feeling for how much smaller is appropriate though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not totally surprised the uncertainties end up smaller: we're fitting 3 images instead of just one as in SdssCentroid, so there's more information available to the fitter. But we can investigate further on DM-50839.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In particular, the total centroid uncertainty (0.0088) is now less than the individual positive/negative uncertainties (0.013)! That may be correct, but it's still surprising to me.

centroid = measBase.CentroidResult((fitResult.params['xcenPos'] + fitResult.params['xcenNeg']) / 2,
(fitResult.params['ycenPos'] + fitResult.params['ycenNeg']) / 2.,
math.sqrt(posCentroid.xErr**2 + negCentroid.xErr**2),
math.sqrt(posCentroid.yErr**2 + negCentroid.yErr**2))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
math.sqrt(posCentroid.yErr**2 + negCentroid.yErr**2))
math.sqrt(posCentroid.yErr**2 + negCentroid.yErr**2))/2

(fitParams['ycenPos'] + fitParams['ycenNeg']) / 2.)
dx, dy = fitParams['xcenPos'] - fitParams['xcenNeg'], fitParams['ycenPos'] - fitParams['ycenNeg']
# TODO: We could include covariances, which could be derived from
# `fitResult.params[name].correl`, but those are correlations.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fitResult should have a covar parameter according to this that would provide the covariance matrix--no need to use correl.

@parejkoj parejkoj force-pushed the tickets/DM-40487 branch from 42a6576 to 561faa1 Compare May 20, 2025 21:41
@parejkoj
Copy link
Contributor Author

Ok, covariances added on a new commit.

parejkoj added 7 commits May 30, 2025 10:10
No need to extract fields, just use the record itself; makes for easier
test debugging.
* Cleanup the return struct from `fitDipole` to use CentroidResult,
so that we can just put that in the source record directly.
* I'm not keen on how I'm using the SDSS Centroid uncertainties as the
test expected values, but I don't know a better approach.
* Not including the correlations/covariances, as that math is trickier.
Add centroids and uncertainties.
Use plt.subplots to cleanup display2dArray with better labels.
lmfit now prefers `params` in the MinimizerResult, which include both
the best fit `value` and the `stderr` (and other things). Switching to
this makes it less confusing, since I needed to use `fitResult.params`
in addition to the existing `fitParams`, which were not at all the same
thing!
This makes it more explicit that the positive/negative/total flux
values are going to be the same unless separateNegParams is set, which
it is not by default from the config.
@parejkoj parejkoj force-pushed the tickets/DM-40487 branch from 561faa1 to 6d86b0c Compare May 30, 2025 17:10
@parejkoj parejkoj merged commit a23cd97 into main May 30, 2025
2 checks passed
@parejkoj parejkoj deleted the tickets/DM-40487 branch May 30, 2025 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants