    PROGRAM g02hkfe

!      G02HKF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g02hkf, nag_wp, x04abf, x04ccf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: iset = 1, nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: eps, tol
       INTEGER                         :: i, ifail, j, ldx, m, maxit, n, nadv, &
                                          nit, nitmon
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: cov(:), theta(:), wk(:), x(:,:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G02HKF Example Program Results'
       WRITE (nout,*)

!      Skip heading in data file
       READ (nin,*)

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

       ldx = n
       ALLOCATE (x(ldx,m),cov(m*(m+1)/2),theta(m),wk(n+m*(m+5)/2))

!      Read in data
       READ (nin,*) (x(i,1:m),i=1,n)

!      Read in the control parameters
       READ (nin,*) nitmon, maxit, tol, eps

!      Set the advisory channel to NOUT for monitoring information
       IF (nitmon/=0) THEN
          nadv = nout
          CALL x04abf(iset,nadv)
       END IF

!      Compute robust estimate of variance / covariance matrix
       ifail = 0
       CALL g02hkf(n,m,x,ldx,eps,cov,theta,maxit,nitmon,tol,nit,wk,ifail)

!      Display results
       WRITE (nout,99999) 'G02HKF required ', nit, ' iterations to converge'
       WRITE (nout,*)
       FLUSH (nout)
       ifail = 0
       CALL x04ccf('Upper','Non-Unit',m,cov,'Covariance matrix',ifail)
       WRITE (nout,*)
       WRITE (nout,*) 'THETA'
       WRITE (nout,99998) (theta(j),j=1,m)

99999  FORMAT (1X,A,I0,A)
99998  FORMAT (1X,F10.3)
    END PROGRAM g02hkfe
