    PROGRAM nag_tsa_kalman_ex01

      ! Example Program Text nag_tsa_kalman
      ! NAG fl90, Release 4. NAG Copyright 2000.

      ! .. Use Statements ..
      USE nag_tsa_kalman, ONLY : nag_kalman_sqrt_cov_var, nag_kalman_predict
      USE nag_tri_lin_sys, ONLY : nag_tri_lin_sol
      USE nag_examples_io, ONLY : nag_std_in, nag_std_out
      USE nag_sym_lin_sys, ONLY : nag_key_pos, nag_sym_lin_fac
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Intrinsic Functions ..
      INTRINSIC ABS, DOT_PRODUCT, EPSILON, KIND, LOG, MAXVAL, SQRT
      ! .. Parameters ..
      INTEGER, PARAMETER :: wp = KIND(1.0D0)
      REAL (wp), PARAMETER :: zero = 0.0_wp
      ! .. Local Scalars ..
      INTEGER :: i, j, l, m, n, ncall, step
      REAL (wp) :: dev
      ! .. Local Arrays ..
      REAL (wp), ALLOCATABLE :: a(:,:), b(:,:), c(:,:), h(:,:), p(:,:), &
       q(:,:), r(:,:), resid(:), s(:,:), s_last(:,:), x(:), y(:), ymean(:)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) 'Example Program Results for nag_tsa_kalman_ex01'

      READ (nag_std_in,*)          ! Skip heading in data file
      READ (nag_std_in,*) ncall, n, m, l

      ALLOCATE (a(n,n),b(n,l),c(m,n),h(m,m),p(n,n),q(l,l),r(m,m),s(n,n),x(n), &
       y(m),resid(m),ymean(m),s_last(n,n)) ! Allocate storage

      READ (nag_std_in,*) x
      READ (nag_std_in,*) ymean
      READ (nag_std_in,*) (a(i,:),i=1,n)
      READ (nag_std_in,*) (b(i,:),i=1,n)
      READ (nag_std_in,*) (c(i,:),i=1,m)
      READ (nag_std_in,*) (r(i,:),i=1,m)
      READ (nag_std_in,*) (q(i,:),i=1,l)

      CALL nag_sym_lin_fac(nag_key_pos,'l',q)

      ! Obtain initial estimate for S via steady solution
      ! of the prediction equation.

      s = zero
      j = 0

      DO
        s_last = s
        j = j + 1

        CALL nag_kalman_predict(s,a,b,q=q)

        IF (MAXVAL(ABS(s-s_last))<0.1_wp*SQRT(EPSILON(1.0_wp))) EXIT
        IF (j==50) THEN
          WRITE (nag_std_out,*) 'nag_kalman_predict failed to obtain &
           &initial estimate for s via steady'
          WRITE (nag_std_out,*) &
           'state solution of the prediction equation in 50 iterations'

          DEALLOCATE (a,b,c,h,p,q,r,s,s_last,x,y,resid, &
           ymean)                  ! Deallocate storage

          STOP
        END IF
      END DO

      dev = zero
      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) ' step        Residuals'

      ! Loop through data

      DO step = 1, ncall
        READ (nag_std_in,*) y
        y = y - ymean

        ! Perform time and measurement update

        IF (step==ncall) THEN

          CALL nag_kalman_sqrt_cov_var(s,a,b,c,r,q=q,h=h,resid=resid,y=y,x=x, &
           p=p)

        ELSE

          CALL nag_kalman_sqrt_cov_var(s,a,b,c,r,q=q,h=h,resid=resid,y=y,x=x)

        END IF
        WRITE (nag_std_out,'(i4,4f12.4)') step, resid

        ! Update loglikelihood

        CALL nag_tri_lin_sol('l',h,resid)

        dev = dev + DOT_PRODUCT(resid,resid)
        DO i = 1, m
          dev = dev + 2.0_wp*LOG(h(i,i))
        END DO
      END DO

      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) ' Final X(i+1|i) '
      WRITE (nag_std_out,'(4f12.4)') x
      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) ' Final Value of P'

      DO i = 1, n
        WRITE (nag_std_out,'(4f12.4)') p(i,1:i)
      END DO
      WRITE (nag_std_out,*)
      WRITE (nag_std_out,'(A,e13.4)') ' Deviance = ', dev

      DEALLOCATE (a,b,c,h,p,q,r,s,s_last,x,y,resid,ymean) ! Deallocate storage

    END PROGRAM nag_tsa_kalman_ex01
