I am performing a binned likelihood fit with RooFit. The model PDF is obtained from some histogram templates.
Is there a way to obtain the Baker-Cousin chi-square? I was thinking the RooStats::ProfileLikelihoodCalculator class could be the solution, but I am not sure about it.
The function to compute directly from a RooFit model is not yet available, but it exists in TH1,
(see TH1::Chisquare(TF1 * f1, "L")[ROOT: TH1 Class Reference].
To use it in RooFit you need to convert the data and the model. I include here a macro as an example:
// example of computing Baker-Cousins chi2 from a RooFit model
void exampleBakerCousins() {
// make model
RooWorkspace w;
w.factory("Gaussian::sig(x[-5,5],m[0,-5,5],s[0.5,0,10])");
w.factory("Bernstein::bkg(x,{a[1,-5,5],b[2,-5,5],c[0.5,-5,5]})");
w.factory("SUM::pdf(ns[100,0,1000]*sig,nb[1000,0,10000]*bkg)");
auto x = w.var("x");
auto pdf = w.pdf("pdf");
auto pl = x->frame();
auto data = pdf->generateBinned(*x,10000);
data->plotOn(pl);
pdf->plotOn(pl);
pl->Draw();
// compute Baker-Cousins chi2:
// converting model to a TF1 and data to a TH1
auto fn = pdf->asTF(*x, RooArgList(), *x);
auto h1 = data->createHistogram("x");
// pdf is normalized, need to scale it to number of events * bin width
auto f = new TF1("f1",[&](double *x, double *) { return h1->Integral()*h1->GetBinWidth(1)*fn->EvalPar(x,nullptr); },-5,5,0);
new TCanvas();
// need to use DrawClone since RooFit object are deleted at the end of the macro
h1->DrawClone();
f->DrawClone("Same");
double chi2_N = h1->Chisquare(f); // Neyman chi2
double chi2_BC = h1->Chisquare(f,"L"); // Baker-Cousins chi2
std::cout << "Baker-Cousins chi2 = " << chi2_BC << " Neyman chi2 " << chi2_N << std::endl;
}