GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
genmul.f
Go to the documentation of this file.
1 SUBROUTINE genmul(n,p,ncat,ix)
2C**********************************************************************
3C
4C SUBROUTINE GENMUL( N, P, NCAT, IX )
5C GENerate an observation from the MULtinomial distribution
6C
7C
8C Arguments
9C
10C
11C N --> Number of events that will be classified into one of
12C the categories 1..NCAT
13C INTEGER N
14C
15C P --> Vector of probabilities. P(i) is the probability that
16C an event will be classified into category i. Thus, P(i)
17C must be [0,1]. Only the first NCAT-1 P(i) must be defined
18C since P(NCAT) is 1.0 minus the sum of the first
19C NCAT-1 P(i).
20C REAL P(NCAT-1)
21C
22C NCAT --> Number of categories. Length of P and IX.
23C INTEGER NCAT
24C
25C IX <-- Observation from multinomial distribution. All IX(i)
26C will be nonnegative and their sum will be N.
27C INTEGER IX(NCAT)
28C
29C
30C Method
31C
32C
33C Algorithm from page 559 of
34C
35C Devroye, Luc
36C
37C Non-Uniform Random Variate Generation. Springer-Verlag,
38C New York, 1986.
39C
40C**********************************************************************
41C .. Scalar Arguments ..
42 INTEGER*4 n,ncat
43C ..
44C .. Array Arguments ..
45 REAL p(*)
46 INTEGER*4 ix(*)
47C ..
48C .. Local Scalars ..
49 REAL prob,ptot,sum
50 INTEGER*4 i,icat,ntot
51C ..
52C .. External Functions ..
53 INTEGER*4 ignbin
54 EXTERNAL ignbin
55C ..
56C .. Intrinsic Functions ..
57 INTRINSIC abs
58C ..
59C .. Executable Statements ..
60
61C Check Arguments
62 IF (n.LT.0) CALL xstopx ('N < 0 in GENMUL')
63 IF (ncat.LE.1) CALL xstopx ('NCAT <= 1 in GENMUL')
64 ptot = 0.0
65 DO 10,i = 1,ncat - 1
66 IF (p(i).LT.0.0) CALL xstopx ('Some P(i) < 0 in GENMUL')
67 IF (p(i).GT.1.0) CALL xstopx ('Some P(i) > 1 in GENMUL')
68 ptot = ptot + p(i)
69 10 CONTINUE
70 IF (ptot.GT.0.99999) CALL xstopx ('Sum of P(i) > 1 in GENMUL')
71
72C Initialize variables
73 ntot = n
74 sum = 1.0
75 DO 20,i = 1,ncat
76 ix(i) = 0
77 20 CONTINUE
78
79C Generate the observation
80 DO 30,icat = 1,ncat - 1
81 prob = p(icat)/sum
82 ix(icat) = ignbin(ntot,prob)
83 ntot = ntot - ix(icat)
84 IF (ntot.LE.0) RETURN
85 sum = sum - p(icat)
86 30 CONTINUE
87 ix(ncat) = ntot
88
89C Finished
90 RETURN
91
92 END
subroutine genmul(n, p, ncat, ix)
Definition genmul.f:2