# LsqFit curve fit does not consider bounds on intercepts correctly

**URL:** <https://discourse.julialang.org/t/lsqfit-curve-fit-does-not-consider-bounds-on-intercepts-correctly/48233>\
**Category:** General Usage\
**Created:** [October 12, 2020, 3:36pm UTC](https://discourse.julialang.org/t/lsqfit-curve-fit-does-not-consider-bounds-on-intercepts-correctly/48233 "2020-10-12T15:36:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 12, 2020, 3:36pm UTC](https://discourse.julialang.org/t/lsqfit-curve-fit-does-not-consider-bounds-on-intercepts-correctly/48233/1 "2020-10-12T15:36:48Z")

</div>

I am trying to get some fits with `LsqFit` with bounds, but I am observing strange results which seem to imply that `LsqFit` is minimizing the correlation coefficient but not the residue of the fit. This is a minimal example, with a linear fit, in which I add a bound to the intercept coefficient:

```julia
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,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:

![fit](https://global.discourse-cdn.com/julialang/original/3X/3/4/34d31f22236e3f42371ccadf0fca7836b5a3f4fe.png)

Of course that is not the line with the minimal residue sum, it is just the same fit, but shifted downwards.

Am I doing or interpreting something wrong?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [October 12, 2020, 5:00pm UTC](https://discourse.julialang.org/t/lsqfit-curve-fit-does-not-consider-bounds-on-intercepts-correctly/48233/2 "2020-10-12T17:00:31Z")

</div>

You’re constraining the parameter vector to not allow an intercept greater than -5,so how could you possibly get anything else? Did you mean to wrote +5?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 12, 2020, 5:20pm UTC](https://discourse.julialang.org/t/lsqfit-curve-fit-does-not-consider-bounds-on-intercepts-correctly/48233/3 "2020-10-12T17:20:28Z")

</div>

I was expecting a greater slope, such that the fit cross the data, that would have a smaller residue relative to the data, while satisfying the bound.

Edit: which can be demonstrated if the model is defined with that parameter equal to -5 from start:

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

```

Results in (the expected result):

![fit](https://global.discourse-cdn.com/julialang/original/3X/0/a/0a075e903c2373702f0d92969ee5cec4e60950ea.png)

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [October 12, 2020, 5:32pm UTC](https://discourse.julialang.org/t/lsqfit-curve-fit-does-not-consider-bounds-on-intercepts-correctly/48233/4 "2020-10-12T17:32:17Z")

</div>

Ah, good point! The gradient for the slope parameter is probably zero in the point in the figure?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 12, 2020, 6:06pm UTC](https://discourse.julialang.org/t/lsqfit-curve-fit-does-not-consider-bounds-on-intercepts-correctly/48233/5 "2020-10-12T18:06:51Z")

</div>

No, it is not. The result is independent on the initial point as well.

Also, this kind of problem occurs with any model, for example:

```julia
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")

```

Result:

![fit](https://global.discourse-cdn.com/julialang/original/3X/4/3/43ab595a9a22e36c169596679fafce75a97ba6f2.png)

Edit: I have opened an issue: [variable for intercept is being ignored during optimization? · Issue #167 · JuliaNLSolvers/LsqFit.jl · GitHub](https://github.com/JuliaNLSolvers/LsqFit.jl/issues/167)

If that is the expected result for some reason, please let me know.
