1
SUBROUTINE cdfbin(which,p,q,s,xn,pr,ompr,status,bound)
2
C**********************************************************************
4
C SUBROUTINE CDFBIN ( WHICH, P, Q, S, XN, PR, OMPR, STATUS, BOUND )
5
C Cumulative Distribution Function
6
C BINomial distribution
12
C Calculates any one parameter of the binomial
13
C distribution given values for the others.
19
C WHICH --> Integer indicating which of the next four argument
20
C values is to be calculated from the others.
22
C iwhich = 1 : Calculate P and Q from S,XN,PR and OMPR
23
C iwhich = 2 : Calculate S from P,Q,XN,PR and OMPR
24
C iwhich = 3 : Calculate XN from P,Q,S,PR and OMPR
25
C iwhich = 4 : Calculate PR and OMPR from P,Q,S and XN
28
C P <--> The cumulation from 0 to S of the binomial distribution.
29
C (Probablility of S or fewer successes in XN trials each
30
C with probability of success PR.)
35
C Input range: [0, 1].
39
C S <--> The number of successes observed.
40
C Input range: [0, XN]
41
C Search range: [0, XN]
44
C XN <--> The number of binomial trials.
45
C Input range: (0, +infinity).
46
C Search range: [1E-100, 1E100]
49
C PR <--> The probability of success in each binomial trial.
58
C DOUBLE PRECISION OMPR
60
C STATUS <-- 0 if calculation completed correctly
61
C -I if input parameter number I is out of range
62
C 1 if answer appears to be lower than lowest
64
C 2 if answer appears to be higher than greatest
67
C 4 if PR + OMPR .ne. 1
70
C BOUND <-- Undefined if STATUS is 0
72
C Bound exceeded by parameter number I if STATUS
75
C Lower search bound if STATUS is 1.
77
C Upper search bound if STATUS is 2.
83
C Formula 26.5.24 of Abramowitz and Stegun, Handbook of
84
C Mathematical Functions (1966) is used to reduce the binomial
85
C distribution to the cumulative incomplete beta distribution.
87
C Computation of other parameters involve a seach for a value that
88
C produces the desired value of P. The search relies on the
89
C monotinicity of P with the other parameter.
92
C**********************************************************************
95
PARAMETER (atol=1.0D-50)
97
PARAMETER (tol=1.0D-8)
98
DOUBLE PRECISION zero,inf
99
PARAMETER (zero=1.0D-100,inf=1.0D100)
101
PARAMETER (one=1.0D0)
103
C .. Scalar Arguments ..
104
DOUBLE PRECISION bound,ompr,p,pr,q,s,xn
107
C .. Local Scalars ..
108
DOUBLE PRECISION ccum,cum,fx,pq,prompr,xhi,xlo
109
LOGICAL qhi,qleft,qporq
111
C .. External Functions ..
112
DOUBLE PRECISION spmpar
115
C .. External Subroutines ..
116
EXTERNAL cumbin,dinvr,dstinv,dstzr,dzror
118
C .. Intrinsic Functions ..
121
IF (.NOT. ((which.LT.1).AND. (which.GT.4))) GO TO 30
122
IF (.NOT. (which.LT.1)) GO TO 10
130
30 IF (which.EQ.1) GO TO 70
131
IF (.NOT. ((p.LT.0.0D0).OR. (p.GT.1.0D0))) GO TO 60
132
IF (.NOT. (p.LT.0.0D0)) GO TO 40
141
70 IF (which.EQ.1) GO TO 110
142
IF (.NOT. ((q.LT.0.0D0).OR. (q.GT.1.0D0))) GO TO 100
143
IF (.NOT. (q.LT.0.0D0)) GO TO 80
152
110 IF (which.EQ.3) GO TO 130
153
IF (.NOT. (xn.LE.0.0D0)) GO TO 120
159
130 IF (which.EQ.2) GO TO 170
160
IF (.NOT. ((s.LT.0.0D0).OR. ((which.NE.3).AND.
161
+ (s.GT.xn)))) GO TO 160
162
IF (.NOT. (s.LT.0.0D0)) GO TO 140
171
170 IF (which.EQ.4) GO TO 210
172
IF (.NOT. ((pr.LT.0.0D0).OR. (pr.GT.1.0D0))) GO TO 200
173
IF (.NOT. (pr.LT.0.0D0)) GO TO 180
182
210 IF (which.EQ.4) GO TO 250
183
IF (.NOT. ((ompr.LT.0.0D0).OR. (ompr.GT.1.0D0))) GO TO 240
184
IF (.NOT. (ompr.LT.0.0D0)) GO TO 220
193
250 IF (which.EQ.1) GO TO 290
195
IF (.NOT. (abs(((pq)-0.5D0)-0.5D0).GT.
196
+ (3.0D0*spmpar(1)))) GO TO 280
197
IF (.NOT. (pq.LT.0.0D0)) GO TO 260
206
290 IF (which.EQ.4) GO TO 330
208
IF (.NOT. (abs(((prompr)-0.5D0)-0.5D0).GT.
209
+ (3.0D0*spmpar(1)))) GO TO 320
210
IF (.NOT. (prompr.LT.0.0D0)) GO TO 300
219
330 IF (.NOT. (which.EQ.1)) qporq = p .LE. q
220
IF ((1).EQ. (which)) THEN
221
CALL cumbin(s,xn,pr,ompr,p,q)
224
ELSE IF ((2).EQ. (which)) THEN
226
CALL dstinv(0.0D0,xn,0.5D0,0.5D0,5.0D0,atol,tol)
228
CALL dinvr(status,s,fx,qleft,qhi)
229
340 IF (.NOT. (status.EQ.1)) GO TO 370
230
CALL cumbin(s,xn,pr,ompr,cum,ccum)
231
IF (.NOT. (qporq)) GO TO 350
236
360 CALL dinvr(status,s,fx,qleft,qhi)
239
370 IF (.NOT. (status.EQ.-1)) GO TO 400
240
IF (.NOT. (qleft)) GO TO 380
250
ELSE IF ((3).EQ. (which)) THEN
252
CALL dstinv(zero,inf,0.5D0,0.5D0,5.0D0,atol,tol)
254
CALL dinvr(status,xn,fx,qleft,qhi)
255
410 IF (.NOT. (status.EQ.1)) GO TO 440
256
CALL cumbin(s,xn,pr,ompr,cum,ccum)
257
IF (.NOT. (qporq)) GO TO 420
262
430 CALL dinvr(status,xn,fx,qleft,qhi)
265
440 IF (.NOT. (status.EQ.-1)) GO TO 470
266
IF (.NOT. (qleft)) GO TO 450
276
ELSE IF ((4).EQ. (which)) THEN
277
CALL dstzr(0.0D0,1.0D0,atol,tol)
278
IF (.NOT. (qporq)) GO TO 500
280
CALL dzror(status,pr,fx,xlo,xhi,qleft,qhi)
282
480 IF (.NOT. (status.EQ.1)) GO TO 490
283
CALL cumbin(s,xn,pr,ompr,cum,ccum)
285
CALL dzror(status,pr,fx,xlo,xhi,qleft,qhi)
292
CALL dzror(status,ompr,fx,xlo,xhi,qleft,qhi)
294
510 IF (.NOT. (status.EQ.1)) GO TO 520
295
CALL cumbin(s,xn,pr,ompr,cum,ccum)
297
CALL dzror(status,ompr,fx,xlo,xhi,qleft,qhi)
302
530 IF (.NOT. (status.EQ.-1)) GO TO 560
303
IF (.NOT. (qleft)) GO TO 540