1
SUBROUTINE cdfnbn(which,p,q,s,xn,pr,ompr,status,bound)
2
C**********************************************************************
4
C SUBROUTINE CDFNBN ( WHICH, P, S, XN, PR, STATUS, BOUND )
5
C Cumulative Distribution Function
6
C Negative BiNomial distribution
12
C Calculates any one parameter of the negative binomial
13
C distribution given values for the others.
15
C The cumulative negative binomial distribution returns the
16
C probability that there will be F or fewer failures before the
17
C XNth success in binomial trials each of which has probability of
20
C The individual term of the negative binomial is the probability of
21
C S failures before XN successes and is
22
C Choose( S, XN+S-1 ) * PR^(XN) * (1-PR)^S
28
C WHICH --> Integer indicating which of the next four argument
29
C values is to be calculated from the others.
31
C iwhich = 1 : Calculate P and Q from S,XN,PR and OMPR
32
C iwhich = 2 : Calculate S from P,Q,XN,PR and OMPR
33
C iwhich = 3 : Calculate XN from P,Q,S,PR and OMPR
34
C iwhich = 4 : Calculate PR and OMPR from P,Q,S and XN
37
C P <--> The cumulation from 0 to S of the negative
38
C binomial distribution.
43
C Input range: (0, 1].
47
C S <--> The upper limit of cumulation of the binomial distribution.
48
C There are F or fewer failures before the XNth success.
49
C Input range: [0, +infinity).
50
C Search range: [0, 1E100]
53
C XN <--> The number of successes.
54
C Input range: [0, +infinity).
55
C Search range: [0, 1E100]
58
C PR <--> The probability of success in each binomial trial.
60
C Search range: [0,1].
67
C DOUBLE PRECISION OMPR
69
C STATUS <-- 0 if calculation completed correctly
70
C -I if input parameter number I is out of range
71
C 1 if answer appears to be lower than lowest
73
C 2 if answer appears to be higher than greatest
76
C 4 if PR + OMPR .ne. 1
79
C BOUND <-- Undefined if STATUS is 0
81
C Bound exceeded by parameter number I if STATUS
84
C Lower search bound if STATUS is 1.
86
C Upper search bound if STATUS is 2.
92
C Formula 26.5.26 of Abramowitz and Stegun, Handbook of
93
C Mathematical Functions (1966) is used to reduce calculation of
94
C the cumulative distribution function to that of an incomplete
97
C Computation of other parameters involve a seach for a value that
98
C produces the desired value of P. The search relies on the
99
C monotinicity of P with the other parameter.
102
C**********************************************************************
105
PARAMETER (tol=1.0D-8)
106
DOUBLE PRECISION atol
107
PARAMETER (atol=1.0D-50)
109
PARAMETER (inf=1.0D100)
111
PARAMETER (one=1.0D0)
113
C .. Scalar Arguments ..
114
DOUBLE PRECISION bound,ompr,p,pr,q,s,xn
117
C .. Local Scalars ..
118
DOUBLE PRECISION ccum,cum,fx,pq,prompr,xhi,xlo
119
LOGICAL qhi,qleft,qporq
121
C .. External Functions ..
122
DOUBLE PRECISION spmpar
125
C .. External Subroutines ..
126
EXTERNAL cumnbn,dinvr,dstinv,dstzr,dzror
128
C .. Intrinsic Functions ..
131
IF (.NOT. ((which.LT.1).OR. (which.GT.4))) GO TO 30
132
IF (.NOT. (which.LT.1)) GO TO 10
140
30 IF (which.EQ.1) GO TO 70
141
IF (.NOT. ((p.LT.0.0D0).OR. (p.GT.1.0D0))) GO TO 60
142
IF (.NOT. (p.LT.0.0D0)) GO TO 40
151
70 IF (which.EQ.1) GO TO 110
152
IF (.NOT. ((q.LE.0.0D0).OR. (q.GT.1.0D0))) GO TO 100
153
IF (.NOT. (q.LE.0.0D0)) GO TO 80
162
110 IF (which.EQ.2) GO TO 130
163
IF (.NOT. (s.LT.0.0D0)) GO TO 120
169
130 IF (which.EQ.3) GO TO 150
170
IF (.NOT. (xn.LT.0.0D0)) GO TO 140
176
150 IF (which.EQ.4) GO TO 190
177
IF (.NOT. ((pr.LT.0.0D0).OR. (pr.GT.1.0D0))) GO TO 180
178
IF (.NOT. (pr.LT.0.0D0)) GO TO 160
187
190 IF (which.EQ.4) GO TO 230
188
IF (.NOT. ((ompr.LT.0.0D0).OR. (ompr.GT.1.0D0))) GO TO 220
189
IF (.NOT. (ompr.LT.0.0D0)) GO TO 200
198
230 IF (which.EQ.1) GO TO 270
200
IF (.NOT. (abs(((pq)-0.5D0)-0.5D0).GT.
201
+ (3.0D0*spmpar(1)))) GO TO 260
202
IF (.NOT. (pq.LT.0.0D0)) GO TO 240
211
270 IF (which.EQ.4) GO TO 310
213
IF (.NOT. (abs(((prompr)-0.5D0)-0.5D0).GT.
214
+ (3.0D0*spmpar(1)))) GO TO 300
215
IF (.NOT. (prompr.LT.0.0D0)) GO TO 280
224
310 IF (.NOT. (which.EQ.1)) qporq = p .LE. q
225
IF ((1).EQ. (which)) THEN
226
CALL cumnbn(s,xn,pr,ompr,p,q)
229
ELSE IF ((2).EQ. (which)) THEN
231
CALL dstinv(0.0D0,inf,0.5D0,0.5D0,5.0D0,atol,tol)
233
CALL dinvr(status,s,fx,qleft,qhi)
234
320 IF (.NOT. (status.EQ.1)) GO TO 350
235
CALL cumnbn(s,xn,pr,ompr,cum,ccum)
236
IF (.NOT. (qporq)) GO TO 330
241
340 CALL dinvr(status,s,fx,qleft,qhi)
244
350 IF (.NOT. (status.EQ.-1)) GO TO 380
245
IF (.NOT. (qleft)) GO TO 360
255
ELSE IF ((3).EQ. (which)) THEN
257
CALL dstinv(0.0D0,inf,0.5D0,0.5D0,5.0D0,atol,tol)
259
CALL dinvr(status,xn,fx,qleft,qhi)
260
390 IF (.NOT. (status.EQ.1)) GO TO 420
261
CALL cumnbn(s,xn,pr,ompr,cum,ccum)
262
IF (.NOT. (qporq)) GO TO 400
267
410 CALL dinvr(status,xn,fx,qleft,qhi)
270
420 IF (.NOT. (status.EQ.-1)) GO TO 450
271
IF (.NOT. (qleft)) GO TO 430
281
ELSE IF ((4).EQ. (which)) THEN
282
CALL dstzr(0.0D0,1.0D0,atol,tol)
283
IF (.NOT. (qporq)) GO TO 480
285
CALL dzror(status,pr,fx,xlo,xhi,qleft,qhi)
287
460 IF (.NOT. (status.EQ.1)) GO TO 470
288
CALL cumnbn(s,xn,pr,ompr,cum,ccum)
290
CALL dzror(status,pr,fx,xlo,xhi,qleft,qhi)
297
CALL dzror(status,ompr,fx,xlo,xhi,qleft,qhi)
299
490 IF (.NOT. (status.EQ.1)) GO TO 500
300
CALL cumnbn(s,xn,pr,ompr,cum,ccum)
302
CALL dzror(status,ompr,fx,xlo,xhi,qleft,qhi)
307
510 IF (.NOT. (status.EQ.-1)) GO TO 540
308
IF (.NOT. (qleft)) GO TO 520