-
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
Create Measurements package extension #248
Open
chriswaudby
wants to merge
3
commits into
JuliaNLSolvers:master
Choose a base branch
from
chriswaudby:MeasurementsExtension
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+107
−2
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module MeasurementsExt | ||
|
||
using LsqFit | ||
isdefined(Base, :get_extension) ? (using Measurements) : (using ..Measurements) | ||
|
||
function LsqFit.curve_fit( | ||
model, | ||
xdata::AbstractArray, | ||
ydata::AbstractArray{Measurement{T}} where T, | ||
p0::AbstractArray; | ||
inplace=false, | ||
kwargs..., | ||
) | ||
y = Measurements.value.(ydata) | ||
ye = Measurements.uncertainty.(ydata) | ||
wt = ye .^ -2 | ||
curve_fit(model, xdata, y, wt, p0; inplace=inplace, kwargs...) | ||
end | ||
|
||
function LsqFit.curve_fit( | ||
model, | ||
jacobian, | ||
xdata::AbstractArray, | ||
ydata::AbstractArray{Measurement{T}} where T, | ||
p0::AbstractArray; | ||
inplace=false, | ||
kwargs..., | ||
) | ||
y = Measurements.value.(ydata) | ||
ye = Measurements.uncertainty.(ydata) | ||
wt = ye .^ -2 | ||
curve_fit(model, jacobian, xdata, y, wt, p0; inplace=inplace, kwargs...) | ||
end | ||
|
||
end # module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using LsqFit | ||
using Measurements | ||
using StableRNGs | ||
|
||
@testset "MeasurementsExt" begin | ||
model(x, p) = p[1] .* exp.(-x .* p[2]) | ||
ptrue = [10, 0.3] | ||
x = LinRange(0, 2, 50) | ||
y0 = model(x, ptrue) | ||
σ = rand(1:5, 50) | ||
y = y0 .± σ | ||
wt = σ .^ -2 | ||
fit0 = curve_fit(model, x, y0, wt, ptrue) # fit to data using weights | ||
fit1 = curve_fit(model, x, y, ptrue) # fit to data using Measurements | ||
@test coef(fit0) ≈ coef(fit1) | ||
|
||
# some example data | ||
rng = StableRNG(125) | ||
x = range(0, stop = 10, length = 20) | ||
y0 = model(x, [1.0, 2.0]) + 0.01 * randn(rng, length(x)) | ||
σ = 0.01 | ||
y = y0 .± σ | ||
wt = ones(length(x)) * σ .^ -2 | ||
p0 = [0.5, 0.5] | ||
|
||
for ad in (:finite, :forward, :forwarddiff) | ||
fit0 = curve_fit(model, x, y0, wt, p0; autodiff = ad) | ||
fit1 = curve_fit(model, x, y, p0; autodiff = ad) | ||
for fit in (fit0, fit1) | ||
@test norm(fit.param - [1.0, 2.0]) < 0.05 | ||
@test fit.converged | ||
|
||
# can also get error estimates on the fit parameters | ||
errors = margin_error(fit, 0.05) | ||
@test norm(errors - [0.021, 0.093]) < 0.01 | ||
end | ||
end | ||
# if your model is differentiable, it can be faster and/or more accurate | ||
# to supply your own jacobian instead of using the finite difference | ||
function jacobian_model(x, p) | ||
J = Array{Float64}(undef, length(x), length(p)) | ||
J[:, 1] = exp.(-x .* p[2]) #dmodel/dp[1] | ||
J[:, 2] = -x .* p[1] .* J[:, 1] #dmodel/dp[2] | ||
J | ||
end | ||
jacobian_fit0 = curve_fit(model, jacobian_model, x, y0, wt, p0;show_trace=true) | ||
jacobian_fit1 = curve_fit(model, jacobian_model, x, y, p0;show_trace=true) | ||
for jacobian_fit in (jacobian_fit0, jacobian_fit1) | ||
@test norm(jacobian_fit.param - [1.0, 2.0]) < 0.05 | ||
@test jacobian_fit.converged | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR's implementation seems to ignore correlation between measurements.
The point of
Measurements.jl
, the only reason they use a very involved implementation (compared to a much simpler one as inUncertain.jl
), is tracking correlation between variables. Ignoring them for fitting can easily be surprising for the user if they even catch it – otherwise it directly leads to incorrect results.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think maybe the covariance matrix might have to be used here instead, but I dont know if how the weights would need to be adjusted for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a monte carlo simulation of a fit could be used as a test case? That way, we know that the uncertainties are taken into account correctly.
Possibly something like this:
which samples from a distribution according to a given correalation matrix and makes a fit. The same result should then be reproduced by this code with Measurements.jl with a given covariance matrix, at least approximately.