    PROGRAM g07bffe

!      G07BFF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g07bff, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: beta, ll, xi
       INTEGER                         :: ifail, n, optopt
!      .. Local Arrays ..
       REAL (KIND=nag_wp)              :: asvc(4), obsvc(4)
       REAL (KIND=nag_wp), ALLOCATABLE :: y(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G07BFF Example Program Results'
       WRITE (nout,*)

!      Skip header
       READ (nin,*)

!      Read in problem size and control parameters
       READ (nin,*) n, optopt

       ALLOCATE (y(n))

!      Read in data
       READ (nin,*) y(1:n)

!      Calculate the GPD parameter estimates
       ifail = 1
       CALL g07bff(n,y,optopt,xi,beta,asvc,obsvc,ll,ifail)
       IF (ifail/=0) THEN
          IF (ifail/=6 .AND. ifail/=7 .AND. ifail/=8) THEN
             WRITE (*,99997) '** G07BFF returned with IFAIL = ', ifail
             GO TO 20
          END IF
       END IF

!      Display parameter estimates
       WRITE (nout,*) 'Parameter estimates'
       WRITE (nout,FMT=99998) 'xi            ', xi
       WRITE (nout,FMT=99998) 'beta          ', beta
       WRITE (nout,*)

!      Display parameter distribution
       IF (optopt>0) THEN
          IF (ifail==7 .OR. ifail==8) THEN
             WRITE (nout,FMT=99999) 'Invalid observed distribution'
          ELSE
             WRITE (nout,*) 'Observed distribution'
             WRITE (nout,FMT=99998) 'Var(xi)         ', obsvc(1)
             WRITE (nout,FMT=99998) 'Var(beta)       ', obsvc(4)
             WRITE (nout,FMT=99998) 'Covar(xi,beta)  ', obsvc(2)
             WRITE (nout,FMT=99998) 'Final log-likelihood:', ll
          END IF
          WRITE (nout,*)

       ELSE
          IF (ifail==6 .OR. ifail==7) THEN
             WRITE (nout,FMT=99999) 'Invalid asymptotic distribution'
          ELSE
             WRITE (nout,*) 'Asymptotic distribution'
             WRITE (nout,FMT=99998) 'Var(xi)         ', asvc(1)
             WRITE (nout,FMT=99998) 'Var(beta)       ', asvc(4)
             WRITE (nout,FMT=99998) 'Covar(xi,beta)  ', asvc(2)
          END IF
       END IF

20     CONTINUE

99999  FORMAT (1X,A)
99998  FORMAT (1X,A,1X,E14.6)
99997  FORMAT (1X,A,I0)
    END PROGRAM g07bffe
