'''program Lc_ev_H_consistent  '''avoids shortcuts of previous programs
''''Get consistent results with eviews and Hansen approach for Lc, but value is scaled too large by (!N+2) to (!N+2.5)
'''follows eviews in placiing constant last
''''based on program FMOLS_e01
!N = 100
WFcreate u  !N
smpl 1 1
genr x1 = 10
genr x2 = 2
smpl 2 !N
genr x1 = x1(-1) +nrnd
genr x2  = x2(-1) +nrnd

genr cx1 = x1 -x1(-1)
genr cx2 = x2 -x2(-1)
smpl 1 1
genr rr = (9/8)*nrnd
smpl 2 !N
genr rr = 0.8*rr(-1) +0.5*nrnd
smpl 1 !N
genr y = 5 +0.3*x1 -0.3*x2 +rr  '''autocorrelated errors in cointegration process (else fmols has less to do)

smpl 1 !N   
ls y c x1 x2

genr u1 = resid
smpl 2 !N
genr u_x1 =cx1
genr u_x2 = cx2
group gu  u1  u_x1  u_x2    '''group the cointegrating model and x-var residuals

gu.lrcov(window=sym,lag=1,kern=bart,bw=fixednw,noc,out=omega)   '''estimate long-run covariance, results placed in omega
gu.lrcov(window=lower,lag=1,kern=bart,bw=fixednw,noc,out=lambda)   '''estimate one-sided (lag) covariance, results placed in lambda.


smpl 1  !N
equation e_eviews.cointreg(method=fmols, trend=const, regdiff, lag=1, kern=bart, bw=fixednw) y x1 x2

vector(3) b_eviews
 b_eviews(1) = e_eviews.@coefs(1)    ''''eviews lists constant last
 b_eviews(2) = e_eviews.@coefs(2)
 b_eviews(3) = e_eviews.@coefs(3)

matrix e_omega = e_eviews.@lrcov    '''''eviews cointreg proc est of lrcov
matrix e_lambda = e_eviews.@lambda12cov   ''''eviews cointreg proc est of one-sided
matrix p_lambda = @subextract(lambda,1,2,3,3)    '''omits first column of one-sided lrcov as generated by LRCOV command

matrix dif_omega = e_omega -omega             '''''Difference between calc by LRCOV and calc by COINTREG, should be zero
matrix dif_lambda = e_lambda -p_lambda      '''''also should be zero

scalar omega11 = @subextract(omega,1,1,1,1)    ''''scalar
rowvector omega12 = @subextract(omega,1,2,1,3)   ''' 1x2
vector omega21 = @subextract(omega,2,1,3,1)   '''   2x1
matrix omega22 = @subextract(omega,2,2,3,3)       ''''2x2

rowvector Q = omega12*(@inverse(omega22))   '''part of calculating yplus and lambda12_plus and omega1_2  In Hansen's programs this is "g"
genr y_plus = y - (Q(1)*u_x1 +Q(2)*u_x2)

rowvector lambda12 = @subextract(lambda,1,2,1,3)   '''eviews w12
matrix lambda22 = @subextract(lambda,2,2,3,3)   

rowvector lam12_plus = lambda12  -(Q*lambda22)  ''''used below in bhat calc, this is lamda12 plus in eviews exposition
scalar temp = Q*omega21
scalar omega1_2 = omega11 -temp   ''''

smpl 2 !N
genr ones = 1
group gx x1 x2 ones
matrix Z = @transpose(@convert(gx) ) ''''a column for each observation t
vector yy_plus = @convert(y_plus)     '''series put into vector
''''''''''''''''''''''' b-hat needs two summations observation by observation
!T = @columns(Z)
matrix(3,3) sumZZ  '''this will store sum Zt*Zt'
vector(3) sumZy '''this will store sum z*yyplus
For !col = 1 to !T
  vector Zt = @columnextract(Z,!col)
  matrix ZZ = Zt*(@transpose(Zt))   '''this should be 3 by 3
  sumZZ = sumZZ +ZZ   '''acumulate sum

  vector  zy = Zt*(yy_plus(!col))  '''3 by 1, is Z*y_plus at date t
  sumZy = sumZy + zy   ''''accumulte sum
Next            '''''note in single equation case then could calc sumZZ = Z'*Z
'''need to take inverse of sumZZ
 matrix first = @inverse(sumZZ)  '''this is first expression in b-hat

matrix M = sumZZ '''used below in Hansen's and Eviews Lc calc
'''need to modify sumZy by transpose of T*(lambda12_plus, 0 )  
 vector(3)  lam   '''note last element is zero
  lam(1) = !T*(lam12_plus(1))
  lam(2) = !T*(lam12_plus(2))
vector second = sumZy -lam  ''''this is second expression in b-hat
vector b_hat = first*second
vector diff = b_hat -b_eviews  '''this comparison of my vs Eviews b-hats should be near zero, and it is
 '''''''''''''''''''''''''''End b-hat calc, now try Lc''''''''''''''''''''''''''''''''''


vector yhat_plus = (@transpose(Z))*b_hat
vector u_plus = yy_plus - yhat_plus
'''''each small s(t) is 3 by 1, but here make a 3 by T matrix of all the s(t)'ies
matrix(3,!T)  small_s
vector(3) lam_noT
  lam_noT(1) = lam12_plus(1)
   lam_noT(2) = lam12_plus(2)
For !col =1 to !T
  vector Zt = @columnextract(Z,!col)    ''''column vec at t
  vector temp_s = Zt*u_plus(!col) -lam_noT   '''this is an s(t)
  small_s(1,!col) = temp_s(1)
   small_s(2,!col) = temp_s(2)
  small_s(3,!col) = temp_s(3)
Next
'''''end of making matrix of small s(t)
'''''''''''''next need to make matrix of the Big S, which are partial sums of s
Matrix(3,!T) Big_S
vector(3) temp_sum
For !col = 1 to !T
  vector s_t = @columnextract(small_s,!col)
  temp_sum = temp_sum +s_t
  Big_S(1,!col) = temp_sum(1)
  Big_S(2,!col) = temp_sum(2)
  Big_S(3,!col) = temp_sum(3)
Next   '''a check is that last column should equal zero
''''''''Now need M which is sum Zt'Zt  in contrast to sumZZ which is sum ZtZt'
'''and need G which is omega1_2 times sum ZtZt' = M

matrix G = omega1_2*M

''''''Eviews Lc
scalar Lc_e   '''will store the sum of which the trace is Lc
For !col = 1 to !T
 vector bigS_t = @columnextract(Big_S,!col)
 matrix temp_Lc = (@transpose(bigS_t))*(@inverse(G))*bigS_t
 Lc_e = Lc_e +temp_Lc(1,1)
Next
'''NOTE final Lc_e  is approach to calculating Lc as described in eviews documentation
''''end  Eviews Lc

'''Hansen Lc
scalar Lc_H  '''Hansen's exposition 
matrix(3,3) temp_HH
For !col = 1 to !T
   vector bigS_t = @columnextract(Big_S,!col)
   matrix tempH = bigS_t*(1/omega1_2)*(@transpose(bigS_t))
   temp_HH = temp_HH +tempH
Next
matrix H = (@inverse(M))*temp_HH
Lc_H = @trace(H)
'''end Hansen Lc

''' My Lc_e = Lc_H, but these are much too large.  Scaling by (1/(!T+k)) almost, but does not fix the problem.  Somewhere I am missing a scale factor and I think some constant term also.
