function OUT = sar_full(y,X,W)

%SAR computes Spatial AutoRegression of y on X given weights W
%
%INPUTS: (i)   y = n-vector of Yi values
%        (ii)  X = (nxk)-matrix of explanatory variables
%                  (Xi1,..,Xik) [NOT including unit vector]
%        (iii) W = (nxn)-matrix of spatial weights
%
%OUTPUTS: OUT = (3x1)-cell array
%         OUT{1} = (k+1)-vector of beta coefficients
%         OUT{2} = rho estimate
%         OUT{3} = Log Likelihood

[n,k] = size(X) ;

%Augment X with unit vector

XX = X ;

clear('X') ;

X(:,1) = ones(n,1) ;

X(:,2:k+1) = XX ;

%Initial OLS estimation of beta

b = X\y ;

e = y - X * b ;

%Initial Estimate of rho

rho = rho_est(W,e) 

z0(1) = rho ;

z0(2:k+2) = b' ;

   
   B = sparse(eye(n) - rho * W) ;
   
   A = B' * B ;
   
   
z = fmins('full_lik',z0,[],[],W,y,X) ; 
   
%Compute Log Likelihood

var = (1/n) * e'*A*e ;

L = -(n/2)*(1 + log(2*pi) + log(var)) + log(det(B));

OUT{1} = z(1) ;

OUT{2} = z(2:k+2) ;

OUT{3} = L ;
