On Sat, 14 Apr 2018, Riccardo (Jack) Lucchetti wrote:
 The following implementation looks reasonably efficient to me.
[...]
And, by the way, lends itself quite nicely to also implementing VAR 
prewhitening à la Andrews:
<hansl>
function matrix andrews (matrix X, bool demeaned[0],
       int lagtrunc[0::4])
     scalar N = cols(X)
     scalar T = rows(X)
     if !demeaned
         matrix X = cdemean(X)
     endif
     matrix E
     Pi = mols(X[2:,], X[1:T-1,], &E)'
     matrix ret = longrunvar(E, 1, lagtrunc)
     ret = qform(inv(I(N) - Pi),ret)
     return ret
end function
</hansl>
-------------------------------------------------------
   Riccardo (Jack) Lucchetti
   Dipartimento di Scienze Economiche e Sociali (DiSES)
   Università Politecnica delle Marche
   (formerly known as Università di Ancona)
   r.lucchetti(a)univpm.it
   
http://www2.econ.univpm.it/servizi/hpp/lucchetti
-------------------------------------------------------