% control.m
% controls Dynare's executions of the JEDC bank capital paper

% among other things, it allows more flexibility when plotting the results
% of impulse response experiments once a "stochsimul" call to dynare has been done. 


impulses = 1;  % 1 if you are interested in displaying impulse responses
               % 2 for unconditinal moments of simulated data



% provides info on the placement of variables of interest to help constructing graphs 
iY = 5;     % aggregate output
iI = 6;     % aggregate investment
iq  = 10;   % relative price of capital goods
iCA = 19;   % Bank-Capital Asset Ratio
ibigA = 8;  % net worth of banking sector (bank capital)
iTL = 21;   % Aggregage Bank Lending
ibigN = 7;  % Aggregage Entrepreneurial Net Worth
iRd = 16;   % Interest Rate
iinfl = 12; % Inflation Rate
iH = 4;     % aggregate hours worked
ich = 1;    % household consumption
ilogy = 43;
ilogI = 44;


if impulses == 1;

% Specify the number of periods for which the impulse response will be
% reported
nir = 20;
dynare BankCapital noclearall

% go retreive the impulse responses generated by dynare
irfFields = fieldnames(oo_.irfs);
[nomvar ncols] = size(irfFields)
myss = oo_.steady_state;
nx = 3;
nvar = nomvar/nx;


% set up the matrix in which I will store the responses for the graphs here
IRB = zeros(nir,nvar);

% select the shock I want to highlight
ix = menu('What shock is graphed?','Technology','Monetary Policy','Bank Capital');
isign = menu('Increase or Decrease in the Shock?','Decrease','Increase');
isign = 2*isign-3;

