'''program compare_Lc    uses single equation shortcuts
''''Begins by creating workfile and generating simulated data y x1 x2
'''The Lc stat as calculated here is scalar named  a_Lc, must compare to Eveiws calculation by opening equation named  e_eviews
'''Have confirmed that the approach taken below to calculate a_Lc yields same results if I calculate Lc using (@transpose(bigS_t))*(@inverse(G))*bigS_t
'''Results for b-hats are equal to those of eviews, compared by vector named  diff
'''For Lc results differ from eviews by +-0.01 or better, scaling by 1/!N rather than 1/!T, AND using all observations in calculating M or G
'''follows eviews in placiing constant last
'''April 11, 2012
'''SEE PROGRAM LC_EV_H_CONSISTENT2F, E, D, ...  FOR COMPARISON TO EVIEWS approach to LC ETC


!N = 100   ''''this determines sample size
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      '''''should be and is near 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

matrix M = Z*(@transpose(Z))
matrix first = @inverse(M)  
!T = @columns(Z)
'''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 Zy = Z*yy_plus
vector second = Zy -lam


'''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   '''''y-hat from fmols
vector u_plus = yy_plus - yhat_plus            '''''residuals from fmols
'''''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)

matrix(!T,3) bigU   ''''will have identical !Tx1 columns of u+,  makes it easier to construct X-var columns multiplied by residuals
colplace(bigU,u_plus,1)
colplace(bigU,u_plus,2)
colplace(bigU,u_plus,3)

matrix Zu = @emult((@transpose(Z)),bigU)   ''''this is almost the score vector, but each row needs adjustment by lam12_plus:
'''next need to subtract lam_noT from each element
vector(!T) lamplus1 = lam12_plus(1)
vector(!T) lamplus2 = lam12_plus(2)
matrix(!T,3) lamplus   '''''this matrix will be !Tx1, columns of lam12_plus(1), lam12_plus(2), 0  (next two lines)
colplace(lamplus,lamplus1,1)
colplace(lamplus,lamplus2,2)   
matrix small_s_transpose = Zu -lamplus   ''''this is !T by 3
matrix small_s = @transpose(small_s_transpose)   '''this is 3 by !T, used below in big_S




'''''''''''''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


''''''''''''''calculate M = X'X, using all observations of X
smpl 1 !N
genr ones = 1
group gx x1 x2 ones
matrix bigZ = @convert(gx) ''''data in rows by observation as usual, bigZ is the usual ols X matrix
matrix bigM = (@transpose(bigZ))*bigZ



'''''Lc using Hansen's approach, I have confirmed that get same value using (@transpose(bigS_t))*(@inverse(G))*bigS_t as in Eviews documentation
matrix H = (1/omega1_2)*Big_S*(@transpose(Big_S))
matrix Lc_H_M = (@inverse(bigM))*H
scalar a_Lc = (@trace(Lc_H_M))*(1/!N)    ''''''THIS COMES CLOSE TO REPRODUCING EVIEWS VALUES uses M employing all obs of x, scale by !N, not !T
'''this equals alt_Lc_e_fix in previous program
