Unbinned Maximum-Likelihood Fit


const int N = 40;
double arr[N];

void AsyRnd( bool outp = true )
{
  // get random number according to asymmetry function
  TF1 *f1 = new TF1("f1","3/8*(1+0.5*x+x*x)", -1, 1 );
  
  for  ( int i =0; i<N; i++ ) {
    //    arr[i] = f1->GetRandom();
    arr[i] = gRandom->Rndm()-0.1;
    if ( outp ) cout << " i = " << i << " x = " << arr[i] << endl;
  }
}
    
double fEval( double x, double p[]) 
{  // calculate asy pdf for given x and asy par
  double val = 3./8*(1.+p[0]*x+x*x);
  return val;
}

void fcn(Int_t &npar,double *gin, double &f, double *par, int iflag)
{  // calculate log likelihood for generated values arr[i]  and asy par
  double logl = 0.;
  for ( int i =0; i<N; i++ ) {
    double fval = fEval( arr[i], par ) ;
    if ( fval < 1e-10 ) fval = 1e-10; // protect against 0/neg
    logl += log( fval );
  }
  // return negative log-lik divided by 2 for error def
  f = -logl/2.;
}

void fAsyMn( ) 
{
  // create asy numbers
  AsyRnd();
  //   initialize TMinuit 
  const int npar = 1;
  gMinuit = new TMinuit(npar);  
  // Tell Minuit the function 
  gMinuit->SetFCN(fcn);
  // Start Werte fuer Minuit
  int ierflg;
  gMinuit->mnparm(0, "Asy", 0.1, 0.01, 0., 0., ierflg);
  // minimization
  double arglist[3] = { 1000., 1., 0. };
  gMinuit->mnexcm("MIGRAD", arglist ,1,ierflg);
  
  double par, epar;
  gMinuit->GetParameter( 0, par, epar ) ;
  cout << "Asy   " << par << " +- " << epar  << endl;
}




GDuckeck 2018-04-10