-
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
weights in curve_fit #69
Comments
Hi @jcrbloch thanks for reporting the issue! Could you elaborate on why " Under heteroskedastic error where Ω is a diagonal matrix, the GLS estimator has . If , then and β is B.L.U.E. |
To comment further, after looking at issue 71: I think the implementation of the fit with error matrix was perfectly fine, and there is no need to change it. The problem is only when passing an error vector, as i explained in my detailed post above. |
Yes it is a mistake. Thanks very much for pointing out! #72 should fix this. |
I think a possible explanation for passing the reciprocal of standard deviation (σ) to residual function is to reduce the computation needed for Scenario 1: pass weight vector as the reciprocal of estimated variances of error, which is PR #72: var_error =fit.resid.^2
wt = 1./var_error
curve_fit(model, tdata, ydata, wt, p0)
# behind the code
sqrt_wt = sqrt.(wt) # which costs a lot of time
f(p) = sqrt_wt .* ( model(xpts, p) - ydata ) # the residual function for least squares algorithm
fit = lmfit(f, p0, wt; kwargs...)
covar = inv(J'*Diagonal(fit.wt)*J) Scenario 2: Pass weight vector as the reciprocal of estimated standard deviation of error, which is essentially PR #74: std_error = abs.(fit.resid) # which doesn't cost much time
sqrt_wt = 1./std_error
curve_fit(model, tdata, ydata, sqrt_wt, p0)
# behind the code
f(p) = sqrt_wt .* ( model(xpts, p) - ydata ) # the residual function for least squares algorithm
wt = sqrt_wt.^2
fit = lmfit(f, p0, wt; kwargs...)
covar = inv(J'*Diagonal(fit.wt)*J) Just for reference, I ran through 2 different scenarios, Scenario 1 costs nearly 2x time of Scenario 2. The notebook could be viewed and reproduced here, make sure you've restarted kernel. |
@jcrbloch this took a while, but |
I should have opened a new issue for this, so here it comes:
The parameter w for weights in curve_fit is not very thoroughly documented. What I see is:
w: (optional) weight applied to the residual; can be a vector (of length(x) size or empty) or matrix (inverse covariance matrix)
From this I assumed that when providing w as a vector the routine expects the inverse of the variance, as this would be in line with the concept of covariance matrix if w is given as a matrix.
However from my application, and after comparing my results with fits done in C and with gnuplot, it looks as if curve_fit uses the weight vector as inverse standard deviations rather than inverse variances. I had a quick non-expert look in the curve_fit source, and that is also what I think I see in the source code. Could you confirm this and explain the logics?
Kind regards,
Jacques Bloch
The text was updated successfully, but these errors were encountered: