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
mps_fire.i
i
Definition: mps_fire.py:428
fit::Likelihood::Likelihood
Likelihood()
Definition: Likelihood.h:25
fit::Likelihood::yield
const Yield & yield() const
Definition: Likelihood.h:42
fit::Likelihood< Sample, PDF, NoExtendedLikelihood >::pdf
PDF & pdf()
Definition: Likelihood.h:66
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
simplePhotonAnalyzer_cfi.sample
sample
Definition: simplePhotonAnalyzer_cfi.py:12
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
gather_cfg.cout
cout
Definition: gather_cfg.py:144
fit::Likelihood::operator()
double operator()() const
Definition: Likelihood.h:27
ExhumeParameters_cfi.PDF
PDF
Definition: ExhumeParameters_cfi.py:8
fit::LikelihoodEvaluator
Definition: Likelihood.h:10
fit::Likelihood< Sample, PDF, NoExtendedLikelihood >::operator()
double operator()() const
Definition: Likelihood.h:58
fit::Likelihood< Sample, PDF, NoExtendedLikelihood >::log
double log() const
Definition: Likelihood.h:59
fit::RootMinuitResultPrinter< Likelihood< Sample, PDF, Yield > >::print
static void print(double amin, unsigned int numberOfFreeParameters, const Likelihood< Sample, PDF, Yield > &f)
Definition: Likelihood.h:77
fit::Likelihood::yield_
Yield * yield_
Definition: Likelihood.h:48
fit::LikelihoodEvaluator::evaluate
static double evaluate(const PDF &pdf, const Tuple &tuple)
Definition: Likelihood.h:11
fit::Likelihood::pdf_
PDF * pdf_
Definition: Likelihood.h:47
fit::LikelihoodEvaluator< PDF, double >::evaluate
static double evaluate(const PDF &pdf, const double val)
Definition: Likelihood.h:17
fit::Likelihood::pdf
const PDF & pdf() const
Definition: Likelihood.h:40
fit::Likelihood::yield
Yield & yield()
Definition: Likelihood.h:41
fit::Likelihood
Definition: Likelihood.h:23
fit::Likelihood::Likelihood
Likelihood(const Sample &sample, PDF &pdf, Yield &yield)
Definition: Likelihood.h:26
fit::Likelihood::sampleSize_
unsigned int sampleSize_
Definition: Likelihood.h:50
fit::Likelihood::absoluteLog
double absoluteLog() const
Definition: Likelihood.h:38
fit::Likelihood< Sample, PDF, NoExtendedLikelihood >::pdf_
PDF * pdf_
Definition: Likelihood.h:71
RootMinuitFuncEvaluator.h
fit::Likelihood< Sample, PDF, NoExtendedLikelihood >::Likelihood
Likelihood(const Sample &sample, PDF &pdf)
Definition: Likelihood.h:57
fit::Likelihood< Sample, PDF, NoExtendedLikelihood >::Evaluator
LikelihoodEvaluator< PDF, typename Sample::value_type > Evaluator
Definition: Likelihood.h:70
fit::Likelihood::Evaluator
LikelihoodEvaluator< PDF, typename Sample::value_type > Evaluator
Definition: Likelihood.h:46
fit::Likelihood< Sample, PDF, NoExtendedLikelihood >::Likelihood
Likelihood()
Definition: Likelihood.h:56
fit::Likelihood::log
double log() const
Definition: Likelihood.h:28
fit::RootMinuitFuncEvaluator
Definition: RootMinuitFuncEvaluator.h:6
fit::Likelihood::sampleSize
unsigned int sampleSize() const
Definition: Likelihood.h:43
fit::NoExtendedLikelihood
Definition: Likelihood.h:20
fit::Likelihood::logNFactorial
double logNFactorial() const
Definition: Likelihood.h:37
fit::Likelihood< Sample, PDF, NoExtendedLikelihood >::pdf
const PDF & pdf() const
Definition: Likelihood.h:67
RootMinuitResultPrinter.h
cmsLHEtoEOSManager.l
l
Definition: cmsLHEtoEOSManager.py:204
heppy_batch.val
val
Definition: heppy_batch.py:351
fit::Likelihood::sample_
Sample sample_
Definition: Likelihood.h:49
fit::Likelihood::pdf
PDF & pdf()
Definition: Likelihood.h:39
dqm-mbProfile.log
log
Definition: dqm-mbProfile.py:17
fit::RootMinuitResultPrinter
Definition: RootMinuitResultPrinter.h:8
fit::RootMinuitFuncEvaluator< Likelihood< Sample, PDF, Yield > >::evaluate
static double evaluate(const Likelihood< Sample, PDF, Yield > &f)
Definition: Likelihood.h:85
fit
Definition: CombinedChiSquaredLikelihood.h:6
fit::Likelihood< Sample, PDF, NoExtendedLikelihood >::sample_
Sample sample_
Definition: Likelihood.h:72