**********************************************************************
*
*  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
*     BJTRACE/[NOBJTRACE] = 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 bjtrace  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=bjtrace,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










