*
*  CONDITION.SRC
*  Computes conditional forecasts. See Section 8.12 of the Manual
*
*  Syntax:
*
*  @CONDITION  model  steps  start  VCV Matrix  constraints  forecasts
*  #  series   entry    value       (one for each constraint)
*
*    model       = VAR model used for forecasts
*    steps       = number of forecast steps
*    start       = start period of forecast
*    VCV Matrix  = SYMMETRIC covariance matrix of residuals. The rows must
*                  correspond to equations in order
*    constraints = number of constraints on future values
*    forecasts   = VECT[SERIES] containing the constrained forecasts. This
*                  is returned by the procedure.
*
*  Supplementary Cards (one for each constraint)
*    series      = endogenous variable constrained
*    entry       = entry of constraint
*    value       = target value
*
PROCEDURE CONDITION  MODEL NFORE NBEG VMAT NCONSTR FCSTOUT
TYPE MODEL MODEL
TYPE INTEGER NFORE NBEG NCONSTR
TYPE VECT[SERIES] *FCSTOUT
TYPE SYMMETRIC VMAT
*
LOCAL INTEGER NVAR
LOCAL INDEX STEP VAR
LOCAL VECTOR VALUE
LOCAL INTEGER I J NSTEPS IEQN IBLOCK NFREE CONSTR CONSENTR
LOCAL RECT CAPR UOVER VOVER VDECOMP OVERR FCSTS IMPULSES
LOCAL VECT LOWR U V
LOCAL RECT[SERIES] IMPFUNC
LOCAL VECT[INTEGER] DEPVARS
*
*   Process  supplementary cards for PROCEDURE.
*
DIMENSION STEP(NCONSTR) VAR(NCONSTR) VALUE(NCONSTR)
*
COMPUTE NVAR=%MODELSIZE(MODEL)
COMPUTE NSTEPS=0
DO I=1,NCONSTR
  ENTER VAR(I) STEP(I) VALUE(I)
  COMPUTE STEP(I)=STEP(I)-NBEG+1
  IF STEP(I)>NSTEPS  ; COMPUTE NSTEPS=STEP(I)
END DO I
*
*  Replace VAR(I) with the equation which has VAR(I) as its dependent
*  variable.
*
COMPUTE DEPVARS=%MODELDEPVARS(MODEL)
DO I=1,NCONSTR
   COMPUTE ENUMBER=0
   DO J=1,NVAR
      IF VAR(I)==DEPVARS(J) {
         COMPUTE ENUMBER=J
         BREAK
      }
   END DO J
   IF ENUMBER==0 {
     DISPLAY 'Constraints Must Use One of the Endogenous Variables'
     DISPLAY 'Please Check Constraint Number' I
     RETURN
   }
   COMPUTE VAR(I)=ENUMBER
END DO I

FORECAST(MODEL=MODEL,RESULTS=FCSTOUT) * NSTEPS NBEG
IMPULSE(MODEL=MODEL,RESULTS=IMPFUNC,NOPRINT) * NSTEPS * VMAT

COMPUTE NFREE=NVAR*NSTEPS
DIMENSION CAPR(NFREE,NCONSTR) VDECOMP(NVAR,NVAR)
DIMENSION U(NFREE) V(NFREE) LOWR(NCONSTR)
DO CONSTR=1,NCONSTR
  COMPUTE LOWR(CONSTR)=VALUE(CONSTR)-FCSTOUT(VAR(CONSTR))(STEP(CONSTR)+NBEG-1)
  OVERLAY CAPR(1,CONSTR) WITH OVERR(NSTEPS,NVAR)
  COMPUTE CONSENTR=1+STEP(CONSTR)
  EWISE OVERR(I,J)=%IF(I<CONSENTR,IMPFUNC(VAR(CONSTR),J)(CONSENTR-I),0.0)
END DO CONSTR
*
*   Compute optimal U
*
COMPUTE U=CAPR*INV( TR(CAPR)*CAPR )*LOWR
OVERLAY U(1) WITH UOVER(NSTEPS,NVAR)
OVERLAY V(1) WITH VOVER(NSTEPS,NVAR)
*
*   Transform to non-orthogonal V
*
COMPUTE VDECOMP=%DECOMP(VMAT)
COMPUTE   VOVER=UOVER*TR(VDECOMP)
*
*   Forecast with V shocks
*
FORECAST(MODEL=MODEL,MATRIX=VOVER,RESULTS=FCSTOUT) * NFORE NBEG
END CONDITION



