**********************************************************************
*
*  BJFORE.SRC
*     Computes forecasts using Box-Jenkins techniques
*     Written by Tom Doan
*
*     Updated for Version 5:
*       -- Now allows you to specify estimation range as well as
*          forecast range (estmation still defaults to end 1 period
*          prior to forecast start)
*       -- This means that you can now forecast over some/all of the
*          estimation range, by specifying FORESTART earlier than REGEND.
*
*  @BJFORE(options) series forestart foreend  forecasts  resids
*     series             : Series to be forecast
*     forestart foreend  : Range of entries to forecast
*     forecasts          : Series for computed forecasts
*     resids             : (Optional) Saves residuals in a series.
*
*  Options:
*     DIFFS    =Number of regular differences [0]
*     SDIFFS   =Number of seasonal differences [0]
*     ARS      =Number of autoregressives[0]
*     MAS      =Number of moving averages[0]
*     SAR      =Number of seasonal autoregressives[0]
*     SMA      =Number of seasonal moving averages[0]
*     SPAN     =Seasonal span. Defaults to CALENDAR seasonal
*     [CONSTANT]/NOCONSTANT = Includes a constant term in the model.
*     TRANS=[NONE]/LOG/ROOT = Allows you to apply a log or square root
*       transformation to the series. Default is no transformation.
*     [PRINT]/NOPRINT = Use NOPRINT to turn off display of BOXJENK estimation output
*     TRACE/[NOTRACE] = Turns on the TRACE option on the BOXJENK
*       instruction, which displays a trace of the estimation process.
*     RSTART=start of estimation range [earliest possible}
*     REND=end of estimation range [forestart-1]
*       These set  the range of entries used to estimate the mode. RSTART defaults
*       to THE earliest possible entry. REND defaults to (forestart-1).
*
PROCEDURE  BJFORE  SERIES  FORESTART  FOREEND  FORECAST RESIDSAVE
TYPE SERIES SERIES *FORECAST *RESIDSAVE
TYPE INTEGER FORESTART FOREEND

OPTIONS INT    DIFFS  0
OPTIONS INT    SDIFFS 0
OPTIONS INT    SMA    0
OPTIONS INT    SAR    0
OPTIONS INT    RSTART 0
OPTIONS INT    REND   0
OPTIONS SWITCH CONST  1
OPTIONS SWITCH TRACE  0
OPTIONS SWITCH PRINT  1
OPTIONS INT    ARS    0
OPTION  INT    MAS    0
OPTION  CHOICE TRANS  1  NONE LOG ROOT
OPTION  INT    SPAN

LOCAL   INTEGER  STARTL ENDL SPANL STEPS REGSTART REGEND
LOCAL   INTEGER  EARLIEST ESTSTART ESTEND STARTDEF ENDDEF
LOCAL   SERIES   WORKX RESIDS
LOCAL   EQUATION BJEQ

IF .NOT.%DEFINED(SERIES).OR..NOT.%DEFINED(FORECAST).OR.$
   .NOT.%DEFINED(FORESTART).OR..NOT.%DEFINED(FOREEND) {
   DISPLAY 'Syntax: @BJFORE series forestart foreend forecast resids'
   RETURN
   }

* Get defined range of dependent variable:
INQUIRE(SERIES=SERIES) STARTDEF ENDDEF

INQUIRE(SEASONAL) SPANL>>SPAN

COMPUTE EARLIEST = STARTDEF+DIFFS+SPANL*SDIFFS+ARS+SPANL*SAR

COMPUTE REGSTART = RSTART, REGEND = REND

IF REGSTART>0
  COMPUTE ESTSTART = REGSTART
ELSE
  COMPUTE ESTSTART = EARLIEST

IF REGEND>0
  COMPUTE ESTEND = REGEND
ELSE
  COMPUTE ESTEND = FORESTART-1

IF ESTSTART<EARLIEST
{
  DISPLAY 'Requested regression start date is too early given available'
  DISPLAY 'data and selected lag structure. Resetting start date to '
  DISPLAY 'earliest possible date: ' %datelabel(ESTSTART)
}

DISPLAY
DISPLAY 'Estimation Range: ' %datelabel(eststart) 'to' %datelabel(estend)
DISPLAY 'Forecast Range:   ' %datelabel(forestart) ' to' %datelabel(foreend)
DISPLAY

IF TRANS == 1
   SET WORKX STARTDEF ENDDEF = SERIES
ELSE IF TRANS == 2
 {
   DISPLAY 'Model uses log transformation'
   SET WORKX STARTDEF ENDDEF = LOG(SERIES)
 }
ELSE
 {
   DISPLAY 'Model uses log transformation'
   SET WORKX STARTDEF ENDDEF = SQRT(SERIES)
 }

LABELS WORKX RESIDS
# %LABEL(SERIES) 'RESIDS'

BOXJENK(DIFFS=DIFFS,SDIFFS=SDIFFS,AR=ARS,MA=MAS,SMA=SMA,SAR=SAR,$
  CONST=CONST,TRACE=TRACE,DEFINE=BJEQ,PRINT=PRINT) WORKX ESTSTART ESTEND RESIDS

COMPUTE STEPS=FOREEND-FORESTART+1
FORECAST 1 STEPS FORESTART
# BJEQ FORECAST

IF TRANS == 2
   SET FORECAST FORESTART FOREEND = EXP(FORECAST)
ELSE IF TRANS == 3
   SET FORECAST FORESTART FOREEND = FORECAST**2

IF %DEFINED(RESIDSAVE)
   SET RESIDSAVE ESTSTART ESTEND = RESIDS

END BJFORE






