File: ms_irf.m

package info (click to toggle)
dynare 6.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,648 kB
  • sloc: cpp: 79,109; ansic: 28,917; objc: 12,430; yacc: 4,528; pascal: 1,993; lex: 1,441; sh: 1,129; python: 634; makefile: 626; lisp: 163; xml: 18
file content (124 lines) | stat: -rw-r--r-- 4,786 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
function [options_, oo_]=ms_irf(varlist, M_, options_, oo_)
% function [options_, oo_]=ms_irf(varlist, M_, options_, oo_)
% Markov-switching SBVAR: Impulse Response Function
%
% INPUTS
%    varlist:     (chararray) list of selected endogenous variables
%    M_:          (struct)    model structure
%    options_:    (struct)    options
%    oo_:         (struct)    results
%
% OUTPUTS
%    options_:    (struct)    options
%    oo_:         (struct)    results
%
% SPECIAL REQUIREMENTS
%    none

% Copyright © 2011-2020 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <https://www.gnu.org/licenses/>.

disp('MS-SBVAR Impulse Response Function');
options_ = set_file_tags(options_);
[options_, oo_] = set_ms_estimation_file(options_.ms.file_tag, options_, oo_);
clean_ms_irf_files(options_.ms.output_file_tag);
irfdir = [options_.ms.output_file_tag filesep 'IRF'];
create_dir(irfdir);

% setup command line options
opt = ['-ir -seed ' num2str(options_.DynareRandomStreams.seed)];
opt = [opt ' -do ' irfdir];
opt = [opt ' -ft ' options_.ms.file_tag];
opt = [opt ' -fto ' options_.ms.output_file_tag];
opt = [opt ' -horizon ' num2str(options_.ms.horizon)];
opt = [opt ' -thin ' num2str(options_.ms.thinning_factor)];

if options_.ms.regimes
    opt = [opt ' -regimes'];
elseif options_.ms.regime
    % regime-1 since regime is 0-indexed in C but 1-indexed in Matlab
    opt = [opt ' -regime ' num2str(options_.ms.regime-1)];
elseif options_.ms.filtered_probabilities
    opt = [opt ' -filtered'];
end

if options_.ms.parameter_uncertainty
    options_ = set_ms_simulation_file(options_);
    opt = [opt ' -parameter_uncertainty'];
    opt = [opt ' -shocks_per_parameter ' num2str(options_.ms.shocks_per_parameter)];
else
    opt = [opt ' -shocks_per_parameter ' num2str(options_.ms.shock_draws)];
end

percentiles_size = 0;
if options_.ms.median
    percentiles_size = 1;
    opt = [opt ' -percentiles ' num2str(percentiles_size) ' 0.5'];
else
    percentiles_size = size(options_.ms.percentiles,2);
    opt = [opt ' -percentiles ' num2str(percentiles_size)];
    for i=1:size(options_.ms.percentiles,2)
        opt = [opt ' ' num2str(options_.ms.percentiles(i))];
    end
end

% irf
ms_sbvar_command_line(opt);

% Plot IRFs
if options_.ms.regimes
    n_chains = length(options_.ms.ms_chain);
    n_regimes=1;
    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
        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) '.mat'], 'irf_data');
        plot_ms_irf(M_, options_, irf_data, irf_title, varlist);
    end
else
    if options_.ms.regime
        irf_data = load([irfdir filesep 'ir_percentiles_regime_' ...
                         num2str(options_.ms.regime-1) '_' options_.ms.output_file_tag ...
                         '.out'], '-ascii');
        irf_title = ['Impulse Response, Regime ' num2str(options_.ms.regime)];
        save_filename = ['irf_regime_' num2str(options_.ms.regime-1) '.mat'];
    elseif options_.ms.filtered_probabilities
        irf_data = load([irfdir filesep 'ir_percentiles_filtered_' ...
                         options_.ms.output_file_tag '.out'], '-ascii');
        irf_title = 'Impulse Response Filtered';
        save_filename = 'irf.mat';
    else
        irf_data = load([irfdir filesep 'ir_percentiles_ergodic_' ...
                         options_.ms.output_file_tag '.out'], '-ascii');
        irf_title = 'Impulse Response Ergodic';
        save_filename = 'irf.mat';
    end

    irf_data = reshape_ascii_irf_data(M_.endo_nbr, percentiles_size, ...
                                      options_.ms.horizon, irf_data);
    save([irfdir filesep save_filename], 'irf_data');
    plot_ms_irf(M_, options_, irf_data, irf_title, varlist);
end
end