LsqFit.jl icon indicating copy to clipboard operation
LsqFit.jl copied to clipboard

variable for intercept is being ignored during optimization?

Open lmiq opened this issue 4 years ago • 5 comments

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:

image

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.

lmiq avatar Oct 12 '20 18:10 lmiq

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:

image

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))

image

lmiq avatar Oct 12 '20 18:10 lmiq

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

pkofod avatar Sep 03 '22 18:09 pkofod

I made a PR that will test this to make sure it doesn't regress..

pkofod avatar Sep 03 '22 18:09 pkofod

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:

image

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.

lmiq avatar Sep 03 '22 20:09 lmiq

Okay, interesting. Let me have a look again then.

pkofod avatar Sep 05 '22 14:09 pkofod