CMS 3D CMS Logo

Likelihood.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_Utilities_Likelihood_h
2 #define PhysicsTools_Utilities_Likelihood_h
5 #include <cmath>
6 #include "TMath.h"
7 
8 namespace fit {
9  template <typename PDF, typename Tuple>
11  static double evaluate(const PDF& pdf, const Tuple& tuple) { return pdf(tuple); }
12  };
13 
14  // to be generalized for tuples in N variables
15  template <typename PDF>
16  struct LikelihoodEvaluator<PDF, double> {
17  static double evaluate(const PDF& pdf, const double val) { return pdf(val); }
18  };
19 
21 
22  template <typename Sample, typename PDF, typename Yield = NoExtendedLikelihood>
23  class Likelihood {
24  public:
26  Likelihood(const Sample& sample, PDF& pdf, Yield& yield) : pdf_(&pdf), yield_(&yield), sample_(sample) {}
27  double operator()() const { return log(); }
28  double log() const {
29  double l = -(*yield_)();
30  for (typename Sample::const_iterator i = sample_.begin(); i != sample_.end(); ++i) {
31  double p = Evaluator::evaluate(*pdf_, *i);
32  l += std::log(p);
33  }
34  sampleSize_ = sample_.size();
35  return l;
36  }
37  double logNFactorial() const { return std::log(TMath::Factorial(sampleSize_)); }
38  double absoluteLog() const { return log() - logNFactorial(); }
39  PDF& pdf() { return *pdf_; }
40  const PDF& pdf() const { return *pdf_; }
41  Yield& yield() { return *yield_; }
42  const Yield& yield() const { return *yield_; }
43  unsigned int sampleSize() const { return sampleSize_; }
44 
45  private:
48  Yield* yield_;
49  Sample sample_;
50  mutable unsigned int sampleSize_ = 0u;
51  };
52 
53  template <typename Sample, typename PDF>
55  public:
57  Likelihood(const Sample& sample, PDF& pdf) : pdf_(&pdf), sample_(sample) {}
58  double operator()() const { return log(); }
59  double log() const {
60  double l = 0;
61  for (typename Sample::const_iterator i = sample_.begin(); i != sample_.end(); ++i) {
63  }
64  return l;
65  }
66  PDF& pdf() { return *pdf_; }
67  const PDF& pdf() const { return *pdf_; }
68 
69  private:
72  Sample sample_;
73  };
74 
75  template <typename Sample, typename PDF, typename Yield>
76  struct RootMinuitResultPrinter<Likelihood<Sample, PDF, Yield> > {
77  static void print(double amin, unsigned int numberOfFreeParameters, const Likelihood<Sample, PDF, Yield>& f) {
78  std::cout << "-2 log(maximum-likelihood) = " << amin << ", free parameters = " << numberOfFreeParameters
79  << std::endl;
80  }
81  };
82 
83  template <typename Sample, typename PDF, typename Yield>
84  struct RootMinuitFuncEvaluator<Likelihood<Sample, PDF, Yield> > {
85  static double evaluate(const Likelihood<Sample, PDF, Yield>& f) { return -2 * f(); }
86  };
87 
88 } // namespace fit
89 
90 #endif
LikelihoodEvaluator< PDF, typename Sample::value_type > Evaluator
Definition: Likelihood.h:70
static double evaluate(const Likelihood< Sample, PDF, Yield > &f)
Definition: Likelihood.h:85
const Yield & yield() const
Definition: Likelihood.h:42
const PDF & pdf() const
Definition: Likelihood.h:40
Likelihood(const Sample &sample, PDF &pdf, Yield &yield)
Definition: Likelihood.h:26
Yield * yield_
Definition: Likelihood.h:48
double operator()() const
Definition: Likelihood.h:27
static double evaluate(const PDF &pdf, const Tuple &tuple)
Definition: Likelihood.h:11
unsigned int sampleSize_
Definition: Likelihood.h:50
static void print(double amin, unsigned int numberOfFreeParameters, const Likelihood< Sample, PDF, Yield > &f)
Definition: Likelihood.h:77
double log() const
Definition: Likelihood.h:28
LikelihoodEvaluator< PDF, typename Sample::value_type > Evaluator
Definition: Likelihood.h:46
static double evaluate(const PDF &pdf, const double val)
Definition: Likelihood.h:17
Yield & yield()
Definition: Likelihood.h:41
double f[11][100]
double absoluteLog() const
Definition: Likelihood.h:38
Sample sample_
Definition: Likelihood.h:49
double logNFactorial() const
Definition: Likelihood.h:37
unsigned int sampleSize() const
Definition: Likelihood.h:43
Likelihood(const Sample &sample, PDF &pdf)
Definition: Likelihood.h:57