ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TUnuranContDist.h
Go to the documentation of this file.
1 // @(#)root/unuran:$Id$
2 // Authors: L. Moneta, J. Leydold Wed Feb 28 2007
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Header file for class TUnuranContDist
12 
13 
14 #ifndef ROOT_Math_TUnuranContDist
15 #define ROOT_Math_TUnuranContDist
16 
17 #ifndef ROOT_Math_TUnuranBaseDist
18 #include "TUnuranBaseDist.h"
19 #endif
20 
21 #ifndef ROOT_Math_IFunctionfwd
22 #include "Math/IFunctionfwd.h"
23 #endif
24 
25 class TF1;
26 
27 
28 
29 
30 /**
31  \class TUnuranContDist
32  \ingroup Unuran
33 
34  TUnuranContDist class describing one dimensional continuous distribution.
35  It is used by TUnuran to generate random numbers according to this distribution via
36  TUnuran::Sample()
37 
38  The class can be constructed from a function (TF1) representing the probability density
39  function of the distribution. Optionally the derivative of the pdf can also be passed.
40 
41  It provides a method to set the domain of the distribution ( SetDomain ) which will correspond to the range
42  of the generated random numbers. By default the domain is (-inf, + inf), indipendently of the
43  range set in the TF1 class used to construct the distribution.
44 
45  In addition, some UNURAN methods requires extra information (cdf function, distribution mode,
46  area of pdf, etc...). This information can as well be set.
47  Some methods require instead of the pdf the log of the pdf.
48  This can also be controlled by setting a flag when constructing this class.
49 */
50 
51 
53 
54 public:
55 
56 
57  /**
58  Constructor from a TF1 objects specifying the pdf and optionally from another function
59  representing the derivative of the pdf. The flag isLogPdf can be used to pass instead of the pdf
60  (and its derivative) the log (and the derivative of the log) of the pdf.
61  By default the distribution has not domain set (it is defined between [-inf,+inf], no mode, no pdf area and no
62  cdf explicity defined. UnuRan, if needed, can compute some of this quantities, but the user if they know them can
63  set them in order to speed up the algorithm. For example in case of the Cdf, if the user has not set it, a numerical
64  integration algorithm is used to estimate the Cdf from the Pdf.
65  In case an algorithm requires only the Cdf (no Pdf), an empty distribution can be constructed and then the user must
66  set afterwards the Cdf.
67  */
68  explicit TUnuranContDist (TF1 * pdf = 0, TF1 * deriv = 0, bool isLogPdf = false );
69 
70  /**
71  Constructor as before but from a generic function object interface for one-dim functions
72  */
73  explicit TUnuranContDist (const ROOT::Math::IGenFunction & pdf, const ROOT::Math::IGenFunction * dpdf = 0, bool isLogPdf = false, bool copyFunc = false);
74 
75 
76  /**
77  Destructor
78  */
79  virtual ~TUnuranContDist ();
80 
81 
82  /**
83  Copy constructor
84  */
86 
87  /**
88  Assignment operator
89  */
91 
92  /**
93  Clone (required by base class)
94  */
95  virtual TUnuranContDist * Clone() const { return new TUnuranContDist(*this); }
96 
97 
98  /**
99  set cdf distribution. If a method requires it
100  and is not set it is then estimated using numerical
101  integration from the pdf
102  */
103  void SetCdf(TF1 * cdf);
104 
105  /**
106  set cdf distribution using a generic function interface
107  */
108  void SetCdf(const ROOT::Math::IGenFunction & cdf);
109 
110  /**
111  Set the distribution domain. If min < max a domain is defined otherwise is undefined
112  */
113  void SetDomain(double xmin, double xmax) {
114  fXmin = xmin;
115  fXmax = xmax;
116  if (fXmin < fXmax)
117  fHasDomain = true;
118  else
119  fHasDomain = false;
120  }
121 
122  /**
123  set the distribution mode (x position of its maximum)
124  */
125  void SetMode(double mode) { fMode = mode; fHasMode=true;}
126 
127  /**
128  set the area below the pdf
129  */
130  void SetPdfArea(double area) { fArea = area; fHasArea=true;}
131 
132  /**
133  check if distribution has a defined domain and return in case its domain
134  */
135  bool GetDomain(double & xmin, double & xmax) const {
136  xmin = fXmin;
137  xmax = fXmax;
138  return fHasDomain;
139  }
140 
141  /**
142  check if a cdf function is provided for the distribution
143  */
144  bool HasCdf() const { return fCdf != 0; }
145 
146  /**
147  check if distribution has a pre-computed mode
148  */
149  bool HasMode() const { return fHasMode; }
150 
151 
152  /**
153  check if distribution has a pre-computed area below the Pdf
154  */
155  bool HasPdfArea() const { return fHasArea; }
156 
157  /**
158  return the mode (x location of maximum of the pdf)
159  */
160  double Mode() const { return fMode; }
161 
162  /**
163  return area below the pdf
164  */
165  double PdfArea() const { return fArea; }
166 
167 
168  /**
169  flag to control if given function represent the log of a pdf
170  */
171  bool IsLogPdf() const { return fIsLogPdf; }
172 
173  /**
174  evaluate the Probability Density function. Used by the UnuRan algorithms
175  */
176  double Pdf ( double x) const;
177 
178  /**
179  evaluate the derivative of the pdf. Used by UnuRan
180  */
181  double DPdf( double x) const;
182 
183  /**
184  evaluate the integral (cdf) on the domain. Used by Unuran algorithm
185  */
186  double Cdf(double x) const;
187 
188 
189 protected:
190 
191 
192 private:
193 
194 
195  const ROOT::Math::IGenFunction * fPdf; // pointer to the pdf
196  const ROOT::Math::IGenFunction * fDPdf; //pointer to the derivative of the pdf
197  const ROOT::Math::IGenFunction * fCdf; //pointer to the cdf (cumulative dist.)
198 
199  double fXmin; //lower value of the domain
200  double fXmax; //upper value of the domain
201  double fMode; //mode of the distribution
202  double fArea; //area below pdf
203 
204  // flags
205  bool fIsLogPdf; //flag to control if function pointer represent log of pdf
206  bool fHasDomain; //flag to control if distribution has a defined domain (otherwise is [-inf,+inf]
207  bool fHasMode; //flag to control if distribution has a pre-computed mode
208  bool fHasArea; //flag to control if distribution has a pre-computed area below the pdf
209  bool fOwnFunc; // flag to indicate if class manages the function pointers
210  //mutable double fX[1]; //! cached vector for using TF1::EvalPar
211 
212  ClassDef(TUnuranContDist,1) //Wrapper class for one dimensional continuous distribution
213 
214 
215 };
216 
217 
218 
219 #endif /* ROOT_Math_TUnuranContDist */
TUnuranBaseDist, base class for Unuran distribution classees such as TUnuranContDist (for one-dimensi...
const ROOT::Math::IGenFunction * fDPdf
float xmin
Definition: THbookFile.cxx:93
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition: IFunction.h:133
virtual ~TUnuranContDist()
Destructor.
bool HasMode() const
check if distribution has a pre-computed mode
void SetCdf(TF1 *cdf)
set cdf distribution.
virtual TUnuranContDist * Clone() const
Clone (required by base class)
Double_t x[n]
Definition: legend1.C:17
TUnuranContDist(TF1 *pdf=0, TF1 *deriv=0, bool isLogPdf=false)
Constructor from a TF1 objects specifying the pdf and optionally from another function representing t...
#define ClassDef(name, id)
Definition: Rtypes.h:254
double cdf(double *x, double *p)
Definition: unuranDistr.cxx:44
double DPdf(double x) const
evaluate the derivative of the pdf.
double Pdf(double x) const
evaluate the Probability Density function.
const ROOT::Math::IGenFunction * fCdf
const ROOT::Math::IGenFunction * fPdf
double Cdf(double x) const
evaluate the integral (cdf) on the domain.
float xmax
Definition: THbookFile.cxx:93
bool HasCdf() const
check if a cdf function is provided for the distribution
bool HasPdfArea() const
check if distribution has a pre-computed area below the Pdf
TUnuranContDist & operator=(const TUnuranContDist &rhs)
Assignment operator.
void SetDomain(double xmin, double xmax)
Set the distribution domain.
bool IsLogPdf() const
flag to control if given function represent the log of a pdf
bool GetDomain(double &xmin, double &xmax) const
check if distribution has a defined domain and return in case its domain
void SetPdfArea(double area)
set the area below the pdf
TUnuranContDist class describing one dimensional continuous distribution.
double Mode() const
return the mode (x location of maximum of the pdf)
1-Dim function class
Definition: TF1.h:149
double PdfArea() const
return area below the pdf
void SetMode(double mode)
set the distribution mode (x position of its maximum)