98 real(kind=kind(1.0D+0)),
PRIVATE :: zero = 0.0, half = 0.5, one = 1.0, two = 2.0, &
99 vsmall = tiny(1.0), vlarge = huge(1.0)
101 INTEGER,
PARAMETER :: dp = selected_real_kind(12, 60)
121 real(kind=kind(1.0D+0)) :: fn_val
124 real(kind=kind(1.0D+0)) :: s = 0.449871, t = -0.386595, a = 0.19600, b = 0.25472, &
125 r1 = 0.27597, r2 = 0.27846, u, v, x, y, q
130 CALL random_number(u)
131 CALL random_number(v)
132 v = 1.7156 * (v - half)
137 q = x**2 + y*(a*y - b*x)
144 IF (v**2 < -4.0*log(u)*u**2)
EXIT
167 real(kind=kind(1.0D+0)),
INTENT(IN) :: s
168 LOGICAL,
INTENT(IN) :: first
169 real(kind=kind(1.0D+0)) :: fn_val
172 WRITE(*, *)
'SHAPE PARAMETER VALUE MUST BE POSITIVE'
178 ELSE IF (s < one)
THEN
197 real(kind=kind(1.0D+0)),
INTENT(IN) :: s
198 LOGICAL,
INTENT(IN) :: first
199 real(kind=kind(1.0D+0)) :: fn_val
202 real(kind=kind(1.0D+0)),
SAVE :: c, d
203 real(kind=kind(1.0D+0)) :: u, v, x
223 CALL random_number(u)
224 IF (u < one - 0.0331*x**4)
THEN
227 ELSE IF (log(u) < half*x**2 + d*(one - v + log(v)))
THEN
252 real(kind=kind(1.0D+0)),
INTENT(IN) :: s
253 LOGICAL,
INTENT(IN) :: first
254 real(kind=kind(1.0D+0)) :: fn_val
257 real(kind=kind(1.0D+0)) :: r, x, w
258 real(kind=kind(1.0D+0)),
SAVE :: a, p, c, uf, vr, d
260 IF (s <= zero .OR. s >= one)
THEN
261 WRITE(*, *)
'SHAPE PARAMETER VALUE OUTSIDE PERMITTED RANGE'
267 p = a/(a + s*exp(-a))
269 WRITE(*, *)
'SHAPE PARAMETER VALUE TOO SMALL'
279 CALL random_number(r)
283 x = a - log((one - r)/(one - p))
285 ELSE IF (r > uf)
THEN
293 CALL random_number(r)
294 IF (one-r <= w .AND. r > zero)
THEN
295 IF (r*(w + one) >= one) cycle
296 IF (-log(r) <= w) cycle
313 INTEGER,
INTENT(IN) :: ndf
314 LOGICAL,
INTENT(IN) :: first
315 real(kind=kind(1.0D+0)) :: fn_val
334 real(kind=kind(1.0D+0)) :: fn_val
337 real(kind=kind(1.0D+0)) :: r
340 CALL random_number(r)
359 real(kind=kind(1.0D+0)),
INTENT(IN) :: a
360 real(kind=kind(1.0D+0)) :: fn_val
385 real(kind=kind(1.0D+0)),
INTENT(IN) :: aa, bb
386 LOGICAL,
INTENT(IN) :: first
387 real(kind=kind(1.0D+0)) :: fn_val
390 real(kind=kind(1.0D+0)),
PARAMETER :: aln4 = 1.3862944
391 real(kind=kind(1.0D+0)) :: a, b, g, r, s, x, y, z
392 real(kind=kind(1.0D+0)),
SAVE :: d, f, h, t, c
393 LOGICAL,
SAVE :: swap
395 IF (aa <= zero .OR. bb <= zero)
THEN
396 WRITE(*, *)
'IMPERMISSIBLE SHAPE PARAMETER VALUE(S)'
412 h = sqrt((two*a*b - f)/(f - two))
416 t = one/(one + (a/(vlarge*b))**b)
422 CALL random_number(r)
423 CALL random_number(x)
425 IF (r < vsmall .OR. s <= zero) cycle
427 x = log(r/(one - r))/h
429 z = c*x + f*log((one + d)/(one + y)) - aln4
430 IF (s - one > z)
THEN
431 IF (s - s*z > one) cycle
432 IF (log(s) > z) cycle
436 IF (4.0*s > (one + one/d)**f) cycle
442 IF (swap) fn_val = one - fn_val
460 INTEGER,
INTENT(IN) :: m
461 real(kind=kind(1.0D+0)) :: fn_val
464 real(kind=kind(1.0D+0)),
SAVE :: s, c, a, f, g
465 real(kind=kind(1.0D+0)) :: r, x, v
467 real(kind=kind(1.0D+0)),
PARAMETER :: three = 3.0, four = 4.0, quart = 0.25, &
468 five = 5.0, sixteen = 16.0
472 WRITE(*, *)
'IMPERMISSIBLE DEGREES OF FREEDOM'
479 a = four/(one + one/s)**c
483 g = ((s + one)/g)**c*sqrt((s+s)/g)
491 CALL random_number(r)
493 CALL random_number(v)
494 x = (two*v - one)*g/r
496 IF (v > five - a*r)
THEN
497 IF (m >= 1 .AND. r*(v + three) > f) cycle
498 IF (r > (one + v/s)**c) cycle
542 INTEGER,
INTENT(IN) :: n
543 real(kind=kind(1.0D+0)),
INTENT(IN) :: h(:), d(:)
544 real(kind=kind(1.0D+0)),
INTENT(IN OUT) :: f(:)
545 real(kind=kind(1.0D+0)),
INTENT(OUT) :: x(:)
546 LOGICAL,
INTENT(IN) :: first
547 INTEGER,
INTENT(OUT) :: ier
551 real(kind=kind(1.0D+0)) :: y, v
555 WRITE(*, *)
'SIZE OF VECTOR IS NON POSITIVE'
562 IF (d(1) < zero)
THEN
570 f(j) = d(1+j*(j-1)/2) * y
576 v = v - f((m-1)*(n2-m)/2+i)**2
586 f((i-1)*(n2-i)/2+i) = v
590 v = v - f((m-1)*(n2-m)/2+i)*f((m-1)*(n2-m)/2 + j)
592 f((i-1)*(n2-i)/2 + j) = v*y
601 x(i) = x(i) + f((j-1)*(n2-j)/2 + i) * y
624 real(kind=kind(1.0D+0)),
INTENT(IN) :: h, b
625 LOGICAL,
INTENT(IN) :: first
626 real(kind=kind(1.0D+0)) :: fn_val
629 real(kind=kind(1.0D+0)) :: ym, xm, r, w, r1, r2, x
630 real(kind=kind(1.0D+0)),
SAVE :: a, c, d, e
631 real(kind=kind(1.0D+0)),
PARAMETER :: quart = 0.25
633 IF (h < zero .OR. b <= zero)
THEN
634 WRITE(*, *)
'IMPERMISSIBLE DISTRIBUTION PARAMETER VALUES'
639 IF (h > quart*b*sqrt(vlarge))
THEN
640 WRITE(*, *)
'THE RATIO H:B IS TOO SMALL'
645 ym = (-d + sqrt(d*d + e))/b
646 IF (ym < vsmall)
THEN
647 WRITE(*, *)
'THE VALUE OF B IS TOO SMALL'
652 xm = (d + sqrt(d*d + e))/b
657 a = w**(-half*h) * sqrt(xm/ym) * exp(-e*(r - ym - one/ym))
659 WRITE(*, *)
'THE VALUE OF H IS TOO LARGE'
666 CALL random_number(r1)
667 IF (r1 <= zero) cycle
668 CALL random_number(r2)
671 IF (log(r1) < d*log(x) + e*(x + one/x) + c)
EXIT
728 real(kind=kind(1.0D+0)),
INTENT(IN) :: mu
729 LOGICAL,
INTENT(IN) :: first
733 real(kind=kind(1.0D+0)) :: b1, b2, c, c0, c1, c2, c3, del, difmuk, e, fk, fx, fy, g, &
734 omega, px, py, t, u, v, x, xx
735 real(kind=kind(1.0D+0)),
SAVE :: s, d, p, q, p0
736 INTEGER :: j, k, kflag
737 LOGICAL,
SAVE :: full_init
738 INTEGER,
SAVE :: l, m
741 real(kind=kind(1.0D+0)),
SAVE :: pp(35)
744 real(kind=kind(1.0D+0)),
PARAMETER :: a0 = -.5, a1 = .3333333, a2 = -.2500068, a3 = .2000118, &
745 a4 = -.1661269, a5 = .1421878, a6 = -.1384794, &
748 real(kind=kind(1.0D+0)),
PARAMETER :: fact(10) = (/ 1., 1., 2., 6., 24., 120., 720., 5040., &
783 CALL random_number(u)
784 IF (d*u >= difmuk*difmuk*difmuk)
RETURN
794 IF (.NOT. full_init)
THEN
800 c1 = b1 - 6.*b2 + 45.*c3
801 c0 = 1. - b1 + 3.*b2 - 15.*c3
806 IF (g < 0.0) go to 50
815 40
IF (fy-u*fy <= py*exp(px-fx))
RETURN
822 CALL random_number(u)
825 IF (t <= (-.6744)) go to 50
837 60
IF (c*abs(u) > py*exp(px+e) - fy*exp(fx+e)) go to 50
843 70
IF (ival>=10) go to 80
845 py = mu**ival/fact(ival+1)
852 80 del = .8333333e-1/fk
853 del = del - 4.8*del*del*del
855 IF (abs(v)>0.25)
THEN
856 px = fk*log(one + v) - difmuk - del
858 px = fk*v*v* (((((((a7*v+a6)*v+a5)*v+a4)*v+a3)*v+a2)*v+a1)*v+a0) - del
860 py = .3989423/sqrt(fk)
861 110 x = (half - difmuk)/s
864 fy = omega* (((c3*xx + c2)*xx + c1)*xx + c0)
865 IF (kflag <= 0) go to 40
884 CALL random_number(u)
892 IF (l == 0) go to 150
894 IF (u > 0.458) j = min(l, m)
896 IF (u <= pp(k)) go to 180
908 IF (u <= q) go to 170
939 INTEGER,
INTENT(IN) :: n
940 real(kind=kind(1.0D+0)),
INTENT(IN) :: p
941 LOGICAL,
INTENT(IN) :: first
948 real(kind=kind(1.0D+0)) :: u, pd, pu
949 real(kind=kind(1.0D+0)),
SAVE :: odds_ratio, p_r
950 real(kind=kind(1.0D+0)),
PARAMETER :: zero = 0.0, one = 1.0
955 odds_ratio = p / (one - p)
958 CALL random_number(u)
972 pd = pd * (rd+1) / (odds_ratio * (n-rd))
982 pu = pu * (n-ru+1) * odds_ratio / ru
1003 INTEGER,
INTENT(IN) :: n, r
1004 real(kind=kind(1.0D+0)),
INTENT(IN) :: p
1005 real(kind=kind(1.0D+0)) :: fn_val
1008 real(kind=kind(1.0D+0)) :: one = 1.0
1011 + r*log(p) + (n-r)*log(one - p) )
1027 REAL (dp),
INTENT(IN) :: x
1032 REAL (dp) :: a1 = -4.166666666554424D-02, a2 = 2.430554511376954D-03, &
1033 a3 = -7.685928044064347D-04, a4 = 5.660478426014386D-04, &
1034 temp, arg, product, lnrt2pi = 9.189385332046727D-1, &
1035 pi = 3.141592653589793D0
1040 IF (x > 0.d0) go to 10
1041 IF (x /= int(x)) go to 10
1048 10 reflect = (x < 0.d0)
1058 20
IF (arg <= 10.d0)
THEN
1059 product = product * arg
1071 fn_val = lnrt2pi + arg * (log(arg) - 1.d0 + &
1072 (((a4*temp + a3)*temp + a2)*temp + a1)*temp) - log(product)
1075 fn_val = log(pi/temp) - fn_val
1144 real(kind=kind(1.0D+0)),
INTENT(IN) :: pp
1145 INTEGER,
INTENT(IN) :: n
1146 LOGICAL,
INTENT(IN) :: first
1150 real(kind=kind(1.0D+0)) :: alv, amaxp, f, f1, f2, u, v, w, w2, x, x1, x2, ynorm, z, z2
1151 real(kind=kind(1.0D+0)),
PARAMETER :: zero = 0.0, half = 0.5, one = 1.0
1152 INTEGER :: i, ix, ix1, k, mp
1154 real(kind=kind(1.0D+0)),
SAVE :: p, q, xnp, ffm, fm, xnpq, p1, xm, xl, xr, c, al, xll, &
1155 xlr, p2, p3, p4, qn, r, g
1174 p1 = int(2.195*sqrt(xnpq) - 4.6*q) + half
1178 c = 0.134 + 20.5 / (15.3 + fm)
1179 al = (ffm-xl) / (ffm - xl*p)
1180 xll = al * (one + half*al)
1181 al = (xr - ffm) / (xr*q)
1182 xlr = al * (one + half*al)
1183 p2 = p1 * (one + c + c)
1190 20 CALL random_number(u)
1192 CALL random_number(v)
1197 ix = xm - p1 * v + u
1205 v = v * c + one - abs(xm-x) / p1
1206 IF (v > one .OR. v <= zero) go to 20
1213 ix = xl + log(v) / xll
1214 IF (ix < 0) go to 20
1215 v = v * (u-p2) * xll
1220 ix = xr - log(v) / xlr
1221 IF (ix > n) go to 20
1222 v = v * (u-p3) * xlr
1229 IF (k <= 20 .OR. k >= xnpq/2-1)
THEN
1242 ELSE IF (m > ix)
THEN
1258 amaxp = (k/xnpq) * ((k*(k/3. + .625) + .1666666666666)/xnpq + half)
1259 ynorm = -k * k / (2.*xnpq)
1261 IF (alv<ynorm - amaxp) go to 110
1262 IF (alv>ynorm + amaxp) go to 20
1275 IF (alv - (xm*log(f1/x1) + (n-m+half)*log(z/w) + (ix-m)*log(w*p/(x1*q)) + &
1276 (13860.-(462.-(132.-(99.-140./f2)/f2)/f2)/f2)/f1/166320. + &
1277 (13860.-(462.-(132.-(99.-140./z2)/z2)/z2)/z2)/z/166320. + &
1278 (13860.-(462.-(132.-(99.-140./x2)/x2)/x2)/x2)/x1/166320. + &
1279 (13860.-(462.-(132.-(99.-140./w2)/w2)/w2)/w2)/w/166320.) > zero)
THEN
1295 CALL random_number(u)
1296 100
IF (u >= f)
THEN
1297 IF (ix > 110) go to 90
1305 110
IF (pp > half) ix = n - ix
1333 real(kind=kind(1.0D+0)),
INTENT(IN) :: sk, p
1339 real(kind=kind(1.0D+0)),
PARAMETER :: h = 0.7
1340 real(kind=kind(1.0D+0)) :: q, x, st, uln, v, r, s, y, g
1343 IF (sk <= zero .OR. p <= zero .OR. p >= one)
THEN
1344 WRITE(*, *)
'IMPERMISSIBLE DISTRIBUTION PARAMETER VALUES'
1356 CALL random_number(r)
1367 IF (st > -uln/log(q))
THEN
1368 WRITE(*, *)
' P IS TOO LARGE FOR THIS VALUE OF SK'
1374 CALL random_number(r)
1405 real(kind=kind(1.0D+0)),
INTENT(IN) :: k
1406 LOGICAL,
INTENT(IN) :: first
1407 real(kind=kind(1.0D+0)) :: fn_val
1413 real(kind=kind(1.0D+0)),
PARAMETER :: pi = 3.14159265
1414 real(kind=kind(1.0D+0)),
SAVE :: p(20), theta(0:20)
1415 real(kind=kind(1.0D+0)) :: sump, r, th, lambda, rlast
1420 WRITE(*, *)
'** Error: argument k for random_von_Mises = ', k
1426 WRITE(*, *)
'** Error: argument k for random_von_Mises = ', k
1439 theta(j) = acos(one - j/k)
1446 CALL integral(theta(j-1), theta(j), p(j), dk)
1449 p(1:nk) = p(1:nk) / sump
1456 CALL random_number(r)
1464 th = theta(j-1) + r*(theta(j) - theta(j-1))
1465 lambda = k - j + one - k*cos(th)
1470 CALL random_number(r)
1476 IF (n .NE. 2*(n/2))
EXIT
1477 CALL random_number(r)
1480 fn_val = sign(th, (r - rlast)/(one - rlast) - half)
1486 SUBROUTINE integral(a, b, result, dk)
1490 REAL (dp),
INTENT(IN) :: dk
1491 real(kind=kind(1.0D+0)),
INTENT(IN) :: a, b
1492 real(kind=kind(1.0D+0)),
INTENT(OUT) :: result
1496 REAL (dp) :: xmid, range, x1, x2, &
1497 x(3) = (/0.238619186083197_dp, 0.661209386466265_dp, 0.932469514203152_dp/), &
1498 w(3) = (/0.467913934572691_dp, 0.360761573048139_dp, 0.171324492379170_dp/)
1501 xmid = (a + b)/2._dp
1502 range = (b - a)/2._dp
1506 x1 = xmid + x(i)*range
1507 x2 = xmid - x(i)*range
1508 result = result + w(i)*(exp(dk*cos(x1)) + exp(dk*cos(x2)))
1511 result = result * range
1513 END SUBROUTINE integral
1521 real(kind=kind(1.0D+0)) :: fn_val
1524 real(kind=kind(1.0D+0)) :: v(2)
1527 CALL random_number(v)
1529 IF (abs(v(2)) < vsmall) cycle
1530 IF (v(1)**2 + v(2)**2 < one)
EXIT
1532 fn_val = v(1) / v(2)
1543 INTEGER,
INTENT(IN) :: n
1544 INTEGER,
INTENT(OUT) :: order(n)
1549 real(kind=kind(1.0D+0)) :: wk
1559 CALL random_number(wk)
1575 INTEGER,
INTENT(IN) :: iounit
1580 INTEGER,
ALLOCATABLE :: seed(:)
1582 CALL random_seed(size=k)
1585 WRITE(*,
'(a, i2, a)')
' Enter ', k,
' integers for random no. seeds: '
1587 WRITE(iounit,
'(a, (7i10))')
' Random no. seeds: ', seed
1588 CALL random_seed(put=seed)
real(kind=kind(1.0d+0)) function random_gamma2(s, first)
real(kind=kind(1.0d+0)) function random_von_mises(k, first)
real(kind=kind(1.0d+0)) function random_weibull(a)
subroutine random_mvnorm(n, h, d, f, first, x, ier)
integer function random_neg_binomial(sk, p)
real(kind=kind(1.0d+0)) function random_chisq(ndf, first)
real(kind=kind(1.0d+0)) function random_exponential()
real(dp) function lngamma(x)
real(kind=kind(1.0d+0)) function bin_prob(n, p, r)
subroutine random_order(order, n)
real(kind=kind(1.0d+0)) function random_t(m)
integer function random_poisson(mu, first)
subroutine seed_random_number(iounit)
integer function random_binomial2(n, pp, first)
real(kind=kind(1.0d+0)) function random_gamma(s, first)
real(kind=kind(1.0d+0)) function random_inv_gauss(h, b, first)
real(kind=kind(1.0d+0)) function random_gamma1(s, first)
real(kind=kind(1.0d+0)) function random_cauchy()
real(kind=kind(1.0d+0)) function random_normal()
function to get random normal with zero mean and stdev 1
A module for random number generation from the following distributions:
real(kind=kind(1.0d+0)) function random_beta(aa, bb, first)
integer function random_binomial1(n, p, first)