C++通过Eigen库实现最小二乘法的三种方法

C++通过Eigen库实现最小二乘法的三种方法

1、最小二乘法的数学原理

C++通过Eigen库实现最小二乘法的三种方法

2、矩阵伪逆的C++实现

Maltab中通过pinv函数可以实现矩阵伪逆的求解,那如何在C++中实现矩阵的计算呢,比如Ax=B,这种表达式是在最小二乘法中常见的表达式,如何通过最小二乘法求解出x呢?通过调用Eigen库即可实现最小二乘法。

方法一 :通过SVD分解实现

实现案例:

#include <iostream>

#include <Eigen/Dense>

 

using namespace std;

using namespace Eigen;

 

int main()

{

   MatrixXf A = MatrixXf::Random(3, 2);

   cout << "Here is the matrix A:\n" << A << endl;

   VectorXf b = VectorXf::Random(3);

   cout << "Here is the right hand side b:\n" << b << endl;

   cout << "The least-squares solution is:\n"

        << A.bdcSvd(ComputeThinU | ComputeThinV).solve(b) << endl;

}

方法二 :通过QR分解实现

实现案例:

MatrixXf A = MatrixXf::Random(3, 2);

VectorXf b = VectorXf::Random(3);

cout << "The solution using the QR decomposition is:\n"

     << A.colPivHouseholderQr().solve(b) << endl;

方法三 :通过常规表达式实现

 Ax = b is equivalent to solving the normal equation ATAx = ATb

实现案例

MatrixXf A = MatrixXf::Random(3, 2);

VectorXf b = VectorXf::Random(3);

cout << "The solution using normal equations is:\n"

     << (A.transpose() * A).ldlt().solve(A.transpose() * b) << endl;

 

当矩阵A为病态矩阵时,通过常规表达式求解时效果不好。

 

总结

SVD分解方法最准确,但是运算速度最慢;常规求解方法运算速度快,但准确度低;QR分解在两者之间。