Logo ROOT  
Reference Guide
TUnuranMultiContDist.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 TUnuranMultiContDist
12 
13 #ifndef ROOT_Math_TUnuranMultiContDist
14 #define ROOT_Math_TUnuranMultiContDist
15 
16 #include "TUnuranBaseDist.h"
17 
18 #include "Math/IFunction.h"
19 
20 
21 
22 #include <vector>
23 
24 class TF1;
25 
26 
27 
28 /**
29  TUnuranMultiContDist class describing multi dimensional continuous distributions.
30  It is used by TUnuran to generate a set of random numbers according to this distribution via
31  TUnuran::Sample(double *).
32  The class can be constructed from a multi-dimensional function (TF1 pointer, which can be actually also a
33  TF2 or a TF3).
34  It provides a method to set the domain of the distribution ( SetDomain ) which will correspond to the range
35  of the generated random numbers. By default the domain is [(-inf,-inf,...)(+inf,+inf,...)], indipendently of the
36  range set in the TF1 class used to construct the distribution.
37 
38  The derivatives of the pdf which are used by some UNURAN methods are estimated numerically in the
39  Derivative() method. Some extra information (like distribution mode) can be set using SetMode.
40  Some methods require instead of the pdf the log of the pdf.
41  This can also be controlled by setting a flag when constructing this class.
42 
43  \ingroup Unuran
44 
45 */
46 
48 
49 public:
50 
51 
52  /**
53  Constructor from a TF1 object representing the Probability density function.
54  The derivatives of the Pdf are estimated, when required by the UNURAN algorithm,
55  using numerical derivation.
56  If a value of dim 0 is passed , the dimension of the function is taken from TF1::GetNdim().
57  This works only for 2D and 3D (for TF2 and TF3 objects).
58  */
59  TUnuranMultiContDist (TF1 * func = 0, unsigned int dim = 0, bool isLogPdf = false);
60 
61 
62  /**
63  Constructor as before but from a generic function object interface for multi-dim functions
64  */
65  TUnuranMultiContDist (const ROOT::Math::IMultiGenFunction & pdf, bool isLogPdf = false);
66 
67  /**
68  Destructor
69  */
70  virtual ~TUnuranMultiContDist ();
71 
72 
73  /**
74  Copy constructor
75  */
77 
78  /**
79  Assignment operator
80  */
82 
83  /**
84  Clone (required by base class)
85  */
86  virtual TUnuranMultiContDist * Clone() const { return new TUnuranMultiContDist(*this); }
87 
88 
89  /**
90  get number of dimension of the distribution
91  */
92  unsigned int NDim() const {
93  return fPdf->NDim();
94  }
95 
96  /**
97  set the domain of the distribution giving an array of minimum and maximum values
98  By default otherwise the domain is undefined, i.e. is [-inf,+inf]
99  To remove the domain do a SetDomain(0,0).
100  There is no possibility to have a domain defined in only one coordinate. Use instead inf or DOUBLE_MAX to
101  specify un infinite domain in that coordinate
102  */
103  void SetDomain(const double *xmin, const double *xmax) {
104  if (xmin == 0 || xmax == 0) return;
105  fXmin = std::vector<double>(xmin,xmin+NDim());
106  fXmax = std::vector<double>(xmax,xmax+NDim());
107  }
108 
109  /**
110  set the mode of the distribution (coordinates of the distribution maximum values)
111  */
112  void SetMode(const double * x) {
113  fMode = std::vector<double>(x,x+NDim());
114  }
115 
116  /**
117  get the distribution lower domain values. Return a null pointer if domain is not defined
118  */
119  const double * GetLowerDomain() const {
120  if (fXmin.size() == 0 || ( fXmin.size() != fXmax.size() ) ) return 0;
121  return &fXmin[0];
122  }
123  /**
124  get the distribution upper domain values. Return a null pointer if domain is not defined
125  */
126  const double * GetUpperDomain() const {
127  if (fXmax.size() == 0 || ( fXmin.size() != fXmax.size() ) ) return 0;
128  return &fXmax[0];
129  }
130 
131 
132  /**
133  get the mode (vector of coordinate positions of the maxima of the distribution)
134  If a mode has not defined return a NULL pointer
135  */
136  const double * GetMode( ) const {
137  if (fMode.size() == 0 ) return 0;
138  return &fMode.front();
139  }
140 
141 
142  /**
143  flag to control if given function represent the log of a pdf
144  */
145  bool IsLogPdf() const { return fIsLogPdf; }
146 
147  /**
148  evaluate the probability density function, used by UnuRan
149  */
150  double Pdf ( const double * x) const;
151 
152  /**
153  evaluate the gradient vector of the Pdf. Used by UnuRan
154  */
155  void Gradient( const double * x, double * grad) const;
156 
157  /**
158  evaluate the partial derivative for the given coordinate. Used by UnuRan
159  */
160  double Derivative( const double * x, int icoord) const;
161 
162 
163 
164 private:
165 
166  const ROOT::Math::IMultiGenFunction * fPdf; //pointer to the pdf
167 
168  std::vector<double> fXmin; //vector with lower x values of the domain
169  std::vector<double> fXmax; //vector with upper x values of the domain
170  std::vector<double> fMode; //vector representing the x coordinates of the maximum of the pdf
171 
172  bool fIsLogPdf; //flag to control if function pointer represent log of pdf
173  bool fOwnFunc; // flag to indicate if class manages the function pointers
174 
175 
176  ClassDef(TUnuranMultiContDist,1) //Wrapper class for multi dimensional continuous distribution
177 
178 
179 };
180 
181 
182 
183 #endif /* ROOT_Math_TUnuranMultiContDist */
TUnuranMultiContDist::GetUpperDomain
const double * GetUpperDomain() const
get the distribution upper domain values.
Definition: TUnuranMultiContDist.h:126
IFunction.h
TUnuranMultiContDist::GetLowerDomain
const double * GetLowerDomain() const
get the distribution lower domain values.
Definition: TUnuranMultiContDist.h:119
TUnuranMultiContDist::fOwnFunc
bool fOwnFunc
Definition: TUnuranMultiContDist.h:173
xmax
float xmax
Definition: THbookFile.cxx:95
TUnuranMultiContDist::Gradient
void Gradient(const double *x, double *grad) const
evaluate the gradient vector of the Pdf.
Definition: TUnuranMultiContDist.cxx:82
TUnuranMultiContDist::NDim
unsigned int NDim() const
get number of dimension of the distribution
Definition: TUnuranMultiContDist.h:92
ROOT::Math::IBaseFunctionMultiDimTempl::NDim
virtual unsigned int NDim() const =0
Retrieve the dimension of the function.
x
Double_t x[n]
Definition: legend1.C:17
TUnuranMultiContDist::fPdf
const ROOT::Math::IMultiGenFunction * fPdf
Definition: TUnuranMultiContDist.h:166
TUnuranBaseDist.h
TUnuranMultiContDist::operator=
TUnuranMultiContDist & operator=(const TUnuranMultiContDist &rhs)
Assignment operator.
Definition: TUnuranMultiContDist.cxx:51
TUnuranMultiContDist::Pdf
double Pdf(const double *x) const
evaluate the probability density function, used by UnuRan
Definition: TUnuranMultiContDist.cxx:75
TUnuranMultiContDist::Clone
virtual TUnuranMultiContDist * Clone() const
Clone (required by base class)
Definition: TUnuranMultiContDist.h:86
TUnuranMultiContDist::Derivative
double Derivative(const double *x, int icoord) const
evaluate the partial derivative for the given coordinate.
Definition: TUnuranMultiContDist.cxx:92
TUnuranMultiContDist::TUnuranMultiContDist
TUnuranMultiContDist(TF1 *func=0, unsigned int dim=0, bool isLogPdf=false)
Constructor from a TF1 object representing the Probability density function.
Definition: TUnuranMultiContDist.cxx:29
xmin
float xmin
Definition: THbookFile.cxx:95
TUnuranMultiContDist
TUnuranMultiContDist class describing multi dimensional continuous distributions.
Definition: TUnuranMultiContDist.h:47
TUnuranMultiContDist::fIsLogPdf
bool fIsLogPdf
Definition: TUnuranMultiContDist.h:172
TUnuranMultiContDist::SetMode
void SetMode(const double *x)
set the mode of the distribution (coordinates of the distribution maximum values)
Definition: TUnuranMultiContDist.h:112
TUnuranMultiContDist::GetMode
const double * GetMode() const
get the mode (vector of coordinate positions of the maxima of the distribution) If a mode has not def...
Definition: TUnuranMultiContDist.h:136
TUnuranMultiContDist::SetDomain
void SetDomain(const double *xmin, const double *xmax)
set the domain of the distribution giving an array of minimum and maximum values By default otherwise...
Definition: TUnuranMultiContDist.h:103
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TUnuranMultiContDist::fXmin
std::vector< double > fXmin
Definition: TUnuranMultiContDist.h:168
TUnuranMultiContDist::fMode
std::vector< double > fMode
Definition: TUnuranMultiContDist.h:170
TUnuranBaseDist
TUnuranBaseDist, base class for Unuran distribution classees such as TUnuranContDist (for one-dimensi...
Definition: TUnuranBaseDist.h:29
ROOT::Math::IBaseFunctionMultiDimTempl
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:62
TF1
1-Dim function class
Definition: TF1.h:213
TUnuranMultiContDist::~TUnuranMultiContDist
virtual ~TUnuranMultiContDist()
Destructor.
Definition: TUnuranMultiContDist.cxx:69
TUnuranMultiContDist::fXmax
std::vector< double > fXmax
Definition: TUnuranMultiContDist.h:169
TUnuranMultiContDist::IsLogPdf
bool IsLogPdf() const
flag to control if given function represent the log of a pdf
Definition: TUnuranMultiContDist.h:145