*
*  ARMADLMSetup sets up the A and SW matrices for a DLM based upon the ARMA
*  model in an input equation. This is the DLM from Jones, "Maximum Likelihood
*  Fitting of ARMA Models," Technometrics, 1980, pp 389-395. The state space
*  representation of x(t) uses a state vector consisting of x(t),x(t+1|t),
*  x(t+2|t),...,x(t+r-1|t), where x(s|t) is the best linear predictor of x(s)
*  given information through t. For this model, A takes the form
*
*  0  1  0 .... 0
*  0  0  1 .... 0
*   ............1
*  p(r-1) p(r-2) ... p(1)
*
*  where the p's are the AR coefficients, and SW is the DD' where D is
*  the vector of "psi weights", that is, the responses of the model to a unit
*  shock. The dimension r=max(p,q+1). The "C" matrix for this DLM is just
*  (1,0,...,0).
*
*  To use the procedure, you first need to define your ARMA equation
*  (using EQUATION or BOXJENK(DEFINE=...). Then call the procedure
*  using the syntax:
*
*  @ARMADELMSetup eq amat swmat
*
*  For the "EQ" parameter, supply the name of your equation. For ARmat and
*  SWmat, supply the names you want to give the A and SW matrices that
*  the procedure will define.
*
*  You can then use these arrays in a subsequent DLM instruction.
*
*  Revision Schedule
*
*   01/03 Written by Tom Doan, Estima
*
procedure ARMADLMSetup eq a sw
type equation eq
type rect *a
type symm *sw
*
local rect[int] eqtable
local vect eqcoeffs
local integer p q r i j
local series psi
*
compute eqtable=%eqntable(eq),eqcoeffs=%eqncoeffs(eq)
compute q=p=0
do i=1,%eqnsize(eq)
   if eqtable(1,i)==%eqndepvar(eq).and.eqtable(2,i)>p
      compute p=eqtable(2,i)
   if eqtable(1,i)==%mvgavge.and.eqtable(2,i)>q
      compute q=eqtable(2,i)
end do i
*
compute r=%imax(p,q+1)
*
impulse(noprint,shock=||1.0||) 1 r
# eq psi
*
dim a(r,r) sw(r,r)
compute sw=%outerxx(%xsubvec(psi,1,r))
ewise a(i,j)=(j==i+1)
*
do i=1,%eqnsize(eq)
   if eqtable(1,i)==%eqndepvar(eq)
      compute a(r,r+1-eqtable(2,i))=eqcoeffs(i)
end do i
end


