*
* @FM( options ) start end
* # list of endogenous variables
*
* Estimates a cointegrating relation among the listed variables using
* fully modified least squares.
*
* Options:
*   DET=NONE/[CONSTANT]/TREND indicates which deterministic components are
*      to be included in the cointegrating regression
*   LAGS=number of lags in the windows used the modifications
*
* Revision Schedule:
*   09/05 Written by Tom Doan, Estima
*   09/06 Added "startl+1 endl" to the final LINREG statement,
*         so that the range used in the computations would be
*         reported correctly.
*
* References:
*   Phillips, P.C.B. and Bruce Hansen (1990), "Statistical Inference in
*     Instrumental Variables Regression with I(1) Processes", Review of
*     Economic Studies, vol 57, pp 99-125.
*
*   Hansen, Bruce (1992), "Efficient Estimation and Testing of Cointegrating
*     Vectors in the Presence of Deterministic Trends", Journal of Econometrics,
*     vol 53, pp 87-121.
*
procedure FM start end
option integer lags 1
option choice det 2 none constant trend
*
local integer startl endl ndet lhs n
local vect[int] v rhs detlist
local series trend zstar ydagger
local vect[series] u
local vect alepht prjcoe lw
local rect cmomx r
local real sigma1
*
enter(varying) v
compute n=%rows(v)
*
inquire(reglist) startl<<start endl<<end
# v
*
* Generate the list of deterministic variables
*
set trend startl endl = t
if det==1
   dim detlist(0)
else
if det==2
   compute detlist=||constant||
else
   compute detlist=||constant,trend||
compute ndet=%rows(detlist)
*
* Split the endogenous variables into the LHS variable (first one)
* and the RHS variables (all others). Generate the differences of
* the RHS variables into u. Get the residuals from projecting those
* onto the deterministic variables.
*
compute lhs=v(1)
dim rhs(n-1) u(n-1)
ewise rhs(i)=v(i+1)
do i=1,n-1
   diff rhs(i) startl+1 endl u(i)
   linreg(noprint) u(i) startl+1 endl u(i)
   # detlist
end do i
*
* Run the preliminary OLS regression
*
linreg(noprint) v(1) startl+1 endl zstar
# detlist rhs
*
* Compute long-run variance of the combination of zstar
* (OLS residuals) and u (differenced RHS variables).
*
mcov(lags=lags,lwindow=neweywest) startl+1 endl
# zstar u
compute %cmom=%cmom/%nobs
*
* Sweep out the differenced variables and pull out the
* coefficients of zstar on those. The residual variance
* is needed later to compute the final covariance matrix,
* so hang onto that as well.
*
compute cmomx =%sweeplist(%cmom,%seq(2,%ncmom))
compute prjcoe=%xsubmat(cmomx,2,%ncmom,1,1)
compute sigma1=cmomx(1,1)
*
* Create the bias-adjusted dependent variable
*
set ydagger startl endl = lhs{0}-%dot(prjcoe,%xt(u,t))
*
* Compute the asymmetrical (current and leads only) version
* of the long-run variance
*
dim lw(2*lags+1)
ewise lw(i)=%if(i>=lags+1,float(2*(lags+1)-i)/(lags+1),0.0)
mcov(lwform=lw,rmatrix=r) startl+1 endl
# zstar u
*
* Compute the cross-product correction. (We don't scale the
* MCOV matrix by %nobs since we need to multiply it up when
* we use it.)
*
compute alepht=%xsubmat(r,2,%ncmom,1,1)-$
   %xsubmat(r,2,%ncmom,2,%ncmom)*prjcoe
*
* Generate a cross product matrix with the original
* variables but with ydagger replacing the y
*
cmom startl+1 endl
# ydagger detlist rhs
*
* Adjust the cross products between the dependent variable and
* the RHS endogenous variables
*
do i=1,n-1
   compute %cmom(1,ndet+1+i)=%cmom(1,ndet+1+i)-alepht(i)
end do i
*
compute cmomx=%sweeplist(%cmom,%seq(2,%ncmom))
linreg(title="Fully Modified LS",create,noscale,$
   coeffs=%xsubmat(cmomx,2,%ncmom,1,1),$
   covmat=sigma1*%xsubmat(cmomx,2,%ncmom,2,%ncmom)) lhs startl+1 endl
# detlist rhs

end

