Computing variance of a variable after linear fit using lsqcurvefit function

조회 수: 14 (최근 30일)
P Rakesh Kumar Dora
P Rakesh Kumar Dora 2023년 6월 26일
댓글: Matt J 2023년 6월 27일
I just want to confirm whether the follwings are correct or not. I have two data sets and I want to linealy fit these data sets using the "lsqcurvefit" function. The linear fit equation is given by :. Following is the code, which also computes the covariance matrix of the parametrs (a,b):
clear all;
xdata = [-0.41095 -0.410820 -0.41074 -0.41068 -0.41063 -0.41058 -0.41055 -0.41052 -0.41049 -0.41047];
ydata = [0.166666666666667 0.142857142857143 0.125000000000000 0.111111111111111 0.100000000000000 0.090909090909091 0.083333333333333 0.076923076923077 0.071428571428571 0.066666666666667];
fun = @(A,xdata)(A(1)+A(2)*xdata);
A0 = [1,-1];
A = lsqcurvefit(fun,A0,xdata,ydata);
[A,resnorm,residual,exitflag,output,lambda,J]= lsqcurvefit(fun,A0,xdata,ydata);
ACovariance = inv(J.'*J)*var(residual) %computes the covariance matrix of the parameters a,b in the equation y=a+bx
Is the above code correct for computing the covariance matrix of the fitting parameters?
Now I want to compute the variance of "y" at some point let's say at . Is the following equation correct for computing the variance of : . Where, are the variance of a and b , which are equal to i think the diagonal elements of the matrix "ACovariance" in the code, and is the covariance of the parametrs a,b , which is equal to i think offdiagonal elements of "ACovariance". Following is a derivation of the above equation:
Now variance
where, variance .
Is the above way of computing variance of y at a point , is correct? If the above are incorrect, could you please, point out how to find variance of the variable "y" extrapolated to some arbitary point, after linear fitting the given data sets.
  댓글 수: 1
Matt J
Matt J 2023년 6월 26일
편집: Matt J 2023년 6월 26일
I want to point out that you would never use lsqcurvefit to fit a linear unconstrained model like this. You would just use polyfit.

댓글을 달려면 로그인하십시오.

답변 (1개)

Matt J
Matt J 2023년 6월 26일
It would be,
yvariance = [1,x0]*ACovariance*[1;x0]
  댓글 수: 7
P Rakesh Kumar Dora
P Rakesh Kumar Dora 2023년 6월 27일
Here the point x0 = 0. I expect the standard deviation of y to be around 0.001. I have this number from a reference, I don't know how they compute. I also updated my datasets, in the main question.
Matt J
Matt J 2023년 6월 27일
Well, the std deviation of your residuals is orders of magnitude smaller than that,
>> std(residual)
ans =
4.8895e-06
I don't see how extrapolating to x0=0 ends up magnifying that 1000 times.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by