Ceres:计算参数的不确定性

时间:2018-04-10 12:38:00

标签: ceres-solver

我正在使用Ceres来拟合,并希望得到拟合参数的不确定性。有人建议使用Covariance类,但我不确定我是否正确阅读了文档。以下是我尝试类比文档以获得简单线性拟合的不确定性:

void Fit::fit_linear_function(const std::vector<double>& x, const std::vector<double>& y, int idx_start, int idx_end, double& k, double& d) {

  Problem problem;
  for (int i = idx_start; i <= idx_end; ++i) {
    //std::cout << "i x y "<<i<< " " << x[i] << " " << y[i] << std::endl;
    problem.AddResidualBlock(
        new ceres::AutoDiffCostFunction<LinearResidual, 1,1,  1>(
            new LinearResidual(x[i], y[i])),
        NULL, &k, &d);
  }
  Covariance::Options options;
  Covariance covariance(options);
  std::vector<std::pair<const double*, const double *>> covariance_blocks;
  covariance_blocks.push_back(std::make_pair(&k,&k));
  covariance_blocks.push_back(std::make_pair(&d,&d));
  CHECK(covariance.Compute(covariance_blocks,&problem));
  double covariance_kk;
  double covariance_dd;
  covariance.GetCovarianceBlock(&k,&k, &covariance_kk);
  covariance.GetCovarianceBlock(&d,&d, &covariance_dd);
  std::cout<< "Covariance test k" << covariance_kk<<std::endl;
  std::cout<< "Covariance test d" << covariance_dd<<std::endl;

它编译并生成输出,但结果与我从scipy获得的结果完全不同,所以我一定是犯了错误。

1 个答案:

答案 0 :(得分:1)

解决问题,然后使用ceres :: Covariance类。

http://ceres-solver.org/nnls_covariance.html