# Nonlinear curve fitting with weights with LsqFit

**URL:** <https://discourse.julialang.org/t/nonlinear-curve-fitting-with-weights-with-lsqfit/78478>\
**Category:** General Usage\
**Tags:** question, package, curve-fitting, lsqfit\
**Created:** [March 25, 2022, 5:31pm UTC](https://discourse.julialang.org/t/nonlinear-curve-fitting-with-weights-with-lsqfit/78478 "2022-03-25T17:31:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![achjaj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/achjaj/32/22358_2.png) [@achjaj](https://discourse.julialang.org/u/achjaj)\
**Post date:** [March 25, 2022, 5:31pm UTC](https://discourse.julialang.org/t/nonlinear-curve-fitting-with-weights-with-lsqfit/78478/1 "2022-03-25T17:31:08Z")

</div>

Hello,

I need to fit the exponential decay law to some data with weights. I tried to use LsqFit.jl, however the `standard_errors` function returns too low values. I discovered it by comparing my results with my friend and I also made the fit in R.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [March 25, 2022, 8:52pm UTC](https://discourse.julialang.org/t/nonlinear-curve-fitting-with-weights-with-lsqfit/78478/2 "2022-03-25T20:52:09Z")

</div>

Well, if you dont’t show us your code nobody will be able to help you. ):

---

<div class="post-metadata">

**Author:** ![achjaj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/achjaj/32/22358_2.png) [@achjaj](https://discourse.julialang.org/u/achjaj)\
**Post date:** [March 26, 2022, 8:25am UTC](https://discourse.julialang.org/t/nonlinear-curve-fitting-with-weights-with-lsqfit/78478/3 "2022-03-26T08:25:23Z")

</div>

I am sorry, I did not think it would be useful as I just called the relevant functions, but anyway here it is:

```julia
using LsqFit

x = [0, 975, 1767, 2682, 5959]
y = [5490.0, 4546.0, 3679.0, 3134.0, 1451.0]
weights = [109.0, 102.0, 96.0, 89.0, 71.0]

@. model(t, p) = p[1]*(0.5)^(t/p[2])
fit = curve_fit(model, x, y, weights, [0.1, 80])
err = round(standard_errors(fit)[2], sigdigits=1)
T = fit.param[2]

println("$T +- $err")

```

Just to clarify, the `weights` variable contains the stderr of measurement of y.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [March 26, 2022, 11:06am UTC](https://discourse.julialang.org/t/nonlinear-curve-fitting-with-weights-with-lsqfit/78478/4 "2022-03-26T11:06:54Z")

</div>

Sorry, please provide a piece of code that is completely defined. I tried your code and get the message:

```julia

julia> @. model(t, p) = p[1]*(0.5)^(t/p[2])
model (generic function with 1 method)

julia> fit = curve_fit(model, x, y, weights, [0.1, 60])
ERROR: UndefVarError: x not defined
Stacktrace:
 [1] top-level scope
   @ REPL[3]:1

```

What are x, y and the weights?

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

In particular:  
"  
Do your best to make your example **self-contained** (“minimal working example”, **MWE** ), so that it runs (or gets to the error that you want help with) as is. This means including package loading (e.g. `using ThatPackage` ) and any **data** that the code operates on. If your data is large or proprietary, generate **example data** if possible and include that. [This StackOverflow answer](https://stackoverflow.com/questions/61723347/how-to-provide-reproducible-sample-data-in-julia/61724167#61724167) explains how a DataFrame can be turned into a string representation that can be copy/pasted.  
"

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 26, 2022, 11:16am UTC](https://discourse.julialang.org/t/nonlinear-curve-fitting-with-weights-with-lsqfit/78478/5 "2022-03-26T11:16:46Z")

</div>

> [@achjaj](#):
>
> just to clarify, the `weights` variable contains the stderr of measurement of y.

This is probably the issue. In `LsqFit.jl`, the weights are assumed to be the inverse of the variances.

---

<div class="post-metadata">

**Author:** ![achjaj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/achjaj/32/22358_2.png) [@achjaj](https://discourse.julialang.org/u/achjaj)\
**Post date:** [March 26, 2022, 11:27am UTC](https://discourse.julialang.org/t/nonlinear-curve-fitting-with-weights-with-lsqfit/78478/6 "2022-03-26T11:27:17Z")

</div>

I edited the code in my previous post.

---

<div class="post-metadata">

**Author:** ![achjaj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/achjaj/32/22358_2.png) [@achjaj](https://discourse.julialang.org/u/achjaj)\
**Post date:** [March 26, 2022, 11:29am UTC](https://discourse.julialang.org/t/nonlinear-curve-fitting-with-weights-with-lsqfit/78478/7 "2022-03-26T11:29:17Z")

</div>

I tried that also, but the error is still suspiciously low.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 26, 2022, 11:52am UTC](https://discourse.julialang.org/t/nonlinear-curve-fitting-with-weights-with-lsqfit/78478/9 "2022-03-26T11:52:41Z")

</div>

Using the inverse of the variances as weights, we obtain ` using LsqFit` :

```julia
p1 = 5541 +/- 86
p2 = 3135 +/- 110

```

Which from the QC plot below does not seem unreasonable.  
What values do the other software packages compute?

 ![LsqFit_weighted_fit](https://global.discourse-cdn.com/julialang/original/3X/2/1/21e9eb18dbf4a6431d7271b63b27ea37a976619e.png)

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [March 26, 2022, 12:00pm UTC](https://discourse.julialang.org/t/nonlinear-curve-fitting-with-weights-with-lsqfit/78478/10 "2022-03-26T12:00:15Z")

</div>

Here is my take on it

```julia
using LsqFit

x = [0, 975, 1767, 2682, 5959]
y = [5490.0, 4546.0, 3679.0, 3134.0, 1451.0]
weights = [109.0, 102.0, 96.0, 89.0, 71.0]

@. model(t, p) = p[1]*(0.5)^(t/p[2])
fit = curve_fit(model, x, y, [0.1, 80])
err = round(standard_errors(fit)[2], sigdigits=1)
T = fit.param[2]

println("$T +- $err")

@. model(t, p) = p[1]*(0.5)^(t/p[2])
fit = curve_fit(model, x, y, weights, [0.1, 80])
err = round(standard_errors(fit)[2], sigdigits=1)
T = fit.param[2]

println("$T +- $err")

@. model(t, p) = p[1]*(0.5)^(t/p[2])
fit = curve_fit(model, x, y, 1 ./ (weights .* weights), [0.1, 80])
err = round(standard_errors(fit)[2], sigdigits=1)
T = fit.param[2]

println("$T +- $err")

```

yielding

```julia
3158.802386807851 +- 100.0
3170.773189838312 +- 0.1
3134.79231889205 +- 100.0

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 26, 2022, 12:05pm UTC](https://discourse.julialang.org/t/nonlinear-curve-fitting-with-weights-with-lsqfit/78478/11 "2022-03-26T12:05:25Z")

</div>

It is much more clear to write:

```julia
σy = [109., 102., 96., 89., 71.] # standard deviations of y
w = 1 ./ σy.^2 # weights for LsqFit

```

---

<div class="post-metadata">

**Author:** ![achjaj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/achjaj/32/22358_2.png) [@achjaj](https://discourse.julialang.org/u/achjaj)\
**Post date:** [March 26, 2022, 12:22pm UTC](https://discourse.julialang.org/t/nonlinear-curve-fitting-with-weights-with-lsqfit/78478/12 "2022-03-26T12:22:52Z")

</div>

I see the problem now, I used `1/σ` instead of `1/σ^2`. Thank you, for your help!
