Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
PoissonLikelihoodFCN.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Author: L. Moneta Fri Aug 17 14:29:24 2007
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2007 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Header file for class PoissonLikelihoodFCN
12 
13 #ifndef ROOT_Fit_PoissonLikelihoodFCN
14 #define ROOT_Fit_PoissonLikelihoodFCN
15 
16 #include "Fit/BasicFCN.h"
17 
18 #include "Math/IParamFunction.h"
19 
20 #include "Fit/BinData.h"
21 
22 #include "Fit/FitUtil.h"
23 
24 
25 #include <memory>
26 
27 //#define PARALLEL
28 // #ifdef PARALLEL
29 // #ifndef ROOT_Fit_FitUtilParallel
30 // #include "Fit/FitUtilParallel.h"
31 // #endif
32 // #endif
33 
34 namespace ROOT {
35 
36  namespace Fit {
37 
38 
39 //___________________________________________________________________________________
40 /**
41  class evaluating the log likelihood
42  for binned Poisson likelihood fits
43  it is template to distinguish gradient and non-gradient case
44 
45  @ingroup FitMethodFunc
46 */
47 template<class DerivFunType, class ModelFunType = ROOT::Math::IParamMultiFunction>
48 class PoissonLikelihoodFCN : public BasicFCN<DerivFunType,ModelFunType,BinData> {
49 
50 public:
51  typedef typename ModelFunType::BackendType T;
52  typedef BasicFCN<DerivFunType,ModelFunType,BinData> BaseFCN;
53 
54  typedef ::ROOT::Math::BasicFitMethodFunction<DerivFunType> BaseObjFunction;
55  typedef typename BaseObjFunction::BaseFunction BaseFunction;
56 
57  typedef ::ROOT::Math::IParamMultiFunctionTempl<T> IModelFunction;
58  typedef typename BaseObjFunction::Type_t Type_t;
59 
60  /**
61  Constructor from unbin data set and model function (pdf)
62  */
63  PoissonLikelihoodFCN (const std::shared_ptr<BinData> & data, const std::shared_ptr<IModelFunction> & func, int weight = 0, bool extended = true, const ::ROOT::Fit::ExecutionPolicy &executionPolicy = ::ROOT::Fit::ExecutionPolicy::kSerial ) :
64  BaseFCN( data, func),
65  fIsExtended(extended),
66  fWeight(weight),
67  fNEffPoints(0),
68  fGrad ( std::vector<double> ( func->NPar() ) ),
69  fExecutionPolicy(executionPolicy)
70  { }
71 
72  /**
73  Constructor from unbin data set and model function (pdf) managed by the users
74  */
75  PoissonLikelihoodFCN (const BinData & data, const IModelFunction & func, int weight = 0, bool extended = true, const ::ROOT::Fit::ExecutionPolicy &executionPolicy = ::ROOT::Fit::ExecutionPolicy::kSerial ) :
76  BaseFCN(std::shared_ptr<BinData>(const_cast<BinData*>(&data), DummyDeleter<BinData>()), std::shared_ptr<IModelFunction>(dynamic_cast<IModelFunction*>(func.Clone() ) ) ),
77  fIsExtended(extended),
78  fWeight(weight),
79  fNEffPoints(0),
80  fGrad ( std::vector<double> ( func.NPar() ) ),
81  fExecutionPolicy(executionPolicy)
82  { }
83 
84 
85  /**
86  Destructor (no operations)
87  */
88  virtual ~PoissonLikelihoodFCN () {}
89 
90  /**
91  Copy constructor
92  */
93  PoissonLikelihoodFCN(const PoissonLikelihoodFCN & f) :
94  BaseFCN(f.DataPtr(), f.ModelFunctionPtr() ),
95  fIsExtended(f.fIsExtended ),
96  fWeight( f.fWeight ),
97  fNEffPoints( f.fNEffPoints ),
98  fGrad( f.fGrad),
99  fExecutionPolicy(f.fExecutionPolicy)
100  { }
101 
102  /**
103  Assignment operator
104  */
105  PoissonLikelihoodFCN & operator = (const PoissonLikelihoodFCN & rhs) {
106  SetData(rhs.DataPtr() );
107  SetModelFunction(rhs.ModelFunctionPtr() );
108  fNEffPoints = rhs.fNEffPoints;
109  fGrad = rhs.fGrad;
110  fIsExtended = rhs.fIsExtended;
111  fWeight = rhs.fWeight;
112  fExecutionPolicy = rhs.fExecutionPolicy;
113  }
114 
115 
116  /// clone the function (need to return Base for Windows)
117  virtual BaseFunction * Clone() const { return new PoissonLikelihoodFCN(*this); }
118 
119  // effective points used in the fit
120  virtual unsigned int NFitPoints() const { return fNEffPoints; }
121 
122  /// i-th likelihood element and its gradient
123  virtual double DataElement(const double * x, unsigned int i, double * g) const {
124  if (i==0) this->UpdateNCalls();
125  return FitUtil::Evaluate<typename BaseFCN::T>::EvalPoissonBinPdf(BaseFCN::ModelFunction(), BaseFCN::Data(), x, i, g);
126  }
127 
128  /// evaluate gradient
129  virtual void Gradient(const double *x, double *g) const
130  {
131  // evaluate the Poisson gradient
132  FitUtil::Evaluate<typename BaseFCN::T>::EvalPoissonLogLGradient(BaseFCN::ModelFunction(), BaseFCN::Data(), x, g,
133  fNEffPoints, fExecutionPolicy);
134  }
135 
136  /// get type of fit method function
137  virtual typename BaseObjFunction::Type_t Type() const { return BaseObjFunction::kLogLikelihood; }
138 
139  bool IsWeighted() const { return (fWeight != 0); }
140 
141  // Use the weights in evaluating the likelihood
142  void UseSumOfWeights() {
143  if (fWeight == 0) return; // do nothing if it was not weighted
144  fWeight = 1;
145  }
146 
147  // Use sum of the weight squared in evaluating the likelihood
148  // (this is needed for calculating the errors)
149  void UseSumOfWeightSquare(bool on = true) {
150  if (fWeight == 0) return; // do nothing if it was not weighted
151  if (on) fWeight = 2;
152  else fWeight = 1;
153  }
154 
155 
156 protected:
157 
158 
159 private:
160 
161  /**
162  Evaluation of the function (required by interface)
163  */
164  virtual double DoEval (const double * x) const {
165  this->UpdateNCalls();
166  return FitUtil::Evaluate<T>::EvalPoissonLogL(BaseFCN::ModelFunction(), BaseFCN::Data(), x, fWeight, fIsExtended,
167  fNEffPoints, fExecutionPolicy);
168  }
169 
170  // for derivatives
171  virtual double DoDerivative(const double * x, unsigned int icoord ) const {
172  Gradient(x, &fGrad[0]);
173  return fGrad[icoord];
174  }
175 
176 
177  //data member
178 
179  bool fIsExtended; // flag to indicate if is extended (when false is a Multinomial lieklihood), default is true
180  int fWeight; // flag to indicate if needs to evaluate using weight or weight squared (default weight = 0)
181 
182  mutable unsigned int fNEffPoints; // number of effective points used in the fit
183 
184  mutable std::vector<double> fGrad; // for derivatives
185 
186  ::ROOT::Fit::ExecutionPolicy fExecutionPolicy; // Execution policy
187 };
188 
189  // define useful typedef's
190  typedef PoissonLikelihoodFCN<ROOT::Math::IMultiGenFunction, ROOT::Math::IParamMultiFunction> PoissonLLFunction;
191  typedef PoissonLikelihoodFCN<ROOT::Math::IMultiGradFunction, ROOT::Math::IParamMultiFunction> PoissonLLGradFunction;
192 
193 
194  } // end namespace Fit
195 
196 } // end namespace ROOT
197 
198 
199 #endif /* ROOT_Fit_PoissonLikelihoodFCN */