-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathranlib.c
1577 lines (1467 loc) · 48.1 KB
/
ranlib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* {=================================================================
*
* ranlib.c
* Ranlib random number generator
* Luis Carvalho (lexcarvalho@gmail.com)
* Adapted from ranlib.c to consider nl_RNG objects
* Original files can be found at http://www.netlib.org/random
*
* ==================================================================} */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "numlua.h"
#include "rng.h"
#define ABS(x) ((x) >= 0 ? (x) : -(x))
#define min(a,b) ((a) <= (b) ? (a) : (b))
#define max(a,b) ((a) >= (b) ? (a) : (b))
/* {=================================================================
* Auxiliar
* ==================================================================} */
static double fsign( double num, double sign )
/* Transfers sign of argument sign to argument num */
{
if ( ( sign>0.0f && num<0.0f ) || ( sign<0.0f && num>0.0f ) )
return -num;
else return num;
}
/************************************************************************
FTNSTOP:
Prints msg to standard error and then exits
************************************************************************/
static void ftnstop(char* msg)
/* msg - error message */
{
if (msg != NULL) fprintf(stderr,"%s\n",msg);
exit(0);
}
/* {=================================================================
* Standard deviates
* ==================================================================} */
static double sexpo(nl_RNG *o)
/*
**********************************************************************
(STANDARD-) E X P O N E N T I A L DISTRIBUTION
**********************************************************************
**********************************************************************
FOR DETAILS SEE:
AHRENS, J.H. AND DIETER, U.
COMPUTER METHODS FOR SAMPLING FROM THE
EXPONENTIAL AND NORMAL DISTRIBUTIONS.
COMM. ACM, 15,10 (OCT. 1972), 873 - 882.
ALL STATEMENT NUMBERS CORRESPOND TO THE STEPS OF ALGORITHM
'SA' IN THE ABOVE PAPER (SLIGHTLY MODIFIED IMPLEMENTATION)
Modified by Barry W. Brown, Feb 3, 1988 to use RANF instead of
SUNIF. The argument IR thus goes away.
**********************************************************************
Q(N) = SUM(ALOG(2.0)**K/K!) K=1,..,N , THE HIGHEST N
(HERE 8) IS DETERMINED BY Q(N)=1.0 WITHIN STANDARD PRECISION
*/
{
static double q[8] = {
0.6931472,0.9333737,0.9888778,0.9984959,0.9998293,0.9999833,0.9999986,1.0
};
static long i;
static double sexpo,a,u,ustar,umin;
static double *q1 = q;
a = 0.0;
u = ranf(o);
goto S30;
S20:
a += *q1;
S30:
u += u;
if(u <= 1.0) goto S20;
u -= 1.0;
if(u > *q1) goto S60;
sexpo = a+u;
return sexpo;
S60:
i = 1;
ustar = ranf(o);
umin = ustar;
S70:
ustar = ranf(o);
if(ustar < umin) umin = ustar;
i += 1;
if(u > *(q+i-1)) goto S70;
sexpo = a+umin**q1;
return sexpo;
}
static double snorm(nl_RNG *o)
/*
**********************************************************************
(STANDARD-) N O R M A L DISTRIBUTION
**********************************************************************
**********************************************************************
FOR DETAILS SEE:
AHRENS, J.H. AND DIETER, U.
EXTENSIONS OF FORSYTHE'S METHOD FOR RANDOM
SAMPLING FROM THE NORMAL DISTRIBUTION.
MATH. COMPUT., 27,124 (OCT. 1973), 927 - 937.
ALL STATEMENT NUMBERS CORRESPOND TO THE STEPS OF ALGORITHM 'FL'
(M=5) IN THE ABOVE PAPER (SLIGHTLY MODIFIED IMPLEMENTATION)
Modified by Barry W. Brown, Feb 3, 1988 to use RANF instead of
SUNIF. The argument IR thus goes away.
**********************************************************************
THE DEFINITIONS OF THE CONSTANTS A(K), D(K), T(K) AND
H(K) ARE ACCORDING TO THE ABOVEMENTIONED ARTICLE
*/
{
static double a[32] = {
0.0,3.917609E-2,7.841241E-2,0.11777,0.1573107,0.1970991,0.2372021,0.2776904,
0.3186394,0.36013,0.4022501,0.4450965,0.4887764,0.5334097,0.5791322,
0.626099,0.6744898,0.7245144,0.7764218,0.8305109,0.8871466,0.9467818,
1.00999,1.077516,1.150349,1.229859,1.318011,1.417797,1.534121,1.67594,
1.862732,2.153875
};
static double d[31] = {
0.0,0.0,0.0,0.0,0.0,0.2636843,0.2425085,0.2255674,0.2116342,0.1999243,
0.1899108,0.1812252,0.1736014,0.1668419,0.1607967,0.1553497,0.1504094,
0.1459026,0.14177,0.1379632,0.1344418,0.1311722,0.128126,0.1252791,
0.1226109,0.1201036,0.1177417,0.1155119,0.1134023,0.1114027,0.1095039
};
static double t[31] = {
7.673828E-4,2.30687E-3,3.860618E-3,5.438454E-3,7.0507E-3,8.708396E-3,
1.042357E-2,1.220953E-2,1.408125E-2,1.605579E-2,1.81529E-2,2.039573E-2,
2.281177E-2,2.543407E-2,2.830296E-2,3.146822E-2,3.499233E-2,3.895483E-2,
4.345878E-2,4.864035E-2,5.468334E-2,6.184222E-2,7.047983E-2,8.113195E-2,
9.462444E-2,0.1123001,0.136498,0.1716886,0.2276241,0.330498,0.5847031
};
static double h[31] = {
3.920617E-2,3.932705E-2,3.951E-2,3.975703E-2,4.007093E-2,4.045533E-2,
4.091481E-2,4.145507E-2,4.208311E-2,4.280748E-2,4.363863E-2,4.458932E-2,
4.567523E-2,4.691571E-2,4.833487E-2,4.996298E-2,5.183859E-2,5.401138E-2,
5.654656E-2,5.95313E-2,6.308489E-2,6.737503E-2,7.264544E-2,7.926471E-2,
8.781922E-2,9.930398E-2,0.11556,0.1404344,0.1836142,0.2790016,0.7010474
};
static long i;
static double snorm,u,s,ustar,aa,w,y,tt;
u = ranf(o);
s = 0.0;
if(u > 0.5) s = 1.0;
u += (u-s);
u = 32.0*u;
i = (long) (u);
if(i == 32) i = 31;
if(i == 0) goto S100;
/*
START CENTER
*/
ustar = u-(float)i;
aa = *(a+i-1);
S40:
if(ustar <= *(t+i-1)) goto S60;
w = (ustar-*(t+i-1))**(h+i-1);
S50:
/*
EXIT (BOTH CASES)
*/
y = aa+w;
snorm = y;
if(s == 1.0) snorm = -y;
return snorm;
S60:
/*
CENTER CONTINUED
*/
u = ranf(o);
w = u*(*(a+i)-aa);
tt = (0.5*w+aa)*w;
goto S80;
S70:
tt = u;
ustar = ranf(o);
S80:
if(ustar > tt) goto S50;
u = ranf(o);
if(ustar >= u) goto S70;
ustar = ranf(o);
goto S40;
S100:
/*
START TAIL
*/
i = 6;
aa = *(a+31);
goto S120;
S110:
aa += *(d+i-1);
i += 1;
S120:
u += u;
if(u < 1.0) goto S110;
u -= 1.0;
S140:
w = u**(d+i-1);
tt = (0.5*w+aa)*w;
goto S160;
S150:
tt = u;
S160:
ustar = ranf(o);
if(ustar > tt) goto S50;
u = ranf(o);
if(ustar >= u) goto S150;
u = ranf(o);
goto S140;
}
static double sgamma(nl_RNG *o,double a)
/*
**********************************************************************
(STANDARD-) G A M M A DISTRIBUTION
**********************************************************************
**********************************************************************
PARAMETER A >= 1.0 !
**********************************************************************
FOR DETAILS SEE:
AHRENS, J.H. AND DIETER, U.
GENERATING GAMMA VARIATES BY A
MODIFIED REJECTION TECHNIQUE.
COMM. ACM, 25,1 (JAN. 1982), 47 - 54.
STEP NUMBERS CORRESPOND TO ALGORITHM 'GD' IN THE ABOVE PAPER
(STRAIGHTFORWARD IMPLEMENTATION)
Modified by Barry W. Brown, Feb 3, 1988 to use RANF instead of
SUNIF. The argument IR thus goes away.
**********************************************************************
PARAMETER 0.0 < A < 1.0 !
**********************************************************************
FOR DETAILS SEE:
AHRENS, J.H. AND DIETER, U.
COMPUTER METHODS FOR SAMPLING FROM GAMMA,
BETA, POISSON AND BINOMIAL DISTRIBUTIONS.
COMPUTING, 12 (1974), 223 - 246.
(ADAPTED IMPLEMENTATION OF ALGORITHM 'GS' IN THE ABOVE PAPER)
**********************************************************************
INPUT: A =PARAMETER (MEAN) OF THE STANDARD GAMMA DISTRIBUTION
OUTPUT: SGAMMA = SAMPLE FROM THE GAMMA-(A)-DISTRIBUTION
COEFFICIENTS Q(K) - FOR Q0 = SUM(Q(K)*A**(-K))
COEFFICIENTS A(K) - FOR Q = Q0+(T*T/2)*SUM(A(K)*V**K)
COEFFICIENTS E(K) - FOR EXP(Q)-1 = SUM(E(K)*Q**K)
PREVIOUS A PRE-SET TO ZERO - AA IS A', AAA IS A"
SQRT32 IS THE SQUAREROOT OF 32 = 5.656854249492380
*/
{
static double q1 = 4.166669E-2;
static double q2 = 2.083148E-2;
static double q3 = 8.01191E-3;
static double q4 = 1.44121E-3;
static double q5 = -7.388E-5;
static double q6 = 2.4511E-4;
static double q7 = 2.424E-4;
static double a1 = 0.3333333;
static double a2 = -0.250003;
static double a3 = 0.2000062;
static double a4 = -0.1662921;
static double a5 = 0.1423657;
static double a6 = -0.1367177;
static double a7 = 0.1233795;
static double e1 = 1.0;
static double e2 = 0.4999897;
static double e3 = 0.166829;
static double e4 = 4.07753E-2;
static double e5 = 1.0293E-2;
static double aa = 0.0;
static double aaa = 0.0;
static double sqrt32 = 5.656854;
static double sgamma,s2,s,d,t,x,u,r,q0,b,si,c,v,q,e,w,p;
if(a == aa) goto S10;
if(a < 1.0) goto S120;
/*
STEP 1: RECALCULATIONS OF S2,S,D IF A HAS CHANGED
*/
aa = a;
s2 = a-0.5;
s = sqrt(s2);
d = sqrt32-12.0*s;
S10:
/*
STEP 2: T=STANDARD NORMAL DEVIATE,
X=(S,1/2)-NORMAL DEVIATE.
IMMEDIATE ACCEPTANCE (I)
*/
t = snorm(o);
x = s+0.5*t;
sgamma = x*x;
if(t >= 0.0) return sgamma;
/*
STEP 3: U= 0,1 -UNIFORM SAMPLE. SQUEEZE ACCEPTANCE (S)
*/
u = ranf(o);
if(d*u <= t*t*t) return sgamma;
/*
STEP 4: RECALCULATIONS OF Q0,B,SI,C IF NECESSARY
*/
if(a == aaa) goto S40;
aaa = a;
r = 1.0/ a;
q0 = ((((((q7*r+q6)*r+q5)*r+q4)*r+q3)*r+q2)*r+q1)*r;
/*
APPROXIMATION DEPENDING ON SIZE OF PARAMETER A
THE CONSTANTS IN THE EXPRESSIONS FOR B, SI AND
C WERE ESTABLISHED BY NUMERICAL EXPERIMENTS
*/
if(a <= 3.686) goto S30;
if(a <= 13.022) goto S20;
/*
CASE 3: A .GT. 13.022
*/
b = 1.77;
si = 0.75;
c = 0.1515/s;
goto S40;
S20:
/*
CASE 2: 3.686 .LT. A .LE. 13.022
*/
b = 1.654+7.6E-3*s2;
si = 1.68/s+0.275;
c = 6.2E-2/s+2.4E-2;
goto S40;
S30:
/*
CASE 1: A .LE. 3.686
*/
b = 0.463+s+0.178*s2;
si = 1.235;
c = 0.195/s-7.9E-2+1.6E-1*s;
S40:
/*
STEP 5: NO QUOTIENT TEST IF X NOT POSITIVE
*/
if(x <= 0.0) goto S70;
/*
STEP 6: CALCULATION OF V AND QUOTIENT Q
*/
v = t/(s+s);
if(fabs(v) <= 0.25) goto S50;
q = q0-s*t+0.25*t*t+(s2+s2)*log(1.0+v);
goto S60;
S50:
q = q0+0.5*t*t*((((((a7*v+a6)*v+a5)*v+a4)*v+a3)*v+a2)*v+a1)*v;
S60:
/*
STEP 7: QUOTIENT ACCEPTANCE (Q)
*/
if(log(1.0-u) <= q) return sgamma;
S70:
/*
STEP 8: E=STANDARD EXPONENTIAL DEVIATE
U= 0,1 -UNIFORM DEVIATE
T=(B,SI)-DOUBLE EXPONENTIAL (LAPLACE) SAMPLE
*/
e = sexpo(o);
u = ranf(o);
u += (u-1.0);
t = b+fsign(si*e,u);
/*
STEP 9: REJECTION IF T .LT. TAU(1) = -.71874483771719
*/
if(t < -0.7187449) goto S70;
/*
STEP 10: CALCULATION OF V AND QUOTIENT Q
*/
v = t/(s+s);
if(fabs(v) <= 0.25) goto S80;
q = q0-s*t+0.25*t*t+(s2+s2)*log(1.0+v);
goto S90;
S80:
q = q0+0.5*t*t*((((((a7*v+a6)*v+a5)*v+a4)*v+a3)*v+a2)*v+a1)*v;
S90:
/*
STEP 11: HAT ACCEPTANCE (H) (IF Q NOT POSITIVE GO TO STEP 8)
*/
if(q <= 0.0) goto S70;
if(q <= 0.5) goto S100;
w = exp(q)-1.0;
goto S110;
S100:
w = ((((e5*q+e4)*q+e3)*q+e2)*q+e1)*q;
S110:
/*
IF T IS REJECTED, SAMPLE AGAIN AT STEP 8
*/
if(c*fabs(u) > w*exp(e-0.5*t*t)) goto S70;
x = s+0.5*t;
sgamma = x*x;
return sgamma;
S120:
/*
ALTERNATE METHOD FOR PARAMETERS A BELOW 1 (.3678794=EXP(-1.))
*/
aa = 0.0;
b = 1.0+0.3678794*a;
S130:
p = b*ranf(o);
if(p >= 1.0) goto S140;
sgamma = exp(log(p)/ a);
if(sexpo(o) < sgamma) goto S130;
return sgamma;
S140:
sgamma = -log((b-p)/ a);
if(sexpo(o) < (1.0-a)*log(sgamma)) goto S130;
return sgamma;
}
/* {=================================================================
* Full deviates
* ==================================================================} */
double genbet(nl_RNG *o,double aa,double bb)
/*
**********************************************************************
GeNerate BETa random deviate
Function
Returns a single random deviate from the beta distribution with
parameters A and B. The density of the beta is
x^(a-1) * (1-x)^(b-1) / B(a,b) for 0 < x < 1
Arguments
aa --> First parameter of the beta distribution
bb --> Second parameter of the beta distribution
Method
R. C. H. Cheng
Generating Beta Variatew with Nonintegral Shape Parameters
Communications of the ACM, 21:317-322 (1978)
(Algorithms BB and BC)
**********************************************************************
*/
{
#define expmax 89.0
#define infnty 1.0E38
static double olda = -1.0;
static double oldb = -1.0;
static double genbet,a,alpha,b,beta,delta,gamma,k1,k2,r,s,t,u1,u2,v,w,y,z;
static long qsame;
qsame = olda == aa && oldb == bb;
if(qsame) goto S20;
if(!(aa <= 0.0 || bb <= 0.0)) goto S10;
fputs(" AA or BB <= 0 in GENBET - Abort!",stderr);
fprintf(stderr," AA: %16.6E BB %16.6E\n",aa,bb);
exit(1);
S10:
olda = aa;
oldb = bb;
S20:
if(!(min(aa,bb) > 1.0)) goto S100;
/*
Alborithm BB
Initialize
*/
if(qsame) goto S30;
a = min(aa,bb);
b = max(aa,bb);
alpha = a+b;
beta = sqrt((alpha-2.0)/(2.0*a*b-alpha));
gamma = a+1.0/beta;
S30:
S40:
u1 = ranf(o);
/*
Step 1
*/
u2 = ranf(o);
v = beta*log(u1/(1.0-u1));
if(!(v > expmax)) goto S50;
w = infnty;
goto S60;
S50:
w = a*exp(v);
S60:
z = pow(u1,2.0)*u2;
r = gamma*v-1.3862944;
s = a+r-w;
/*
Step 2
*/
if(s+2.609438 >= 5.0*z) goto S70;
/*
Step 3
*/
t = log(z);
if(s > t) goto S70;
/*
Step 4
*/
if(r+alpha*log(alpha/(b+w)) < t) goto S40;
S70:
/*
Step 5
*/
if(!(aa == a)) goto S80;
genbet = w/(b+w);
goto S90;
S80:
genbet = b/(b+w);
S90:
goto S230;
S100:
/*
Algorithm BC
Initialize
*/
if(qsame) goto S110;
a = max(aa,bb);
b = min(aa,bb);
alpha = a+b;
beta = 1.0/b;
delta = 1.0+a-b;
k1 = delta*(1.38889E-2+4.16667E-2*b)/(a*beta-0.777778);
k2 = 0.25+(0.5+0.25/delta)*b;
S110:
S120:
u1 = ranf(o);
/*
Step 1
*/
u2 = ranf(o);
if(u1 >= 0.5) goto S130;
/*
Step 2
*/
y = u1*u2;
z = u1*y;
if(0.25*u2+z-y >= k1) goto S120;
goto S170;
S130:
/*
Step 3
*/
z = pow(u1,2.0)*u2;
if(!(z <= 0.25)) goto S160;
v = beta*log(u1/(1.0-u1));
if(!(v > expmax)) goto S140;
w = infnty;
goto S150;
S140:
w = a*exp(v);
S150:
goto S200;
S160:
if(z >= k2) goto S120;
S170:
/*
Step 4
Step 5
*/
v = beta*log(u1/(1.0-u1));
if(!(v > expmax)) goto S180;
w = infnty;
goto S190;
S180:
w = a*exp(v);
S190:
if(alpha*(log(alpha/(b+w))+v)-1.3862944 < log(z)) goto S120;
S200:
/*
Step 6
*/
if(!(a == aa)) goto S210;
genbet = w/(b+w);
goto S220;
S210:
genbet = b/(b+w);
S230:
S220:
return genbet;
#undef expmax
#undef infnty
}
double genchi(nl_RNG *o,double df)
/*
**********************************************************************
Generate random value of CHIsquare variable
Function
Generates random deviate from the distribution of a chisquare
with DF degrees of freedom random variable.
Arguments
df --> Degrees of freedom of the chisquare
(Must be positive)
Method
Uses relation between chisquare and gamma.
**********************************************************************
*/
{
static double genchi;
if(!(df <= 0.0)) goto S10;
fputs("DF <= 0 in GENCHI - ABORT",stderr);
fprintf(stderr,"Value of DF: %16.6E\n",df);
exit(1);
S10:
genchi = 2.0*gengam(o,1.0,df/2.0);
return genchi;
}
double genexp(nl_RNG *o,double av)
/*
**********************************************************************
GENerate EXPonential random deviate
Function
Generates a single random deviate from an exponential
distribution with mean AV.
Arguments
av --> The mean of the exponential distribution from which
a random deviate is to be generated.
Method
Renames SEXPO from TOMS as slightly modified by BWB to use RANF
instead of SUNIF.
For details see:
Ahrens, J.H. and Dieter, U.
Computer Methods for Sampling From the
Exponential and Normal Distributions.
Comm. ACM, 15,10 (Oct. 1972), 873 - 882.
**********************************************************************
*/
{
static float genexp;
genexp = sexpo(o)*av;
return genexp;
}
double genf(nl_RNG *o,double dfn,double dfd)
/*
**********************************************************************
GENerate random deviate from the F distribution
Function
Generates a random deviate from the F (variance ratio)
distribution with DFN degrees of freedom in the numerator
and DFD degrees of freedom in the denominator.
Arguments
dfn --> Numerator degrees of freedom
(Must be positive)
dfd --> Denominator degrees of freedom
(Must be positive)
Method
Directly generates ratio of chisquare variates
**********************************************************************
*/
{
static double genf,xden,xnum;
if(!(dfn <= 0.0 || dfd <= 0.0)) goto S10;
fputs("Degrees of freedom nonpositive in GENF - abort!",stderr);
fprintf(stderr,"DFN value: %16.6EDFD value: %16.6E\n",dfn,dfd);
exit(1);
S10:
xnum = genchi(o,dfn)/dfn;
/*
GENF = ( GENCHI( DFN ) / DFN ) / ( GENCHI( DFD ) / DFD )
*/
xden = genchi(o,dfd)/dfd;
if(!(xden <= 9.999999999998E-39*xnum)) goto S20;
fputs(" GENF - generated numbers would cause overflow",stderr);
fprintf(stderr," Numerator %16.6E Denominator %16.6E\n",xnum,xden);
fputs(" GENF returning 1.0E38",stderr);
genf = 1.0E38;
goto S30;
S20:
genf = xnum/xden;
S30:
return genf;
}
double gengam(nl_RNG *o,double a,double r)
/*
**********************************************************************
GENerates random deviates from GAMma distribution
Function
Generates random deviates from the gamma distribution whose
density is
(A**R)/Gamma(R) * X**(R-1) * Exp(-A*X)
Arguments
a --> Location parameter of Gamma distribution
r --> Shape parameter of Gamma distribution
Method
Renames SGAMMA from TOMS as slightly modified by BWB to use RANF
instead of SUNIF.
For details see:
(Case R >= 1.0)
Ahrens, J.H. and Dieter, U.
Generating Gamma Variates by a
Modified Rejection Technique.
Comm. ACM, 25,1 (Jan. 1982), 47 - 54.
Algorithm GD
(Case 0.0 <= R <= 1.0)
Ahrens, J.H. and Dieter, U.
Computer Methods for Sampling from Gamma,
Beta, Poisson and Binomial Distributions.
Computing, 12 (1974), 223-246/
Adapted algorithm GS.
**********************************************************************
*/
{
static double gengam;
gengam = sgamma(o,r);
gengam /= a;
return gengam;
}
void genmul(nl_RNG *o,long n,double *p,long ncat,long *ix)
/*
**********************************************************************
GENerate an observation from the MULtinomial distribution
Arguments
N --> Number of events that will be classified into one of
the categories 1..NCAT
P --> Vector of probabilities. P(i) is the probability that
an event will be classified into category i. Thus, P(i)
must be [0,1]. Only the first NCAT-1 P(i) must be defined
since P(NCAT) is 1.0 minus the sum of the first
NCAT-1 P(i).
NCAT --> Number of categories. Length of P and IX.
IX <-- Observation from multinomial distribution. All IX(i)
will be nonnegative and their sum will be N.
Method
Algorithm from page 559 of
Devroye, Luc
Non-Uniform Random Variate Generation. Springer-Verlag,
New York, 1986.
**********************************************************************
*/
{
static double prob,ptot,sum;
static long i,icat,ntot;
if(n < 0) ftnstop("N < 0 in GENMUL");
if(ncat <= 1) ftnstop("NCAT <= 1 in GENMUL");
ptot = 0.0F;
for(i=0; i<ncat-1; i++) {
if(*(p+i) < 0.0F) ftnstop("Some P(i) < 0 in GENMUL");
if(*(p+i) > 1.0F) ftnstop("Some P(i) > 1 in GENMUL");
ptot += *(p+i);
}
if(ptot > 0.99999F) ftnstop("Sum of P(i) > 1 in GENMUL");
/*
Initialize variables
*/
ntot = n;
sum = 1.0F;
for(i=0; i<ncat; i++) ix[i] = 0;
/*
Generate the observation
*/
for(icat=0; icat<ncat-1; icat++) {
prob = *(p+icat)/sum;
*(ix+icat) = ignbin(o,ntot,prob);
ntot -= *(ix+icat);
if(ntot <= 0) return;
sum -= *(p+icat);
}
*(ix+ncat-1) = ntot;
/*
Finished
*/
return;
}
double gennch(nl_RNG *o,double df,double xnonc)
/*
**********************************************************************
Generate random value of Noncentral CHIsquare variable
Function
Generates random deviate from the distribution of a noncentral
chisquare with DF degrees of freedom and noncentrality parameter
xnonc.
Arguments
df --> Degrees of freedom of the chisquare
(Must be > 1.0)
xnonc --> Noncentrality parameter of the chisquare
(Must be >= 0.0)
Method
Uses fact that noncentral chisquare is the sum of a chisquare
deviate with DF-1 degrees of freedom plus the square of a normal
deviate with mean XNONC and standard deviation 1.
**********************************************************************
*/
{
static double gennch;
if(!(df <= 1.0 || xnonc < 0.0)) goto S10;
fputs("DF <= 1 or XNONC < 0 in GENNCH - ABORT",stderr);
fprintf(stderr,"Value of DF: %16.6E Value of XNONC%16.6E\n",df,xnonc);
exit(1);
S10:
gennch = genchi(o,df-1.0)+pow(gennor(o,sqrt(xnonc),1.0),2.0);
return gennch;
}
double gennf(nl_RNG *o,double dfn,double dfd,double xnonc)
/*
**********************************************************************
GENerate random deviate from the Noncentral F distribution
Function
Generates a random deviate from the noncentral F (variance ratio)
distribution with DFN degrees of freedom in the numerator, and DFD
degrees of freedom in the denominator, and noncentrality parameter
XNONC.
Arguments
dfn --> Numerator degrees of freedom
(Must be >= 1.0)
dfd --> Denominator degrees of freedom
(Must be positive)
xnonc --> Noncentrality parameter
(Must be nonnegative)
Method
Directly generates ratio of noncentral numerator chisquare variate
to central denominator chisquare variate.
**********************************************************************
*/
{
static double gennf,xden,xnum;
static long qcond;
qcond = dfn <= 1.0 || dfd <= 0.0 || xnonc < 0.0;
if(!qcond) goto S10;
fputs("In GENNF - Either (1) Numerator DF <= 1.0 or",stderr);
fputs("(2) Denominator DF < 0.0 or ",stderr);
fputs("(3) Noncentrality parameter < 0.0",stderr);
fprintf(stderr,
"DFN value: %16.6EDFD value: %16.6EXNONC value: \n%16.6E\n",dfn,dfd,
xnonc);
exit(1);
S10:
xnum = gennch(o,dfn,xnonc)/dfn;
/*
GENNF = ( GENNCH( DFN, XNONC ) / DFN ) / ( GENCHI( DFD ) / DFD )
*/
xden = genchi(o,dfd)/dfd;
if(!(xden <= 9.999999999998E-39*xnum)) goto S20;
fputs(" GENNF - generated numbers would cause overflow",stderr);
fprintf(stderr," Numerator %16.6E Denominator %16.6E\n",xnum,xden);
fputs(" GENNF returning 1.0E38",stderr);
gennf = 1.0E38;
goto S30;
S20:
gennf = xnum/xden;
S30:
return gennf;
}
double gennor(nl_RNG *o,double av,double sd)
/*
**********************************************************************
GENerate random deviate from a NORmal distribution
Function
Generates a single random deviate from a normal distribution
with mean, AV, and standard deviation, SD.
Arguments
av --> Mean of the normal distribution.
sd --> Standard deviation of the normal distribution.
Method
Renames SNORM from TOMS as slightly modified by BWB to use RANF
instead of SUNIF.
For details see:
Ahrens, J.H. and Dieter, U.
Extensions of Forsythe's Method for Random
Sampling from the Normal Distribution.
Math. Comput., 27,124 (Oct. 1973), 927 - 937.
**********************************************************************
*/
{
static double gennor;
gennor = sd*snorm(o)+av;
return gennor;
}
void genprm(nl_RNG *o,long *iarray,int larray)
/*
**********************************************************************
GENerate random PeRMutation of iarray
Arguments
iarray <--> On output IARRAY is a random permutation of its
value on input
larray <--> Length of IARRAY
**********************************************************************
*/
{
static long i,itmp,iwhich,D1,D2;
for(i=1,D1=1,D2=(larray-i+D1)/D1; D2>0; D2--,i+=D1) {
iwhich = ignuin(o,i,larray);
itmp = *(iarray+iwhich-1);
*(iarray+iwhich-1) = *(iarray+i-1);
*(iarray+i-1) = itmp;
}
}
double genunf(nl_RNG *o,double low,double high)
/*
**********************************************************************
float genunf(float low,float high)
GeNerate Uniform Real between LOW and HIGH
Function
Generates a real uniformly distributed between LOW and HIGH.
Arguments
low --> Low bound (exclusive) on real value to be generated
high --> High bound (exclusive) on real value to be generated
**********************************************************************
*/
{
static double genunf;
if(!(low > high)) goto S10;
fprintf(stderr,"LOW > HIGH in GENUNF: LOW %16.6E HIGH: %16.6E\n",low,high);
fputs("Abort",stderr);
exit(1);
S10:
genunf = low+(high-low)*ranf(o);
return genunf;
}
long ignbin(nl_RNG *o,long n,double pp)
/*
**********************************************************************
GENerate BINomial random deviate
Function
Generates a single random deviate from a binomial
distribution whose number of trials is N and whose
probability of an event in each trial is P.
Arguments
n --> The number of trials in the binomial distribution
from which a random deviate is to be generated.
p --> The probability of an event in each trial of the
binomial distribution from which a random deviate
is to be generated.
ignbin <-- A random deviate yielding the number of events
from N independent trials, each of which has
a probability of event P.
Method
This is algorithm BTPE from:
Kachitvichyanukul, V. and Schmeiser, B. W.
Binomial Random Variate Generation.
Communications of the ACM, 31, 2