📄 leastsqr.asv
字号:
function [parameter, variance, cov, x0, s0] = leastqur(x,y)
%LEASTQUR Least square computation
% PARAMETER = LESTQUR(X, Y) performs least square computation on N-by-(P+1)
% data matrix X and N-by-1 vector Y, returns the final parameter; Rows of
% X correspond to observations, column to variables. Rows of Y and rows
% of X is coherently related; our task is to estimated the relationship
% between them.
%
% This arithmatic assumes the relationship between X and Y is linear and
% X isnot random, moreover, each observation is irrelevant and
% equally-weighted.
%
% [PARAMETER, VARIANCE] = LEASTSQR(X, Y) returns the variance of Y.
%
% [PARAMETER, VARIANCE, COV] = LEASTSQR(X, Y) returns the covariance
% matrix of the returned PARAMETER.
%
% $Date: 2008/02/25 16:39:01 $
[n,p] = size(x);
[n2,p2] = size(y);
if n < (p-1)
warning('The observation isnot big enough for sufficient estimation(n<p-1)');
else
if n ~= n2
warning('Row size of x and y should be equal');
else
if p2 >1
warning('There should be only one column in y');
else
% Center X by substracting off the column means
x0 = x - repmat(mean(x,1),n,1);
x0(:,1) = 1;
s0 = x0'*x0;
parameter = inv(s0)*x0'*y;
rss = y'*y - parameter'*x0'*y;
variance = rss/(n-p-1);
cov = variance*inv(s0);
end
end
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -