Loop over parameters

This forum is closed. You can read the posts but cannot write. We have migrated the forum to a new location where you will have to reset your password.
Forum rules
This forum is closed. You can read the posts but cannot write. We have migrated the forum to a new location (https://forum.dynare.org) where you will have to reset your password.

Re: Loop over parameters

Postby htastan » Fri Jan 17, 2014 1:11 pm

Suppose that we want to estimate the parameters using GMM. In particular, we would like to match the theoretical moments implied by the linearized model to the sample moments based on the data. Also assume that we have a separate toolbox for GMM estimation in which we just need to code the moment conditions in a separate m file. In this case, we need to pass the information obtained from first run of the Dynare to parse the code to the moment function in GMM and the objective function. Can we do this as follows:

1. In a Matlab script file define the parameters and all other necessary stuff along with

global var_list_ oo_ M_ ex0_ options_ estimation_info info
save parameters gamma1 alpha1 delta1 rho1 beta1 sigma1
dynare stochgrowth_dyna noclearall

2. In the mod file define

parameters gamma alpha delta rho beta sigma;

load parameters;

set_param_value('gamma',gamma1) % utility parameter
set_param_value('alpha',alpha1) % production elasticity
set_param_value('delta',delta1) % depreciation rate
set_param_value('rho',rho1) % production shock autocorrelation
set_param_value('beta',beta1 ) % discount factor
set_param_value('sigma',sigma1) % production shock volatility

at the beginning, the rest of the mode file is standard.

3. In the GMM moment function matlab m file:

function mom = GMMmoments(theta,X,avg)

global var_list_ oo_ M_ ex0_ options_ estimation_info info

gamma1 = theta(1);
alpha1 = theta(2);
delta1 = theta(3);
rho1 = theta(4);
beta1 = theta(5);
sigma1 = theta(6);

yobs = X(:,1);
cobs = X(:,2);
kobs = X(:,3);
iobs = X(:,4);

set_param_value('gamma',gamma1)
set_param_value('alpha',alpha1)
set_param_value('delta',delta1)
set_param_value('rho',rho1)
set_param_value('beta',beta1 )
set_param_value('sigma',sigma1)

info = stoch_simul(var_list_);
if info
disp('Stochastic Simulation failed');
end

xmean = oo_.mean;
xvar = diag(oo_.var);

Basically, what I did here is to define Dynare solution variables as GLOBAL variables in the main file and the moment file. I am not a big fan of using global variables but I could not use local variables. Is this procedure OK? Is there any other ways?

(By the way I did not post the full codes but they seem to work OK)
htastan
 
Posts: 10
Joined: Wed Mar 31, 2010 12:10 pm

Re: Loop over parameters

Postby jpfeifer » Fri Jan 17, 2014 1:46 pm

Looks correct to me. There are ways to get around using global variables in the main file, but they are typically not worth the hassle. The only global variables you should need are
Code: Select all
oo_ M_  options_


One important comment: if you are using simulated moments, make sure you use the same random numbers for every parameter draw.
------------
Johannes Pfeifer
University of Cologne
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

Re: Loop over parameters

Postby htastan » Fri Jan 17, 2014 2:10 pm

Exactly. The same set of shocks has to be used in the SMM or indirect inference. One can easily code this using Dynare with second or third order linearization.
Better way is of course to solve the nonlinear eq'm conditions using residual based methods, e.g. collocation, and then to embed it into the indirect inference framework. That would be a very nice extension to the Dynare.

By the way I think we also need

var_list_

as a global variable in addition to the ones you mentioned.
Thanks for your reply,

Cheers
htastan
 
Posts: 10
Joined: Wed Mar 31, 2010 12:10 pm

Re: Loop over parameters

Postby jpfeifer » Fri Jan 17, 2014 2:40 pm

If you did not specify variables for var_list_, you can just use
Code: Select all
var_list_=[];
info = stoch_simul(var_list_);


If you did, you can either make var_list_ global or use something along the lines of
Code: Select all
var_list_=[];
var_list_ = 'yobs ';
var_list_ = char(var_list_, 'cobs');
var_list_ = char(var_list_, 'kobs');
var_list_ = char(var_list_, 'iobs');
info = stoch_simul(var_list_);
------------
Johannes Pfeifer
University of Cologne
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

Re: Loop over parameters

Postby bpyemba » Wed Feb 19, 2014 7:29 am

Hi all,

I am trying to evaluate the conditional welfare and I am getting the following error

??? Reference to non-existent field 'eigval'.

Error in ==> check at 80
eigenvalues_ = dr.eigval;

Error in ==> openeconomy_loop_det at 626
oo_.dr.eigval = check(M_,options_,oo_);

Error in ==> dynare at 120
evalin('base',fname) ;

Error in ==> max_cond_welfare2 at 31
dynare openeconomy_loop_det noclearall nolog

How can I solve the problem?

Thanks Boniface
Attachments
Boniface2.zip
(9.09 KiB) Downloaded 195 times
bpyemba
 
Posts: 13
Joined: Wed Feb 20, 2013 6:38 am

Re: Loop over parameters

Postby jpfeifer » Wed Feb 19, 2014 9:07 am

Your openeconomy_loop_det.mod is unable to compute the steady state.
------------
Johannes Pfeifer
University of Cologne
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

Re: Loop over parameters

Postby bpyemba » Wed Feb 19, 2014 9:19 am

The steady state is reached using dynare 4.3.3. It doesn't work using the version 4.4.1.
bpyemba
 
Posts: 13
Joined: Wed Feb 20, 2013 6:38 am

Re: Loop over parameters

Postby bpyemba » Wed Feb 19, 2014 9:21 am

Hi,

Could do you please run with 4.3.3?

Boniface
bpyemba
 
Posts: 13
Joined: Wed Feb 20, 2013 6:38 am

Re: Loop over parameters

Postby jpfeifer » Wed Feb 19, 2014 9:34 am

No, also with Dynare 4.3.3 it does not find the steady state. This is the fundamental source of your problem.
------------
Johannes Pfeifer
University of Cologne
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

Re: Loop over parameters

Postby bpyemba » Wed Feb 19, 2014 10:24 am

Thanks, I don't understand how it works perfectly with 4.3.3 in my computer, but not with 4.4.1. I am working on.
bpyemba
 
Posts: 13
Joined: Wed Feb 20, 2013 6:38 am

Re: Loop over parameters

Postby jpfeifer » Sat Mar 08, 2014 11:40 am

You could try
Code: Select all
steady(solve_algo=4);

in Dynare 4.4.2
------------
Johannes Pfeifer
University of Cologne
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

Re: Loop over parameters

Postby dphm » Sun Sep 20, 2015 12:36 am

Hi everyone

If I may follow up from this post, I am actually doing the same as kipfilet. But I gotta get the impulse response functions from the newly resolved steady state.

Previously, when I executed the "dynare" command, I simply could use oo_.irfs in my m file. But now, I don't know where the IRFs are stored after I run the command "[dr_,~,M_,~,oo_] = resol(0,M1_,options1_,oo1_);".

May I know how I can plot the impulse response functions thereafter?

Thanks.
dphm
 
Posts: 16
Joined: Fri Sep 11, 2015 12:27 am

Re: Loop over parameters

Postby jpfeifer » Mon Sep 21, 2015 7:09 am

in that case you actually need to loop over
Code: Select all
info = stoch_simul(var_list_);

instead of resol. The irfs will then be stored in their usual place.
------------
Johannes Pfeifer
University of Cologne
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

Re: Loop over parameters

Postby HouseC » Sat Dec 05, 2015 1:34 pm

Dears,
I'm following discussion here and what I'm also doing with grid search is to generate expected welfare for different monetary rules setting.

Here is my example code for loop.
Code: Select all
aaa=[
    0.1 0.3
    0.1 0.3
    0.1 0.3
    0.1 0.3];
first_time = 1;
welfare=zeros(1,2);
for i1=1:2
    e=aaa(1,i1);
    f=aaa(2,i1);
    g=aaa(3,i1);
    h=aaa(4,i1);

    if first_time
save parameterfile1 e f g h;
dynare LcpTax.mod noclearall;
       first_time = 0;
    else
set_param_value('sigma1',e);
set_param_value('sigma2',f);
set_param_value('sigmas1',g);
set_param_value('sigmas2',h);

nv=101;
eu_v=zeros(1,nv);
eus_v=zeros(1,nv);
eu_w=zeros(1,nv);

for jj=1:nv;
v=(jj-1)/(nv-1);
info=stoch_simul(var_list_);
eu_v(jj)=oo_.mean(26);
cbar=oo_.steady_state(1);
lbar=oo_.steady_state(3);
eu_v(jj)=(((eu_v(jj)*(1-beta)+eta*lbar)*(1-rho))^(1/(1-rho))/cbar-1)*100;

eus_v(jj)=oo_.mean(27);
csbar=oo_.steady_state(13);
lsbar=oo_.steady_state(15);
eus_v(jj)=(((eus_v(jj)*(1-beta)+eta*lsbar)*(1-rho))^(1/(1-rho))/csbar-1)*100;
eu_w(jj)=n*eu_v(jj)+(1-n)*eus_v(jj);

end;


My questions is:
based on the discussion, we only need to call dynare once and then change different parameter and redo the simulation. I double check the result in this example code, and find that the estimated welfare are different between calling dynare each time and the method mentioned in the previous code.
I believe that calling dynare for each loop will return the correct result, then anyone could suggest what's the problem with my code listed above?
HouseC
 
Posts: 41
Joined: Fri Apr 19, 2013 3:36 am

Re: Loop over parameters

Postby monsoon » Sun Dec 06, 2015 1:47 am

Just clarifying a doubt with reference to the above topic. Is looping important when we deal with endogenous collateral constraints?
monsoon
 
Posts: 35
Joined: Mon Nov 09, 2015 4:47 am

PreviousNext

Return to Dynare help

Who is online

Users browsing this forum: No registered users and 8 guests