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