Sie sind auf Seite 1von 6

if nl = 0 then stop;

while ~r(] + 1 ) = 1 do
(i ~--j + 1;
j (--j + nl;
nl ~ cr(i, j)) ;
stop;

Iteration

M a n a g e m e n t Science/
Operations Research

Example. L e t /3 be a string, l(/3) = 2761. L e t us


apply the algorithm to 0. = R(2761)/3000. Writing 0.
in m o r e detail, 0. = 1 0 0 1 1 0 0 1 0 1 0 1 1 0 0 1 0 0 1 0 / 3 0 0 0 .
W e see that
i <--- 1; j ~-- 3;

nl ~ 4;

T h e n 0-(4) = 1; thus
i <-- 4;

j(-7;

nl ~-- 12;

T h e n 0-(8) = 1; thus
i~8;

j~19;

D. F. Shanno
Editor

Generating Beta
Variates with
Nonintegral Shape
Parameters
R. C. H. Cheng
University of Wales Institute of Science and
Technology

n1#--2761;

Since 0-(20) = 0, the algorithm stops. T h e algorithm


has c o m p u t e d n l. Using it we easily identify /3 as

/3 = a(j + 2, j + nl + 1).
Notice that the algorithm stops on the last c h a r a c t e r of

R(n) without checking the next letter. T h e r e a s o n for


this is that B(n) is a r e p r e s e n t a t i o n of n whose most
significant bit is 1. T h e r e f o r e the first time we have
a(j + 1) = 0, this 0 is the one put explicitly in the
definition of R(n) ( = R ' ( n ) 0 ) .
Next we show that as the length of the strings
increases, the additional length which the R(n) introduce b e c o m e s relatively negligible.
THEOREM. If m is a fixed positive integer, then

A new rejection method is described for generating


beta variates. The method is compared with previously
published methods both theoretically and through
computer timings. It is suggested that the method has
advantages in both speed and programming simplicity
over previous methods, especially for "difficult"
combinations of parameter values.
Key Words and Phrases: beta variates, random
numbers, simulation
CR Categories: 5.5, 8.1

lim l(0-( al, o~ . . . . .


olin)) -- i as ~ ni "-~ oo.
l(at ~ otto)
i=1
PROOF.

3 + ~ ni + ~ l(R(nO)
1(0"(01~1) 0/)2, . . . ,

0~))

i=1

i=1

i=l

i=1

<-1+

3+2

LG(nO + 4m
i=1

n~
i=1

by Corollary 2. Thus the limit is 1.


[]
Notice that the c o n v e r g e n c e to 1 is rapid. F o r
e x a m p l e , for strings of length 21 bits, the o v e r h e a d is
less than 2 percent.
Received August 1975; revised April 1977.

References
1. Bentley, J. L., and Yao, A.C. An almost optical algorithm for
unbounded searching. Inform. Processing Letters 5 (1976), 82-87.
2. Elias, P. Universal codeword sets and representations of the
integers. IEEE Trans. Inform. Theory 2 1 , 2 (March 1975), 194203.
3. Levenshtein, V.E. On the redundancy and delay of separable
codes for the natural numbers. Problems of Cybernetics 20 (1968),
173-179.
317

General permission to make fair use in teaching or research of


all or part of this material is granted to individual readers and to
nonprofit libraries acting for them provided that A C M ' s copyright
notice is given and that reference is made to the publication, to its
date of issue, and to the fact that reprinting privileges were granted
by permission of the Association for Computing Machinery. To
otherwise reprint a figure, table, other substantial excerpt, or the
entire work requires specific permission as does republication, or
systematic or multiple reproduction.
Author's address: Department of Mathematics, UWlST, King
Edward VII Ave., Cardiff CFI3NV, United Kingdom.
1978 ACM 0001-0782/78/0400-0317 $00.75
Communications
of
the A C M

April 1978
Volume 21
Number 4

1. Introduction
A new method is described below for generating
beta variates with density function

f(x) = x"-l(1 - x)b-a/B(a, b), 0 < x < 1,


where a, b > 0 are shape parameters which may be
nonintegral. For convenience such a variate will be
called a Bx(a, b) variate.
In making comparisons with previously published
methods, attention will be' confined to those of similar
type, that is, methods which are valid for a wide range
of a, b values, which are reasonably easy and short to
program in a machine-independent language like FORTRAN, and which do not require lengthy initialization
procedures such as the setting up of special tables.
Ahrens and Dieter [1] and Atkinson and Pearce [2]
have reviewed and tested a number of beta variate
generators, and, of those that are of the above type,
the best would appear to be the method of J6hnk [4],
the BN method of Ahrens and Dieter [1], the switch
methods of Atkinson and Whittaker [3], and possibly
the well-known gamma variate transformation method.
As details of the methods can be found in the above
references, only a brief critique of their general features is given here.
All the above generators except the last are based
on rejection methods in which candidate variates are
subjected to trials leading to their rejection or acceptance. As we shall primarily be considering rejection
methods, it is worth pointing out that the two main
factors determining their speed are (i) the expected
number of trials required for each variate generated
(i.e. accepted), and (ii) the complexity of calculations
required per trial, this latter factor being largely determined by the number of logarithmic or exponential
function evaluations needed and the number of uniform random numbers required. It is also worth bearing in mind the number and complexity of initial
constants that have to be set. This factor is unimportant
and can be neglected if a stream of variates with fixed
a and b values is needed, as constants can then be set
just once and saved for subsequent calls. If, however,
a and b vary with each call, then calculation of constants would have to be included in variate generation
time.
J6hnk's method is valid in principle for any a, b >
0. Two exponentiations of type yX and two random
numbers are required per trial. The expected number
of trials per variate accepted is F(a + b + 1)/{F(a +
1)F(b + 1)}, which increases without bound as a or b
increases. In practice the method is slow if either a or
b is much greater than one.
Two rejection methods incorporating a 'switch' are
described by Atkinson and Whittaker [3] for the case
where at least one parameter is less than one. The first
covers the case where both a, b < 1. Each trial
requires the generation of two random numbers and
318

the equivalent of four log/exponential evaluations. The


method complements J6hnk's method in that fewer
trials are required when a + b > 1 and more when a +
b < 1. The method needs the setting of two constants,
including the evaluation of two square roots. The
second switch method covers the case where one
parameter is greater than, and the other is less than,
one. It is computationally very similar to the first, the
main difference being that the preset constants (using
the formula corresponding to Atkinson and Whittaker's eq. (11)) require one exponentiation of form yX.
The BN method of Ahrens and Dieter is valid for
a, b > 1. The generation of a uniform and a normal
variate together with three log evaluations are the
most time-consuming calculations required per trial.
The expected number of trials needed per variate
generated is
{~r/(2c)}(a - 1)a-l(b - 1)b-lF(a + b)/{cCF(a)F(b)},
where c = a + b - 2. This quantity increases without
bound if either (i) a and b both tend to one, or (ii) a
or b is held fixed and the other tends to infinity. In
practice the method is therefore slow if either of these
cases obtains. Six constants are required by BN, including one log evaluation and one square root.
The gamma variate transformation method is valid
for any a, b > 0. A pair of independent gamma
variates, Xx and X2, with parameters a and b is first
generated. The ratio XI/(Xx + )(2) is then a Bl(a, b)
variate. This method has been examined by Ahrens
and Dieter [1], Whittaker [7], and Atkinson and
Pearce [2] and appears to date to be the best method
for the case in which both parameters are greater than
one but are very different in value. As a general
method, it is slow, as gamma variates are not really
much easier to generate than beta variates.
It should perhaps be mentioned that special methods exist for the case that a and b are integers. Because
of this restriction, they are not considered further
except to note that, though fast when a and b are
small, such methods tend to slow rapidly as a and b
increase.
The method described below is also of the rejection
type and covers all a, b > 0. Four log/exponential
function evaluations and two random numbers are
required per trial. The main feature of the method,
however, is that the expected number of trials needed
per variate generated is bounded above; it is thus
possible to render the speed of variate generation
largely insensitive to the particular values of a and b
chosen. Three constants are needed, including a square
root if a, b > 1. In practice the method compares well
in speed and programming simplicity with the above
methods for most a and b combinations.
In the next section the rationale of the method is
described together with the basic computational algorithm. In Section 3 two modifications are described
which offer increased speed. Timings and comparisons
are given in the final section.
Communications
of
the ACM

April 1978
Volume 21
Number 4

2. D e s c r i p t i o n o f Basic M e t h o d

The m e t h o d operates by generating beta variates


of the second kind, i.e. variates with density
f(x) = xa-1/B(a, b)(1 + x) a+b, x > 0.

(2.1)

Such a variate will be called a Bz(a, b) variate. If Y is


such a variate, then it is well known that X = Y / ( 1 +
Y) is a Bl(a, b) variate.
The following adaptation of the well-known envelope rejection method is used (see, for example, Wallace [6] or Atkinson and Pearce [2]). Let f and g be
two density functions defined for x > 0 and let G be
the distribution function corresponding to the latter
density. Let
a = maXx>o{f/g}
be finite. T a k e a pair of independent uniform r a n d o m
numbers, U1 and U2, and set
X = G-I(UI).

(2.2)

Then, if
R(X) -- f ( X ) / A g ( X )

>- U2,

(2.3)

accept (deliver) X, otherwise reject it and repeat with


another pair of r a n d o m numbers. Each accepted X has
density f(x). The expected n u m b e r of trials per X
accepted is A .
To obtain B2(a, b) variates, setf(x) as in (2.1) and
let
G(x) = xX/(tz + xX), x > O,
so that
g(x) = h lax h--l(

Algorithm B A (a, b > O)


Initialization: Set ct = a + b. If min (a, b) -< 1, set fl =
max (a-1, b-a) ; otherwise set fl = X/{(a - 2)/(2ab a)}. S e t y = a + /3-1 .
1. G e n e r a t e r a n d o m numbers U1, /.]2 and set V =
fl log {U1/(1 - U1)}, W = ae v.
2. If a log { a / ( b + W)} + y V - 1.3862944 <
log (U~U~), go to 1. (The constant is log 4.)
3. Deliver X = W / ( b + W).
If a and b are to be kept fixed, then the constants
calculated in the initialization step can be saved between calls.
Variate generation time is dependent on A , the
expected n u m b e r of trials required per variate generated. Selected values of A are given in Table I. It is
worth distinguishing between the case min (a, b) > 1,
when A never rises above A = 4/e -~ 1.47 (corresponding to a or b = 1, the other tending to infinity),
and the case rain (a, b) -< 1, when A can rise to a
m a x i m u m of 4 (corresponding to a or b fixed, the
other tending to zero). In this latter situation, when
rejection rate is high, it is well worthwhile modifying
the basic algorithm to improve performance. This is
discussed in the next section.

3. Modifications to Basic A l g o r i t h m

At the expense of mathematical elegance, it is


possible to modify algorithm BA in order to significantly increase variate generation speed. The cases
rain (a, b) > 1 and min (a, b) -< i are considered
separately.

],~ ..[_ X ,k) --2

Here G is chosen to facilitate calculation of X in (2.2) ;


/z and X are parameters which may be adjusted to give
a small A value c o m m e n s u r a t e with ease in evaluating
(2.3). Suggested values of these parameters are /z =
(a/b) ~ and
f m i n (a, b)
X = ~X/{[2ab - (a + b)]
[
/(a + b - 2)}

if min (a, b) - 1,
(2.4)
if min (a, b) > 1.

To find A for this choice of /z and h, it is easiest to


take logarithms. The derivative of log (f/g) has a root
atx = a / b . E l e m e n t a r y , but tedious, calculations show
that this root corresponds to the absolute m a x i m u m of
log (f/g) and hence o f f / g also for any a, b > 0. The
maximum value o f f / g is

3 . 1 Min (a, b) > 1

Looking at algorithm BA, one sees that the computation time per trial is largely taken up by the four
log/exponential evaluations of steps 1 and 2. When
the rejection rate is low, which is the case when min
(a, b) > 1, the evaluations at step 1 cannot very well
be avoided. However, those at step 2 can often be
avoided by use of a simple preliminary test.
We use the result that, as log z is a concave
function of z, a tangent always lies above its graph.
Thus Oz - log 0 - 1 -> log z for all z > 0 and any fixed
0 > 0. This result can be applied at step 2 of algorithm
B A with z -- b + W. We then have that, if the
preliminary test
a log a - log 4 + y V - a{O(b + W)

A = 4aabb/hB(a, b)(a + b) "+~,

(3.1.1)

- log 0 - 1} -> log (U~/-/2)

with ?, as given in (2.4).


With A found, the computational algorithm can be
written out explicitly. The best layout will depend on
the computer used, but the following reduces expensive
log evaluations to a minimum. For convenience the
algorithm is called:

is satisfied, then the accurate step 2 test of B A is


bound to lead to acceptance and so need not be carried
out. Here 0 can be chosen to maximize the conditional
probability of (3.1.1)'s being satisfied given that the
trial leads to acceptance. After some calculation, it
turns out that the best choice is to let 0 = a -1 when

319

Communications
of
the ACM

April 1978
Volume 21
Number 4

Table I. Values of A . the Expected N u m b e r of Trials per Beta


Variate G e n e r a t e d by Algorithm B A .

R(U,) <_ k,(1

0.0+

0.0+
0.1
0.5
1.0
2.0
5.0
10.0
oc

2.00
4.00
4.00
4.00
4.00
4.00
4.00
4.00

0.1
1.77
2.70
2.86
2.94
2.99
3.01
3.02

0.5

1.0

2.0

5.0

1.00
1.19
1.34
1.40
1.47

1.06
1.13
1.18
1.25

2/./1) -2,

1_ [
kl >- 4 \

/ L

1.11
1.15

~b(~:) = 2a~: + (a + b)~:b - (a - b) = 0.


1.13

b combinations but is never less than e / 4 if we impose


the restriction a -< b. This can be done without loss of
generality by first setting a and b equal to the smaller
and larger, respectively, of the two original p a r a m e t e r s
a0 and b0 say. If an interchange does occur, then we
n e e d only deliver X = b/(b + W) instead of X = W/(b
+ W) to obtain a B~(ao, bo) variate with p a r a m e t e r s the
right way round.
Evaluation of log (U~U2) in (3.1.1) can also be
avoided in like m a n n e r . It turns out that the precise
value of 0 is not critical in this case, and a simple
choice is to set O = 5, irrespective of the values of a
and b. The modified algorithm, which is called BB for
convenience, is as follows.

Algorithm BB (min(ao, b0) > 1)


Initialization: Set a = min (a0, b0) ; b = max (a0, bo) ; o~
= a + b ; fl = ~ / [ ( a - 2)/(2ab - a ) ] ; y = a + fl-1.
1. G e n e r a t e r a n d o m n u m b e r s /-]1, U2 and set V =
fl log{U1/(1 - U1)}, W = ae v, Z = U]U2, g = TV
- 1.3862944, S = a + R - W. (The constant is log

4.)
2. If S + 2.609438 -> 5Z, go to 5. (The constant is 1
+ log 5.)
3. Set T = l o g Z . I f S > _ T, g o t o 5 .
4. I f R + a log{ct/(b + IV)} < T, go to 1.
5. I f a = a0 d e l i v e r X = W/(b + W) ; otherwise deliver

X = b/(b + W).
(a, b) -

LEMMA 2. I f Ua >-- and min (a, b) = b <- 1, then


Ui-2 -< "4R(/./1) - k2/-/1 -s.

provided
k2 -> {(a + b)/a} a+b.

320

i.

(3.2.4)

The first l e m m a allows a pretest for rejection when


U~ < ; for if Us > k~(1 - 2 U 0 -2 then (3.2.1) implies
Us > R(UO and so step 2 of B A leads to rejection.
The second l e m m a is applied in a similar way to give
pretests of both rejection and acceptance when U~ -

.
The only p r o b l e m is to find suitable values for kl
and k2. T h e best choice is kx = go and ks = {(a +
b)/a} a+b (with Ko calculated, say, by solving (3.2.3) for
~ and then using ~: to find Ko). U n f o r t u n a t e l y the
evaluation of b o t h of these values is time c o n s u m i n g ,
making it only practical if the p a r a m e t e r s a and b are
fixed so that kl and k2 only have to be evaluated once.
A simpler alternative is to use a p p r o x i m a t e values for
kl and kz satisfying (3.2.2) and (3.2.4). Suggested
easily calculated values are
14),

/~2 = 1 + ( 2 + 8 - 1 ) b ,

This case is treated in a similar way to the previous


one, t h o u g h the details are a little m o r e complicated.
The rejection rate can be high w h e n min (a, b) -<
1; so it is worthwhile avoiding the log and exponential
function evaluations by m e a n s of pre-tests, for b o t h
steps 1 and 2 of algorithm B A , for those trials which
end up in rejection. It will be supposed that a and b
are set equal to the larger and smaller, respectively, of
the original p a r a m e t e r s , with a suitable a d j u s t m e n t at
final variate delivery if transposition occurs. This is the
reverse of the previous case. It is possible to take a -<
b as b e f o r e ; h o w e v e r , a s y m m e t r i e s in the following
lemmas then lead to a slightly less c o m p a c t final
algorithm. W e use the following two results.
LEMMA

(3.2.3)

This result is o b t a i n e d by considering H =


(1 - 2UI)2R(U1). Setting its derivative equal to zero,
we find after s o m e calculation that the m a x i m u m of H ,
u n d e r the conditions of the l e m m a , is o b t a i n e d for/./1
= ~:b/( 1 + ~b), w h e r e ~ is the r o o t of (3.2.3). This
value o f U1 yields Ko of (3.2.2) as being the m a x i m u m
of H .
In a similar way we also have:

k~ = 8 ( 1 + 3 b ) / ( 1 8 a / b Min

b(1 + 0)] a+b


a(1
-0)J

where 0 = ~b, and ~ is the unique root o f


1.10
1.12
1.17

the a b o v e conditional probability is eabb(a + b)-a+bW x


(a + b)/F(b). This quantity can be small for certain a,

3.2

(3.2.1)

provided

10.0

(Upper half same


as lower half.)
1.27
1.54
1.72
1.84
1.89
1.94

hand side o f (2.3)), treated as a function o f U1, satisfies

If Ux < and a >- b, then R (the left-

where 8 = 1 + a - b. It seems difficult to verify


(3.2.2) and (3.2.4) for these values by using analytic
a r g u m e n t s alone; h o w e v e r , the results are readily established with the aid of some numerical calculations.
Consider k~. A fairly detailed numerical tabulation
shows that k~ is a relatively crude a p p r o x i m a t i o n except
near the p o i n t s a = b = 1 and a = b = 0, and possibly
as a ---> ~ or as b ---> 0 w h e n numerical calculations are
inconclusive. H o w e v e r , all these cases can be c o v e r e d
by e l e m e n t a r y analytic arguments. F o r example, in the
n e i g h b o r h o o d of a = b = 1 we can write a = 1 - ~ +
"0, b = 1 - ~ with E, '0 >-- 0 and show that, to first
order in E and '0, ka = 1 - 0.75~ - 3.5"0, whilst to the
same o r d e r Ko = 1 - E log 4 - '000 - log "0) where ~b
d e p e n d s on E and "0 but is b o u n d e d by IqJ] -< log 2. It
Communications
of
the A C M

April 1978
Volume 21
Number 4

follows that for e, "0 sufficiently small kl -> Ke with


equality at a = b = 1. Similar elementary considerations can be used to show that kl - Ko in the other
cases.
A similar verification can be made that k2 >-- {(a +
b)/a} ab with equality only at a = b = 1 and along b =
0.
Using the above approximate values for k~ and kz,
we can write the modified algorithm:

Table II. Time (in ~sec) to Generate One Beta Variate on a CDC
7600.
a

b
1.001

A l g o r i t h m B C (rnin(ao, bo) <- 1)

Initialization: Set a = max (a0, b0); b = min (a0, b0); a


=

b;

/3

b-~;

b;

KI

8(0.0138889+0.0416667b)/(a/3-0.777778); Kz = 0.25
+ (0.5+0.258-~)b. (Here K1 = k l / 4 , K2 = k2/4.)
1. Generate random numbers Ui, Uz, and if U1 --- go
to 3.
2. S e t Y = U~ U2, Z = UI Y. If O.25 U2 + Z - Y >- K~,
go to 1; otherwise go to 5.
3. Set Z = U21Uz. If Z --- 0.25, set V =
/3 log { U 1 / ( 1 - U 1 ) } , W = ae v, and go to 6.
4. If Z_> K2 go to 1.
5. Set V = /3 log { U I / ( 1 - U 1 ) } , W = ae v. If
a{log [ a / ( b + W ) ] + V} - 1.3862944 < log Z, go
to 1.
6. If a = a0 deliver X = W / ( b + W ) ; otherwise deliver
X = b/(b+W).

We stress that in algorithm BC we have not looked


for elegance but have concentrated on efficiency of
variate generation. The program length, though longer
than B A or BB, still compares quite well with most of
the published methods already mentioned.

4.

Timings

The aforementioned algorithms were timed on the


University of London C o m p u t e r Centre's C D C 7600
machine, and the results are given below. Programs
were based on a D O loop generating 10,000 variates
by function call. For simplicity the timings tabulated
are for the case in which constants required by each
routine are present and saved between calls. The times
required to set constants are shown separately, and
these times have to be added to variate generation
time if constants are to be reset with each call.
For the BN algorithm, normal variates were generated by using Marsaglia's improved version of the
Box-Miiller method (described in Marsaglia and Bray
[5], for example, and referred to by Atkinson and
Pearce [2] as the polar method of Marsaglia). Normal
variates were thus generated in pairs on alternate calls,
with one being saved and used at the next call. This
method of normal variate generation was chosen as
being the best compromise between speed and programming simplicity and is in accordance with the
recommendations of Atkinson and Pearce. In fact,
though we do not go into details here, in terms of
321

10

20

1.001

10

20

BA 23.1
BB 20.0
BN 257.5
BA
BB
BN

26.5
22.9
48.4

24.2
21.1
37.9

BA
BB
BN

29.3
25.4
58.6

25.5
21.4
38.4

25.0
21.7
35.4

BA
BB
BN
BA
BB
BN

31.0
26.5
74.4
31.5
27.2
98.0

26.6
21.9
42.9
27.3
22.4
50.2

25.3
21.3
36.5
25.8
21.1
40.2

25.2
22.0
34.5
25.6
21.4
35.7

25.5
22.0
34.0

BA

BB

BN

5.6

5.6

9.9

Initialization Times (txsec)

speed alone the m e t h o d appears quite competitive on


the C D C 7600, at least with the compiler used, viz.
FTN 4.2.
Table II covers the case min (a, b) > 1. J6hnk's
method is not included as it was uniformly (and appreciably) slower than the other methods in this case. As
will be seen, BB was uniformly faster than B A , which
was uniformly faster than BN. To be fair to BN, it
should be stressed that its speed could be improved by
using more sophisticated normal variate generation
techniques. It is estimated that with a fast assembler
normal varJate generator the BN times would be reduced to the point that they would be at least as fast as
B A or BB when a and b are roughly equal and large.
H o w e v e r , irrespective of the normal generator used,
there are two significant advantages that B A and BB
have over BN. First, the performance of B A and BB
is relatively constant over all allowable combinations
of a and b whereas the performance of BN deteriorates
markedly when either a and b both approach one or
when they b e c o m e very different in value. Second, for
the case in which a and b may be varied so that
constants have to be recalculated with each call, B A
and BB take only an extra 5 . 6 / , s e c per variate whereas
BN takes an extra 9.9 /,sec. In terms of program
length, both B A and BB were less than half that of
BN.
Table III covers the case min (a, b) ~< 1. Depending on the values of a and b and on whether they vary
with each variate call, no single method dominates all
other methods. Consider first the case that a and b are
fixed. In the r e g i o n a , b ~< 1, J6hnk's m e t h o d is best if
a + b < 1, whilst if a + b > 1 then BC or the first
switch method is best, with the switch m e t h o d slightly
faster if either p a r a m e t e r is near one and with BC
slightly faster otherwise. In the region where one of
Communications
of
the ACM

April 1978
Volume 21
Number 4

Table III. Time (in/xsec) to Generate One Beta Variate on a CDC


7600.
a

b
0.1

0.3

0.5

0.7

0.9

1.001

2.0

5.0

20.0

BA
BC
J6hnk
Switch
BA
BC
J6hnk
Switch
BA
BC
J6hnk
Switch
BA
BC
J6hnk
Switch
BA
BC
J6hnk
Switch
BA
BC
J6hnk
Switch
BA
BC
J6hnk
Switch
BA
BC
J6hnk
Switch
BA
BC
J6hnk
Switch

.1

.3

.5

.7

.9

37.7
28.0
21.6
40.2
51.8
27.6
22.1
34.3
54.5
27.5
22.5
30.4
57.6
27.4
22.8
27.4
58.1
26.8
23.0
25.5
58.4
27.3
23.2
23.7
60.2
26.7
24.3
24.2
60.2
26.3
25.8
24.6
61.2
26.0
29.0
24.8

31.9
27.5
23.3
33.0
36.9
26.8
24.5
30.8
39.5
26.9
25.6
28.0
41.1
27.2
26.3
25.1
41.6
27.2
26.8
24.9
45.3
27.6
30.4
26.1
46.2
27.2
37.9
26.8
47.1
27.1
54.3
27.7

28.1
26.2
26.6
29.2
30.8
26.3
27.9
27.3
32.6
26.1
29.7
25.0
33.3
26.4
30.6
26.6
36.7
27.1
38.0
27.5
39.1
27.3
52.8
28.5
40.6
27.4
97.1
29.0

25.4
24.7
30.4
26.2
27.2
25.0
32.9
24.6
27.6
25.1
34.2
27.5
31.2
25.8
45.1
28.2
33.9
27.1
72.3
29.1
35.4
27.3
171.7
29.5

23.5
23.4
35.9
24.3
24.2
23.7
38.0
26.8
27.9
24.7
53.5
27.3
30.7
26.2
98.2
27.3
32.5
26.9
296.4
27.5

Initialization Time (p.sec)

BA

BC

1.1

4.8

Switch 1 Switch 2
10.3

12.1

the parameters is greater than one, the second switch


m e t h o d is slightly faster than BC if the other parameter
is small, whilst BC is slightly faster otherwise. Here
J6hnk's method slows rapidly as either parameter
increases. N o t e that for the case of a and b fixed, B A
is not competitive except possibly when one or the
other parameter is near one in value. N o w consider
the case that constants have to be reset at each call.
The switch methods are not so competitive in this case
as the constants required by them are relatively slow
to calculate. J6hnk's m e t h o d is now best in the region
given roughly by where either one of the parameters is
small or where a + b ~ 1.5 ; outside this region B A is
best if a and b are both close to one; BC is best
otherwise. In terms of program length J6hnk's method
is slightly shorter than B A , both being about half the
length of the switch methods or BC.
The gamma variate transformation method has not
been included explicitly. This was because most g a m m a
variate generators valid for nonintegral shape parameters (such as the GS and G O routines of Ahrens and
322

Dieter [1] or the method of Wallace [6]) took as long


to generate a gamma variate as BB or BC did to
generate a beta. Thus the gamma variate transformation method gave generation times approaching twice
those for BB or BC.
Summarizing, B A is a simple method for generating
Bl(a, b) variates for all a, b > 0. It is reasonably fast
for values of a and b down to about 0.5. The more
complicated versions BB and BC cover all a, b > 0
between them and offer quicker variate generation
speed. Compared with previously published methods,
B A and BB are very competitive for the case min (a,
b) > 1, especially when the parameter values may
alter with each call. For the case min (a, b) < 1,
though B A and BC are not uniformly faster than
previously published methods (for instance J6hnk's
method is best if a and/or b are small), they do have
the advantage over other methods of having a variate
generation speed which is both largely independent of
parameter value and not seriously affected if parameter
values are allowed to vary with each variate call.
Received August 1976; revised April 1977

References
1. Ahrens, J.H., and Dieter, U. Computer methods for sampling
from gamma, beta, Poisson and binomial distribution. Computing
(Vienna), 12 (1974), 223-246.
2. Atkinson, A.C., and Pearce, M.C. The computer generation of
beta, gamma and normal random variables. J. Roy. Statist. Soc. Ser.
A 139 (1976), 431-461.
3. Atkinson, A.C., and Whittaker, J. A switching algorithm for
the generation of beta random variables with at least one parameter
less than 1. J. Roy. Statist. Soc. Ser. A. 139 (1976), 462-467.
4. J6hnk, M.D. Erzeugung von Betaverteilten und
Gammaverteilten Zuffallszahlen. Metrika 8 (1964), 5-15.
5. Marsaglia, G., and Bray, T.A. A convenient method for
generating normal variables. SIAM Rev. 6 (1964), 260-264.
6. Wallace, N.D. Computer generation of gamma random variates
with non-integral shape parameters. Comm. A C M 17, 12 (Dec.
1974), 691-695.
7. Whittaker, J. Generating gamma and beta random variables
with non-integral shape parameters. Appl. Statist. 23 (1974), 210214.

Communications
of
the ACM

April 1978
Volume 21
Number 4

Das könnte Ihnen auch gefallen