| 1 |
|
| #include "ranlib.h" |
| 2 |
|
| #include <stdio.h> |
| 3 |
|
| #include <math.h> |
| 4 |
|
| #include <stdlib.h> |
| 5 |
|
| #define ABS(x) ((x) >= 0 ? (x) : -(x)) |
| 6 |
|
| #define min(a,b) ((a) <= (b) ? (a) : (b)) |
| 7 |
|
| #define max(a,b) ((a) >= (b) ? (a) : (b)) |
| 8 |
|
| void ftnstop(char*); |
| 9 |
0 |
| float genbet(float aa,float bb) |
| 10 |
|
| /* |
| 11 |
|
| ********************************************************************** |
| 12 |
|
| float genbet(float aa,float bb) |
| 13 |
|
| GeNerate BETa random deviate |
| 14 |
|
| Function |
| 15 |
|
| Returns a single random deviate from the beta distribution with |
| 16 |
|
| parameters A and B. The density of the beta is |
| 17 |
|
| x^(a-1) * (1-x)^(b-1) / B(a,b) for 0 < x < 1 |
| 18 |
|
| Arguments |
| 19 |
|
| aa --> First parameter of the beta distribution |
| 20 |
|
| |
| 21 |
|
| bb --> Second parameter of the beta distribution |
| 22 |
|
| |
| 23 |
|
| Method |
| 24 |
|
| R. C. H. Cheng |
| 25 |
|
| Generating Beta Variatew with Nonintegral Shape Parameters |
| 26 |
|
| Communications of the ACM, 21:317-322 (1978) |
| 27 |
|
| (Algorithms BB and BC) |
| 28 |
|
| ********************************************************************** |
| 29 |
|
| */ |
| 30 |
|
| { |
| 31 |
|
| #define expmax 89.0 |
| 32 |
|
| #define infnty 1.0E38 |
| 33 |
|
| static float olda = -1.0; |
| 34 |
|
| static float oldb = -1.0; |
| 35 |
|
| static float genbet,a,alpha,b,beta,delta,gamma,k1,k2,r,s,t,u1,u2,v,w,y,z; |
| 36 |
|
| static long qsame; |
| 37 |
|
| |
| 38 |
0 |
▶ | qsame = olda == aa && oldb == bb; |
Branch: 2 branches
| Branch | True | False | Total |
| qsame = olda == aa && oldb == bb; |
0 |
0 |
0 |
| qsame = olda == aa && oldb == bb; |
0 |
0 |
0 |
|
| 39 |
0 |
▶ | if(qsame) goto S20; |
Branch: 1 branch
| Branch | True | False | Total |
| if(qsame) goto S20; |
0 |
0 |
0 |
|
| 40 |
0 |
▶ | if(!(aa <= 0.0 || bb <= 0.0)) goto S10; |
Branch: 2 branches
| Branch | True | False | Total |
| if(!(aa <= 0.0 || bb <= 0.0)) goto S10; |
0 |
0 |
0 |
| if(!(aa <= 0.0 || bb <= 0.0)) goto S10; |
0 |
0 |
0 |
|
| 41 |
0 |
| fputs(" AA or BB <= 0 in GENBET - Abort!",stderr); |
| 42 |
0 |
| fprintf(stderr," AA: %16.6E BB %16.6E\n",aa,bb); |
| 43 |
0 |
| exit(1); |
| 44 |
0 |
| S10: |
| 45 |
0 |
| olda = aa; |
| 46 |
0 |
| oldb = bb; |
| 47 |
0 |
| S20: |
| 48 |
0 |
▶ | if(!(min(aa,bb) > 1.0)) goto S100; |
Branch: 2 branches
| Branch | True | False | Total |
| if(!(min(aa,bb) > 1.0)) goto S100; |
0 |
0 |
0 |
| if(!(min(aa,bb) > 1.0)) goto S100; |
0 |
0 |
0 |
|
| 49 |
|
| /* |
| 50 |
|
| Alborithm BB |
| 51 |
|
| Initialize |
| 52 |
|
| */ |
| 53 |
0 |
▶ | if(qsame) goto S30; |
Branch: 1 branch
| Branch | True | False | Total |
| if(qsame) goto S30; |
0 |
0 |
0 |
|
| 54 |
0 |
▶ | a = min(aa,bb); |
Branch: 1 branch
| Branch | True | False | Total |
| a = min(aa,bb); |
0 |
0 |
0 |
|
| 55 |
0 |
▶ | b = max(aa,bb); |
Branch: 1 branch
| Branch | True | False | Total |
| b = max(aa,bb); |
0 |
0 |
0 |
|
| 56 |
0 |
| alpha = a+b; |
| 57 |
0 |
| beta = sqrt((alpha-2.0)/(2.0*a*b-alpha)); |
| 58 |
0 |
| gamma = a+1.0/beta; |
| 59 |
0 |
| S30: |
| 60 |
0 |
| S40: |
| 61 |
0 |
| u1 = ranf(); |
| 62 |
|
| /* |
| 63 |
|
| Step 1 |
| 64 |
|
| */ |
| 65 |
0 |
| u2 = ranf(); |
| 66 |
0 |
| v = beta*log(u1/(1.0-u1)); |
| 67 |
0 |
▶ | if(!(v > expmax)) goto S50; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(v > expmax)) goto S50; |
0 |
0 |
0 |
|
| 68 |
0 |
| w = infnty; |
| 69 |
0 |
| goto S60; |
| 70 |
0 |
| S50: |
| 71 |
0 |
| w = a*exp(v); |
| 72 |
0 |
| S60: |
| 73 |
0 |
| z = pow(u1,2.0)*u2; |
| 74 |
0 |
| r = gamma*v-1.3862944; |
| 75 |
0 |
| s = a+r-w; |
| 76 |
|
| /* |
| 77 |
|
| Step 2 |
| 78 |
|
| */ |
| 79 |
0 |
▶ | if(s+2.609438 >= 5.0*z) goto S70; |
Branch: 1 branch
| Branch | True | False | Total |
| if(s+2.609438 >= 5.0*z) goto S70; |
0 |
0 |
0 |
|
| 80 |
|
| /* |
| 81 |
|
| Step 3 |
| 82 |
|
| */ |
| 83 |
0 |
| t = log(z); |
| 84 |
0 |
▶ | if(s > t) goto S70; |
Branch: 1 branch
| Branch | True | False | Total |
| if(s > t) goto S70; |
0 |
0 |
0 |
|
| 85 |
|
| /* |
| 86 |
|
| Step 4 |
| 87 |
|
| */ |
| 88 |
0 |
▶ | if(r+alpha*log(alpha/(b+w)) < t) goto S40; |
Branch: 1 branch
| Branch | True | False | Total |
| if(r+alpha*log(alpha/(b+w)) < t) goto S40; |
0 |
0 |
0 |
|
| 89 |
0 |
| S70: |
| 90 |
|
| /* |
| 91 |
|
| Step 5 |
| 92 |
|
| */ |
| 93 |
0 |
▶ | if(!(aa == a)) goto S80; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(aa == a)) goto S80; |
0 |
0 |
0 |
|
| 94 |
0 |
| genbet = w/(b+w); |
| 95 |
0 |
| goto S90; |
| 96 |
0 |
| S80: |
| 97 |
0 |
| genbet = b/(b+w); |
| 98 |
0 |
| S90: |
| 99 |
0 |
| goto S230; |
| 100 |
0 |
| S100: |
| 101 |
|
| /* |
| 102 |
|
| Algorithm BC |
| 103 |
|
| Initialize |
| 104 |
|
| */ |
| 105 |
0 |
▶ | if(qsame) goto S110; |
Branch: 1 branch
| Branch | True | False | Total |
| if(qsame) goto S110; |
0 |
0 |
0 |
|
| 106 |
0 |
▶ | a = max(aa,bb); |
Branch: 1 branch
| Branch | True | False | Total |
| a = max(aa,bb); |
0 |
0 |
0 |
|
| 107 |
0 |
▶ | b = min(aa,bb); |
Branch: 1 branch
| Branch | True | False | Total |
| b = min(aa,bb); |
0 |
0 |
0 |
|
| 108 |
0 |
| alpha = a+b; |
| 109 |
0 |
| beta = 1.0/b; |
| 110 |
0 |
| delta = 1.0+a-b; |
| 111 |
0 |
| k1 = delta*(1.38889E-2+4.16667E-2*b)/(a*beta-0.777778); |
| 112 |
0 |
| k2 = 0.25+(0.5+0.25/delta)*b; |
| 113 |
0 |
| S110: |
| 114 |
0 |
| S120: |
| 115 |
0 |
| u1 = ranf(); |
| 116 |
|
| /* |
| 117 |
|
| Step 1 |
| 118 |
|
| */ |
| 119 |
0 |
| u2 = ranf(); |
| 120 |
0 |
▶ | if(u1 >= 0.5) goto S130; |
Branch: 1 branch
| Branch | True | False | Total |
| if(u1 >= 0.5) goto S130; |
0 |
0 |
0 |
|
| 121 |
|
| /* |
| 122 |
|
| Step 2 |
| 123 |
|
| */ |
| 124 |
0 |
| y = u1*u2; |
| 125 |
0 |
| z = u1*y; |
| 126 |
0 |
▶ | if(0.25*u2+z-y >= k1) goto S120; |
Branch: 1 branch
| Branch | True | False | Total |
| if(0.25*u2+z-y >= k1) goto S120; |
0 |
0 |
0 |
|
| 127 |
0 |
| goto S170; |
| 128 |
0 |
| S130: |
| 129 |
|
| /* |
| 130 |
|
| Step 3 |
| 131 |
|
| */ |
| 132 |
0 |
| z = pow(u1,2.0)*u2; |
| 133 |
0 |
▶ | if(!(z <= 0.25)) goto S160; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(z <= 0.25)) goto S160; |
0 |
0 |
0 |
|
| 134 |
0 |
| v = beta*log(u1/(1.0-u1)); |
| 135 |
0 |
▶ | if(!(v > expmax)) goto S140; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(v > expmax)) goto S140; |
0 |
0 |
0 |
|
| 136 |
0 |
| w = infnty; |
| 137 |
0 |
| goto S150; |
| 138 |
0 |
| S140: |
| 139 |
0 |
| w = a*exp(v); |
| 140 |
0 |
| S150: |
| 141 |
0 |
| goto S200; |
| 142 |
0 |
| S160: |
| 143 |
0 |
▶ | if(z >= k2) goto S120; |
Branch: 1 branch
| Branch | True | False | Total |
| if(z >= k2) goto S120; |
0 |
0 |
0 |
|
| 144 |
0 |
| S170: |
| 145 |
|
| /* |
| 146 |
|
| Step 4 |
| 147 |
|
| Step 5 |
| 148 |
|
| */ |
| 149 |
0 |
| v = beta*log(u1/(1.0-u1)); |
| 150 |
0 |
▶ | if(!(v > expmax)) goto S180; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(v > expmax)) goto S180; |
0 |
0 |
0 |
|
| 151 |
0 |
| w = infnty; |
| 152 |
0 |
| goto S190; |
| 153 |
0 |
| S180: |
| 154 |
0 |
| w = a*exp(v); |
| 155 |
0 |
| S190: |
| 156 |
0 |
▶ | if(alpha*(log(alpha/(b+w))+v)-1.3862944 < log(z)) goto S120; |
Branch: 1 branch
| Branch | True | False | Total |
| if(alpha*(log(alpha/(b+w))+v)-1.3862944 < log(z)) goto S120; |
0 |
0 |
0 |
|
| 157 |
0 |
| S200: |
| 158 |
|
| /* |
| 159 |
|
| Step 6 |
| 160 |
|
| */ |
| 161 |
0 |
▶ | if(!(a == aa)) goto S210; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(a == aa)) goto S210; |
0 |
0 |
0 |
|
| 162 |
0 |
| genbet = w/(b+w); |
| 163 |
0 |
| goto S220; |
| 164 |
0 |
| S210: |
| 165 |
0 |
| genbet = b/(b+w); |
| 166 |
0 |
| S230: |
| 167 |
0 |
| S220: |
| 168 |
0 |
| return genbet; |
| 169 |
|
| #undef expmax |
| 170 |
|
| #undef infnty |
| 171 |
|
| } |
| 172 |
0 |
| float genchi(float df) |
| 173 |
|
| /* |
| 174 |
|
| ********************************************************************** |
| 175 |
|
| float genchi(float df) |
| 176 |
|
| Generate random value of CHIsquare variable |
| 177 |
|
| Function |
| 178 |
|
| Generates random deviate from the distribution of a chisquare |
| 179 |
|
| with DF degrees of freedom random variable. |
| 180 |
|
| Arguments |
| 181 |
|
| df --> Degrees of freedom of the chisquare |
| 182 |
|
| (Must be positive) |
| 183 |
|
| |
| 184 |
|
| Method |
| 185 |
|
| Uses relation between chisquare and gamma. |
| 186 |
|
| ********************************************************************** |
| 187 |
|
| */ |
| 188 |
|
| { |
| 189 |
|
| static float genchi; |
| 190 |
|
| |
| 191 |
0 |
▶ | if(!(df <= 0.0)) goto S10; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(df <= 0.0)) goto S10; |
0 |
0 |
0 |
|
| 192 |
0 |
| fputs("DF <= 0 in GENCHI - ABORT",stderr); |
| 193 |
0 |
| fprintf(stderr,"Value of DF: %16.6E\n",df); |
| 194 |
0 |
| exit(1); |
| 195 |
0 |
| S10: |
| 196 |
0 |
| genchi = 2.0*gengam(1.0,df/2.0); |
| 197 |
0 |
| return genchi; |
| 198 |
|
| } |
| 199 |
0 |
| float genexp(float av) |
| 200 |
|
| /* |
| 201 |
|
| ********************************************************************** |
| 202 |
|
| float genexp(float av) |
| 203 |
|
| GENerate EXPonential random deviate |
| 204 |
|
| Function |
| 205 |
|
| Generates a single random deviate from an exponential |
| 206 |
|
| distribution with mean AV. |
| 207 |
|
| Arguments |
| 208 |
|
| av --> The mean of the exponential distribution from which |
| 209 |
|
| a random deviate is to be generated. |
| 210 |
|
| Method |
| 211 |
|
| Renames SEXPO from TOMS as slightly modified by BWB to use RANF |
| 212 |
|
| instead of SUNIF. |
| 213 |
|
| For details see: |
| 214 |
|
| Ahrens, J.H. and Dieter, U. |
| 215 |
|
| Computer Methods for Sampling From the |
| 216 |
|
| Exponential and Normal Distributions. |
| 217 |
|
| Comm. ACM, 15,10 (Oct. 1972), 873 - 882. |
| 218 |
|
| ********************************************************************** |
| 219 |
|
| */ |
| 220 |
|
| { |
| 221 |
|
| static float genexp; |
| 222 |
|
| |
| 223 |
0 |
| genexp = sexpo()*av; |
| 224 |
0 |
| return genexp; |
| 225 |
|
| } |
| 226 |
0 |
| float genf(float dfn,float dfd) |
| 227 |
|
| /* |
| 228 |
|
| ********************************************************************** |
| 229 |
|
| float genf(float dfn,float dfd) |
| 230 |
|
| GENerate random deviate from the F distribution |
| 231 |
|
| Function |
| 232 |
|
| Generates a random deviate from the F (variance ratio) |
| 233 |
|
| distribution with DFN degrees of freedom in the numerator |
| 234 |
|
| and DFD degrees of freedom in the denominator. |
| 235 |
|
| Arguments |
| 236 |
|
| dfn --> Numerator degrees of freedom |
| 237 |
|
| (Must be positive) |
| 238 |
|
| dfd --> Denominator degrees of freedom |
| 239 |
|
| (Must be positive) |
| 240 |
|
| Method |
| 241 |
|
| Directly generates ratio of chisquare variates |
| 242 |
|
| ********************************************************************** |
| 243 |
|
| */ |
| 244 |
|
| { |
| 245 |
|
| static float genf,xden,xnum; |
| 246 |
|
| |
| 247 |
0 |
▶ | if(!(dfn <= 0.0 || dfd <= 0.0)) goto S10; |
Branch: 2 branches
| Branch | True | False | Total |
| if(!(dfn <= 0.0 || dfd <= 0.0)) goto S10; |
0 |
0 |
0 |
| if(!(dfn <= 0.0 || dfd <= 0.0)) goto S10; |
0 |
0 |
0 |
|
| 248 |
0 |
| fputs("Degrees of freedom nonpositive in GENF - abort!",stderr); |
| 249 |
0 |
| fprintf(stderr,"DFN value: %16.6EDFD value: %16.6E\n",dfn,dfd); |
| 250 |
0 |
| exit(1); |
| 251 |
0 |
| S10: |
| 252 |
0 |
| xnum = genchi(dfn)/dfn; |
| 253 |
|
| /* |
| 254 |
|
| GENF = ( GENCHI( DFN ) / DFN ) / ( GENCHI( DFD ) / DFD ) |
| 255 |
|
| */ |
| 256 |
0 |
| xden = genchi(dfd)/dfd; |
| 257 |
0 |
▶ | if(!(xden <= 9.999999999998E-39*xnum)) goto S20; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(xden <= 9.999999999998E-39*xnum)) goto S20; |
0 |
0 |
0 |
|
| 258 |
0 |
| fputs(" GENF - generated numbers would cause overflow",stderr); |
| 259 |
0 |
| fprintf(stderr," Numerator %16.6E Denominator %16.6E\n",xnum,xden); |
| 260 |
0 |
| fputs(" GENF returning 1.0E38",stderr); |
| 261 |
0 |
| genf = 1.0E38; |
| 262 |
0 |
| goto S30; |
| 263 |
0 |
| S20: |
| 264 |
0 |
| genf = xnum/xden; |
| 265 |
0 |
| S30: |
| 266 |
0 |
| return genf; |
| 267 |
|
| } |
| 268 |
0 |
| float gengam(float a,float r) |
| 269 |
|
| /* |
| 270 |
|
| ********************************************************************** |
| 271 |
|
| float gengam(float a,float r) |
| 272 |
|
| GENerates random deviates from GAMma distribution |
| 273 |
|
| Function |
| 274 |
|
| Generates random deviates from the gamma distribution whose |
| 275 |
|
| density is |
| 276 |
|
| (A**R)/Gamma(R) * X**(R-1) * Exp(-A*X) |
| 277 |
|
| Arguments |
| 278 |
|
| a --> Location parameter of Gamma distribution |
| 279 |
|
| r --> Shape parameter of Gamma distribution |
| 280 |
|
| Method |
| 281 |
|
| Renames SGAMMA from TOMS as slightly modified by BWB to use RANF |
| 282 |
|
| instead of SUNIF. |
| 283 |
|
| For details see: |
| 284 |
|
| (Case R >= 1.0) |
| 285 |
|
| Ahrens, J.H. and Dieter, U. |
| 286 |
|
| Generating Gamma Variates by a |
| 287 |
|
| Modified Rejection Technique. |
| 288 |
|
| Comm. ACM, 25,1 (Jan. 1982), 47 - 54. |
| 289 |
|
| Algorithm GD |
| 290 |
|
| (Case 0.0 <= R <= 1.0) |
| 291 |
|
| Ahrens, J.H. and Dieter, U. |
| 292 |
|
| Computer Methods for Sampling from Gamma, |
| 293 |
|
| Beta, Poisson and Binomial Distributions. |
| 294 |
|
| Computing, 12 (1974), 223-246/ |
| 295 |
|
| Adapted algorithm GS. |
| 296 |
|
| ********************************************************************** |
| 297 |
|
| */ |
| 298 |
|
| { |
| 299 |
|
| static float gengam; |
| 300 |
|
| |
| 301 |
0 |
| gengam = sgamma(r); |
| 302 |
0 |
| gengam /= a; |
| 303 |
0 |
| return gengam; |
| 304 |
|
| } |
| 305 |
0 |
| void genmn(float *parm,float *x,float *work) |
| 306 |
|
| /* |
| 307 |
|
| ********************************************************************** |
| 308 |
|
| void genmn(float *parm,float *x,float *work) |
| 309 |
|
| GENerate Multivariate Normal random deviate |
| 310 |
|
| Arguments |
| 311 |
|
| parm --> Parameters needed to generate multivariate normal |
| 312 |
|
| deviates (MEANV and Cholesky decomposition of |
| 313 |
|
| COVM). Set by a previous call to SETGMN. |
| 314 |
|
| 1 : 1 - size of deviate, P |
| 315 |
|
| 2 : P + 1 - mean vector |
| 316 |
|
| P+2 : P*(P+3)/2 + 1 - upper half of cholesky |
| 317 |
|
| decomposition of cov matrix |
| 318 |
|
| x <-- Vector deviate generated. |
| 319 |
|
| work <--> Scratch array |
| 320 |
|
| Method |
| 321 |
|
| 1) Generate P independent standard normal deviates - Ei ~ N(0,1) |
| 322 |
|
| 2) Using Cholesky decomposition find A s.t. trans(A)*A = COVM |
| 323 |
|
| 3) trans(A)E + MEANV ~ N(MEANV,COVM) |
| 324 |
|
| ********************************************************************** |
| 325 |
|
| */ |
| 326 |
|
| { |
| 327 |
|
| static long i,icount,j,p,D1,D2,D3,D4; |
| 328 |
|
| static float ae; |
| 329 |
|
| |
| 330 |
0 |
| p = (long) (*parm); |
| 331 |
|
| /* |
| 332 |
|
| Generate P independent normal deviates - WORK ~ N(0,1) |
| 333 |
|
| */ |
| 334 |
0 |
▶ | for(i=1; i<=p; i++) *(work+i-1) = snorm(); |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=1; i<=p; i++) *(work+i-1) = snorm(); |
0 |
0 |
0 |
|
| 335 |
0 |
▶ | for(i=1,D3=1,D4=(p-i+D3)/D3; D4>0; D4--,i+=D3) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=1,D3=1,D4=(p-i+D3)/D3; D4>0; D4--,i+=D3) { |
0 |
0 |
0 |
|
| 336 |
|
| /* |
| 337 |
|
| PARM (P+2 : P*(P+3)/2 + 1) contains A, the Cholesky |
| 338 |
|
| decomposition of the desired covariance matrix. |
| 339 |
|
| trans(A)(1,1) = PARM(P+2) |
| 340 |
|
| trans(A)(2,1) = PARM(P+3) |
| 341 |
|
| trans(A)(2,2) = PARM(P+2+P) |
| 342 |
|
| trans(A)(3,1) = PARM(P+4) |
| 343 |
|
| trans(A)(3,2) = PARM(P+3+P) |
| 344 |
|
| trans(A)(3,3) = PARM(P+2-1+2P) ... |
| 345 |
|
| trans(A)*WORK + MEANV ~ N(MEANV,COVM) |
| 346 |
|
| */ |
| 347 |
0 |
| icount = 0; |
| 348 |
0 |
| ae = 0.0; |
| 349 |
0 |
▶ | for(j=1,D1=1,D2=(i-j+D1)/D1; D2>0; D2--,j+=D1) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=1,D1=1,D2=(i-j+D1)/D1; D2>0; D2--,j+=D1) { |
0 |
0 |
0 |
|
| 350 |
0 |
| icount += (j-1); |
| 351 |
0 |
| ae += (*(parm+i+(j-1)*p-icount+p)**(work+j-1)); |
| 352 |
|
| } |
| 353 |
0 |
| *(x+i-1) = ae+*(parm+i); |
| 354 |
|
| } |
| 355 |
0 |
| } |
| 356 |
0 |
| void genmul(long n,float *p,long ncat,long *ix) |
| 357 |
|
| /* |
| 358 |
|
| ********************************************************************** |
| 359 |
|
| |
| 360 |
|
| void genmul(int n,float *p,int ncat,int *ix) |
| 361 |
|
| GENerate an observation from the MULtinomial distribution |
| 362 |
|
| Arguments |
| 363 |
|
| N --> Number of events that will be classified into one of |
| 364 |
|
| the categories 1..NCAT |
| 365 |
|
| P --> Vector of probabilities. P(i) is the probability that |
| 366 |
|
| an event will be classified into category i. Thus, P(i) |
| 367 |
|
| must be [0,1]. Only the first NCAT-1 P(i) must be defined |
| 368 |
|
| since P(NCAT) is 1.0 minus the sum of the first |
| 369 |
|
| NCAT-1 P(i). |
| 370 |
|
| NCAT --> Number of categories. Length of P and IX. |
| 371 |
|
| IX <-- Observation from multinomial distribution. All IX(i) |
| 372 |
|
| will be nonnegative and their sum will be N. |
| 373 |
|
| Method |
| 374 |
|
| Algorithm from page 559 of |
| 375 |
|
| |
| 376 |
|
| Devroye, Luc |
| 377 |
|
| |
| 378 |
|
| Non-Uniform Random Variate Generation. Springer-Verlag, |
| 379 |
|
| New York, 1986. |
| 380 |
|
| |
| 381 |
|
| ********************************************************************** |
| 382 |
|
| */ |
| 383 |
|
| { |
| 384 |
|
| static float prob,ptot,sum; |
| 385 |
|
| static long i,icat,ntot; |
| 386 |
0 |
▶ | if(n < 0) ftnstop("N < 0 in GENMUL"); |
Branch: 1 branch
| Branch | True | False | Total |
| if(n < 0) ftnstop("N < 0 in GENMUL"); |
0 |
0 |
0 |
|
| 387 |
0 |
▶ | if(ncat <= 1) ftnstop("NCAT <= 1 in GENMUL"); |
Branch: 1 branch
| Branch | True | False | Total |
| if(ncat <= 1) ftnstop("NCAT <= 1 in GENMUL"); |
0 |
0 |
0 |
|
| 388 |
0 |
| ptot = 0.0F; |
| 389 |
0 |
▶ | for(i=0; i<ncat-1; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<ncat-1; i++) { |
0 |
0 |
0 |
|
| 390 |
0 |
▶ | if(*(p+i) < 0.0F) ftnstop("Some P(i) < 0 in GENMUL"); |
Branch: 1 branch
| Branch | True | False | Total |
| if(*(p+i) < 0.0F) ftnstop("Some P(i) < 0 in GENMUL"); |
0 |
0 |
0 |
|
| 391 |
0 |
▶ | if(*(p+i) > 1.0F) ftnstop("Some P(i) > 1 in GENMUL"); |
Branch: 1 branch
| Branch | True | False | Total |
| if(*(p+i) > 1.0F) ftnstop("Some P(i) > 1 in GENMUL"); |
0 |
0 |
0 |
|
| 392 |
0 |
| ptot += *(p+i); |
| 393 |
|
| } |
| 394 |
0 |
▶ | if(ptot > 0.99999F) ftnstop("Sum of P(i) > 1 in GENMUL"); |
Branch: 1 branch
| Branch | True | False | Total |
| if(ptot > 0.99999F) ftnstop("Sum of P(i) > 1 in GENMUL"); |
0 |
0 |
0 |
|
| 395 |
|
| /* |
| 396 |
|
| Initialize variables |
| 397 |
|
| */ |
| 398 |
0 |
| ntot = n; |
| 399 |
0 |
| sum = 1.0F; |
| 400 |
0 |
▶ | for(i=0; i<ncat; i++) ix[i] = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<ncat; i++) ix[i] = 0; |
0 |
0 |
0 |
|
| 401 |
|
| /* |
| 402 |
|
| Generate the observation |
| 403 |
|
| */ |
| 404 |
0 |
▶ | for(icat=0; icat<ncat-1; icat++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(icat=0; icat<ncat-1; icat++) { |
0 |
0 |
0 |
|
| 405 |
0 |
| prob = *(p+icat)/sum; |
| 406 |
0 |
| *(ix+icat) = ignbin(ntot,prob); |
| 407 |
0 |
| ntot -= *(ix+icat); |
| 408 |
0 |
▶ | if(ntot <= 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if(ntot <= 0) return; |
0 |
0 |
0 |
|
| 409 |
0 |
| sum -= *(p+icat); |
| 410 |
|
| } |
| 411 |
0 |
| *(ix+ncat-1) = ntot; |
| 412 |
|
| /* |
| 413 |
|
| Finished |
| 414 |
|
| */ |
| 415 |
0 |
| return; |
| 416 |
|
| } |
| 417 |
0 |
| float gennch(float df,float xnonc) |
| 418 |
|
| /* |
| 419 |
|
| ********************************************************************** |
| 420 |
|
| float gennch(float df,float xnonc) |
| 421 |
|
| Generate random value of Noncentral CHIsquare variable |
| 422 |
|
| Function |
| 423 |
|
| Generates random deviate from the distribution of a noncentral |
| 424 |
|
| chisquare with DF degrees of freedom and noncentrality parameter |
| 425 |
|
| xnonc. |
| 426 |
|
| Arguments |
| 427 |
|
| df --> Degrees of freedom of the chisquare |
| 428 |
|
| (Must be > 1.0) |
| 429 |
|
| xnonc --> Noncentrality parameter of the chisquare |
| 430 |
|
| (Must be >= 0.0) |
| 431 |
|
| Method |
| 432 |
|
| Uses fact that noncentral chisquare is the sum of a chisquare |
| 433 |
|
| deviate with DF-1 degrees of freedom plus the square of a normal |
| 434 |
|
| deviate with mean XNONC and standard deviation 1. |
| 435 |
|
| ********************************************************************** |
| 436 |
|
| */ |
| 437 |
|
| { |
| 438 |
|
| static float gennch; |
| 439 |
|
| |
| 440 |
0 |
▶ | if(!(df <= 1.0 || xnonc < 0.0)) goto S10; |
Branch: 2 branches
| Branch | True | False | Total |
| if(!(df <= 1.0 || xnonc < 0.0)) goto S10; |
0 |
0 |
0 |
| if(!(df <= 1.0 || xnonc < 0.0)) goto S10; |
0 |
0 |
0 |
|
| 441 |
0 |
| fputs("DF <= 1 or XNONC < 0 in GENNCH - ABORT",stderr); |
| 442 |
0 |
| fprintf(stderr,"Value of DF: %16.6E Value of XNONC%16.6E\n",df,xnonc); |
| 443 |
0 |
| exit(1); |
| 444 |
0 |
| S10: |
| 445 |
0 |
| gennch = genchi(df-1.0)+pow(gennor(sqrt(xnonc),1.0),2.0); |
| 446 |
0 |
| return gennch; |
| 447 |
|
| } |
| 448 |
0 |
| float gennf(float dfn,float dfd,float xnonc) |
| 449 |
|
| /* |
| 450 |
|
| ********************************************************************** |
| 451 |
|
| float gennf(float dfn,float dfd,float xnonc) |
| 452 |
|
| GENerate random deviate from the Noncentral F distribution |
| 453 |
|
| Function |
| 454 |
|
| Generates a random deviate from the noncentral F (variance ratio) |
| 455 |
|
| distribution with DFN degrees of freedom in the numerator, and DFD |
| 456 |
|
| degrees of freedom in the denominator, and noncentrality parameter |
| 457 |
|
| XNONC. |
| 458 |
|
| Arguments |
| 459 |
|
| dfn --> Numerator degrees of freedom |
| 460 |
|
| (Must be >= 1.0) |
| 461 |
|
| dfd --> Denominator degrees of freedom |
| 462 |
|
| (Must be positive) |
| 463 |
|
| xnonc --> Noncentrality parameter |
| 464 |
|
| (Must be nonnegative) |
| 465 |
|
| Method |
| 466 |
|
| Directly generates ratio of noncentral numerator chisquare variate |
| 467 |
|
| to central denominator chisquare variate. |
| 468 |
|
| ********************************************************************** |
| 469 |
|
| */ |
| 470 |
|
| { |
| 471 |
|
| static float gennf,xden,xnum; |
| 472 |
|
| static long qcond; |
| 473 |
|
| |
| 474 |
0 |
▶ | qcond = dfn <= 1.0 || dfd <= 0.0 || xnonc < 0.0; |
Branch: 3 branches
| Branch | True | False | Total |
| qcond = dfn <= 1.0 || dfd <= 0.0 || xnonc < 0.0; |
0 |
0 |
0 |
| qcond = dfn <= 1.0 || dfd <= 0.0 || xnonc < 0.0; |
0 |
0 |
0 |
| qcond = dfn <= 1.0 || dfd <= 0.0 || xnonc < 0.0; |
0 |
0 |
0 |
|
| 475 |
0 |
▶ | if(!qcond) goto S10; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!qcond) goto S10; |
0 |
0 |
0 |
|
| 476 |
0 |
| fputs("In GENNF - Either (1) Numerator DF <= 1.0 or",stderr); |
| 477 |
0 |
| fputs("(2) Denominator DF < 0.0 or ",stderr); |
| 478 |
0 |
| fputs("(3) Noncentrality parameter < 0.0",stderr); |
| 479 |
0 |
| fprintf(stderr, |
| 480 |
|
| "DFN value: %16.6EDFD value: %16.6EXNONC value: \n%16.6E\n",dfn,dfd, |
| 481 |
|
| xnonc); |
| 482 |
0 |
| exit(1); |
| 483 |
0 |
| S10: |
| 484 |
0 |
| xnum = gennch(dfn,xnonc)/dfn; |
| 485 |
|
| /* |
| 486 |
|
| GENNF = ( GENNCH( DFN, XNONC ) / DFN ) / ( GENCHI( DFD ) / DFD ) |
| 487 |
|
| */ |
| 488 |
0 |
| xden = genchi(dfd)/dfd; |
| 489 |
0 |
▶ | if(!(xden <= 9.999999999998E-39*xnum)) goto S20; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(xden <= 9.999999999998E-39*xnum)) goto S20; |
0 |
0 |
0 |
|
| 490 |
0 |
| fputs(" GENNF - generated numbers would cause overflow",stderr); |
| 491 |
0 |
| fprintf(stderr," Numerator %16.6E Denominator %16.6E\n",xnum,xden); |
| 492 |
0 |
| fputs(" GENNF returning 1.0E38",stderr); |
| 493 |
0 |
| gennf = 1.0E38; |
| 494 |
0 |
| goto S30; |
| 495 |
0 |
| S20: |
| 496 |
0 |
| gennf = xnum/xden; |
| 497 |
0 |
| S30: |
| 498 |
0 |
| return gennf; |
| 499 |
|
| } |
| 500 |
0 |
| float gennor(float av,float sd) |
| 501 |
|
| /* |
| 502 |
|
| ********************************************************************** |
| 503 |
|
| float gennor(float av,float sd) |
| 504 |
|
| GENerate random deviate from a NORmal distribution |
| 505 |
|
| Function |
| 506 |
|
| Generates a single random deviate from a normal distribution |
| 507 |
|
| with mean, AV, and standard deviation, SD. |
| 508 |
|
| Arguments |
| 509 |
|
| av --> Mean of the normal distribution. |
| 510 |
|
| sd --> Standard deviation of the normal distribution. |
| 511 |
|
| Method |
| 512 |
|
| Renames SNORM from TOMS as slightly modified by BWB to use RANF |
| 513 |
|
| instead of SUNIF. |
| 514 |
|
| For details see: |
| 515 |
|
| Ahrens, J.H. and Dieter, U. |
| 516 |
|
| Extensions of Forsythe's Method for Random |
| 517 |
|
| Sampling from the Normal Distribution. |
| 518 |
|
| Math. Comput., 27,124 (Oct. 1973), 927 - 937. |
| 519 |
|
| ********************************************************************** |
| 520 |
|
| */ |
| 521 |
|
| { |
| 522 |
|
| static float gennor; |
| 523 |
|
| |
| 524 |
0 |
| gennor = sd*snorm()+av; |
| 525 |
0 |
| return gennor; |
| 526 |
|
| } |
| 527 |
0 |
| void genprm(long *iarray,int larray) |
| 528 |
|
| /* |
| 529 |
|
| ********************************************************************** |
| 530 |
|
| void genprm(long *iarray,int larray) |
| 531 |
|
| GENerate random PeRMutation of iarray |
| 532 |
|
| Arguments |
| 533 |
|
| iarray <--> On output IARRAY is a random permutation of its |
| 534 |
|
| value on input |
| 535 |
|
| larray <--> Length of IARRAY |
| 536 |
|
| ********************************************************************** |
| 537 |
|
| */ |
| 538 |
|
| { |
| 539 |
|
| static long i,itmp,iwhich,D1,D2; |
| 540 |
|
| |
| 541 |
0 |
▶ | for(i=1,D1=1,D2=(larray-i+D1)/D1; D2>0; D2--,i+=D1) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=1,D1=1,D2=(larray-i+D1)/D1; D2>0; D2--,i+=D1) { |
0 |
0 |
0 |
|
| 542 |
0 |
| iwhich = ignuin(i,larray); |
| 543 |
0 |
| itmp = *(iarray+iwhich-1); |
| 544 |
0 |
| *(iarray+iwhich-1) = *(iarray+i-1); |
| 545 |
0 |
| *(iarray+i-1) = itmp; |
| 546 |
|
| } |
| 547 |
0 |
| } |
| 548 |
0 |
| float genunf(float low,float high) |
| 549 |
|
| /* |
| 550 |
|
| ********************************************************************** |
| 551 |
|
| float genunf(float low,float high) |
| 552 |
|
| GeNerate Uniform Real between LOW and HIGH |
| 553 |
|
| Function |
| 554 |
|
| Generates a real uniformly distributed between LOW and HIGH. |
| 555 |
|
| Arguments |
| 556 |
|
| low --> Low bound (exclusive) on real value to be generated |
| 557 |
|
| high --> High bound (exclusive) on real value to be generated |
| 558 |
|
| ********************************************************************** |
| 559 |
|
| */ |
| 560 |
|
| { |
| 561 |
|
| static float genunf; |
| 562 |
|
| |
| 563 |
0 |
▶ | if(!(low > high)) goto S10; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(low > high)) goto S10; |
0 |
0 |
0 |
|
| 564 |
0 |
| fprintf(stderr,"LOW > HIGH in GENUNF: LOW %16.6E HIGH: %16.6E\n",low,high); |
| 565 |
0 |
| fputs("Abort",stderr); |
| 566 |
0 |
| exit(1); |
| 567 |
0 |
| S10: |
| 568 |
0 |
| genunf = low+(high-low)*ranf(); |
| 569 |
0 |
| return genunf; |
| 570 |
|
| } |
| 571 |
818 |
| void gscgn(long getset,long *g) |
| 572 |
|
| /* |
| 573 |
|
| ********************************************************************** |
| 574 |
|
| void gscgn(long getset,long *g) |
| 575 |
|
| Get/Set GeNerator |
| 576 |
|
| Gets or returns in G the number of the current generator |
| 577 |
|
| Arguments |
| 578 |
|
| getset --> 0 Get |
| 579 |
|
| 1 Set |
| 580 |
|
| g <-- Number of the current random number generator (1..32) |
| 581 |
|
| ********************************************************************** |
| 582 |
|
| */ |
| 583 |
|
| { |
| 584 |
|
| #define numg 32L |
| 585 |
|
| static long curntg = 1; |
| 586 |
818 |
▶ | if(getset == 0) *g = curntg; |
Branch: 1 branch
| Branch | True | False | Total |
| if(getset == 0) *g = curntg; |
654 |
164 |
818 |
|
| 587 |
|
| else { |
| 588 |
164 |
▶ | if(*g < 0 || *g > numg) { |
Branch: 2 branches
| Branch | True | False | Total |
| if(*g < 0 || *g > numg) { |
164 |
0 |
164 |
| if(*g < 0 || *g > numg) { |
0 |
164 |
164 |
|
| 589 |
0 |
| fputs(" Generator number out of range in GSCGN",stderr); |
| 590 |
0 |
| exit(0); |
| 591 |
|
| } |
| 592 |
164 |
| curntg = *g; |
| 593 |
|
| } |
| 594 |
|
| #undef numg |
| 595 |
818 |
| } |
| 596 |
655 |
| void gsrgs(long getset,long *qvalue) |
| 597 |
|
| /* |
| 598 |
|
| ********************************************************************** |
| 599 |
|
| void gsrgs(long getset,long *qvalue) |
| 600 |
|
| Get/Set Random Generators Set |
| 601 |
|
| Gets or sets whether random generators set (initialized). |
| 602 |
|
| Initially (data statement) state is not set |
| 603 |
|
| If getset is 1 state is set to qvalue |
| 604 |
|
| If getset is 0 state returned in qvalue |
| 605 |
|
| ********************************************************************** |
| 606 |
|
| */ |
| 607 |
|
| { |
| 608 |
|
| static long qinit = 0; |
| 609 |
|
| |
| 610 |
655 |
▶ | if(getset == 0) *qvalue = qinit; |
Branch: 1 branch
| Branch | True | False | Total |
| if(getset == 0) *qvalue = qinit; |
654 |
1 |
655 |
|
| 611 |
1 |
| else qinit = *qvalue; |
| 612 |
655 |
| } |
| 613 |
490 |
| void gssst(long getset,long *qset) |
| 614 |
|
| /* |
| 615 |
|
| ********************************************************************** |
| 616 |
|
| void gssst(long getset,long *qset) |
| 617 |
|
| Get or Set whether Seed is Set |
| 618 |
|
| Initialize to Seed not Set |
| 619 |
|
| If getset is 1 sets state to Seed Set |
| 620 |
|
| If getset is 0 returns T in qset if Seed Set |
| 621 |
|
| Else returns F in qset |
| 622 |
|
| ********************************************************************** |
| 623 |
|
| */ |
| 624 |
|
| { |
| 625 |
|
| static long qstate = 0; |
| 626 |
490 |
▶ | if(getset != 0) qstate = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if(getset != 0) qstate = 1; |
5 |
485 |
490 |
|
| 627 |
485 |
| else *qset = qstate; |
| 628 |
490 |
| } |
| 629 |
0 |
| long ignbin(long n,float pp) |
| 630 |
|
| /* |
| 631 |
|
| ********************************************************************** |
| 632 |
|
| long ignbin(long n,float pp) |
| 633 |
|
| GENerate BINomial random deviate |
| 634 |
|
| Function |
| 635 |
|
| Generates a single random deviate from a binomial |
| 636 |
|
| distribution whose number of trials is N and whose |
| 637 |
|
| probability of an event in each trial is P. |
| 638 |
|
| Arguments |
| 639 |
|
| n --> The number of trials in the binomial distribution |
| 640 |
|
| from which a random deviate is to be generated. |
| 641 |
|
| p --> The probability of an event in each trial of the |
| 642 |
|
| binomial distribution from which a random deviate |
| 643 |
|
| is to be generated. |
| 644 |
|
| ignbin <-- A random deviate yielding the number of events |
| 645 |
|
| from N independent trials, each of which has |
| 646 |
|
| a probability of event P. |
| 647 |
|
| Method |
| 648 |
|
| This is algorithm BTPE from: |
| 649 |
|
| Kachitvichyanukul, V. and Schmeiser, B. W. |
| 650 |
|
| Binomial Random Variate Generation. |
| 651 |
|
| Communications of the ACM, 31, 2 |
| 652 |
|
| (February, 1988) 216. |
| 653 |
|
| ********************************************************************** |
| 654 |
|
| SUBROUTINE BTPEC(N,PP,ISEED,JX) |
| 655 |
|
| BINOMIAL RANDOM VARIATE GENERATOR |
| 656 |
|
| MEAN .LT. 30 -- INVERSE CDF |
| 657 |
|
| MEAN .GE. 30 -- ALGORITHM BTPE: ACCEPTANCE-REJECTION VIA |
| 658 |
|
| FOUR REGION COMPOSITION. THE FOUR REGIONS ARE A TRIANGLE |
| 659 |
|
| (SYMMETRIC IN THE CENTER), A PAIR OF PARALLELOGRAMS (ABOVE |
| 660 |
|
| THE TRIANGLE), AND EXPONENTIAL LEFT AND RIGHT TAILS. |
| 661 |
|
| BTPE REFERS TO BINOMIAL-TRIANGLE-PARALLELOGRAM-EXPONENTIAL. |
| 662 |
|
| BTPEC REFERS TO BTPE AND "COMBINED." THUS BTPE IS THE |
| 663 |
|
| RESEARCH AND BTPEC IS THE IMPLEMENTATION OF A COMPLETE |
| 664 |
|
| USABLE ALGORITHM. |
| 665 |
|
| REFERENCE: VORATAS KACHITVICHYANUKUL AND BRUCE SCHMEISER, |
| 666 |
|
| "BINOMIAL RANDOM VARIATE GENERATION," |
| 667 |
|
| COMMUNICATIONS OF THE ACM, FORTHCOMING |
| 668 |
|
| WRITTEN: SEPTEMBER 1980. |
| 669 |
|
| LAST REVISED: MAY 1985, JULY 1987 |
| 670 |
|
| REQUIRED SUBPROGRAM: RAND() -- A UNIFORM (0,1) RANDOM NUMBER |
| 671 |
|
| GENERATOR |
| 672 |
|
| ARGUMENTS |
| 673 |
|
| N : NUMBER OF BERNOULLI TRIALS (INPUT) |
| 674 |
|
| PP : PROBABILITY OF SUCCESS IN EACH TRIAL (INPUT) |
| 675 |
|
| ISEED: RANDOM NUMBER SEED (INPUT AND OUTPUT) |
| 676 |
|
| JX: RANDOMLY GENERATED OBSERVATION (OUTPUT) |
| 677 |
|
| VARIABLES |
| 678 |
|
| PSAVE: VALUE OF PP FROM THE LAST CALL TO BTPEC |
| 679 |
|
| NSAVE: VALUE OF N FROM THE LAST CALL TO BTPEC |
| 680 |
|
| XNP: VALUE OF THE MEAN FROM THE LAST CALL TO BTPEC |
| 681 |
|
| P: PROBABILITY USED IN THE GENERATION PHASE OF BTPEC |
| 682 |
|
| FFM: TEMPORARY VARIABLE EQUAL TO XNP + P |
| 683 |
|
| M: INTEGER VALUE OF THE CURRENT MODE |
| 684 |
|
| FM: FLOATING POINT VALUE OF THE CURRENT MODE |
| 685 |
|
| XNPQ: TEMPORARY VARIABLE USED IN SETUP AND SQUEEZING STEPS |
| 686 |
|
| P1: AREA OF THE TRIANGLE |
| 687 |
|
| C: HEIGHT OF THE PARALLELOGRAMS |
| 688 |
|
| XM: CENTER OF THE TRIANGLE |
| 689 |
|
| XL: LEFT END OF THE TRIANGLE |
| 690 |
|
| XR: RIGHT END OF THE TRIANGLE |
| 691 |
|
| AL: TEMPORARY VARIABLE |
| 692 |
|
| XLL: RATE FOR THE LEFT EXPONENTIAL TAIL |
| 693 |
|
| XLR: RATE FOR THE RIGHT EXPONENTIAL TAIL |
| 694 |
|
| P2: AREA OF THE PARALLELOGRAMS |
| 695 |
|
| P3: AREA OF THE LEFT EXPONENTIAL TAIL |
| 696 |
|
| P4: AREA OF THE RIGHT EXPONENTIAL TAIL |
| 697 |
|
| U: A U(0,P4) RANDOM VARIATE USED FIRST TO SELECT ONE OF THE |
| 698 |
|
| FOUR REGIONS AND THEN CONDITIONALLY TO GENERATE A VALUE |
| 699 |
|
| FROM THE REGION |
| 700 |
|
| V: A U(0,1) RANDOM NUMBER USED TO GENERATE THE RANDOM VALUE |
| 701 |
|
| (REGION 1) OR TRANSFORMED INTO THE VARIATE TO ACCEPT OR |
| 702 |
|
| REJECT THE CANDIDATE VALUE |
| 703 |
|
| IX: INTEGER CANDIDATE VALUE |
| 704 |
|
| X: PRELIMINARY CONTINUOUS CANDIDATE VALUE IN REGION 2 LOGIC |
| 705 |
|
| AND A FLOATING POINT IX IN THE ACCEPT/REJECT LOGIC |
| 706 |
|
| K: ABSOLUTE VALUE OF (IX-M) |
| 707 |
|
| F: THE HEIGHT OF THE SCALED DENSITY FUNCTION USED IN THE |
| 708 |
|
| ACCEPT/REJECT DECISION WHEN BOTH M AND IX ARE SMALL |
| 709 |
|
| ALSO USED IN THE INVERSE TRANSFORMATION |
| 710 |
|
| R: THE RATIO P/Q |
| 711 |
|
| G: CONSTANT USED IN CALCULATION OF PROBABILITY |
| 712 |
|
| MP: MODE PLUS ONE, THE LOWER INDEX FOR EXPLICIT CALCULATION |
| 713 |
|
| OF F WHEN IX IS GREATER THAN M |
| 714 |
|
| IX1: CANDIDATE VALUE PLUS ONE, THE LOWER INDEX FOR EXPLICIT |
| 715 |
|
| CALCULATION OF F WHEN IX IS LESS THAN M |
| 716 |
|
| I: INDEX FOR EXPLICIT CALCULATION OF F FOR BTPE |
| 717 |
|
| AMAXP: MAXIMUM ERROR OF THE LOGARITHM OF NORMAL BOUND |
| 718 |
|
| YNORM: LOGARITHM OF NORMAL BOUND |
| 719 |
|
| ALV: NATURAL LOGARITHM OF THE ACCEPT/REJECT VARIATE V |
| 720 |
|
| X1,F1,Z,W,Z2,X2,F2, AND W2 ARE TEMPORARY VARIABLES TO BE |
| 721 |
|
| USED IN THE FINAL ACCEPT/REJECT TEST |
| 722 |
|
| QN: PROBABILITY OF NO SUCCESS IN N TRIALS |
| 723 |
|
| REMARK |
| 724 |
|
| IX AND JX COULD LOGICALLY BE THE SAME VARIABLE, WHICH WOULD |
| 725 |
|
| SAVE A MEMORY POSITION AND A LINE OF CODE. HOWEVER, SOME |
| 726 |
|
| COMPILERS (E.G.,CDC MNF) OPTIMIZE BETTER WHEN THE ARGUMENTS |
| 727 |
|
| ARE NOT INVOLVED. |
| 728 |
|
| ISEED NEEDS TO BE DOUBLE PRECISION IF THE IMSL ROUTINE |
| 729 |
|
| GGUBFS IS USED TO GENERATE UNIFORM RANDOM NUMBER, OTHERWISE |
| 730 |
|
| TYPE OF ISEED SHOULD BE DICTATED BY THE UNIFORM GENERATOR |
| 731 |
|
| ********************************************************************** |
| 732 |
|
| *****DETERMINE APPROPRIATE ALGORITHM AND WHETHER SETUP IS NECESSARY |
| 733 |
|
| */ |
| 734 |
|
| { |
| 735 |
|
| static float psave = -1.0; |
| 736 |
|
| static long nsave = -1; |
| 737 |
|
| static long ignbin,i,ix,ix1,k,m,mp,T1; |
| 738 |
|
| static float al,alv,amaxp,c,f,f1,f2,ffm,fm,g,p,p1,p2,p3,p4,q,qn,r,u,v,w,w2,x,x1, |
| 739 |
|
| x2,xl,xll,xlr,xm,xnp,xnpq,xr,ynorm,z,z2; |
| 740 |
|
| |
| 741 |
0 |
▶ | if(pp != psave) goto S10; |
Branch: 1 branch
| Branch | True | False | Total |
| if(pp != psave) goto S10; |
0 |
0 |
0 |
|
| 742 |
0 |
▶ | if(n != nsave) goto S20; |
Branch: 1 branch
| Branch | True | False | Total |
| if(n != nsave) goto S20; |
0 |
0 |
0 |
|
| 743 |
0 |
▶ | if(xnp < 30.0) goto S150; |
Branch: 1 branch
| Branch | True | False | Total |
| if(xnp < 30.0) goto S150; |
0 |
0 |
0 |
|
| 744 |
0 |
| goto S30; |
| 745 |
0 |
| S10: |
| 746 |
|
| /* |
| 747 |
|
| *****SETUP, PERFORM ONLY WHEN PARAMETERS CHANGE |
| 748 |
|
| */ |
| 749 |
0 |
| psave = pp; |
| 750 |
0 |
▶ | p = min(psave,1.0-psave); |
Branch: 1 branch
| Branch | True | False | Total |
| p = min(psave,1.0-psave); |
0 |
0 |
0 |
|
| 751 |
0 |
| q = 1.0-p; |
| 752 |
0 |
| S20: |
| 753 |
0 |
| xnp = n*p; |
| 754 |
0 |
| nsave = n; |
| 755 |
0 |
▶ | if(xnp < 30.0) goto S140; |
Branch: 1 branch
| Branch | True | False | Total |
| if(xnp < 30.0) goto S140; |
0 |
0 |
0 |
|
| 756 |
0 |
| ffm = xnp+p; |
| 757 |
0 |
| m = ffm; |
| 758 |
0 |
| fm = m; |
| 759 |
0 |
| xnpq = xnp*q; |
| 760 |
0 |
| p1 = (long) (2.195*sqrt(xnpq)-4.6*q)+0.5; |
| 761 |
0 |
| xm = fm+0.5; |
| 762 |
0 |
| xl = xm-p1; |
| 763 |
0 |
| xr = xm+p1; |
| 764 |
0 |
| c = 0.134+20.5/(15.3+fm); |
| 765 |
0 |
| al = (ffm-xl)/(ffm-xl*p); |
| 766 |
0 |
| xll = al*(1.0+0.5*al); |
| 767 |
0 |
| al = (xr-ffm)/(xr*q); |
| 768 |
0 |
| xlr = al*(1.0+0.5*al); |
| 769 |
0 |
| p2 = p1*(1.0+c+c); |
| 770 |
0 |
| p3 = p2+c/xll; |
| 771 |
0 |
| p4 = p3+c/xlr; |
| 772 |
0 |
| S30: |
| 773 |
|
| /* |
| 774 |
|
| *****GENERATE VARIATE |
| 775 |
|
| */ |
| 776 |
0 |
| u = ranf()*p4; |
| 777 |
0 |
| v = ranf(); |
| 778 |
|
| /* |
| 779 |
|
| TRIANGULAR REGION |
| 780 |
|
| */ |
| 781 |
0 |
▶ | if(u > p1) goto S40; |
Branch: 1 branch
| Branch | True | False | Total |
| if(u > p1) goto S40; |
0 |
0 |
0 |
|
| 782 |
0 |
| ix = xm-p1*v+u; |
| 783 |
0 |
| goto S170; |
| 784 |
0 |
| S40: |
| 785 |
|
| /* |
| 786 |
|
| PARALLELOGRAM REGION |
| 787 |
|
| */ |
| 788 |
0 |
▶ | if(u > p2) goto S50; |
Branch: 1 branch
| Branch | True | False | Total |
| if(u > p2) goto S50; |
0 |
0 |
0 |
|
| 789 |
0 |
| x = xl+(u-p1)/c; |
| 790 |
0 |
▶ | v = v*c+1.0-ABS(xm-x)/p1; |
Branch: 1 branch
| Branch | True | False | Total |
| v = v*c+1.0-ABS(xm-x)/p1; |
0 |
0 |
0 |
|
| 791 |
0 |
▶ | if(v > 1.0 || v <= 0.0) goto S30; |
Branch: 2 branches
| Branch | True | False | Total |
| if(v > 1.0 || v <= 0.0) goto S30; |
0 |
0 |
0 |
| if(v > 1.0 || v <= 0.0) goto S30; |
0 |
0 |
0 |
|
| 792 |
0 |
| ix = x; |
| 793 |
0 |
| goto S70; |
| 794 |
0 |
| S50: |
| 795 |
|
| /* |
| 796 |
|
| LEFT TAIL |
| 797 |
|
| */ |
| 798 |
0 |
▶ | if(u > p3) goto S60; |
Branch: 1 branch
| Branch | True | False | Total |
| if(u > p3) goto S60; |
0 |
0 |
0 |
|
| 799 |
0 |
| ix = xl+log(v)/xll; |
| 800 |
0 |
▶ | if(ix < 0) goto S30; |
Branch: 1 branch
| Branch | True | False | Total |
| if(ix < 0) goto S30; |
0 |
0 |
0 |
|
| 801 |
0 |
| v *= ((u-p2)*xll); |
| 802 |
0 |
| goto S70; |
| 803 |
0 |
| S60: |
| 804 |
|
| /* |
| 805 |
|
| RIGHT TAIL |
| 806 |
|
| */ |
| 807 |
0 |
| ix = xr-log(v)/xlr; |
| 808 |
0 |
▶ | if(ix > n) goto S30; |
Branch: 1 branch
| Branch | True | False | Total |
| if(ix > n) goto S30; |
0 |
0 |
0 |
|
| 809 |
0 |
| v *= ((u-p3)*xlr); |
| 810 |
0 |
| S70: |
| 811 |
|
| /* |
| 812 |
|
| *****DETERMINE APPROPRIATE WAY TO PERFORM ACCEPT/REJECT TEST |
| 813 |
|
| */ |
| 814 |
0 |
| k = ABS(ix-m); |
| 815 |
0 |
▶ | if(k > 20 && k < xnpq/2-1) goto S130; |
Branch: 2 branches
| Branch | True | False | Total |
| if(k > 20 && k < xnpq/2-1) goto S130; |
0 |
0 |
0 |
| if(k > 20 && k < xnpq/2-1) goto S130; |
0 |
0 |
0 |
|
| 816 |
|
| /* |
| 817 |
|
| EXPLICIT EVALUATION |
| 818 |
|
| */ |
| 819 |
0 |
| f = 1.0; |
| 820 |
0 |
| r = p/q; |
| 821 |
0 |
| g = (n+1)*r; |
| 822 |
0 |
| T1 = m-ix; |
| 823 |
0 |
▶ | if(T1 < 0) goto S80; |
Branch: 1 branch
| Branch | True | False | Total |
| if(T1 < 0) goto S80; |
0 |
0 |
0 |
|
| 824 |
0 |
▶ | else if(T1 == 0) goto S120; |
Branch: 1 branch
| Branch | True | False | Total |
| else if(T1 == 0) goto S120; |
0 |
0 |
0 |
|
| 825 |
0 |
| else goto S100; |
| 826 |
0 |
| S80: |
| 827 |
0 |
| mp = m+1; |
| 828 |
0 |
▶ | for(i=mp; i<=ix; i++) f *= (g/i-r); |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=mp; i<=ix; i++) f *= (g/i-r); |
0 |
0 |
0 |
|
| 829 |
0 |
| goto S120; |
| 830 |
0 |
| S100: |
| 831 |
0 |
| ix1 = ix+1; |
| 832 |
0 |
▶ | for(i=ix1; i<=m; i++) f /= (g/i-r); |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=ix1; i<=m; i++) f /= (g/i-r); |
0 |
0 |
0 |
|
| 833 |
0 |
| S120: |
| 834 |
0 |
▶ | if(v <= f) goto S170; |
Branch: 1 branch
| Branch | True | False | Total |
| if(v <= f) goto S170; |
0 |
0 |
0 |
|
| 835 |
0 |
| goto S30; |
| 836 |
0 |
| S130: |
| 837 |
|
| /* |
| 838 |
|
| SQUEEZING USING UPPER AND LOWER BOUNDS ON ALOG(F(X)) |
| 839 |
|
| */ |
| 840 |
0 |
| amaxp = k/xnpq*((k*(k/3.0+0.625)+0.1666666666666)/xnpq+0.5); |
| 841 |
0 |
| ynorm = -(k*k/(2.0*xnpq)); |
| 842 |
0 |
| alv = log(v); |
| 843 |
0 |
▶ | if(alv < ynorm-amaxp) goto S170; |
Branch: 1 branch
| Branch | True | False | Total |
| if(alv < ynorm-amaxp) goto S170; |
0 |
0 |
0 |
|
| 844 |
0 |
▶ | if(alv > ynorm+amaxp) goto S30; |
Branch: 1 branch
| Branch | True | False | Total |
| if(alv > ynorm+amaxp) goto S30; |
0 |
0 |
0 |
|
| 845 |
|
| /* |
| 846 |
|
| STIRLING'S FORMULA TO MACHINE ACCURACY FOR |
| 847 |
|
| THE FINAL ACCEPTANCE/REJECTION TEST |
| 848 |
|
| */ |
| 849 |
0 |
| x1 = ix+1.0; |
| 850 |
0 |
| f1 = fm+1.0; |
| 851 |
0 |
| z = n+1.0-fm; |
| 852 |
0 |
| w = n-ix+1.0; |
| 853 |
0 |
| z2 = z*z; |
| 854 |
0 |
| x2 = x1*x1; |
| 855 |
0 |
| f2 = f1*f1; |
| 856 |
0 |
| w2 = w*w; |
| 857 |
0 |
| if(alv <= xm*log(f1/x1)+(n-m+0.5)*log(z/w)+(ix-m)*log(w*p/(x1*q))+(13860.0- |
| 858 |
0 |
| (462.0-(132.0-(99.0-140.0/f2)/f2)/f2)/f2)/f1/166320.0+(13860.0-(462.0- |
| 859 |
0 |
| (132.0-(99.0-140.0/z2)/z2)/z2)/z2)/z/166320.0+(13860.0-(462.0-(132.0- |
| 860 |
0 |
| (99.0-140.0/x2)/x2)/x2)/x2)/x1/166320.0+(13860.0-(462.0-(132.0-(99.0 |
| 861 |
0 |
▶ | -140.0/w2)/w2)/w2)/w2)/w/166320.0) goto S170; |
Branch: 1 branch
| Branch | True | False | Total |
| -140.0/w2)/w2)/w2)/w2)/w/166320.0) goto S170; |
0 |
0 |
0 |
|
| 862 |
0 |
| goto S30; |
| 863 |
0 |
| S140: |
| 864 |
|
| /* |
| 865 |
|
| INVERSE CDF LOGIC FOR MEAN LESS THAN 30 |
| 866 |
|
| */ |
| 867 |
0 |
| qn = pow(q,(double)n); |
| 868 |
0 |
| r = p/q; |
| 869 |
0 |
| g = r*(n+1); |
| 870 |
0 |
| S150: |
| 871 |
0 |
| ix = 0; |
| 872 |
0 |
| f = qn; |
| 873 |
0 |
| u = ranf(); |
| 874 |
0 |
| S160: |
| 875 |
0 |
▶ | if(u < f) goto S170; |
Branch: 1 branch
| Branch | True | False | Total |
| if(u < f) goto S170; |
0 |
0 |
0 |
|
| 876 |
0 |
▶ | if(ix > 110) goto S150; |
Branch: 1 branch
| Branch | True | False | Total |
| if(ix > 110) goto S150; |
0 |
0 |
0 |
|
| 877 |
0 |
| u -= f; |
| 878 |
0 |
| ix += 1; |
| 879 |
0 |
| f *= (g/ix-r); |
| 880 |
0 |
| goto S160; |
| 881 |
0 |
| S170: |
| 882 |
0 |
▶ | if(psave > 0.5) ix = n-ix; |
Branch: 1 branch
| Branch | True | False | Total |
| if(psave > 0.5) ix = n-ix; |
0 |
0 |
0 |
|
| 883 |
0 |
| ignbin = ix; |
| 884 |
0 |
| return ignbin; |
| 885 |
|
| } |
| 886 |
0 |
| long ignnbn(long n,float p) |
| 887 |
|
| /* |
| 888 |
|
| ********************************************************************** |
| 889 |
|
| |
| 890 |
|
| long ignnbn(long n,float p) |
| 891 |
|
| GENerate Negative BiNomial random deviate |
| 892 |
|
| Function |
| 893 |
|
| Generates a single random deviate from a negative binomial |
| 894 |
|
| distribution. |
| 895 |
|
| Arguments |
| 896 |
|
| N --> The number of trials in the negative binomial distribution |
| 897 |
|
| from which a random deviate is to be generated. |
| 898 |
|
| P --> The probability of an event. |
| 899 |
|
| Method |
| 900 |
|
| Algorithm from page 480 of |
| 901 |
|
| |
| 902 |
|
| Devroye, Luc |
| 903 |
|
| |
| 904 |
|
| Non-Uniform Random Variate Generation. Springer-Verlag, |
| 905 |
|
| New York, 1986. |
| 906 |
|
| ********************************************************************** |
| 907 |
|
| */ |
| 908 |
|
| { |
| 909 |
|
| static long ignnbn; |
| 910 |
|
| static float y,a,r; |
| 911 |
|
| /* |
| 912 |
|
| .. |
| 913 |
|
| .. Executable Statements .. |
| 914 |
|
| */ |
| 915 |
|
| /* |
| 916 |
|
| Check Arguments |
| 917 |
|
| */ |
| 918 |
0 |
▶ | if(n < 0) ftnstop("N < 0 in IGNNBN"); |
Branch: 1 branch
| Branch | True | False | Total |
| if(n < 0) ftnstop("N < 0 in IGNNBN"); |
0 |
0 |
0 |
|
| 919 |
0 |
▶ | if(p <= 0.0F) ftnstop("P <= 0 in IGNNBN"); |
Branch: 1 branch
| Branch | True | False | Total |
| if(p <= 0.0F) ftnstop("P <= 0 in IGNNBN"); |
0 |
0 |
0 |
|
| 920 |
0 |
▶ | if(p >= 1.0F) ftnstop("P >= 1 in IGNNBN"); |
Branch: 1 branch
| Branch | True | False | Total |
| if(p >= 1.0F) ftnstop("P >= 1 in IGNNBN"); |
0 |
0 |
0 |
|
| 921 |
|
| /* |
| 922 |
|
| Generate Y, a random gamma (n,(1-p)/p) variable |
| 923 |
|
| */ |
| 924 |
0 |
| r = (float)n; |
| 925 |
0 |
| a = p/(1.0F-p); |
| 926 |
0 |
| y = gengam(a,r); |
| 927 |
|
| /* |
| 928 |
|
| Generate a random Poisson(y) variable |
| 929 |
|
| */ |
| 930 |
0 |
| ignnbn = ignpoi(y); |
| 931 |
0 |
| return ignnbn; |
| 932 |
|
| } |
| 933 |
0 |
| long ignpoi(float mu) |
| 934 |
|
| /* |
| 935 |
|
| ********************************************************************** |
| 936 |
|
| long ignpoi(float mu) |
| 937 |
|
| GENerate POIsson random deviate |
| 938 |
|
| Function |
| 939 |
|
| Generates a single random deviate from a Poisson |
| 940 |
|
| distribution with mean AV. |
| 941 |
|
| Arguments |
| 942 |
|
| av --> The mean of the Poisson distribution from which |
| 943 |
|
| a random deviate is to be generated. |
| 944 |
|
| genexp <-- The random deviate. |
| 945 |
|
| Method |
| 946 |
|
| Renames KPOIS from TOMS as slightly modified by BWB to use RANF |
| 947 |
|
| instead of SUNIF. |
| 948 |
|
| For details see: |
| 949 |
|
| Ahrens, J.H. and Dieter, U. |
| 950 |
|
| Computer Generation of Poisson Deviates |
| 951 |
|
| From Modified Normal Distributions. |
| 952 |
|
| ACM Trans. Math. Software, 8, 2 |
| 953 |
|
| (June 1982),163-179 |
| 954 |
|
| ********************************************************************** |
| 955 |
|
| ********************************************************************** |
| 956 |
|
| |
| 957 |
|
| |
| 958 |
|
| P O I S S O N DISTRIBUTION |
| 959 |
|
| |
| 960 |
|
| |
| 961 |
|
| ********************************************************************** |
| 962 |
|
| ********************************************************************** |
| 963 |
|
| |
| 964 |
|
| FOR DETAILS SEE: |
| 965 |
|
| |
| 966 |
|
| AHRENS, J.H. AND DIETER, U. |
| 967 |
|
| COMPUTER GENERATION OF POISSON DEVIATES |
| 968 |
|
| FROM MODIFIED NORMAL DISTRIBUTIONS. |
| 969 |
|
| ACM TRANS. MATH. SOFTWARE, 8,2 (JUNE 1982), 163 - 179. |
| 970 |
|
| |
| 971 |
|
| (SLIGHTLY MODIFIED VERSION OF THE PROGRAM IN THE ABOVE ARTICLE) |
| 972 |
|
| |
| 973 |
|
| ********************************************************************** |
| 974 |
|
| INTEGER FUNCTION IGNPOI(IR,MU) |
| 975 |
|
| INPUT: IR=CURRENT STATE OF BASIC RANDOM NUMBER GENERATOR |
| 976 |
|
| MU=MEAN MU OF THE POISSON DISTRIBUTION |
| 977 |
|
| OUTPUT: IGNPOI=SAMPLE FROM THE POISSON-(MU)-DISTRIBUTION |
| 978 |
|
| MUPREV=PREVIOUS MU, MUOLD=MU AT LAST EXECUTION OF STEP P OR B. |
| 979 |
|
| TABLES: COEFFICIENTS A0-A7 FOR STEP F. FACTORIALS FACT |
| 980 |
|
| COEFFICIENTS A(K) - FOR PX = FK*V*V*SUM(A(K)*V**K)-DEL |
| 981 |
|
| SEPARATION OF CASES A AND B |
| 982 |
|
| */ |
| 983 |
|
| { |
| 984 |
|
| extern float fsign( float num, float sign ); |
| 985 |
|
| static float a0 = -0.5; |
| 986 |
|
| static float a1 = 0.3333333; |
| 987 |
|
| static float a2 = -0.2500068; |
| 988 |
|
| static float a3 = 0.2000118; |
| 989 |
|
| static float a4 = -0.1661269; |
| 990 |
|
| static float a5 = 0.1421878; |
| 991 |
|
| static float a6 = -0.1384794; |
| 992 |
|
| static float a7 = 0.125006; |
| 993 |
|
| static float muold = 0.0; |
| 994 |
|
| static float muprev = 0.0; |
| 995 |
|
| static float fact[10] = { |
| 996 |
|
| 1.0,1.0,2.0,6.0,24.0,120.0,720.0,5040.0,40320.0,362880.0 |
| 997 |
|
| }; |
| 998 |
|
| static long ignpoi,j,k,kflag,l,m; |
| 999 |
|
| static float b1,b2,c,c0,c1,c2,c3,d,del,difmuk,e,fk,fx,fy,g,omega,p,p0,px,py,q,s, |
| 1000 |
|
| t,u,v,x,xx,pp[35]; |
| 1001 |
|
| |
| 1002 |
0 |
▶ | if(mu == muprev) goto S10; |
Branch: 1 branch
| Branch | True | False | Total |
| if(mu == muprev) goto S10; |
0 |
0 |
0 |
|
| 1003 |
0 |
▶ | if(mu < 10.0) goto S120; |
Branch: 1 branch
| Branch | True | False | Total |
| if(mu < 10.0) goto S120; |
0 |
0 |
0 |
|
| 1004 |
|
| /* |
| 1005 |
|
| C A S E A. (RECALCULATION OF S,D,L IF MU HAS CHANGED) |
| 1006 |
|
| */ |
| 1007 |
0 |
| muprev = mu; |
| 1008 |
0 |
| s = sqrt(mu); |
| 1009 |
0 |
| d = 6.0*mu*mu; |
| 1010 |
|
| /* |
| 1011 |
|
| THE POISSON PROBABILITIES PK EXCEED THE DISCRETE NORMAL |
| 1012 |
|
| PROBABILITIES FK WHENEVER K >= M(MU). L=IFIX(MU-1.1484) |
| 1013 |
|
| IS AN UPPER BOUND TO M(MU) FOR ALL MU >= 10 . |
| 1014 |
|
| */ |
| 1015 |
0 |
| l = (long) (mu-1.1484); |
| 1016 |
0 |
| S10: |
| 1017 |
|
| /* |
| 1018 |
|
| STEP N. NORMAL SAMPLE - SNORM(IR) FOR STANDARD NORMAL DEVIATE |
| 1019 |
|
| */ |
| 1020 |
0 |
| g = mu+s*snorm(); |
| 1021 |
0 |
▶ | if(g < 0.0) goto S20; |
Branch: 1 branch
| Branch | True | False | Total |
| if(g < 0.0) goto S20; |
0 |
0 |
0 |
|
| 1022 |
0 |
| ignpoi = (long) (g); |
| 1023 |
|
| /* |
| 1024 |
|
| STEP I. IMMEDIATE ACCEPTANCE IF IGNPOI IS LARGE ENOUGH |
| 1025 |
|
| */ |
| 1026 |
0 |
▶ | if(ignpoi >= l) return ignpoi; |
Branch: 1 branch
| Branch | True | False | Total |
| if(ignpoi >= l) return ignpoi; |
0 |
0 |
0 |
|
| 1027 |
|
| /* |
| 1028 |
|
| STEP S. SQUEEZE ACCEPTANCE - SUNIF(IR) FOR (0,1)-SAMPLE U |
| 1029 |
|
| */ |
| 1030 |
0 |
| fk = (float)ignpoi; |
| 1031 |
0 |
| difmuk = mu-fk; |
| 1032 |
0 |
| u = ranf(); |
| 1033 |
0 |
▶ | if(d*u >= difmuk*difmuk*difmuk) return ignpoi; |
Branch: 1 branch
| Branch | True | False | Total |
| if(d*u >= difmuk*difmuk*difmuk) return ignpoi; |
0 |
0 |
0 |
|
| 1034 |
0 |
| S20: |
| 1035 |
|
| /* |
| 1036 |
|
| STEP P. PREPARATIONS FOR STEPS Q AND H. |
| 1037 |
|
| (RECALCULATIONS OF PARAMETERS IF NECESSARY) |
| 1038 |
|
| .3989423=(2*PI)**(-.5) .416667E-1=1./24. .1428571=1./7. |
| 1039 |
|
| THE QUANTITIES B1, B2, C3, C2, C1, C0 ARE FOR THE HERMITE |
| 1040 |
|
| APPROXIMATIONS TO THE DISCRETE NORMAL PROBABILITIES FK. |
| 1041 |
|
| C=.1069/MU GUARANTEES MAJORIZATION BY THE 'HAT'-FUNCTION. |
| 1042 |
|
| */ |
| 1043 |
0 |
▶ | if(mu == muold) goto S30; |
Branch: 1 branch
| Branch | True | False | Total |
| if(mu == muold) goto S30; |
0 |
0 |
0 |
|
| 1044 |
0 |
| muold = mu; |
| 1045 |
0 |
| omega = 0.3989423/s; |
| 1046 |
0 |
| b1 = 4.166667E-2/mu; |
| 1047 |
0 |
| b2 = 0.3*b1*b1; |
| 1048 |
0 |
| c3 = 0.1428571*b1*b2; |
| 1049 |
0 |
| c2 = b2-15.0*c3; |
| 1050 |
0 |
| c1 = b1-6.0*b2+45.0*c3; |
| 1051 |
0 |
| c0 = 1.0-b1+3.0*b2-15.0*c3; |
| 1052 |
0 |
| c = 0.1069/mu; |
| 1053 |
0 |
| S30: |
| 1054 |
0 |
▶ | if(g < 0.0) goto S50; |
Branch: 1 branch
| Branch | True | False | Total |
| if(g < 0.0) goto S50; |
0 |
0 |
0 |
|
| 1055 |
|
| /* |
| 1056 |
|
| 'SUBROUTINE' F IS CALLED (KFLAG=0 FOR CORRECT RETURN) |
| 1057 |
|
| */ |
| 1058 |
0 |
| kflag = 0; |
| 1059 |
0 |
| goto S70; |
| 1060 |
0 |
| S40: |
| 1061 |
|
| /* |
| 1062 |
|
| STEP Q. QUOTIENT ACCEPTANCE (RARE CASE) |
| 1063 |
|
| */ |
| 1064 |
0 |
▶ | if(fy-u*fy <= py*exp(px-fx)) return ignpoi; |
Branch: 1 branch
| Branch | True | False | Total |
| if(fy-u*fy <= py*exp(px-fx)) return ignpoi; |
0 |
0 |
0 |
|
| 1065 |
0 |
| S50: |
| 1066 |
|
| /* |
| 1067 |
|
| STEP E. EXPONENTIAL SAMPLE - SEXPO(IR) FOR STANDARD EXPONENTIAL |
| 1068 |
|
| DEVIATE E AND SAMPLE T FROM THE LAPLACE 'HAT' |
| 1069 |
|
| (IF T <= -.6744 THEN PK < FK FOR ALL MU >= 10.) |
| 1070 |
|
| */ |
| 1071 |
0 |
| e = sexpo(); |
| 1072 |
0 |
| u = ranf(); |
| 1073 |
0 |
| u += (u-1.0); |
| 1074 |
0 |
| t = 1.8+fsign(e,u); |
| 1075 |
0 |
▶ | if(t <= -0.6744) goto S50; |
Branch: 1 branch
| Branch | True | False | Total |
| if(t <= -0.6744) goto S50; |
0 |
0 |
0 |
|
| 1076 |
0 |
| ignpoi = (long) (mu+s*t); |
| 1077 |
0 |
| fk = (float)ignpoi; |
| 1078 |
0 |
| difmuk = mu-fk; |
| 1079 |
|
| /* |
| 1080 |
|
| 'SUBROUTINE' F IS CALLED (KFLAG=1 FOR CORRECT RETURN) |
| 1081 |
|
| */ |
| 1082 |
0 |
| kflag = 1; |
| 1083 |
0 |
| goto S70; |
| 1084 |
0 |
| S60: |
| 1085 |
|
| /* |
| 1086 |
|
| STEP H. HAT ACCEPTANCE (E IS REPEATED ON REJECTION) |
| 1087 |
|
| */ |
| 1088 |
0 |
▶ | if(c*fabs(u) > py*exp(px+e)-fy*exp(fx+e)) goto S50; |
Branch: 1 branch
| Branch | True | False | Total |
| if(c*fabs(u) > py*exp(px+e)-fy*exp(fx+e)) goto S50; |
0 |
0 |
0 |
|
| 1089 |
0 |
| return ignpoi; |
| 1090 |
0 |
| S70: |
| 1091 |
|
| /* |
| 1092 |
|
| STEP F. 'SUBROUTINE' F. CALCULATION OF PX,PY,FX,FY. |
| 1093 |
|
| CASE IGNPOI .LT. 10 USES FACTORIALS FROM TABLE FACT |
| 1094 |
|
| */ |
| 1095 |
0 |
▶ | if(ignpoi >= 10) goto S80; |
Branch: 1 branch
| Branch | True | False | Total |
| if(ignpoi >= 10) goto S80; |
0 |
0 |
0 |
|
| 1096 |
0 |
| px = -mu; |
| 1097 |
0 |
| py = pow(mu,(double)ignpoi)/ *(fact+ignpoi); |
| 1098 |
0 |
| goto S110; |
| 1099 |
0 |
| S80: |
| 1100 |
|
| /* |
| 1101 |
|
| CASE IGNPOI .GE. 10 USES POLYNOMIAL APPROXIMATION |
| 1102 |
|
| A0-A7 FOR ACCURACY WHEN ADVISABLE |
| 1103 |
|
| .8333333E-1=1./12. .3989423=(2*PI)**(-.5) |
| 1104 |
|
| */ |
| 1105 |
0 |
| del = 8.333333E-2/fk; |
| 1106 |
0 |
| del -= (4.8*del*del*del); |
| 1107 |
0 |
| v = difmuk/fk; |
| 1108 |
0 |
▶ | if(fabs(v) <= 0.25) goto S90; |
Branch: 1 branch
| Branch | True | False | Total |
| if(fabs(v) <= 0.25) goto S90; |
0 |
0 |
0 |
|
| 1109 |
0 |
| px = fk*log(1.0+v)-difmuk-del; |
| 1110 |
0 |
| goto S100; |
| 1111 |
0 |
| S90: |
| 1112 |
0 |
| px = fk*v*v*(((((((a7*v+a6)*v+a5)*v+a4)*v+a3)*v+a2)*v+a1)*v+a0)-del; |
| 1113 |
0 |
| S100: |
| 1114 |
0 |
| py = 0.3989423/sqrt(fk); |
| 1115 |
0 |
| S110: |
| 1116 |
0 |
| x = (0.5-difmuk)/s; |
| 1117 |
0 |
| xx = x*x; |
| 1118 |
0 |
| fx = -0.5*xx; |
| 1119 |
0 |
| fy = omega*(((c3*xx+c2)*xx+c1)*xx+c0); |
| 1120 |
0 |
▶ | if(kflag <= 0) goto S40; |
Branch: 1 branch
| Branch | True | False | Total |
| if(kflag <= 0) goto S40; |
0 |
0 |
0 |
|
| 1121 |
0 |
| goto S60; |
| 1122 |
0 |
| S120: |
| 1123 |
|
| /* |
| 1124 |
|
| C A S E B. (START NEW TABLE AND CALCULATE P0 IF NECESSARY) |
| 1125 |
|
| */ |
| 1126 |
0 |
| muprev = 0.0; |
| 1127 |
0 |
▶ | if(mu == muold) goto S130; |
Branch: 1 branch
| Branch | True | False | Total |
| if(mu == muold) goto S130; |
0 |
0 |
0 |
|
| 1128 |
0 |
| muold = mu; |
| 1129 |
0 |
| m = max(1L,(long) (mu)); |
| 1130 |
0 |
| l = 0; |
| 1131 |
0 |
| p = exp(-mu); |
| 1132 |
0 |
| q = p0 = p; |
| 1133 |
0 |
| S130: |
| 1134 |
|
| /* |
| 1135 |
|
| STEP U. UNIFORM SAMPLE FOR INVERSION METHOD |
| 1136 |
|
| */ |
| 1137 |
0 |
| u = ranf(); |
| 1138 |
0 |
| ignpoi = 0; |
| 1139 |
0 |
▶ | if(u <= p0) return ignpoi; |
Branch: 1 branch
| Branch | True | False | Total |
| if(u <= p0) return ignpoi; |
0 |
0 |
0 |
|
| 1140 |
|
| /* |
| 1141 |
|
| STEP T. TABLE COMPARISON UNTIL THE END PP(L) OF THE |
| 1142 |
|
| PP-TABLE OF CUMULATIVE POISSON PROBABILITIES |
| 1143 |
|
| (0.458=PP(9) FOR MU=10) |
| 1144 |
|
| */ |
| 1145 |
0 |
▶ | if(l == 0) goto S150; |
Branch: 1 branch
| Branch | True | False | Total |
| if(l == 0) goto S150; |
0 |
0 |
0 |
|
| 1146 |
0 |
| j = 1; |
| 1147 |
0 |
▶ | if(u > 0.458) j = min(l,m); |
Branch: 1 branch
| Branch | True | False | Total |
| if(u > 0.458) j = min(l,m); |
0 |
0 |
0 |
|
| 1148 |
0 |
▶ | for(k=j; k<=l; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(k=j; k<=l; k++) { |
0 |
0 |
0 |
|
| 1149 |
0 |
▶ | if(u <= *(pp+k-1)) goto S180; |
Branch: 1 branch
| Branch | True | False | Total |
| if(u <= *(pp+k-1)) goto S180; |
0 |
0 |
0 |
|
| 1150 |
|
| } |
| 1151 |
0 |
▶ | if(l == 35) goto S130; |
Branch: 1 branch
| Branch | True | False | Total |
| if(l == 35) goto S130; |
0 |
0 |
0 |
|
| 1152 |
0 |
| S150: |
| 1153 |
|
| /* |
| 1154 |
|
| STEP C. CREATION OF NEW POISSON PROBABILITIES P |
| 1155 |
|
| AND THEIR CUMULATIVES Q=PP(K) |
| 1156 |
|
| */ |
| 1157 |
0 |
| l += 1; |
| 1158 |
0 |
▶ | for(k=l; k<=35; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(k=l; k<=35; k++) { |
0 |
0 |
0 |
|
| 1159 |
0 |
| p = p*mu/(float)k; |
| 1160 |
0 |
| q += p; |
| 1161 |
0 |
| *(pp+k-1) = q; |
| 1162 |
0 |
▶ | if(u <= q) goto S170; |
Branch: 1 branch
| Branch | True | False | Total |
| if(u <= q) goto S170; |
0 |
0 |
0 |
|
| 1163 |
|
| } |
| 1164 |
0 |
| l = 35; |
| 1165 |
0 |
| goto S130; |
| 1166 |
0 |
| S170: |
| 1167 |
0 |
| l = k; |
| 1168 |
0 |
| S180: |
| 1169 |
0 |
| ignpoi = k; |
| 1170 |
0 |
| return ignpoi; |
| 1171 |
|
| } |
| 1172 |
0 |
| long ignuin(long low,long high) |
| 1173 |
|
| /* |
| 1174 |
|
| ********************************************************************** |
| 1175 |
|
| long ignuin(long low,long high) |
| 1176 |
|
| GeNerate Uniform INteger |
| 1177 |
|
| Function |
| 1178 |
|
| Generates an integer uniformly distributed between LOW and HIGH. |
| 1179 |
|
| Arguments |
| 1180 |
|
| low --> Low bound (inclusive) on integer value to be generated |
| 1181 |
|
| high --> High bound (inclusive) on integer value to be generated |
| 1182 |
|
| Note |
| 1183 |
|
| If (HIGH-LOW) > 2,147,483,561 prints error message on * unit and |
| 1184 |
|
| stops the program. |
| 1185 |
|
| ********************************************************************** |
| 1186 |
|
| IGNLGI generates integers between 1 and 2147483562 |
| 1187 |
|
| MAXNUM is 1 less than maximum generable value |
| 1188 |
|
| */ |
| 1189 |
|
| { |
| 1190 |
|
| #define maxnum 2147483561L |
| 1191 |
|
| static long ignuin,ign,maxnow,range,ranp1; |
| 1192 |
|
| |
| 1193 |
0 |
▶ | if(!(low > high)) goto S10; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(low > high)) goto S10; |
0 |
0 |
0 |
|
| 1194 |
0 |
| fputs(" low > high in ignuin - ABORT",stderr); |
| 1195 |
0 |
| exit(1); |
| 1196 |
|
| |
| 1197 |
0 |
| S10: |
| 1198 |
0 |
| range = high-low; |
| 1199 |
0 |
▶ | if(!(range > maxnum)) goto S20; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(range > maxnum)) goto S20; |
0 |
0 |
0 |
|
| 1200 |
0 |
| fputs(" high - low too large in ignuin - ABORT",stderr); |
| 1201 |
0 |
| exit(1); |
| 1202 |
|
| |
| 1203 |
0 |
| S20: |
| 1204 |
0 |
▶ | if(!(low == high)) goto S30; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(low == high)) goto S30; |
0 |
0 |
0 |
|
| 1205 |
0 |
| ignuin = low; |
| 1206 |
0 |
| return ignuin; |
| 1207 |
|
| |
| 1208 |
0 |
| S30: |
| 1209 |
|
| /* |
| 1210 |
|
| Number to be generated should be in range 0..RANGE |
| 1211 |
|
| Set MAXNOW so that the number of integers in 0..MAXNOW is an |
| 1212 |
|
| integral multiple of the number in 0..RANGE |
| 1213 |
|
| */ |
| 1214 |
0 |
| ranp1 = range+1; |
| 1215 |
0 |
| maxnow = maxnum/ranp1*ranp1; |
| 1216 |
0 |
| S40: |
| 1217 |
0 |
| ign = ignlgi()-1; |
| 1218 |
0 |
▶ | if(!(ign <= maxnow)) goto S50; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(ign <= maxnow)) goto S50; |
0 |
0 |
0 |
|
| 1219 |
0 |
| ignuin = low+ign%ranp1; |
| 1220 |
0 |
| return ignuin; |
| 1221 |
0 |
| S50: |
| 1222 |
0 |
| goto S40; |
| 1223 |
|
| #undef maxnum |
| 1224 |
|
| #undef err1 |
| 1225 |
|
| #undef err2 |
| 1226 |
|
| } |
| 1227 |
0 |
| long lennob( char *str ) |
| 1228 |
|
| /* |
| 1229 |
|
| Returns the length of str ignoring trailing blanks but not |
| 1230 |
|
| other white space. |
| 1231 |
|
| */ |
| 1232 |
|
| { |
| 1233 |
|
| long i, i_nb; |
| 1234 |
|
| |
| 1235 |
0 |
▶ | for (i=0, i_nb= -1L; *(str+i); i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0, i_nb= -1L; *(str+i); i++) |
0 |
0 |
0 |
|
| 1236 |
0 |
▶ | if ( *(str+i) != ' ' ) i_nb = i; |
Branch: 1 branch
| Branch | True | False | Total |
| if ( *(str+i) != ' ' ) i_nb = i; |
0 |
0 |
0 |
|
| 1237 |
0 |
| return (i_nb+1); |
| 1238 |
|
| } |
| 1239 |
310 |
| long mltmod(long a,long s,long m) |
| 1240 |
|
| /* |
| 1241 |
|
| ********************************************************************** |
| 1242 |
|
| long mltmod(long a,long s,long m) |
| 1243 |
|
| Returns (A*S) MOD M |
| 1244 |
|
| This is a transcription from Pascal to Fortran of routine |
| 1245 |
|
| MULtMod_Decompos from the paper |
| 1246 |
|
| L'Ecuyer, P. and Cote, S. "Implementing a Random Number Package |
| 1247 |
|
| with Splitting Facilities." ACM Transactions on Mathematical |
| 1248 |
|
| Software, 17:98-111 (1991) |
| 1249 |
|
| Arguments |
| 1250 |
|
| a, s, m --> |
| 1251 |
|
| ********************************************************************** |
| 1252 |
|
| */ |
| 1253 |
|
| { |
| 1254 |
|
| #define h 32768L |
| 1255 |
|
| static long mltmod,a0,a1,k,p,q,qh,rh; |
| 1256 |
|
| /* |
| 1257 |
|
| H = 2**((b-2)/2) where b = 32 because we are using a 32 bit |
| 1258 |
|
| machine. On a different machine recompute H |
| 1259 |
|
| */ |
| 1260 |
310 |
▶ | if(!(a <= 0 || a >= m || s <= 0 || s >= m)) goto S10; |
Branch: 4 branches
| Branch | True | False | Total |
| if(!(a <= 0 || a >= m || s <= 0 || s >= m)) goto S10; |
310 |
0 |
310 |
| if(!(a <= 0 || a >= m || s <= 0 || s >= m)) goto S10; |
310 |
0 |
310 |
| if(!(a <= 0 || a >= m || s <= 0 || s >= m)) goto S10; |
310 |
0 |
310 |
| if(!(a <= 0 || a >= m || s <= 0 || s >= m)) goto S10; |
310 |
0 |
310 |
|
| 1261 |
0 |
| fputs(" a, m, s out of order in mltmod - ABORT!",stderr); |
| 1262 |
0 |
| fprintf(stderr," a = %12ld s = %12ld m = %12ld\n",a,s,m); |
| 1263 |
0 |
| fputs(" mltmod requires: 0 < a < m; 0 < s < m",stderr); |
| 1264 |
0 |
| exit(1); |
| 1265 |
310 |
| S10: |
| 1266 |
310 |
▶ | if(!(a < h)) goto S20; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(a < h)) goto S20; |
310 |
0 |
310 |
|
| 1267 |
0 |
| a0 = a; |
| 1268 |
0 |
| p = 0; |
| 1269 |
0 |
| goto S120; |
| 1270 |
310 |
| S20: |
| 1271 |
310 |
| a1 = a/h; |
| 1272 |
310 |
| a0 = a-h*a1; |
| 1273 |
310 |
| qh = m/h; |
| 1274 |
310 |
| rh = m-h*qh; |
| 1275 |
310 |
▶ | if(!(a1 >= h)) goto S50; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(a1 >= h)) goto S50; |
155 |
155 |
310 |
|
| 1276 |
155 |
| a1 -= h; |
| 1277 |
155 |
| k = s/qh; |
| 1278 |
155 |
| p = h*(s-k*qh)-k*rh; |
| 1279 |
190 |
| S30: |
| 1280 |
190 |
▶ | if(!(p < 0)) goto S40; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(p < 0)) goto S40; |
155 |
35 |
190 |
|
| 1281 |
35 |
| p += m; |
| 1282 |
35 |
| goto S30; |
| 1283 |
155 |
| S40: |
| 1284 |
155 |
| goto S60; |
| 1285 |
155 |
| S50: |
| 1286 |
155 |
| p = 0; |
| 1287 |
310 |
| S60: |
| 1288 |
|
| /* |
| 1289 |
|
| P = (A2*S*H)MOD M |
| 1290 |
|
| */ |
| 1291 |
310 |
▶ | if(!(a1 != 0)) goto S90; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(a1 != 0)) goto S90; |
0 |
310 |
310 |
|
| 1292 |
310 |
| q = m/a1; |
| 1293 |
310 |
| k = s/q; |
| 1294 |
310 |
| p -= (k*(m-a1*q)); |
| 1295 |
310 |
▶ | if(p > 0) p -= m; |
Branch: 1 branch
| Branch | True | False | Total |
| if(p > 0) p -= m; |
125 |
185 |
310 |
|
| 1296 |
310 |
| p += (a1*(s-k*q)); |
| 1297 |
393 |
| S70: |
| 1298 |
393 |
▶ | if(!(p < 0)) goto S80; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(p < 0)) goto S80; |
310 |
83 |
393 |
|
| 1299 |
83 |
| p += m; |
| 1300 |
83 |
| goto S70; |
| 1301 |
0 |
| S90: |
| 1302 |
310 |
| S80: |
| 1303 |
310 |
| k = p/qh; |
| 1304 |
|
| /* |
| 1305 |
|
| P = ((A2*H + A1)*S)MOD M |
| 1306 |
|
| */ |
| 1307 |
310 |
| p = h*(p-k*qh)-k*rh; |
| 1308 |
382 |
| S100: |
| 1309 |
382 |
▶ | if(!(p < 0)) goto S110; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(p < 0)) goto S110; |
310 |
72 |
382 |
|
| 1310 |
72 |
| p += m; |
| 1311 |
72 |
| goto S100; |
| 1312 |
0 |
| S120: |
| 1313 |
310 |
| S110: |
| 1314 |
310 |
▶ | if(!(a0 != 0)) goto S150; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(a0 != 0)) goto S150; |
0 |
310 |
310 |
|
| 1315 |
|
| /* |
| 1316 |
|
| P = ((A2*H + A1)*H*S)MOD M |
| 1317 |
|
| */ |
| 1318 |
310 |
| q = m/a0; |
| 1319 |
310 |
| k = s/q; |
| 1320 |
310 |
| p -= (k*(m-a0*q)); |
| 1321 |
310 |
▶ | if(p > 0) p -= m; |
Branch: 1 branch
| Branch | True | False | Total |
| if(p > 0) p -= m; |
308 |
2 |
310 |
|
| 1322 |
310 |
| p += (a0*(s-k*q)); |
| 1323 |
461 |
| S130: |
| 1324 |
461 |
▶ | if(!(p < 0)) goto S140; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(p < 0)) goto S140; |
310 |
151 |
461 |
|
| 1325 |
151 |
| p += m; |
| 1326 |
151 |
| goto S130; |
| 1327 |
0 |
| S150: |
| 1328 |
310 |
| S140: |
| 1329 |
310 |
| mltmod = p; |
| 1330 |
310 |
| return mltmod; |
| 1331 |
|
| #undef h |
| 1332 |
|
| } |
| 1333 |
0 |
| void phrtsd(char* phrase,long *seed1,long *seed2) |
| 1334 |
|
| /* |
| 1335 |
|
| ********************************************************************** |
| 1336 |
|
| void phrtsd(char* phrase,long *seed1,long *seed2) |
| 1337 |
|
| PHRase To SeeDs |
| 1338 |
|
| |
| 1339 |
|
| Function |
| 1340 |
|
| |
| 1341 |
|
| Uses a phrase (character string) to generate two seeds for the RGN |
| 1342 |
|
| random number generator. |
| 1343 |
|
| Arguments |
| 1344 |
|
| phrase --> Phrase to be used for random number generation |
| 1345 |
|
| |
| 1346 |
|
| seed1 <-- First seed for generator |
| 1347 |
|
| |
| 1348 |
|
| seed2 <-- Second seed for generator |
| 1349 |
|
| |
| 1350 |
|
| Note |
| 1351 |
|
| |
| 1352 |
|
| Trailing blanks are eliminated before the seeds are generated. |
| 1353 |
|
| Generated seed values will fall in the range 1..2^30 |
| 1354 |
|
| (1..1,073,741,824) |
| 1355 |
|
| ********************************************************************** |
| 1356 |
|
| */ |
| 1357 |
|
| { |
| 1358 |
|
| |
| 1359 |
|
| static char table[] = |
| 1360 |
|
| "abcdefghijklmnopqrstuvwxyz\ |
| 1361 |
|
| ABCDEFGHIJKLMNOPQRSTUVWXYZ\ |
| 1362 |
|
| 0123456789\ |
| 1363 |
|
| !@#$%^&*()_+[];:'\\\"<>?,./"; |
| 1364 |
|
| |
| 1365 |
|
| long ix; |
| 1366 |
|
| |
| 1367 |
|
| static long twop30 = 1073741824L; |
| 1368 |
|
| static long shift[5] = { |
| 1369 |
|
| 1L,64L,4096L,262144L,16777216L |
| 1370 |
|
| }; |
| 1371 |
|
| static long i,ichr,j,lphr,values[5]; |
| 1372 |
|
| extern long lennob(char *str); |
| 1373 |
|
| |
| 1374 |
0 |
| *seed1 = 1234567890L; |
| 1375 |
0 |
| *seed2 = 123456789L; |
| 1376 |
0 |
| lphr = lennob(phrase); |
| 1377 |
0 |
▶ | if(lphr < 1) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if(lphr < 1) return; |
0 |
0 |
0 |
|
| 1378 |
0 |
▶ | for(i=0; i<=(lphr-1); i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<=(lphr-1); i++) { |
0 |
0 |
0 |
|
| 1379 |
0 |
▶ | for (ix=0; table[ix]; ix++) if (*(phrase+i) == table[ix]) break; |
Branch: 2 branches
| Branch | True | False | Total |
| for (ix=0; table[ix]; ix++) if (*(phrase+i) == table[ix]) break; |
0 |
0 |
0 |
| for (ix=0; table[ix]; ix++) if (*(phrase+i) == table[ix]) break; |
0 |
0 |
0 |
|
| 1380 |
0 |
▶ | if (!table[ix]) ix = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!table[ix]) ix = 0; |
0 |
0 |
0 |
|
| 1381 |
0 |
| ichr = ix % 64; |
| 1382 |
0 |
▶ | if(ichr == 0) ichr = 63; |
Branch: 1 branch
| Branch | True | False | Total |
| if(ichr == 0) ichr = 63; |
0 |
0 |
0 |
|
| 1383 |
0 |
▶ | for(j=1; j<=5; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=1; j<=5; j++) { |
0 |
0 |
0 |
|
| 1384 |
0 |
| *(values+j-1) = ichr-j; |
| 1385 |
0 |
▶ | if(*(values+j-1) < 1) *(values+j-1) += 63; |
Branch: 1 branch
| Branch | True | False | Total |
| if(*(values+j-1) < 1) *(values+j-1) += 63; |
0 |
0 |
0 |
|
| 1386 |
|
| } |
| 1387 |
0 |
▶ | for(j=1; j<=5; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=1; j<=5; j++) { |
0 |
0 |
0 |
|
| 1388 |
0 |
| *seed1 = ( *seed1+*(shift+j-1)**(values+j-1) ) % twop30; |
| 1389 |
0 |
| *seed2 = ( *seed2+*(shift+j-1)**(values+6-j-1) ) % twop30; |
| 1390 |
|
| } |
| 1391 |
|
| } |
| 1392 |
|
| #undef twop30 |
| 1393 |
|
| } |
| 1394 |
485 |
| float ranf(void) |
| 1395 |
|
| /* |
| 1396 |
|
| ********************************************************************** |
| 1397 |
|
| float ranf(void) |
| 1398 |
|
| RANDom number generator as a Function |
| 1399 |
|
| Returns a random floating point number from a uniform distribution |
| 1400 |
|
| over 0 - 1 (endpoints of this interval are not returned) using the |
| 1401 |
|
| current generator |
| 1402 |
|
| This is a transcription from Pascal to Fortran of routine |
| 1403 |
|
| Uniform_01 from the paper |
| 1404 |
|
| L'Ecuyer, P. and Cote, S. "Implementing a Random Number Package |
| 1405 |
|
| with Splitting Facilities." ACM Transactions on Mathematical |
| 1406 |
|
| Software, 17:98-111 (1991) |
| 1407 |
|
| ********************************************************************** |
| 1408 |
|
| */ |
| 1409 |
|
| { |
| 1410 |
|
| static float ranf; |
| 1411 |
|
| /* |
| 1412 |
|
| 4.656613057E-10 is 1/M1 M1 is set in a data statement in IGNLGI |
| 1413 |
|
| and is currently 2147483563. If M1 changes, change this also. |
| 1414 |
|
| */ |
| 1415 |
485 |
| ranf = ignlgi()*4.656613057E-10; |
| 1416 |
485 |
| return ranf; |
| 1417 |
|
| } |
| 1418 |
0 |
| void setgmn(float *meanv,float *covm,long p,float *parm) |
| 1419 |
|
| /* |
| 1420 |
|
| ********************************************************************** |
| 1421 |
|
| void setgmn(float *meanv,float *covm,long p,float *parm) |
| 1422 |
|
| SET Generate Multivariate Normal random deviate |
| 1423 |
|
| Function |
| 1424 |
|
| Places P, MEANV, and the Cholesky factoriztion of COVM |
| 1425 |
|
| in GENMN. |
| 1426 |
|
| Arguments |
| 1427 |
|
| meanv --> Mean vector of multivariate normal distribution. |
| 1428 |
|
| covm <--> (Input) Covariance matrix of the multivariate |
| 1429 |
|
| normal distribution |
| 1430 |
|
| (Output) Destroyed on output |
| 1431 |
|
| p --> Dimension of the normal, or length of MEANV. |
| 1432 |
|
| parm <-- Array of parameters needed to generate multivariate norma |
| 1433 |
|
| deviates (P, MEANV and Cholesky decomposition of |
| 1434 |
|
| COVM). |
| 1435 |
|
| 1 : 1 - P |
| 1436 |
|
| 2 : P + 1 - MEANV |
| 1437 |
|
| P+2 : P*(P+3)/2 + 1 - Cholesky decomposition of COVM |
| 1438 |
|
| Needed dimension is (p*(p+3)/2 + 1) |
| 1439 |
|
| ********************************************************************** |
| 1440 |
|
| */ |
| 1441 |
|
| { |
| 1442 |
|
| extern void spofa(float *a,long lda,long n,long *info); |
| 1443 |
|
| static long T1; |
| 1444 |
|
| static long i,icount,info,j,D2,D3,D4,D5; |
| 1445 |
0 |
| T1 = p*(p+3)/2+1; |
| 1446 |
|
| /* |
| 1447 |
|
| TEST THE INPUT |
| 1448 |
|
| */ |
| 1449 |
0 |
▶ | if(!(p <= 0)) goto S10; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(p <= 0)) goto S10; |
0 |
0 |
0 |
|
| 1450 |
0 |
| fputs("P nonpositive in SETGMN",stderr); |
| 1451 |
0 |
| fprintf(stderr,"Value of P: %12ld\n",p); |
| 1452 |
0 |
| exit(1); |
| 1453 |
0 |
| S10: |
| 1454 |
0 |
| *parm = p; |
| 1455 |
|
| /* |
| 1456 |
|
| PUT P AND MEANV INTO PARM |
| 1457 |
|
| */ |
| 1458 |
0 |
▶ | for(i=2,D2=1,D3=(p+1-i+D2)/D2; D3>0; D3--,i+=D2) *(parm+i-1) = *(meanv+i-2); |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=2,D2=1,D3=(p+1-i+D2)/D2; D3>0; D3--,i+=D2) *(parm+i-1) = *(meanv+i-2); |
0 |
0 |
0 |
|
| 1459 |
|
| /* |
| 1460 |
|
| Cholesky decomposition to find A s.t. trans(A)*(A) = COVM |
| 1461 |
|
| */ |
| 1462 |
0 |
| spofa(covm,p,p,&info); |
| 1463 |
0 |
▶ | if(!(info != 0)) goto S30; |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(info != 0)) goto S30; |
0 |
0 |
0 |
|
| 1464 |
0 |
| fputs(" COVM not positive definite in SETGMN",stderr); |
| 1465 |
0 |
| exit(1); |
| 1466 |
0 |
| S30: |
| 1467 |
0 |
| icount = p+1; |
| 1468 |
|
| /* |
| 1469 |
|
| PUT UPPER HALF OF A, WHICH IS NOW THE CHOLESKY FACTOR, INTO PARM |
| 1470 |
|
| COVM(1,1) = PARM(P+2) |
| 1471 |
|
| COVM(1,2) = PARM(P+3) |
| 1472 |
|
| : |
| 1473 |
|
| COVM(1,P) = PARM(2P+1) |
| 1474 |
|
| COVM(2,2) = PARM(2P+2) ... |
| 1475 |
|
| */ |
| 1476 |
0 |
▶ | for(i=1,D4=1,D5=(p-i+D4)/D4; D5>0; D5--,i+=D4) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=1,D4=1,D5=(p-i+D4)/D4; D5>0; D5--,i+=D4) { |
0 |
0 |
0 |
|
| 1477 |
0 |
▶ | for(j=i-1; j<p; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=i-1; j<p; j++) { |
0 |
0 |
0 |
|
| 1478 |
0 |
| icount += 1; |
| 1479 |
0 |
| *(parm+icount-1) = *(covm+i-1+j*p); |
| 1480 |
|
| } |
| 1481 |
|
| } |
| 1482 |
0 |
| } |
| 1483 |
0 |
| float sexpo(void) |
| 1484 |
|
| /* |
| 1485 |
|
| ********************************************************************** |
| 1486 |
|
| |
| 1487 |
|
| |
| 1488 |
|
| (STANDARD-) E X P O N E N T I A L DISTRIBUTION |
| 1489 |
|
| |
| 1490 |
|
| |
| 1491 |
|
| ********************************************************************** |
| 1492 |
|
| ********************************************************************** |
| 1493 |
|
| |
| 1494 |
|
| FOR DETAILS SEE: |
| 1495 |
|
| |
| 1496 |
|
| AHRENS, J.H. AND DIETER, U. |
| 1497 |
|
| COMPUTER METHODS FOR SAMPLING FROM THE |
| 1498 |
|
| EXPONENTIAL AND NORMAL DISTRIBUTIONS. |
| 1499 |
|
| COMM. ACM, 15,10 (OCT. 1972), 873 - 882. |
| 1500 |
|
| |
| 1501 |
|
| ALL STATEMENT NUMBERS CORRESPOND TO THE STEPS OF ALGORITHM |
| 1502 |
|
| 'SA' IN THE ABOVE PAPER (SLIGHTLY MODIFIED IMPLEMENTATION) |
| 1503 |
|
| |
| 1504 |
|
| Modified by Barry W. Brown, Feb 3, 1988 to use RANF instead of |
| 1505 |
|
| SUNIF. The argument IR thus goes away. |
| 1506 |
|
| |
| 1507 |
|
| ********************************************************************** |
| 1508 |
|
| Q(N) = SUM(ALOG(2.0)**K/K!) K=1,..,N , THE HIGHEST N |
| 1509 |
|
| (HERE 8) IS DETERMINED BY Q(N)=1.0 WITHIN STANDARD PRECISION |
| 1510 |
|
| */ |
| 1511 |
|
| { |
| 1512 |
|
| static float q[8] = { |
| 1513 |
|
| 0.6931472,0.9333737,0.9888778,0.9984959,0.9998293,0.9999833,0.9999986,1.0 |
| 1514 |
|
| }; |
| 1515 |
|
| static long i; |
| 1516 |
|
| static float sexpo,a,u,ustar,umin; |
| 1517 |
|
| static float *q1 = q; |
| 1518 |
0 |
| a = 0.0; |
| 1519 |
0 |
| u = ranf(); |
| 1520 |
0 |
| goto S30; |
| 1521 |
0 |
| S20: |
| 1522 |
0 |
| a += *q1; |
| 1523 |
0 |
| S30: |
| 1524 |
0 |
| u += u; |
| 1525 |
0 |
▶ | if(u <= 1.0) goto S20; |
Branch: 1 branch
| Branch | True | False | Total |
| if(u <= 1.0) goto S20; |
0 |
0 |
0 |
|
| 1526 |
0 |
| u -= 1.0; |
| 1527 |
0 |
▶ | if(u > *q1) goto S60; |
Branch: 1 branch
| Branch | True | False | Total |
| if(u > *q1) goto S60; |
0 |
0 |
0 |
|
| 1528 |
0 |
| sexpo = a+u; |
| 1529 |
0 |
| return sexpo; |
| 1530 |
0 |
| S60: |
| 1531 |
0 |
| i = 1; |
| 1532 |
0 |
| ustar = ranf(); |
| 1533 |
0 |
| umin = ustar; |
| 1534 |
0 |
| S70: |
| 1535 |
0 |
| ustar = ranf(); |
| 1536 |
0 |
▶ | if(ustar < umin) umin = ustar; |
Branch: 1 branch
| Branch | True | False | Total |
| if(ustar < umin) umin = ustar; |
0 |
0 |
0 |
|
| 1537 |
0 |
| i += 1; |
| 1538 |
0 |
▶ | if(u > *(q+i-1)) goto S70; |
Branch: 1 branch
| Branch | True | False | Total |
| if(u > *(q+i-1)) goto S70; |
0 |
0 |
0 |
|
| 1539 |
0 |
| sexpo = a+umin**q1; |
| 1540 |
0 |
| return sexpo; |
| 1541 |
|
| } |
| 1542 |
0 |
| float sgamma(float a) |
| 1543 |
|
| /* |
| 1544 |
|
| ********************************************************************** |
| 1545 |
|
| |
| 1546 |
|
| |
| 1547 |
|
| (STANDARD-) G A M M A DISTRIBUTION |
| 1548 |
|
| |
| 1549 |
|
| |
| 1550 |
|
| ********************************************************************** |
| 1551 |
|
| ********************************************************************** |
| 1552 |
|
| |
| 1553 |
|
| PARAMETER A >= 1.0 ! |
| 1554 |
|
| |
| 1555 |
|
| ********************************************************************** |
| 1556 |
|
| |
| 1557 |
|
| FOR DETAILS SEE: |
| 1558 |
|
| |
| 1559 |
|
| AHRENS, J.H. AND DIETER, U. |
| 1560 |
|
| GENERATING GAMMA VARIATES BY A |
| 1561 |
|
| MODIFIED REJECTION TECHNIQUE. |
| 1562 |
|
| COMM. ACM, 25,1 (JAN. 1982), 47 - 54. |
| 1563 |
|
| |
| 1564 |
|
| STEP NUMBERS CORRESPOND TO ALGORITHM 'GD' IN THE ABOVE PAPER |
| 1565 |
|
| (STRAIGHTFORWARD IMPLEMENTATION) |
| 1566 |
|
| |
| 1567 |
|
| Modified by Barry W. Brown, Feb 3, 1988 to use RANF instead of |
| 1568 |
|
| SUNIF. The argument IR thus goes away. |
| 1569 |
|
| |
| 1570 |
|
| ********************************************************************** |
| 1571 |
|
| |
| 1572 |
|
| PARAMETER 0.0 < A < 1.0 ! |
| 1573 |
|
| |
| 1574 |
|
| ********************************************************************** |
| 1575 |
|
| |
| 1576 |
|
| FOR DETAILS SEE: |
| 1577 |
|
| |
| 1578 |
|
| AHRENS, J.H. AND DIETER, U. |
| 1579 |
|
| COMPUTER METHODS FOR SAMPLING FROM GAMMA, |
| 1580 |
|
| BETA, POISSON AND BINOMIAL DISTRIBUTIONS. |
| 1581 |
|
| COMPUTING, 12 (1974), 223 - 246. |
| 1582 |
|
| |
| 1583 |
|
| (ADAPTED IMPLEMENTATION OF ALGORITHM 'GS' IN THE ABOVE PAPER) |
| 1584 |
|
| |
| 1585 |
|
| ********************************************************************** |
| 1586 |
|
| INPUT: A =PARAMETER (MEAN) OF THE STANDARD GAMMA DISTRIBUTION |
| 1587 |
|
| OUTPUT: SGAMMA = SAMPLE FROM THE GAMMA-(A)-DISTRIBUTION |
| 1588 |
|
| COEFFICIENTS Q(K) - FOR Q0 = SUM(Q(K)*A**(-K)) |
| 1589 |
|
| COEFFICIENTS A(K) - FOR Q = Q0+(T*T/2)*SUM(A(K)*V**K) |
| 1590 |
|
| COEFFICIENTS E(K) - FOR EXP(Q)-1 = SUM(E(K)*Q**K) |
| 1591 |
|
| PREVIOUS A PRE-SET TO ZERO - AA IS A', AAA IS A" |
| 1592 |
|
| SQRT32 IS THE SQUAREROOT OF 32 = 5.656854249492380 |
| 1593 |
|
| */ |
| 1594 |
|
| { |
| 1595 |
|
| extern float fsign( float num, float sign ); |
| 1596 |
|
| static float q1 = 4.166669E-2; |
| 1597 |
|
| static float q2 = 2.083148E-2; |
| 1598 |
|
| static float q3 = 8.01191E-3; |
| 1599 |
|
| static float q4 = 1.44121E-3; |
| 1600 |
|
| static float q5 = -7.388E-5; |
| 1601 |
|
| static float q6 = 2.4511E-4; |
| 1602 |
|
| static float q7 = 2.424E-4; |
| 1603 |
|
| static float a1 = 0.3333333; |
| 1604 |
|
| static float a2 = -0.250003; |
| 1605 |
|
| static float a3 = 0.2000062; |
| 1606 |
|
| static float a4 = -0.1662921; |
| 1607 |
|
| static float a5 = 0.1423657; |
| 1608 |
|
| static float a6 = -0.1367177; |
| 1609 |
|
| static float a7 = 0.1233795; |
| 1610 |
|
| static float e1 = 1.0; |
| 1611 |
|
| static float e2 = 0.4999897; |
| 1612 |
|
| static float e3 = 0.166829; |
| 1613 |
|
| static float e4 = 4.07753E-2; |
| 1614 |
|
| static float e5 = 1.0293E-2; |
| 1615 |
|
| static float aa = 0.0; |
| 1616 |
|
| static float aaa = 0.0; |
| 1617 |
|
| static float sqrt32 = 5.656854; |
| 1618 |
|
| static float sgamma,s2,s,d,t,x,u,r,q0,b,si,c,v,q,e,w,p; |
| 1619 |
0 |
▶ | if(a == aa) goto S10; |
Branch: 1 branch
| Branch | True | False | Total |
| if(a == aa) goto S10; |
0 |
0 |
0 |
|
| 1620 |
0 |
▶ | if(a < 1.0) goto S120; |
Branch: 1 branch
| Branch | True | False | Total |
| if(a < 1.0) goto S120; |
0 |
0 |
0 |
|
| 1621 |
|
| /* |
| 1622 |
|
| STEP 1: RECALCULATIONS OF S2,S,D IF A HAS CHANGED |
| 1623 |
|
| */ |
| 1624 |
0 |
| aa = a; |
| 1625 |
0 |
| s2 = a-0.5; |
| 1626 |
0 |
| s = sqrt(s2); |
| 1627 |
0 |
| d = sqrt32-12.0*s; |
| 1628 |
0 |
| S10: |
| 1629 |
|
| /* |
| 1630 |
|
| STEP 2: T=STANDARD NORMAL DEVIATE, |
| 1631 |
|
| X=(S,1/2)-NORMAL DEVIATE. |
| 1632 |
|
| IMMEDIATE ACCEPTANCE (I) |
| 1633 |
|
| */ |
| 1634 |
0 |
| t = snorm(); |
| 1635 |
0 |
| x = s+0.5*t; |
| 1636 |
0 |
| sgamma = x*x; |
| 1637 |
0 |
▶ | if(t >= 0.0) return sgamma; |
Branch: 1 branch
| Branch | True | False | Total |
| if(t >= 0.0) return sgamma; |
0 |
0 |
0 |
|
| 1638 |
|
| /* |
| 1639 |
|
| STEP 3: U= 0,1 -UNIFORM SAMPLE. SQUEEZE ACCEPTANCE (S) |
| 1640 |
|
| */ |
| 1641 |
0 |
| u = ranf(); |
| 1642 |
0 |
▶ | if(d*u <= t*t*t) return sgamma; |
Branch: 1 branch
| Branch | True | False | Total |
| if(d*u <= t*t*t) return sgamma; |
0 |
0 |
0 |
|
| 1643 |
|
| /* |
| 1644 |
|
| STEP 4: RECALCULATIONS OF Q0,B,SI,C IF NECESSARY |
| 1645 |
|
| */ |
| 1646 |
0 |
▶ | if(a == aaa) goto S40; |
Branch: 1 branch
| Branch | True | False | Total |
| if(a == aaa) goto S40; |
0 |
0 |
0 |
|
| 1647 |
0 |
| aaa = a; |
| 1648 |
0 |
| r = 1.0/ a; |
| 1649 |
0 |
| q0 = ((((((q7*r+q6)*r+q5)*r+q4)*r+q3)*r+q2)*r+q1)*r; |
| 1650 |
|
| /* |
| 1651 |
|
| APPROXIMATION DEPENDING ON SIZE OF PARAMETER A |
| 1652 |
|
| THE CONSTANTS IN THE EXPRESSIONS FOR B, SI AND |
| 1653 |
|
| C WERE ESTABLISHED BY NUMERICAL EXPERIMENTS |
| 1654 |
|
| */ |
| 1655 |
0 |
▶ | if(a <= 3.686) goto S30; |
Branch: 1 branch
| Branch | True | False | Total |
| if(a <= 3.686) goto S30; |
0 |
0 |
0 |
|
| 1656 |
0 |
▶ | if(a <= 13.022) goto S20; |
Branch: 1 branch
| Branch | True | False | Total |
| if(a <= 13.022) goto S20; |
0 |
0 |
0 |
|
| 1657 |
|
| /* |
| 1658 |
|
| CASE 3: A .GT. 13.022 |
| 1659 |
|
| */ |
| 1660 |
0 |
| b = 1.77; |
| 1661 |
0 |
| si = 0.75; |
| 1662 |
0 |
| c = 0.1515/s; |
| 1663 |
0 |
| goto S40; |
| 1664 |
0 |
| S20: |
| 1665 |
|
| /* |
| 1666 |
|
| CASE 2: 3.686 .LT. A .LE. 13.022 |
| 1667 |
|
| */ |
| 1668 |
0 |
| b = 1.654+7.6E-3*s2; |
| 1669 |
0 |
| si = 1.68/s+0.275; |
| 1670 |
0 |
| c = 6.2E-2/s+2.4E-2; |
| 1671 |
0 |
| goto S40; |
| 1672 |
0 |
| S30: |
| 1673 |
|
| /* |
| 1674 |
|
| CASE 1: A .LE. 3.686 |
| 1675 |
|
| */ |
| 1676 |
0 |
| b = 0.463+s+0.178*s2; |
| 1677 |
0 |
| si = 1.235; |
| 1678 |
0 |
| c = 0.195/s-7.9E-2+1.6E-1*s; |
| 1679 |
0 |
| S40: |
| 1680 |
|
| /* |
| 1681 |
|
| STEP 5: NO QUOTIENT TEST IF X NOT POSITIVE |
| 1682 |
|
| */ |
| 1683 |
0 |
▶ | if(x <= 0.0) goto S70; |
Branch: 1 branch
| Branch | True | False | Total |
| if(x <= 0.0) goto S70; |
0 |
0 |
0 |
|
| 1684 |
|
| /* |
| 1685 |
|
| STEP 6: CALCULATION OF V AND QUOTIENT Q |
| 1686 |
|
| */ |
| 1687 |
0 |
| v = t/(s+s); |
| 1688 |
0 |
▶ | if(fabs(v) <= 0.25) goto S50; |
Branch: 1 branch
| Branch | True | False | Total |
| if(fabs(v) <= 0.25) goto S50; |
0 |
0 |
0 |
|
| 1689 |
0 |
| q = q0-s*t+0.25*t*t+(s2+s2)*log(1.0+v); |
| 1690 |
0 |
| goto S60; |
| 1691 |
0 |
| S50: |
| 1692 |
0 |
| q = q0+0.5*t*t*((((((a7*v+a6)*v+a5)*v+a4)*v+a3)*v+a2)*v+a1)*v; |
| 1693 |
0 |
| S60: |
| 1694 |
|
| /* |
| 1695 |
|
| STEP 7: QUOTIENT ACCEPTANCE (Q) |
| 1696 |
|
| */ |
| 1697 |
0 |
▶ | if(log(1.0-u) <= q) return sgamma; |
Branch: 1 branch
| Branch | True | False | Total |
| if(log(1.0-u) <= q) return sgamma; |
0 |
0 |
0 |
|
| 1698 |
0 |
| S70: |
| 1699 |
|
| /* |
| 1700 |
|
| STEP 8: E=STANDARD EXPONENTIAL DEVIATE |
| 1701 |
|
| U= 0,1 -UNIFORM DEVIATE |
| 1702 |
|
| T=(B,SI)-DOUBLE EXPONENTIAL (LAPLACE) SAMPLE |
| 1703 |
|
| */ |
| 1704 |
0 |
| e = sexpo(); |
| 1705 |
0 |
| u = ranf(); |
| 1706 |
0 |
| u += (u-1.0); |
| 1707 |
0 |
| t = b+fsign(si*e,u); |
| 1708 |
|
| /* |
| 1709 |
|
| STEP 9: REJECTION IF T .LT. TAU(1) = -.71874483771719 |
| 1710 |
|
| */ |
| 1711 |
0 |
▶ | if(t < -0.7187449) goto S70; |
Branch: 1 branch
| Branch | True | False | Total |
| if(t < -0.7187449) goto S70; |
0 |
0 |
0 |
|
| 1712 |
|
| /* |
| 1713 |
|
| STEP 10: CALCULATION OF V AND QUOTIENT Q |
| 1714 |
|
| */ |
| 1715 |
0 |
| v = t/(s+s); |
| 1716 |
0 |
▶ | if(fabs(v) <= 0.25) goto S80; |
Branch: 1 branch
| Branch | True | False | Total |
| if(fabs(v) <= 0.25) goto S80; |
0 |
0 |
0 |
|
| 1717 |
0 |
| q = q0-s*t+0.25*t*t+(s2+s2)*log(1.0+v); |
| 1718 |
0 |
| goto S90; |
| 1719 |
0 |
| S80: |
| 1720 |
0 |
| q = q0+0.5*t*t*((((((a7*v+a6)*v+a5)*v+a4)*v+a3)*v+a2)*v+a1)*v; |
| 1721 |
0 |
| S90: |
| 1722 |
|
| /* |
| 1723 |
|
| STEP 11: HAT ACCEPTANCE (H) (IF Q NOT POSITIVE GO TO STEP 8) |
| 1724 |
|
| */ |
| 1725 |
0 |
▶ | if(q <= 0.0) goto S70; |
Branch: 1 branch
| Branch | True | False | Total |
| if(q <= 0.0) goto S70; |
0 |
0 |
0 |
|
| 1726 |
0 |
▶ | if(q <= 0.5) goto S100; |
Branch: 1 branch
| Branch | True | False | Total |
| if(q <= 0.5) goto S100; |
0 |
0 |
0 |
|
| 1727 |
0 |
| w = exp(q)-1.0; |
| 1728 |
0 |
| goto S110; |
| 1729 |
0 |
| S100: |
| 1730 |
0 |
| w = ((((e5*q+e4)*q+e3)*q+e2)*q+e1)*q; |
| 1731 |
0 |
| S110: |
| 1732 |
|
| /* |
| 1733 |
|
| IF T IS REJECTED, SAMPLE AGAIN AT STEP 8 |
| 1734 |
|
| */ |
| 1735 |
0 |
▶ | if(c*fabs(u) > w*exp(e-0.5*t*t)) goto S70; |
Branch: 1 branch
| Branch | True | False | Total |
| if(c*fabs(u) > w*exp(e-0.5*t*t)) goto S70; |
0 |
0 |
0 |
|
| 1736 |
0 |
| x = s+0.5*t; |
| 1737 |
0 |
| sgamma = x*x; |
| 1738 |
0 |
| return sgamma; |
| 1739 |
0 |
| S120: |
| 1740 |
|
| /* |
| 1741 |
|
| ALTERNATE METHOD FOR PARAMETERS A BELOW 1 (.3678794=EXP(-1.)) |
| 1742 |
|
| */ |
| 1743 |
0 |
| aa = 0.0; |
| 1744 |
0 |
| b = 1.0+0.3678794*a; |
| 1745 |
0 |
| S130: |
| 1746 |
0 |
| p = b*ranf(); |
| 1747 |
0 |
▶ | if(p >= 1.0) goto S140; |
Branch: 1 branch
| Branch | True | False | Total |
| if(p >= 1.0) goto S140; |
0 |
0 |
0 |
|
| 1748 |
0 |
| sgamma = exp(log(p)/ a); |
| 1749 |
0 |
▶ | if(sexpo() < sgamma) goto S130; |
Branch: 1 branch
| Branch | True | False | Total |
| if(sexpo() < sgamma) goto S130; |
0 |
0 |
0 |
|
| 1750 |
0 |
| return sgamma; |
| 1751 |
0 |
| S140: |
| 1752 |
0 |
| sgamma = -log((b-p)/ a); |
| 1753 |
0 |
▶ | if(sexpo() < (1.0-a)*log(sgamma)) goto S130; |
Branch: 1 branch
| Branch | True | False | Total |
| if(sexpo() < (1.0-a)*log(sgamma)) goto S130; |
0 |
0 |
0 |
|
| 1754 |
0 |
| return sgamma; |
| 1755 |
|
| } |
| 1756 |
388 |
| float snorm(void) |
| 1757 |
|
| /* |
| 1758 |
|
| ********************************************************************** |
| 1759 |
|
| |
| 1760 |
|
| |
| 1761 |
|
| (STANDARD-) N O R M A L DISTRIBUTION |
| 1762 |
|
| |
| 1763 |
|
| |
| 1764 |
|
| ********************************************************************** |
| 1765 |
|
| ********************************************************************** |
| 1766 |
|
| |
| 1767 |
|
| FOR DETAILS SEE: |
| 1768 |
|
| |
| 1769 |
|
| AHRENS, J.H. AND DIETER, U. |
| 1770 |
|
| EXTENSIONS OF FORSYTHE'S METHOD FOR RANDOM |
| 1771 |
|
| SAMPLING FROM THE NORMAL DISTRIBUTION. |
| 1772 |
|
| MATH. COMPUT., 27,124 (OCT. 1973), 927 - 937. |
| 1773 |
|
| |
| 1774 |
|
| ALL STATEMENT NUMBERS CORRESPOND TO THE STEPS OF ALGORITHM 'FL' |
| 1775 |
|
| (M=5) IN THE ABOVE PAPER (SLIGHTLY MODIFIED IMPLEMENTATION) |
| 1776 |
|
| |
| 1777 |
|
| Modified by Barry W. Brown, Feb 3, 1988 to use RANF instead of |
| 1778 |
|
| SUNIF. The argument IR thus goes away. |
| 1779 |
|
| |
| 1780 |
|
| ********************************************************************** |
| 1781 |
|
| THE DEFINITIONS OF THE CONSTANTS A(K), D(K), T(K) AND |
| 1782 |
|
| H(K) ARE ACCORDING TO THE ABOVEMENTIONED ARTICLE |
| 1783 |
|
| */ |
| 1784 |
|
| { |
| 1785 |
|
| static float a[32] = { |
| 1786 |
|
| 0.0,3.917609E-2,7.841241E-2,0.11777,0.1573107,0.1970991,0.2372021,0.2776904, |
| 1787 |
|
| 0.3186394,0.36013,0.4022501,0.4450965,0.4887764,0.5334097,0.5791322, |
| 1788 |
|
| 0.626099,0.6744898,0.7245144,0.7764218,0.8305109,0.8871466,0.9467818, |
| 1789 |
|
| 1.00999,1.077516,1.150349,1.229859,1.318011,1.417797,1.534121,1.67594, |
| 1790 |
|
| 1.862732,2.153875 |
| 1791 |
|
| }; |
| 1792 |
|
| static float d[31] = { |
| 1793 |
|
| 0.0,0.0,0.0,0.0,0.0,0.2636843,0.2425085,0.2255674,0.2116342,0.1999243, |
| 1794 |
|
| 0.1899108,0.1812252,0.1736014,0.1668419,0.1607967,0.1553497,0.1504094, |
| 1795 |
|
| 0.1459026,0.14177,0.1379632,0.1344418,0.1311722,0.128126,0.1252791, |
| 1796 |
|
| 0.1226109,0.1201036,0.1177417,0.1155119,0.1134023,0.1114027,0.1095039 |
| 1797 |
|
| }; |
| 1798 |
|
| static float t[31] = { |
| 1799 |
|
| 7.673828E-4,2.30687E-3,3.860618E-3,5.438454E-3,7.0507E-3,8.708396E-3, |
| 1800 |
|
| 1.042357E-2,1.220953E-2,1.408125E-2,1.605579E-2,1.81529E-2,2.039573E-2, |
| 1801 |
|
| 2.281177E-2,2.543407E-2,2.830296E-2,3.146822E-2,3.499233E-2,3.895483E-2, |
| 1802 |
|
| 4.345878E-2,4.864035E-2,5.468334E-2,6.184222E-2,7.047983E-2,8.113195E-2, |
| 1803 |
|
| 9.462444E-2,0.1123001,0.136498,0.1716886,0.2276241,0.330498,0.5847031 |
| 1804 |
|
| }; |
| 1805 |
|
| static float h[31] = { |
| 1806 |
|
| 3.920617E-2,3.932705E-2,3.951E-2,3.975703E-2,4.007093E-2,4.045533E-2, |
| 1807 |
|
| 4.091481E-2,4.145507E-2,4.208311E-2,4.280748E-2,4.363863E-2,4.458932E-2, |
| 1808 |
|
| 4.567523E-2,4.691571E-2,4.833487E-2,4.996298E-2,5.183859E-2,5.401138E-2, |
| 1809 |
|
| 5.654656E-2,5.95313E-2,6.308489E-2,6.737503E-2,7.264544E-2,7.926471E-2, |
| 1810 |
|
| 8.781922E-2,9.930398E-2,0.11556,0.1404344,0.1836142,0.2790016,0.7010474 |
| 1811 |
|
| }; |
| 1812 |
|
| static long i; |
| 1813 |
|
| static float snorm,u,s,ustar,aa,w,y,tt; |
| 1814 |
388 |
| u = ranf(); |
| 1815 |
388 |
| s = 0.0; |
| 1816 |
388 |
▶ | if(u > 0.5) s = 1.0; |
Branch: 1 branch
| Branch | True | False | Total |
| if(u > 0.5) s = 1.0; |
195 |
193 |
388 |
|
| 1817 |
388 |
| u += (u-s); |
| 1818 |
388 |
| u = 32.0*u; |
| 1819 |
388 |
| i = (long) (u); |
| 1820 |
388 |
▶ | if(i == 32) i = 31; |
Branch: 1 branch
| Branch | True | False | Total |
| if(i == 32) i = 31; |
0 |
388 |
388 |
|
| 1821 |
388 |
▶ | if(i == 0) goto S100; |
Branch: 1 branch
| Branch | True | False | Total |
| if(i == 0) goto S100; |
18 |
370 |
388 |
|
| 1822 |
|
| /* |
| 1823 |
|
| START CENTER |
| 1824 |
|
| */ |
| 1825 |
370 |
| ustar = u-(float)i; |
| 1826 |
370 |
| aa = *(a+i-1); |
| 1827 |
380 |
| S40: |
| 1828 |
380 |
▶ | if(ustar <= *(t+i-1)) goto S60; |
Branch: 1 branch
| Branch | True | False | Total |
| if(ustar <= *(t+i-1)) goto S60; |
18 |
362 |
380 |
|
| 1829 |
362 |
| w = (ustar-*(t+i-1))**(h+i-1); |
| 1830 |
388 |
| S50: |
| 1831 |
|
| /* |
| 1832 |
|
| EXIT (BOTH CASES) |
| 1833 |
|
| */ |
| 1834 |
388 |
| y = aa+w; |
| 1835 |
388 |
| snorm = y; |
| 1836 |
388 |
▶ | if(s == 1.0) snorm = -y; |
Branch: 1 branch
| Branch | True | False | Total |
| if(s == 1.0) snorm = -y; |
195 |
193 |
388 |
|
| 1837 |
388 |
| return snorm; |
| 1838 |
18 |
| S60: |
| 1839 |
|
| /* |
| 1840 |
|
| CENTER CONTINUED |
| 1841 |
|
| */ |
| 1842 |
18 |
| u = ranf(); |
| 1843 |
18 |
| w = u*(*(a+i)-aa); |
| 1844 |
18 |
| tt = (0.5*w+aa)*w; |
| 1845 |
18 |
| goto S80; |
| 1846 |
4 |
| S70: |
| 1847 |
4 |
| tt = u; |
| 1848 |
4 |
| ustar = ranf(); |
| 1849 |
22 |
| S80: |
| 1850 |
22 |
▶ | if(ustar > tt) goto S50; |
Branch: 1 branch
| Branch | True | False | Total |
| if(ustar > tt) goto S50; |
8 |
14 |
22 |
|
| 1851 |
14 |
| u = ranf(); |
| 1852 |
14 |
▶ | if(ustar >= u) goto S70; |
Branch: 1 branch
| Branch | True | False | Total |
| if(ustar >= u) goto S70; |
4 |
10 |
14 |
|
| 1853 |
10 |
| ustar = ranf(); |
| 1854 |
10 |
| goto S40; |
| 1855 |
18 |
| S100: |
| 1856 |
|
| /* |
| 1857 |
|
| START TAIL |
| 1858 |
|
| */ |
| 1859 |
18 |
| i = 6; |
| 1860 |
18 |
| aa = *(a+31); |
| 1861 |
18 |
| goto S120; |
| 1862 |
20 |
| S110: |
| 1863 |
20 |
| aa += *(d+i-1); |
| 1864 |
20 |
| i += 1; |
| 1865 |
38 |
| S120: |
| 1866 |
38 |
| u += u; |
| 1867 |
38 |
▶ | if(u < 1.0) goto S110; |
Branch: 1 branch
| Branch | True | False | Total |
| if(u < 1.0) goto S110; |
20 |
18 |
38 |
|
| 1868 |
18 |
| u -= 1.0; |
| 1869 |
27 |
| S140: |
| 1870 |
27 |
| w = u**(d+i-1); |
| 1871 |
27 |
| tt = (0.5*w+aa)*w; |
| 1872 |
27 |
| goto S160; |
| 1873 |
3 |
| S150: |
| 1874 |
3 |
| tt = u; |
| 1875 |
30 |
| S160: |
| 1876 |
30 |
| ustar = ranf(); |
| 1877 |
30 |
▶ | if(ustar > tt) goto S50; |
Branch: 1 branch
| Branch | True | False | Total |
| if(ustar > tt) goto S50; |
18 |
12 |
30 |
|
| 1878 |
12 |
| u = ranf(); |
| 1879 |
12 |
▶ | if(ustar >= u) goto S150; |
Branch: 1 branch
| Branch | True | False | Total |
| if(ustar >= u) goto S150; |
3 |
9 |
12 |
|
| 1880 |
9 |
| u = ranf(); |
| 1881 |
9 |
| goto S140; |
| 1882 |
|
| } |
| 1883 |
0 |
| float fsign( float num, float sign ) |
| 1884 |
|
| /* Transfers sign of argument sign to argument num */ |
| 1885 |
|
| { |
| 1886 |
0 |
▶ | if ( ( sign>0.0f && num<0.0f ) || ( sign<0.0f && num>0.0f ) ) |
Branch: 4 branches
| Branch | True | False | Total |
| if ( ( sign>0.0f && num<0.0f ) || ( sign<0.0f && num>0.0f ) ) |
0 |
0 |
0 |
| if ( ( sign>0.0f && num<0.0f ) || ( sign<0.0f && num>0.0f ) ) |
0 |
0 |
0 |
| if ( ( sign>0.0f && num<0.0f ) || ( sign<0.0f && num>0.0f ) ) |
0 |
0 |
0 |
| if ( ( sign>0.0f && num<0.0f ) || ( sign<0.0f && num>0.0f ) ) |
0 |
0 |
0 |
|
| 1887 |
0 |
| return -num; |
| 1888 |
0 |
| else return num; |
| 1889 |
|
| } |
| 1890 |
|
| /************************************************************************ |
| 1891 |
|
| FTNSTOP: |
| 1892 |
|
| Prints msg to standard error and then exits |
| 1893 |
|
| ************************************************************************/ |
| 1894 |
0 |
| void ftnstop(char* msg) |
| 1895 |
|
| /* msg - error message */ |
| 1896 |
|
| { |
| 1897 |
0 |
▶ | if (msg != NULL) fprintf(stderr,"%s\n",msg); |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg != NULL) fprintf(stderr,"%s\n",msg); |
0 |
0 |
0 |
|
| 1898 |
0 |
| exit(0); |
| 1899 |
|
| } |