-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathieva.f90
13984 lines (12331 loc) · 375 KB
/
ieva.f90
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
!*********************************************************
!***** FDS5-Evac: Modules and misc routines *****
!*********************************************************
!
! VTT Technical Research Centre of Finland 2008
!
! Author: Timo Korhonen
! Date: 13.8.2008
! FDS Version: 5.2.0
! Evac Version: 2.0.0
!
! This file (ieva.f90) contains:
! * dcdflib.f90 (Netlib cumulative density function library)
! * stat.f90 (random number generation etc, by T.K./Pfs3.0)
! * miscellaneous Evac routines, like initializations
!
!*********************************************************
Module DCDFLIB
Implicit None
CHARACTER(255), PARAMETER :: ievaid='$Id: ieva.f90 7968 2011-03-24 20:05:55Z mcgratta $'
CHARACTER(255), PARAMETER :: ievarev='$Revision: 7968 $'
CHARACTER(255), PARAMETER :: ievadate='$Date: 2011-03-24 14:05:55 -0600 (Thu, 24 Mar 2011) $'
Private
Public cdfbet,cdfgam,cdfnor,GET_REV_ieva,gamma
!
!*********************************************************
!***** DCDFLIB *****
!*********************************************************
!
! The Fortran90 version form:
! http://people.scs.fsu.edu/~burkardt/f_src/dcdflib/dcdflib.html
!
! Assigned goto statements removed by Timo Korhonen, 2008.
! Statement functions replaced with internal function by Timo Korhonen, 2008.
!
! Original F77 version from Netlib: http://www.netlib.org/random:
!
! DCDFLIB
!
! Library of Fortran Routines for Cumulative Distribution
! Functions, Inverses, and Other Parameters
!
! (February, 1994)
! Summary Documentation of Each Routine
!
! Compiled and Written by:
!
! Barry W. Brown
! James Lovato
! Kathy Russell
!
! Department of Biomathematics, Box 237
! The University of Texas, M.D. Anderson Cancer Center
! 1515 Holcombe Boulevard
! Houston, TX 77030
!
! This work was supported by grant CA-16672 from the National Cancer Institute.
!
!
! SUMMARY OF DCDFLIB
!
! This library contains routines to compute cumulative distribution
! functions, inverses, and parameters of the distribution for the
! following set of statistical distributions:
!
! (1) Beta
! (2) Binomial
! (3) Chi-square
! (4) Noncentral Chi-square
! (5) F
! (6) Noncentral F
! (7) Gamma
! (8) Negative Binomial
! (9) Normal
! (10) Poisson
! (11) Student's t
!
! Given values of all but one parameter of a distribution, the other is
! computed. These calculations are done with FORTRAN Double Precision
! variables.
!*********************************************************
!
!
!*********************************************************
!!$ Real ( kind = 8 ) :: algdiv, alnrel, apser, bcorr, beta, beta_asym, &
!!$ beta_frac, beta_log, beta_pser, beta_rcomp, beta_rcomp1, &
!!$ beta_up, dbetrm, dexpm1, dinvnr, dlanor, dstrem, dt1, &
!!$ error_f, error_fc, esum, eval_pol, exparg, fpser, &
!!$ gam1, gamma, gamma_ln1, gamma_log, gsumln, ipmpar, &
!!$ psi, rcomp, rexp, rlog, rlog1, stvaln
Contains
Function algdiv ( a, b )
!*****************************************************************************80
!
!! ALGDIV computes ln ( Gamma ( B ) / Gamma ( A + B ) ) when 8 <= B.
!
! Discussion:
!
! In this algorithm, DEL(X) is the function defined by
!
! ln ( Gamma(X) ) = ( X - 0.5 ) * ln ( X ) - X + 0.5 * ln ( 2 * PI )
! + DEL(X).
!
! Reference:
!
! Armido DiDinato, Alfred Morris,
! Algorithm 708:
! Significant Digit Computation of the Incomplete Beta Function Ratios,
! ACM Transactions on Mathematical Software,
! Volume 18, 1993, pages 360-373.
!
! Parameters:
!
! Input, real ( kind = 8 ) A, B, define the arguments.
!
! Output, real ( kind = 8 ) ALGDIV, the value of ln(Gamma(B)/Gamma(A+B)).
!
Implicit None
Real ( kind = 8 ) a
Real ( kind = 8 ) algdiv
! Real ( kind = 8 ) alnrel
Real ( kind = 8 ) b
Real ( kind = 8 ) c
Real ( kind = 8 ), Parameter :: c0 = 0.833333333333333D-01
Real ( kind = 8 ), Parameter :: c1 = -0.277777777760991D-02
Real ( kind = 8 ), Parameter :: c2 = 0.793650666825390D-03
Real ( kind = 8 ), Parameter :: c3 = -0.595202931351870D-03
Real ( kind = 8 ), Parameter :: c4 = 0.837308034031215D-03
Real ( kind = 8 ), Parameter :: c5 = -0.165322962780713D-02
Real ( kind = 8 ) d
Real ( kind = 8 ) h
Real ( kind = 8 ) s11
Real ( kind = 8 ) s3
Real ( kind = 8 ) s5
Real ( kind = 8 ) s7
Real ( kind = 8 ) s9
Real ( kind = 8 ) t
Real ( kind = 8 ) u
Real ( kind = 8 ) v
Real ( kind = 8 ) w
Real ( kind = 8 ) x
Real ( kind = 8 ) x2
If ( b < a ) Then
h = b / a
c = 1.0D+00 / ( 1.0D+00 + h )
x = h / ( 1.0D+00 + h )
d = a + ( b - 0.5D+00 )
Else
h = a / b
c = h / ( 1.0D+00 + h )
x = 1.0D+00 / ( 1.0D+00 + h )
d = b + ( a - 0.5D+00 )
End If
!
! Set SN = (1 - X**N)/(1 - X).
!
x2 = x * x
s3 = 1.0D+00 + ( x + x2 )
s5 = 1.0D+00 + ( x + x2 * s3 )
s7 = 1.0D+00 + ( x + x2 * s5 )
s9 = 1.0D+00 + ( x + x2 * s7 )
s11 = 1.0D+00 + ( x + x2 * s9 )
!
! Set W = DEL(B) - DEL(A + B).
!
t = ( 1.0D+00 / b )**2
w = (((( &
c5 * s11 * t &
+ c4 * s9 ) * t &
+ c3 * s7 ) * t &
+ c2 * s5 ) * t &
+ c1 * s3 ) * t &
+ c0
w = w * ( c / b )
!
! Combine the results.
!
u = d * alnrel ( a / b )
v = a * ( Log ( b ) - 1.0D+00 )
If ( v < u ) Then
algdiv = ( w - v ) - u
Else
algdiv = ( w - u ) - v
End If
Return
End Function algdiv
Function alnrel ( a )
!*****************************************************************************80
!
!! ALNREL evaluates the function ln ( 1 + A ).
!
! Reference:
!
! Armido DiDinato, Alfred Morris,
! Algorithm 708:
! Significant Digit Computation of the Incomplete Beta Function Ratios,
! ACM Transactions on Mathematical Software,
! Volume 18, 1993, pages 360-373.
!
! Parameters:
!
! Input, real ( kind = 8 ) A, the argument.
!
! Output, real ( kind = 8 ) ALNREL, the value of ln ( 1 + A ).
!
Implicit None
Real ( kind = 8 ) a
Real ( kind = 8 ) alnrel
Real ( kind = 8 ), Parameter :: p1 = -0.129418923021993D+01
Real ( kind = 8 ), Parameter :: p2 = 0.405303492862024D+00
Real ( kind = 8 ), Parameter :: p3 = -0.178874546012214D-01
Real ( kind = 8 ), Parameter :: q1 = -0.162752256355323D+01
Real ( kind = 8 ), Parameter :: q2 = 0.747811014037616D+00
Real ( kind = 8 ), Parameter :: q3 = -0.845104217945565D-01
Real ( kind = 8 ) t
Real ( kind = 8 ) t2
Real ( kind = 8 ) w
Real ( kind = 8 ) x
If ( Abs ( a ) <= 0.375D+00 ) Then
t = a / ( a + 2.0D+00 )
t2 = t * t
w = ((( p3 * t2 + p2 ) * t2 + p1 ) * t2 + 1.0D+00 ) &
/ ((( q3 * t2 + q2 ) * t2 + q1 ) * t2 + 1.0D+00 )
alnrel = 2.0D+00 * t * w
Else
x = 1.0D+00 + Real ( a, kind = 8 )
alnrel = Log ( x )
End If
Return
End Function alnrel
Function apser ( a, b, x, eps )
!*****************************************************************************80
!
!! APSER computes the incomplete beta ratio I(SUB(1-X))(B,A).
!
! Discussion:
!
! APSER is used only for cases where
!
! A <= min ( EPS, EPS * B ),
! B * X <= 1, and
! X <= 0.5.
!
! Reference:
!
! Armido DiDinato, Alfred Morris,
! Algorithm 708:
! Significant Digit Computation of the Incomplete Beta Function Ratios,
! ACM Transactions on Mathematical Software,
! Volume 18, 1993, pages 360-373.
!
! Parameters:
!
! Input, real ( kind = 8 ) A, B, X, the parameters of the
! incomplete beta ratio.
!
! Input, real ( kind = 8 ) EPS, a tolerance.
!
! Output, real ( kind = 8 ) APSER, the computed value of the
! incomplete beta ratio.
!
Implicit None
Real ( kind = 8 ) a
Real ( kind = 8 ) aj
Real ( kind = 8 ) apser
Real ( kind = 8 ) b
Real ( kind = 8 ) bx
Real ( kind = 8 ) c
Real ( kind = 8 ) eps
Real ( kind = 8 ), Parameter :: g = 0.577215664901533D+00
Real ( kind = 8 ) j
! Real ( kind = 8 ) psi
Real ( kind = 8 ) s
Real ( kind = 8 ) t
Real ( kind = 8 ) tol
Real ( kind = 8 ) x
bx = b * x
t = x - bx
If ( b * eps <= 0.02D+00 ) Then
c = Log ( x ) + psi ( b ) + g + t
Else
c = Log ( bx ) + g + t
End If
tol = 5.0D+00 * eps * Abs ( c )
j = 1.0D+00
s = 0.0D+00
Do
j = j + 1.0D+00
t = t * ( x - bx / j )
aj = t / j
s = s + aj
If ( Abs ( aj ) <= tol ) Then
Exit
End If
End Do
apser = -a * ( c + s )
Return
End Function apser
Function bcorr ( a0, b0 )
!*****************************************************************************80
!
!! BCORR evaluates DEL(A0) + DEL(B0) - DEL(A0 + B0).
!
! Discussion:
!
! The function DEL(A) is a remainder term that is used in the expression:
!
! ln ( Gamma ( A ) ) = ( A - 0.5 ) * ln ( A )
! - A + 0.5 * ln ( 2 * PI ) + DEL ( A ),
!
! or, in other words, DEL ( A ) is defined as:
!
! DEL ( A ) = ln ( Gamma ( A ) ) - ( A - 0.5 ) * ln ( A )
! + A + 0.5 * ln ( 2 * PI ).
!
! Reference:
!
! Armido DiDinato, Alfred Morris,
! Algorithm 708:
! Significant Digit Computation of the Incomplete Beta Function Ratios,
! ACM Transactions on Mathematical Software,
! Volume 18, 1993, pages 360-373.
!
! Parameters:
!
! Input, real ( kind = 8 ) A0, B0, the arguments.
! It is assumed that 8 <= A0 and 8 <= B0.
!
! Output, real ( kind = 8 ) BCORR, the value of the function.
!
Implicit None
Real ( kind = 8 ) a
Real ( kind = 8 ) a0
Real ( kind = 8 ) b
Real ( kind = 8 ) b0
Real ( kind = 8 ) bcorr
Real ( kind = 8 ) c
Real ( kind = 8 ), Parameter :: c0 = 0.833333333333333D-01
Real ( kind = 8 ), Parameter :: c1 = -0.277777777760991D-02
Real ( kind = 8 ), Parameter :: c2 = 0.793650666825390D-03
Real ( kind = 8 ), Parameter :: c3 = -0.595202931351870D-03
Real ( kind = 8 ), Parameter :: c4 = 0.837308034031215D-03
Real ( kind = 8 ), Parameter :: c5 = -0.165322962780713D-02
Real ( kind = 8 ) h
Real ( kind = 8 ) s11
Real ( kind = 8 ) s3
Real ( kind = 8 ) s5
Real ( kind = 8 ) s7
Real ( kind = 8 ) s9
Real ( kind = 8 ) t
Real ( kind = 8 ) w
Real ( kind = 8 ) x
Real ( kind = 8 ) x2
a = Min ( a0, b0 )
b = Max ( a0, b0 )
h = a / b
c = h / ( 1.0D+00 + h )
x = 1.0D+00 / ( 1.0D+00 + h )
x2 = x * x
!
! Set SN = (1 - X**N)/(1 - X)
!
s3 = 1.0D+00 + ( x + x2 )
s5 = 1.0D+00 + ( x + x2 * s3 )
s7 = 1.0D+00 + ( x + x2 * s5 )
s9 = 1.0D+00 + ( x + x2 * s7 )
s11 = 1.0D+00 + ( x + x2 * s9 )
!
! Set W = DEL(B) - DEL(A + B)
!
t = ( 1.0D+00 / b )**2
w = (((( &
c5 * s11 * t &
+ c4 * s9 ) * t &
+ c3 * s7 ) * t &
+ c2 * s5 ) * t &
+ c1 * s3 ) * t &
+ c0
w = w * ( c / b )
!
! Compute DEL(A) + W.
!
t = ( 1.0D+00 / a )**2
bcorr = ((((( &
c5 * t &
+ c4 ) * t &
+ c3 ) * t &
+ c2 ) * t &
+ c1 ) * t &
+ c0 ) / a + w
Return
End Function bcorr
Function beta ( a, b )
!*****************************************************************************80
!
!! BETA evaluates the beta function.
!
! Modified:
!
! 03 December 1999
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, real ( kind = 8 ) A, B, the arguments of the beta function.
!
! Output, real ( kind = 8 ) BETA, the value of the beta function.
!
Implicit None
Real ( kind = 8 ) a
Real ( kind = 8 ) b
Real ( kind = 8 ) beta
! Real ( kind = 8 ) beta_log
beta = Exp ( beta_log ( a, b ) )
Return
End Function beta
Function beta_asym ( a, b, lambda, eps )
!*****************************************************************************80
!
!! BETA_ASYM computes an asymptotic expansion for IX(A,B), for large A and B.
!
! Reference:
!
! Armido DiDinato, Alfred Morris,
! Algorithm 708:
! Significant Digit Computation of the Incomplete Beta Function Ratios,
! ACM Transactions on Mathematical Software,
! Volume 18, 1993, pages 360-373.
!
! Parameters:
!
! Input, real ( kind = 8 ) A, B, the parameters of the function.
! A and B should be nonnegative. It is assumed that both A and B
! are greater than or equal to 15.
!
! Input, real ( kind = 8 ) LAMBDA, the value of ( A + B ) * Y - B.
! It is assumed that 0 <= LAMBDA.
!
! Input, real ( kind = 8 ) EPS, the tolerance.
!
Implicit None
Integer, Parameter :: num = 20
Real ( kind = 8 ) a
Real ( kind = 8 ) a0(num+1)
Real ( kind = 8 ) b
Real ( kind = 8 ) b0(num+1)
! Real ( kind = 8 ) bcorr
Real ( kind = 8 ) beta_asym
Real ( kind = 8 ) bsum
Real ( kind = 8 ) c(num+1)
Real ( kind = 8 ) d(num+1)
Real ( kind = 8 ) dsum
Real ( kind = 8 ), Parameter :: e0 = 1.12837916709551D+00
Real ( kind = 8 ), Parameter :: e1 = 0.353553390593274D+00
Real ( kind = 8 ) eps
! Real ( kind = 8 ) error_fc
Real ( kind = 8 ) f
Real ( kind = 8 ) h
Real ( kind = 8 ) h2
Real ( kind = 8 ) hn
Integer i
Integer j
Real ( kind = 8 ) j0
Real ( kind = 8 ) j1
Real ( kind = 8 ) lambda
Integer m
Integer mm1
Integer mmj
Integer n
Integer np1
Real ( kind = 8 ) r
Real ( kind = 8 ) r0
Real ( kind = 8 ) r1
! Real ( kind = 8 ) rlog1
Real ( kind = 8 ) s
Real ( kind = 8 ) sum1
Real ( kind = 8 ) t
Real ( kind = 8 ) t0
Real ( kind = 8 ) t1
Real ( kind = 8 ) u
Real ( kind = 8 ) w
Real ( kind = 8 ) w0
Real ( kind = 8 ) z
Real ( kind = 8 ) z0
Real ( kind = 8 ) z2
Real ( kind = 8 ) zn
Real ( kind = 8 ) znm1
beta_asym = 0.0D+00
If ( a < b ) Then
h = a / b
r0 = 1.0D+00 / ( 1.0D+00 + h )
r1 = ( b - a ) / b
w0 = 1.0D+00 / Sqrt ( a * ( 1.0D+00 + h ))
Else
h = b / a
r0 = 1.0D+00 / ( 1.0D+00 + h )
r1 = ( b - a ) / a
w0 = 1.0D+00 / Sqrt ( b * ( 1.0D+00 + h ))
End If
f = a * rlog1 ( - lambda / a ) + b * rlog1 ( lambda / b )
t = Exp ( - f )
If ( t == 0.0D+00 ) Then
Return
End If
z0 = Sqrt ( f )
z = 0.5D+00 * ( z0 / e1 )
z2 = f + f
a0(1) = ( 2.0D+00 / 3.0D+00 ) * r1
c(1) = -0.5D+00 * a0(1)
d(1) = -c(1)
j0 = ( 0.5D+00 / e0 ) * error_fc ( 1, z0 )
j1 = e1
sum1 = j0 + d(1) * w0 * j1
s = 1.0D+00
h2 = h * h
hn = 1.0D+00
w = w0
znm1 = z
zn = z2
Do n = 2, num, 2
hn = h2 * hn
a0(n) = 2.0D+00 * r0 * ( 1.0D+00 + h * hn ) &
/ ( n + 2.0D+00 )
np1 = n + 1
s = s + hn
a0(np1) = 2.0D+00 * r1 * s / ( n + 3.0D+00 )
Do i = n, np1
r = -0.5D+00 * ( i + 1.0D+00 )
b0(1) = r * a0(1)
Do m = 2, i
bsum = 0.0D+00
mm1 = m - 1
Do j = 1, mm1
mmj = m - j
bsum = bsum + ( j * r - mmj ) * a0(j) * b0(mmj)
End Do
b0(m) = r * a0(m) + bsum / m
End Do
c(i) = b0(i) / ( i + 1.0D+00 )
dsum = 0.0
Do j = 1, i-1
dsum = dsum + d(i-j) * c(j)
End Do
d(i) = - ( dsum + c(i) )
End Do
j0 = e1 * znm1 + ( n - 1.0D+00 ) * j0
j1 = e1 * zn + n * j1
znm1 = z2 * znm1
zn = z2 * zn
w = w0 * w
t0 = d(n) * w * j0
w = w0 * w
t1 = d(np1) * w * j1
sum1 = sum1 + ( t0 + t1 )
If ( ( Abs ( t0 ) + Abs ( t1 )) <= eps * sum1 ) Then
u = Exp ( - bcorr ( a, b ) )
beta_asym = e0 * t * u * sum1
Return
End If
End Do
u = Exp ( - bcorr ( a, b ) )
beta_asym = e0 * t * u * sum1
Return
End Function beta_asym
Function beta_frac ( a, b, x, y, lambda, eps )
!*****************************************************************************80
!
!! BETA_FRAC evaluates a continued fraction expansion for IX(A,B).
!
! Reference:
!
! Armido DiDinato, Alfred Morris,
! Algorithm 708:
! Significant Digit Computation of the Incomplete Beta Function Ratios,
! ACM Transactions on Mathematical Software,
! Volume 18, 1993, pages 360-373.
!
! Parameters:
!
! Input, real ( kind = 8 ) A, B, the parameters of the function.
! A and B should be nonnegative. It is assumed that both A and
! B are greater than 1.
!
! Input, real ( kind = 8 ) X, Y. X is the argument of the
! function, and should satisy 0 <= X <= 1. Y should equal 1 - X.
!
! Input, real ( kind = 8 ) LAMBDA, the value of ( A + B ) * Y - B.
!
! Input, real ( kind = 8 ) EPS, a tolerance.
!
! Output, real ( kind = 8 ) BETA_FRAC, the value of the continued
! fraction approximation for IX(A,B).
!
Implicit None
Real ( kind = 8 ) a
Real ( kind = 8 ) alpha
Real ( kind = 8 ) an
Real ( kind = 8 ) anp1
Real ( kind = 8 ) b
Real ( kind = 8 ) beta
Real ( kind = 8 ) beta_frac
! Real ( kind = 8 ) beta_rcomp
Real ( kind = 8 ) bn
Real ( kind = 8 ) bnp1
Real ( kind = 8 ) c
Real ( kind = 8 ) c0
Real ( kind = 8 ) c1
Real ( kind = 8 ) e
Real ( kind = 8 ) eps
Real ( kind = 8 ) lambda
Real ( kind = 8 ) n
Real ( kind = 8 ) p
Real ( kind = 8 ) r
Real ( kind = 8 ) r0
Real ( kind = 8 ) s
Real ( kind = 8 ) t
Real ( kind = 8 ) w
Real ( kind = 8 ) x
Real ( kind = 8 ) y
Real ( kind = 8 ) yp1
beta_frac = beta_rcomp ( a, b, x, y )
If ( beta_frac == 0.0D+00 ) Then
Return
End If
c = 1.0D+00 + lambda
c0 = b / a
c1 = 1.0D+00 + 1.0D+00 / a
yp1 = y + 1.0D+00
n = 0.0D+00
p = 1.0D+00
s = a + 1.0D+00
an = 0.0D+00
bn = 1.0D+00
anp1 = 1.0D+00
bnp1 = c / c1
r = c1 / c
!
! Continued fraction calculation.
!
Do
n = n + 1.0D+00
t = n / a
w = n * ( b - n ) * x
e = a / s
alpha = ( p * ( p + c0 ) * e * e ) * ( w * x )
e = ( 1.0D+00 + t ) / ( c1 + t + t )
beta = n + w / s + e * ( c + n * yp1 )
p = 1.0D+00 + t
s = s + 2.0D+00
!
! Update AN, BN, ANP1, and BNP1.
!
t = alpha * an + beta * anp1
an = anp1
anp1 = t
t = alpha * bn + beta * bnp1
bn = bnp1
bnp1 = t
r0 = r
r = anp1 / bnp1
If ( Abs ( r - r0 ) <= eps * r ) Then
beta_frac = beta_frac * r
Exit
End If
!
! Rescale AN, BN, ANP1, and BNP1.
!
an = an / bnp1
bn = bn / bnp1
anp1 = r
bnp1 = 1.0D+00
End Do
Return
End Function beta_frac
Subroutine beta_grat ( a, b, x, y, w, eps, ierr )
!*****************************************************************************80
!
!! BETA_GRAT evaluates an asymptotic expansion for IX(A,B).
!
! Reference:
!
! Armido DiDinato, Alfred Morris,
! Algorithm 708:
! Significant Digit Computation of the Incomplete Beta Function Ratios,
! ACM Transactions on Mathematical Software,
! Volume 18, 1993, pages 360-373.
!
! Parameters:
!
! Input, real ( kind = 8 ) A, B, the parameters of the function.
! A and B should be nonnegative. It is assumed that 15 <= A
! and B <= 1, and that B is less than A.
!
! Input, real ( kind = 8 ) X, Y. X is the argument of the
! function, and should satisy 0 <= X <= 1. Y should equal 1 - X.
!
! Input/output, real ( kind = 8 ) W, a quantity to which the
! result of the computation is to be added on output.
!
! Input, real ( kind = 8 ) EPS, a tolerance.
!
! Output, integer IERR, an error flag, which is 0 if no error
! was detected.
!
Implicit None
Real ( kind = 8 ) a
! Real ( kind = 8 ) algdiv
! Real ( kind = 8 ) alnrel
Real ( kind = 8 ) b
Real ( kind = 8 ) bm1
Real ( kind = 8 ) bp2n
Real ( kind = 8 ) c(30)
Real ( kind = 8 ) cn
Real ( kind = 8 ) coef
Real ( kind = 8 ) d(30)
Real ( kind = 8 ) dj
Real ( kind = 8 ) eps
! Real ( kind = 8 ) gam1
Integer i
Integer ierr
Real ( kind = 8 ) j
Real ( kind = 8 ) l
Real ( kind = 8 ) lnx
Integer n
Real ( kind = 8 ) n2
Real ( kind = 8 ) nu
Real ( kind = 8 ) p
Real ( kind = 8 ) q
Real ( kind = 8 ) r
Real ( kind = 8 ) s
Real ( kind = 8 ) sum1
Real ( kind = 8 ) t
Real ( kind = 8 ) t2
Real ( kind = 8 ) u
Real ( kind = 8 ) v
Real ( kind = 8 ) w
Real ( kind = 8 ) x
Real ( kind = 8 ) y
Real ( kind = 8 ) z
bm1 = ( b - 0.5D+00 ) - 0.5D+00
nu = a + 0.5D+00 * bm1
If ( y <= 0.375D+00 ) Then
lnx = alnrel ( - y )
Else
lnx = Log ( x )
End If
z = -nu * lnx
If ( b * z == 0.0D+00 ) Then
ierr = 1
Return
End If
!
! Computation of the expansion.
!
! Set R = EXP(-Z)*Z**B/GAMMA(B)
!
r = b * ( 1.0D+00 + gam1 ( b ) ) * Exp ( b * Log ( z ))
r = r * Exp ( a * lnx ) * Exp ( 0.5D+00 * bm1 * lnx )
u = algdiv ( b, a ) + b * Log ( nu )
u = r * Exp ( - u )
If ( u == 0.0D+00 ) Then
ierr = 1
Return
End If
Call gamma_rat1 ( b, z, r, p, q, eps )
v = 0.25D+00 * ( 1.0D+00 / nu )**2
t2 = 0.25D+00 * lnx * lnx
l = w / u
j = q / r
sum1 = j
t = 1.0D+00
cn = 1.0D+00
n2 = 0.0D+00
Do n = 1, 30
bp2n = b + n2
j = ( bp2n * ( bp2n + 1.0D+00 ) * j &
+ ( z + bp2n + 1.0D+00 ) * t ) * v
n2 = n2 + 2.0D+00
t = t * t2
cn = cn / ( n2 * ( n2 + 1.0D+00 ))
c(n) = cn
s = 0.0D+00
coef = b - n
Do i = 1, n-1
s = s + coef * c(i) * d(n-i)
coef = coef + b
End Do
d(n) = bm1 * cn + s / n
dj = d(n) * j
sum1 = sum1 + dj
If ( sum1 <= 0.0D+00 ) Then
ierr = 1
Return
End If
If ( Abs ( dj ) <= eps * ( sum1 + l ) ) Then
ierr = 0
w = w + u * sum1
Return
End If
End Do
ierr = 0
w = w + u * sum1
Return
End Subroutine beta_grat
Subroutine beta_inc ( a, b, x, y, w, w1, ierr )
!*****************************************************************************80
!
!! BETA_INC evaluates the incomplete beta function IX(A,B).
!
! Author:
!
! Alfred Morris,
! Naval Surface Weapons Center,
! Dahlgren, Virginia.
!
! Reference:
!
! Armido DiDinato, Alfred Morris,
! Algorithm 708:
! Significant Digit Computation of the Incomplete Beta Function Ratios,
! ACM Transactions on Mathematical Software,
! Volume 18, 1993, pages 360-373.
!
! Parameters:
!
! Input, real ( kind = 8 ) A, B, the parameters of the function.
! A and B should be nonnegative.
!
! Input, real ( kind = 8 ) X, Y. X is the argument of the
! function, and should satisy 0 <= X <= 1. Y should equal 1 - X.
!
! Output, real ( kind = 8 ) W, W1, the values of IX(A,B) and
! 1-IX(A,B).
!
! Output, integer IERR, the error flag.
! 0, no error was detected.
! 1, A or B is negative;
! 2, A = B = 0;
! 3, X < 0 or 1 < X;
! 4, Y < 0 or 1 < Y;
! 5, X + Y /= 1;
! 6, X = A = 0;
! 7, Y = B = 0.
!
Implicit None
Real ( kind = 8 ) a
Real ( kind = 8 ) a0
! Real ( kind = 8 ) apser
Real ( kind = 8 ) b
Real ( kind = 8 ) b0
! Real ( kind = 8 ) beta_asym
! Real ( kind = 8 ) beta_frac
! Real ( kind = 8 ) beta_pser
! Real ( kind = 8 ) beta_up
Real ( kind = 8 ) eps
! Real ( kind = 8 ) fpser
Integer ierr
Integer ierr1
Integer ind
Real ( kind = 8 ) lambda
Integer n
Real ( kind = 8 ) t
Real ( kind = 8 ) w
Real ( kind = 8 ) w1
Real ( kind = 8 ) x
Real ( kind = 8 ) x0
Real ( kind = 8 ) y
Real ( kind = 8 ) y0
Real ( kind = 8 ) z
eps = Epsilon ( eps )
w = 0.0D+00
w1 = 0.0D+00
If ( a < 0.0D+00 .Or. b < 0.0D+00 ) Then
ierr = 1
Return
End If
If ( a == 0.0D+00 .And. b == 0.0D+00 ) Then
ierr = 2
Return
End If
If ( x < 0.0D+00 .Or. 1.0D+00 < x ) Then
ierr = 3
Return
End If
If ( y < 0.0D+00 .Or. 1.0D+00 < y ) Then
ierr = 4
Return