MATLAB: In the Curve Fitting GUI, are “weights” the magnitude of your error bars, or the reciprocal of that? (Or something else?)

curve fittingMATLAB

I have a vector of x values, a vector of y values, and a vector of error bars for those y values. I want to create a weighted linear least squares fit. In the Curve Fitting Tool GUI, how do I input the error bars. Do I need to take the reciprocal before putting this into weights? Or do they go directly in? Or do I do something else?

Best Answer

A good way to think of a weight in a weighted regression is as a replication factor. That is, if all of my data points have a weight of 1, except I'll assign a weight of k (for nominally integer k) to one of the data points. This should be equivalent to simply replicating that same data point k times.
For example, as a test, I should get the same estimates for a linear polynomial fit here:
x1 = [1 2 3];
y1 = [1 3 6];
w = [1 1 2];
x2 = [1 2 3 3];
y2 = [1 3 6 6];
P1 = fit(x1',y1','poly1','weight',w)
P2 = fit(x2',y2','poly1')
So here the third point has either a weight of 2, or I replicated that point, so now there are two samples at that location.
Note that the confidence intervals will be different, because in one case we have 4 points, and in the other, we have 3 points. I assume that will probably screw with the t statistics.
Anyway, if you think of a weight in terms of replication factor, then you can think of it as also the inverse of the square root of the error bar width. That is, if point K has error bars twice as wide as point L, then point K should have a weight of 1/sqrt(2) relative to point L.
Next, you need to be careful, in that the weight vector is unaffected by scaling. If you scale all of the weights by some constant factor k, then nothing changes.