File Exchange

image thumbnail

Linear fit with both uncertainties in x and in y

version 1.2.0.0 (12 KB) by Julien Browaeys
Performs a linear fit with uncertainties in x and y, using a Monte Carlo method

19 Downloads

Updated 25 Oct 2017

View Version History

View License

This tool computes a linear fit when there are uncertainties in x data and y data. The best slope and intercept are computed by minimizing the chi2 calculated using both standard deviation on x and y (Deming fit). Data points are simulated using a Monte Carlo method so as to obtain the error on the fitted parameters.
If error on both x and y is not specified, error is assumed constant on y and computed.
Result of the fit is by default displayed, the best fit and a hull of all the possible fits are drawn.

Cite As

Julien Browaeys (2020). Linear fit with both uncertainties in x and in y (https://www.mathworks.com/matlabcentral/fileexchange/45711-linear-fit-with-both-uncertainties-in-x-and-in-y), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (14)

Julien Browaeys

@Ali please contact me through email (available in the help of the function) as some of your questions are more related to the nature of fine fitting with uncertainties rather than the inner working of the function itself.

Regarding the arguments of the function:
- verbosity states the quantity of informative text that is displayed when the function is executed. There are three levels of verbosity: 0 (nothing), 1 (some text) or anything > 1 (display of all information);
- NSigma is the number of standard deviations you choose to define uncertainties in the fitted parameters. If you want 95% confidence intervals choose NSigma = 1.96. For NSigma = 1, confidence level is around 68 %.

Ali Mehrabifard

Thank you for your reply.
I have kind of solved the issue by replacing the zero elements of both error vectors with a very small value (1e-09). Do you see any problem with this solution to get a fit line? However, the uncertainties associated with the slope and intercepts are very small. Could it be due to the small error (1e-09)? In other words, the fitting line is passing very close to this point with minimal error bars. In fact, are the uncertainties associated with the slope and intercept correct?

I have applied the Orthogonal Distance Regression (ODR) in python, where you could get the linear fit for data with uncertainties in both x and y directions, on the same data where I have replaced the 0 elements in error vectors with 1e-09, and the slope, intercept and their uncertainties are different from your code (the uncertainties are larger than the results of your code). I want to compare the goodness of the fit of your code and the ODR code. To this end, I was wondering if it is possible to get goodness of the fit parameters (e.g. chi-squared/reduced chi-squared, p-value)? I know your code is minimizing the chi2, is it possible to print out the value at the end?

I am not able to get the 95% interval at the output figure as well. Would you mind guiding me how to get this fixed?

I am using the command "linfitxy(xdata, ydata,xerr,yerr,'Verbosity',2, 'NbLoop', 1000, 'NSigma',2);" from example 3, since both xerr and yerr are vectors.
I am not quite sure what the "Verbosity" is and if I choose a number >2, what does that do? Similarly for NSigma.

I would appreciate it, if you address these questions!
Many thanks,
Ali

Ali Mehrabifard

I am not able to get the 95% interval at the output figure as well. Would you mind guiding me how to get this fixed?

I am using the command "linfitxy(xdata, ydata,xerr,yerr,'Verbosity',2, 'NbLoop', 1000, 'NSigma',2);" from example 3, since both xerr and yerr are vectors.
I am not quite sure what the "Verbosity" is and if I choose a number >2, what does that do? Similarly for NSigma.

Many thanks,
Ali

Julien Browaeys

> More than one point with null uncertainty, no fit possible" Is it because I have zero in my error vector?
Yes, precisely. If you have one point with (0,0) uncertainty, then the line fit must go through that point. If you have two of such points, then there is no point fitting, as you already know that the line joining these two points is the only possible solution.

Ali Mehrabifard

I am having x, y data with uncertainties in both direction. The error vectors for x and y are containing zero elements. I am receiving an error ""Error using linfitxy (line 174)
More than one point with null uncertainty, no fit possible"
Is it because I have zero in my error vector?

Valerio Vitali

Great code!
Just one question: which is the level of the confidence lines plotted in the figure? 95%? or 90%?
Thanks

alephnull

Kevin Chen

Simon Melgaard

Jeff Lapierre

Bugra Kaytanli

Brendan Coleman

Rogier Westerhoff

Great code!!
One small thingy: when you do not plot, a figure still appears. I changed that by putting the holdstatus (line 100) to:

if plotting
holdstatus=ishold;
end

and it works.

Olivier Cardoso

MATLAB Release Compatibility
Created with R2011b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!