MATLAB: Using lscov for weighted least squares; why do the results differ for the same weights

lscovweight vector or diagonal matrix?weighted least squares

Hello,
Using the weighted least squares example on the matlab lscov documentation page…
x1 = [.2 .5 .6 .8 1.0 1.1]';
x2 = [.1 .3 .4 .9 1.1 1.4]';
X = [ones(size(x1)) x1 x2];
y = [.17 .26 .28 .23 .27 .34]';
w = [1 1 1 1 1 .1]';
(1) A call to lscov with the weights as a vector produces…
>> lscov(X,y,w)
ans =
0.1046
0.4614
-0.2621
(2) However, if I represent the weight vector as a diagonal matrix (should be okay) I get very different results, why?
>> lscov(X,y,diag(w))
ans =
0.1310
0.2380
-0.0423
(3) Also, if I instead augment X and y with the weights and run it through ordinary least squares I get similar results to the first try, but still different, why?
>> Xp = diag(w)*X;
>> yp = diag(w)*y;
>> lscov(Xp,yp)
ans =
0.1005
0.4956
-0.2957
Which of these three is "best"?

Best Answer

The correct is
>> lscov(X,y,w)
ans =
0.1046
0.4614
-0.2621
It is equivalent to
>> lscov(X,y,inv(diag(w)))
ans =
0.1046
0.4614
-0.2621
Following is incorrect
Xp = diag(w)*X;
yp = diag(w)*y;
lscov(Xp,yp)
'w' is weight for errors not the values itself.
Related Question