Ziggourat method

The method Ziggourat is an algorithm to generate random random numbers of law not uniforms. It is about a method of rejection and to be selected to simulate a Random variable having a strictly monotonous density. This method can also be applied to symmetrical laws unimodal such that the normal Law by choosing a point on one of the halves and by choosing the side by chance. This method was developed by George Marsaglia and Wai Wan Tang. Like the majority of the algorithms of this type, it in general supposes that one has a generator of random random numbers of uniform law, a pseudo-random generator. In the majority of the cases, like the normal Law or the exponential Law, the algoritme consists to generate a floating number, a random index of table, to make a research in a table, a multiplication and a comparison. This algorithm is considerably faster than the other methods to simulate variables random of normal law, as the Method of Boxes-Muller which require to calculate a square root or a logarithm. On another side, this method is more complex to implement and requires precalculated tables, so that it is to use best when one needs random random numbers in great quantity.

The name of this method comes owing to the fact that it covers the density of the law with rectangular segments piled up by order of size decreasing, what thus resembles a Ziggourat.

Theory

The Ziggourate method is a method of rejection. The position of a point is generated by chance inside a curve slightly more delimiting one surface slightly larger than that délimitéé by the density of the law considered. One tests then if this point is in this last surface. If it is the case, one turns over the X-coordinate of the point. If not, it is rejected this point and one draws new.

To build the surface which recovers the density, one chooses a surface made up of n areas of equal sizes, of which n-1 is rectangles piled up with the top of a nonrectangular area which covers the tail of the density of the law.

If f is the density of the law to be simulated, the base of the ziggourate is thus composed of an active rectangle whose left lower corner for coordonéées (0,0) and the corner higher than for coordinates (x_0, F (x_0)), and of the whole of the points located under the curve (X, F (X)) for x \ geq x_0. This layer delimits an area of surface A. One places at the top a rectangular area of which the left lower corner is (0, x_0) and the corner higher right (x_1, F (x_0)), where x_1 is so that the surface of this rectangle is equal to A. One builds in the same way a rectangle of coordinates defined by (0, x_1) and (x_2, F (x_1)) of surface A One thus builds a succession of points x_0, x_1, \ ldots, x_n for a n number of layers given in advance (typically, n-256). The coordinates of the points x_0, x_1, \ ldots, x_n depend on n.

By being unaware of for the moment the problem of the first layer which is not rectangular, the algorithm is the following:

  1. One chooses way by chance a i layer with a probability 1/n.

  2. If i=0, one uses a specific algorithm then (see low)
  3. One gives itself a U_0 realization of a uniform random variable on # One poses x-U_0x_ {i-1}
  4. If x \ Leq x_i, then one turns over x
  5. If not one gives oneself a U_1 realization of a uniform random variable on # One calculates y=y_ {i-1} +U_1 (y_i there _ {i-1}) with y_j=f (x_j)
  6. If f (X) >y, one turns over x
  7. If not, one starts again the algoritme since the beginning.

For a law of which the density is symmetrical, it is just enough to test if |X|\ Leq x_i by using for U_0 one uniform random variable on ] - 1,1

Algorithms for the tail of the density

Losrque density with a noncompact support (as it is the case for the normal Loi, the exponential law,…), it is then necessary to use a specific algorithm when the first section is selected. This algorithm depends on law. The first layer can be divided into a rectangular central area, and an area with an infinite tail. One can use the same algorithm by adding a fictitious point x_ {- 1} =A/y_0 and by drawing a point x=U_0x_ {- 1} where U_0 a U_0 realization of a uniform random variable on If x \ Leq x_0, one turns over x. If not, one needs to draw a realization from this random variable knowing that it is larger that the value x_0. For the exponential law of parameter \ lambda, that is done very easily by x=x_0- \ ln (U) where U is the realization of a uniform random variable on

Another possibility consists in calling the Ziggourate algorithm in a recursive way on the tail of the density.

For the normal law, G. Marsaglia proposes the following method:

  1. One calculates x=- \ ln (U_1) /x_0 where U_1 is the realization of a uniform random variable on # Let y=- \ ln (U_2) where U_2 is the realization of a uniform random variable on # If 2y>x^2 then one turns over x+x_0,
  2. If not, one returns to the first step.

As for a typical size of table, x_0 \ approx 3.5, the test of step 3 is almost always checked. Let us note that - \ ln (U_i) returns a realization of an exponential random variable. If one lays out of a Ziggourate method for the exponential one, one can use it for this purpose.

Optimizations

The algorithm can turn in an effective way by using tables precalculated for the x_i and the y_i, but there exist easy ways to make it still faster. The principal one is that nothing requires that one uses for f a function of surface 1. One can thus use this algorithm on not standardized densities, which accelerates the calculation of f (X) (for example, for the normal law, one can use f (X) = \ exp (- x^2/2) instead of f (X) = \ exp (x^2/2)/\ sqrt {2 \ pi} ).

How to generate the tables?

It is possible to store in the algorithm the whole table of the points x_i and f (x_i) , or just them values n, A and x_0 and to calculate the other values right during the phase of initialization calculations.

How to find x_0 and A?

Being given a number of level n, one needs to find the value of x_0 and A such as x_0 \ times F (x_0) + \ int_ {x_0} ^ {+ \ infty} F (T) \ mathrm {D} t=A and x_ {n-1} \ times (F (0) - F (x_ {n-1}))=A. For the exponential law of paramète \ lambda, one has the surface \ int_ {x_0} ^ {+ \ infty} F (T) \ mathrm {D} t=- \ lambda^ {- 1} \ exp (- \ lambda x_0) . The function of distribution of the normal law is very often implemented in many numeric libraries. For other laws, it can be necessary to use procedures of numerical integration. The idea is then to use an algorithm of search for zero on the basis of an estimate initial of x_0 in order to solve

x_0 \ times F (x_0) + \ int_ {x_0} ^ {+ \ infty} F (T) \ mathrm {D} t=x_ {n-1} \ times (F (0) - F (x_ {n-1}))

by calculating the x_i successively until x_ {n-1} so that the surface of each section is equal to x_0 \ times F (x_0) + \ int_ {x_0} ^ {+ \ infty} F (T) \ mathrm {D} t=A for an estimate data of x_0_/0

See too

References

  • George Marsaglia and Wai Wan Tsang, “The Ziggurat Method for Generating Random Variables”, in Newspaper off Statistical Software , volume 5,8,2000.
  • Jurgen A. Doornik, “Year Improved Ziggurat Method to Normal Generate Random Samples”, Nuffield College, Oxford.
  • David B. Thomas, Philip G.W. Leong, Wayne Luk and John D. Villasenor, “Gaussian Random Number Generators”, in ACM Computing Surveys , volume 39,2007.
Random links:Duck mallard | Frederic Magné | The Community of communes of Thiérache of the Center | Oudewater | Jacques Népote | Aéroport_d'admission_de_Rankin