% Loop over credit shock size
% Created by Camilo Marchesini after asking in Dynare Forum

    % When you invoke the solver, I suggest you set irf=0 and specify

% options_.nomoments=0;
% options_.nofunctions=1;
% options_.nograph=1;
% options_.verbosity=0;
% options_.noprint=1;
% options_.TeX=0; 

% Declare model name.

model_name='dyn_main';

% Call model.

eval(['dynare ',dyn_main])

% Shock of interest (make shock name as character type)

shock={'enH'};  exo_name=char(shock);

% Find position in declaration of exogenous variables.

eps_pos=strmatch(exo_name,M_.exo_names,'exact');

% get variable positions in variable list.

pi_pos=strmatch('pi',M_.endo_names,'exact');

y_pos=strmatch('y',M_.endo_names,'exact');

% Grid.

SIGM_grid = 0.01:0.005:0.5; % Size of shock.

% Pre-allocate.

variance.pi=zeros(length(SIGM_grid),1);

variance.y=zeros(length(SIGM_grid),1);

% Storage matrix.

mat_loop=zeros(3,length(SIGM_grid));

% Start counter.

n=0;

% Start loop.

  for ii = 1:length(SIGM_grid)

          % Assign variance to the correct diagonal element in the

          % variance-covariance matrix.

          M_.Sigma_e(eps_pos,eps_pos) = SIGM_grid(ii); 

          

          info=stoch_simul(var_list_); % loop over stoch_simul.

 

      if info == 0

        %read out variances.    

        variance.xH(ii)=oo_.var(xH_pos,xH_pos); %finding domestic exports variance

        variance.xF(ii)=oo_.var(xF_pos,xF_pos); %finding domestic imports variance

      else

        variance.xH(ii)=NaN;

        variance.xF(ii)=NaN;

     end

% Next iteration.   

n=n+1;

% Store.

mat_loop(:,n)=[SIGM_grid(ii);sqrt(variance.xH(ii));sqrt(variance.xF(ii))];

  end