Program g13dbfe

!     G13DBF Example Program Text

!     Mark 26.1 Release. NAG Copyright 2016.

!     .. 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 100
        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

100   Continue

99999 Format (1X,A,I10)
99998 Format (1X,5F12.5)
99997 Format (1X,A,I5)
    End Program g13dbfe