*     G13FCF Example Program Text
*     Mark 20 Release. NAG Copyright 2001.
*     .. Parameters ..
      INTEGER          NOUT
      PARAMETER        (NOUT=6)
      INTEGER          NPARMX, NUM
      DOUBLE PRECISION ZERO
      PARAMETER        (NPARMX=10,NUM=1500,ZERO=0.0D0)
      INTEGER          NUM1, NREGMX
      PARAMETER        (NUM1=3000,NREGMX=10)
*     .. Local Scalars ..
      DOUBLE PRECISION DF, FAC1, GAMMA, HP, LGF, MEAN, TOL, XTERM
      INTEGER          I, IFLAG, IGEN, IP, IQ, K, LDX, LWK, MAXIT, MN,
     +                 NPAR, NPAR2, NREG, NT
      LOGICAL          FCALL
      CHARACTER        DIST
*     .. Local Arrays ..
      DOUBLE PRECISION BX(10), COVAR(NPARMX,NPARMX), CVAR(100),
     +                 ETM(NUM1), HT(NUM1+10), HTM(NUM1), PARAM(NPARMX),
     +                 RVEC(40), RWSAV(9), SC(NPARMX), SE(NPARMX),
     +                 THETA(NPARMX), WK(NUM1*3+NPARMX+NREGMX*NUM1+20*
     +                 20+1), X(NUM1,10), YT(NUM1+10)
      INTEGER          ISEED(4)
      LOGICAL          COPTS(2)
*     .. External Subroutines ..
      EXTERNAL         G05HLF, G05KBF, G13FCF, G13FDF
*     .. Intrinsic Functions ..

      INTRINSIC        DBLE, SIN
*     .. Executable Statements ..
      WRITE (NOUT,*) 'G13FCF Example Program Results'

      ISEED(1) = 111
      IGEN = 0

      LDX = NUM1
      BX(1) = 1.5D0
      BX(2) = 2.5D0
      BX(3) = 3.0D0
      MEAN = 3.0D0

      DO 20 I = 1, NUM
         FAC1 = DBLE(I)*0.01D0
         X(I,1) = 0.01D0 + 0.7D0*SIN(FAC1)
         X(I,2) = 0.5D0 + FAC1*0.1D0
         X(I,3) = 1.0D0
   20 CONTINUE

      MN = 1
      NREG = 2
      GAMMA = -0.4D0
      IP = 1
      IQ = 1
      NPAR = IQ + IP + 1
      LWK = NREG*NUM + 3*NUM + NPAR + NREG + MN + 404

      PARAM(1) = 0.08D0
      PARAM(2) = 0.2D0
      PARAM(3) = 0.7D0

      FCALL = .TRUE.
      DIST = 'N'
      DF = 4.1D0

