tBuLi / symfit

Symbolic Fitting; fitting as it should be.
http://symfit.readthedocs.org
MIT License
235 stars 19 forks source link

ODR for errors in independent variables #101

Open tBuLi opened 7 years ago

tBuLi commented 7 years ago

The ODR approach allows for measurement errors to be provided to all variables. symfit already supports that syntax, now all that has to be done is make it work by building a new ODRFit object.

https://docs.scipy.org/doc/scipy/reference/odr.html

Before implementing this feature a refactoring of the object structure for fitting objects is needed, from which the Global Fitting PR will also benefit.

Syntax example:

from symfit import variables, parameters, Fit

x, y = variables('x, y')
a, b = parameters('a, b')

model = {y: a * x + b}
fit = Fit(model, x=xdata, y=ydata, sigma_x=xerr, sigma_y=yerr)
pckroon commented 5 years ago

I think @Jhsmit needs this for his http://github.com/jhsmit/colicoords/ project. If I understand correctly, he fits radial distribution functions, which can be significantly sensitive to errors in independent coordinates. In this case it shouldn't be too bad since the sigma_x is in the order of the size of a pixel (I think), assuming there's no larger error coming from the microscope (at least half the wavelength used).

To make this happen in the current framework all that needs to be done is to make a new Objective, and put the brains in Fit to make it choose the correct objective if a sigma_x is given.

See also:

tBuLi commented 5 years ago

I agree that to implement the basis should be very straight forward. One of the things that was stopping me is that we have not yet found sources on how to generalize this to multivariate vector functions like our models.

But perhaps that is premature optimization; we could implement it in such a way that the objective only accepts scalar models of one variable for now.

pckroon commented 5 years ago

IIRC the first link I put does mention it (but it might be multiple independent variables). Either way, it's effectively a log-likelihood (since units of dependent and independent variables usually differ), so combine as we do there.

bytelinker commented 3 years ago

Hi tBuLi, there is some documentation about sigma_x, using it I get "got an unexpected keyword argument 'sigma_x'". So I think it is not implemented yet. Would be good, being able to use odr with bounds. Happy New Year ByteLinker

pckroon commented 3 years ago

Hi ByteLinker,

unfortunately ODR is not implemented yet, since neither of us had time to look into it. Now that someone is actually interested in using it that may change though. But don't wait for it. For your immediate needs, scipy can do ODR fitting. I'm not familiar with the interface though.