00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include <sys/types.h>
00047 #include <sys/time.h>
00048 #include <stdio.h>
00049 #include <stdlib.h>
00050 #include <unistd.h>
00051 #include <string.h>
00052 #include <math.h>
00053 #include <time.h>
00054 #include "rng.h"
00055
00056 #define RANDOM 1
00057 #define DRAND48 2
00058 #define MINIMAL_INT 3
00059 #define MINIMAL_REAL 4
00060 #define GFSR521 5
00061 #define TGFSR 6
00062
00063 #ifndef GENERATOR
00064 #define GENERATOR GFSR521
00065 #endif
00066
00067
00068
00069 #define GenIs(g) (GENERATOR == g)
00070
00071 #include <strings.h>
00072 #if GenIs(MINIMAL_REAL)
00073 #include <math.h>
00074 #elif GenIs(RANDOM)
00075 long random(void);
00076 int srandom(unsigned);
00077 char *initstate(unsigned, char *, int);
00078 char *setstate(char *);
00079 #elif GenIs(DRAND48)
00080 #include <stdlib.h>
00081 #endif
00082
00083
00084
00085 static int
00086 rng_is_bigendian(void)
00087 {
00088 union {
00089 long l;
00090 char c[sizeof(long)];
00091 } u;
00092
00093 u.l = 1L;
00094 return(u.c[sizeof(long) - 1] == 1);
00095 }
00096
00097
00098
00099
00100
00101 static double
00102 rng_getnand(void)
00103 {
00104
00105 static int ieee_nan_inited = 0;
00106
00107 static union {
00108 double d;
00109 unsigned char c[8];
00110 } nan;
00111
00112 if (!ieee_nan_inited) {
00113 int i;
00114 for (i = 0; i < 8; i++)
00115 nan.c[i] = 1;
00116 if (rng_is_bigendian()) {
00117 nan.c[0] = 0x7F;
00118 nan.c[1] = 0xF0;
00119 } else {
00120 nan.c[7] = 0x7F;
00121 nan.c[6] = 0xF0;
00122 }
00123 ieee_nan_inited = 1;
00124 }
00125 return(nan.d);
00126 }
00127
00128
00129
00130
00131
00132
00133 char *
00134 rng_name(void)
00135 {
00136 #if GenIs(RANDOM)
00137 return("random from UNIX");
00138
00139 #elif GenIs(MINIMAL_REAL)
00140 return("LCG of Park/Miller (real version)");
00141
00142 #elif GenIs(MINIMAL_INT)
00143 return("LCG of Park/Miller (int version)");
00144
00145 #elif GenIs(DRAND48)
00146 return("drand48 from UNIX");
00147
00148 #elif GenIs(GFSR521)
00149 return("GFSR of length-521 of Ripley 1990");
00150
00151 #elif GenIs(TGFSR)
00152 return("Matsumoto `twisted' GFSR");
00153
00154 #else
00155 #error "empty function"
00156 #endif
00157 }
00158
00159
00160
00161
00162 int
00163 rng_numeric_name(void)
00164 {
00165 return(GENERATOR);
00166 }
00167
00168
00169
00170 #if GenIs(GFSR521)
00171 #define GFSR_P 521
00172 #define GFSR_Q 32
00173 #define GFSR_L 32
00174 static unsigned state[GFSR_P];
00175 static unsigned *state_dst;
00176 static unsigned *state_src;
00177
00178 #elif GenIs(RANDOM)
00179 #define RANDOM_REGLENGTH 64
00180 static long state[RANDOM_REGLENGTH];
00181
00182 #elif GenIs(MINIMAL_REAL)
00183 static double state;
00184
00185 #elif GenIs(MINIMAL_INT)
00186 static int state;
00187
00188 #elif GenIs(TGFSR)
00189 #define TGFSR_P 624
00190 static unsigned long state[TGFSR_P];
00191
00192 #endif
00193
00194
00195
00196 #if GenIs(GFSR521)
00197 #define MODULUS_I 2147483647
00198 #define MULTIPLIER_I 16807
00199 #define MODoverMULT_I 127773
00200 #define MODmodMULT_I 2836
00201
00202 static
00203 void
00204 init_gfsr(int seed)
00205 {
00206 unsigned char x0[2*GFSR_P];
00207 int x0_seed;
00208 int i, j;
00209
00210
00211 for (x0_seed = seed, i = 0; i < GFSR_P; i++) {
00212 x0_seed =
00213 MULTIPLIER_I * (x0_seed % MODoverMULT_I) -
00214 MODmodMULT_I * (x0_seed / MODoverMULT_I);
00215 if (x0_seed <= 0)
00216 x0_seed += MODULUS_I;
00217 x0[i] = (x0_seed > (MODULUS_I/2));
00218 }
00219
00220 for (i = GFSR_P; i < 2*GFSR_P; i++)
00221 x0[i] = x0[i - GFSR_P] ^ x0[i - GFSR_Q];
00222
00223 for (i = 0; i < GFSR_P; i++)
00224 for (state[i] = j = 0; j < GFSR_L; j++)
00225 state[i] |= x0[i+16*(j+1)] << j;
00226
00227 state_dst = state + (GFSR_P-GFSR_Q-1);
00228 state_src = state + (GFSR_P-1);
00229 }
00230 #endif
00231
00232
00233 #if GenIs(TGFSR)
00234 static
00235 void
00236 init_tgfsr(unsigned long seed)
00237 {
00238 int k;
00239
00240
00241
00242
00243
00244
00245 state[0]= seed & 0xffffffff;
00246 for (k=1; k<TGFSR_P; k++)
00247 state[k] = (69069 * state[k-1]) & 0xffffffff;
00248 }
00249 #endif
00250
00251
00252
00253
00254 int
00255 rng_init_state_seeded(int seed)
00256 {
00257 #if GenIs(RANDOM)
00258 initstate((unsigned) seed, (char *) state, sizeof(state));
00259 setstate((char *) state);
00260
00261 #elif GenIs(MINIMAL_REAL)
00262 state = seed;
00263
00264 #elif GenIs(MINIMAL_INT)
00265 state = seed;
00266
00267 #elif GenIs(DRAND48)
00268 srand48((long) seed);
00269
00270 #elif GenIs(GFSR521)
00271 init_gfsr(seed);
00272
00273 #elif GenIs(TGFSR)
00274 init_tgfsr((unsigned long) seed);
00275
00276 #else
00277 #error "empty function"
00278 #endif
00279 return(seed);
00280 }
00281
00282
00283
00284
00285
00286 int
00287 rng_new_seed(void)
00288 {
00289 unsigned int t = 0;
00290 FILE *fp;
00291 struct timeval tv;
00292 int ok = 0;
00293 int nread;
00294
00295
00296 if (!ok) {
00297 fp = fopen("/dev/urandom", "r");
00298 if (fp != NULL) {
00299 nread = fread(&t, sizeof(t), (size_t) 1, fp);
00300 fclose(fp);
00301 if (nread == 1)
00302 ok = 1;
00303 else
00304 t = 0;
00305 }
00306 }
00307
00308 if (!ok) {
00309
00310
00311 fp = popen("ps auxww | cksum", "r");
00312 if (fp != NULL) {
00313 nread = fscanf(fp, "%u", &t);
00314 pclose(fp);
00315 if (nread == 1)
00316 ok = 1;
00317 else
00318 t = 0;
00319 }
00320 }
00321
00322
00323
00324
00325 if (!ok) {
00326 gettimeofday(&tv, NULL);
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339 t =
00340 ((tv.tv_sec & 0xffffffff) << 0) ^
00341 ((tv.tv_usec & 0x000fffff) << 11) ^
00342 ((clock() & 0x00ffffff) << 7) ^
00343 ((getuid() & 0x000fffff) << 10) ^
00344 ((getpid() & 0x000fffff) << 9) ^
00345 ((getppid() & 0x0000ffff) << 1);
00346 }
00347
00348 t &= 0x7fffffff;
00349 if (t == 0) t = 1;
00350 return(t);
00351 }
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362 int
00363 rng_init_state(void)
00364 {
00365 static int seed = 0;
00366
00367
00368 if (seed == 0) {
00369
00370 rng_init_state_seeded(seed = rng_new_seed());
00371 }
00372 return seed;
00373 }
00374
00375
00376
00377
00378
00379
00380
00381 int
00382 rng_state_length(void)
00383 {
00384 int bufused;
00385
00386 #if GenIs(RANDOM)
00387 bufused = sizeof(state);
00388
00389 #elif GenIs(MINIMAL_REAL)
00390 bufused = sizeof(state);
00391
00392 #elif GenIs(MINIMAL_INT)
00393 bufused = sizeof(state);
00394
00395 #elif GenIs(DRAND48)
00396 bufused = 3*sizeof(unsigned short);
00397
00398 #elif GenIs(GFSR521)
00399 bufused = sizeof(state) + sizeof(state_dst) + sizeof(state_src);
00400
00401 #elif GenIs(TGFSR)
00402 bufused = sizeof(state);
00403
00404 #else
00405 #error "empty function"
00406 #endif
00407 return(bufused);
00408 }
00409
00410
00411
00412
00413
00414
00415 int
00416 rng_get_state(char *buf, int buflen)
00417 {
00418
00419 int bufused = rng_state_length();
00420
00421 #if GenIs(RANDOM)
00422 if (buflen >= bufused)
00423 bcopy((void *) state, (void *) buf, (size_t) bufused);
00424 else
00425 bufused = 0;
00426
00427 #elif GenIs(MINIMAL_REAL)
00428 if (buflen >= bufused)
00429 bcopy((void *) &state, (void *) buf, (size_t) bufused);
00430 else
00431 bufused = 0;
00432
00433 #elif GenIs(MINIMAL_INT)
00434 if (buflen >= bufused)
00435 bcopy((void *) &state, (void *) buf, (size_t) bufused);
00436 else
00437 bufused = 0;
00438
00439 #elif GenIs(DRAND48)
00440 if (buflen >= bufused) {
00441 unsigned short *buf_internal;
00442
00443
00444
00445 buf_internal = seed48((unsigned short *) buf);
00446
00447 bcopy((void *) buf_internal, (void *) buf, bufused);
00448
00449 seed48((unsigned short *) buf);
00450 }
00451 else
00452 bufused = 0;
00453
00454 #elif GenIs(GFSR521)
00455 if (buflen >= bufused) {
00456
00457 bcopy((void *) state,
00458 (void *) buf,
00459 (size_t) sizeof(state));
00460 bcopy((void *) &state_dst,
00461 (void *) (buf + sizeof(state)),
00462 (size_t) sizeof(state_dst));
00463 bcopy((void *) &state_src,
00464 (void *) (buf + sizeof(state) + sizeof(state_dst)),
00465 (size_t) sizeof(state_src));
00466 }
00467 else
00468 bufused = 0;
00469
00470 #elif GenIs(TGFSR)
00471 if (buflen >= bufused)
00472 bcopy((void *) state, (void *) buf, (size_t) bufused);
00473 else
00474 bufused = 0;
00475
00476 #else
00477 #error "empty function"
00478 #endif
00479 return(bufused);
00480 }
00481
00482
00483
00484
00485
00486
00487 int
00488 rng_set_state(char *buf, int buflen)
00489 {
00490 int bufneed = rng_state_length();
00491
00492 #if GenIs(RANDOM)
00493 if (buflen >= bufneed) {
00494 bcopy((void *) buf, (void *) state, (size_t) bufneed);
00495 setstate((char *) state);
00496 }
00497 else
00498 bufneed = 0;
00499
00500 #elif GenIs(MINIMAL_REAL)
00501 if (buflen >= bufneed)
00502 bcopy((void *) buf, (void *) &state, (size_t) bufneed);
00503 else
00504 bufneed = 0;
00505
00506 #elif GenIs(MINIMAL_INT)
00507 if (buflen >= bufneed)
00508 bcopy((void *) buf, (void *) &state, (size_t) bufneed);
00509 else
00510 bufneed = 0;
00511
00512 #elif GenIs(DRAND48)
00513 if (buflen >= bufneed) {
00514
00515 seed48((unsigned short *) buf);
00516 }
00517 else
00518 bufneed = 0;
00519
00520 #elif GenIs(GFSR521)
00521 if (buflen >= bufneed) {
00522
00523 bcopy((void *) buf, (void *) state, (size_t) sizeof(state));
00524 bcopy((void *) (buf + sizeof(state)),
00525 (void *) &state_dst,
00526 (size_t) sizeof(state_dst));
00527 bcopy((void *) (buf + sizeof(state) + sizeof(state_dst)),
00528 (void *) &state_src,
00529 (size_t) sizeof(state_src));
00530 }
00531 else
00532 bufneed = 0;
00533
00534 #elif GenIs(TGFSR)
00535 if (buflen >= bufneed)
00536 bcopy((void *) buf, (void *) state, (size_t) bufneed);
00537 else
00538 bufneed = 0;
00539
00540 #else
00541 #error "empty function"
00542 #endif
00543 return(bufneed);
00544 }
00545
00546
00547
00548 #if GenIs(RANDOM)
00549 #define MODULUS 2147483648.0
00550 #define MODULUS_INV 4.656612873077393e-10
00551
00552 #elif GenIs(MINIMAL_REAL)
00553 #define MODULUS 2147483647.0
00554 #define MODULUS_INV 4.656612875245797e-10
00555 #define MULTIPLIER 16807.0
00556
00557 #elif GenIs(MINIMAL_INT)
00558 #define MODULUS 2147483647
00559 #define MODULUS_INV 4.656612875245797e-10
00560 #define MULTIPLIER 16807
00561 #define MODoverMULT 127773
00562 #define MODmodMULT 2836
00563
00564 #elif GenIs(GFSR521)
00565 #define MODULUS 4294967296
00566 #define MODULUS_INV 2.3283064365387e-10
00567
00568 #elif GenIs(TGFSR)
00569
00570 #define N_twist 624
00571 #define M_twist 397
00572 #define MATRIX_A 0x9908b0df
00573 #define UPPER_MASK 0x80000000
00574 #define LOWER_MASK 0x7fffffff
00575
00576 #define TEMPERING_MASK_B 0x9d2c5680
00577 #define TEMPERING_MASK_C 0xefc60000
00578 #define TEMPERING_SHIFT_U(y) (y >> 11)
00579 #define TEMPERING_SHIFT_S(y) (y << 7)
00580 #define TEMPERING_SHIFT_T(y) (y << 15)
00581 #define TEMPERING_SHIFT_L(y) (y >> 18)
00582
00583 #endif
00584
00585
00586
00587
00588 double
00589 rng_uniform (void)
00590 {
00591 #if GenIs(RANDOM)
00592 return(((double) random()) * MODULUS_INV);
00593
00594 #elif GenIs(MINIMAL_REAL)
00595 return(
00596 (state = fmod(MULTIPLIER * state, MODULUS)) * MODULUS_INV
00597 );
00598 #elif GenIs(MINIMAL_INT)
00599 state =
00600 MULTIPLIER * (state % MODoverMULT) -
00601 MODmodMULT * (state / MODoverMULT);
00602 if (state <= 0)
00603 state += MODULUS;
00604 return(state * MODULUS_INV);
00605
00606 #elif GenIs(DRAND48)
00607 return(drand48());
00608
00609 #elif GenIs(GFSR521)
00610 double rval;
00611
00612 rval = *state_dst * MODULUS_INV;
00613 *state_dst ^= *state_src;
00614 state_dst = (state_dst > state) ? state_dst-1 : state+(GFSR_P-1);
00615 state_src = (state_src > state) ? state_src-1 : state+(GFSR_P-1);
00616 return(rval);
00617
00618 #elif GenIs(TGFSR)
00619 unsigned long y;
00620 static int k = 1;
00621 static unsigned long mag01[2]={0x0, MATRIX_A};
00622
00623
00624 if(k == N_twist){
00625 int kk;
00626 for (kk=0;kk<N_twist-M_twist;kk++) {
00627 y = (state[kk]&UPPER_MASK)|(state[kk+1]&LOWER_MASK);
00628 state[kk] = state[kk+M_twist] ^ (y >> 1) ^ mag01[y & 0x1];
00629 }
00630 for (;kk<N_twist-1;kk++) {
00631 y = (state[kk]&UPPER_MASK)|(state[kk+1]&LOWER_MASK);
00632 state[kk] = state[kk+(M_twist-N_twist)] ^ (y >> 1) ^ mag01[y & 0x1];
00633 }
00634 y = (state[N_twist-1]&UPPER_MASK)|(state[0]&LOWER_MASK);
00635 state[N_twist-1] = state[M_twist-1] ^ (y >> 1) ^ mag01[y & 0x1];
00636
00637 k = 0;
00638 }
00639
00640 y = state[k++];
00641 y ^= TEMPERING_SHIFT_U(y);
00642 y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
00643 y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
00644 y &= 0xffffffff;
00645 y ^= TEMPERING_SHIFT_L(y);
00646
00647 return ( (double)y * (1.0/ (unsigned long)0xffffffff) );
00648
00649 #else
00650 #error "empty function"
00651 #endif
00652 }
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682 double
00683 rng_normal(void) {
00684 static double ace_in_the_hole;
00685 static int have_ace = 0;
00686 double u1, u2;
00687 double norm;
00688
00689 if (have_ace) {
00690 have_ace = 0;
00691 return ace_in_the_hole;
00692 } else {
00693 do {
00694
00695 u1 = 2.0*rng_uniform() - 1.0;
00696 u2 = 2.0*rng_uniform() - 1.0;
00697 } while (1.0 <= (norm = u1*u1 + u2*u2));
00698 norm = sqrt(-2.0 * log(norm)/norm);
00699 ace_in_the_hole = u1 * norm;
00700 have_ace = 1;
00701 return u2 * norm;
00702 }
00703 }
00704
00705
00706
00707
00708 double
00709 rng_normal_old(void) {
00710 return (sqrt (-2.0 * log(rng_uniform())) *
00711 sin(2.0 * M_PI * rng_uniform()));
00712 }
00713
00714
00715
00716
00717
00718 double
00719 rng_normal_sdev(double mu, double sigma) {
00720 return rng_normal()*sigma + mu;
00721 }
00722
00723
00724
00725
00726
00727 double
00728 rng_normal_var(double mu, double sigma2) {
00729 return rng_normal()*sqrt(sigma2) + mu;
00730 }
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806 double
00807 rng_gamma(double alpha, double theta)
00808 {
00809
00810 double X, U, V, W, Y, Z;
00811 double b, c, t;
00812
00813 if (alpha <= 0 || theta < 0)
00814 return rng_getnand();
00815 if (alpha == 1.0) {
00816
00817 X = -log(rng_uniform());
00818 } else if (alpha == 2.0) {
00819
00820 X = -log(rng_uniform() * rng_uniform());
00821 } else if (alpha < 1.0) {
00822
00823 c = 1.0 / alpha;
00824 t = 0.07 + 0.75 * sqrt(1.0 - alpha);
00825 b = 1.0 + exp(-t) * alpha / t;
00826
00827 while (1) {
00828 U = rng_uniform();
00829 W = rng_uniform();
00830 V = b * U;
00831 if (V <= 1.0) {
00832 X = t * pow(V, c);
00833
00834
00835
00836
00837 if (W * (2.0 + X) <= (2.0 - X)) break;
00838 if (W <= exp(-X)) break;
00839 } else {
00840 X = -log(c * t * (b - V));
00841 Y = X / t;
00842 if ((W * (alpha + Y - alpha*Y)) <= 1.0) break;
00843 if (W <= pow(Y, alpha - 1.0)) break;
00844 }
00845 }
00846 } else {
00847
00848
00849
00850
00851 b = alpha - 1.0;
00852 c = 3.0 * alpha - 0.75;
00853
00854 while (1) {
00855 U = rng_uniform();
00856 V = rng_uniform();
00857 W = U * (1.0 - U);
00858 Y = sqrt(c / W) * (U - 0.5);
00859 X = b + Y;
00860 if (X >= 0.0) {
00861 Z = 64.0 * W*W*W * V*V;
00862
00863
00864
00865
00866 if ((Z * X) <= (X - 2.0 * Y * Y)) break;
00867 if (log(Z) <= (2.0 * (b * log(X/b) - Y))) break;
00868 }
00869 }
00870 }
00871 return X * theta;
00872 }
00873
00874
00875
00876
00877
00878
00879
00880 double
00881 rng_beta(double a, double b)
00882 {
00883 double Ga = rng_gamma(a, 1.0);
00884 double Gb = rng_gamma(b, 1.0);
00885 return Ga / (Ga + Gb);
00886
00887
00888 }
00889
00890
00891
00892
00893
00894 double
00895 rng_chi2(double r)
00896 {
00897 return rng_gamma(r * 0.5, 2.0);
00898 }
00899
00900
00901
00902
00903
00904
00905 double
00906 rng_exponential()
00907 {
00908 return -log(rng_uniform());
00909 }
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936 static
00937 double
00938 rng_poisson_count(double lambda)
00939 {
00940 const double gate = exp(-lambda);
00941 double prod, x;
00942
00943 if (gate == 1) {
00944
00945
00946
00947
00948 return (rng_uniform() < lambda) ? 1.0 : 0.0;
00949 }
00950
00951 for (prod = 1.0, x = 0.0; prod > gate; prod *= rng_uniform(), x++)
00952 ;
00953 return x-1.0;
00954 }
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973 static
00974 double
00975 rng_poisson_trans_reject(double mu)
00976 {
00977 double b, a, v_r;
00978 double one_alpha;
00979 double U, US, V;
00980 double k;
00981
00982
00983 b = 0.931 + 2.53 * sqrt(mu);
00984 a = -0.059 + 0.02483 * b;
00985 v_r = 0.9277 - 3.6224 / (b - 2.0);
00986
00987 while (1) {
00988 U = rng_uniform();
00989 V = rng_uniform();
00990 U = U - 0.5;
00991 US = 0.5 - fabs(U);
00992 k = floor((2.0*a/US + b) * U + mu + 0.43);
00993 if (k < 0)
00994 continue;
00995 if (US >= 0.07 && V <= v_r)
00996 break;
00997 if (US < 0.013 && V > US)
00998 continue;
00999
01000 one_alpha = 1.1239 + 1.1328 / (b - 3.4);
01001
01002 if (log(V * one_alpha / (a/(US*US) + b))
01003 <= (-mu + k*log(mu) - lgamma(k+1.0)))
01004 break;
01005 }
01006 return k;
01007 }
01008
01009
01010 double
01011 rng_poisson(double lambda)
01012 {
01013
01014 const double mode_switch = 30;
01015
01016 if (lambda <= 0.0)
01017 return rng_getnand();
01018 else if (lambda < mode_switch)
01019
01020
01021 return rng_poisson_count(lambda);
01022 else
01023
01024 return rng_poisson_trans_reject(lambda);
01025 }
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035 double
01036 rng_geometric(double p)
01037 {
01038 if (p < 0.25)
01039
01040
01041
01042 return ceil(log(rng_uniform()) / log1p(-p));
01043 else
01044 return ceil(log(rng_uniform()) / log(1.0 - p));
01045 }
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063 static
01064 double
01065 rng_binomial_wait(double n, double p)
01066 {
01067 double sum, x;
01068
01069
01070 if (n == 0.0)
01071 return 0.0;
01072 else if (p == 0.0)
01073 return 0.0;
01074 else if (p == 1.0)
01075 return n;
01076
01077 for (sum = 0, x = 0; sum <= n; sum += rng_geometric(p), x++)
01078 ;
01079 return x - 1.0;
01080 }
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104 static
01105 double
01106 rng_binomial_trans_reject(double n, double p)
01107 {
01108 const double spq = sqrt(n*p*(1-p));
01109 double b, a, c, v_r;
01110 double alpha, lpq, m, h;
01111 double U, US, V;
01112 double k;
01113
01114
01115 b = 1.15 + 2.53 * spq;
01116 a = -0.0873 + 0.0248 * b + 0.01 * p;
01117 c = n * p + 0.5;
01118 v_r = 0.92 - 4.2 / b;
01119
01120 while (1) {
01121 U = rng_uniform();
01122 V = rng_uniform();
01123 U = U - 0.5;
01124 US = 0.5 - fabs(U);
01125 k = floor((2.0 * a/US + b) * U + c);
01126 if (k < 0 || k > n)
01127 continue;
01128 if (US >= 0.07 && V <= v_r)
01129 break;
01130
01131 alpha = (2.83 + 5.1/b) * spq;
01132 lpq = log(p/(1-p));
01133 m = floor(n*p+p);
01134 h = lgamma(m+1) + lgamma(n-m+1);
01135
01136 V = log(V * alpha/(a / (US*US) + b));
01137 if (V <= h - lgamma(k+1) -lgamma(n-k+1) + (k-m)*lpq)
01138 break;
01139 }
01140 return k;
01141 }
01142
01143
01144
01145
01146
01147
01148
01149
01150
01151
01152 double
01153 rng_binomial(double n, double p)
01154 {
01155
01156
01157 const double mode_switch = 16;
01158 int flip;
01159 double X;
01160
01161
01162
01163 if (!(n >= 0.0) || !(floor(n) == n) || !isfinite(n))
01164 return rng_getnand();
01165 if (!(p >= 0.0 && p <= 1.0))
01166 return rng_getnand();
01167
01168 if (n == 0.0)
01169 return 0.0;
01170 else if (p == 0.0)
01171 return 0.0;
01172 else if (p == 1.0)
01173 return n;
01174
01175 if (p > 0.5) {
01176 p = 1.0 - p;
01177 flip = 1;
01178 } else {
01179 flip = 0;
01180 }
01181
01182 if ((n*p) < mode_switch) {
01183
01184 X = rng_binomial_wait(n, p);
01185 } else {
01186
01187 X = rng_binomial_trans_reject(n, p);
01188 }
01189
01190 return flip ? (n - X) : X;
01191 }
01192
01193
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206
01207
01208
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219
01220
01221
01222
01223 static
01224 int
01225 rng_cholesky(double *a,
01226 int N)
01227 {
01228 double fac;
01229 int i, j, k;
01230
01231 for (k = 0; k < N; k++) {
01232 if (a[k*N+k] <= 0.0)
01233 return 0;
01234 fac = a[k*N+k] = sqrt(a[k*N+k]);
01235 fac = 1/fac;
01236 for (j = k+1; j < N; j++)
01237 a[j*N+k] *= fac;
01238 for (j = k+1; j < N; j++)
01239 for (i = j; i < N; i++)
01240 a[i*N+j] -= a[i*N+k] * a[j*N+k];
01241 }
01242 return 1;
01243 }
01244
01245
01246
01247
01248
01249
01250
01251
01252
01253
01254
01255
01256
01257
01258
01259
01260
01261 #ifdef NOT_DEFINED
01262
01263 static
01264 int
01265 rng_invert_upper(double *a,
01266 int N)
01267 {
01268 int i,j,k;
01269 double sum;
01270
01271 for (j = 0; j < N; j++) {
01272 if (a[j*N+j] == 0.0)
01273 return 0;
01274 a[j*N+j] = 1 / a[j*N+j];
01275 for (i = j+1; i < N; i++) {
01276 sum = 0.0;
01277 for (k = j; k < i; k++) {
01278 sum -= a[i*N+k] * a[k*N+j];
01279 }
01280 a[i*N+j] = sum / a[i*N+i];
01281 }
01282 }
01283 return 1;
01284 }
01285
01286 #endif
01287
01288
01289
01290
01291
01292 static
01293 void
01294 rng_mult_upper_triangles(double* c, double* a, double* b, int N)
01295 {
01296 int i, j, k;
01297
01298 for (i = 0; i < N; i++)
01299 for (j = i; j < N; j++) {
01300
01301 c[j+i*N] = 0;
01302
01303 c[i+j*N] = 0;
01304 for (k = i; k <= j; k++)
01305
01306
01307
01308
01309
01310 c[i+j*N] += a[i+k*N] * b[k+j*N];
01311 }
01312 }
01313
01314
01315
01316
01317
01318
01319 static
01320 void
01321 rng_square_upper_triangle(double* b, double* a, int N)
01322 {
01323 int i, j, k;
01324
01325 for (i = 0; i < N; i++)
01326 for (j = i; j < N; j++) {
01327
01328 b[i+j*N] = 0;
01329 for (k = 0; k <= i; k++)
01330
01331
01332
01333
01334 b[i+j*N] += a[k+i*N] * a[k+j*N];
01335
01336 b[j+i*N] = b[i+j*N];
01337 }
01338 }
01339
01340
01341
01342
01343
01344
01345
01346
01347
01348
01349
01350
01351
01352
01353
01354
01355
01356
01357
01358
01359
01360
01361
01362
01363
01364
01365 int
01366 rng_normal_vector(
01367 double* x,
01368 double* mu,
01369 double* sigma,
01370 int n,
01371 int d,
01372 int stridex1,
01373 int stridex2,
01374 int stridemu
01375 )
01376 {
01377 int i, j, k;
01378 double *z;
01379 double *R;
01380
01381
01382 R = calloc(d*d, sizeof(double));
01383 z = calloc(d , sizeof(double));
01384 if (R==NULL || z==NULL) {
01385 (void)fprintf(stderr, "rng_normal_vector2: failed calloc\n");
01386 return 0;
01387 }
01388
01389 memcpy((void *) R, (void *) sigma, sizeof(double)*d*d);
01390
01391 if (!rng_cholesky(R, d)) {
01392 free(R); free(z); return(0);
01393 }
01394
01395
01396 for (i = 0; i < n; i++) {
01397
01398 for (j = 0; j < d; j++)
01399 z[j] = rng_normal();
01400
01401
01402 for (j = 0; j < d; j++) {
01403
01404 x[i*stridex1+j*stridex2] = mu ? mu[j*stridemu] : 0.0;
01405
01406 for (k = 0; k <= j; k++)
01407 x[i*stridex1+j*stridex2] += R[j*d+k] * z[k];
01408 }
01409 }
01410 free((void *) R); free((void *) z);
01411 return(1);
01412 }
01413
01414
01415
01416
01417
01418
01419
01420
01421
01422
01423
01424
01425
01426
01427
01428
01429
01430
01431
01432
01433
01434
01435
01436
01437
01438
01439
01440
01441
01442
01443
01444
01445
01446
01447
01448
01449
01450
01451
01452
01453
01454
01455
01456
01457
01458
01459
01460
01461
01462
01463
01464
01465
01466
01467
01468
01469
01470
01471 static
01472 int
01473 rng_wishart_chol(double *sigma, double dof, int N, double *w)
01474 {
01475 int i,j;
01476 double *G = NULL;
01477 double *T = NULL;
01478 double *Z = NULL;
01479 int ok = 0;
01480
01481
01482 G = (double *) calloc(N*N, sizeof(double));
01483 T = (double *) calloc(N*N, sizeof(double));
01484 Z = (double *) calloc(N*N, sizeof(double));
01485 if (!G || !T || !Z)
01486 goto done;
01487
01488
01489
01490
01491
01492 for (j = 0; j < N; j++)
01493 for (i = 0; i < N; i++)
01494 if (i == j)
01495 T[i+N*i] = sqrt(rng_chi2(dof - ((double) i)));
01496 else if (i < j)
01497 T[i+N*j] = rng_normal();
01498 else
01499 T[i+N*j] = 0.0;
01500
01501
01502 bcopy(sigma, G, N*N*sizeof(double));
01503 if (!rng_cholesky(G, N))
01504 goto done;
01505
01506
01507 rng_mult_upper_triangles(Z, T, G, N);
01508
01509
01510 rng_square_upper_triangle(w, Z, N);
01511
01512
01513
01514
01515
01516
01517 ok = 1;
01518 done:
01519 free(G);
01520 free(T);
01521 free(Z);
01522 return ok;
01523 }
01524
01525
01526
01527
01528
01529
01530
01531
01532
01533 static
01534 int
01535 rng_wishart_outer(double *sigma, double dof, int N, double *w)
01536 {
01537 double *x, *x1;
01538 int i, j, s;
01539 int ok;
01540
01541
01542 if ((x = (double *) calloc(N*dof, sizeof(double))) == NULL)
01543 return 0;
01544
01545 ok = rng_normal_vector(x,
01546 NULL,
01547 sigma,
01548 dof,
01549 N,
01550 N,
01551 1,
01552 1);
01553 if (!ok) {
01554 free(x);
01555 return 0;
01556 }
01557
01558 bzero(w, N*N*sizeof(*w));
01559 for (s = 0; s < dof; s++) {
01560 x1 = x + s*N;
01561 for (i = 0; i < N; i++)
01562 for (j = 0; j < N; j++)
01563 w[i*N+j] += x1[i] * x1[j];
01564 }
01565
01566 free(x);
01567 return 1;
01568 }
01569
01570
01571
01572
01573
01574
01575
01576
01577
01578
01579
01580
01581
01582
01583 int
01584 rng_wishart(double* sigma, double dof, double scale, int N, double* w)
01585 {
01586 int ok;
01587 int i;
01588
01589 if (dof > N) {
01590
01591 ok = rng_wishart_chol(sigma, dof, N, w);
01592 } else {
01593
01594 ok = rng_wishart_outer(sigma, dof, N, w);
01595 }
01596
01597 if (ok && scale != 1.0)
01598 for (i = 0; i < N*N; i++)
01599 w[i] *= scale;
01600
01601 return ok;
01602 }
01603