% Loop over credit shock size
% With Camilo Marchesini's help 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 ',model_name])

% 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.

ex_pos=strmatch('exH',M_.endo_names,'exact'); %Home exports

etaH_pos=strmatch('exF',M_.endo_names,'exact'); %tightness of credit constr

% Grid.
ini=0.001^2; %var=stderr^2
fin=0.32^2;

SIGM_grid = ini:0.0000005:fin; % Size of shock.

% Pre-allocate.

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

variance.exF=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.exH(ii)=oo_.var(exH_pos,exH_pos); %finding domestic exports variance

        variance.exF(ii)=oo_.var(exF_pos,exF_pos); %finding domestic imports variance

      else

        variance.exH(ii)=NaN;

        variance.exF(ii)=NaN;

     end

% Next iteration.   

n=n+1;

% Store.

mat_loop(:,n)=[SIGM_grid(ii);sqrt(variance.exH(ii));sqrt(variance.exF(ii))];

  end