Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TUnuran.h
Go to the documentation of this file.
1// @(#)root/unuran:$Id$
2// Author: L. Moneta Tue Sep 26 16:25:09 2006
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7 * *
8 * *
9 **********************************************************************/
10
11// Header file for class TUnuran
12
13#ifndef ROOT_TUnuran
14#define ROOT_TUnuran
15
16#include <string>
17
18#include "TUnuranBaseDist.h"
19
20
21class TUnuranContDist;
24class TUnuranEmpDist;
25
26#include <memory>
27
28
29/**
30
31 \class TUnuran
32 \ingroup Unuran
33
34 TUnuran class.
35 Interface to the UNU.RAN package for generating non uniform random
36 numbers. This class wraps the UNU.RAN calls in C++ methods.
37 It provides methods for initializing Unuran and then to sample the
38 desired distribution.
39 It provides support for initializing UNU.RAN in these following way (various signatures
40 for TUnuran::Init)
41 - with string API via TUnuran::Init passing the distribution type and the method
42 - using a one-dimensional distribution object defined by TUnuranContDist
43 - using a multi-dimensional distribution object defined by TUnuranMultiContDist
44 - using a discrete one-dimensional distribution object defined by TUnuranDiscrDist
45 - using an empirical distribution defined by TUnuranEmpDist
46 - using pre-defined distributions. Presently only support for Poisson (TUnuran::InitPoisson)
47 and Binomial (TUnuran::InitBinomial) are provided. Other distributions can however be generated
48 using the previous methods (in particular via the string API)
49
50 The sampling is provided via these methods:
51 - TUnuran::Sample() returns a double for all one-dimensional distribution
52 - TUnuran::SampleDiscr() returns an integer for one-dimensional discrete distribution
53 - TUnuran::Sample(double *) sample a multi-dimensional distribution. A pointer to a vector with
54 size at least equal to the distribution dimension must be passed
55
56 In addition is possible to set the random number generator in the constructor of the class, its seed
57 via the TUnuran::SetSeed() method.
58*/
59
60
61
62//class TUnuranGenerator;
63struct unur_gen;
64typedef struct unur_gen UNUR_GEN;
65
66// struct unur_urng_generic;
67// typedef struct unur_urng_generic UNUR_URNG;
68
69struct unur_distr;
70typedef struct unur_distr UNUR_DISTR;
71
72struct unur_urng;
73typedef struct unur_urng UNUR_URNG;
74
75
76class TRandom;
77class TH1;
78
79class TUnuran {
80
81public:
82
83 /**
84 Constructor with a generator instance and given level of log output
85 */
86 TUnuran (TRandom * r = nullptr, unsigned int log = 0);
87
88
89 /**
90 Destructor
91 */
92 ~TUnuran ();
93
94private:
95 // usually copying is non trivial, so we make this unaccessible
96
97 /**
98 Copy constructor
99 */
100 TUnuran(const TUnuran &);
101
102 /**
103 Assignment operator
104 */
105 TUnuran & operator = (const TUnuran & rhs);
106
107public:
108 /**
109 Initialize with Unuran string API interface.
110 See https://statmath.wu.ac.at/unuran/doc/unuran.html#StringAPI
111
112 @param distr : UNU.RAN distribution string
113 @param method : UNU.RAN method string
114
115 Here is an example using the string API:
116 ```
117 Tunuran unr;
118 unr.Init("normal(3.,0.75); domain = (0,inf)", "method = tdr; c = 0");
119 ```
120 */
121 bool Init(const std::string & distr, const std::string & method);
122
123
124 /**
125 Initialize method for continuous one-dimensional distribution.
126 User must provide a distribution object (which is copied inside) and a string for a method.
127 For the list of available method for 1D cont. distribution see the
128 <A href="http://statmath.wu-wien.ac.at/unuran/doc/unuran.html#Methods_005ffor_005fCONT">UnuRan doc</A>.
129 A re-initialization is needed whenever distribution parameters have been changed.
130 Note that the method string can contain in addition to the method name all the specific method
131 parameters specified using the UNURAN method string API.
132 For example a valid string can be `"method=arou; max_segments=1000; max_sqhratio = 0.9"`
133 */
134 bool Init(const TUnuranContDist & distr, const std::string & method = "auto");
135
136 /**
137 Initialize method for continuous multi-dimensional distribution.
138 User must provide a distribution object (which is copied inside) and a string for a method.
139 For the list of available method for multivariate cont. distribution see the
140 <A href="http://statmath.wu-wien.ac.at/unuran/doc/unuran.html#Methods_005ffor_005fCVEC">UnuRan doc</A>
141 A re-initialization is needed whenever distribution parameters have been changed.
142
143 The default method used for multi-dimensional distributions is "vnrou"
144 Note that some of the multi-dimensional continuous distribution methods like "hitro" are based on Markov-CHain sampler and
145 they are much faster for sampling but require more time to converge. Furthermore, since they are Markov-Chain methods their
146 generated sample values are correlated and cannot be used as i.i.d., one can instead use the obtained sample distribution.
147 (see also the ROOT issue: #10222 ).
148
149 */
150 bool Init(const TUnuranMultiContDist & distr, const std::string & method = "vnrou");
151
152
153 /**
154 Initialize method for continuous one-dimensional discrete distribution.
155 User must provide a distribution object (which is copied inside) and a string for a method.
156 For the list of available method for 1D discrete distribution see the
157 <A href="http://statmath.wu-wien.ac.at/unuran/doc/unuran.html#Methods_005ffor_005fDISCR">UnuRan doc</A>
158 A re-initialization is needed whenever distribution parameters have been changed.
159
160 */
161 bool Init(const TUnuranDiscrDist & distr, const std::string & method = "auto");
162
163
164 /**
165 Initialize method for continuous empirical distribution.
166 User must provide a distribution object (which is copied inside) and a string for a method.
167 The distribution object can represent binned (only 1D) or unbinned (1D or multi-dim) data
168 The method for the unbinned empirical distribution are based on the kernel smoothing, see
169 <A href="http://statmath.wu-wien.ac.at/software/unuran/doc/unuran.html#EMPK">UnuRan doc</A>
170 A re-initialization is needed whenever distribution parameters have been changed.
171
172 */
173 bool Init(const TUnuranEmpDist & distr, const std::string & method = "empk");
174
175
176 /**
177 Initialize method for the Poisson distribution.
178 Used to generate poisson numbers for a constant parameter mu of the Poisson distribution.
179 Use after the method TUnuran::SampleDiscr to generate the numbers.
180 The flag reinit perform a fast re-initialization when only the distribution parameters
181 are changed in the subsequent calls.
182 If the same TUnuran object is used to generate with other distributions it cannot be used.
183 */
184 bool InitPoisson(double mu, const std::string & method = "dstd");
185
186 /**
187 Initialize method for the Binomial distribution.
188 Used to generate poisson numbers for a constant parameters (n,p) of the Binomial distribution.
189 Use after the method TUnuran::SampleDiscr to generate the numbers.
190 The flag reinit perform a fast re-initialization when only the distribution parameters
191 are changed in the subsequent calls.
192 If the same TUnuran object is used to generate with other distributions it cannot be used.
193 */
194 bool InitBinomial(unsigned int ntot, double prob, const std::string & method = "dstd");
195
196 /**
197 Reinitialize UNURAN by changing the distribution parameters but maintaining same distribution and method.
198 It is implemented now only for predefined discrete distributions like the poisson or the binomial
199 */
200 bool ReInitDiscrDist(unsigned int npar, double * params);
201
202 /**
203 Sample 1D distribution.
204 User is responsible for having previously correctly initialized with TUnuran::Init
205 */
206 double Sample();
207
208 /**
209 Sample multidimensional distributions.
210 User is responsible for having previously correctly initialized with TUnuran::Init
211 */
212 bool SampleMulti(double * x);
213
214 /**
215 Sample discrete distributions.
216 User is responsible for having previously correctly initialized with TUnuran::Init
217 */
218 int SampleDiscr();
219
220 /**
221 Set the random engine.
222 Must be called before init to have effect
223 */
225 fRng = r;
226 }
227
228 /**
229 Return instance of the random engine used.
230 */
232 return fRng;
233 }
234
235 /**
236 Return an information string about the used Unuran generator method.
237 @param extended : if true return some helper information about the existing options of the method.
238 */
239 std::string GetInfo(bool extended = false);
240
241 /**
242 Return an ID string about the unuran generator method.
243 */
244 std::string GetGenId() const;
245
246 /**
247 Return the dimension of unuran generator method.
248 For 1D method returns 1 and for the multi-dimensional case
249 must be equal to the distribution dimension.
250 */
251 int GetDimension() const;
252
253 /**
254 Return the type of the distribution.
255 See documentation of `unuran_distr_get_type` for the possible
256 types of distributions.
257 */
258 int GetDistType() const;
259
260 /// Return true for a univariate continuous distribution.
261 bool IsDistCont() const;
262 /// Return true for a multivariate continuous distribution.
263 bool IsDistMultiCont() const;
264 /// Return true for a discrete distribution.
265 bool IsDistDiscrete() const;
266 /// Return true for an empirical distribution.
267 bool IsDistEmpirical() const;
268
269
270
271 /**
272 set the seed for the random number generator
273 */
274 void SetSeed(unsigned int seed);
275
276 /**
277 set log level
278 */
279 bool SetLogLevel(unsigned int iflag = 1);
280
281 /**
282 set stream for log and error (not yet implemented)
283 */
284 bool SetLogStream() { return false;}
285
286 /**
287 used Unuran method
288 */
289 const std::string & MethodName() const { return fMethod; }
290
291protected:
292
293
294 bool SetRandomGenerator();
295
296 bool SetContDistribution(const TUnuranContDist & dist );
297
298 bool SetMultiDistribution(const TUnuranMultiContDist & dist );
299
300 bool SetDiscreteDistribution(const TUnuranDiscrDist & dist );
301
302 bool SetEmpiricalDistribution(const TUnuranEmpDist & dist );
303
304 /**
305 change the method and initialize Unuran with the previously given distribution
306 */
307 bool SetMethodAndInit();
308
309
310
311// private:
312
313 UNUR_GEN * fGen; //pointer to the UnuRan C generator struct
314 UNUR_DISTR * fUdistr; //pointer to the UnuRan C distribution struct
315 UNUR_URNG * fUrng; // pointer to Unuran C random generator struct
316 std::unique_ptr<TUnuranBaseDist> fDist; // pointer for distribution wrapper
317 TRandom * fRng; //pointer to ROOT random number generator
318 std::string fMethod; //string representing the method
319
320};
321
322
323#endif /* ROOT_Math_TUnuran */
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
struct unur_urng UNUR_URNG
Definition TUnuran.h:73
struct unur_gen UNUR_GEN
Definition TUnuran.h:64
struct unur_distr UNUR_DISTR
Definition TUnuran.h:70
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:59
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
TUnuranContDist class describing one dimensional continuous distribution.
TUnuranDiscrDist class for one dimensional discrete distribution.
TUnuranEmpDist class for describing empirical distributions.
TUnuranMultiContDist class describing multi dimensional continuous distributions.
TUnuran class.
Definition TUnuran.h:79
std::string GetGenId() const
Return an ID string about the unuran generator method.
Definition TUnuran.cxx:382
bool IsDistCont() const
Return true for a univariate continuous distribution.
Definition TUnuran.cxx:403
bool SetMethodAndInit()
change the method and initialize Unuran with the previously given distribution
Definition TUnuran.cxx:345
int SampleDiscr()
Sample discrete distributions.
Definition TUnuran.cxx:420
bool SetLogStream()
set stream for log and error (not yet implemented)
Definition TUnuran.h:284
const std::string & MethodName() const
used Unuran method
Definition TUnuran.h:289
std::unique_ptr< TUnuranBaseDist > fDist
Definition TUnuran.h:316
bool SetDiscreteDistribution(const TUnuranDiscrDist &dist)
Definition TUnuran.cxx:300
int GetDistType() const
Return the type of the distribution.
Definition TUnuran.cxx:396
bool InitBinomial(unsigned int ntot, double prob, const std::string &method="dstd")
Initialize method for the Binomial distribution.
Definition TUnuran.cxx:475
bool SetContDistribution(const TUnuranContDist &dist)
Definition TUnuran.cxx:169
std::string GetInfo(bool extended=false)
Return an information string about the used Unuran generator method.
Definition TUnuran.cxx:375
bool SampleMulti(double *x)
Sample multidimensional distributions.
Definition TUnuran.cxx:434
bool ReInitDiscrDist(unsigned int npar, double *params)
Reinitialize UNURAN by changing the distribution parameters but maintaining same distribution and met...
Definition TUnuran.cxx:490
bool IsDistMultiCont() const
Return true for a multivariate continuous distribution.
Definition TUnuran.cxx:407
UNUR_DISTR * fUdistr
Definition TUnuran.h:314
bool Init(const std::string &distr, const std::string &method)
Initialize with Unuran string API interface.
Definition TUnuran.cxx:75
UNUR_GEN * fGen
Definition TUnuran.h:313
bool SetRandomGenerator()
Definition TUnuran.cxx:152
bool SetLogLevel(unsigned int iflag=1)
set log level
Definition TUnuran.cxx:446
UNUR_URNG * fUrng
Definition TUnuran.h:315
TRandom * fRng
Definition TUnuran.h:317
bool SetMultiDistribution(const TUnuranMultiContDist &dist)
Definition TUnuran.cxx:215
bool IsDistEmpirical() const
Return true for an empirical distribution.
Definition TUnuran.cxx:415
double Sample()
Sample 1D distribution.
Definition TUnuran.cxx:427
bool SetEmpiricalDistribution(const TUnuranEmpDist &dist)
Definition TUnuran.cxx:259
TRandom * GetRandom()
Return instance of the random engine used.
Definition TUnuran.h:231
~TUnuran()
Destructor.
Definition TUnuran.cxx:53
bool InitPoisson(double mu, const std::string &method="dstd")
Initialize method for the Poisson distribution.
Definition TUnuran.cxx:461
std::string fMethod
Definition TUnuran.h:318
TUnuran & operator=(const TUnuran &rhs)
Assignment operator.
Definition TUnuran.cxx:68
int GetDimension() const
Return the dimension of unuran generator method.
Definition TUnuran.cxx:389
void SetSeed(unsigned int seed)
set the seed for the random number generator
Definition TUnuran.cxx:442
void SetRandom(TRandom *r)
Set the random engine.
Definition TUnuran.h:224
bool IsDistDiscrete() const
Return true for a discrete distribution.
Definition TUnuran.cxx:411
Double_t x[n]
Definition legend1.C:17