Some Simple Gamma Variate Generators

7 downloads 664 Views 659KB Size Report
5 hours ago - For more information about JSTOR, please contact support@jstor.org. Your use of .... Tadikamalla 20 24 20 21 22 23 26 24 107 1. GKMI 17 14 ...
Some Simple Gamma Variate Generators Author(s): R. C. H. Cheng and G. M. Feast Source: Journal of the Royal Statistical Society. Series C (Applied Statistics), Vol. 28, No. 3 (1979), pp. 290-295 Published by: Wiley for the Royal Statistical Society Stable URL: http://www.jstor.org/stable/2347200 Accessed: 04-08-2017 15:27 UTC JSTOR is a not-for-profit service that helps scholars, researchers, and students discover, use, and build upon a wide range of content in a trusted digital archive. We use information technology and tools to increase productivity and facilitate new forms of scholarship. For more information about JSTOR, please contact [email protected]. Your use of the JSTOR archive indicates your acceptance of the Terms & Conditions of Use, available at http://about.jstor.org/terms

Royal Statistical Society, Wiley are collaborating with JSTOR to digitize, preserve and extend access to Journal of the Royal Statistical Society. Series C (Applied Statistics)

This content downloaded from 193.194.76.5 on Fri, 04 Aug 2017 15:27:33 UTC All use subject to http://about.jstor.org/terms

Appi. Statist. (1979), 28, No. 3, pp. 290-295

Some Simple Gamma Variate Generators By R. C. H. CHENG and G. M. FEAST University of Wales Institute of Science and Technology, Cardiff, U.K.

[Received August 1978. Revised December 1978] SUMMARY Gamma variates with index a> 1 are produced by combining two adaptations of Kinderman and Monahan's technique for generating random variates by the use of the ratio of uniform variates. Expensive logarithmic/exponential evaluation is

frequently avoided. The method is uniformly fast for all cc> 1, is compact and ea to program.

Keywords: GAMMA DISTRIBUrION; RANDOM NUMBERS; REJECTION METHOD; SIMULATION 1. INTRODUCTION

KINDERMAN AND MONAHAN (1977) describe an interesting method for generating random variates which uses the ratio of uniform variates, and they apply it to various different distributions, but not to the gamma distribution. We give two particular versions of the meth for generating gamma variates with density function f(x) = x e/-l P(cx). The two are complementary in that they are efficient over different ranges of ax but can readily be co to give a composite algorithm that is consistently fast for all a> 1. In making comparisons we shall only consider those algorithms which are compact and easy to implement in machine independent languages like Fortran. Compact methods usually rely on a rejection technique whereby test variates are subject to trials for acceptance or rejection. Such techniques are well known (see, for example, Tadikamalla, 1978), and we do

not give details here except to note that their speed depends on the average number of trials needed for each variate accepted, as well as on the amount of computation required per trial. The number of trials needed will not vary with machine, but the speed of calculation will vary; not only with machine, but also with compiler and its "optimization level". The most time-consuming calculations are for the generation of random numbers and logarithmic/ exponential evaluations, but on some machines these operations are relatively fast. In the timings to be given later we try to give a reasonably complete picture by timing on two machines with rather different characteristics. The effort required to set up constants should also be borne in mind. These "constants" actually depend on ax, and if ax has to be varied frequently, then constant set-up time has to be taken into account in assessing the speed of variate generation. Of previously published algorithms no single one is uniformly best for all cx> 1. Roughly speaking they fall into two types. Firstly, there are those which are fast for small cx, but which slow as ax increases. Of these the most competitive are those of Atkinson (1977) and Tadikamalla (1978). Fishman's GF routine, described by Atkinson and Pearce (1976), is fastest if ax is very close to 1, but is outperformed even for values of ca as low as 1-5. Then are those methods whose efficiency increases with a. These are preferable for large cx; the most competitive seem to be the GO method of Ahrens and Dieter (1974) and the GB method of Cheng (1977). The method XG of Best (1978) is not considered in detail as it is competitive only in the special case for which ax varies with each call, when it is slightly faster than GB. When ax is fixed, it is outperformed by GB. All the above methods except XG are limited in speed by requiring at least two logarithmic/exponential evaluations per trial.

This content downloaded from 193.194.76.5 on Fri, 04 Aug 2017 15:27:33 UTC All use subject to http://about.jstor.org/terms

