*
* @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
*
*   DEFINE=equation to define describing the cointegrating relation [not used]
*   ECT=VECT[EQUATION] to define describing the cointegrating relation [not used]
*     (ECT is for compatibility with other procedures).
*
*   [PRINT]/NOPRINT
*   TITLE="title for report" ["Fully Modified LS"]
*
* Revision Schedule:
*   09/2005 Written by Tom Doan, Estima
*   09/2006 Added "startl+1 endl" to the final LINREG statement,
*           so that the range used in the computations would be
*           reported correctly.
*   04/2007 Added PRINT/NOPRINT option
*   05/2008 Correct demeaning with DET=TREND. Took out 1 & t on the first
*           differences rather than just 1.
*   09/2009 Add DEFINE option
*   10/2014 Add TITLE and ECT options
*
* 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
option switch     print 1
option equation   *define
option string     title
option vect[equ]  *ect
*
local integer     startl endl ndet lhs n
local vect[int]   v rhs detlist demeanlist
local series      trend zstar ydagger
local vect[series] u
local vect        alepht prjcoe lw
local rect        cmomx 	r
local real        sigma1
local string      ltitle
*
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
dim demeanlist(0)
if det==1
   dim detlist(0)
else
if det==2
   compute detlist=||constant||,demeanlist=||constant||
else
   compute detlist=||constant,trend||,demeanlist=||constant||
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
* de-meaning 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)
   if %rows(demeanlist)>0 {
      linreg(noprint) u(i) startl+1 endl u(i)
      # demeanlist
   }
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))
*
if %defined(title)
   compute ltitle=title
else
   compute ltitle="Fully Modified LS"
*
linreg(title=ltitle,print=print,create,noscale,$
   coeffs=%xsubmat(cmomx,2,%ncmom,1,1),$
   covmat=sigma1*%xsubmat(cmomx,2,%ncmom,2,%ncmom),define=define) lhs startl+1 endl
# detlist rhs
*
if %defined(ect) {
   dim ect(1)
   equation(coeffs=%beta) ect(1) lhs
   # detlist rhs
}
end
