我正在使用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
获得的结果完全不同,所以我一定是犯了错误。