% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % Simple Poisson model using fake data
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

tic;  % start stop watch

rand('state',37); % set arbitrary seed for uniform draws
randn('state',37); % set arbitrary seed for normal draws

load c:\klaus\AAEC6564\mlab\worksp\Poisson_data;

% Open log file 
[fid]=fopen('c:\klaus\AAEC6564\mlab\logs\mod7_Poisson_rwc.txt','w');
if fid==-1;
   warning('File could not be opened');
else;
   disp('File opened successfully');
end;

N=length(y); %total number of observations
k=size(X,2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% starting values, priors, and tuners
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% general elements
r1=250000; % burn-ins
r2=10000; % keepers
R=r1+r2;

% generic OLS
bols=inv(X'*X)*X'*y;

% elements for beta (fixed coefficients)
mub0=zeros(k,1); 
Vb0=eye(k)*100; 

%variance elements for candidate-generating density (CGD)
load c:\klaus\AAEC6564\mlab\worksp\mod7_Poisson_rwc_prep betamat;
c=2.8;
Vc=cov(betamat');

betadraw = bols;

[betamat,acceptmat]=gs_Poisson_rwc(y,X,k,N,mub0,Vb0,c,Vc,betadraw,r1,r2);

'GS done'


% put all draws together
allmat=[betamat];%
kdiag=klausdiagnostics_greater0(allmat);

fprintf(fid,'total number of iterations =\t%6.0f \n',R);
fprintf(fid,'burn-in iterations =\t%6.0f \n',r1);
fprintf(fid,'tuner for variance in MH part=\t%6.3f \n',c);
fprintf(fid,'acceptance rates in MH part =\t%6.3f \n',acceptmat);
fprintf(fid,'\n');

fprintf(fid,'True betas \n\n');
fprintf(fid,'%6.3f\n',bet');
fprintf(fid,'\n');

% beta stuff
out=kdiag'; 
fprintf(fid,'Output table for betas \n\n');
fprintf(fid,'mean\t\tstd\t\tp(>0)\t\tnse\t\tIEF\t\tm*\t\tcd\n\n');
fprintf(fid,'%6.3f\t%6.3f\t%6.3f\t%6.3f\t%6.3f\t%6.3f\t%6.3f\n',out);
fprintf(fid,'\n');

finish = toc/60;
fprintf(fid,'Time elapsed in minutes \n\n');
fprintf(fid,'%6.3f\n',finish);



st=fclose(fid);
if st==0;
   disp('File closed successfully');
else;
   warning('Problem with closing file');
end;

save c:\klaus\AAEC6564\mlab\worksp\mod7_Poisson_rwc betamat;

