%run_bayes.m estimates the parameters of the model and stores the result in bayes.mat
%Replication materials for: ``What Do Long Data Tell Us About the Inflation Hike Post COVID-19 Pandemic?''
%© Stephanie Schmitt-Grohé and Martín Uribe,  2023. 

clear all, clc

tic


%load the seed for the random number generator that replicates the results in the paper.  Otherwise, the following two commands  can be percentaged out. 
load rng_replication.mat rng_replication   
%produced by executing the commands
%rng('shuffle')
%rng_replication = rng
%save rng_replication.mat rng_replication
rng(rng_replication); 


do50million = 1;% set length of MCMC chain

CD = cd, disp(CD);

disp('run_bayes.m  may take several hours to run'); disp(' ')

mxlp = -inf; %likelihood
mxpar = [];%parameter vector 

I = 10; %# of times probe.m will be run
for  i = 1:I

disp(['Running probe.m: run ' num2str(i) ' of ' num2str(I)]);

[mxpar0,mxlp0,saverng0] = probe; 

if  mxlp0 > mxlp
mxlp = mxlp0;
mxpar =  mxpar0;
saverng = saverng0;
save probe.mat mxpar mxlp saverng i
end %if  mxlp0 > mxlp
end%for i=1:I

save probe.mat mxpar mxlp saverng i

disp('Running clone.m.');
clone;

load clone.mat MPE MP
%produced by running clone.m 
load probe.mat mxlp mxpar %produced by running probe.m
if  MP>mxlp
param0 = MPE; %param
posterior0 = MP; %draw of log posterior likelihood.
else %if  MP>mxlp
param0 = mxpar;
posterior0 = mxlp;
end %if  MP>mxlp

disp('Done getting an initial condition for bayes.m')

disp('Running bayes.m')
bayes(param0,posterior0);% produces bayes.mat file
!copy bayes.mat bayes1.mat

if do50million
disp('Produce a 1-million-draw posterior chain randomly picked from an MCMC chain of 50 million draws ')
load bayes.mat Ci kappa param posterior
nburn = 1e6;
ndraw = 50e6;
randpick = 1e6;
 progress_report = 1;
bayes(param,posterior,Ci,kappa,nburn,ndraw,randpick, progress_report) 
end


disp('Estimation Completed')

elapsed_time = toc/60^2; %time to completion in hours

disp(['Time to completion ' num2str(elapsed_time) ' hours'])

save run_bayes.mat
