LsqFit.jl
LsqFit.jl copied to clipboard
Create Measurements package extension
This PR creates a simple extension for the Measurements package, allowing data with uncertainties to be passed directly for fitting, e.g.:
using LsqFit, Measurements
x = [1.0, 2.0, 3.0]
y = [1 ± 0.1, 2.2 ± 0.2, 2.9 ± 0.3]
model(x, p) = p[1] * x
p0 = [1.]
curve_fit(model, x, y, p0)
The extension creates a simple wrapper for LsqFit.curve_fit
that dispatches on ydata::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 main curve_fit
function.
A unit test checks that the extension gives the same results as when weights are passed manually.
An __init__
function is defined in LsqFit.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).