# Curve Fitting With Error In Variables

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

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

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.

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

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.

Not 100% in Julia but FWIW, the SciPy.jl package makes it easy to use scipy.odr, which handles errors in both variables.

1 Like

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

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:

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

Good luck.

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.

1 Like