如何理解最小二乘法?如何程序实现。

最小二乘法,个人觉得叫“最小平方法”更恰当。

本文以下这篇文章的程序实现。

使用了Ceres优化库。

https://mp.weixin.qq.com/s/4e9ZiiGIOWx_ZUGjzgavWw

 1 #include <iostream>
 2 #include <ceres/ceres.h>
 3 using namespace std;
 4 using namespace ceres;
 5 
 6 struct CURVE_FITTING_COST
 7 {
 8     CURVE_FITTING_COST(double x, double y):_x(x),_y(y){}
 9     template <typename T>
10     bool operator()(const T* const ab, T* residual) const
11     {    
12         //y-0*x+a;
13         //y-ax-b
14         residual[0] = T(_y) - ab[0]*T(_x) - ab[1];
15         return true;
16         
17     }
18     const double _x,_y;
19     
20 };
21 
22 int main(int argc, char** argv)
23 {
24     double x[5] = {25, 27, 31, 33, 35};
25     double y[5] = {110, 115, 155, 160, 180}; 
26     double ab[2]={0,0};
27 
28     //构建最小平方问题
29     Problem problem;
30     for(int i=0; i<5; i++)
31     {
32         problem.AddResidualBlock(
33             new AutoDiffCostFunction<CURVE_FITTING_COST,1,2>(new CURVE_FITTING_COST(x[i], y[i])),
34             nullptr,
35             ab
36         );
37         
38     }
39     
40     Solver::Options options;
41     options.linear_solver_type = DENSE_QR;
42     options.minimizer_progress_to_stdout = true;
43 
44     Solver::Summary summary;
45     Solve(options, &problem, &summary);
46 
47     cout<<"estimated a = ";
48     for(auto a:ab) cout<<a<<" ";
49     cout<<endl;
50 }

输出结果为:

  

➜ build ./least_square
iter cost cost_change |gradient| |step| tr_ratio tr_radius ls_iter iter_time total_time
0 5.367500e+04 0.00e+00 2.22e+04 0.00e+00 0.00e+00 1.00e+04 0 1.27e-04 2.07e-04
1 4.723331e+01 5.36e+04 3.31e+00 7.21e+01 1.00e+00 3.00e+04 1 2.00e-04 8.96e-04
2 4.709303e+01 1.40e-01 9.81e-03 1.93e+00 1.00e+00 9.00e+04 1 1.10e-04 1.03e-03
estimated a,b = 7.20902 -73.7123

猜你喜欢

转载自www.cnblogs.com/wherewhenwho/p/9173506.html
今日推荐