clear all;
close all;
clc;

load('SOE.mat');


% % Numero de periodos
T = 20;
% % Numero de filas en el grafico
f = 5;
% % Numero de columnas en el grafico
c = 4;
% % Paises a presentar

% Variable
variables=  {'y','c','inve', 'xHstar','m', 'pinf', 'pinfh', 'pinfF','w','r', 'lab','u','labstar',     'piS',      'ygap','winf'}; %'xi',
% Nombre Variable (aparece en el titulo)
nombres  =  {     'PIB',  'Consumo',    'Inversión', 'Exportaciones','Importaciones','Inflación', 'Inflación no Transable','Inflación Transable', 'Salario Real', 'Tasa de Interés', 'Empleo', 'Desempleo', 'Población Activa','Depreciación',  'Brecha de Producto','Inflación Salarial'}; % 'Riesgo País',  


% % % Choques
shocks     = {'a','b','qs','g','pinf','w','pinfM','zeta','y_star','Rstar','ls',  'pFstar','m'}; %'zeta2','pistar',
shocks_oferta     = {'a','pinf','pinfM',  'pFstar','zeta','Rstar','y_star'};%,'pistar'
shocks_demanda     = {'b','qs','g','m'}; %'zeta2',
shocks_lab     = {'w','ls'}; 

% % % Nombre Choque (aparece en la figura)
name_cho   = {'Productividad','Fricciones Financieras','Inversión','Gasto público','Mark up precios','Mark-up Salarios','Mark up importados','riesgo país','\zeta_2','Demanda Externa','Inflación externa','Tasa de interés f','Oferta laboral','Precios importados' ,'Política monetaria'};  
name_cho_oferta   = {'Productividad','Mark up precios','Mark up importados','Precio Importados','Riesgo País','R^f','PIB Externo'};  %,'Inflación Externa'
plot_options.v_adj      = ones(1,size(variables,2)+1);
t = 1:1:T;
name_cho_demanda= {'Fric. Financieras','Inve','G.Público','Monetario'};  %,'\zeta_2'
name_cho_lab= {'Mark up salarios','Shock a la oferta de trabajo'};  
%% 

