# Curve Fitting With Error In Variables

**URL:** <https://discourse.julialang.org/t/curve-fitting-with-error-in-variables/82614>\
**Category:** Optimization (Mathematical)\
**Tags:** curve-fitting\
**Created:** [June 12, 2022, 1:40am UTC](https://discourse.julialang.org/t/curve-fitting-with-error-in-variables/82614 "2022-06-12T01:40:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vini-fda](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vini-fda/32/29448_2.png) [@vini-fda](https://discourse.julialang.org/u/vini-fda)\
**Post date:** [June 12, 2022, 1:40am UTC](https://discourse.julialang.org/t/curve-fitting-with-error-in-variables/82614/1 "2022-06-12T01:40:57Z")

</div>

I’m looking for a way to do curve fitting where the data has error both on x and y. I’ve read up on physics forums about [errors-in-variables](https://www.wavemetrics.com/products/igorpro/dataanalysis/curvefitting/errorsinvariables) fitting, but I’d like to know if there are already any functionalities that julia provides to make this sort of regression.

Specifically, my problem is to try and fit a curve for the reflectance of an Electromagnetic Wave(red laser) which s-polarized(TE mode). The theoretical curve depends on the angle of incidence \theta\_i and the relative index of refraction n, with a formula that is given by the Fresnel equation:

R\_s = |\Gamma^{\text{TE}}|^2 = \left| \frac{\sqrt{n^2 - \sin^2(\theta\_i)} - \cos \theta\_i}{\sqrt{n^2 - \sin^2(\theta\_i)} + \cos \theta\_i} \right|^2

There’s also the problem that, if n\<1, there might be total internal reflection, so the curve will be discontinuous at the critical angle \theta\_L (image taken from [wikipedia](https://en.wikipedia.org/wiki/Fresnel_equations#/media/File:Fresnel_power_glass-to-air.svg)):

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

This is the experimental data, with x and y uncertainties:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/4/3425c5e58c40d3aaf6ae57d8d3f6ce5564dcf8f1.png)

Currently, my solution was to just ignore the uncertainties and use Optim.jl, but I’d like to know if there’s a better way, preferably accounting for the uncertainties.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [June 12, 2022, 1:59am UTC](https://discourse.julialang.org/t/curve-fitting-with-error-in-variables/82614/2 "2022-06-12T01:59:00Z")

</div>

I’d let people help with more specific suggestions but maybe you could try to represent your x and y with this package:

[https://github.com/JuliaPhysics/Measurements.jl](https://github.com/JuliaPhysics/Measurements.jl)

---

<div class="post-metadata">

**Author:** ![vini-fda](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vini-fda/32/29448_2.png) [@vini-fda](https://discourse.julialang.org/u/vini-fda)\
**Post date:** [June 12, 2022, 4:23am UTC](https://discourse.julialang.org/t/curve-fitting-with-error-in-variables/82614/3 "2022-06-12T04:23:11Z")

</div>

Thanks! Yes, I’m aware of the existence of Measurements.jl, with which I created the plot with the experimental data. But it seems that there is no integration with other packages such as LsqFit.jl and Optim.jl. [There’s an issue on LsqFit.jl’s Github about interplay with Measurements.jl, by the way](https://github.com/JuliaNLSolvers/LsqFit.jl/issues/143).

---

<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:** [June 12, 2022, 6:19am UTC](https://discourse.julialang.org/t/curve-fitting-with-error-in-variables/82614/4 "2022-06-12T06:19:48Z")

</div>

Not 100% in Julia but FWIW, the [SciPy.jl package](https://github.com/AtsushiSakai/SciPy.jl) makes it easy to use [scipy.odr](https://docs.scipy.org/doc/scipy/reference/odr.html), which handles errors in both variables.

---

<div class="post-metadata">

**Author:** ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)\
**Post date:** [September 3, 2022, 7:25pm UTC](https://discourse.julialang.org/t/curve-fitting-with-error-in-variables/82614/5 "2022-09-03T19:25:12Z")

</div>

For the problem of errors-in-variables, the total least squares estimator would be a solution:

> **[GitHub - baggepinnen/TotalLeastSquares.jl: Solve many kinds of least-squares...](https://github.com/baggepinnen/TotalLeastSquares.jl)**
>
> Solve many kinds of least-squares and matrix-recovery problems - GitHub - baggepinnen/TotalLeastSquares.jl: Solve many kinds of least-squares and matrix-recovery problems

However, your function is highly non-linear. Maybe a Taylor/McLaurin expansion would be a solution so you can construct a design matrix of independent variables.

Eive is an other option if you can expand your function to series and construct a design matrix:

> **[GitHub - jbytecode/ErrorsInVariables.jl: Errors-in-variables estimation in...](https://github.com/jbytecode/ErrorsInVariables.jl)**
>
> Errors-in-variables estimation in linear regression using Compact Genetic Algorithms - GitHub - jbytecode/ErrorsInVariables.jl: Errors-in-variables estimation in linear regression using Compact Gen...

The latter is my attempt with a citation and I have just submitted to the Julia repository.

Good luck.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [September 4, 2022, 4:27am UTC](https://discourse.julialang.org/t/curve-fitting-with-error-in-variables/82614/6 "2022-09-04T04:27:19Z")

</div>

Since I just saw this revived today, I’d strongly recommend writing a Bayesian model for this problem. Turing,.jl would be a good choice.