% rewrite the responses in deviations from s.s. rather than in absolute
% numbers
for i = 1:nvar
temp = char(irfFields(i+nvar*(ix-1)));
mytemp = getfield(oo_.irfs,temp);
mytemp = isign*100*(mytemp')./myss(i);
IRB(:,i) =  mytemp(1:nir,:);

end

answ2 = menu('What is being computed?','Benchmark Case','Second Case');
if answ2 == 1;
   IR = IRB;
end;


[nperiods cols] = size(IRB);
TT = nperiods;
dt = (1:TT)';


% for interest rates and inflation rates, might be useful to report
% responses measured as net, annualized, percentage interest rates

netr = zeros(TT,1);
netrB = netr; 
infl = zeros(TT,1);
inflB = infl;

Rd_ss = pi_ss/bet;
for i = 1:TT
   netr(i) = 100*(((IR(i,iRd)/100+1.0)*Rd_ss)^4-1);
   netrB(i) = 100*(((IRB(i,iRd)/100+1.0)*Rd_ss)^4-1);
   infl(i) = 100*(((IR(i,iinfl)/100+1.0)*pi_ss)^4-1.0);
   inflB(i) = 100*(((IRB(i,iinfl)/100+1.0)*pi_ss)^4-1.0);
end;





% setting up printing parameters
myylabel = 'Deviation from s.s. (%)';
myylabel2 = 'Percentage points';
myylabelshort = 'Deviation from s.s.';
mysize = 11;
mysize2 = 9;
mysize3 = 9;
myxlabel = 'Quarters';

epais = 2.0;

trait1 = 'k-';
trait2 = 'k--';

fmt = 5.4;

legende1 = 'Bank Capital Channel';
legende2 = ' No Bank Capital Channel';



figure(1)



subplot(331),plot(dt,IR(1:TT,iY),trait1,dt,IRB(1:TT,iY),trait2,'LineWidth',epais),
%axis([0 20 -0.6 0.2])
%axis([0 40 -2.0 0])
title('Output','FontWeight','bold','FontSize',mysize)
xlabel(myxlabel,'FontSize',mysize3,'FontWeight','bold')
ylabel(myylabel,'FontWeight','bold','FontSize',mysize2)
set(gca,'FontSize',mysize);
xt = get(gca, 'YTick');
xt1 = fortick(xt,fmt);
set(gca, 'YTickLabel', xt1, 'YTick', xt);

subplot(332),plot(dt,IR(1:TT,iI),trait1,dt,IRB(1:TT,iI),trait2,'LineWidth',epais),
%axis([0 20 -2 1])
%axis([0 40 -10 0])
title('Investment','FontWeight','bold','FontSize',mysize)
xlabel(myxlabel,'FontSize',mysize3,'FontWeight','bold')
ylabel(myylabel,'FontWeight','bold','FontSize',mysize2)
legend(legende1,legende2)
set(gca,'FontSize',mysize);
xt = get(gca, 'YTick');
xt1 = fortick(xt,fmt);
set(gca, 'YTickLabel', xt1, 'YTick', xt);


subplot(333),plot(dt,IR(1:TT,iq),trait1,dt,IRB(1:TT,iq),trait2,'LineWidth',epais),
%axis([0 20 -2 1])
%axis([0 40 -0.4 0.4])
title('Price of Capital','FontWeight','bold','FontSize',mysize)
xlabel(myxlabel,'FontSize',mysize3,'FontWeight','bold')
ylabel(myylabel,'FontWeight','bold','FontSize',mysize2)
%legend(legende1,legende2)
set(gca,'FontSize',mysize);
xt = get(gca, 'YTick');
xt1 = fortick(xt,fmt);
set(gca, 'YTickLabel', xt1, 'YTick', xt);


%subplot(334),plot(dt,IR(1:TT,iCA),trait1,'LineWidth',epais),
subplot(334),plot(dt,IR(1:TT,iCA),trait1,dt,IRB(1:TT,iCA),trait2,'LineWidth',epais),
%axis([0 20 -1 2])
%axis([0 40 -2 4])
xlabel(myxlabel,'FontSize',mysize3,'FontWeight','bold')
ylabel(myylabel,'FontWeight','bold','FontSize',mysize2)
%legend(legende1,legende2)
title('Bank Capital-Asset Ratio','FontWeight','bold','FontSize',mysize)
set(gca,'FontSize',mysize);
xt = get(gca, 'YTick');
xt1 = fortick(xt,fmt);
set(gca, 'YTickLabel', xt1, 'YTick', xt);


%subplot(335),plot(dt,IR(1:TT,ibigA),trait1,'LineWidth',epais),
subplot(335),plot(dt,IR(1:TT,ibigA),trait1,dt,IRB(1:TT,ibigA),trait2,'LineWidth',epais),
%axis([0 20 -2 1])
%axis([0 40 -10 1])
xlabel(myxlabel,'FontSize',mysize3,'FontWeight','bold')
ylabel(myylabel,'FontWeight','bold','FontSize',mysize2)
%legend(legende1,legende2)
title('Bank Net Worth','FontWeight','bold','FontSize',mysize)
set(gca,'FontSize',mysize);
xt = get(gca, 'YTick');
xt1 = fortick(xt,fmt);
set(gca, 'YTickLabel', xt1, 'YTick', xt);


subplot(336),plot(dt,IR(1:TT,iTL),trait1,dt,IRB(1:TT,iTL),trait2,'LineWidth',epais),
%axis([0 20 -2 1])
%axis([0 40 -10 1])
xlabel(myxlabel,'FontSize',mysize3,'FontWeight','bold')
ylabel(myylabel,'FontWeight','bold','FontSize',mysize2)
%legend(legende1,legende2)
title('Bank Lending','FontWeight','bold','FontSize',mysize)
set(gca,'FontSize',mysize);
xt = get(gca, 'YTick');
xt1 = fortick(xt,fmt);
set(gca, 'YTickLabel', xt1, 'YTick', xt);

subplot(337),plot(dt,IR(1:TT,ibigN),trait1,dt,IRB(1:TT,ibigN),trait2,'LineWidth',epais),
%axis([0 20 -2 1])
%axis([0 40 -10 1])
xlabel(myxlabel,'FontSize',mysize3,'FontWeight','bold')
ylabel(myylabel,'FontWeight','bold','FontSize',mysize2)
%legend(legende1,legende2)
title('Entrepreneurial Net Worth','FontWeight','bold','FontSize',mysize)
set(gca,'FontSize',mysize);
xt = get(gca, 'YTick');
xt1 = fortick(xt,fmt);
set(gca, 'YTickLabel', xt1, 'YTick', xt);

%subplot(338),plot(dt,IR(1:TT,iRd),trait1,dt,IRB(1:TT,iRd),trait2,'LineWidth',epais),
subplot(338),plot(dt,netr,trait1,dt,netrB,trait2,'LineWidth',epais),
%axis([0 20 -2 1])
%axis([0 40 -10 1])
xlabel(myxlabel,'FontSize',mysize3,'FontWeight','bold')
ylabel(myylabel2,'FontWeight','bold','FontSize',mysize2)
%legend(legende1,legende2)
title('Short Term Rate','FontWeight','bold','FontSize',mysize)
set(gca,'FontSize',mysize);
xt = get(gca, 'YTick');
xt1 = fortick(xt,fmt);
set(gca, 'YTickLabel', xt1, 'YTick', xt);

%subplot(339),plot(dt,IR(1:TT,iinfl),trait1,dt,IRB(1:TT,iinfl),trait2,'LineWidth',epais),
subplot(339),plot(dt,infl,trait1,dt,inflB,trait2,'LineWidth',epais),
%axis([0 20 -2 1])
%axis([0 40 -10 1])
xlabel(myxlabel,'FontSize',mysize3,'FontWeight','bold')
ylabel(myylabel2,'FontWeight','bold','FontSize',mysize2)
%legend(legende1,legende2)
title('Inflation','FontWeight','bold','FontSize',mysize)
set(gca,'FontSize',mysize);
xt = get(gca, 'YTick');
xt1 = fortick(xt,fmt);
set(gca, 'YTickLabel', xt1, 'YTick', xt);

if(0)
temp1 = [dt IR(1:TT,iY) IR(1:TT,iI) IR(1:TT,iq) IR(1:TT,iCA) IR(1:TT,ibigA) IR(1:TT,iTL) IR(1:TT,ibigN) IR(1:TT,iRd) IR(1:TT,iinfl)];
temp2 = [dt IRB(1:TT,iY) IRB(1:TT,iI) IRB(1:TT,iq) IRB(1:TT,iCA) IRB(1:TT,ibigA) IRB(1:TT,iTL) IR(1:TT,ibigN) IRB(1:TT,iRd) IRB(1:TT,iinfl)];
%temp2 = [dt IRB(1:TT,iY) IRB(1:TT,iI) IRB(1:TT,iq) zeros(nir,1) zeros(nir,1) IRB(1:TT,iTL) IR(1:TT,ibigN) IRB(1:TT,iRd) IRB(1:TT,iinfl)];
disp(temp1)
disp(temp2)
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


else
    
    
    %Display unconditional moments of simulated data
    
    
dynare BankCapital noclearall


choose = [iCA iI iY iTL iH ich iinfl ilogy ilogI];

mystring = ['CA Ratio            '
            'Investment          '
            'GDP                 '
            'Bank Lending        '
            'Hours (aggregate)   '
            'HH Consumption      '
            'Inflation           '
            ' Log of Y           '
            ' Log of I           ']; 
        
temp = oo_.mean;
temp1 = sqrt(diag(oo_.var));
temp2 = temp1./temp1(ilogy);

table1  = [ temp(choose) temp1(choose) temp2(choose)];
    
disp('   ')
disp('   ')
disp('          Table 1: Moments of Variables over Simulations ')
disp('*********************************************************************')
disp(['variable (X)    ','  Average(X) ', ' std(X)' ,  '  Std(X)/std(GDP)  ' ]);
disp('*********************************************************************')
disp([mystring num2str(table1,'%11.6f')])

end