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