Logo ROOT   6.10/00
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
math/unuran/doc/Unuran.md
Go to the documentation of this file.
1 /**
2 
3 Universal Non Uniform Random number generator for generating non uniform pseudo-random numbers
4 
5 \defgroup Unuran Unuran
6 
7 \ingroup Math
8 
9 UNU.RAN, (Universal Non Uniform Random number generator for generating non uniform pseudo-random numbers)
10 is an ANSI C library licensed under GPL.<br>
11 It contains universal (also called automatic or black-box) algorithms that can generate random numbers from
12 large classes of continuous or discrete distributions, and also from practically all standard distributions.
13 An extensive online documentation are available at the (UNU.RAN Web Site)[http://statistik.wu-wien.ac.at/unuran/]
14 
15 New classes have been introduced to use the UNU.RAN C library from ROOT and C++ from ROOT and using C++ objects.
16 To use UNU.RAN one needs always an instance of the class **TUnuran**.
17 It can then be used in two distinct ways:
18 - using the UNU.RAN native string API for pre-defined distributions (see<a href="http://statistik.wu-wien.ac.at/unuran/doc/unuran.html#StringAPI"> UNU.RAN documentation</a> for the string API):
19 
20 ~~~{.cpp}
21  TUnuran unr;
22  //initialize unuran to generate normal random numbers using a "arou" method
23  unr.Init("normal()","method=arou");
24  //......
25  // sample distributions N times (generate N random numbers)
26  for (int i = 0; i &lt; N; ++i)
27  double x = unr.Sample();
28 
29 ~~~
30 
31 
32 - Using a distribution object. We have then the following cases depending on the dimension and the distribution object.
33 
34 - For 1D distribution the class **TUnuranContDist** must be used.
35  - A **TUnuranContDist** object can be created from a function
36  providing the pdf (probability density function) and optionally one providing the derivative of the pdf.
37  - If the derivative is not provided and the generation method requires it, then it is estimated numerically.
38  - The user can optionally provide the
39  - cdf (cumulative distribution function) via the **TUnuranContDist::SetCdf** function,
40  - the mode via **TUnuranContDist::SetMode**,
41  - the domain via **TUnuranContDist::SetDomain** for generating numbers in a restricted region,
42  - the area below the pdf via **TUnuranContDist::SetPdfArea**.
43 
44 Some of this information is required depending on the chosen UNURAN generation method.
45 
46 ~~~~{.cpp}
47  //1D case: create a distribution from two TF1 object pointers pdfFunc
48  TUnuranContDist dist( pdfFunc);
49  //initialize unuran passing the distribution and a string defining the method
50  unr.Init(dist, "method=hinv");
51  // sample distribution N times (generate N random numbers)
52  for (int i = 0; i &lt; N; ++i)
53  double x = unr.Sample();
54 ~~~~
55 
56 - For multi-dimensional distribution the class **TUnuranMultiContDist** must be used.
57 In this case only the multi-dimensional pdf is required
58 
59 ~~~~{.cpp}
60  //Multi-Dim case from a TF1 (or TF2 or TF3) object describing a multi-dimensional function
61  TUnuranMultiContDist dist( pdfFuncMulti);
62  // the recommended method for multi-dimensional function is "hitro"
63  unr.Init(dist, "method=hitro");
64  // sample distribution N times (generate N random numbers)
65  double x[NDIM];
66  for (int i = 0; i &lt; N; ++i)
67  unr.SampleMulti(x);
68 ~~~~
69 
70 - For discrete distribution the class **TUnuranDiscrDist** must be used.
71  The distribution can be initialized from a TF1 or from a vector of probabilities.
72 
73 ~~~~{.cpp}
74  // create distribution from a vector of probabilities
75  double pv[NSize] = {0.1,0.2,.......};
76  TUnuranDiscrDist dist(pv, pv+NSize);
77  // the recommended method for discrete distribution is
78  unr.Init(dist, "method=dgt");
79  // sample N times (generate N random numbers)
80  for (int i = 0; i &lt; N; ++i)
81  int k = unr.SampleDiscr();
82 ~~~~
83 
84 - For empirical distribution the class **TUnuranEmpDist** must be used.
85  In this case one can generate random numbers from a set of data (un-binned) in one or multi-dimension or
86  from a set of binned data in one dimension (similar to TH1::GetRandom() ).
87  - For unbin data the parent distribution is estimated by UNU.RAN using a gaussian kernel smoothing algorithm.
88  One can create the distribution class directly from a vector of data or from the buffer of TH1.
89 
90 ~~~~{.cpp}
91  // create distribution from a set of data 1D
92  // vdata is an std::vector containing the data
93  TUnuranEmpDist dist( vdata.begin(),vdata.end());
94  unr.Init(dist);
95  // sample N times (generate N random numbers)
96  for (int i = 0; i &lt; N; ++i)
97  double x = unr.Sample();
98 ~~~~
99 
100 - In the case of multi-dimension emprical distributions one needs to pass in addition to the iterators, the data dimension. It is assumed that the data are stored in the vector in this order : `(x0,y0,...),(x1,y1,....)`.
101 
102 - For binned data (only one dimensional data are supported) one uses directly the histogram
103 
104 ~~~{.cpp}
105  // create an empirical distribution from an histogram
106  // if the histogram has a buffer one must use TUnuranEmpDist(h1,false)
107  TH1 * h1 = ... // histogram pointer
108  TUnuranEmpDist binDist( h1);
109  unr.Init(binDist);
110  // sample N times (generate N random numbers)
111  for (int i = 0; i &lt; N; ++i)
112  double x = unr.Sample();
113 ~~~
114 
115 - This is equivalent to TH1::GetRandom(), but sampling is faster, therefore, since it requires some initialization time,
116  it becomes convenient when generating a large sample of random numbers.
117 
118 Functionality is also provided via the C++ classes for using a different random number generator by passing a
119 TRandom pointer when constructing the TUnuran class (by default the ROOT gRandom is passed to UNURAN).
120 
121 The (UNU.RAN documentation)[http://statistik.wu-wien.ac.at/unuran/doc/unuran.html#Top] provides a detailed
122 description of all the available methods and the possible options which one can pass to UNU.RAN for the various distributions.
123 */
124