%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Switching regression model
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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\switch_data;

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% prepare data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

f=find(y1<=0);
y1(f)=0;
g=find(y1>0);
y1(g)=1;

y=[y1 y2 y3];

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

%generate Xi and collect as separate matrices
X=cell(n,1);
for i=1:n
    X{i}=[X1(i,:) zeros(1,k2+k3); zeros(1,k1) X2(i,:) zeros(1,k3);...
          zeros(1,k1+k2) X3(i,:)];
end

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

% generic OLS
bols1=inv(X1'*X1)*X1'*y1;
bols2=inv(X2'*X2)*X2'*y2;
bols3=inv(X3'*X3)*X3'*y3;
clear y1 y2 y3;

% elements for beta
mu0=zeros(k,1); %diffuse prior for mean of betas
V0=eye(k)*100; % diffuse prior for varcov of beta
betadraw=[bols1;bols2;bols3];

% elements for E
v0=m+1; % same as KPT
S0=eye(m); 
Edraw=iw_sig11(v0,inv(S0),1); % use Nobile's (2000) contrained IW draws-routine

% elements for ystar
nf=length(f); % number of zero cases
ng=length(g); % number of >0 cases
l0=-inf*ones(nf,1); %lower truncation point for zero cases
r0=zeros(nf,1);% upper truncation point for zero cases
lpos=zeros(ng,1);% lower truncation point for positive amounts
rpos=inf*ones(ng,1);%upper truncation point for positive amount

ydraw=y;

[betamat,Emat]=gs_switch(X,X1,X2,X3,k1,k2,k3,k,m,n,r1,r2,mu0,V0,betadraw,v0,S0,Edraw,...
    f,g,nf,ng,l0,r0,lpos,rpos,ydraw);

'GS done'
% put all draws together & run diagnostics
allmat=[betamat;Emat];
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,'\n');

fprintf(fid,'true betas \t%6.3f\n',[beta1true;beta2true;beta3true]); 
fprintf(fid,'\n');
fprintf(fid,'\n');

fprintf(fid,'true E \t%6.3f\n',var_vec(Etrue)); 
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');

out=kdiag(k+1:end,:)';
fprintf(fid,'Output table for E \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\mod9_switch betamat Emat;


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;
