1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
DOUBLE PRECISION FUNCTION genbet(aa,bb)
C**********************************************************************
C
C DOUBLE PRECISION FUNCTION GENBET( A, B )
C GeNerate BETa random deviate
C
C
C Function
C
C
C Returns a single random deviate from the beta distribution with
C parameters A and B. The density of the beta is
C x^(a-1) * (1-x)^(b-1) / B(a,b) for 0 < x < 1
C
C
C Arguments
C
C
C A --> First parameter of the beta distribution
C DOUBLE PRECISION A
C JJV (A > 1.0E-37)
C
C B --> Second parameter of the beta distribution
C DOUBLE PRECISION B
C JJV (B > 1.0E-37)
C
C
C Method
C
C
C R. C. H. Cheng
C Generating Beta Variates with Nonintegral Shape Parameters
C Communications of the ACM, 21:317-322 (1978)
C (Algorithms BB and BC)
C
C**********************************************************************
C .. Parameters ..
C Close to the largest number that can be exponentiated
DOUBLE PRECISION expmax
C JJV changed this - 89 was too high, and LOG(1.0E38) = 87.49823
PARAMETER (expmax=87.49823)
C Close to the largest representable single precision number
DOUBLE PRECISION infnty
PARAMETER (infnty=1.0E38)
C JJV added the parameter minlog
C Close to the smallest number of which a LOG can be taken.
DOUBLE PRECISION minlog
PARAMETER (minlog=1.0E-37)
C ..
C .. Scalar Arguments ..
DOUBLE PRECISION aa,bb
C ..
C .. Local Scalars ..
DOUBLE PRECISION a,alpha,b,beta,delta,gamma,k1,k2,olda,oldb,r,s,t,
+ u1,u2,v,w,y,z
LOGICAL qsame
C ..
C .. External Functions ..
DOUBLE PRECISION ranf
EXTERNAL ranf
C ..
C .. Intrinsic Functions ..
INTRINSIC exp,log,max,min,sqrt
C ..
C .. Save statement ..
C JJV added a,b
SAVE olda,oldb,alpha,beta,gamma,k1,k2,a,b
C ..
C .. Data statements ..
C JJV changed these to ridiculous values
DATA olda,oldb/-1.0E37,-1.0E37/
C ..
C .. Executable Statements ..
qsame = (olda.EQ.aa) .AND. (oldb.EQ.bb)
IF (qsame) GO TO 20
C JJV added small minimum for small log problem in calc of W
C in Rand.c
10 olda = aa
oldb = bb
20 IF (.NOT. (min(aa,bb).GT.1.0)) GO TO 100
C Alborithm BB
C
C Initialize
C
IF (qsame) GO TO 30
a = min(aa,bb)
b = max(aa,bb)
alpha = a + b
beta = sqrt((alpha-2.0)/ (2.0*a*b-alpha))
gamma = a + 1.0/beta
30 CONTINUE
40 u1 = ranf()
C
C Step 1
C
u2 = ranf()
v = beta*log(u1/ (1.0-u1))
C JJV altered this
IF (v.GT.expmax) GO TO 55
C JJV added checker to see if a*exp(v) will overflow
C JJV 50 _was_ w = a*exp(v); also note here a > 1.0
50 w = exp(v)
IF (w.GT.infnty/a) GO TO 55
w = a*w
GO TO 60
55 w = infnty
60 z = u1**2*u2
r = gamma*v - 1.3862944
s = a + r - w
C
C Step 2
C
IF ((s+2.609438).GE. (5.0*z)) GO TO 70
C
C Step 3
C
t = log(z)
IF (s.GT.t) GO TO 70
C
C Step 4
C
C JJV added checker to see if log(alpha/(b+w)) will
C JJV overflow. If so, we count the log as -INF, and
C JJV consequently evaluate conditional as true, i.e.
C JJV the algorithm rejects the trial and starts over
C JJV May not need this here since ALPHA > 2.0
IF (alpha/(b+w).LT.minlog) GO TO 40
IF ((r+alpha*log(alpha/ (b+w))).LT.t) GO TO 40
C
C Step 5
C
70 IF (.NOT. (aa.EQ.a)) GO TO 80
genbet = w/ (b+w)
GO TO 90
80 genbet = b/ (b+w)
90 GO TO 230
C Algorithm BC
C
C Initialize
C
100 IF (qsame) GO TO 110
a = max(aa,bb)
b = min(aa,bb)
alpha = a + b
beta = 1.0/b
delta = 1.0 + a - b
k1 = delta* (0.0138889+0.0416667*b)/ (a*beta-0.777778)
k2 = 0.25 + (0.5+0.25/delta)*b
110 CONTINUE
120 u1 = ranf()
C
C Step 1
C
u2 = ranf()
IF (u1.GE.0.5) GO TO 130
C
C Step 2
C
y = u1*u2
z = u1*y
IF ((0.25*u2+z-y).GE.k1) GO TO 120
GO TO 170
C
C Step 3
C
130 z = u1**2*u2
IF (.NOT. (z.LE.0.25)) GO TO 160
v = beta*log(u1/ (1.0-u1))
C JJV instead of checking v > expmax at top, I will check
C JJV if a < 1, then check the appropriate values
IF (a.GT.1.0) GO TO 135
C JJV A < 1 so it can help out if EXP(V) would overflow
IF (v.GT.expmax) GO TO 132
w = a*exp(v)
GO TO 200
132 w = v + log(a)
IF (w.GT.expmax) GO TO 140
w = exp(w)
GO TO 200
C JJV in this case A > 1
135 IF (v.GT.expmax) GO TO 140
w = exp(v)
IF (w.GT.infnty/a) GO TO 140
w = a*w
GO TO 200
140 w = infnty
GO TO 200
160 IF (z.GE.k2) GO TO 120
C
C Step 4
C
C
C Step 5
C
170 v = beta*log(u1/ (1.0-u1))
C JJV same kind of checking as above
IF (a.GT.1.0) GO TO 175
C JJV A < 1 so it can help out if EXP(V) would overflow
IF (v.GT.expmax) GO TO 172
w = a*exp(v)
GO TO 190
172 w = v + log(a)
IF (w.GT.expmax) GO TO 180
w = exp(w)
GO TO 190
C JJV in this case A > 1
175 IF (v.GT.expmax) GO TO 180
w = exp(v)
IF (w.GT.infnty/a) GO TO 180
w = a*w
GO TO 190
180 w = infnty
C JJV here we also check to see if log overlows; if so, we treat it
C JJV as -INF, which means condition is true, i.e. restart
190 IF (alpha/(b+w).LT.minlog) GO TO 120
IF ((alpha* (log(alpha/ (b+w))+v)-1.3862944).LT.log(z)) GO TO 120
C
C Step 6
C
200 IF (.NOT. (a.EQ.aa)) GO TO 210
genbet = w/ (b+w)
GO TO 220
210 genbet = b/ (b+w)
220 CONTINUE
230 RETURN
END
|