%SPATIAL_SUR_TEST simulates a set of data for 
%  testing the Spatial SUR model in Anselin.
%
% The model tested is of the form:
%
%     y1 = b0 + b1*x1 + b2*z1 + e1
%     y2 = a0 + a1*x2 + a2*z2 + e2
%
% where (e1,e2) ~ N[(0,0),(sig1*I,cov*I;cov*I,sig2)]
%       

%NOTE: Weight Matrix, W, assumed in work space


%Parameter Specification

n = 26;

rho1 = .4;
rho2 = .7; 


b0 = 1;
b1 = 2;
b2 = 3;
a0 = 3;
a1 = 5;
a2 = 1;

b = [b0,b1,b2,a0,a1,a2]';


%Weight Matrix manipulations

I = eye(n);

B1 = I - rho1*W;

B2 = I - rho2*W;

D1 = inv(B1);

D2 = inv(B2);

B = [B1,zeros(n);zeros(n),B2];

D = [D1,zeros(n);zeros(n),D2];


%Construct Data

x1 = 10*rand(n,1);
x2 = 20*rand(n,1);
z1 = 10*rand(n,1);
z2 = 20*rand(n,1);
u  = ones(n,1);

Z = zeros(n,3);

X = [u,x1,z1,Z;Z,u,x2,z2];

% Construct Covariance Matrix

sig1 = 2;
sig2 = 4;
cov = .7;

I = eye(n);

S = [sig1,cov;cov,sig2];

COV = kron(S,I);

A = chol(COV);


%Simulate Errors 

UU = normrnd(0,1,2*n,1);

U = A'*UU;

e = D*U;

y = X*b + e;

y1 = y(1:n);

y2 = y(n+1:2*n);

DAT = [y1,x1,z1,y2,x2,z2];










