Open chriswaudby opened 1 year ago
Hi @pkofod, haven't made a pull request before so don't know the etiquette, but it's been a few weeks since I submitted this - are you able to take a look? Let me know if there's anything else I should be doing!
Thanks, not it's not bad etiquette to bump :) Sorry for not seeing this.
I think sometimes there can be corner cases with automatic differentiation where things can go wrong, so if we are to merge this PR, I think the tests have to be significantly extended to cover the various ways which the user-facing functions can be called: different combinations of inputs, different choices for AD, etc.
This works very well, thanks a lot :D
using LsqFit, Plots, Measurements
@. gauss(x,p) = p[1] + p[2] * exp(-(x-p[3])^2/p[4]^2)
x = range(-1,1,100)
yerr = gauss(x,[0,1,0,0.5]) + 0.1*randn(length(x)) .± 0.1
fit = curve_fit(gauss, x,yerr,Float64[0,1,0,1])
begin
scatter(x,yerr)
plot!(x,gauss(x,fit.param);ribbon = gauss(x,fit.param.+stderror(fit)) - gauss(x,fit.param.-stderror(fit)))
end
savefig("fit.png")
This PR creates a simple extension for the Measurements package, allowing data with uncertainties to be passed directly for fitting, e.g.:
The extension creates a simple wrapper for
LsqFit.curve_fit
that dispatches onydata::AbstractArray{Measurement{T}} where T
. Values and uncertainties are extracted from the input, then weights are calculated as the inverse square of the uncertainties and passed to the maincurve_fit
function.A unit test checks that the extension gives the same results as when weights are passed manually.
An
__init__
function is defined inLsqFit.jl
to load the extension for Julia versions <1.9 (see discussion at https://discourse.julialang.org/t/package-extensions-for-julia-1-9/93397).