A typical value produced by the algorithm only requires the generation of one random floating-point value and one random table index, followed by one table lookup, one multiply operation and one comparison. Sometimes (2.5% of the time, in the case of a normal or exponential distribution when using typical table sizes)[citation needed] more computations are required. Nevertheless, the algorithm is computationally much faster[citation needed] than the two most commonly used methods of generating normally distributed random numbers, the Marsaglia polar method and the Box–Muller transform, which require at least one logarithm and one square root calculation for each pair of generated values. However, since the ziggurat algorithm is more complex to implement it is best used when large quantities of random numbers are required.
The term ziggurat algorithm dates from Marsaglia's paper with Wai Wan Tsang in 2000; it is so named because it is conceptually based on covering the probability distribution with rectangular segments stacked in decreasing order of size, resulting in a figure that resembles a ziggurat.
Theory of operation
The ziggurat algorithm is a rejection sampling algorithm; it randomly generates a point in a distribution slightly larger than the desired distribution, then tests whether the generated point is inside the desired distribution. If not, it tries again. Given a random point underneath a probability density curve, its x coordinate is a random number with the desired distribution.
The distribution the ziggurat algorithm chooses from is made up of n equal-area regions; n − 1 rectangles that cover the bulk of the desired distribution, on top of a non-rectangular base that includes the tail of the distribution.
Given a monotone decreasing probability density function f(x), defined for all x ≥ 0, the base of the ziggurat is defined as all points inside the distribution and below y1 = f(x1). This consists of a rectangular region from (0, 0) to (x1, y1), and the (typically infinite) tail of the distribution, where x > x1 (and y < y1).
This layer (call it layer 0) has area A. On top of this, add a rectangular layer of width x1 and height A/x1, so it also has area A. The top of this layer is at height y2 = y1 + A/x1, and intersects the density function at a point (x2, y2), where y2 = f(x2). This layer includes every point in the density function between y1 and y2, but (unlike the base layer) also includes points such as (x1, y2) which are not in the desired distribution.
Further layers are then stacked on top. To use a precomputed table of size n (n = 256 is typical), one chooses x1 such that xn = 0, meaning that the top box, layer n − 1, reaches the distribution's peak at (0, f(0)) exactly.
Layer i extends vertically from yi to yi +1, and can be divided into two regions horizontally: the (generally larger) portion from 0 to xi +1 which is entirely contained within the desired distribution, and the (small) portion from xi +1 to xi, which is only partially contained.
Ignoring for a moment the problem of layer 0, and given uniform random variables U0 and U1 ∈ [0,1), the ziggurat algorithm can be described as:
Choose a random layer 0 ≤ i < n.
Let x = U0xi.
If x < xi +1, return x.
Let y = yi + U1(yi +1 − yi).
Compute f(x). If y < f(x), return x.
Otherwise, choose new random numbers and go back to step 1.
Step 1 amounts to choosing a low-resolution y coordinate. Step 3 tests if the x coordinate is clearly within the desired density function without knowing more about the y coordinate. If it is not, step 4 chooses a high-resolution y coordinate, and step 5 does the rejection test.
With closely spaced layers, the algorithm terminates at step 3 a very large fraction of the time. For the top layer n − 1, however, this test always fails, because xn = 0.
Layer 0 can also be divided into a central region and an edge, but the edge is an infinite tail. To use the same algorithm to check if the point is in the central region, generate a fictitious x0 = A/y1. This will generate points with x < x1 with the correct frequency, and in the rare case that layer 0 is selected and x ≥ x1, use a special fallback algorithm to select a point at random from the tail. Because the fallback algorithm is used less than one time in a thousand, speed is not essential.
Thus, the full ziggurat algorithm for one-sided distributions is:
Choose a random layer 0 ≤ i < n.
Let x = U0xi
If x < xi +1, return x.
If i = 0, generate a point from the tail using the fallback algorithm.
Let y = yi + U1(yi +1 − yi).
Compute f(x). If y < f(x), return x.
Otherwise, choose new random numbers and go back to step 1.
For a two-sided distribution, the result must be negated 50% of the time. This can often be done conveniently by choosing U0 ∈ (−1,1) and, in step 3, testing if |x| < xi +1.
Fallback algorithms for the tail
Because the ziggurat algorithm only generates most outputs very rapidly, and requires a fallback algorithm whenever x > x1, it is always more complex than a more direct implementation. The specific fallback algorithm depends on the distribution.
For an exponential distribution, the tail looks just like the body of the distribution. One way is to fall back to the most elementary algorithm E = −ln(U1) and let x = x1 − ln(U1). Another is to call the ziggurat algorithm recursively and add x1 to the result.
For a normal distribution, Marsaglia suggests a compact algorithm:
Let x = −ln(U1)/x1.
Let y = −ln(U2).
If 2y > x2, return x + x1.
Otherwise, go back to step 1.
Since x1 ≈ 3.5 for typical table sizes, the test in step 3 is almost always successful. Since −ln(U1) is an exponentially distributed variate, an implementation of the exponential distribution may be used.
Optimizations
The algorithm can be performed efficiently with precomputed tables of xi and yi = f(xi), but there are some modifications to make it even faster:
Nothing in the ziggurat algorithm depends on the probability distribution function being normalized (integral under the curve equal to 1), removing normalizing constants can speed up the computation of f(x).
Most uniform random number generators are based on integer random number generators which return an integer in the range [0, 232 − 1]. A table of 2−32xi lets you use such numbers directly for U0.
When computing two-sided distributions using a two-sided U0 as described earlier, the random integer can be interpreted as a signed number in the range [−231, 231 − 1], and a scale factor of 2−31 can be used.
Rather than comparing U0xi to xi +1 in step 3, it is possible to precompute xi +1/xi and compare U0 with that directly. If U0 is an integer random number generator, these limits may be premultiplied by 232 (or 231, as appropriate) so an integer comparison can be used.
With the above two changes, the table of unmodified xi values is no longer needed and may be deleted.
When generating IEEE 754 single-precision floating point values, which only have a 24-bit mantissa (including the implicit leading 1), the least-significant bits of a 32-bit integer random number are not used. These bits may be used to select the layer number. (See the references below for a detailed discussion of this.)
The first three steps may be put into an inline function, which can call an out-of-line implementation of the less frequently needed steps.
Generating the tables
It is possible to store the entire table precomputed, or just include the values n, y1, A, and an implementation of f −1(y) in the source code, and compute the remaining values when initializing the random number generator.
As previously described, you can find xi = f −1(yi) and yi +1 = yi + A/xi. Repeat n − 1 times for the layers of the ziggurat. At the end, you should have yn = f(0). There will be some round-off error, but it is a useful sanity test to see that it is acceptably small.
When actually filling in the table values, just assume that xn = 0 and yn = f(0), and accept the slight difference in layer n − 1's area as rounding error.
Finding x1 and A
Given an initial (guess at) x1, you need a way to compute the area t of the tail for which x > x1. For the exponential distribution, this is just e−x1, while for the normal distribution, assuming you are using the unnormalized f(x) = e−x2/2, this is √π/2erfc(x/√2). For more awkward distributions, numerical integration may be required.
With this in hand, from x1, you can find y1 = f(x1), the area t in the tail, and the area of the base layer A = x1y1 + t.
Then compute the series yi and xi as above. If yi > f(0) for any i < n, then the initial estimate x1 was too low, leading to too large an area A. If yn < f(0), then the initial estimate x1 was too high.
Given this, use a root-finding algorithm (such as the bisection method) to find the value x1 which produces yn−1 as close to f(0) as possible. Alternatively, look for the value which makes the area of the topmost layer, xn−1(f(0) − yn−1), as close to the desired value A as possible. This saves one evaluation of f −1(x) and is actually the condition of greatest interest.
McFarland's variation
Christopher D. McFarland has proposed a further-optimized version.[1] This applies three algorithmic changes, at the expense of slightly larger tables.
First, the common case considers only the rectangular portions, from (0, yi −1) to (xi, yi) The odd-shaped regions to the right of these (mostly almost triangular, plus the tail) are handled separately. This simplifies and speeds up the algorithm's fast path.
Second, the exact area of the odd-shaped regions is used; they are not rounded up to include the entire rectangle to (xi −1, yi). This increases the probability that the fast path will be used.
One major consequence of this is that the number of layers is slightly less than n. Even though the area of the odd-shaped portions is taken exactly, the total adds up to more than one layer's worth. The area per layer is adjusted so that the number of rectangular layers is an integer. If the initial 0 ≤ i < n exceeds the number of rectangular layers, phase 2 proceeds.
If the value sought lies in any of the odd-shaped regions, the alias method is used to choose one, based on its true area. This is a small amount of additional work, and requires additional alias tables, but chooses one of the layers' right-hand sides.
The chosen odd-shaped region is rejection sampled, but if a sample is rejected, the algorithm does not return to the beginning. The true area of each odd-shaped region was used to choose a layer, so the rejection-sampling loop stays in that layer until a point is chosen.
Third, the almost-triangular shape of most odd-shaped portions is exploited, although this must be divided into three cases depending on the second derivative of the probability distribution function in the selected layer.
If the function is convex (as the exponential distribution is everywhere, and the normal distribution is for |x| > 1), then the function is strictly contained within the lower triangle. Two unit uniform deviates U1 and U2 are chosen, and before they are scaled to the rectangle enclosing the odd-shaped region, their sum is tested. If U1 + U2 > 1, the point is in the upper triangle and can be reflected to (1−U1, 1−U2). Then, if U1 + U2 < 1−ε, for some suitable tolerance ε, the point is definitely below the curve and can immediately be accepted. Only for points very close to the diagonal is it necessary to compute the distribution function f(x) to perform an exact rejection test. (The tolerance ε should in theory depend on the layer, but a single maximum value can be used on all layers with little loss.)
If the function is concave (as the normal distribution is for |x| < 1), it includes a small portion of the upper triangle so reflection is impossible, but points whose normalized coordinates satisfy U1 + U2 ≤ 1 can be immediately accepted, and points for which U1 + U2 > 1+ε can be immediately rejected.
In the one layer which straddles |x| = 1, the normal distribution has an inflection point, and the exact rejection test must be applied if 1−ε <U1 + U2 < 1+ε.
The tail is handled as in the original Ziggurat algorithm, and can be thought of as a fourth case for the shape of the odd-shaped region to the right.
Marsaglia, George; Tsang, Wai Wan (2 October 2000). "The Ziggurat Method for Generating Random Variables". Journal of Statistical Software. 5 (8). Retrieved 2007-06-20. This paper numbers the layers from 1 starting at the top, and makes layer 0 at the bottom a special case, while the explanation above numbers layers from 0 at the bottom.
Jurgen A. Doornik (2005). "An Improved Ziggurat Method to Generate Normal Random Samples"(PDF). Nuffield College, Oxford. Retrieved 2007-06-20. Describes the hazards of using the least-significant bits of the integer random number generator to choose the layer number.
Normal Behavior By Cleve Moler, MathWorks, describing the ziggurat algorithm introduced in MATLAB version 5, 2001.
David B. Thomas; Philip H.W. Leong; Wayne Luk; John D. Villasenor (October 2007). "Gaussian Random Number Generators"(PDF). ACM Computing Surveys. 39 (4): 11:1–38. doi:10.1145/1287620.1287622. ISSN0360-0300. S2CID10948255. Retrieved 2009-07-27. [W]hen maintaining extremely high statistical quality is the first priority, and subject to that constraint, speed is also desired, the Ziggurat method will often be the most appropriate choice. Comparison of several algorithms for generating Gaussian random numbers.
Nadler, Boaz (2006). "Design Flaws in the Implementation of the Ziggurat and Monty Python methods (And some remarks on Matlab randn)". arXiv:math/0603058.. Illustrates problems with underlying uniform pseudo-random number generators and how those problems affect the ziggurat algorithm's output.
ФуліньїFouligny Країна Франція Регіон Гранд-Ест Департамент Мозель Округ Форбак-Буле-Мозель Кантон Фолькемон Код INSEE 57230 Поштові індекси 57220 Координати 49°05′39″ пн. ш. 6°30′19″ сх. д.H G O Висота 219 - 315 м.н.р.м. Площа 5,96 км² Населення 194 (01-2020[1]) Густота 35,07 о...
El Modulón, Centro Comunitario, Ciudad de México, 1998. X'teresa, Centro de Arte Alternativo, Ciudad de México, 1996. Escuela Nacional de Danza, Centro Nacional de las Artes, Ciudad de México, 1996 (vista general). Escuela Nacional de Danza, Centro Nacional de las Artes, Ciudad de México, 1996 (detalle). torreHelix, Guadalajara, México, 2008 (proyecto). torreHelix, Guadalajara, México, 2008 (proyecto). Luis Vicente Flores es un arquitecto mexicano perteneciente a la generación de la d...
. Млини ударної дії — млини, в яких подрібнення відбувається внаслідок зіткнення частинок з робочими елементами ротора, які швидко обертаються у нерухомому корпусі. До цього типу млинів належать дезінтегратори і дисмембратори. Зміст 1 Конструкція і принцип дії 1.1 Дезін
Garuda Indonesia Penerbangan 152PK-GAI, Pesawat yang terlibat dalam kecelakaan, di Bandar Udara Internasional Ngurah Rai pada Tanggal 17 November 1988.Ringkasan peristiwaTanggal26 September 1997RingkasanMenabrak gunung (disebabkan kesalahan ATC), Cuaca Kurang Baik disertakan Awan TebalLokasiDesa Buah Nabar, Deli Serdang,Sumatera Utara, IndonesiaPenumpang222Awak12Tewas234Selamat0Jenis pesawatAirbus A300-B4OperatorGaruda IndonesiaRegistrasiPK-GAI Garuda Indonesia Penerbangan GA 152 adalah ...
German infantry weapons in the Askifou War Museum, Crete Lists of World War II military equipment are lists of military equipment in use during World War II (1939–1945). They include lists of aircraft, ships, vehicles, weapons, personal equipment, uniforms, and other equipment. There are aggregated military equipment lists by country, and lists of classes of equipment broken down by country or by type. Aircraft List of aircraft of World War II List of World War II military aircraft of Germa...
Railway station in Tsukuba, Ibaraki Prefecture, Japan This article is about the railway station in Ibaraki. For the monorail station in Osaka, see Bampaku-kinen-kōen Station. Bampaku-kinenkōen Station万博記念公園駅The east side of Bampaku-kinenkōen Station in June 2010General informationLocation4386 Shimana, Tsukuba-shi, Ibaraki-ken 300-2655JapanCoordinates36°03′30″N 140°03′34″E / 36.058446°N 140.059389°E / 36.058446; 140.059389Operated byMetropol...
For other uses, see Kantishna. AirportKantishna AirportIATA: noneICAO: noneFAA LID: 5Z5SummaryAirport typePublicOwnerState of AlaskaServesKantishna, AlaskaElevation AMSL1,575 ft / 480 mCoordinates63°32′30″N 150°59′38″W / 63.54167°N 150.99389°W / 63.54167; -150.99389Map5Z5Location of airport in AlaskaRunways Direction Length Surface ft m 10/28 1,850 564 Gravel StatisticsAircraft operations (2005)1,200Enplanements (2008)985Sources: Federal Avia...
Newmarket ParkThe park's entrance in the west, off Sarawia StreetLocationNewmarket, AucklandCoordinates36°51′53″S 174°47′00″E / 36.8646°S 174.7833°E / -36.8646; 174.7833Area6 haOperated byAuckland Council Newmarket Park is an approximately 6-hectare-large (15-acre) park in Auckland, New Zealand.[1] It is located in the triangle between three suburbs, northeast of the Newmarket, southeast of Parnell and northwest of Remuera. It is located p...
محتوى هذه المقالة في طور الترجمة من مقالة «List of Mesopotamian deities» في ويكيبيديا الإنجليزية. رجاءً لا تحرر هذه الصفحة في أثناء وجود هذه الرسالة في رأسها، لكي لا يحدث أي تضارب في التحرير. ختم أسطواني أكدي يعود لحوالي سنة 2300 ق. م، وتصور الآلهة إنانا وأوتو وإنكي وإسيمود.[1] آلهة الع...
Korean shamanic ritual for healing mental illnesses Durin-gutPicture of a Durin-gut in 1971Korean nameHangul두린굿Revised RomanizationDurin-gutMcCune–ReischauerTurin-gutOther nameHangul미친굿Revised RomanizationMichin-gutMcCune–ReischauerMich'in-gutOther nameHangul추는굿Revised RomanizationChuneun-gutMcCune–ReischauerCh'unŭn-gut The Durin-gut (lit. 'deranged ritual'), also called the Michin-gut (lit. 'insane ritual') and the Chuneun-gut (lit. 'dancing...
Madison, WisconsinPusat kota Madison LambangJulukan: Mad Town atau Mad CityMadison di Dane County, WisconsinMunisipalKotaIncorporated1848Pemerintahan • MayorPaul SoglinPopulasi (2005) • Kota221,551 • Metropolitan366,950Zona waktuUTC-6 (Central)Situs webhttp://www.ci.madison.wi.us Madison adalah ibu kota negara bagian Wisconsin. Penduduknya berjumlah 218.000 jiwa (2005). Madison merupakan pusat administratif dari Dane County dan merupakan kota kedu...
هذه المقالة يتيمة إذ تصل إليها مقالات أخرى قليلة جدًا. فضلًا، ساعد بإضافة وصلة إليها في مقالات متعلقة بها. (فبراير 2021) فينيسيوس ميلر معلومات شخصية الميلاد سنة 1990 (العمر 32–33 سنة) ريو دي جانيرو الطول 1.74 م (5 قدم 8 1⁄2 بوصة) مركز اللعب وسط الجنسية البرازيل ...
Belum Cukup UmurSutradara Nayato Fio Nuala Produser Albert Pondaga Ditulis oleh Viva Westi PemeranJoanna AlexandraZidni AdamMentariPenata musikTya Subiyakto SatrioSinematograferFreddy A. LinggaPenyuntingHerman Kumala PancaDistributorHand Made FilmTanggal rilis4 Maret 2010Durasi77 menit[1]Negara Indonesia Bahasa Indonesia Belum Cukup Umur adalah film Indonesia yang dirilis pada tahun 4 Maret 2010 yang disutradarai oleh Nayato Fio Nuala. Film ini dibintangi antara lain oleh Joanna...
The examples and perspective in this article may not represent a worldwide view of the subject. You may improve this article, discuss the issue on the talk page, or create a new article, as appropriate. (May 2022) (Learn how and when to remove this template message) Field of scientific study Part of a series onPsychology Outline History Subfields Basic psychology Abnormal Affective neuroscience Affective science Behavioral genetics Behavioral neuroscience Behaviorism Cognitive/Cognitivism Cog...
Artikel ini sebatang kara, artinya tidak ada artikel lain yang memiliki pranala balik ke halaman ini.Bantulah menambah pranala ke artikel ini dari artikel yang berhubungan atau coba peralatan pencari pranala.Tag ini diberikan pada Februari 2023. Sungai HangatDesaNegara IndonesiaProvinsiJambiKabupatenKerinciKecamatanGunung RayaKode Kemendagri15.01.01.2016 Luas- km²Jumlah penduduk- jiwaKepadatan- jiwa/km² Sungai Hangat adalah salah satu desa yang berada di Kecamatan Gunung Raya, Kabupate...
2005 demo album by VektorDemolitionDemo album by VektorReleased2005/2006GenreTechnical thrash metal, progressive metalLength48:17LabelSelf-releasedVektor chronology Nucleus(2003) Demolition(2005) Black Future(2009) Demolition is the first full-length demo recorded by the thrash metal band Vektor. Although a self-released demo, it is occasionally regarded as the band's first full-length album, due to its 48-minute length. Track listing No.TitleLength1.Spiral Galaxy (instrumental)1:132....
Strategi Solo vs Squad di Free Fire: Cara Menang Mudah!