Code to set initial values for estimation and calibrated values using the oo_ structure

Hi All
Here is a follow-up to my previous post

The following code will print output in the command window, to be pasted in the estimated_param_init block and the calibration (of deep parameters) section that follows after the parameter names are first declared.

When I code, the order in which I declare the parameters is preserved in all sections (initial declaration, calibration, priors, initial values, bounds).

The code below is written to suit that style, and has not been tested for cases when the order of declaration is different from time to time. I am not sure if order of declaration matters for the order in which the names of shocks and parameters are save in the oo_ structure.

Cheers
Reuben


% Make initialisation and calibration values for next DYNARE estimation

firstestimparastr='alpha'; % Define the name of first structural parameter that is estimated

%Create vector of IRIS parameter and shock names
estimnames4dynare = [fieldnames(oo_.posterior_mode.shocks_std); fieldnames(oo_.posterior_mode.parameters)] ;
% Concatenate tables
mergetables4dynare = [struct2table(oo_.posterior_mode.shocks_std), struct2table(oo_.posterior_mode.parameters) ];
posteriormode4dynare=table2struct(mergetables4dynare);
posteriormodevals4dynare=structfun(@(xeval) xeval(:), posteriormode4dynare);

exoestim=cell(M_.exo_nbr,1);
shockstdnames=fieldnames(oo_.posterior_mode.shocks_std);
for ii=1:M_.exo_nbr
    exoestim(ii,:)= cellstr(['stderr ' shockstdnames{ii}]);
end
bayestopt_.name(1:M_.exo_nbr,:)=exoestim; % Replace the shock names in the vector with 'stderr shocknames'
a= bayestopt_.name ;
b =repmat(',',numel(bayestopt_.name),1);  % Dynare uses ',' for the estimated_parameters_init section
b1 =repmat('=',numel(bayestopt_.name),1); % Dynare uses '=' for the calibration section
c = num2str(posteriormodevals4dynare,'%.10f') ;          % Convert parameter value to string, and control number of digits
d=repmat(';',numel(bayestopt_.name),1); % Dynare syntax
e=repmat(' % ',numel(bayestopt_.name),1); % For comment that defines longname of parameter
estpara1=find(ismember(cellstr(M_.param_names),firstestimparastr));
estimparalongnames=cellstr(M_.param_names_long(estpara1:end,:)); % Can be avoided if we do not desire to use long names
shocklongnames=cellstr(M_.exo_names_long);% Can be avoided if we do not desire to use long names
f=[shocklongnames;estimparalongnames];% Can be avoided if we do not desire to use long names
disp('=====================================================================')
disp('Paste into estimated_parameters_init block for next dynare estimation')
disp('=====================================================================')
disp(char(strcat(a,b,c,d,e,f)))% e and f can be avoided if we do not desire to use long names
disp('=====================================================================')
gg=strcat(a,b1,c,d,e,f); % Concatenate all ,   % e and f can be avoided if we do not desire to use long names
gg=gg(M_.exo_nbr+1:end, :)  % Start from first estimated structural parameter that appears after the shock std devs.
disp('==============================')
disp('Paste into calibration section')
disp('==============================')
disp(char(gg))

Dear Reuben,

I think two improvements could easily made to your contribution:

  1. Make it a function. Also we may want to have the liberty to choose another moment (not necessarily the posterior mode).
  2. The error prone copy/paste step could be avoided if you print the into a file instead of the matlab prompt. The file could then be included where needed using the macroprocessor @#include directive.

Best,
Stéphane.

Thanks Stephane. Those are very useful recommendations.

Hi Stephane
With regard to your comment

The error prone copy/paste step could be avoided if you print the into a file instead of the matlab prompt. The file could then be included where needed using the macroprocessor @#include directive.

Would you have some example code on how to implement this? That is, using the @#include for accessing the parameter values for the estimated_params_init block ? I have never used this option before.

Thanks
Reuben

Hi Reuben, Is your question about the creation of the file to be included? Or is it about the use of the @#include directive?

Best,
Stéphane.

Actually, both. Would the file be a mod or m file with values ‘printed into’… or would it be a mat file with the parameter values stored in matrices or structures? I have seen examples of the include directive used to harness another mod file…

Thanks
Reuben

The @#include directive simply tells Dynare to put the text in the file to included into this very spot of the mod-file. Take example1.mod:

var y, c, k, a, h, b;
varexo e, u;

parameters beta, rho, alpha, delta, theta, psi, tau;

alpha = 0.36;
rho   = 0.95;
tau   = 0.025;
beta  = 0.99;
delta = 0.025;
psi   = 0;
theta = 2.95;

phi   = 0.1;

model;
c*theta*h^(1+psi)=(1-alpha)*y;
k = beta*(((exp(b)*c)/(exp(b(+1))*c(+1)))
    *(exp(b(+1))*alpha*y(+1)+(1-delta)*k));
y = exp(a)*(k(-1)^alpha)*(h^(1-alpha));
k = exp(b)*(y-c)+(1-delta)*k(-1);
a = rho*a(-1)+tau*b(-1) + e;
b = tau*a(-1)+rho*b(-1) + u;
end;

initval;
y = 1.08068253095672;
c = 0.80359242014163;
h = 0.29175631001732;
k = 11.08360443260358;
a = 0;
b = 0;
e = 0;
u = 0;
end;

shocks;
var e; stderr 0.009;
var u; stderr 0.009;
var e, u = phi*0.009*0.009;
end;

stoch_simul;

You could split this into a main file

var y, c, k, a, h, b;
varexo e, u;

parameters beta, rho, alpha, delta, theta, psi, tau;

alpha = 0.36;
rho   = 0.95;
tau   = 0.025;
beta  = 0.99;
delta = 0.025;
psi   = 0;
theta = 2.95;

phi   = 0.1;

model;
c*theta*h^(1+psi)=(1-alpha)*y;
k = beta*(((exp(b)*c)/(exp(b(+1))*c(+1)))
    *(exp(b(+1))*alpha*y(+1)+(1-delta)*k));
y = exp(a)*(k(-1)^alpha)*(h^(1-alpha));
k = exp(b)*(y-c)+(1-delta)*k(-1);
a = rho*a(-1)+tau*b(-1) + e;
b = tau*a(-1)+rho*b(-1) + u;
end;

initval;
y = 1.08068253095672;
c = 0.80359242014163;
h = 0.29175631001732;
k = 11.08360443260358;
a = 0;
b = 0;
e = 0;
u = 0;
end;

@#include "shocks.inc"

stoch_simul;

where the file shocks.inc contains the shocks-block

shocks;
var e; stderr 0.009;
var u; stderr 0.009;
var e, u = phi*0.009*0.009;
end;

What you would do in your case is print the definition of the block into a text-file.

1 Like

Thanks a lot, Johannes. Wonderfully explained!