需要精确的最小二乘拟合算法
我已经尝试了两种实现最小二乘拟合(LSF)算法的方法,如图所示此处。
第一个代码只是教科书方法,如 Wolfram 在 LSF 上的页面所述。第二个代码重新排列方程以最小化机器错误。这两个代码对我的数据产生相似的结果。我将这些结果与 Matlab 的 p=polyfit(x,y,1) 函数进行了比较,使用相关系数来衡量拟合的“优度”并比较 3 个例程中的每一个。我观察到,虽然所有 3 种方法都产生了良好的结果,但至少对于我的数据而言,Matlab 的例程最适合(其他 2 种例程的结果彼此相似)。
Matlab 的 p=polyfit(x,y,1) 函数使用 Vandermonde 矩阵、V(nx 2 矩阵)和 QR 分解来解决最小二乘问题。在Matlab代码中,它看起来像:
V = [x1,1; x2,1; x3,1; ... xn,1] % this line is pseudo-code
[Q,R] = qr(V,0);
p = R\(Q'*y); % performs same as p = V\y
我不是数学家,所以我不明白为什么它会更准确。虽然差异很小,但在我的例子中,我需要从 LSF 获取斜率并将其乘以一个大数,因此准确性的任何改进都会显示在我的结果中。
由于我无法理解的原因,我无法在工作中使用 Matlab 的例程。因此,我想知道是否有人有更准确的基于方程的方法建议,我可以使用它,在舍入误差/机器精度/等方面,这是对上述两种方法的改进。
任何意见表示赞赏!提前致谢。
I've experimented with the two ways of implementing a least-squares fit (LSF) algorithm shown here.
The first code is simply the textbook approach, as described by Wolfram's page on LSF. The second code re-arranges the equation to minimize machine errors. Both codes produce similar results for my data. I compared these results with Matlab's p=polyfit(x,y,1) function, using correlation coefficients to measure the "goodness" of fit and compare each of the 3 routines. I observed that while all 3 methods produced good results, at least for my data, Matlab's routine had the best fit (the other 2 routines had similar results to each other).
Matlab's p=polyfit(x,y,1) function uses a Vandermonde matrix, V (n x 2 matrix) and QR factorization to solve the least-squares problem. In Matlab code, it looks like:
V = [x1,1; x2,1; x3,1; ... xn,1] % this line is pseudo-code
[Q,R] = qr(V,0);
p = R\(Q'*y); % performs same as p = V\y
I'm not a mathematician, so I don't understand why it would be more accurate. Although the difference is slight, in my case I need to obtain the slope from the LSF and multiply it by a large number, so any improvement in accuracy shows up in my results.
For reasons I can't get into, I cannot use Matlab's routine in my work. So, I'm wondering if anyone has a more accurate equation-based approach recommendation I could use that is an improvement over the above two approaches, in terms of rounding errors/machine accuracy/etc.
Any comments appreciated! thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于多项式拟合,您可以创建一个 Vandermonde 矩阵并求解线性系统,就像您已经完成的那样。
另一种解决方案是使用 Gauss-Newton 等方法来拟合数据(因为系统是线性的,一次迭代应该就可以了)。这些方法之间存在差异。一个可能的原因是龙格现象。
For a polynomial fitting, you can create a Vandermonde matrix and solve the linear system, as you already done.
Another solution is using methods like Gauss-Newton to fit the data (since the system is linear, one iteration should do fine). There are differences between the methods. One possibly reason is the Runge's phenomenon.