%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Show the effect of inefficient blocking
% for the labor supply model
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear; 

% Set random number generator, start stop watch, open output file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rand('state',37); % set arbitrary seed for uniform draws
randn('state',37); % set arbitrary seed for normal draws

tic;  % start stop watch


% Load & prepare data
%%%%%%%%%%%%%%%%%%%%%%
load c:\klaus\AAEC6564\mlab\worksp\labordata;
%We will use  Mroz's (1987) labor data described in Greene (5th edition),
%p. 51

% Contents of Data (columns)

%1       LFP = A dummy variable = 1 if woman worked in 1975, else 0 
%2       WHRS = Wife's hours of work in 1975 
%3       KL6 = Number of children less than 6 years old in household 
%4       K618 = Number of children between ages 6 and 18 in household 
%5       WA = Wife's age 
%6       WE = Wife's educational attainment, in years 
%7       WW = Wife's average hourly earnings, in 1975 dollars 
%8       RPWG = Wife's wage reported at the time of the 1976 interview (not = 1975 estimated wage) 
%9       HHRS = Husband's hours worked in 1975 
%10      HA = Husband's age 
%11      HE = Husband's educational attainment, in years 
%12      HW = Husband's wage, in 1975 dollars 
%13      FAMINC = Family income, in 1975 dollars 
%14      WMED = Wife's mother's educational attainment, in years 
%15      WFED = Wife's father's educational attainment, in years 
%16      UN = Unemployment rate in county of residence, in percentage points. 
%17      CIT = Dummy variable = 1 if live in large city (SMSA), else 0 
%18      AX = Actual years of wife's previous labor market experience 

f=find(data(:,1)==0); % we only include women that worked in 1975
data(f,:)=[];

y=log(data(:,2).*data(:,7)); %dependent variable = log(hrs worked* hourly wage)
KL6=data(:,3); %number of children under 6
K618=data(:,4); % Number of children between ages 6 and 18 in household 
age=data(:,5);
age2=age.^2;
edu=data(:,6); %years of education

n=length(y);

%%%%%%%%%%%%%%%%%%%%%%%
% Group X into 3 blocks
%%%%%%%%%%%%%%%%%%%%%%%

X=[ones(n,1) age age2 edu KL6 K618];
X1=X(:,1:2);
X2=X(:,3:4);
X3=X(:,5:6);

k1=size(X1,2);
k2=size(X2,2);
k3=size(X3,2);
k=k1+k2+k3;

% Estimation
%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% starting values, priors, and tuners
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% general elements
r1=5000; % burn-in
r2=10000; % keepers
R=r1+r2;

% generic OLS
bols=inv(X'*X)*X'*y;
res=y-X*bols;
s2=(res'*res)/(n-k);

% elements for beta1
mu01=zeros(k1,1); 
V01=eye(k1)*100; 
beta1draw=bols(1:k1); 

% elements for beta2
mu02=zeros(k2,1); 
V02=eye(k2)*100; 
beta2draw=bols(k1+1:k1+k2); 

% elements for beta3
mu03=zeros(k3,1); 
V03=eye(k3)*100; 
beta3draw=bols(k1+k2+1:end); 

% elements for sig2
sig2draw=s2; % use OLS variance as starting draw for Gibbs Sampler
v0=1/2;
tau0=1/2; % diffuse prior shape and scale

[beta1mat,beta2mat,beta3mat,sig2mat]=...
    gs_normal_blocked(X,X1,X2,X3,y,n,k,k1,k2,k3,r1,r2,mu01,V01,beta1draw,...
    mu02,V02,beta2draw,mu03,V03,beta3draw,v0,tau0,sig2draw);

'GS done'


% put all draws together & run diagnostics
allmat=[beta1mat;beta2mat;beta3mat;sig2mat];
kdiag=klausdiagnostics_greater0(allmat);

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

fprintf(fid,'total number of iterations =\t%6.0f \n',R);
fprintf(fid,'burn-in iterations =\t%6.0f \n',r1);
fprintf(fid,'\n');

fprintf(fid,'OLS betas \t%6.3f\n',bols); 
fprintf(fid,'OLS s2 \t%6.3f\n',s2); 
fprintf(fid,'\n');
fprintf(fid,'\n');

% beta stuff
out=kdiag(1:k,:)';
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');

%sig2 stuff
out=kdiag(k+1,:)';
fprintf(fid,'Output table for sig2 \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');

save c:\klaus\AAEC6564\mlab\worksp\mod2blocking beta1mat beta2mat beta3mat sig2mat ...
    X1 X2 X3 X y;

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;










