BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
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;
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  */
94  BaseFCN(f.DataPtr(), f.ModelFunctionPtr() ),
96  fWeight( f.fWeight ),
98  fGrad( f.fGrad),
100  { }
101 
102  /**
103  Assignment operator
104  */
106  SetData(rhs.DataPtr() );
107  SetModelFunction(rhs.ModelFunctionPtr() );
108  fNEffPoints = rhs.fNEffPoints;
109  fGrad = rhs.fGrad;
110  fIsExtended = rhs.fIsExtended;
111  fWeight = rhs.fWeight;
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,
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
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,
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
192 
193 
194  } // end namespace Fit
195 
196 } // end namespace ROOT
197 
198 
199 #endif /* ROOT_Fit_PoissonLikelihoodFCN */
virtual unsigned int NFitPoints() const
PoissonLikelihoodFCN(const BinData &data, const IModelFunction &func, int weight=0, bool extended=true, const ::ROOT::Fit::ExecutionPolicy &executionPolicy=::ROOT::Fit::ExecutionPolicy::kSerial)
::ROOT::Math::IParamMultiFunctionTempl< T > IModelFunction
virtual double DoDerivative(const double *x, unsigned int icoord) const
virtual double DataElement(const double *x, unsigned int i, double *g) const
i-th likelihood element and its gradient
::ROOT::Math::BasicFitMethodFunction< DerivFunType > BaseObjFunction
PoissonLikelihoodFCN(const PoissonLikelihoodFCN &f)
virtual BaseFunction * Clone() const
clone the function (need to return Base for Windows)
virtual double DoEval(const double *x) const
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)
BaseObjFunction::BaseFunction BaseFunction
virtual void Gradient(const double *x, double *g) const
evaluate gradient
::ROOT::Fit::ExecutionPolicy fExecutionPolicy
PoissonLikelihoodFCN & operator=(const PoissonLikelihoodFCN &rhs)
BasicFCN< DerivFunType, ModelFunType, BinData > BaseFCN
virtual BaseObjFunction::Type_t Type() const
get type of fit method function
Type_t
enumeration specyfing the possible fit method types
FunctionType::BaseFunc BaseFunction
PoissonLikelihoodFCN< ROOT::Math::IMultiGradFunction, ROOT::Math::IParamMultiFunction > PoissonLLGradFunction
PoissonLikelihoodFCN< ROOT::Math::IMultiGenFunction, ROOT::Math::IParamMultiFunction > PoissonLLFunction
Definition: TUUID.h:7
static double EvalPoissonBinPdf(const IModelFunctionTempl< double > &func, const BinData &data, const double *p, unsigned int i, double *g)
evaluate the pdf (Poisson) contribution to the logl (return actually log of pdf) and its gradient
Definition: FitUtil.h:1493
static double EvalPoissonLogL(const IModelFunctionTempl< double > &func, const BinData &data, const double *p, int iWeight, bool extended, unsigned int &nPoints, ROOT::Fit::ExecutionPolicy executionPolicy, unsigned nChunks=0)
Definition: FitUtil.h:1468
static void EvalPoissonLogLGradient(const IModelFunctionTempl< double > &func, const BinData &data, const double *p, double *g, unsigned int &nPoints, ROOT::Fit::ExecutionPolicy executionPolicy=ROOT::Fit::ExecutionPolicy::kSerial, unsigned nChunks=0)
Definition: FitUtil.h:1498