*      GOTO 88


      ISEED(1) = 111
      CALL G05KBF(IGEN,ISEED)
      CALL G05HLF(DIST,300,IP,IQ,PARAM,GAMMA,DF,HT,YT,FCALL,RVEC,IGEN,
     +            ISEED,RWSAV,IFLAG)

      FCALL = .FALSE.
      CALL G05HLF(DIST,NUM,IP,IQ,PARAM,GAMMA,DF,HT,YT,FCALL,RVEC,IGEN,
     +            ISEED,RWSAV,IFLAG)

      DO 60 I = 1, NUM
         XTERM = ZERO
         DO 40 K = 1, NREG
            XTERM = XTERM + X(I,K)*BX(K)
   40    CONTINUE
         IF (MN.EQ.1) THEN
            YT(I) = MEAN + XTERM + YT(I)
         ELSE
            YT(I) = XTERM + YT(I)
         END IF
   60 CONTINUE

      IFLAG = -1

      DO 80 I = 1, NPAR
         THETA(I) = PARAM(I)*0.5D0
   80 CONTINUE

      THETA(NPAR+1) = GAMMA*0.5D0

      IF (MN.EQ.1) THEN
         THETA(NPAR+1+MN) = MEAN*0.5D0
      END IF

      DO 100 I = 1, NREG
         THETA(NPAR+1+MN+I) = BX(I)*0.5D0
  100 CONTINUE

      MAXIT = 50
      TOL = 1.0D-5

      COPTS(1) = .TRUE.
      COPTS(2) = .TRUE.
      NPAR2 = 2 + IP + IQ + MN + NREG

      CALL G13FCF(DIST,YT,X,LDX,NUM,IP,IQ,NREG,MN,NPAR2,THETA,SE,SC,
     +            COVAR,NPARMX,HP,ETM,HTM,LGF,COPTS,MAXIT,TOL,WK,LWK,
     +            IFLAG)

      WRITE (NOUT,*)
      WRITE (NOUT,*) 'Normal distribution'
      WRITE (NOUT,*)
      WRITE (NOUT,*) '          Parameter       Standard        Correct'
      WRITE (NOUT,*) '          estimates       errors          values'

      DO 120 I = 1, NPAR
         WRITE (NOUT,99999) THETA(I), SE(I), PARAM(I)
  120 CONTINUE

      WRITE (NOUT,99999) THETA(NPAR+1), SE(NPAR+1), GAMMA

      IF (MN.EQ.1) THEN
         WRITE (NOUT,99999) THETA(NPAR+2), SE(NPAR+2), MEAN
      END IF

      DO 140 I = 1, NREG
         WRITE (NOUT,99999) THETA(NPAR+MN+1+I), SE(NPAR+MN+1+I), BX(I)
  140 CONTINUE

      NT = 4
      CALL G13FDF(NUM,NT,IP,IQ,THETA,GAMMA,CVAR,HTM,ETM,IFLAG)

      WRITE (NOUT,*)
      WRITE (NOUT,99998) 'Volatility forecast = ', CVAR(NT)
      WRITE (NOUT,*)

      LWK = NUM1*3 + NPARMX + NREGMX*NUM1 + 1
      LDX = NUM1



      BX(1) = 1.5D0
      BX(2) = 2.5D0
      BX(3) = 3.0D0
      MEAN = 3.0D0

      DO 160 I = 1, NUM
         FAC1 = DBLE(I)*0.01D0
         X(I,1) = 0.01D0 + 0.7D0*SIN(FAC1)
         X(I,2) = 0.5D0 + FAC1*0.1D0
         X(I,3) = 1.0D0
  160 CONTINUE

      MN = 1
      NREG = 2
      GAMMA = -0.4D0
      IP = 1
      IQ = 1
      NPAR = IQ + IP + 1
      LWK = NREG*NUM + 3*NUM + NPAR + NREG + MN + 405

      PARAM(1) = 0.1D0
      PARAM(2) = 0.1D0
      PARAM(3) = 0.8D0

      FCALL = .TRUE.
      DIST = 'T'

      ISEED(1) = 111
      CALL G05KBF(IGEN,ISEED)

      CALL G05HLF(DIST,300,IP,IQ,PARAM,GAMMA,DF,HT,YT,FCALL,RVEC,IGEN,
     +            ISEED,RWSAV,IFLAG)

      FCALL = .FALSE.
      CALL G05HLF(DIST,NUM,IP,IQ,PARAM,GAMMA,DF,HT,YT,FCALL,RVEC,IGEN,
     +            ISEED,RWSAV,IFLAG)

      FCALL = .FALSE.
      CALL G05HLF(DIST,NUM,IP,IQ,PARAM,GAMMA,DF,HT,YT,FCALL,RVEC,IGEN,
     +            ISEED,RWSAV,IFLAG)

      DO 200 I = 1, NUM
         XTERM = ZERO
         DO 180 K = 1, NREG
            XTERM = XTERM + X(I,K)*BX(K)
  180    CONTINUE
         IF (MN.EQ.1) THEN
            YT(I) = MEAN + XTERM + YT(I)
         ELSE
            YT(I) = XTERM + YT(I)
         END IF
  200 CONTINUE

      IFLAG = -1

      DO 220 I = 1, NPAR
         THETA(I) = PARAM(I)*0.5D0
  220 CONTINUE

      THETA(NPAR+1) = GAMMA*0.5D0
      THETA(NPAR+2) = DF*0.5D0

      IF (MN.EQ.1) THEN
         THETA(NPAR+2+MN) = MEAN*0.5D0
      END IF
      DO 240 I = 1, NREG
         THETA(NPAR+MN+2+I) = BX(I)*0.5D0
  240 CONTINUE

      MAXIT = 100
      TOL = 1.0D-5

      COPTS(1) = .TRUE.
      COPTS(2) = .TRUE.
      NPAR2 = 3 + IP + IQ + MN + NREG

      CALL G13FCF(DIST,YT,X,LDX,NUM,IP,IQ,NREG,MN,NPAR2,THETA,SE,SC,
     +            COVAR,NPARMX,HP,ETM,HTM,LGF,COPTS,MAXIT,TOL,WK,LWK,
     +            IFLAG)

      WRITE (NOUT,*)
      WRITE (NOUT,*) 'Student t-distribution'
      WRITE (NOUT,*)
      WRITE (NOUT,*) '          Parameter       Standard        Correct'
      WRITE (NOUT,*) '          estimates       errors          values'

      DO 260 I = 1, NPAR
         WRITE (NOUT,99999) THETA(I), SE(I), PARAM(I)
  260 CONTINUE

      WRITE (NOUT,99999) THETA(NPAR+1), SE(NPAR+1), GAMMA

      WRITE (NOUT,99999) THETA(NPAR+2), SE(NPAR+2), DF

      IF (MN.EQ.1) THEN
         WRITE (NOUT,99999) THETA(NPAR+2+MN), SE(NPAR+2+MN), MEAN
      END IF

      DO 280 I = 1, NREG
         WRITE (NOUT,99999) THETA(NPAR+2+MN+I), SE(NPAR+2+MN+I), BX(I)
  280 CONTINUE

      NT = 4
      CALL G13FDF(NUM,NT,IP,IQ,THETA,GAMMA,CVAR,HTM,ETM,IFLAG)

      WRITE (NOUT,*)
      WRITE (NOUT,99998) 'Volatility forecast = ', CVAR(NT)
      WRITE (NOUT,*)
      STOP
*

99999 FORMAT (1X,3F16.4)
99998 FORMAT (1X,A,F12.4)
      END
