betai.f

Go to the documentation of this file.
00001 *DECK BETAI
00002       REAL FUNCTION BETAI (X, PIN, QIN)
00003 C***BEGIN PROLOGUE  BETAI
00004 C***PURPOSE  Calculate the incomplete Beta function.
00005 C***LIBRARY   SLATEC (FNLIB)
00006 C***CATEGORY  C7F
00007 C***TYPE      SINGLE PRECISION (BETAI-S, DBETAI-D)
00008 C***KEYWORDS  FNLIB, INCOMPLETE BETA FUNCTION, SPECIAL FUNCTIONS
00009 C***AUTHOR  Fullerton, W., (LANL)
00010 C***DESCRIPTION
00011 C
00012 C   BETAI calculates the REAL incomplete beta function.
00013 C
00014 C   The incomplete beta function ratio is the probability that a
00015 C   random variable from a beta distribution having parameters PIN and
00016 C   QIN will be less than or equal to X.
00017 C
00018 C     -- Input Arguments -- All arguments are REAL.
00019 C   X      upper limit of integration.  X must be in (0,1) inclusive.
00020 C   PIN    first beta distribution parameter.  PIN must be .GT. 0.0.
00021 C   QIN    second beta distribution parameter.  QIN must be .GT. 0.0.
00022 C
00023 C***REFERENCES  Nancy E. Bosten and E. L. Battiste, Remark on Algorithm
00024 C                 179, Communications of the ACM 17, 3 (March 1974),
00025 C                 pp. 156.
00026 C***ROUTINES CALLED  ALBETA, R1MACH, XERMSG
00027 C***REVISION HISTORY  (YYMMDD)
00028 C   770401  DATE WRITTEN
00029 C   890531  Changed all specific intrinsics to generic.  (WRB)
00030 C   890531  REVISION DATE from Version 3.2
00031 C   891214  Prologue converted to Version 4.0 format.  (BAB)
00032 C   900315  CALLs to XERROR changed to CALLs to XERMSG.  (THJ)
00033 C   900326  Removed duplicate information from DESCRIPTION section.
00034 C           (WRB)
00035 C   920528  DESCRIPTION and REFERENCES sections revised.  (WRB)
00036 C***END PROLOGUE  BETAI
00037       LOGICAL FIRST
00038       SAVE EPS, ALNEPS, SML, ALNSML, FIRST
00039       DATA FIRST /.TRUE./
00040 C***FIRST EXECUTABLE STATEMENT  BETAI
00041       IF (FIRST) THEN
00042          EPS = R1MACH(3)
00043          ALNEPS = LOG(EPS)
00044          SML = R1MACH(1)
00045          ALNSML = LOG(SML)
00046       ENDIF
00047       FIRST = .FALSE.
00048 C
00049       IF (X .LT. 0. .OR. X .GT. 1.0) CALL XERMSG ('SLATEC', 'BETAI',
00050      +   'X IS NOT IN THE RANGE (0,1)', 1, 2)
00051       IF (PIN .LE. 0. .OR. QIN .LE. 0.) CALL XERMSG ('SLATEC', 'BETAI',
00052      +   'P AND/OR Q IS LE ZERO', 2, 2)
00053 C
00054       Y = X
00055       P = PIN
00056       Q = QIN
00057       IF (Q.LE.P .AND. X.LT.0.8) GO TO 20
00058       IF (X.LT.0.2) GO TO 20
00059       Y = 1.0 - Y
00060       P = QIN
00061       Q = PIN
00062 C
00063  20   IF ((P+Q)*Y/(P+1.).LT.EPS) GO TO 80
00064 C
00065 C EVALUATE THE INFINITE SUM FIRST.
00066 C TERM WILL EQUAL Y**P/BETA(PS,P) * (1.-PS)I * Y**I / FAC(I)
00067 C
00068       PS = Q - AINT(Q)
00069       IF (PS.EQ.0.) PS = 1.0
00070       XB = P*LOG(Y) -  ALBETA(PS, P) - LOG(P)
00071       BETAI = 0.0
00072       IF (XB.LT.ALNSML) GO TO 40
00073 C
00074       BETAI = EXP (XB)
00075       TERM = BETAI*P
00076       IF (PS.EQ.1.0) GO TO 40
00077 C
00078       N = MAX (ALNEPS/LOG(Y), 4.0E0)
00079       DO 30 I=1,N
00080         TERM = TERM*(I-PS)*Y/I
00081         BETAI = BETAI + TERM/(P+I)
00082  30   CONTINUE
00083 C
00084 C NOW EVALUATE THE FINITE SUM, MAYBE.
00085 C
00086  40   IF (Q.LE.1.0) GO TO 70
00087 C
00088       XB = P*LOG(Y) + Q*LOG(1.0-Y) - ALBETA(P,Q) - LOG(Q)
00089       IB = MAX (XB/ALNSML, 0.0E0)
00090       TERM = EXP (XB - IB*ALNSML)
00091       C = 1.0/(1.0-Y)
00092       P1 = Q*C/(P+Q-1.)
00093 C
00094       FINSUM = 0.0
00095       N = Q
00096       IF (Q.EQ.REAL(N)) N = N - 1
00097       DO 50 I=1,N
00098         IF (P1.LE.1.0 .AND. TERM/EPS.LE.FINSUM) GO TO 60
00099         TERM = (Q-I+1)*C*TERM/(P+Q-I)
00100 C
00101         IF (TERM.GT.1.0) IB = IB - 1
00102         IF (TERM.GT.1.0) TERM = TERM*SML
00103 C
00104         IF (IB.EQ.0) FINSUM = FINSUM + TERM
00105  50   CONTINUE
00106 C
00107  60   BETAI = BETAI + FINSUM
00108  70   IF (Y.NE.X .OR. P.NE.PIN) BETAI = 1.0 - BETAI
00109       BETAI = MAX (MIN (BETAI, 1.0), 0.0)
00110       RETURN
00111 C
00112  80   BETAI = 0.0
00113       XB = P*LOG(MAX(Y,SML)) - LOG(P) - ALBETA(P,Q)
00114       IF (XB.GT.ALNSML .AND. Y.NE.0.) BETAI = EXP (XB)
00115       IF (Y.NE.X .OR. P.NE.PIN) BETAI = 1.0 - BETAI
00116       RETURN
00117 C
00118       END
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines