function OUT = sar_nn_sim(rho,N,k)

%SAR_NN_SIM simulates a symmetric nearest-neighbor autoregressive process on a 
%        randomly sampled set of points in the unit square
%
%INPUTS: (i)     rho = autoregressive parameter
%        (ii)    N = number of points to locate
%        (iii)   k = number of simulated data sets
%
%OUTPUT: OUT{1} = (Nx2)-matrix of location coordinates (Xi,Yi)
%        OUT{2} = sparse nearest-neighbor weight matrix.
%        OUT{3} = (Nxk)-matrix of k simulated data sets.

%Make Points 

L = rand(N,2) ;

U = ones(N,1) ;

%Compute nearest neighbors

DAT = dist_wts(L,2) ; 

W = DAT{1} ;

%Compute inv(I - rho*W)

M = inv(eye(N) - rho*W) ;

%Simulate data

X = normrnd(0,1,N,k) ;

%convert to covariance stationary data

Y = M * X ;

%Output Data

OUT{1} = L ;
OUT{2} = W ;
OUT{3} = Y ;

%Compute measures

DAT = zeros(k,4);

for i = 1:k
    
    z = Y(:,i);
    
	I = (z'*W*z)/(z'*z) ;
	
	rho = (z'*W*z)/(z'*W'*W*z) ;
	
	r = (z'*W*z)/(norm(z)*norm(W*z)) ;
	
	d = (norm(z - W*z)/norm(z))^2 ;	
	
	v = [I,rho,r,d] ;
	
	DAT(i,:) = [I,rho,r,d];
    
end

OUT{4} = DAT;

