function results = ecm_manu1(y,nlag,r)
% PURPOSE: performs error correction model estimation
%MANU ADDED SOME LINES AT TWO LOCATIONS, SO AS TO NORMALIZE AND STORE THE
%COINTEGRATION VECTOR

%---------------------------------------------------
% USAGE: result = ecm(y,nlag,r) 
% where:    y    = an (nobs x neqs) matrix of y-vectors in levels
%           nlag = the lag length
%           r    = # of cointegrating relations to use
%                  (optional: this will be determined using
%                  Johansen's trace test at 95%-level if left blank)                                    
% NOTES: constant vector automatically included
%         x-matrix of exogenous variables not allowed
%         error correction variables are automatically
%         constructed using output from Johansen's ML-estimator 
%---------------------------------------------------
% RETURNS a structure
% results.meth = 'ecm'
% results.nobs = nobs, # of observations
% results.neqs = neqs, # of equations
% results.nlag = nlag, # of lags
% results.nvar = nlag*neqs+nx+1, # of variables per equation
% results.coint= # of co-integrating relations (or r if input)
% results.index= index of co-integrating variables ranked by
%                size of eigenvalues large to small
% --- the following are referenced by equation # --- 
% results(eq).beta   = bhat for equation eq (includes ec-bhats)
% results(eq).tstat  = t-statistics 
% results(eq).tprob  = t-probabilities
% results(eq).resid  = residuals 
% results(eq).yhat   = predicted values (levels) (nlag+2:nobs,1)
% results(eq).dyhat  = predicted values (differenced) (nlag+2:nobs,1)
% results(eq).y      = actual y-level values (nobs x 1)
% results(eq).dy     = actual y-differenced values (nlag+2:nobs,1)
% results(eq).sige   = e'e/(n-k)
% results(eq).rsqr   = r-squared
% results(eq).rbar   = r-squared adjusted
% results(eq).ftest  = Granger F-tests
% results(eq).fprob  = Granger marginal probabilities
% ---------------------------------------------------    
% SEE ALSO: ecmf, becm, recm, prt_var 
% ---------------------------------------------------

% written by:
% James P. LeSage, Dept of Economics
% University of Toledo
% 2801 W. Bancroft St,
% Toledo, OH 43606
% jpl@jpl.econ.utoledo.edu

[nobs neqs] = size(y);

nx = 0; 

if nargin == 3 & r > 0 % user is specifying the # of error correction terms to
             % include and there are error correction terms -- get them using johansen()
 jres = johansen(y,0,nlag);
 % recover error correction vectors
 ecvectors = jres.evec;
   index = jres.ind;
   
 %MANU ADDED THE LINES BELOW -- only for our specific setting
 %The first line only adjusts the first column, i.e. the single cointegrating vector
 if r==1
     %if neqs == 4 %4 variables (without LTI)
     ecvectors(:,1:r)=ecvectors(:,1:r)/ecvectors(1,1:r); %Normalize single cointegrating vector by house price (1st in order)
     cointvec=ecvectors(:,1:r);
     %elseif neqs == 5
     %    ecvectors(:,1:r)=ecvectors(:,1:r)/ecvectors(1,1:r); %Normalize single cointegrating vector by house price (still 1st in order when LTI is included)
     %    cointvec=ecvectors(:,1:r);
     %end
 elseif r==2 & neqs == 4 %This captures k = 11. (It works as long as I don't run any other 4-variable VAR with 2 cointegration relations)
     ecvectors(:,1)=ecvectors(:,1)/ecvectors(1,1); 
     ecvectors(:,2)=ecvectors(:,2)-ecvectors(1,2)*ecvectors(:,1);
     ecvectors(:,2)=ecvectors(:,2)/ecvectors(2,2);
     ecvectors(:,1)=ecvectors(:,1)-ecvectors(2,1)*ecvectors(:,2);
     cointvec=ecvectors(:,1:r);
 elseif r==3 & neqs ==4 %This captures k = 12. 
     ecvectors(:,1)=ecvectors(:,1)/ecvectors(1,1); 
     ecvectors(:,2)=ecvectors(:,2)-ecvectors(1,2)*ecvectors(:,1);
     ecvectors(:,3)=ecvectors(:,3)-ecvectors(1,3)*ecvectors(:,1); 
     ecvectors(:,2)=ecvectors(:,2)/ecvectors(2,2);
     ecvectors(:,1)=ecvectors(:,1)-ecvectors(2,1)*ecvectors(:,2);
     ecvectors(:,3)=ecvectors(:,3)-ecvectors(2,3)*ecvectors(:,2);
     ecvectors(:,3)=ecvectors(:,3)/ecvectors(3,3);
     ecvectors(:,1)=ecvectors(:,1)-ecvectors(3,1)*ecvectors(:,3);
     ecvectors(:,2)=ecvectors(:,2)-ecvectors(3,2)*ecvectors(:,3);
     cointvec=ecvectors(:,1:r);
 elseif r == 2 & neqs == 7 %This captures k = 13.
     ecvectors(:,1)=ecvectors(:,1)/ecvectors(1,1); 
     ecvectors(:,2)=ecvectors(:,2)-ecvectors(1,2)*ecvectors(:,1);
     ecvectors(:,2)=ecvectors(:,2)/ecvectors(2,2);
     ecvectors(:,1)=ecvectors(:,1)-ecvectors(2,1)*ecvectors(:,2);
     cointvec=ecvectors(:,1:r);
 elseif r == 3 & neqs == 7 %This captures k = 14.
     ecvectors(:,1)=ecvectors(:,1)/ecvectors(1,1); 
     ecvectors(:,2)=ecvectors(:,2)-ecvectors(1,2)*ecvectors(:,1);
     ecvectors(:,3)=ecvectors(:,3)-ecvectors(1,3)*ecvectors(:,1); 
     ecvectors(:,2)=ecvectors(:,2)/ecvectors(2,2);
     ecvectors(:,1)=ecvectors(:,1)-ecvectors(2,1)*ecvectors(:,2);
     ecvectors(:,3)=ecvectors(:,3)-ecvectors(2,3)*ecvectors(:,2);
     ecvectors(:,3)=ecvectors(:,3)/ecvectors(3,3);
     ecvectors(:,1)=ecvectors(:,1)-ecvectors(3,1)*ecvectors(:,3);
     ecvectors(:,2)=ecvectors(:,2)-ecvectors(3,2)*ecvectors(:,3);
     cointvec=ecvectors(:,1:r);
 end
 

 %Make sure that index doesn't alter order of columns
if neqs == 3
    if index == [1 2 3]'
    else
        error('eigenvalue indices not 1 2 3')
    end 
elseif neqs == 4
    if index == [1 2 3 4]'
    else
        error('eigenvalue indices not 1 2 3 4')
    end
elseif neqs == 5
    if index == [1 2 3 4 5]'
    else
        error('eigenvalue indices not 1 2 3 4 5')
    end
elseif neqs == 6
    if index == [1 2 3 4 5 6]'
    else
        error('eigenvalue indices not 1 2 3 4 5 6')
    end 
elseif neqs == 7
    if index == [1 2 3 4 5 6 7]'
    else
        error('eigenvalue indices not 1 2 3 4 5 6 7')
    end  
    
end
%MANU ADDED THE LINES ABOVE



 % construct r-error correction variables
 x = mlag(y(:,index),1)*ecvectors(:,1:r); 
   [nobs2 nx] = size(x);
   

elseif nargin == 3 & r == 0 %User specifies that there is no cointegration relation, so it is a VAR in first differences
%MANU ADDED THIS

%In this case, don't construct any error correction terms
%nx, the number of cointegration terms, remains zero.


elseif nargin == 2 % we need to find r
 jres = johansen(y,0,nlag);
 % find r = # significant co-integrating relations using
 % the trace statistic output
 trstat = jres.lr1;
 tsignf = jres.cvt;
 r = 0;
 for i=1:neqs;
  if trstat(i,1) > tsignf(i,2)
   r = i;
  end;
 end;
 % recover error correction vectors
 ecvectors = jres.evec;
   index = jres.ind;


   
 % construct r error correction variables
 x = mlag(y(:,index),1)*ecvectors(:,1:r); 
   [junk nx] = size(x);    
else
 error('Wrong # of arguments to ecm');
end;






% nvar adjusted for constant term 
 k = neqs*nlag+nx+1; %Number of coefficients per equation
 nvar = k;

% transform to 1st difference form
dy = tdiff(y,1);
dy = trimr(dy,1,0); % account for differencing
if r > 0 %MANU ADDED THIS CONDITIONALITY (BUT NOT THE LINE BELOW)
    x = trimr(x,1,0);   % account for differencing
end

% call VAR using 1st difference and co-integrating variables
% call depends on whether we have an x-matrix or not
if nx ~= 0 
results = vare(dy,nlag,x);
else
results = vare(dy,nlag);
end;

for j=1:neqs;
results(j).y = y(:,j);
results(j).dy = dy(:,j);
results(j).dyhat = results(j).yhat;
% find predicted values in levels form
ylag = lag(y(:,j),1);
ylag = trimr(ylag,nlag+1,0);
yhat = results(j).yhat + ylag;
results(j).yhat = yhat;
%MANU ADDED THE LINE BELOW. THAT WAY, I CAN GET RESIDUALS INTO
%IMPULSOVECM_MANU WITHOUT HAVING TO RE-ESTIMATE VECM THERE
results(j).resid = results(j).resid;
end;

if r > 0 %There is at least one cointegration relation %MANU ADDED THIS LINE
    results(1).meth = 'ecm';
    results(1).coint = r;
    results(1).index = index;
    %MANU ADDED THE LINES BELOW. THAT WAY, impulsovecm_manu has the
    %cointegrating vector(s).
    %if r==1
    results(1).cointvec  = cointvec;
    %end
elseif r == 0 %no cointegration relation, i.e. VAR in first differences %MANU ADDED THESE THREE LINES
    results(1).meth = 'vare';
    results(1).coint = r; %0 cointegration relationships
    results(1).index = nan;
    results(1).cointvec = nan;
end

