ROOT  6.06/09
Reference Guide
TUnuranDiscrDist.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 TUnuranDiscrDist
12 
13 
14 #ifndef ROOT_Math_TUnuranDiscrDist
15 #define ROOT_Math_TUnuranDiscrDist
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 
26 #include <vector>
27 
28 
29 class TF1;
30 
31 
32 /**
33  TUnuranDiscrDist class for one dimensional discrete distribution.
34  It is used by TUnuran to generate integer random numbers according to this distribution via
35  TUnuran::SampleDiscr().
36 
37  The class can be constructed from a one-dimensional function (TF1 pointer)
38  representing the discrete distribution (probability mesh function)
39  (for example a TF1("f","TMath::PoissonI(x,[0])") ) or from a
40  vector of probability, used by passing an iterator specifying the begin and the end of the vector.
41  In the latter case the domain of the distribution will be defined by the vector, while in the first case is by
42  default (0,+inf).
43  a Method to set the domain of the distribution ( SetDomain ) is provided and it defines the range
44  of the generated random numbers.
45 
46  The derivatives of the pdf which are used by some UNURAN methods are estimated numerically in the
47  Derivative() method.
48  Some extra information (like distribution mode, cdf function, probability sum, etc..)
49  can be set as well otherwise will be estimated internally if required.
50 
51  \class TUnuranDiscrDist
52  \ingroup Unuran
53 */
54 
56 
57 public:
58 
59  /**
60  Constructor from a generic function object specifying the pdf
61  */
62  TUnuranDiscrDist (const ROOT::Math::IGenFunction & func, bool copyFunc = false );
63 
64  /**
65  Constructor from a TF1 objects specifying the pdf
66  */
67  TUnuranDiscrDist (TF1 * func );
68 
69  /**
70  Constructor from a vector of probability
71  */
72  template<class Iterator>
73  TUnuranDiscrDist (Iterator * begin, Iterator * end) :
74  fPVec(begin,end),
75  fPmf(0),
76  fCdf(0),
77  fXmin(1),
78  fXmax(-1),
79  fMode(0),
80  fSum(0),
81  fHasDomain(0),
82  fHasMode(0),
83  fHasSum(0),
85  {}
86 
87  /**
88  Destructor
89  */
90  virtual ~TUnuranDiscrDist ();
91 
92  /**
93  Copy constructor
94  */
96 
97  /**
98  Assignment operator
99  */
101 
102  /**
103  Clone (required by base class)
104  */
105  virtual TUnuranDiscrDist * Clone() const { return new TUnuranDiscrDist(*this); }
106 
107  /**
108  set cdf distribution from a generic function interface. If a method requires it
109  and is not set it is estimated numerically
110  */
111  void SetCdf(const ROOT::Math::IGenFunction & cdf);
112 
113  /**
114  set cdf distribution from a TF1 pointer. If a method requires it
115  and is not set it is estimated numerically
116  */
117  void SetCdf(TF1 * cdf);
118 
119 
120  /**
121  Set the distribution domain, by default the domain is [0,INT_MAX]
122  If xmin >= xmax a domain is removed
123  */
124  void SetDomain(int xmin, int xmax) {
125  fXmin = xmin;
126  fXmax = xmax;
127  if (fXmin < fXmax)
128  fHasDomain = true;
129  else
130  fHasDomain = false;
131  }
132 
133 
134  /**
135  set the mode of the distribution (location of maximum probability)
136  */
137  void SetMode(int mode) { fMode = mode; fHasMode=true;}
138 
139  /**
140  set the value of the sum of the probabilities in the given domain
141  */
142  void SetProbSum(double sum) { fSum = sum; fHasSum=true; }
143 
144  /**
145  check if distribution has domain and return in case its domain
146  */
147  bool GetDomain(int & xmin, int & xmax) const {
148  xmin = fXmin;
149  xmax = fXmax;
150  return fHasDomain;
151  }
152 
153  /**
154  get the mode (x location of function maximum)
155  */
156  int Mode() const { return fMode; }
157 
158  /**
159  return area of the pdf
160  */
161  double ProbSum() const { return fSum; }
162 
163 
164  /**
165  flag to control if distribution provides the mode
166  */
167  bool HasMode() const { return fHasMode; }
168 
169 
170  /**
171  flag to control if distribution provides the total area of the probability function
172  */
173  bool HasProbSum() const { return fHasSum; }
174 
175  /**
176  flag to control if distribution provides also a Cdf
177  */
178  bool HasCdf() const { return fCdf != 0; }
179 
180 
181  /**
182  retrieve a reference to the vector of the probabilities : Prob(i)
183  If the distribution is defined from a function (i.e. for distribution with undefined domain)
184  the vector is empty.
185  */
186  const std::vector<double> & ProbVec() const { return fPVec; }
187 
188  /**
189  evaluate the distribution (probability mesh function) at the integer value x.
190  Used internally by UnuRan
191  For integer values outside the domain the function must return 0.0
192  */
193  double Pmf ( int x) const;
194 
195  /**
196  evaluate the integral (cdf) on the given domain
197  */
198  double Cdf(int x) const;
199 
200 
201 protected:
202 
203 
204 private:
205 
206  std::vector<double> fPVec; //Vector of the probabilities
207  mutable std::vector<double> fPVecSum; //Vector of the sum of the probabilities
208  const ROOT::Math::IGenFunction *fPmf; //pointer to a function calculating the probability
209  const ROOT::Math::IGenFunction *fCdf; //pointer to the cumulative distribution function
210  int fXmin; //lower value of the domain
211  int fXmax; //upper value of the domain
212  int fMode; //mode of the distribution
213  double fSum; //total sum of the probabilities in the given domain
214  // flags
215  bool fHasDomain; //flag to control if distribution has a defined domain (otherwise is [0,INT_MAX])
216  bool fHasMode; //flag to control if distribution has a pre-computed mode
217  bool fHasSum; //flag to control if distribution has a pre-computed sum of the probabilities
218  bool fOwnFunc; // flag to control if distribution owns the funcitno pointers
219 
220  ClassDef(TUnuranDiscrDist,1) //Wrapper class for one dimensional discrete distribution
221 
222 
223 };
224 
225 
226 
227 #endif /* ROOT_Math_TUnuranDiscrDist */
TUnuranBaseDist, base class for Unuran distribution classees such as TUnuranContDist (for one-dimensi...
bool HasCdf() const
flag to control if distribution provides also a Cdf
int Mode() const
get the mode (x location of function maximum)
float xmin
Definition: THbookFile.cxx:93
void SetProbSum(double sum)
set the value of the sum of the probabilities in the given domain
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition: IFunction.h:133
const ROOT::Math::IGenFunction * fCdf
const ROOT::Math::IGenFunction * fPmf
void SetMode(int mode)
set the mode of the distribution (location of maximum probability)
ClassImp(TIterator) Bool_t TIterator return false
Compare two iterator objects.
Definition: TIterator.cxx:20
double Pmf(int x) const
evaluate the distribution (probability mesh function) at the integer value x.
TUnuranDiscrDist(Iterator *begin, Iterator *end)
Constructor from a vector of probability.
Double_t x[n]
Definition: legend1.C:17
#define ClassDef(name, id)
Definition: Rtypes.h:254
double cdf(double *x, double *p)
Definition: unuranDistr.cxx:44
TUnuranDiscrDist(const ROOT::Math::IGenFunction &func, bool copyFunc=false)
Constructor from a generic function object specifying the pdf.
bool GetDomain(int &xmin, int &xmax) const
check if distribution has domain and return in case its domain
void SetDomain(int xmin, int xmax)
Set the distribution domain, by default the domain is [0,INT_MAX] If xmin >= xmax a domain is removed...
TUnuranDiscrDist class for one dimensional discrete distribution.
void SetCdf(const ROOT::Math::IGenFunction &cdf)
set cdf distribution from a generic function interface.
const std::vector< double > & ProbVec() const
retrieve a reference to the vector of the probabilities : Prob(i) If the distribution is defined from...
bool HasMode() const
flag to control if distribution provides the mode
float xmax
Definition: THbookFile.cxx:93
TUnuranDiscrDist & operator=(const TUnuranDiscrDist &rhs)
Assignment operator.
double ProbSum() const
return area of the pdf
bool HasProbSum() const
flag to control if distribution provides the total area of the probability function ...
double func(double *x, double *p)
Definition: stressTF1.cxx:213
1-Dim function class
Definition: TF1.h:149
double Cdf(int x) const
evaluate the integral (cdf) on the given domain
virtual TUnuranDiscrDist * Clone() const
Clone (required by base class)
virtual ~TUnuranDiscrDist()
Destructor.
std::vector< double > fPVecSum
std::vector< double > fPVec