BCPrior.h
1 #ifndef __BCPRIOR__H
2 #define __BCPRIOR__H
3 
23 /*
24  * Copyright (C) 2007-2018, the BAT core developer team
25  * All rights reserved.
26  *
27  * For the licensing terms see doc/COPYING.
28  * For documentation see http://mpp.mpg.de/bat
29  */
30 
31 // ---------------------------------------------------------
32 
33 
34 #include "BCH1D.h"
35 #include "BCH2D.h"
36 
37 #include <TF1.h>
38 
39 #include <cstddef>
40 #include <cmath>
41 #include <limits>
42 
43 class TH1;
44 class TH2;
45 class TRandom;
46 
47 // ---------------------------------------------------------
48 
49 class BCPrior
50 {
51 
52 public:
53 
59  BCPrior();
60 
63  BCPrior(const BCPrior& other);
64 
67  virtual ~BCPrior();
68 
75  friend void swap(BCPrior& A, BCPrior& B);
76 
86  virtual double GetLogPrior(double x) = 0;
87 
90  virtual BCPrior* Clone() const = 0;
91  // { return new [Derived Class](*this); }
92 
95  virtual bool IsValid() const = 0;
96 
106  virtual double GetLogNormalizedPrior(double x)
107  { return GetLogPrior(x) - fLogIntegral;}
108 
114  virtual double GetPrior(double x, bool normalize = false);
115 
118  virtual TF1& GetFunction()
119  { return fPriorFunction; }
120 
123  virtual const TF1& GetFunction() const
124  { return fPriorFunction; }
125 
128  virtual void SetFunctionRange(double xmin, double xmax);
129 
135  virtual double GetMode(double xmin = -std::numeric_limits<double>::infinity(), double xmax = std::numeric_limits<double>::infinity());
136 
143  virtual double GetRawMoment(unsigned n, double xmin = -std::numeric_limits<double>::infinity(), double xmax = std::numeric_limits<double>::infinity());
144 
150  virtual double GetIntegral(double xmin = -std::numeric_limits<double>::infinity(), double xmax = std::numeric_limits<double>::infinity());
151 
158  virtual double GetCentralMoment(unsigned n, double xmin = -std::numeric_limits<double>::infinity(), double xmax = std::numeric_limits<double>::infinity());
159 
166  virtual double GetStandardizedMoment(unsigned n, double xmin = -std::numeric_limits<double>::infinity(), double xmax = std::numeric_limits<double>::infinity());
167 
173  virtual double GetMean(double xmin = -std::numeric_limits<double>::infinity(), double xmax = std::numeric_limits<double>::infinity())
174  { return GetRawMoment(1, xmin, xmax); }
175 
181  virtual double GetVariance(double xmin = -std::numeric_limits<double>::infinity(), double xmax = std::numeric_limits<double>::infinity())
182  { return GetCentralMoment(2, xmin, xmax); }
183 
189  virtual double GetStandardDeviation(double xmin = -std::numeric_limits<double>::infinity(), double xmax = std::numeric_limits<double>::infinity())
190  { return sqrt(fabs(GetVariance(xmin, xmax))); }
191 
197  virtual double GetSkewness(double xmin = -std::numeric_limits<double>::infinity(), double xmax = std::numeric_limits<double>::infinity())
198  { return GetStandardizedMoment(3, xmin, xmax); }
199 
205  virtual double GetKurtosis(double xmin = -std::numeric_limits<double>::infinity(), double xmax = std::numeric_limits<double>::infinity())
206  { return GetStandardizedMoment(4, xmin, xmax); }
207 
214  virtual double GetRandomValue(double xmin, double xmax, TRandom* const R = NULL);
215 
222  virtual double GetPriorForROOT(double* x, double* /*p*/)
223  { return GetPrior(x[0]); }
224 
227  virtual double GetNormalizedPriorForROOT(double* x, double* /*p*/)
228  { return GetPrior(x[0]); }
229 
232  virtual double GetLogPriorForROOT(double* x, double* /*p*/)
233  { return GetLogPrior(x[0]); }
234 
237  virtual double GetNormalizedLogPriorForROOT(double* x, double* /*p*/)
238  { return GetLogPrior(x[0]) - fLogIntegral; }
239 
242  virtual double CalculateAndStoreIntegral(double xmin = -std::numeric_limits<double>::infinity(), double xmax = std::numeric_limits<double>::infinity())
243  { fLogIntegral = log(GetIntegral(xmin, xmax)); return fLogIntegral;}
244 
247  virtual void StoreIntegral(double I)
248  { fLogIntegral = log(I); }
249 
252  virtual void StoreLogIntegral(double logI)
253  { fLogIntegral = logI; }
254 
257  virtual double GetStoredIntegral() const
258  { return exp(fLogIntegral); }
259 
262  virtual double GetStoredLogIntegral() const
263  { return fLogIntegral; }
264 
267  virtual void FillHistogramByCenterValue(TH1* h);
268 
271  virtual void FillHistogramByIntegral(TH1* h);
272 
278  virtual BCH1D GetBCH1D(TH1* bins, const std::string& name = "prior");
279 
286  virtual BCH2D GetBCH2D(BCPrior* ordinate, TH2* bins, const std::string& name = "prior");
287 
290 protected:
292 
293  double fLogIntegral;
294 };
295 
296 #endif
A class to represent the prior of a parameter.
Definition: BCPrior.h:49
virtual double GetPriorForROOT(double *x, double *)
For accessing prior as ROOT TF1.
Definition: BCPrior.h:222
virtual double GetKurtosis(double xmin=-std::numeric_limits< double >::infinity(), double xmax=std::numeric_limits< double >::infinity())
Get kurtosis of prior.
Definition: BCPrior.h:205
virtual void FillHistogramByCenterValue(TH1 *h)
Fill histogram by prior evaluated at bin center.
Definition: BCPrior.cxx:140
virtual BCPrior * Clone() const =0
Clone function.
virtual double GetLogPriorForROOT(double *x, double *)
For accessing log(prior) as ROOT TF1.
Definition: BCPrior.h:232
virtual double GetLogNormalizedPrior(double x)
Get log of normalized prior.
Definition: BCPrior.h:106
virtual void StoreIntegral(double I)
Store integral;.
Definition: BCPrior.h:247
virtual void SetFunctionRange(double xmin, double xmax)
Set range of ROOT TF1 function.
Definition: BCPrior.cxx:133
virtual double GetNormalizedPriorForROOT(double *x, double *)
For accessing normalized prior as ROOT TF1.
Definition: BCPrior.h:227
virtual TF1 & GetFunction()
Return back ROOT TF1 evaluating BCPrior::GetPrior.
Definition: BCPrior.h:118
virtual double GetStoredLogIntegral() const
Get stored integral.
Definition: BCPrior.h:262
A class for handling 2D distributions.
Definition: BCH2D.h:37
TF1 fPriorFunction
Definition: BCPrior.h:291
BCPrior()
Empty constructor.
Definition: BCPrior.cxx:23
friend void swap(BCPrior &A, BCPrior &B)
swap
Definition: BCPrior.cxx:42
virtual double GetMode(double xmin=-std::numeric_limits< double >::infinity(), double xmax=std::numeric_limits< double >::infinity())
Return mode of prior (in range).
Definition: BCPrior.cxx:65
virtual double GetStandardDeviation(double xmin=-std::numeric_limits< double >::infinity(), double xmax=std::numeric_limits< double >::infinity())
Get standard deviation of prior.
Definition: BCPrior.h:189
virtual double GetPrior(double x, bool normalize=false)
Get prior.
Definition: BCPrior.cxx:51
virtual double GetStoredIntegral() const
Get stored integral.
Definition: BCPrior.h:257
virtual double CalculateAndStoreIntegral(double xmin=-std::numeric_limits< double >::infinity(), double xmax=std::numeric_limits< double >::infinity())
Calculate and store integral for use in normalized TF1s.
Definition: BCPrior.h:242
virtual const TF1 & GetFunction() const
Return back ROOT TF1 evaluating BCPrior::GetPrior.
Definition: BCPrior.h:123
A class for handling 1D distributions.
Definition: BCH1D.h:34
virtual double GetNormalizedLogPriorForROOT(double *x, double *)
For accessing normalized log(prior) as ROOT TF1.
Definition: BCPrior.h:237
virtual void StoreLogIntegral(double logI)
Store log(integral);.
Definition: BCPrior.h:252
virtual BCH2D GetBCH2D(BCPrior *ordinate, TH2 *bins, const std::string &name="prior")
Get BCH2D object for prior.
Definition: BCPrior.cxx:176
virtual double GetSkewness(double xmin=-std::numeric_limits< double >::infinity(), double xmax=std::numeric_limits< double >::infinity())
Get skewness of prior.
Definition: BCPrior.h:197
virtual double GetLogPrior(double x)=0
Get log of prior.
virtual double GetIntegral(double xmin=-std::numeric_limits< double >::infinity(), double xmax=std::numeric_limits< double >::infinity())
Get integral of prior.
Definition: BCPrior.cxx:81
virtual BCH1D GetBCH1D(TH1 *bins, const std::string &name="prior")
Get BCH1D object for prior.
Definition: BCPrior.cxx:159
virtual double GetRandomValue(double xmin, double xmax, TRandom *const R=NULL)
Definition: BCPrior.cxx:126
double fLogIntegral
Log of integral of unnormalized pdf over the range.
Definition: BCPrior.h:293
virtual double GetMean(double xmin=-std::numeric_limits< double >::infinity(), double xmax=std::numeric_limits< double >::infinity())
Get mean of prior.
Definition: BCPrior.h:173
virtual double GetCentralMoment(unsigned n, double xmin=-std::numeric_limits< double >::infinity(), double xmax=std::numeric_limits< double >::infinity())
Get central moment of prior distrubion.
Definition: BCPrior.cxx:88
virtual void FillHistogramByIntegral(TH1 *h)
Fill histogram by integrating prior over bin and dividing by bin width.
Definition: BCPrior.cxx:149
virtual double GetStandardizedMoment(unsigned n, double xmin=-std::numeric_limits< double >::infinity(), double xmax=std::numeric_limits< double >::infinity())
Get standardised moment of prior distrubion.
Definition: BCPrior.cxx:112
virtual double GetVariance(double xmin=-std::numeric_limits< double >::infinity(), double xmax=std::numeric_limits< double >::infinity())
Get variance of prior.
Definition: BCPrior.h:181
virtual bool IsValid() const =0
virtual double GetRawMoment(unsigned n, double xmin=-std::numeric_limits< double >::infinity(), double xmax=std::numeric_limits< double >::infinity())
Get raw moment of prior distrubion.
Definition: BCPrior.cxx:72
virtual ~BCPrior()
Destructor.
Definition: BCPrior.cxx:37