I am using Ceres to make a fit, and would like to get an uncertainty for the fit parameters. It has been suggested to use the Covariance
class, but I am not sure whether I read the documentation correctly. Here is what I tried in analogy to the documentation to get the uncertainties for a simple linear fit:
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;
It compiles and produces output, but the results are quite off from what I get from scipy
so I must have made a mistake.