OpenMCTDHB v2.3
|
00001 function alngam ( xvalue, ifault ) 00002 00003 !*****************************************************************************80 00004 ! 00005 !! ALNGAM computes the logarithm of the gamma function. 00006 ! 00007 ! Modified: 00008 ! 00009 ! 13 January 2008 00010 ! 00011 ! Author: 00012 ! 00013 ! Original FORTRAN77 version by Allan Macleod. 00014 ! FORTRAN90 version by John Burkardt. 00015 ! 00016 ! Reference: 00017 ! 00018 ! Allan Macleod, 00019 ! Algorithm AS 245, 00020 ! A Robust and Reliable Algorithm for the Logarithm of the Gamma Function, 00021 ! Applied Statistics, 00022 ! Volume 38, Number 2, 1989, pages 397-402. 00023 ! 00024 ! Parameters: 00025 ! 00026 ! Input, real ( kind = 8 ) XVALUE, the argument of the Gamma function. 00027 ! 00028 ! Output, integer ( kind = 4 ) IFAULT, error flag. 00029 ! 0, no error occurred. 00030 ! 1, XVALUE is less than or equal to 0. 00031 ! 2, XVALUE is too big. 00032 ! 00033 ! Output, real ( kind = 8 ) ALNGAM, the logarithm of the gamma function of X. 00034 ! 00035 implicit none 00036 00037 real ( kind = 8 ) alngam 00038 real ( kind = 8 ), parameter :: alr2pi = 0.918938533204673D+00 00039 integer ( kind = 4 ) ifault 00040 real ( kind = 8 ), dimension ( 9 ) :: r1 = (/ 00041 -2.66685511495D+00, 00042 -24.4387534237D+00, 00043 -21.9698958928D+00, 00044 11.1667541262D+00, 00045 3.13060547623D+00, 00046 0.607771387771D+00, 00047 11.9400905721D+00, 00048 31.4690115749D+00, 00049 15.2346874070D+00 /) 00050 real ( kind = 8 ), dimension ( 9 ) :: r2 = (/ 00051 -78.3359299449D+00, 00052 -142.046296688D+00, 00053 137.519416416D+00, 00054 78.6994924154D+00, 00055 4.16438922228D+00, 00056 47.0668766060D+00, 00057 313.399215894D+00, 00058 263.505074721D+00, 00059 43.3400022514D+00 /) 00060 real ( kind = 8 ), dimension ( 9 ) :: r3 = (/ 00061 -2.12159572323D+05, 00062 2.30661510616D+05, 00063 2.74647644705D+04, 00064 -4.02621119975D+04, 00065 -2.29660729780D+03, 00066 -1.16328495004D+05, 00067 -1.46025937511D+05, 00068 -2.42357409629D+04, 00069 -5.70691009324D+02 /) 00070 real ( kind = 8 ), dimension ( 5 ) :: r4 = (/ 00071 0.279195317918525D+00, 00072 0.4917317610505968D+00, 00073 0.0692910599291889D+00, 00074 3.350343815022304D+00, 00075 6.012459259764103D+00 /) 00076 real ( kind = 8 ) x 00077 real ( kind = 8 ) x1 00078 real ( kind = 8 ) x2 00079 real ( kind = 8 ), parameter :: xlge = 5.10D+05 00080 real ( kind = 8 ), parameter :: xlgst = 1.0D+30 00081 real ( kind = 8 ) xvalue 00082 real ( kind = 8 ) y 00083 00084 x = xvalue 00085 alngam = 0.0D+00 00086 ! 00087 ! Check the input. 00088 ! 00089 if ( xlgst <= x ) then 00090 ifault = 2 00091 return 00092 end if 00093 00094 if ( x <= 0.0D+00 ) then 00095 ifault = 1 00096 return 00097 end if 00098 00099 ifault = 0 00100 ! 00101 ! Calculation for 0 < X < 0.5 and 0.5 <= X < 1.5 combined. 00102 ! 00103 if ( x < 1.5D+00 ) then 00104 00105 if ( x < 0.5D+00 ) then 00106 00107 alngam = - log ( x ) 00108 y = x + 1.0D+00 00109 ! 00110 ! Test whether X < machine epsilon. 00111 ! 00112 if ( y == 1.0D+00 ) then 00113 return 00114 end if 00115 00116 else 00117 00118 alngam = 0.0D+00 00119 y = x 00120 x = ( x - 0.5D+00 ) - 0.5D+00 00121 00122 end if 00123 00124 alngam = alngam + x * (((( & 00125 r1(5) * y & 00126 + r1(4) ) * y & 00127 + r1(3) ) * y & 00128 + r1(2) ) * y & 00129 + r1(1) ) / (((( & 00130 y & 00131 + r1(9) ) * y & 00132 + r1(8) ) * y & 00133 + r1(7) ) * y & 00134 + r1(6) ) 00135 00136 return 00137 00138 end if 00139 ! 00140 ! Calculation for 1.5 <= X < 4.0. 00141 ! 00142 if ( x < 4.0D+00 ) then 00143 00144 y = ( x - 1.0D+00 ) - 1.0D+00 00145 00146 alngam = y * (((( & 00147 r2(5) * x & 00148 + r2(4) ) * x & 00149 + r2(3) ) * x & 00150 + r2(2) ) * x & 00151 + r2(1) ) / (((( & 00152 x & 00153 + r2(9) ) * x & 00154 + r2(8) ) * x & 00155 + r2(7) ) * x & 00156 + r2(6) ) 00157 ! 00158 ! Calculation for 4.0 <= X < 12.0. 00159 ! 00160 else if ( x < 12.0D+00 ) then 00161 00162 alngam = (((( & 00163 r3(5) * x & 00164 + r3(4) ) * x & 00165 + r3(3) ) * x & 00166 + r3(2) ) * x & 00167 + r3(1) ) / (((( & 00168 x & 00169 + r3(9) ) * x & 00170 + r3(8) ) * x & 00171 + r3(7) ) * x & 00172 + r3(6) ) 00173 ! 00174 ! Calculation for 12.0 <= X. 00175 ! 00176 else 00177 00178 y = log ( x ) 00179 alngam = x * ( y - 1.0D+00 ) - 0.5D+00 * y + alr2pi 00180 00181 if ( x <= xlge ) then 00182 00183 x1 = 1.0D+00 / x 00184 x2 = x1 * x1 00185 00186 alngam = alngam + x1 * ( ( & 00187 r4(3) * & 00188 x2 + r4(2) ) * & 00189 x2 + r4(1) ) / ( ( & 00190 x2 + r4(5) ) * & 00191 x2 + r4(4) ) 00192 00193 end if 00194 00195 end if 00196 00197 return 00198 end 00199 subroutine bbl ( mu, theta, rl, mrl, lm, rnl ) 00200 00201 !*****************************************************************************80 00202 ! 00203 !! BBL calculates the beta binomial log likelihood. 00204 ! 00205 ! Modified: 00206 ! 00207 ! 30 March 1999 00208 ! 00209 ! Author: 00210 ! 00211 ! Original FORTRAN77 version by D Smith. 00212 ! FORTRAN90 version by John Burkardt. 00213 ! 00214 ! Reference: 00215 ! 00216 ! D Smith, 00217 ! Algorithm AS 189, 00218 ! Maximum Likelihood Estimation of the Parameters of the Beta 00219 ! Binomial Distribution, 00220 ! Applied Statistics, 00221 ! Volume 32, Number 2, 1983, pages 196-204. 00222 ! 00223 ! Parameters: 00224 ! 00225 ! Input, real ( kind = 8 ) MU, the estimated value of MU. 00226 ! 00227 ! Input, real ( kind = 8 ) THETA, the estimated value of THETA. 00228 ! 00229 ! Input, integer ( kind = 4 ) RL(MRL,3), array of coefficients of 00230 ! (MU + R * THETA), ( 1 - MU + R * THETA) and ( 1 + R * THETA ) terms. 00231 ! 00232 ! Input, integer ( kind = 4 ) MRL, the first dimension of the RL array, 00233 ! which must be at least the maximum of the values in IN(*). 00234 ! 00235 ! Input, integer ( kind = 4 ) LM(3), contain the values Max ( IX(J) - 1 ), 00236 ! Max ( IN(J) - IX(J) - 1 ), and Max ( IN(J) - 1 ). 00237 ! 00238 ! Output, real ( kind = 8 ) RNL, the log likelihood. 00239 ! 00240 implicit none 00241 00242 integer ( kind = 4 ) mrl 00243 00244 real ( kind = 8 ) a 00245 integer ( kind = 4 ) i 00246 integer ( kind = 4 ) lm(3) 00247 real ( kind = 8 ) mu 00248 integer ( kind = 4 ) mlm 00249 integer ( kind = 4 ) rl(mrl,3) 00250 real ( kind = 8 ) rnl 00251 real ( kind = 8 ) theta 00252 00253 rnl = 0.0D+00 00254 mlm = lm(3) 00255 00256 do i = 1, mlm 00257 00258 a = real ( i - 1, kind = 8 ) * theta 00259 00260 if ( i <= lm(1) ) then 00261 rnl = rnl + real ( rl(i,1), kind = 8 ) * log ( mu + a ) 00262 end if 00263 00264 if ( i <= lm(2) ) then 00265 rnl = rnl + real ( rl(i,2), kind = 8 ) * log ( 1.0D+00 - mu + a ) 00266 end if 00267 00268 rnl = rnl - real ( rl(i,3), kind = 8 ) * log ( 1.0D+00 + a ) 00269 00270 end do 00271 00272 return 00273 end 00274 subroutine bbme ( n, ix, in, inf, mu, theta ) 00275 00276 !*****************************************************************************80 00277 ! 00278 !! BBME estimates MU and THETA of the beta binomial distribution. 00279 ! 00280 ! Discussion: 00281 ! 00282 ! The method of moments is used. 00283 ! 00284 ! Modified: 00285 ! 00286 ! 02 February 2003 00287 ! 00288 ! Author: 00289 ! 00290 ! Original FORTRAN77 version by D Smith. 00291 ! FORTRAN90 version by John Burkardt. 00292 ! 00293 ! Reference: 00294 ! 00295 ! D Smith, 00296 ! Algorithm AS 189, 00297 ! Maximum Likelihood Estimation of the Parameters of the Beta 00298 ! Binomial Distribution, 00299 ! Applied Statistics, 00300 ! Volume 32, Number 2, 1983, pages 196-204. 00301 ! 00302 ! Parameters: 00303 ! 00304 ! Input, integer ( kind = 4 ) N, the number of observations or trials. 00305 ! 00306 ! Input, integer ( kind = 4 ) IX(N), contains the number of successes for 00307 ! each trial. 00308 ! 00309 ! Input, integer ( kind = 4 ) IN(N), contains the number tested on each trial. 00310 ! 00311 ! Input, real ( kind = 8 ) INF, the maximum acceptable value for THETA. 00312 ! 00313 ! Output, real ( kind = 8 ) MU, the estimate for MU. 00314 ! 00315 ! Output, real ( kind = 8 ) THETA, the estimate for THETA. 00316 ! 00317 implicit none 00318 00319 integer ( kind = 4 ) n 00320 00321 real ( kind = 8 ) d1 00322 real ( kind = 8 ) d2 00323 integer ( kind = 4 ) i 00324 integer ( kind = 4 ) in(n) 00325 real ( kind = 8 ) inf 00326 integer ( kind = 4 ) ix(n) 00327 logical j 00328 real ( kind = 8 ) mu 00329 real ( kind = 8 ) p(n) 00330 real ( kind = 8 ) r 00331 real ( kind = 8 ) s 00332 real ( kind = 8 ) theta 00333 real ( kind = 8 ) tp 00334 real ( kind = 8 ) w(n) 00335 real ( kind = 8 ) w_sum 00336 00337 j = .false. 00338 w(1:n) = real ( in(1:n), kind = 8 ) 00339 p(1:n) = real ( ix(1:n), kind = 8 ) / real ( in(1:n), kind = 8 ) 00340 00341 do 00342 00343 w_sum = sum ( w(1:n) ) 00344 tp = dot_product ( w(1:n), p(1:n) ) 00345 00346 tp = tp / w_sum 00347 00348 s = 0.0D+00 00349 d1 = 0.0D+00 00350 d2 = 0.0D+00 00351 do i = 1, n 00352 r = p(i) - tp 00353 s = s + w(i) * r * r 00354 r = w(i) * ( 1.0D+00 - w(i) / w_sum ) 00355 d1 = d1 + r / real ( in(i), kind = 8 ) 00356 d2 = d2 + r 00357 end do 00358 00359 s = real ( n - 1, kind = 8 ) * s / real ( n, kind = 8 ) 00360 r = tp * ( 1.0D+00 - tp ) 00361 00362 if ( r == 0.0D+00 ) then 00363 exit 00364 end if 00365 00366 r = ( s - r * d1 ) / ( r * ( d2 - d1 ) ) 00367 r = max ( r, 0.0D+00 ) 00368 00369 if ( j ) then 00370 exit 00371 end if 00372 00373 w(1:n) = w(1:n) / ( 1.0D+00 + r * ( w(1:n) - 1.0D+00 ) ) 00374 00375 j = .true. 00376 00377 end do 00378 ! 00379 ! Set the estimates. 00380 ! 00381 mu = tp 00382 00383 if ( r < 1.0D+00 ) then 00384 00385 theta = r / ( 1.0D+00 - r ) 00386 00387 theta = min ( theta, inf ) 00388 00389 else 00390 00391 theta = inf 00392 00393 end if 00394 00395 return 00396 end 00397 subroutine bbml ( n, ix, in, rl, mrl, iter, ccrit, mu, theta, mu_se, theta_se, & 00398 rnl, ifault ) 00399 00400 !*****************************************************************************80 00401 ! 00402 !! BBML estimates the parameters of a beta binomial distribution 00403 ! 00404 ! Definition: 00405 ! 00406 ! The beta binomial probability density function for X successes 00407 ! out of N trials is 00408 ! 00409 ! PDF(X) ( N, MU, THETA ) = 00410 ! C(N,X) * Product ( 0 <= R <= X - 1 ) ( MU + R * THETA ) 00411 ! * Product ( 0 <= R <= N - X - 1 ) ( 1 - MU + R * THETA ) 00412 ! / Product ( 0 <= R <= N - 1 ) ( 1 + R * THETA ) 00413 ! 00414 ! where 00415 ! 00416 ! C(N,X) is the combinatorial coefficient; 00417 ! MU is the expectation of the underlying Beta distribution; 00418 ! THETA is a shape parameter. 00419 ! 00420 ! A THETA value of 0 results in a PDF equivalent to the binomial 00421 ! distribution: 00422 ! 00423 ! PDF(X) ( N, MU, 0 ) = C(N,X) * MU**X * ( 1 - MU )**(N-X) 00424 ! 00425 ! This PDF can be reformulated as: 00426 ! 00427 ! PDF2(X)(A,B,C) = Beta(A+X,B+C-X) 00428 ! / ( (C+1) * Beta(X+1,C-X+1) * Beta(A,B) ) for 0 <= X <= C. 00429 ! 00430 ! Given A, B, C for PDF2, the equivalent PDF has: 00431 ! 00432 ! N = C 00433 ! MU = A / ( A + B ) 00434 ! THETA = 1 / ( A + B ) 00435 ! 00436 ! Given N, MU, THETA for PDF, the equivalent PDF2 has: 00437 ! 00438 ! A = MU / THETA 00439 ! B = ( 1 - MU ) / THETA 00440 ! C = N 00441 ! 00442 ! Modified: 00443 ! 00444 ! 30 March 1999 00445 ! 00446 ! Author: 00447 ! 00448 ! Original FORTRAN77 version by D Smith. 00449 ! FORTRAN90 version by John Burkardt. 00450 ! 00451 ! Reference: 00452 ! 00453 ! D Smith, 00454 ! Algorithm AS 189, 00455 ! Maximum Likelihood Estimation of the Parameters of the Beta 00456 ! Binomial Distribution, 00457 ! Applied Statistics, 00458 ! Volume 32, Number 2, 1983, pages 196-204. 00459 ! 00460 ! Parameters: 00461 ! 00462 ! Input, integer ( kind = 4 ) N, the number of observations or trials. 00463 ! 00464 ! Input, integer ( kind = 4 ) IX(N), contains the number of successes for 00465 ! each trial. 00466 ! 00467 ! Input, integer ( kind = 4 ) IN(N), contains the number tested on each trial. 00468 ! 00469 ! Workspace, integer ( kind = 4 ) RL(MRL,3), array of coefficients of 00470 ! (MU + R * THETA), ( 1 - MU + R * THETA) and ( 1 + R * THETA ) terms. 00471 ! 00472 ! Input, integer ( kind = 4 ) MRL, the first dimension of the RL array, 00473 ! which must be at least the maximum of the values in IN(*). 00474 ! 00475 ! Input/output, integer ( kind = 4 ) ITER; 00476 ! On input, the maximum number of iterations allowed. 00477 ! On output, the number of iterations taken. 00478 ! 00479 ! Input, real ( kind = 8 ) CCRIT, the convergence criterion. The iteration is 00480 ! judged to have converged when abs ( delta MU ) and 00481 ! abs ( delta THETA) are less than or equal to CCRIT. 00482 ! 00483 ! Output, real ( kind = 8 ) MU, the maximum likelihood estimate of MU, the mean 00484 ! of the beta binomial distribution. 00485 ! 00486 ! Output, real ( kind = 8 ) THETA, the maximum likelihood estimate of THETA, the 00487 ! shape parameter of the beta binomial distribution. 00488 ! 00489 ! Output, real ( kind = 8 ) MU_SE, the standard error of the estimate of MU; 00490 ! returned as -1.0 if it cannot be calculated. 00491 ! 00492 ! Output, real ( kind = 8 ) THETA_SE, the standard error of the estimate of 00493 ! THETA; returned as -1.0 if it cannot be calculated. 00494 ! 00495 ! Output, real ( kind = 8 ) RNL, the log likelihood for the maximum likelihood 00496 ! estimates. 00497 ! 00498 ! Output, integer ( kind = 4 ) IFAULT, error flag. 00499 ! 0, no error. 00500 ! 1, N <= 1; 00501 ! 2, IX(I) = 0 for all I; 00502 ! 3, IX(I) = IN(I) for all I; 00503 ! 4, max ( IN(I) ) > MRL; 00504 ! 5, either IX(I) < 0 or IN(I) < IX(I) for some I; 00505 ! 6, MU went outside the range of [0,1], or THETA went outside the 00506 ! range [0,INF], where INF represents Infinity; 00507 ! 7, if the maximum number of iterations was exceeded; 00508 ! 8, if the damped Newton-Raphson iteration failed. 00509 ! 00510 implicit none 00511 00512 integer ( kind = 4 ) n 00513 00514 real ( kind = 8 ) a 00515 real ( kind = 8 ) b 00516 real ( kind = 8 ) c 00517 real ( kind = 8 ) ccrit 00518 real ( kind = 8 ) d 00519 real ( kind = 8 ) del 00520 real ( kind = 8 ) dum 00521 real ( kind = 8 ) e 00522 real ( kind = 8 ) eps 00523 real ( kind = 8 ) f 00524 real ( kind = 8 ) fd(2) 00525 integer ( kind = 4 ) ifault 00526 integer ( kind = 4 ) in(n) 00527 real ( kind = 8 ), parameter :: inf = 1.0D+06 00528 integer ( kind = 4 ) iter 00529 integer ( kind = 4 ) iter_max 00530 integer ( kind = 4 ) ix(n) 00531 integer ( kind = 4 ) lm(3) 00532 logical mc 00533 real ( kind = 8 ) mu 00534 real ( kind = 8 ) mu_se 00535 integer ( kind = 4 ) mrl 00536 integer ( kind = 4 ) nnd 00537 integer ( kind = 4 ) rd1(2,2) 00538 integer ( kind = 4 ) rd2(2,3) 00539 integer ( kind = 4 ) rd3(2,4) 00540 integer ( kind = 4 ) rl(mrl,3) 00541 real ( kind = 8 ) rnl 00542 real ( kind = 8 ) sd(3) 00543 real ( kind = 8 ) td(4) 00544 real ( kind = 8 ) theta 00545 real ( kind = 8 ) theta_se 00546 real ( kind = 8 ) ub(2) 00547 00548 rd1(1,1) = 1 00549 rd1(2,1) = - 1 00550 rd1(1,2) = 1 00551 rd1(2,2) = 1 00552 00553 rd2(1,1) = - 1 00554 rd2(2,1) = - 1 00555 rd2(1,2) = - 1 00556 rd2(2,2) = 1 00557 rd2(1,3) = - 1 00558 rd2(2,3) = - 1 00559 00560 rd3(1,1) = 2 00561 rd3(2,1) = - 2 00562 rd3(1,2) = 2 00563 rd3(2,2) = 2 00564 rd3(1,3) = 2 00565 rd3(2,3) = - 2 00566 rd3(1,4) = 2 00567 rd3(2,4) = 2 00568 00569 iter_max = iter 00570 iter = 0 00571 mc = .true. 00572 ub(1) = 0.01D+00 00573 ub(2) = 0.01D+00 00574 ! 00575 ! Set the arrays RL and LM. 00576 ! 00577 call set ( n, ix, in, rl, mrl, lm, ifault ) 00578 00579 if ( ifault /= 0 ) then 00580 return 00581 end if 00582 00583 mu_se = - 1.0D+00 00584 theta_se = - 1.0D+00 00585 nnd = 0 00586 ! 00587 ! Calculate initial estimates by the method of moments. 00588 ! 00589 call bbme ( n, ix, in, inf, mu, theta ) 00590 00591 if ( theta == inf ) then 00592 go to 50 00593 end if 00594 ! 00595 ! Newton-Raphson iteration on first derivatives. 00596 ! 00597 5 continue 00598 00599 if ( iter_max < iter ) then 00600 ifault = 7 00601 go to 60 00602 end if 00603 ! 00604 ! Calculate the first derivatives of the log likelihood. 00605 ! 00606 call gder ( mu, theta, rl, mrl, lm, 2, rd1, fd ) 00607 ! 00608 ! Calculate the second derivatives of the log likelihood. 00609 ! 00610 call gder ( mu, theta, rl, mrl, lm, 3, rd2, sd ) 00611 ! 00612 ! Calculate the third derivatives of the log likelihood. 00613 ! 00614 call gder ( mu, theta, rl, mrl, lm, 4, rd3, td ) 00615 ! 00616 ! Calculate the increments. 00617 ! 00618 dum = sd(1) * sd(3) - sd(2) * sd(2) 00619 00620 if ( sd(1) < 0.0D+00 .and. 0.0D+00 < dum ) then 00621 go to 15 00622 end if 00623 ! 00624 ! Non negative definite matrix. 00625 ! 00626 nnd = nnd + 1 00627 ! 00628 ! SD(1) is always negative so a gradient step is made on MU. 00629 ! 00630 a = mu - fd(1) / sd(1) 00631 b = theta 00632 00633 if ( fd(2) /= 0.0D+00 ) then 00634 b = b + sign ( ub(2), fd(2) ) 00635 end if 00636 00637 if ( a <= 0.0D+00 ) then 00638 a = 0.0001D+00 00639 else if ( 1.0D+00 <= a ) then 00640 a = 0.9999D+00 00641 end if 00642 00643 b = max ( b, 0.0D+00 ) 00644 b = min ( b, inf ) 00645 00646 call bbl ( mu, theta, rl, mrl, lm, c ) 00647 call bbl ( a, b, rl, mrl, lm, d ) 00648 00649 if ( 10 < nnd .or. d <= c ) then 00650 go to 40 00651 end if 00652 00653 iter = iter + 1 00654 mu = a 00655 theta = b 00656 go to 5 00657 00658 15 continue 00659 00660 del = ( fd(2) * sd(2) - fd(1) * sd(3) ) / dum 00661 eps = ( fd(1) * sd(2) - fd(2) * sd(1) ) / dum 00662 ! 00663 ! Check to see if the Lipschitz condition is satisfied. 00664 ! 00665 a = sd(2) * td(2) - td(1) * sd(3) 00666 b = sd(2) * td(3) - td(2) * sd(3) 00667 c = td(1) * sd(2) - td(2) * sd(1) 00668 d = sd(2) * td(2) - sd(1) * td(3) 00669 e = sd(2) * td(4) - td(3) * sd(3) 00670 f = td(3) * sd(2) - td(4) * sd(1) 00671 00672 a = del * a + eps * b 00673 c = del * c + eps * d 00674 e = del * b + eps * e 00675 f = del * d + eps * f 00676 00677 dum = ( a * a + c * c + e * e + f * f ) / dum / dum 00678 00679 if ( 1.0D+00 <= dum ) then 00680 go to 20 00681 end if 00682 00683 if ( abs ( del ) <= ccrit .and. abs ( eps ) <= ccrit ) then 00684 mc = .false. 00685 end if 00686 00687 go to 45 00688 ! 00689 ! Failure of the Lipschitz condition. 00690 ! A step in the direction of the gradient is made. 00691 ! 00692 20 continue 00693 00694 a = fd(1)**2 00695 b = fd(2)**2 00696 c = a * sd(1) + 2.0D+00 * sd(2) * fd(1) * fd(2) + b * sd(3) 00697 00698 if ( c /= 0.0D+00 ) then 00699 00700 c = - ( a + b ) / c 00701 del = c * fd(1) 00702 eps = c * fd(2) 00703 00704 if ( ub(1) < abs ( del ) ) then 00705 del = sign ( ub(1), del ) 00706 end if 00707 00708 ub(1) = 2.0D+00 * abs ( del ) 00709 00710 if ( ub(2) < abs ( eps ) ) then 00711 eps = sign ( ub(2), eps ) 00712 end if 00713 00714 ub(2) = 2.0D+00 * abs ( eps ) 00715 00716 else 00717 00718 if ( fd(1) /= 0.0D+00 ) then 00719 del = sign ( ub(1), fd(1) ) 00720 else 00721 del = 0.0D+00 00722 end if 00723 00724 if ( fd(2) /= 0.0D+00 ) then 00725 eps = sign ( ub(2), fd(2) ) 00726 else 00727 eps = 0.0D+00 00728 end if 00729 00730 end if 00731 00732 call bbl ( mu, theta, rl, mrl, lm, c ) 00733 ! 00734 ! Begin loop. 00735 ! 00736 35 continue 00737 00738 a = mu + del 00739 b = theta + eps 00740 00741 if ( a <= 0.0D+00 ) then 00742 a = 0.0001D+00 00743 end if 00744 00745 if ( 1.0D+00 <= a ) then 00746 a = 0.9999D+00 00747 end if 00748 00749 del = a - mu 00750 00751 b = max ( b, 0.0D+00 ) 00752 b = min ( b, inf ) 00753 00754 eps = b - theta 00755 call bbl ( a, b, rl, mrl, lm, d ) 00756 ! 00757 ! Check to see if gradient step has increased log likelihood. 00758 ! 00759 if ( c < d ) then 00760 go to 45 00761 end if 00762 00763 del = del / 2.0D+00 00764 eps = eps / 2.0D+00 00765 00766 if ( ccrit < abs ( del ) .or. ccrit < abs ( eps ) ) then 00767 go to 35 00768 end if 00769 00770 40 continue 00771 00772 ifault = 8 00773 go to 60 00774 00775 45 continue 00776 00777 iter = iter + 1 00778 a = mu + del 00779 b = theta + eps 00780 00781 if ( 0.0D+00 < a .and. a < 1.0D+00 .and. 0.0D+00 <= b .and. b <= inf ) then 00782 go to 55 00783 end if 00784 00785 if ( a <= 0.0D+00 ) then 00786 mu = 0.0D+00 00787 end if 00788 00789 if ( 1.0D+00 <= a ) then 00790 mu = 1.0D+00 00791 end if 00792 00793 if ( b < 0.0D+00 ) then 00794 theta = 0.0D+00 00795 end if 00796 00797 if ( inf < b ) then 00798 theta = inf 00799 end if 00800 00801 50 continue 00802 00803 ifault = 6 00804 go to 60 00805 00806 55 continue 00807 00808 mu = a 00809 theta = b 00810 00811 if ( mc ) then 00812 go to 5 00813 end if 00814 ! 00815 ! Calculate the standard errors. 00816 ! 00817 if ( sd(1) < 0.0D+00 ) then 00818 mu_se = sqrt ( - 1.0D+00 / sd(1) ) 00819 end if 00820 00821 if ( sd(3) < 0.0D+00 ) then 00822 theta_se = sqrt ( - 1.0D+00 / sd(3) ) 00823 end if 00824 ! 00825 ! Calculate the log likelihood. 00826 ! 00827 60 continue 00828 00829 call bbl ( mu, theta, rl, mrl, lm, rnl ) 00830 00831 return 00832 end 00833 function beta ( a, b ) 00834 00835 !*****************************************************************************80 00836 ! 00837 !! BETA returns the value of the Beta function. 00838 ! 00839 ! Formula: 00840 ! 00841 ! BETA(A,B) = ( GAMMA ( A ) * GAMMA ( B ) ) / GAMMA ( A + B ) 00842 ! = Integral ( 0 <= T <= 1 ) T**(A-1) (1-T)**(B-1) dT. 00843 ! 00844 ! Modified: 00845 ! 00846 ! 10 July 1998 00847 ! 00848 ! Author: 00849 ! 00850 ! John Burkardt 00851 ! 00852 ! Parameters: 00853 ! 00854 ! Input, real ( kind = 8 ) A, B, the parameters of the function. 00855 ! 0.0 < A, 00856 ! 0.0 < B. 00857 ! 00858 ! Output, real ( kind = 8 ) BETA, the value of the function. 00859 ! 00860 implicit none 00861 00862 real ( kind = 8 ) a 00863 real ( kind = 8 ) alngam 00864 real ( kind = 8 ) b 00865 real ( kind = 8 ) beta 00866 integer ( kind = 4 ) ifault 00867 ! 00868 ! Check. 00869 ! 00870 if ( a <= 0.0D+00 .or. b <= 0.0D+00 ) then 00871 write ( *, '(a)' ) ' ' 00872 write ( *, '(a)' ) 'BETA - Fatal error!' 00873 write ( *, '(a)' ) ' Both A and B must be greater than 0.' 00874 stop 00875 end if 00876 00877 beta = exp ( alngam ( a, ifault ) & 00878 + alngam ( b, ifault ) & 00879 - alngam ( a + b, ifault ) ) 00880 00881 return 00882 end 00883 subroutine beta_binomial_cdf_inv ( cdf, a, b, c, x ) 00884 00885 !*****************************************************************************80 00886 ! 00887 !! BETA_BINOMIAL_CDF_INV inverts the Beta Binomial CDF. 00888 ! 00889 ! Discussion: 00890 ! 00891 ! A simple-minded discrete approach is used. 00892 ! 00893 ! Modified: 00894 ! 00895 ! 07 December 1999 00896 ! 00897 ! Author: 00898 ! 00899 ! John Burkardt 00900 ! 00901 ! Parameters: 00902 ! 00903 ! Input, real ( kind = 8 ) CDF, the value of the CDF. 00904 ! 00905 ! Input, real ( kind = 8 ) A, B, parameters of the PDF. 00906 ! 0.0 < A, 00907 ! 0.0 < B. 00908 ! 00909 ! Input, integer ( kind = 4 ) C, a parameter of the PDF. 00910 ! 0 <= C. 00911 ! 00912 ! Output, integer ( kind = 4 ) X, the smallest X whose cumulative density 00913 ! function is greater than or equal to CDF. 00914 ! 00915 implicit none 00916 00917 real ( kind = 8 ) a 00918 real ( kind = 8 ) b 00919 real ( kind = 8 ) beta 00920 integer ( kind = 4 ) c 00921 real ( kind = 8 ) cdf 00922 real ( kind = 8 ) cum 00923 real ( kind = 8 ) pdf 00924 integer ( kind = 4 ) x 00925 integer ( kind = 4 ) y 00926 00927 if ( cdf <= 0.0D+00 ) then 00928 00929 x = 0 00930 00931 else 00932 00933 cum = 0.0D+00 00934 00935 do y = 0, c 00936 00937 pdf = beta ( a + real ( y, kind = 8 ), 00938 b + real ( c - y, kind = 8 ) ) / ( real ( c + 1, kind = 8 ) 00939 * beta ( real ( y + 1, kind = 8 ), 00940 real ( c - y + 1, kind = 8 ) ) * beta ( a, b ) ) 00941 00942 cum = cum + pdf 00943 00944 if ( cdf <= cum ) then 00945 x = y 00946 return 00947 end if 00948 00949 end do 00950 00951 x = c 00952 00953 end if 00954 00955 return 00956 end 00957 subroutine beta_binomial_check ( a, b, c ) 00958 00959 !*****************************************************************************80 00960 ! 00961 !! BETA_BINOMIAL_CHECK checks the parameters of the Beta Binomial PDF. 00962 ! 00963 ! Modified: 00964 ! 00965 ! 07 December 1999 00966 ! 00967 ! Author: 00968 ! 00969 ! John Burkardt 00970 ! 00971 ! Parameters: 00972 ! 00973 ! Input, real ( kind = 8 ) A, B, parameters of the PDF. 00974 ! 0.0 < A, 00975 ! 0.0 < B. 00976 ! 00977 ! Input, integer ( kind = 4 ) C, a parameter of the PDF. 00978 ! 0 <= C. 00979 ! 00980 implicit none 00981 00982 real ( kind = 8 ) a 00983 real ( kind = 8 ) b 00984 integer ( kind = 4 ) c 00985 00986 if ( a <= 0.0D+00 ) then 00987 write ( *, '(a)' ) ' ' 00988 write ( *, '(a)' ) 'BETA_BINOMIAL_CHECK - Fatal error!' 00989 write ( *, '(a)' ) ' A <= 0.' 00990 stop 00991 end if 00992 00993 if ( b <= 0.0D+00 ) then 00994 write ( *, '(a)' ) ' ' 00995 write ( *, '(a)' ) 'BETA_BINOMIAL_CHECK - Fatal error!' 00996 write ( *, '(a)' ) ' B <= 0.' 00997 stop 00998 end if 00999 01000 if ( c < 0 ) then 01001 write ( *, '(a)' ) ' ' 01002 write ( *, '(a)' ) 'BETA_BINOMIAL_CHECK - Fatal error!' 01003 write ( *, '(a)' ) ' C < 0.' 01004 stop 01005 end if 01006 01007 return 01008 end 01009 subroutine beta_binomial_sample ( a, b, c, seed, x ) 01010 01011 !*****************************************************************************80 01012 ! 01013 !! BETA_BINOMIAL_SAMPLE samples the Beta Binomial CDF. 01014 ! 01015 ! Modified: 01016 ! 01017 ! 07 December 1999 01018 ! 01019 ! Author: 01020 ! 01021 ! John Burkardt 01022 ! 01023 ! Parameters: 01024 ! 01025 ! Input, real ( kind = 8 ) A, B, parameters of the PDF. 01026 ! 0.0 < A, 01027 ! 0.0 < B. 01028 ! 01029 ! Input, integer ( kind = 4 ) C, a parameter of the PDF. 01030 ! 0 <= C. 01031 ! 01032 ! Input/output, integer( kind = 4 ) SEED, a seed for the random 01033 ! number generator. 01034 ! 01035 ! Output, integer( kind = 4 ) X, a sample of the PDF. 01036 ! 01037 implicit none 01038 01039 real ( kind = 8 ) a 01040 real ( kind = 8 ) b 01041 integer ( kind = 4 ) c 01042 real ( kind = 8 ) cdf 01043 real ( kind = 8 ) r8_uniform_01 01044 integer ( kind = 4 ) seed 01045 integer ( kind = 4 ) x 01046 01047 cdf = r8_uniform_01 ( seed ) 01048 01049 call beta_binomial_cdf_inv ( cdf, a, b, c, x ) 01050 01051 return 01052 end 01053 subroutine gder ( mu, theta, rl, mrl, lm, ider, rd, pd ) 01054 01055 !*****************************************************************************80 01056 ! 01057 !! GDER computes derivatives of the log likelihood. 01058 ! 01059 ! Modified: 01060 ! 01061 ! 30 March 1999 01062 ! 01063 ! Author: 01064 ! 01065 ! D Smith 01066 ! FORTRAN90 version by John Burkardt 01067 ! 01068 ! Reference: 01069 ! 01070 ! D Smith, 01071 ! Algorithm AS 189, 01072 ! Maximum Likelihood Estimation of the Parameters of the Beta 01073 ! Binomial Distribution, 01074 ! Applied Statistics, 01075 ! Volume 32, Number 2, 1983, pages 196-204. 01076 ! 01077 ! Parameters: 01078 ! 01079 ! Input, real ( kind = 8 ) MU, the estimated value of MU. 01080 ! 01081 ! Input, real ( kind = 8 ) THETA, the estimated value of THETA. 01082 ! 01083 ! Input, integer ( kind = 4 ) RL(MRL,3), array of coefficients of 01084 ! (MU + R * THETA), ( 1 - MU + R * THETA) and ( 1 + R * THETA ) terms. 01085 ! 01086 ! Input, integer ( kind = 4 ) MRL, the first dimension of the RL array, 01087 ! which must be at least the maximum of the values in IN(*). 01088 ! 01089 ! Input, integer ( kind = 4 ) LM(3), contain the values Max ( IX(J) - 1 ), 01090 ! Max ( IN(J) - IX(J) - 1 ), and Max ( IN(J) - 1 ). 01091 ! 01092 ! Input, integer ( kind = 4 ) IDER, 1 plus the order of the derivative 01093 ! desired. IDER can be 2, 3 or 4. 01094 ! 01095 ! Input, integer ( kind = 4 ) RD(2,IDER), an array of coefficients. 01096 ! 01097 ! Output, real ( kind = 8 ) PD(IDER), the derivatives of the log likelihood. 01098 ! 01099 implicit none 01100 01101 integer ( kind = 4 ) ider 01102 integer ( kind = 4 ) mrl 01103 01104 real ( kind = 8 ) a 01105 real ( kind = 8 ) b 01106 real ( kind = 8 ) c 01107 real ( kind = 8 ) d 01108 integer ( kind = 4 ) i 01109 integer ( kind = 4 ) j 01110 integer ( kind = 4 ) k 01111 integer ( kind = 4 ) kk 01112 integer ( kind = 4 ) lm(3) 01113 real ( kind = 8 ) mu 01114 integer ( kind = 4 ) mlm 01115 real ( kind = 8 ) pd(ider) 01116 integer ( kind = 4 ) rd(2,ider) 01117 integer ( kind = 4 ) rl(mrl,3) 01118 real ( kind = 8 ) theta 01119 01120 mlm = lm(3) 01121 kk = ider - 1 01122 01123 pd(1:ider) = 0.0D+00 01124 01125 do i = 1, mlm 01126 01127 c = real ( i - 1, kind = 8 ) 01128 a = c * theta 01129 01130 do j = 1, 3 01131 01132 if ( i <= lm(j) ) then 01133 01134 if ( j == 1 ) then 01135 d = mu + a 01136 else if ( j == 2 ) then 01137 d = 1.0D+00 - mu + a 01138 else if ( j == 3 ) then 01139 d = 1.0D+00 + a 01140 end if 01141 01142 b = real ( rl(i,j) ) / d**kk 01143 01144 if ( j /= 3 ) then 01145 01146 do k = 1, ider 01147 pd(k) = pd(k) + real ( rd(j,k), kind = 8 ) * b 01148 b = b * c 01149 end do 01150 01151 else 01152 01153 d = - real ( rd(1,1), kind = 8 ) * b * c**kk 01154 pd(ider) = pd(ider) + d 01155 01156 end if 01157 01158 end if 01159 01160 end do 01161 01162 end do 01163 01164 return 01165 end 01166 function r8_uniform_01 ( seed ) 01167 01168 !*****************************************************************************80 01169 ! 01170 !! R8_UNIFORM_01 returns a unit pseudorandom R8. 01171 ! 01172 ! Discussion: 01173 ! 01174 ! An R8 is a real ( kind = 8 ) value. 01175 ! 01176 ! For now, the input quantity SEED is an integer variable. 01177 ! 01178 ! This routine implements the recursion 01179 ! 01180 ! seed = 16807 * seed mod ( 2**31 - 1 ) 01181 ! r8_uniform_01 = seed / ( 2**31 - 1 ) 01182 ! 01183 ! The integer arithmetic never requires more than 32 bits, 01184 ! including a sign bit. 01185 ! 01186 ! If the initial seed is 12345, then the first three computations are 01187 ! 01188 ! Input Output R8_UNIFORM_01 01189 ! SEED SEED 01190 ! 01191 ! 12345 207482415 0.096616 01192 ! 207482415 1790989824 0.833995 01193 ! 1790989824 2035175616 0.947702 01194 ! 01195 ! Modified: 01196 ! 01197 ! 31 May 2007 01198 ! 01199 ! Author: 01200 ! 01201 ! John Burkardt 01202 ! 01203 ! Reference: 01204 ! 01205 ! Paul Bratley, Bennett Fox, Linus Schrage, 01206 ! A Guide to Simulation, 01207 ! Second Edition, 01208 ! Springer, 1987, 01209 ! ISBN: 0387964673, 01210 ! LC: QA76.9.C65.B73. 01211 ! 01212 ! Bennett Fox, 01213 ! Algorithm 647: 01214 ! Implementation and Relative Efficiency of Quasirandom 01215 ! Sequence Generators, 01216 ! ACM Transactions on Mathematical Software, 01217 ! Volume 12, Number 4, December 1986, pages 362-376. 01218 ! 01219 ! Pierre L'Ecuyer, 01220 ! Random Number Generation, 01221 ! in Handbook of Simulation, 01222 ! edited by Jerry Banks, 01223 ! Wiley, 1998, 01224 ! ISBN: 0471134031, 01225 ! LC: T57.62.H37. 01226 ! 01227 ! Peter Lewis, Allen Goodman, James Miller, 01228 ! A Pseudo-Random Number Generator for the System/360, 01229 ! IBM Systems Journal, 01230 ! Volume 8, Number 2, 1969, pages 136-143. 01231 ! 01232 ! Parameters: 01233 ! 01234 ! Input/output, integer ( kind = 4 ) SEED, the "seed" value, which should 01235 ! NOT be 0. On output, SEED has been updated. 01236 ! 01237 ! Output, real ( kind = 8 ) R8_UNIFORM_01, a new pseudorandom variate, 01238 ! strictly between 0 and 1. 01239 ! 01240 implicit none 01241 01242 integer ( kind = 4 ), parameter :: i4_huge = 2147483647 01243 integer ( kind = 4 ) k 01244 real ( kind = 8 ) r8_uniform_01 01245 integer ( kind = 4 ) seed 01246 01247 if ( seed == 0 ) then 01248 write ( *, '(a)' ) ' ' 01249 write ( *, '(a)' ) 'R8_UNIFORM_01 - Fatal error!' 01250 write ( *, '(a)' ) ' Input value of SEED = 0.' 01251 stop 01252 end if 01253 01254 k = seed / 127773 01255 01256 seed = 16807 * ( seed - k * 127773 ) - k * 2836 01257 01258 if ( seed < 0 ) then 01259 seed = seed + i4_huge 01260 end if 01261 ! 01262 ! Although SEED can be represented exactly as a 32 bit integer, 01263 ! it generally cannot be represented exactly as a 32 bit real number! 01264 ! 01265 r8_uniform_01 = real ( seed, kind = 8 ) * 4.656612875D-10 01266 01267 return 01268 end 01269 subroutine set ( n, ix, in, rl, mrl, lm, ifault ) 01270 01271 !*****************************************************************************80 01272 ! 01273 !! SET sets up the arrays RL and LM. 01274 ! 01275 ! Modified: 01276 ! 01277 ! 30 March 1999 01278 ! 01279 ! Author: 01280 ! 01281 ! D Smith 01282 ! FORTRAN90 version by John Burkardt 01283 ! 01284 ! Reference: 01285 ! 01286 ! D Smith, 01287 ! Algorithm AS 189, 01288 ! Maximum Likelihood Estimation of the Parameters of the Beta 01289 ! Binomial Distribution, 01290 ! Applied Statistics, 01291 ! Volume 32, Number 2, 1983, pages 196-204. 01292 ! 01293 ! Parameters: 01294 ! 01295 ! Input, integer ( kind = 4 ) N, the number of observations or trials. 01296 ! 01297 ! Input, integer ( kind = 4 ) IX(N), contains the number of successes for 01298 ! each trial. 01299 ! 01300 ! Input, integer ( kind = 4 ) IN(N), contains the number tested on each trial. 01301 ! 01302 ! Output, integer ( kind = 4 ) RL(MRL,3), array of coefficients of 01303 ! (MU + R * THETA), ( 1 - MU + R * THETA) and ( 1 + R * THETA ) terms. 01304 ! 01305 ! Input, integer ( kind = 4 ) MRL, the first dimension of the RL array, 01306 ! which must be at least the maximum of the values in IN(*). 01307 ! 01308 ! Output, integer ( kind = 4 ) LM(3), contain the values Max ( IX(J) - 1 ), 01309 ! Max ( IN(J) - IX(J) - 1 ), and Max ( IN(J) - 1 ). 01310 ! 01311 ! Output, integer ( kind = 4 ) IFAULT, error flag. 01312 ! 0, no error. 01313 ! 1, N <= 1; 01314 ! 2, IX(I) = 0 for all I; 01315 ! 3, IX(I) = IN(I) for all I; 01316 ! 4, max ( IN(I) ) > MRL; 01317 ! 5, either IX(I) < 0 or IN(I) < IX(I) for some I. 01318 ! 01319 implicit none 01320 01321 integer ( kind = 4 ) mrl 01322 integer ( kind = 4 ) n 01323 01324 integer ( kind = 4 ) i 01325 integer ( kind = 4 ) ifault 01326 integer ( kind = 4 ) in(n) 01327 integer ( kind = 4 ) ix(n) 01328 integer ( kind = 4 ) j 01329 integer ( kind = 4 ) jj 01330 integer ( kind = 4 ) k 01331 integer ( kind = 4 ) lm(3) 01332 integer ( kind = 4 ) mar 01333 integer ( kind = 4 ) rl(mrl,3) 01334 ! 01335 ! Check the input data. 01336 ! 01337 if ( n <= 1 ) then 01338 ifault = 1 01339 return 01340 end if 01341 01342 ifault = 2 01343 do i = 1, n 01344 if ( 0 < ix(i) ) then 01345 ifault = 0 01346 end if 01347 end do 01348 01349 if ( ifault == 2 ) then 01350 return 01351 end if 01352 01353 ifault = 3 01354 do i = 1, n 01355 if ( ix(i) < in(i) ) then 01356 ifault = 0 01357 end if 01358 end do 01359 01360 if ( ifault == 3 ) then 01361 return 01362 end if 01363 ! 01364 ! Form the matrix of counts. 01365 ! 01366 ifault = 4 01367 01368 lm(1:3) = 0 01369 rl(1:mrl,1:3) = 0 01370 01371 do i = 1, n 01372 01373 jj = ix(i) 01374 mar = 1 01375 01376 10 continue 01377 01378 if ( jj < 0 ) then 01379 ifault = 5 01380 return 01381 end if 01382 01383 if ( 0 < jj ) then 01384 01385 if ( mrl < jj ) then 01386 return 01387 end if 01388 01389 if ( lm(mar) < jj ) then 01390 lm(mar) = jj 01391 end if 01392 01393 rl(jj,mar) = rl(jj,mar) + 1 01394 01395 end if 01396 01397 if ( mar == 1 ) then 01398 jj = in(i) - ix(i) 01399 mar = 2 01400 go to 10 01401 else if ( mar == 2 ) then 01402 jj = in(i) 01403 mar = 3 01404 go to 10 01405 end if 01406 01407 end do 01408 01409 ifault = 0 01410 ! 01411 ! Evaluate number of calls to different terms of likelihood function. 01412 ! 01413 do i = 1, 3 01414 01415 jj = lm(i) - 1 01416 k = jj 01417 do j = 1, jj 01418 rl(k,i) = rl(k,i) + rl(k+1,i) 01419 k = k - 1 01420 end do 01421 01422 end do 01423 01424 return 01425 end 01426 subroutine timestamp ( ) 01427 01428 !*****************************************************************************80 01429 ! 01430 !! TIMESTAMP prints the current YMDHMS date as a time stamp. 01431 ! 01432 ! Example: 01433 ! 01434 ! May 31 2001 9:45:54.872 AM 01435 ! 01436 ! Modified: 01437 ! 01438 ! 31 May 2001 01439 ! 01440 ! Author: 01441 ! 01442 ! John Burkardt 01443 ! 01444 ! Parameters: 01445 ! 01446 ! None 01447 ! 01448 implicit none 01449 01450 character ( len = 8 ) ampm 01451 integer ( kind = 4 ) d 01452 character ( len = 8 ) date 01453 integer ( kind = 4 ) h 01454 integer ( kind = 4 ) m 01455 integer ( kind = 4 ) mm 01456 character ( len = 9 ), parameter, dimension(12) :: month = (/ 01457 'January ', 'February ', 'March ', 'April ', 01458 'May ', 'June ', 'July ', 'August ', 01459 'September', 'October ', 'November ', 'December ' /) 01460 integer ( kind = 4 ) n 01461 integer ( kind = 4 ) s 01462 character ( len = 10 ) time 01463 integer ( kind = 4 ) values(8) 01464 integer ( kind = 4 ) y 01465 character ( len = 5 ) zone 01466 01467 call date_and_time ( date, time, zone, values ) 01468 01469 y = values(1) 01470 m = values(2) 01471 d = values(3) 01472 h = values(5) 01473 n = values(6) 01474 s = values(7) 01475 mm = values(8) 01476 01477 if ( h < 12 ) then 01478 ampm = 'AM' 01479 else if ( h == 12 ) then 01480 if ( n == 0 .and. s == 0 ) then 01481 ampm = 'Noon' 01482 else 01483 ampm = 'PM' 01484 end if 01485 else 01486 h = h - 12 01487 if ( h < 12 ) then 01488 ampm = 'PM' 01489 else if ( h == 12 ) then 01490 if ( n == 0 .and. s == 0 ) then 01491 ampm = 'Midnight' 01492 else 01493 ampm = 'AM' 01494 end if 01495 end if 01496 end if 01497 01498 write ( *, '(a,1x,i2,1x,i4,2x,i2,a1,i2.2,a1,i2.2,a1,i3.3,1x,a)' ) & 01499 trim ( month(m) ), d, y, h, ':', n, ':', s, '.', mm, trim ( ampm ) 01500 01501 return 01502 end