Commit 87a405dc authored by Marco Ratto's avatar Marco Ratto
Browse files

add utillities for foreast, irf, plot_irf and squeeze SD into occbin, and...

add utillities for foreast, irf, plot_irf and squeeze SD into occbin, and their relevant options [this commit must be better split before making a PR on gitlab]
parent 94d2b92c
function OccBinIRF(shockname,vars_irf,length_irf,shocksign,axes_years,irflocation_lin,irflocation_piece)
global oo_ M_ options_
if nargin <3
length_irf = 30;
end
if nargin <4
shocksign = '1';
end
if nargin <5
axes_years = 0;
steps_irf = 1;
end
if nargin <6
irflocation_lin = 'oo_.linear_irfs.';
end
if nargin <7
irflocation_piece = 'oo_.occbin_irfs.';
end
if axes_years == 1
steps_irf = 4;
end
idxshocks = find(ismember(M_.exo_names, shockname));
ncols = 3;
nrows = 3;
for j=idxshocks'
shockname = M_.exo_names{j};
j1 = 0;
isub = 0;
ifig = 0;
% Variables
% ----------------------
for i = 1:length(vars_irf)
j1=j1+1;
if mod(j1,9)==1,
hfig = dyn_figure(options_,'name','OccbinIRFs');
ifig=ifig+1;
isub=0;
end
isub=isub+1;
subplot(nrows,ncols,isub)
irfvalues = eval([irflocation_lin,vars_irf{i,1},'_',shockname,shocksign]);
plot(irfvalues(1:steps_irf:length_irf),'linewidth',2)
hold on
irfvalues = eval([irflocation_piece,vars_irf{i,1},'_',shockname,shocksign]);
plot(irfvalues(1:steps_irf:length_irf),'r--','linewidth',2)
if axes_years == 1
xlabel('Years')
else
xlabel('Quarters')
end
xlim([1 (length_irf/steps_irf)]);
title(vars_irf{i,2})
% Annotation Box
% ----------------------
if mod(j1,9)==0,
annotation('textbox', [0.1,0,0.35,0.05],'String', 'Linear','Color','Blue','horizontalalignment','center','interpreter','none');
annotation('textbox', [0.55,0,0.35,0.05],'String', 'Piecewise','Color','Red','horizontalalignment','center','interpreter','none');
dyn_saveas(gcf,[M_.fname,'_irf_occbin','_',shockname,shocksign,'_',int2str(ifig)],options_);
end
if mod(j1,9)~=0 && i==length(vars_irf),
annotation('textbox', [0.1,0,0.35,0.05],'String', 'Linear','Color','Blue','horizontalalignment','center','interpreter','none');
annotation('textbox', [0.55,0,0.35,0.05],'String', 'Piecewise','Color','Red','horizontalalignment','center','interpreter','none');
dyn_saveas(hfig,[M_.fname,'_','_irf_occbin','_',shockname,shocksign,'_',int2str(ifig)],options_);
end
end
end
end
\ No newline at end of file
function PlotOccBinSimul(zlin,zpiece,vars_irf,options,M,zlin2,zpiece2)
%% Options and defaults
if isfield(options.occbin,'plot_simul')
% opts_plot = options.occbin.plot_simul;
% assign_options(opts_plot)
else
options = set_default_occbin_options(options,M,'plot_simul');
end
opts_plot = options.occbin.plot_simul;
if opts_plot.length_simul>0
length_simul = opts_plot.length_simul;
else
length_simul = size(zpiece,1);
end
%
% if isfield(opts_plot,'simulname')
simulname = opts_plot.simulname;
% else
% simulname = 'occbin_simul';
% end
% if ~isfield(opts_plot,'use_grid')
% opts_plot.use_grid = 1;
% end
if isfield(opts_plot,'my_dir')
my_dir = opts_plot.my_dir;
if ~exist(my_dir, 'dir')
mkdir(my_dir)
end
else
my_dir = pwd;
end
% if isfield(opts_plot,'labels')
my_legend = opts_plot.labels;
% else
% my_legend ={'Linear','Piecewise','Simulation 3','Simulation 4'};
% end
% if ~isfield(opts_plot,'ncols')
% opts_plot.ncols = 3;
% end
%
% if ~isfield(opts_plot,'nrows')
% opts_plot.nrows = 3;
% end
if isfield(opts_plot,'timeaxis')
periods = opts_plot.timeaxis;
else
periods = 1:length_simul;
end
if ~isfield(opts_plot,'timeaxis_steps')
opts_plot.timeaxis_steps = 4;
end
if isfield(opts_plot,'figure_size')
figure_size = opts_plot.figure_size;
end
if ~isfield(opts_plot,'add_zero_line')
opts_plot.add_zero_line=0;
end
if isfield(opts_plot,'linewidth')
lw = opts_plot.linewidth;
else
lw = 2;
end
if isfield(opts_plot,'cutoff')
cutoff = opts_plot.cutoff;
else
cutoff = 0;
end
% if isfield(opts_plot,'threshold')
threshold = opts_plot.threshold;
% else
% threshold = 10^-6;
% end
if isfield(opts_plot,'mystst_simul')
mystst_simul = opts_plot.mystst_simul;
mystst_simul_pos = opts_plot.mystst_simul_pos;
add_absval_graph = 1;
else
add_absval_graph = 0;
end
if ~isfield(opts_plot,'log_normalize_graph')
opts_plot.log_normalize_graph=0;
end
% opts_plot = set_default_option(opts_plot,'add_steadystate',0);
add_steadystate = opts_plot.add_steadystate;
current_directory = pwd;
%% Plotting
% Legend
newPosition = [0.5 0.01 0.00 0.015];
newUnits = 'normalized';
% Plotting Setup
% ----------------------
npanel = opts_plot.ncols*opts_plot.nrows;
j1 = 0;
isub = 0;
ifig = 0;
% Variables
% ----------------------
for i = 1:size(vars_irf,1)
j1=j1+1;
if mod(j1,npanel)==1
gcf = dyn_figure(options.nodisplay,'name','OccbinSimul','PaperPositionMode', 'auto','PaperType','A4','PaperOrientation','portrait','renderermode','auto','position',figure_size);
ifig=ifig+1;
isub=0;
end
isub=isub+1;
% Plot
%subplot(opts_plot.nrows,opts_plot.ncols,isub)
subtightplot(opts_plot.nrows,opts_plot.ncols,isub,opts_plot.subplot_gap,opts_plot.marg_h)
idxvar = strmatch(vars_irf{i,1},M.endo_names,'exact');
if size(vars_irf,2) > 2
exofactor = vars_irf{i,3};
else
exofactor = 1;
end
% Steady state
if add_steadystate || (size(vars_irf,2) > 3 && vars_irf{i,4} == 1)
stst = get_mean(vars_irf{i,1});
else
stst = 0;
end
values = [zlin(1:length_simul,idxvar) zpiece(1:length_simul,idxvar)];
if nargin > 5
values = [values zlin2(1:length_simul,idxvar)];
end
if nargin > 6
values = [values zpiece2(1:length_simul,idxvar)];
end
values = values+stst;
% if (add_absval_graph && i == mystst_simul_pos)
if (add_absval_graph && ismember(i,mystst_simul_pos))
for k = 1:size(values,2)
values(:,k) = mystst_simul{k}(1:length_simul,idxvar);
end
end
values = values*exofactor;
values(abs(values) <threshold) = 0;
if ismember(i,opts_plot.log_normalize_graph)
values = exp(values)/exp(values(opts_plot.normalization_point))*100;
end
if opts_plot.add_zero_line
hold on
plot(values*0,'linewidth',1,'linestyle','-','color','k','HandleVisibility','off');
end
hplot = plot(values,'linewidth',lw);
for k = 1:size(values,2)
set(hplot(k),'linestyle',opts_plot.mystyles{k},'color',opts_plot.mycolors(k,:));
end
% Axes and Title
if isfield(opts_plot,'xlabel')
xlabel(opts_plot.xlabel)
end
if opts_plot.timeaxis
first_year = find(periods == round(periods),1);
xticks([first_year:opts_plot.timeaxis_steps:length_simul])
xticklabels({periods(first_year:opts_plot.timeaxis_steps:length_simul)})
xtickangle(45)
end
xlim([1 (length_simul-cutoff)]);
if size(vars_irf,2) > 2
title(vars_irf{i,2})
else
title(regexprep(vars_irf{i,1},'_',' '))
end
if size(vars_irf,2) > 4
ylabel(vars_irf{i,5})
end
if opts_plot.use_grid
grid on
end
% Annotation Box
% ----------------------
if mod(j1,npanel)==0
hL = legend(my_legend);
set(hL,'Position', newPosition,'Units', newUnits,'Orientation','horizontal','FontSize',10);
outname = [my_dir,filesep,M.fname,'_',simulname,int2str(ifig)];
if opts_plot.print_emf
print(gcf,outname,'-dmeta')
end
dyn_saveas(gcf,outname,options.nodisplay,options.graph_format);
end
if mod(j1,npanel)~=0 && i==size(vars_irf,1)
hL = legend(my_legend);
set(hL,'Position', newPosition,'Units', newUnits,'Orientation','horizontal','FontSize',10);
%set(gcf, 'Position', get(0, 'Screensize'));
outname = [my_dir,filesep,M.fname,'_',simulname,int2str(ifig)];
if opts_plot.print_emf
print(gcf,outname,'-dmeta')
end
dyn_saveas(gcf,outname,options.nodisplay,options.graph_format);
end
end
cd(current_directory)
end
function h=subtightplot(m,n,p,gap,marg_h,marg_w,varargin)
%function h=subtightplot(m,n,p,gap,marg_h,marg_w,varargin)
%
% Functional purpose: A wrapper function for Matlab function subplot. Adds the ability to define the gap between
% neighbouring subplots. Unfotrtunately Matlab subplot function lacks this functionality, and the gap between
% subplots can reach 40% of figure area, which is pretty lavish.
%
% Input arguments (defaults exist):
% gap- two elements vector [vertical,horizontal] defining the gap between neighbouring axes. Default value
% is 0.01. Note this vale will cause titles legends and labels to collide with the subplots, while presenting
% relatively large axis.
% marg_h margins in height in normalized units (0...1)
% or [lower uppper] for different lower and upper margins
% marg_w margins in width in normalized units (0...1)
% or [left right] for different left and right margins
%
% Output arguments: same as subplot- none, or axes handle according to function call.
%
% Issues & Comments: Note that if additional elements are used in order to be passed to subplot, gap parameter must
% be defined. For default gap value use empty element- [].
%
% Usage example: h=subtightplot((2,3,1:2,[0.5,0.2])
if (nargin<4) || isempty(gap), gap=0.01; end
if (nargin<5) || isempty(marg_h), marg_h=0.05; end
if (nargin<6) || isempty(marg_w), marg_w=marg_h; end
if isscalar(gap), gap(2)=gap; end
if isscalar(marg_h), marg_h(2)=marg_h; end
if isscalar(marg_w), marg_w(2)=marg_w; end
gap_vert = gap(1);
gap_horz = gap(2);
marg_lower = marg_h(1);
marg_upper = marg_h(2);
marg_left = marg_w(1);
marg_right = marg_w(2);
%note n and m are switched as Matlab indexing is column-wise, while subplot indexing is row-wise :(
[subplot_col,subplot_row]=ind2sub([n,m],p);
% note subplot suppors vector p inputs- so a merged subplot of higher dimentions will be created
subplot_cols=1+max(subplot_col)-min(subplot_col); % number of column elements in merged subplot
subplot_rows=1+max(subplot_row)-min(subplot_row); % number of row elements in merged subplot
% single subplot dimensions:
%height=(1-(m+1)*gap_vert)/m;
%axh = (1-sum(marg_h)-(Nh-1)*gap(1))/Nh;
height=(1-(marg_lower+marg_upper)-(m-1)*gap_vert)/m;
%width =(1-(n+1)*gap_horz)/n;
%axw = (1-sum(marg_w)-(Nw-1)*gap(2))/Nw;
width =(1-(marg_left+marg_right)-(n-1)*gap_horz)/n;
% merged subplot dimensions:
merged_height=subplot_rows*( height+gap_vert )- gap_vert;
merged_width= subplot_cols*( width +gap_horz )- gap_horz;
% merged subplot position:
merged_bottom=(m-max(subplot_row))*(height+gap_vert) +marg_lower;
merged_left=(min(subplot_col)-1)*(width+gap_horz) +marg_left;
pos_vec=[merged_left merged_bottom merged_width merged_height];
% h_subplot=subplot(m,n,p,varargin{:},'Position',pos_vec);
% Above line doesn't work as subplot tends to ignore 'position' when same mnp is utilized
h=subplot('Position',pos_vec,varargin{:});
if (nargout < 1), clear h; end
end
function PlotOccBinSimulS(my_simuls,vars_irf,options,M)
%% Options and defaults
if ~isfield(options.occbin,'plot_simul')
options = set_default_occbin_options(options,M,'plot_simul');
end
opts_plot = options.occbin.plot_simul;
if opts_plot.length_simul>0
length_simul = opts_plot.length_simul;
else
fieldn = fieldnames(my_simuls);
length_simul = max(100,size(my_simuls.(fieldn{1}),1));
end
simulname = opts_plot.simulname;
if isfield(opts_plot,'my_dir')
my_dir = opts_plot.my_dir;
if ~exist(my_dir, 'dir')
mkdir(my_dir)
end
else
my_dir = pwd;
end
my_legend = opts_plot.labels;
if isfield(opts_plot,'timeaxis')
periods = opts_plot.timeaxis;
else
periods = 1:length_simul;
end
if ~isfield(opts_plot,'timeaxis_steps')
opts_plot.timeaxis_steps = 4;
end
if isfield(opts_plot,'figure_size')
figure_size = opts_plot.figure_size;
end
if ~isfield(opts_plot,'add_zero_line')
opts_plot.add_zero_line=0;
end
if ~isfield(opts_plot,'add_vertical_line')
opts_plot.add_vertical_line=0;
end
if ~isfield(opts_plot,'scale_y')
opts_plot.scale_y=0;
end
if isfield(opts_plot,'linewidth')
lw = opts_plot.linewidth;
else
lw = 2;
end
if isfield(opts_plot,'cutoff')
cutoff = opts_plot.cutoff;
else
cutoff = 0;
end
if ~isfield(opts_plot,'start_plot')
opts_plot.start_plot = 1;
end
threshold = opts_plot.threshold;
if isfield(opts_plot,'mystst_simul')
mystst_simul = opts_plot.mystst_simul;
mystst_simul_pos = opts_plot.mystst_simul_pos;
add_absval_graph = 1;
else
add_absval_graph = 0;
end
if ~isfield(opts_plot,'log_normalize_graph')
opts_plot.log_normalize_graph=0;
end
if isfield(opts_plot,'legend_position')
newPosition = opts_plot.legend_position;
else
newPosition = [0.5 0.01 0.00 0.015];
end
newUnits = 'normalized';
add_steadystate = opts_plot.add_steadystate;
%current_directory = pwd;
%% Plotting
% Plotting Setup
% ----------------------
npanel = opts_plot.ncols*opts_plot.nrows;
j1 = 0;
isub = 0;
ifig = 0;
% Variables
% ----------------------
for i = 1:size(vars_irf,1)
j1=j1+1;
if mod(j1,npanel)==1
gcf = dyn_figure(options.nodisplay,'name','OccbinSimul','PaperPositionMode', 'auto','PaperType','A4','PaperOrientation','portrait','renderermode','auto','position',figure_size);
ifig=ifig+1;
isub=0;
end
isub=isub+1;
% Plot
subplot(opts_plot.nrows,opts_plot.ncols,isub)
idxvar = strmatch(vars_irf{i,1},M.endo_names,'exact');
if isempty(idxvar)
error('Variable %s not found!',vars_irf{i,1})
end
xlim([opts_plot.start_plot (length_simul-cutoff)]);
if size(vars_irf,2) > 2
exofactor = vars_irf{i,3};
else
exofactor = 1;
end
% Steady state
if add_steadystate || (size(vars_irf,2) > 3 && vars_irf{i,4} == 1)
stst = get_mean(vars_irf{i,1});
else
stst = 0;
end
simuls = fieldnames(my_simuls);
for s = 1:length(simuls)
my_field = char(simuls(s));
values = my_simuls.(my_field)(1:length_simul,idxvar);
values = values+stst;
if (add_absval_graph && ismember(i,mystst_simul_pos))
values = mystst_simul{s}(1:length_simul,idxvar);
end
values = values*exofactor;
values(abs(values) <threshold) = 0;
if ismember(i,opts_plot.log_normalize_graph)
values = exp(values)/exp(values(opts_plot.normalization_point))*100;
end
if opts_plot.add_zero_line
hold on
plot(values*0,'linewidth',1,'linestyle','-','color','k','HandleVisibility','off');
end
hplot = plot(values,'linewidth',lw);
hold all
set(hplot,'linestyle',opts_plot.mystyles{s},'color',opts_plot.mycolors(s,:));
if any(opts_plot.add_vertical_line > 0)
for kk = 1:length(opts_plot.add_vertical_line)
xvalue = opts_plot.add_vertical_line(kk);
yl = ylim; % current y-axis limits
plot([xvalue xvalue],[yl(1) yl(2)],'linewidth',1,'linestyle','-','color','k','HandleVisibility','off');
end
end
end
if ismember(i,opts_plot.scale_y)
yl = ylim; % current y-axis limits
ylim([yl(1)*opts_plot.scale_y1 yl(2)*opts_plot.scale_y2 ]);
end
% Axes and Title
if isfield(opts_plot,'xlabel')
xlabel(opts_plot.xlabel)
end
if opts_plot.timeaxis
first_year = find(periods == round(periods),1);
xticks(first_year:opts_plot.timeaxis_steps:length_simul)
xticklabels({periods(first_year:opts_plot.timeaxis_steps:length_simul)})
xtickangle(45)
end
if size(vars_irf,2) > 2
title(vars_irf{i,2})
else
title(regexprep(vars_irf{i,1},'_',' '))
end
if size(vars_irf,2) > 4
ylabel(vars_irf{i,5})
end
if opts_plot.use_grid
grid on