    PROGRAM g13dbfe

!      G13DBF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g13dbf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: v0
       INTEGER                         :: i, ifail, iwa, k, ldc0, nk, nl, ns,  &
                                          nvp
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: c(:,:,:), c0(:,:), d(:,:,:),         &
                                          db(:,:), p(:), v(:), w(:,:,:),       &
                                          wa(:), wb(:,:,:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G13DBF Example Program Results'
       WRITE (nout,*)

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

!      Read series length, and numbers of lags
       READ (nin,*) ns, nl, nk

       ldc0 = ns
       iwa = (2*ns+1)*ns
       ALLOCATE (c0(ldc0,ns),c(ldc0,ldc0,nl),p(nk),v(nk),d(ldc0,ldc0,nk), &
          w(ldc0,ldc0,nk),wb(ldc0,ldc0,nk),wa(iwa),db(ldc0,ns))

!      Read autocovariances
       READ (nin,*) (c0(i,1:ns),i=1,ns)
       READ (nin,*) ((c(i,1:ns,k),i=1,ns),k=1,nl)

!      Calculate multivariate partial autocorrelation function
       ifail = -1
       CALL g13dbf(c0,c,ldc0,ns,nl,nk,p,v0,v,d,db,w,wb,nvp,wa,iwa,ifail)
       IF (ifail/=0) THEN
          IF (ifail/=3) THEN
             GO TO 20
          END IF
       END IF

!      Display results
       WRITE (nout,99999) 'Number of valid parameters =', nvp
       WRITE (nout,*)
       WRITE (nout,*) 'Multivariate partial autocorrelations'
       WRITE (nout,99998) p(1:nk)
       WRITE (nout,*)
       WRITE (nout,*) 'Zero lag predictor error variance determinant'
       WRITE (nout,*) 'followed by error variance ratios'
       WRITE (nout,99998) v0, v(1:nk)
       WRITE (nout,*)
       WRITE (nout,*) 'Prediction error variances'
       DO k = 1, nk
          WRITE (nout,*)
          WRITE (nout,99997) 'Lag =', k
          DO i = 1, ns
             WRITE (nout,99998) d(i,1:ns,k)
          END DO
       END DO
       WRITE (nout,*)
       WRITE (nout,*) 'Last backward prediction error variances'
       WRITE (nout,*)
       WRITE (nout,99997) 'Lag =', nvp
       DO i = 1, ns
          WRITE (nout,99998) db(i,1:ns)
       END DO
       WRITE (nout,*)
       WRITE (nout,*) 'Prediction coefficients'
       DO k = 1, nk
          WRITE (nout,*)
          WRITE (nout,99997) 'Lag =', k
          DO i = 1, ns
             WRITE (nout,99998) w(i,1:ns,k)
          END DO
       END DO
       WRITE (nout,*)
       WRITE (nout,*) 'Backward prediction coefficients'
       DO k = 1, nk
          WRITE (nout,*)
          WRITE (nout,99997) 'Lag =', k
          DO i = 1, ns
             WRITE (nout,99998) wb(i,1:ns,k)
          END DO
       END DO

20     CONTINUE

99999  FORMAT (1X,A,I10)
99998  FORMAT (1X,5F12.5)
99997  FORMAT (1X,A,I5)
    END PROGRAM g13dbfe
