From ad580b8cc64eb360e57b8ffd61019641b10ba6ce Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 8 Mar 2012 18:25:37 +0100 Subject: [PATCH] MS-SBVAR: clean up plotting functions --- matlab/ms-sbvar/ms_forecast.m | 8 ++- matlab/ms-sbvar/ms_irf.m | 4 +- matlab/ms-sbvar/ms_variance_decomposition.m | 9 ++-- matlab/ms-sbvar/plot_ms_forecast.m | 49 ++++++++++--------- matlab/ms-sbvar/plot_ms_irf.m | 45 +++++++++-------- .../ms-sbvar/plot_ms_variance_decomposition.m | 28 ++++++----- ...ot_ms_variance_decomposition_error_bands.m | 21 +++++--- 7 files changed, 89 insertions(+), 75 deletions(-) diff --git a/matlab/ms-sbvar/ms_forecast.m b/matlab/ms-sbvar/ms_forecast.m index 68a3152da..cea1af46a 100644 --- a/matlab/ms-sbvar/ms_forecast.m +++ b/matlab/ms-sbvar/ms_forecast.m @@ -87,6 +87,7 @@ if options_.ms.regimes end for regime_i=1:n_regimes + forecast_title = ['Forecast, Regimes ' num2str(regime_i)]; forecast_data = load([forecastdir filesep 'forecasts_percentiles_regime_' ... num2str(regime_i-1) '_' options_.ms.output_file_tag ... '.out'], '-ascii'); @@ -94,9 +95,7 @@ if options_.ms.regimes percentiles_size, options_.ms.horizon, forecast_data); save([forecastdir filesep 'forecast_regime_' num2str(regime_i-1)], ... 'forecast_data'); - plot_ms_forecast(M_, options_, forecast_data, ... - ['Forecast, Regimes' num2str(regime_i)], ... - options_.graph_save_formats, options_.TeX); + plot_ms_forecast(M_, options_, forecast_data, forecast_title); end else if options_.ms.regime @@ -115,7 +114,6 @@ else forecast_data = reshape_ascii_forecast_data(M_.endo_nbr, ... percentiles_size, options_.ms.horizon, forecast_data); save([forecastdir filesep save_filename], 'forecast_data'); - plot_ms_forecast(M_, options_, forecast_data, forecast_title, ... - options_.graph_save_formats, options_.TeX); + plot_ms_forecast(M_, options_, forecast_data, forecast_title); end end diff --git a/matlab/ms-sbvar/ms_irf.m b/matlab/ms-sbvar/ms_irf.m index a69610f96..4b62538fd 100644 --- a/matlab/ms-sbvar/ms_irf.m +++ b/matlab/ms-sbvar/ms_irf.m @@ -89,14 +89,14 @@ if options_.ms.regimes end for regime_i=1:n_regimes + irf_title = ['Impulse Responses, Regime ' num2str(regime_i)]; irf_data = load([irfdir filesep 'ir_percentiles_regime_' ... num2str(regime_i-1) '_' options_.ms.output_file_tag ... '.out'], '-ascii'); irf_data = reshape_ascii_irf_data(M_.endo_nbr, percentiles_size, ... options_.ms.horizon, irf_data); save([irfdir filesep 'irf_regime_' num2str(regime_i-1)], 'irf_data'); - plot_ms_irf(M_, options_, irf_data, ... - ['Impulse Responses, Regime ' num2str(regime_i)], varlist); + plot_ms_irf(M_, options_, irf_data, irf_title, varlist); end else if options_.ms.regime diff --git a/matlab/ms-sbvar/ms_variance_decomposition.m b/matlab/ms-sbvar/ms_variance_decomposition.m index 3633cce3f..648c89952 100644 --- a/matlab/ms-sbvar/ms_variance_decomposition.m +++ b/matlab/ms-sbvar/ms_variance_decomposition.m @@ -100,16 +100,14 @@ if options_.ms.regimes for i_chain=1:n_chains n_regimes = n_regimes*length(options_.ms.ms_chain(i_chain).regime); end - for regime_i=1:n_regimes + vd_title = ['Variance Decomposition, Regime ' num2str(regime_i)]; vd_data = load([outfile num2str(regime_i-1) '_' ... options_.ms.output_file_tag '.out'], '-ascii'); vd_data = reshape_ascii_variance_decomposition_data( ... M_.endo_nbr, percentiles_size, options_.ms.horizon, vd_data); save([vddir filesep 'variance_decomposition_regime_' num2str(regime_i-1)], 'vd_data'); - plot_ms_variance_decomposition(M_, options_, vd_data, ... - ['Variance Decomposition, Regime ' num2str(regime_i)], ... - options_.graph_save_formats, options_.TeX); + plot_ms_variance_decomposition(M_, options_, vd_data, vd_title); end else if options_.ms.regime @@ -127,7 +125,6 @@ else vd_data = reshape_ascii_variance_decomposition_data( ... M_.endo_nbr, percentiles_size, options_.ms.horizon, vd_data); save([vddir filesep save_filename], 'vd_data'); - plot_ms_variance_decomposition(M_, options_, vd_data, ... - vd_title, options_.graph_save_formats, options_.TeX); + plot_ms_variance_decomposition(M_, options_, vd_data, vd_title); end end diff --git a/matlab/ms-sbvar/plot_ms_forecast.m b/matlab/ms-sbvar/plot_ms_forecast.m index 636a5917d..c6f0661f3 100644 --- a/matlab/ms-sbvar/plot_ms_forecast.m +++ b/matlab/ms-sbvar/plot_ms_forecast.m @@ -1,14 +1,19 @@ -function plot_ms_forecast(M_,options_,forecast,title_,save_graph_formats,TeX) -% function plot_ms_forecast(M_,options_,forecast,title_,save_graph_formats,TeX) +function plot_ms_forecast(M_, options_, forecast, figure_name) +% function plot_ms_forecast(M_, options_, forecast, figure_name) % plots the forecast from the output from a ms-sbvar % % INPUTS -% M_ -% forecast should be in the form (percentile x horizon x nvar ), if banded otherwise -% ( horizon x nvar ) +% M_: (struct) model structure +% options_: (struct) options +% forecast: (matrix) in the form (percentile x horizon x nvar ), if banded otherwise +% ( horizon x nvar ) +% figure_name: (string) title % -% title: title +% OUTPUTS +% none % +% SPECIAL REQUIREMENTS +% none % Copyright (C) 2011-2012 Dynare Team % @@ -62,34 +67,34 @@ function plot_ms_forecast(M_,options_,forecast,title_,save_graph_formats,TeX) end if num_percentiles == 1 - plot_point_forecast(forecast, nvars, nr, nc, var_list, title_, ... - save_graph_formats, TeX, names, tex_names, ... + plot_point_forecast(forecast, nvars, nr, nc, var_list, figure_name, ... + options_.graph_save_formats, options_.TeX, names, tex_names, ... [options_.ms.output_file_tag filesep 'Output' filesep 'Forecast']); else plot_banded_forecast(forecast, nvars, nr, nc, var_list, num_percentiles, ... - title_, save_graph_formats, TeX, names, tex_names, ... + figure_name, options_.graph_save_formats, options_.TeX, names, tex_names, ... [options_.ms.output_file_tag filesep 'Output' filesep 'Forecast']); end end -function plot_point_forecast(forecast,nvars,nr,nc,endo_names,title_,save_graph_formats,TeX,names,tex_names,dirname) +function plot_point_forecast(forecast,nvars,nr,nc,endo_names,figure_name,save_graph_formats,TeX,names,tex_names,dirname) if nvars > nr*nc - graph_name = 'MS-Forecast (1)'; - fig = figure('Name','Forecast (I)'); + graph_name = ['MS (1) ' figure_name]; + figure('Name', graph_name); else - graph_name = 'MS-Forecast'; - fig = figure('Name','Forecast'); + graph_name = figure_name; + figure('Name', graph_name); end m = 1; n_fig = 1; for j=1:nvars if m > nr*nc - graph_name = ['MS-Forecast (' int2str(n_fig) ')']; + graph_name = ['MS (' int2str(n_fig) ') ' figure_name]; dyn_save_graph(dirname,['MS-forecast-' int2str(n_fig)],... save_graph_formats,TeX,names,tex_names,graph_name); n_fig =n_fig+1; - figure('Name',['MS-Forecast (' int2str(n_fig) ')']); + figure('Name', graph_name); m = 1; end subplot(nr,nc,m); @@ -105,19 +110,19 @@ function plot_point_forecast(forecast,nvars,nr,nc,endo_names,title_,save_graph_f end end -function plot_banded_forecast(forecast,nvars,nr,nc,endo_names,num_percentiles,title_,save_graph_formats,TeX,names,tex_names,dirname) +function plot_banded_forecast(forecast,nvars,nr,nc,endo_names,num_percentiles,figure_name,save_graph_formats,TeX,names,tex_names,dirname) if nvars > nr*nc - graph_name = 'MS-Forecast (1)'; - fig = figure('Name','Forecast (I)'); + graph_name = ['MS (1) ' figure_name]; + figure('Name', graph_name); else - graph_name = 'MS-Forecast'; - fig = figure('Name','Forecast'); + graph_name = figure_name; + figure('Name', graph_name); end m = 1; n_fig = 1; for j=1:nvars if m > nr*nc - graph_name = ['MS-Forecast (' int2str(n_fig) ')']; + graph_name = ['MS (' int2str(n_fig) ') ' figure_name]; dyn_save_graph(dirname,['MS-forecast-' int2str(n_fig)],... save_graph_formats,TeX,names,tex_names,graph_name); n_fig =n_fig+1; diff --git a/matlab/ms-sbvar/plot_ms_irf.m b/matlab/ms-sbvar/plot_ms_irf.m index ee9252ad0..82c972747 100644 --- a/matlab/ms-sbvar/plot_ms_irf.m +++ b/matlab/ms-sbvar/plot_ms_irf.m @@ -1,16 +1,19 @@ -function plot_ms_irf(M_, options_, irf, title_, varlist) -% function plot_ms_irf(M_, options_, irf, title_, varlist) +function plot_ms_irf(M_, options_, irf, figure_name, varlist) +% function plot_ms_irf(M_, options_, irf, figure_name, varlist) % plots the impulse responses from the output from a ms-sbvar % % INPUTS -% M_ -% irf should be in the form (percentile x horizon x (nvar x nvar)), if banded otherwise -% ( horizon x (nvar x nvar) ) -% title: optional super title +% M_: (struct) model structure +% options_: (struct) options +% irf: (matrix) in the form (percentile x horizon x (nvar x nvar)), if banded otherwise +% ( horizon x (nvar x nvar) ) +% figure_name: (string) title % -% The element in position (k,i+j*nvars) of ir is the response of the ith -% variable to the jth shock at horizon k. Horizon 0 is the contemporaneous -% response. +% OUTPUTS +% none +% +% SPECIAL REQUIREMENTS +% none % Copyright (C) 2011-2012 Dynare Team % @@ -30,7 +33,7 @@ function plot_ms_irf(M_, options_, irf, title_, varlist) % along with Dynare. If not, see . if nargin < 4 - title_ = ''; + figure_name = ''; end nvars = M_.endo_nbr; @@ -79,7 +82,7 @@ function plot_ms_irf(M_, options_, irf, title_, varlist) end if size(endo_names,1) ~= nvars - error('The names passed are not the same length as the number of variables') + error('The names passed are not the same length as the number of variables'); end if num_percentiles == 1 @@ -90,7 +93,7 @@ function plot_ms_irf(M_, options_, irf, title_, varlist) shock(:,i) = irf(:,((i-1) + ((s-1)*nvars)+1)); end plot_point_irf_for_shock(shock, nvars,endo_names, deblank(endo_names(s,:)), ... - title_, [options_.ms.output_file_tag filesep 'Output' filesep 'IRF'], options_, names, tex_names); + figure_name, [options_.ms.output_file_tag filesep 'Output' filesep 'IRF'], options_, names, tex_names); end else for s=1:nvars @@ -101,25 +104,25 @@ function plot_ms_irf(M_, options_, irf, title_, varlist) end end plot_banded_irf_for_shock(shock, nvars,endo_names, deblank(endo_names(s,:)), ... - title_, [options_.ms.output_file_tag filesep 'Output' filesep 'IRF'], options_, names, tex_names); + figure_name, [options_.ms.output_file_tag filesep 'Output' filesep 'IRF'], options_, names, tex_names); end end end -function [fig] = plot_point_irf_for_shock(irf,nvars,endo_names,shock_name,title_,dirname,options_,names,tex_names) - fig = figure('Name',title_); +function [fig] = plot_point_irf_for_shock(irf,nvars,endo_names,shock_name,figure_name,dirname,options_,names,tex_names) + fig = figure('Name',figure_name); for k=1:nvars subplot(ceil(sqrt(nvars)), ceil(sqrt(nvars)),k); plot(irf(:,k)) disp([endo_names(k,:) ' shock from ' shock_name]); title([endo_names(k,:) ' shock from ' shock_name]); end - dyn_save_graph(dirname,[title_ ' ' shock_name],options_.graph_save_formats, ... - options_.TeX,names,tex_names,[title_ ' ' shock_name]); + dyn_save_graph(dirname,[figure_name ' ' shock_name],options_.graph_save_formats, ... + options_.TeX,names,tex_names,[figure_name ' ' shock_name]); end -function [fig] = plot_banded_irf_for_shock(irf,nvars, endo_names, shock_name,title_,dirname,options_,names,tex_names) - fig = figure('Name',title_); +function [fig] = plot_banded_irf_for_shock(irf,nvars, endo_names, shock_name,figure_name,dirname,options_,names,tex_names) + fig = figure('Name',figure_name); npercentiles = size(irf,3); for k=1:nvars subplot(ceil(sqrt(nvars)), ceil(sqrt(nvars)),k); @@ -131,6 +134,6 @@ function [fig] = plot_banded_irf_for_shock(irf,nvars, endo_names, shock_name,tit disp([endo_names(k,:) ' shock from ' shock_name]); title([endo_names(k,:) ' shock from ' shock_name]); end - dyn_save_graph(dirname,[title_ ' ' shock_name],options_.graph_save_formats, ... - options_.TeX,names,tex_names,[title_ ' ' shock_name]); + dyn_save_graph(dirname,[figure_name ' ' shock_name],options_.graph_save_formats, ... + options_.TeX,names,tex_names,[figure_name ' ' shock_name]); end diff --git a/matlab/ms-sbvar/plot_ms_variance_decomposition.m b/matlab/ms-sbvar/plot_ms_variance_decomposition.m index 74b5e470d..0188af2cc 100644 --- a/matlab/ms-sbvar/plot_ms_variance_decomposition.m +++ b/matlab/ms-sbvar/plot_ms_variance_decomposition.m @@ -1,12 +1,14 @@ -function plot_ms_variance_decomposition(M_, options_, vd, title_, graph_save_formats, TeX, varargin) -% function plot_ms_variance_decomposition(M_, options_, vd, title_, graph_save_formats, TeX, varargin) +function plot_ms_variance_decomposition(M_, options_, vd, figure_name, varargin) +% function plot_ms_variance_decomposition(M_, options_, vd, figure_name, varargin) % plot the variance decomposition of shocks % -% Inputs -% M_ -% shocks: matrix of the individual shocks Tx(KxK)with J=number of shocks +% INPUTS +% M_: (struct) model structure +% options_: (struct) options +% vd: (matrix) variance decomposition +% figure_name: (string) graph name % -% Optional Inputs +% OPTIONAL INPUTS % 'data': the actual data, TxK with K=number of data series % 'steady': the steady state value, TxK % 'shock_names': to specify the names of the shocks @@ -14,8 +16,11 @@ function plot_ms_variance_decomposition(M_, options_, vd, title_, graph_save_for % 'dates': pass a date vector to use, otherwise will just index on 1:T % 'colors': Jx3 list of the rgb colors to use for each shock % -% Example: -% plot_historic_decomposition(shocks,'VD','shock_names',shock_names,'series_names',series_names) +% OUTPUTS +% none +% +% SPECIAL REQUIREMENTS +% none % Copyright (C) 2011-2012 Dynare Team % @@ -35,7 +40,7 @@ function plot_ms_variance_decomposition(M_, options_, vd, title_, graph_save_for % along with Dynare. If not, see . if length(size(vd)) == 3 - plot_ms_variance_decomposition_error_bands(M_, options_, vd, title_); + plot_ms_variance_decomposition_error_bands(M_, options_, vd, figure_name); return; end @@ -111,7 +116,7 @@ end % add an extra period to the time series x(T+1) = x(T) + (x(T) - x(T-1)); - figure('Name',title_) + figure('Name',figure_name) for k=1:K % Go through each series subplot(K,1,k); @@ -163,5 +168,6 @@ end end dyn_save_graph([options_.ms.output_file_tag filesep 'Output' ... filesep 'Variance_Decomposition'], 'MS-Variance-Decomposition', ... - graph_save_formats, TeX,names,tex_names,'Variance decomposition'); + options_.graph_save_formats, options_.TeX, names, tex_names, ... + 'Variance decomposition'); end diff --git a/matlab/ms-sbvar/plot_ms_variance_decomposition_error_bands.m b/matlab/ms-sbvar/plot_ms_variance_decomposition_error_bands.m index 4ddecbd7b..cc03e87c7 100644 --- a/matlab/ms-sbvar/plot_ms_variance_decomposition_error_bands.m +++ b/matlab/ms-sbvar/plot_ms_variance_decomposition_error_bands.m @@ -1,5 +1,5 @@ -function plot_ms_variance_decomposition_error_bands(M_, options_, vddata, title_) -% function plot_ms_variance_decomposition_error_bands(M_, options_, vddata, title_) +function plot_ms_variance_decomposition_error_bands(M_, options_, vddata, figure_name) +% function plot_ms_variance_decomposition_error_bands(M_, options_, vddata, figure_name) % plots the variance decomposition with percentiles % % INPUTS @@ -7,8 +7,13 @@ function plot_ms_variance_decomposition_error_bands(M_, options_, vddata, title_ % options_: (struct) options % vddata: (matrix) variance_decomposition (percentile, options_.ms.horizon, nvar % x nvar) -% title: (string) title +% figure_name: (string) title % +% OUTPUTS +% none +% +% SPECIAL REQUIREMENTS +% none % Copyright (C) 2011-2012 Dynare Team % @@ -74,15 +79,15 @@ for s=1:nvars end end plot_banded_vddata_for_shock(shock, nvars, endo_names, ... - deblank(endo_names(s,:)), title_, ... + deblank(endo_names(s,:)), figure_name, ... [options_.ms.output_file_tag filesep 'Output' filesep 'Variance_Decomposition'], ... options_, names, tex_names); end end function [fig] = plot_banded_vddata_for_shock(vddata, nvars, endo_names, ... - shock_name, title_, dirname, options_, names, tex_names) -fig = figure('Name', title_); + shock_name, figure_name, dirname, options_, names, tex_names) +fig = figure('Name', figure_name); npercentiles = size(vddata,3); for k=1:nvars subplot(ceil(sqrt(nvars)), ceil(sqrt(nvars)),k); @@ -94,7 +99,7 @@ for k=1:nvars disp([endo_names(k,:) ' contribution to ' shock_name]); title([endo_names(k,:) ' contribution to ' shock_name]); end -dyn_save_graph(dirname, [title_ ' ' shock_name], ... +dyn_save_graph(dirname, [figure_name ' ' shock_name], ... options_.graph_save_formats, options_.TeX, names, tex_names, ... - [title_ ' ' shock_name]); + [figure_name ' ' shock_name]); end