我正在使用 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
所以我一定犯了一个错误。