% % % choques de oferta
figure('Name',['Shocks de Oferta'],'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'WindowStyle','docked');
    for kk=1:length(variables)
        if isequal(abs(oo_.steady_state(loc(M_.endo_names,variables{kk}))),0)==1
        div_ss=1;
        else
        div_ss=abs(oo_.steady_state(loc(M_.endo_names,variables{kk})));
        end
        subplot(f,c,kk);
        a=plot_options.v_adj(kk)/div_ss;
        eval(['xx = plot([a*transpose(oo_','.irfs.',variables{kk},'_e',shocks_oferta{1},')',' -a*transpose(oo_','.irfs.',variables{kk},'_e',shocks_oferta{2},')',' -a*transpose(oo_','.irfs.',variables{kk},'_e',shocks_oferta{3},')',' -a*transpose(oo_','.irfs.',variables{kk},'_e',shocks_oferta{4},')',' -a*transpose(oo_','.irfs.',variables{kk},'_e',shocks_oferta{5},')',' -a*transpose(oo_','.irfs.',variables{kk},'_e',shocks_oferta{6},')',' a*transpose(oo_','.irfs.',variables{kk},'_e',shocks_oferta{7},')]);'])%',' a*transpose(oo_','.irfs.',variables{kk},'_e',shocks_oferta{5},')
        set(xx(1),'Color','k','LineStyle','-' ,'LineWidth',1);
        set(xx(2),'Color','k','LineStyle','-','LineWidth',1,'Marker','x','MarkerSize',4);
        set(xx(3),'Color','k','LineStyle',':','LineWidth',2);
        set(xx(4),'Color','k','LineStyle','-','LineWidth',1,'Marker','o','MarkerSize',2,'MarkerEdgeColor','black','MarkerFaceColor',[0 0 0]);
        set(xx(5),'Color','k','LineStyle','-.' ,'LineWidth',1);
        set(xx(6),'Color','k','LineStyle','-','LineWidth',1,'Marker','+','MarkerSize',4);
        set(xx(7),'Color','k','LineStyle','-','LineWidth',1,'Marker','s','MarkerSize',2,'MarkerEdgeColor','black','MarkerFaceColor',[0 0 0]);
       % set(xx(7),'Color','k','LineStyle','-','LineWidth',1,'Marker','*','MarkerSize',4);
        pbaspect auto
        hold on;
        %plot(zeros(T,1),'k','Linewidth',1,'LineStyle','-')
        title([nombres{kk}],'FontSize',8,'FontWeight','bold')
        ylabel('p.b','FontSize',8);dir
        set(gca,'Fontsize',8);
        xlim([1 T]);
        grid on;
     set(gcf, 'PaperType', 'A4')
    set(gcf, 'PaperUnits', 'normalized');
   
% 	set(gcf, 'PaperUnits', 'inches');
%     set(gcf, 'PaperSize', [8.5 8.5]);
    set(gcf, 'PaperPositionMode', 'manual');
    set(gcf,'units','normalized', 'PaperPosition', [0 0 1 1]);
    set(gcf, 'renderer', 'painters');
    lege  = legend(name_cho_oferta{1},name_cho_oferta{2},name_cho_oferta{3},name_cho_oferta{4},name_cho_oferta{5},name_cho_oferta{6},name_cho_oferta{7}); %,name_cho_oferta{5}
   set(lege,'Fontsize',8,'Position',[0.35 0.15 0.3 0.03]);

  
 %print(gcf,'-djpeg', 'Shocks de Demanda1.jpeg','-r0');
  print(gcf,'-dpdf', 'adOferta.pdf','-r0');
    end
%% 
% % Choques de dda
figure('Name',['Shocks de Demanda'],'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'WindowStyle','docked');
    for kk=1:length(variables)
        if isequal(abs(oo_.steady_state(loc(M_.endo_names,variables{kk}))),0)==1
        div_ss=1;
        else
        div_ss=abs(oo_.steady_state(loc(M_.endo_names,variables{kk})));
        end
        subplot(f,c,kk);
        a=plot_options.v_adj(kk)/div_ss;
        eval(['xx = plot([a*transpose(oo_','.irfs.',variables{kk},'_e',shocks_demanda{1},')',' a*transpose(oo_','.irfs.',variables{kk},'_e',shocks_demanda{2},')',' a*transpose(oo_','.irfs.',variables{kk},'_e',shocks_demanda{3},')',' -a*transpose(oo_','.irfs.',variables{kk},'_e',shocks_demanda{4},')]);'])%',' -a*transpose(oo_','.irfs.',variables{kk},'_e',shocks_demanda{8},')',' a*transpose(oo_','.irfs.',variables{kk},'_e',shocks_demanda{5},')
        set(xx(1),'Color','k','LineStyle','-' ,'LineWidth',1);
        set(xx(2),'Color','k','LineStyle','-','LineWidth',1,'Marker','x','MarkerSize',4);
        set(xx(3),'Color','k','LineStyle',':','LineWidth',2);
        set(xx(4),'Color','k','LineStyle','-','LineWidth',1,'Marker','o','MarkerSize',2,'MarkerEdgeColor','black','MarkerFaceColor',[0 0 0]);
        %set(xx(5),'Color','k','LineStyle','-.' ,'LineWidth',1);
        pbaspect auto
        hold on;
        %plot(zeros(T,1),'k','Linewidth',1,'LineStyle','-')
        title([nombres{kk}],'FontSize',8,'FontWeight','bold')
        ylabel('p.b','FontSize',8);dir
        set(gca,'Fontsize',8);
        xlim([1 T]);
        grid on;
 set(gcf, 'PaperType', 'A4')
    set(gcf, 'PaperUnits', 'normalized');
   
% 	set(gcf, 'PaperUnits', 'inches');
%     set(gcf, 'PaperSize', [8.5 8.5]);
    set(gcf, 'PaperPositionMode', 'manual');
    set(gcf,'units','normalized', 'PaperPosition', [0 0 1 1]);
    set(gcf, 'renderer', 'painters');
    lege  = legend(name_cho_demanda{1},name_cho_demanda{2},name_cho_demanda{3},name_cho_demanda{4}); %,name_cho_demanda{8},name_cho_demanda{5}
   set(lege,'Fontsize',8,'Orientation','Horizontal','Position',[0.35 0.2 0.3 0.03]);

  
 %print(gcf,'-djpeg', 'Shocks de Demanda1.jpeg','-r0');
  print(gcf,'-dpdf', 'adDemanda.pdf','-r0');
    end
%% 
% % % choques laborales
figure('Name',['Shocks en el mercado laboral'],'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'WindowStyle','docked');
    for kk=1:length(variables)
        if isequal(abs(oo_.steady_state(loc(M_.endo_names,variables{kk}))),0)==1
        div_ss=1;
        else
        div_ss=abs(oo_.steady_state(loc(M_.endo_names,variables{kk})));
        end
        subplot(f,c,kk);
        a=plot_options.v_adj(kk)/div_ss;
        eval(['xx = plot([a*transpose(oo_','.irfs.',variables{kk},'_e',shocks_lab{1},')',' a*transpose(oo_','.irfs.',variables{kk},'_e',shocks_lab{2},')]);'])
        set(xx(1),'Color','k','LineStyle','-' ,'LineWidth',2);
        set(xx(2),'Color','k','LineStyle','--','LineWidth',1);
        pbaspect auto
        hold on;
        %plot(zeros(T,1),'k','Linewidth',1,'LineStyle','-')
        title([nombres{kk}],'FontSize',14,'FontWeight','bold')
        ylabel('p.b','FontSize',8);dir
        set(gca,'Fontsize',8);
        xlim([1 T]);
        grid on;
    set(gcf, 'PaperType', 'A4')
    set(gcf, 'PaperUnits', 'normalized');
   
% 	set(gcf, 'PaperUnits', 'inches');
%     set(gcf, 'PaperSize', [8.5 8.5]);
    set(gcf, 'PaperPositionMode', 'manual');
    set(gcf,'units','normalized', 'PaperPosition', [0 0 1 1]);
    set(gcf, 'renderer', 'painters');
    lege  = legend(name_cho_lab{1},name_cho_lab{2}); 
    set(lege,'Fontsize',8,'Orientation','Horizontal','Position',[0.35 0.2 0.3 0.03]);

  
 %print(gcf,'-djpeg', 'Shocks de Demanda1.jpeg','-r0');
  print(gcf,'-dpdf', 'adMLaboral.pdf','-r0');
    end
% MATLAB function: loc.m                        July 22, 1992
%        loc(mstring,'sstring') returns the number of the row of
%        mstring that has the same non-blanck characters as
%        sstring. mstring is a matrix of characters. Each of its
%        rows corresponds to a "name". sstring is a character
%        string. It is the "name" we are looking for in mstring.
%        Note that sstring must be placed in between single
%        quotation marks.

function [x] = loc(mstring,sstring,switchmod)

[rm,cm]=size(mstring);
cs=max(size(sstring));

% If necessary, add blanck columns to sstring so it will have the
%  same number of columns as mstring.
if cm>cs;
    nblancks=cm-cs;
    for i=1:nblancks
        sstring=[sstring,' '];
    end
end

if(cm~=max(size(sstring)))
    disp(['problem with padding ',sstring])
    disp('The character string might be longer than name list')
    mstring
    %return
    pause
end

x=[];
for r=1:rm;
    if(length(find(mstring(r,:)==sstring))==cm)
        x=r;
    end
end

if(x==0)
    if(~exist('switchmod')); disp(['Could not find ',sstring]); 
end
end
end

