3 Universal Non Uniform Random number generator for generating non uniform pseudo-random numbers
5 \defgroup Unuran Unuran
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/]
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):
22 //initialize unuran to generate normal random numbers using a "arou" method
23 unr.Init("normal()","method=arou");
25 // sample distributions N times (generate N random numbers)
26 for (int i = 0; i < N; ++i)
27 double x = unr.Sample();
32 - Using a distribution object. We have then the following cases depending on the dimension and the distribution object.
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**.
44 Some of this information is required depending on the chosen UNURAN generation method.
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 < N; ++i)
53 double x = unr.Sample();
56 - For multi-dimensional distribution the class **TUnuranMultiContDist** must be used.
57 In this case only the multi-dimensional pdf is required
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)
66 for (int i = 0; i < N; ++i)
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.
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 < N; ++i)
81 int k = unr.SampleDiscr();
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.
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());
95 // sample N times (generate N random numbers)
96 for (int i = 0; i < N; ++i)
97 double x = unr.Sample();
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,....)`.
102 - For binned data (only one dimensional data are supported) one uses directly the histogram
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);
110 // sample N times (generate N random numbers)
111 for (int i = 0; i < N; ++i)
112 double x = unr.Sample();
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.
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).
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.