LsqFit.jl
LsqFit.jl copied to clipboard
variable for intercept is being ignored during optimization?
julia> using Plots, LsqFit
julia> x = collect(1:10); y = x.^2; scatter(x,y)
julia> @. model(x,p) = p[1]*x^2 + p[2]
model (generic function with 1 method)
julia> p0 = [ 5. , -20. ];
julia> fit = curve_fit(model, x, y, p0);
julia> plot!(x,model(x,fit.param),label="no bounds")
julia> fit = curve_fit(model, x, y, p0, upper=[+Inf,-20.]);
julia> plot!(x,model(x,fit.param),label="with bounds")
Results in:
I was expecting a greater slope for the result of the fitting, such to minimize the residue. Actually, one gets exactly the same function as the unbounded case, except shifted such that the constant parameter is satisfied.
This can be reproduces even with linear fits, and the result is clearly wrong.
using LsqFit
x = collect(1:10); y = copy(x);
@. model(x,p) = p[1]*x + p[2]
p0 = [ 0. , -5. ];
fit = curve_fit(model,x,y,p0); # no bounds
fit_bounded = curve_fit(model,x,y,p0,upper=[+Inf,-5.]); # with bounds
scatter(x,y)
plot!(x,model(x,fit.param),label="no bounds")
plot!(x,model(x,fit_bounded.param),label="with bounds")
Result:
If one fixes the intercept by hand, one gets the expected outcome:
julia> @. model(x,p) = p[1]*x - 5.
model (generic function with 1 method)
julia> x = collect(1:10); y = x;
julia> fit = curve_fit(model, x, y, p0);
julia> scatter(x,y)
julia> plot!(x,model(x,fit.param))
It's a bit interesting, because your script doesn't actually run, so I'm thinking that maybe you had some variables already assigned? The bounded on has no p0 for example. Anyway, it appears to work on master
julia> coef( curve_fit(model,x,y,p0,upper=[+Inf,-5.]))
2-element Vector{Float64}:
1.2272727144233866
-5.0
julia> coef( curve_fit(model,x,y,p0))
2-element Vector{Float64}:
0.9999999999999999
1.950857761523023e-16
I made a PR that will test this to make sure it doesn't regress..
I fixed the example of the linear fit (there was a p0
missing), and I still get similar results.
The expected outcome would be that the intercept is on -5 and the adjusted line crosses the scatter line, to minimize deviations.
Yet, we get this:
The upper bound of the intercept is not being violated, but it is affecting very badly the result of the fit (maybe it is just a case of bad global convergence).
The title of the issue is bad, anyways.
Okay, interesting. Let me have a look again then.