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