FRAN Subroutine

public subroutine FRAN(N, Nu1, Nu2, Istart, X)

NAME

fran(3f) - [M_datapac:RANDOM] generate F random numbers

SYNOPSIS

   SUBROUTINE FRAN(N,Nu1,Nu2,Istart,X)

    INTEGER,intent(in)        :: N
    INTEGER,intent(in)        :: Nu1
    INTEGER,intent(in)        :: Nu2
    INTEGER,intent(inout)     :: Istart
    REAL(kind=wp),intent(out) :: X(:)

DESCRIPTION

FRAN(3f) generates a random sample of size n from the F distribution
with integer degrees of freedom parameters = NU1 AND NU2.

This distribution is defined for all non-negative x.

The probability density function is given in the references below.

INPUT ARGUMENTS

N       The desired integer number of random numbers to be generated.

NU1     The integer degrees of freedom for the numerator of the F ratio.
        nu1 should be a positive integer variable.

NU2     The integer degrees of freedom NU2 should be a positive
        integer variable for the denominator of the F ratio.

ISTART  An integer flag code which (if set to 0) will start the
        generator over and hence produce the same random sample
        over and over again upon successive calls to this subroutine
        within a run; or (if set to some integer value not equal to 0,
        like, say, 1) will allow the generator to continue from where
        it stopped and hence produce different random samples upon
        successive calls to this subroutine within a run.

OUTPUT ARGUMENTS

X       A vector (of dimension at least N) into which the generated
        random sample will be placed.

EXAMPLES

Sample program:

program demo_fran
use M_datapac, only : fran
implicit none
! call fran(x,y)
end program demo_fran

Results:

AUTHOR

The original DATAPAC library was written by James Filliben of the
Statistical Engineering Division, National Institute of Standards
and Technology.

MAINTAINER

John Urban, 2022.05.31

LICENSE

CC0-1.0

REFERENCES

  • Mood and Grable, Introduction to the Theory of Statistics, 1963, pages 231-232.
  • Johnson and Kotz, Continuous Univariate Distributions–2, 1970, pages 75-93.
  • Hastings and Peacock, Statistical Distributions–A Handbook for Students and Practitioners, 1975, page 64.

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: N
integer, intent(in) :: Nu1
integer, intent(in) :: Nu2
integer, intent(inout) :: Istart
real(kind=wp), intent(out) :: X(:)

Source Code

SUBROUTINE FRAN(N,Nu1,Nu2,Istart,X)
INTEGER,intent(in)        :: N
INTEGER,intent(in)        :: Nu1
INTEGER,intent(in)        :: Nu2
INTEGER,intent(inout)     :: Istart
REAL(kind=wp),intent(out) :: X(:)
REAL(kind=wp) :: anu1 , anu2 , arg1 , arg2 , chs1 , chs2 , pi , sum , y , z
INTEGER       :: i , j
INTEGER       :: iseed
!
DIMENSION y(2) , z(2)
DATA pi/3.14159265358979_wp/
!
!     CHECK THE INPUT ARGUMENTS FOR ERRORS
!
      IF ( N<1 ) THEN
         WRITE (G_IO,99001)
         99001 FORMAT (' ***** FATAL ERROR--THE FIRST  INPUT ARGUMENT TO FRAN(3f) IS NON-POSITIVE *****')
         WRITE (G_IO,99004) N
         RETURN
      ELSEIF ( Nu1<=0 ) THEN
         WRITE (G_IO,99002)
         99002 FORMAT (' ***** FATAL ERROR--THE SECOND INPUT ARGUMENT TO FRAN(3f) IS NON-POSITIVE *****')
         WRITE (G_IO,99004) Nu1
         RETURN
      ELSEIF ( Nu2<=0 ) THEN
         WRITE (G_IO,99003)
         99003 FORMAT (' ***** FATAL ERROR--THE THIRD  INPUT ARGUMENT TO FRAN(3f) IS NON-POSITIVE *****')
         WRITE (G_IO,99004) Nu2
         RETURN
      ELSE
!
!-----START POINT-----------------------------------------------------
!
         CALL UNIRAN(1,Istart,y)
!
!     GENERATE N F RANDOM NUMBERS
!     USING THE DEFINITION THAT
!     A F VARIATE WITH NU1 AND NU2 DEGREES OF FREEDOM
!     EQUALS (CHS1/NU1)/(CHS2/NU2)
!     WHERE CHS1 IS A CHI-SQUARED VARIATE
!     WITH NU1 DEGREES OF FREEDOM,
!     AND   CHS2 IS A CHI-SQUARED VARIATE
!     WITH NU2 DEGREES OF FREEDOM.
!     FIRST GENERATE UNIFORM (0,1) RANDOM NUMBERS,
!     THEN GENERATE NORMAL RANDOM NUMBERS,
!     THEN CHI-SQUARED RANDOM NUMBERS WITH NU1 DEGREES
!     OF FREEDOM,
!     THEN CHI-SQUARED RANDOM NUMBERS WITH NU2 DEGREES
!     OF FREEDOM,
!     AND THEN FINALLY THE F RANDOM NUMBER.
!
         anu1 = Nu1
         anu2 = Nu2
         iseed=1
         DO i = 1 , N
!
            sum = 0.0_wp
            DO j = 1 , Nu1 , 2
               CALL UNIRAN(2,iseed,y)
               arg1 = -2.0_wp*LOG(y(1))
               arg2 = 2.0_wp*pi*y(2)
               z(1) = (SQRT(arg1))*(COS(arg2))
               z(2) = (SQRT(arg1))*(SIN(arg2))
               sum = sum + z(1)*z(1)
               IF ( j/=Nu1 ) sum = sum + z(2)*z(2)
            ENDDO
            chs1 = sum
!
            sum = 0.0_wp
            DO j = 1 , Nu2 , 2
               CALL UNIRAN(2,iseed,y)
               arg1 = -2.0_wp*LOG(y(1))
               arg2 = 2.0_wp*pi*y(2)
               z(1) = (SQRT(arg1))*(COS(arg2))
               z(2) = (SQRT(arg1))*(SIN(arg2))
               sum = sum + z(1)*z(1)
               IF ( j/=Nu2 ) sum = sum + z(2)*z(2)
            ENDDO
            chs2 = sum
!
            X(i) = (chs1/anu1)/(chs2/anu2)
!
         ENDDO
      ENDIF
99004 FORMAT (' ','***** THE VALUE OF THE ARGUMENT IS ',I0,' *****')
!
END SUBROUTINE FRAN