%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ordered Probit a la Poirier & Tobias Book chapter
% i.e. re-parameterized a la Nandram & Chen
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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\OP_data;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% starting values, priors, and tuners
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% general elements
r1=15000; % burn-ins
r2=5000; % keepers
R=r1+r2;
k=size(X,2);
nc=3; %number of cutoff points to be estimated
% generic OLS
bols=inv(X'*X)*X'*y;
res=y-X*bols;
s2=(res'*res)/(n-k);
% elements for beta
mu0=zeros(k,1); %diffuse prior for mean of betas
V0=eye(k)*100; % diffuse prior for varcov of beta
betadraw=bols;
% elements for del2 (squared inverse of highest cutoff (c3), but also new
% variance of regression error)
eta0=1/2;
kappa0=1/2; % diffuse prior shape and scale
del2draw=s2; % use OLS variance as starting draw for GS (Tobias uses "1" in his code)
% Priors and Metropolis Hastings elements for cutoffs
Vc=10;
stdc1=0.8*sqrt(1/n);
stdc2=0.8*sqrt(1/n);
c1draw=1/4;
c2draw=1/2; % starting draws for re-parameterized thresholds
[betamat,cmat,acceptmat]=gs_OP(X,y,k,n,nc,r1,r2,mu0,V0,betadraw,eta0,kappa0,del2draw,...
Vc,stdc1,stdc2,c1draw,c2draw,f0,f1,f2,f3,f4);
'GS done'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Convergence Checks - Geweke's CD diagnostic
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Open log file
[fid]=fopen('c:\klaus\aaec6564\mlab\logs\mod11_op.txt','w');
if fid==-1;
warning('File could not be opened');
else;
disp('File opened successfully');
end;
% put all draws together & run diagnostics
allmat=[betamat;cmat];
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,'acceptance rates in MH part =\t%6.3f \n',acceptmat);
betatrue=bet';
fprintf(fid,'True betas \n\n');
fprintf(fid,'%6.3f\n',betatrue);
fprintf(fid,'\n');
ctrue=[c1;c2;c3]';
fprintf(fid,'True cutoffs \n\n');
fprintf(fid,'%6.3f\n',ctrue);
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');
%c stuff
out=kdiag(k+1:end,:)';
fprintf(fid,'Output table for thresholds \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\mod11_op betamat cmat;
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;