Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
weights in curve_fit #69
I should have opened a new issue for this, so here it comes:
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?
This was referenced
Jun 14, 2018
Hi @jcrbloch thanks for reporting the issue!
Could you elaborate on why "
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.