-
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
Add plot recipe for fit result #180
Open
gustaphe
wants to merge
4
commits into
JuliaNLSolvers:master
Choose a base branch
from
gustaphe:plot
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.
Open
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
benchmarks/graphs/* | ||
*~ | ||
*.kate-swp | ||
Manifest.toml | ||
test/*.png | ||
docs/build/ |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,51 @@ | ||
@recipe function f(model::Function, fit::LsqFitResult; significance=0.05, purpose=:neither) | ||
@series begin | ||
seriestype --> :line | ||
label --> "Fit" | ||
x->model(x, fit.param) | ||
end | ||
if purpose in (:confidence, :both) | ||
@series begin | ||
seriestype := :line | ||
seriescolor := :black | ||
linestyle := :dash | ||
label := ["Confidence interval" nothing] | ||
|
||
[ | ||
x -> model(x, fit.param) - margin_error(model, x, fit, significance), | ||
x -> model(x, fit.param) + margin_error(model, x, fit, significance) | ||
] | ||
end | ||
end | ||
if purpose in (:prediction, :both) | ||
@series begin | ||
seriestype := :line | ||
seriescolor := :black | ||
linestyle := :dot | ||
label := ["Prediction interval" nothing] | ||
|
||
[ | ||
x -> model(x, fit.param) - margin_error(model, x, fit, significance; purpose=:prediction) | ||
x -> model(x, fit.param) + margin_error(model, x, fit, significance; purpose=:prediction) | ||
] | ||
end | ||
end | ||
end | ||
|
||
|
||
""" | ||
```julia | ||
margin_error(model, x, fit, significance; purpose) | ||
``` | ||
Find the width at `x` of the confidence or prediction interval. | ||
""" | ||
function margin_error(model::Function, x, fit::LsqFitResult, alpha=0.05; purpose=:confidence) | ||
g = p -> ForwardDiff.gradient(p -> model(x, p), fit.param) | ||
c = g(fit.param)' * estimate_covar(fit) * g(fit.param) | ||
if purpose === :prediction | ||
c = c + 1 | ||
end | ||
dist = TDist(dof(fit)) | ||
critical_values = quantile(dist, 1 - alpha/2) | ||
return sqrt(c*rss(fit)/dof(fit))*critical_values | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
let | ||
@testset "Plotting" begin | ||
model(x, p) = @. p[1] * x^2 + p[2] * x + p[3] + p[4] * exp(-(x - p[5])^2 / p[6]^2) | ||
p_true = [0.5, 0.7, 0.0, 4.5, 0.3, 0.5] | ||
xdata = -4.0:0.5:4.0 | ||
ydata = model(xdata, p_true) + 0.7 * randn(size(xdata)) | ||
|
||
f = curve_fit(model, xdata, ydata, fill(1.0, 6)) | ||
|
||
p = plot( | ||
map((:neither, :prediction, :confidence, :both)) do purpose | ||
plot( | ||
xdata, | ||
ydata; | ||
seriestype=:scatter, | ||
label="Data", | ||
legend_foreground_color=nothing, | ||
legend_background_color=nothing, | ||
legend=:topleft, | ||
) | ||
plot!( | ||
x -> model(x, p_true); | ||
seriestype=:line, | ||
label="Ground truth", | ||
linestyle=:dot, | ||
) | ||
plot!(model, f; purpose=purpose, title="$purpose") | ||
end...; | ||
layout=(2, 2), | ||
) | ||
@test p isa Plots.Plot | ||
savefig(p, "plots.png") | ||
println("Plots saved to `test/plots.png`") | ||
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
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.
I see this was in the original formulation on stackexchange, but where does the 1 come from?
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.
My statistics textbook has a derivation for this, but it's sadly in Swedish. I'll see later today if I can find a good reference. Essentially the difference between prediction and confidence coincidentally comes down to 1 dof.
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 see. You can post the picture of the Swedish text, I'll be able to read it.
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.
Here's the derivation in Seber, G.A.F & Wild, C.J 1989: Statistical inference (My textbook only had it for the linear case). My statistics isn't quite strong enough that I could convert this directly to working code, but I think it contains the source of the
1
.Intuitively, your next value is likely to land in an interval that is the confidence interval plus one "standard deviation" to either side, because that's where it's likely to land given the position of the true mean is inside the confidence interval. I hope this makes sense, otherwise it might be an idea to hold off on this part of the PR.