%% set inputs for solution below
%  The program produces responses to the shocks selected below under
%  irfshock. Paths for all the endogenous variables in the model selected
%  are produced. VariableName_difference holds the piece-wise linear solution for
%  VariableName.  VariableName_uncdifference holds the linear solution for
%  VariableName.

global M_ oo_

% modname below chooses model
% directory. But simple param choices are made from paramfile_borrcon in current
% directory.

modnam = 'SYP_loglinear';
modnamstar = 'SYP_loglinear_b';

% see notes 1 and 2 in README
constraint = 'i_r <  i_d_bound + log(Pi_ss/beta_d - 1)';
constraint_relax ='i_r_not > i_d_bound + log(Pi_ss/beta_d - 1)';

% Pick innovation for IRFs
irfshock =char('varepsilon_xi');      % label for innovation for IRFs

shockssequence = zeros(20,1);
shockssequence(3) = 0.25;
maxiter = 20;
nperiods = 20;

    
[zdatalinear zdatapiecewise zdatass oobase_ Mbase_  ] = ...
  solve_one_constraint(modnam,modnamstar,...
  constraint, constraint_relax,...
  shockssequence,irfshock,nperiods);


% unpack the IRFs
for i=1:Mbase_.endo_nbr
  eval([deblank(Mbase_.endo_names(i,:)),'_l=zdatalinear(:,i);']);
  eval([deblank(Mbase_.endo_names(i,:)),'_p=zdatapiecewise(:,i);']);
  eval([deblank(Mbase_.endo_names(i,:)),'_ss=zdatass(i);']);
end
   
titlelist = char('i^{r} (reserve rate)','i^{d} (deposit rate)','i^{l} (loan rate)','Y (output)', '\Pi (inflation)', '\xi');
percent = 'Percent';
level = 'Level';
ylabels = char('%','%','%', '% deviation from SS', '%', 'Level');
figtitle = '';
legendlist = cellstr(char('No bound','Negative rates'));

% i_r_level_ss = Pi_ss/beta_d - 1;
% % i_r_log_p = (i_r_p + log(i_r_level_ss));
% % i_r_level_p = 400*exp(i_r_log_p);
% i_r_log_l = (i_r_l + log(i_r_level_ss));
% i_r_level_l = 400*exp(i_r_log_l);
% 
% i_d_level_ss = Pi_ss/beta_d - 1;
% i_d_log_p = (i_d_p + log(i_d_level_ss));
% i_d_level_p = 400*exp(i_d_log_p);
% i_d_log_l = (i_d_l + log(i_d_level_ss));
% i_d_level_l = 400*exp(i_d_log_l);
% 
% i_l_level_ss = Pi_ss/beta_l - 1;
% i_l_log_p = (i_l_p + log(i_l_level_ss));
% i_l_level_p = 400*exp(i_l_log_p);
% i_l_log_l = (i_d_l + log(i_l_level_ss));
% i_l_level_l = 400*exp(i_l_log_l);
% 
% pi_level_ss = Pi_ss;
% pi_log_p = (pi_p + log(pi_level_ss));
% pi_level_p = 400*(exp(pi_log_p)-1);
% pi_log_l = (pi_l + log(pi_level_ss));
% pi_level_l = 400*(exp(pi_log_l)-1);
% 
% xi_level_ss = 1;
% xi_log_p = (xi_p + log(xi_level_ss));
% xi_level_p = exp(xi_p);
% xi_log_l = (xi_l + log(xi_level_ss));
% xi_level_l = exp(xi_l);


i_r_level_ss = Pi_ss/beta_d - 1;
i_r_level_p = 400*(i_r_p + i_r_level_ss);
i_r_level_l = 400*(i_r_l + i_r_level_ss);

i_d_level_ss = Pi_ss/beta_d - 1;
i_d_level_p = 400*(i_d_p + i_d_level_ss);
i_d_level_l = 400*(i_d_l + i_d_level_ss);

i_l_level_ss = Pi_ss/beta_l - 1;
i_l_level_p = 400*(i_l_p + i_l_level_ss);
i_l_level_l = 400*(i_l_l + i_l_level_ss);

xi_level_ss = 1;
xi_level_p = (xi_p + xi_level_ss);
xi_level_l = (xi_l + xi_level_ss);

pi_level_ss = Pi_ss;
pi_level_p = (pi_p + Pi_ss);
pi_level_l = (pi_l + Pi_ss);

line1=[i_r_level_l, i_d_level_l, i_l_level_l, 100*Y_l, 400*(pi_level_l-1), xi_level_l];
%line2=[i_r_level_p, i_d_level_p, i_l_level_p, 100*Y_p, 400*(pi_level_p-1), xi_level_p];

makechart(titlelist,legendlist,figtitle,ylabels,line1);
