% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % Simple Poisson model using fake data
% % starter script for Chib method
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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;

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');
clear betamat;

%load actual GS output
load c:\klaus\AAEC6564\mlab\worksp\mod7_Poisson_rwc betamat;
[logpmarg,lp,llf,lpost]=gs_Poisson_rwc_chib(y,X,k,N,mub0,Vb0,c,Vc,r1,r2,betamat);

'GS done'

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

fprintf(fid,'logpmarg=\t%6.4f \n',logpmarg);
fprintf(fid,'lp=\t%6.4f \n',lp);
fprintf(fid,'llf=\t%6.4f \n',llf);
fprintf(fid,'lpost=\t%6.4f \n',lpost);


save c:\klaus\AAEC6564\mlab\worksp\mod7_Poisson_rwc_chib logpmarg lp llf lpost;

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

