Fit a custom function to data with errors in all dimensions (i.e. nonlinear total least square, deming, odrpack?)

2 views (last 30 days)
Hi,
I have been looking for a Matlab function that can do a nonlinear total least square fit, basically fit a custom function to data which has errors in all dimensions. The easiest case being x and y data-points with different given standard deviations in x and y, for every single point. This is a very common scenario in all natural sciences and just because most people only know how to do a least square fit with errors in y does not mean it wouldn't be extremely useful. I know the problem is far more complicated than a simple y-error, this is probably why most (not even physicists like myself) learned how to properly do this with multidimensional errors.
I would expect that a software like matlab could do it but unless I'm bad at reading the otherwise mostly useful help pages I think even a 'full' Matlab license doesn't provide such fitting functionality. Other tools like Origin, Igor, Scipy use the freely available fortran package "ODRPACK95", for instance. There are few contributions about total least square or deming fits on the file exchange, but they're for linear fits only, which is of little use to me.
I'd be happyfor any hint that can help me out, but I kinda lost the mathematical background to just go and implement this fitting/optimization functionality myself without at least a good paper telling me how to.
kind regards
  5 Comments
Andreas Brand
Andreas Brand on 10 Mar 2017
Edited: Andreas Brand on 10 Mar 2017
new matlab release is out and.... nothing on total least squares, not to mention weighted total least squares. I guess I'll have to switch to python with this too. Less and less reasons remain for me to use matlab :-/
Matthew Cooper
Matthew Cooper on 8 Jul 2021
I've submitted a feature request for this previously. You should too (and anyone reading this). Note that the bionformatics toolbox has something called a 'combined' error model, which is similar but not exactly the same as your 'total least squares' error model. It's better. Total least squares does not account for correlated errors in the x-y or higher dimensions. Total least squares will give a biased fit if the errors are correlated. If you don't think the errors are correlated, why not use principal components? However, if you think they're correlated, then to get an unbiased fit, you need to minimize a different distance (not orthogonal), but the distance depends on the error structure, and requires iteration. Look up York (2004) for a simple implementation in two dimensions. There are file exchange submissions for this. For higher dimensions, you can find methods in various disciplines ranging from econometrics to system identification. I'd bet the econometrics toolbox has something that works. Hopefully the Mathworks engineers will extend the bionformatics error model options to other toolboxes, like statistics and machine learning, where this functionality is really needed!

Sign in to comment.

Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 2 Jan 2017
Well if you want some tools for matlab they aren't too difficult to find: What a quick search at the file exchange offers up
HTH
  5 Comments
Andreas Brand
Andreas Brand on 5 Jan 2017
Edited: Andreas Brand on 5 Jan 2017
@ Bjorn, no worries, I'm not offended, I see your point but I looked through these contribution and the documentation seems very poor (at least to my eye). Various variables unexplained, who am I to say if it'd be mathematically correct to change any of this. Some correlation coefficient decision determines the output... this could be anything for an arbitrary, nonlinear model. I'm talking about this file now, or did you mean another one? total-least-squares (fit_2D)
If John (D'Errico) thinks it's no good advice to try and implement it yourself, I'm tempted to believe him. Having (passively) known him/his work for years I consider it good advice to believe his statements. I'm not so sure it makes sense to fiddle with some code one doesn't understand (or that is poorly documented).
@ Walter, sure you can define custom models for all kinds of fitting & optimization functions in matlab, but I haven't found a single Matlab function (built in or 3rd party) allowing custom models for fitting and then using total least squares, instead of ordinary least squares (only considering the y-distance). If you can point one out that would be greatly appreciated.
so bottom line. I still consider this to be a valid request. I see no obvious solution here yet the necessity to consider x and y errors when fitting models seems obvious to me. I've contacted Mathworks and they told me the same thing as some of you: "There is something on the fileExchange." I find this nonchalant answer by MathWorks a bit frustrating as it tells me they didn't even understand my request, because I was never talking about simple line fits. Every other major tool can to this (especially the expensive ones like Matlab). As a scientist who likes to use Matlab as a set of very accessible high-level tools this left me a bit speechless. I got no response regarding my question if this could be added in a future version.
Bjorn Gustavsson
Bjorn Gustavsson on 6 Jan 2017
I agree with your general opinions, however, I'm a bit more optimistic about this (easy to be since I wont do any of this work for money or research so failures wont bite me). The file I looked at was demoNRM.m, and that seemed possible to modify to more complicated model-functions. I have to admit that I've not looked under the hood of this tool so I have no idea whether it is general enough, but in the case I would start working on TLS I'd start looking at this (and then perhaps other) file exchange contribution(s).
HTH

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!