
%This example program performs a simple Monte Carlo experiment with a CAR model using the data from:
%Pace, R. Kelley, and Ronald Barry, Quick Computation of Regressions with a Spatially Autoregressive Dependent Variable,
%Geographical Analysis, Volume 29, Number 3, July 1997, p. 232-247. 

%Written by Kelley Pace, 6/23/97

%For more on maximum likelihood CAR computations for estimation and simulation see Pace and Barry (1998) "Fast CARS," 
%Journal of Statistical Computation and Simulation

%The procedure below is computationally inefficient due to restarting the estimation afresh each iteration. 
%A larger MC study can be made vastly speedier using the techniques in "Fast CARS."





%This example uses Nearest Neighbor weights
%It uses the symmetric weighting matrix, wsw


load xsub;
load y;
load wswnn;
wsw=wswnn;
load detvalznn;
detvalz=detvalznn;

[n,k1]=size(xsub);
betas=ones(k1,1);
xbetas=xsub*betas;

iter=5;
true_parameter=0.75;

tic;
[rvcorr]=fsimcar1(wswnn,randn(n,iter),true_parameter);
toc;

elapsed_time =

    1.4620



alphavec=zeros(iter,1);
tic;
for i=1:iter;
y=xbetas+rvcorr(:,i);
[alphamax,loglik,emax,bmax,likratios,prhigher]=fcar1(xsub,y,detvalz,wsw);
alphavec(i)=alphamax;
end;
y=xbetas+rvcorr(:,i);
[alphamax,loglik,emax,bmax,likratios,prhigher]=fcar1(xsub,y,detvalz,wsw);
alphavec(i)=alphamax;
end;
y=xbetas+rvcorr(:,i);
[alphamax,loglik,emax,bmax,likratios,prhigher]=fcar1(xsub,y,detvalz,wsw);
alphavec(i)=alphamax;
end;
y=xbetas+rvcorr(:,i);
[alphamax,loglik,emax,bmax,likratios,prhigher]=fcar1(xsub,y,detvalz,wsw);
alphavec(i)=alphamax;
end;
y=xbetas+rvcorr(:,i);
[alphamax,loglik,emax,bmax,likratios,prhigher]=fcar1(xsub,y,detvalz,wsw);
alphavec(i)=alphamax;
end;
toc;

elapsed_time =

    7.5110


true_parameter

true_parameter =

    0.7500

[min(alphavec) median(alphavec) max(alphavec)]

ans =

    0.7200    0.7300    0.7900


iter

iter =

     5


diary off;
