简体   繁体   English

在 Matlab 中使用最小二乘法进行参数估计

[英]Parameters Estimation using Least Square Method in Matlab

I have the next questions: Consider a set of equations y=ax+b where i know the y and x and want to estimate the a and b using least square method.我有下一个问题:考虑一组方程y=ax+b ,其中我知道 y 和 x 并想使用最小二乘法估计ab Let's assume to have Y=[y1 ; y2]让我们假设有Y=[y1 ; y2] Y=[y1 ; y2] and Y=[y1 ; y2]
A=[x1 1; x2 1] A=[x1 1; x2 1] so that Y=A*[a;b] A=[x1 1; x2 1]使得Y=A*[a;b]

According to least square method : B=[a;b]=( transpose(A)*A )^-1*transpose(A)*Y根据最小二乘法: B=[a;b]=( transpose(A)*A )^-1*transpose(A)*Y

  1. (A'*A) \\ A'*Y and A\\Y are the same? (A'*A) \\ A'*YA\\Y是一样的吗?

  2. Which is the best method to calculate B:哪个是计算 B 的最佳方法:

    inv( transpose(A)*A ) *transpose(A)*Y

    (transpose(A)*A) \\ transpose(A)*Y

    (A'*A) \\ A'*Y

    pinv(A)*Y (calculate the pseudo-inverse matrix) pinv(A)*Y (计算伪逆矩阵)

all the above give slightly different results以上所有给出的结果略有不同

Before solving your doubts, a remark is mandatory.在解决您的疑问之前,必须进行评论。 When you want to transpose a matrix using the shorthand operator... you should not use ' , but .'当您想使用速记运算符转置矩阵时……您不应该使用' ,而是.' . . The first one is the shorthand operator for the conjugate transpose while the second one is the correct shorthand operator to use for the transpose .第一个是共轭转置的速记运算符,而第二个是用于transpose的正确速记运算符。 While they normally produce the same result, using the former with matrices containing complex numbers could mess up your calculations.虽然它们通常会产生相同的结果,但将前者与包含复数的矩阵一起使用可能会扰乱您的计算。

Since you didn't provide a data sample, here are the settings I deployed for my tests:由于您没有提供数据样本,以下是我为测试部署的设置:

Y = [2; 4];
A = [3 1; 7 1];

Now, let's go step-by-step.现在,让我们一步一步来。 For what concerns your first answer, yes, the two operations are equivalent on a mathematical point of view and produce basically the same result:对于您的第一个答案,是的,从数学角度来看,这两个操作是等效的,并且产生的结果基本相同:

>> B = A \ Y

B =
    0.5
    0.5

-----------------------------

>> B = inv(A.' * A) * A.' * Y

B =
    0.500000000000001
    0.5

The slight difference you see is due to the fact that INV(A) * b is less accurate than A \\ b , and this is clearly stated even by the Matlab code interpreter if you hover a call to the inv function followed by a multiplication (which should be marked with an orange warning highlight):您看到的细微差别是由于INV(A) * b不如A \\ b准确,如果您将对inv函数的调用悬停在乘法之后,即使 Matlab 代码解释器也清楚地说明了这一点(应标有橙色警告突出显示):

警告

This partially answers also your second question, but let's do an exhaustive benchmark.这也部分回答了您的第二个问题,但让我们做一个详尽的基准测试。 I discarded the computation performed using inv(A.' * A) * A.' * Y我放弃了使用inv(A.' * A) * A.' * Y执行的计算inv(A.' * A) * A.' * Y since it is recommended to avoid it. inv(A.' * A) * A.' * Y因为建议避免它。 Here we go:开始了:

tic();
for i = 1:100000
    B = A \ Y;
end
toc();

tic();
for i = 1:100000
    B = pinv(A) * Y;
end
toc();

tic();
for i = 1:100000
    B = (A.' * A) \ A.' * Y;
end
toc();

This is the result of the benchmark:这是基准测试的结果:

Elapsed time is 0.187067 seconds.
Elapsed time is 2.987651 seconds.
Elapsed time is 2.173117 seconds.

Given that the three approaches have the same accurancy... the first one is by far, and obviously, the fastest one.鉴于这三种方法具有相同的准确度……第一种方法显然是迄今为止最快的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM