!   G02HDF Example Program Text
!   Mark 23 Release. NAG Copyright 2011.

    MODULE g02hdfe_mod

!      G02HDF Example Program Module:
!             Parameters and User-defined Routines

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       REAL (KIND=nag_wp), PARAMETER       :: dchi = 1.5_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: one = 1.0_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: two = 2.0_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: zero = 0.0_nag_wp
       INTEGER, PARAMETER                  :: iset = 1, nin = 5, nout = 6
    CONTAINS
!dec$ attributes offload : mic :: psi
       FUNCTION psi(t)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: psi
!         .. Parameters ..
          REAL (KIND=nag_wp), PARAMETER       :: c = 1.5_nag_wp
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: t
!         .. Intrinsic Functions ..
          INTRINSIC                              abs
!         .. Executable Statements ..
          IF (t<=-c) THEN
             psi = -c
          ELSE IF (abs(t)<c) THEN
             psi = t
          ELSE
             psi = c
          END IF
          RETURN
!dec$ attributes offload : mic :: psi
       END FUNCTION psi

!dec$ attributes offload : mic :: chi
       FUNCTION chi(t)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: chi
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: t
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: ps
!         .. Intrinsic Functions ..
          INTRINSIC                              abs
!         .. Executable Statements ..
          ps = dchi
          IF (abs(t)<dchi) THEN
             ps = t
          END IF
          chi = ps*ps/two
          RETURN
!dec$ attributes offload : mic :: chi
       END FUNCTION chi

!dec$ attributes offload : mic :: betcal
       SUBROUTINE betcal(n,wgt,beta)
!         Calculate BETA for Schweppe type regression

!         .. Use Statements ..
          USE nag_library, ONLY : s15abf, x01aaf, x02akf
!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (OUT)    :: beta
          INTEGER, INTENT (IN)                :: n
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: wgt(n)
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: amaxex, anormc, b, d2, dc,    &
                                                 dw, dw2, pc, w2
          INTEGER                             :: i, ifail
!         .. Intrinsic Functions ..
          INTRINSIC                              exp, log, real, sqrt
!         .. Executable Statements ..
          amaxex = -log(x02akf())
          anormc = sqrt(x01aaf(zero)*two)
          d2 = dchi*dchi
          beta = zero
          DO i = 1, n
             w2 = wgt(i)*wgt(i)
             dw = wgt(i)*dchi
             ifail = 0
             pc = s15abf(dw,ifail)
             dw2 = dw*dw
             dc = zero
             IF (dw2<amaxex) THEN
                dc = exp(-dw2/two)/anormc
             END IF
             b = (-dw*dc+pc-0.5_nag_wp)/w2 + (one-pc)*d2
             beta = b*w2/real(n,kind=nag_wp) + beta
          END DO
          RETURN
!dec$ attributes offload : mic :: betcal
       END SUBROUTINE betcal
    END MODULE g02hdfe_mod
    PROGRAM g02hdfe
!      G02HDF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : g02hdf, x04abf
       USE g02hdfe_mod, ONLY : betcal, chi, iset, nag_wp, nin, nout, psi
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: beta, eps, psip0, sigma, tol
       INTEGER                             :: i, ifail, indw, isigma, k, ldx,  &
                                              m, maxit, n, nadv, nit, nitmon
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: rs(:), theta(:), wgt(:), wk(:),  &
                                              x(:,:), y(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G02HDF Example Program Results'
       WRITE (nout,*)

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

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

       ldx = n
       ALLOCATE (x(ldx,m),y(n),wgt(n),theta(m),rs(n),wk((m+4)*n))

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

!      Set first column of X to 1 for the constant term
       x(1:n,1) = 1.0E0_nag_wp

!      Set BETA
       CALL betcal(n,wgt,beta)

!      Read in value for PSI(0)
       READ (nin,*) psip0

!      Read in control parameters
       READ (nin,*) indw, isigma, 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

!      Read in initial values
       READ (nin,*) sigma
       READ (nin,*) theta(1:m)

!      Perform bounded influence regression
       ifail = -1
       CALL g02hdf(chi,psi,psip0,beta,indw,isigma,n,m,x,ldx,y,wgt,theta,k, &
          sigma,rs,tol,eps,maxit,nitmon,nit,wk,ifail)
       IF (ifail==7) THEN
          WRITE (nout,*) 'Some of the following results may be unreliable'
       ELSE IF (ifail/=0) THEN
          GO TO 20
       END IF

!      Display results
       WRITE (nout,99999) 'G02HDF required ', nit, ' iterations to converge'
       WRITE (nout,99999) '                   K = ', k
       WRITE (nout,99998) '               Sigma = ', sigma
       WRITE (nout,*) '    THETA'
       WRITE (nout,99997) (theta(i),i=1,m)
       WRITE (nout,*)
       WRITE (nout,*) '  Weights  Residuals'
       WRITE (nout,99996) (wgt(i),rs(i),i=1,n)

20     CONTINUE

99999  FORMAT (1X,A,I0,A)
99998  FORMAT (1X,A,F9.4)
99997  FORMAT (1X,F9.4)
99996  FORMAT (1X,2F9.4)
    END PROGRAM g02hdfe
