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