SOME SIMPLE GAMMA VARIATE GENERATORS 291

In terms of compactness all are very easy to program, except possibly GO which is a slightly longer algorithm, especially if the normal variate generator that it requires has also to be programmed.

As for constant set-up, Tadikamalla's method requires next to none; GB requires three constants including a square root; GO requires seven with two square roots; Atkinson's method is rather expensive with constants that include the equivalent of four log evaluations and a square root.

In Section 2 we give two particular versions of Kinderman and Monahan's general technique, which we call GKMI and GKM2. Algorithm GKMI is of the first type described above, as it is fast for 1 < ax< 3 but slows as a increases. Algorithm GKM2 is of the second type; it is somewhat slower for a near 1 but rapidly speeds up as a increases. In both versions, logarithm evaluations are avoided in many trials. For speed and compactness, they compare well with other methods of their respective types. More importantly, they combine easily to give a composite algorithm, called GKM3, which remains compact whilst covering all a> 1. 2. THE ALGORITHMS

Kinderman and Monahan suggested the following method for generating variates from some prescribed distribution. Let h(x) be a non-negative function with finite integral K, and

let C = {(u, v): 0 < u (hi(v/u)}. Then if (U, V) is uniformly distributed over C, X = V/U has density K-1 h(x).

To generate gamma variates with densityf(x) - xo1 e--/F(o), x > 0, we set h(x) = xa-I e-x. Elementary considerations show that C lies inside the rectangle

R = {(u, v): 0 u k, kl 0< v< k}, (1) where k1 = {(o- 1)/e}"a-l)/2, k2 = {(al+ l)/e}(x+l)/2. An obvious way to obtain (U, V) uniformly distributed over C is, thus, to generate (U, V) uniformly distributed over either R, or some convenient rectangle enclosing R, and then test to see if (U, V) lies in C. A fairly compact implementation of this procedure is as follows. (1) Set W = bU1/U2 where U1, U2 are U(O, 1) variates. (2) If clog U2-log W+ W-1 < 0, accept X = (xs- I) W; otherwise repeat from (1). Here c = 2/(a - 1), and the algorithm is valid for any b > k_ k2/{kl(x - 1)}, being most efficient when equality holds. In fact, at the expense of only a marginal loss of efficiency for values of

a near 1, we can avoid evaluation of k. Elementary calculation shows that k2jkl satisfies the inequality k2/k < a - (6a)-1, and this yields an approximation

b = {o - (6")-1}/(oc- 1) (2) that is adequate for practical purposes. Turning now to the log evaluations of the test at step (2), we note that these can frequently be avoided by a pre-test. An acceptance pre-test can be obtained by applying the inequality,

log z < z- 1, to both logarithms (with z = U2 and W-1 respectively). This shows that if cU2- c- 2 + W+ W-1 < 0, then the test at step 2 must result in acceptance. Inclusion of these modifications yields the first of our suggested algorithms which for convenience is called GKMI.

Algorithm GKMI

Constants: Set a = ao-1, b = {oa-(6ac)1}/a, c = 2/a, d = c + 2. 1. Generate independent U(O, 1) variates U1 and U2.

2. Let W = bUJ/U2. If cU2- d+ W+ W-1 (0 go to step 4. 3. If clogU2-log W+ W-1 >0 go to step 1. 4. Deliver X = aW.

This content downloaded from 193.194.76.5 on Fri, 04 Aug 2017 15:27:33 UTC All use subject to http://about.jstor.org/terms

292

APPLIED

STATISTICS

It turns out that this algorithm is satisfactory only if a, is less than about 4, as the number

of trials needed per variate generated increases roughly as Vur. This is because the region C

becomes concentrated about the line U1 = U2 as a increases. More precisely, C is bounded by

the tangents UE1=U2+h+, U1=U2-h, where

h= ? (r- 1) (fPrJ)(a')'2 kjl' exp (-/PrJ2), += [(x+13 1) + 1{( +p + 1)2-4(a - 1)Ip}]/(2p), and ,--= . It follows that C lies inside any parallelogram of the form:

P = 0(ul,U2) -H_ K,Ul - U2 H+,,
    h,. An adequate choice for HA is obtained by considering t

    As (x -l, h+ ?2 4e-17/12 and h-+ 1; whilst as x-* oo, hA = 4{2/(eos)} T

    These results together with some simple numerical tabulation for intermediate a values show that we can take the crude (but for practical purposes adequate) approximations

    H+ 1- {21(ea)}, H_- l/Va. We can now extend the competitiveness of GKMI over a wider range of ol by using P to provide a rejection pre-test: for if the point (U1, U2) lies outside P, then the test at step 3 must lead to rejection. However, a more efficient use of the result is to reverse the roles of R and P, i.e. we sample over P, and then use R for the rejection pre-test. A simple implementation of this procedure gives the second suggested algorithm. Algorithm GKM2

    Constants: a, b, c, d as in GKM1; f= a

    1. Generate independent U(O, 1) variables U1 and U. Set U2 = U +f-(1 - 186U). Repeat this step unless 0 < U2 < 1. (The constant must not be less than 1 + 1(2/e).) Steps 2, 3 and 4 are as in GKMI.

    This method is complementary to GKMI in that it is slower than GKM1 for x near 1, but rapidly becomes faster as ox increases. As GKMI and GKM2 differ only in step 1 it is easy to combine them by use of a switch. The composite algorithm is called GKM3. Algorithm GKM3

    This is exactly the same as GKM2 except that step 1 is replaced by: 1'. Use step 1 of GKMI or GKM2 according as as is less than or greater than a prescribed

    value ao. The suggested value for oa. is 2 5; this ensures that the speed of variate generation is sub-

    stantially the same for all u > 1. The above algorithms are not the only versions possible. There are faster ones which use more efficient regions than R or P for the initial sampling; but this is at the expense of greatly increased effort in the setting up of constants. The suggested versions do not sacrifice much by way of speed, but maintain compactness and ease of constant set-up.

    This content downloaded from 193.194.76.5 on Fri, 04 Aug 2017 15:27:33 UTC All use subject to http://about.jstor.org/terms

    SOME SIMPLE GAMMA VARIATE GENERATORS 293

    3. TIMINGS

    Comparative timings were made on a CDC 7600 (Table 1) and on an ICL 470 (Table 2), and were based on a DO loop generating 10 000 and 1000 variates respectively by function call. The times do not include the times needed to set up constants; these are shown separately. For small as the results are shown in more detail in Figures 1 and 2. TABLE 1

    Time (in usec) to generate one gamma variate on a CDC 7600 (with FTN 4.6 compiler, optimization level 1) Initial CZ 1.001 1.5 2 3 4 5 10 20 50 set up

    Atkinson 20 21 21 22 23 24 26 30 39 24

    Tadikamalla 20 24 20 21 22 23 26 24 107 1 GKMI 17 14 15 17 19 21 29 41 66 5

    GO

    -

    -

    -

    22

    2121

    19

    17

    15

    9

    GB 22 19 18 18 17 17 17 17 16 6 GKM2 21 15 16 15 15 15 15 16 16 8

    GKM3 18 15 15 16 16 16 16 17 17 8

    TABLE 2

    Time (in lusec) to generate one gamm (with FTRANI compiler)

    1.001 1.5 2 3 4 5 10 20 50 Initial set up

    Atkinson 820 860 890 930 960 980 1100 Not timed 900 Tadikamalla 780 950 880 980 1080 1180 1670 Not timed 58 GKM1 800 660 690 800 920 1000 1290 Not timed 100 GO - - - 1180 1120 1110 1020 920 850 300 GB 1020 910 870 850 850 820 810 790 800 180 GKM2 1190 810 820 780 770 770 760 770 780 230 GKM3 820 710 740 790 780 820 780 820 780 250

    The version of GO used Marsaglia and Bray's convenient method for generating normal variates. (We note in passing that this algorithm, as given by Marsaglia and Bray (1964) or by Atkinson and Pearce (1976), seems to contain an error in step 4 which samples from the

    tails. In the original notation of the references, acceptance should be conditional on I I or I TI > 3, and not as shown.) The version of Tadikamalla's method used a faster test at step 4

    than that given. In the implemented version, it is not the two sides of the inequality, but their logarithms that are compared. This gives a saving equivalent to a log evaluation. Tables 1 and 2 show that in the first group of algorithms (which slow as a increases) there is little to choose between Atkinson's and Tadikamalla's methods in the range 1 < CZ < 4 where they are competitive with other algorithms, except that the latter is rather more susceptible to a slow random number generator than the former. Also, the tabulated values slightly favour Tadikamalla's method, as it is somewhat faster for integer values.

    This content downloaded from 193.194.76.5 on Fri, 04 Aug 2017 15:27:33 UTC All use subject to http://about.jstor.org/terms

    294

    APPLIED

    STATISTICS

    In comparison, GKMI compares well in the range 1 < a < 4, it is faster than either alternative except on the 470 for of values very close to I (not shown in the tables), when it is marginally slower than Tadikamalla's method. If constant set-up is taken into account, then Atkinson's method is rather expensive compared with the other two methods. For a > 2, GKM2 is faster than GB, though for a > 10 there is little to choose between them. On the CDC 7600, with its fast intrinsic random number generator RAN F, GO is ultimately the fastest routine. However, the sensitivity of GO to the normal generator is shown by the 470 timings; here random numbers are generated rather slowly, so that Marsaglia and Bray's normal generator, which requires about 3 9 random numbers per variate generated, and hence GO are rather slow. If we compare constant set-up time: GB is fastest followed by GKM2 and GO. Though GKM2 is fairly competitive compared with GB or GO, its main advantage is that it can be combined easily with GKM I. Consider the composite algorithm GKM3. The switch ax-value, cx0, was set to 2 1 on the 7600 and to 2-6 on the 470. As can be seen, GKM3 is only marginally slower than the faster of GKMI or GKM2 for all o> 1. If a general algorithm covering all o> 1 is required, GKM3 offers a performance not obtained with previous methods. To summarise: GKMI and GKM2 are fast gamma generators which between them cover all o> 1. They are easily combined into a compact algorithm, GKM3, covering all ac> 1. In terms of speed, except for ax values quite close to unity where Tadikamalla's method is slightly faster, they were the fastest methods timed, whether constant set-up time is included or not.

    TIME

    (,usec) 26i

    Atkinson

    Tadikamalla

    24-

    _

    22-

    GO

    \ ~ ~~~~~~~~~~~~~~~~~~~~ --_ - --GB

    16-

    \

    _

    GKM3

    14-

    2

    4

    6

    8

    10

    INDEXca

    FIG. 1. Time (in ,tsec) to generate one gamma variate on a CDC 7600 (with FTN 4.6 compiler, optimization level 1).

    This content downloaded from 193.194.76.5 on Fri, 04 Aug 2017 15:27:33 UTC All use subject to http://about.jstor.org/terms

    SOME SIMPLE GAMMA VARIATE GENERATORS 295

    TIME

    (jusec) 1500-

    1,, Tadikamalla

    1400-

    ,

    1300-,"

    1200-

    11

    -

    =

    AtAk

    inson GO

    1000-V,'G

    900- f

    800- ^~~ ~~~~~~~~ ~~~~~~~~~~~GB

    GKM3

    700-

    2

    4

    6

    8

    10

    INDEXo

    Fio. 2. Time (in yAsec) to generate one gamma variate on an ICL 470 (with FTRAN1 compiler).

    REFERENCES AHRENS, J. H. and DIETER, U. (1974). Computer methods for sampling from gamma, beta, Poisson and binomial distributions. Computing (Vienna), 12, 223-246. ATKINSON, A. C. (1977). An easily programmed algorithm for generating gamma random variables. J. R. Statist. Soc. A, 140, 232-234. ATKINSON, A. C. and PEARCE, M. C. (1976). The computer generation of beta, gamma and normal random variables (with Discussion). J. R. Statist. Soc. A, 139, 431-460. BEST, D. J. (1978). Letter to the Editor. Appl. Statist., 27, 181. CHENG, R. C. H. (1977). The generation of gamma variables with non-integral shape parameters. Appl. Statist., 26, 71-75. KINDERMAN, A. J. and MONAHAN, J. F. (1977). Computer generation of random variables using the ratio of uniform deviates. Ass. Comput. Mech. Monograph Series, Trans. Math. Soft., 3, 257-260. MARSAGLIA, G. and BRAY, T. A. (1964). A convenient method for generating normal variables. SIAM Rev., 6, 260-264. TADIKAMALLA, P. R. (1978). Computer generation of gamma random variables. Commun. Ass. Comput. Mach., 21, 419-422.

    This content downloaded from 193.194.76.5 on Fri, 04 Aug 2017 15:27:33 UTC All use subject to http://about.jstor.org/terms