-
Notifications
You must be signed in to change notification settings - Fork 80
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
Fix estimate_covar in case of !isempty(fit.wt) #241
Conversation
closed and reopened to see if tests run, if not can I maybe ask you to rebase this PR? |
Codecov Report
@@ Coverage Diff @@
## master #241 +/- ##
==========================================
- Coverage 90.34% 90.31% -0.04%
==========================================
Files 4 4
Lines 259 258 -1
==========================================
- Hits 234 233 -1
Misses 25 25
|
I remember we discussed this. We may have made the wrong decision, but I'll try to dig out the old discussions. |
I now remember the details. The reason why it is the way it is is that it is correct if you are inputting the variances as weights. Then there is no reason to estimate the sigma, because it will be 1 by construction (as I think you found out yourself). This is under the assumption that the variance is correct of course. If you calculate it anyway, the estimated variance will be around 1 if you input the variances as weights. Hooowever, I realize that people use weights differently than the classical "weight by the variance" application, and in that case you obviously cannot know that MSE will be one. I'm also not sure the interpretation stays as clear, but maybe that's just a problem for the users and not me :) I think we should maybe implement it as a switch instead of just alway applying it? I agree the |
Hey, thanks for your explanation! After looking at it again I also realized the wt = 1/sigma^2 assumption and that therefore the inv(J'J) was indeed correct. It's maybe a bit confusing also implementation wise, to only use the QR decomposition for the no-weights code path, since it would also be applicable to the weights one. Looking at the documentation (https://julianlsolvers.github.io/LsqFit.jl/latest/tutorial/) again, there's this sentence |
I think I don't mind discussing these things, please keep it coming. I had less time to finish this package than originally planned, so I'm happy that someone keeps it alive by questioning the implementations. |
As per https://julianlsolvers.github.io/LsqFit.jl/latest/tutorial/#Goodness-of-Fit-1 there is a factor fit.wt missing in the covariance calculation for a weighted fit.
Reading your documentation again, I think you're correct. I changed the commit accordingly, since on the current master this factor fit.wt is missing. |
I think you're wrong. If you look at all the curvefit calls, the sort or chol of wt is called u and is applied at the objective type level, so the J you're seeing in the code already has the "sqrt" weight applied. |
You are of course right again, I didn't realise that the code uses a different J from the documentation. |
While trying to figure out how LsqFit determines the covariance of the fit parameters, I found this inconsistency between the two used methods in
estimate_covar
. According to your own docs this factor of sigma^2 is missing at one occasion. I also documented the relation used for the QR decomposition because at least for me this wasn't obvious.I'm also not sure why there is this special casing for isempty(fit.wt) in the first place since the two methods are mathematically identical but I didn't want to make any wrong assumptions here and therefore only added the fix to the existing case.
Edit: Looking a bit through git blame, it seems like the original idea was to use the weights in this position:
55eefde#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5R76
Which was then later removed.
To me, using the inverse weights as variance might somehow make sense, but in the current state I'm pretty sure it is just wrong, it is equivalent to setting sigma^2 = 1.