File: common_parsing.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 (127 lines) | stat: -rw-r--r-- 4,624 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
125
126
127
function [Y, lhssub, X, startdates, enddates, residnames] = common_parsing(ds, ast, overlapping_dates, param_names)
%function [Y, lhssub, X, startdates, enddates, residnames] = common_parsing(ds, ast, overlapping_dates, param_names)
%
% Code common to sur.m and pooled_ols.m
%
% INPUTS
%   ds                [dseries]         dataset
%   ast               [cell array]      JSON representation of abstract syntax tree
%   overlapping_dates [boolean]         if true, dates are same across equations
%
% OUTPUTS
%   Y                 [cell array]      dependent variables
%   lhssub            [cell array]      RHS subtracted from LHS
%   X                 [cell array]      regressors
%   startdates        [cell array]      first observed period for each
%                                       equation
%   enddates          [cell array]      last observed period for each
%                                       equation
%   startidxs         [vector]          rows corresponding to each
%                                       equation's observations
%   residnames        [cell array]      name of residual in each equation
%   param_names       [cellstr or cell] list of parameters to estimate (if
%                                       empty, estimate all)
%
% SPECIAL REQUIREMENTS
%   none

% Copyright © 2019 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/>.

global M_

%% Initialize variables
neqs = length(ast);
Y = cell(neqs, 1);
lhssub = cell(neqs, 1);
X = cell(neqs, 1);
startdates = cell(neqs, 1);
enddates = cell(neqs, 1);
residnames = cell(neqs, 1);

%% Loop over equations
for i = 1:neqs
    [Y{i}, lhssub{i}, X{i}, residnames{i}, startdates{i}, enddates{i}] = ...
        parse_ols_style_equation(ds, ast{i});
end

if overlapping_dates
    maxfp = max([startdates{:}]);
    minlp = min([enddates{:}]);
    for i = 1:neqs
        Y{i} = Y{i}(maxfp:minlp);
        if ~isempty(X{i})
            X{i} = X{i}(maxfp:minlp);
        end
        if ~isempty(lhssub{i})
            lhssub{i} = lhssub{i}(maxfp:minlp);
        end
        startdates{i} = maxfp;
        enddates{i} = minlp;
    end
end

if ~isempty(param_names)
    if iscell(param_names) && ~iscellstr(param_names)
        assert(length(param_names) == neqs, 'error w param_names arg');
    else
        pn = param_names;
        pn_found_cellstr = false(length(param_names), 1);
    end
    for i = 1:neqs
        if iscell(param_names) && ~iscellstr(param_names)
            pn = param_names{i};
            pn_found_cellstr_i = false(length(pn), 1);
        end
        names = X{i}.name;
        newlhssub = dseries();
        for j = 1:length(names)
            idx = find(strcmp(names{j}, pn));
            if isempty(idx)
                pval = M_.params(strcmp(names{j}, M_.param_names));
                if isnan(pval) || isinf(pval)
                    error(['Could not find param init value for ' names{j}])
                end
                newlhssub = newlhssub + pval * X{i}.(names{j});
                X{i} = X{i}.remove(names{j});
            else
                if iscell(param_names) && ~iscellstr(param_names)
                    pn_found_cellstr_i = true;
                else
                    pn_found_cellstr(idx) = true;
                end
            end
        end
        Y{i} = Y{i} - newlhssub;
        lhssub{i} = lhssub{i} + newlhssub;
        if iscell(param_names) && ~iscellstr(param_names)
            if ~all(pn_found_cellstr_i)
                error(['parameters specified in param_names (' ...
                    strjoin(pn(~pn_found_cellstr_i), ', ') ...
                    ') were not found in eq ' num2str(i) ' to be estimated'])
            end
        end
    end
    if ~(iscell(param_names) && ~iscellstr(param_names))
        if ~all(pn_found_cellstr)
            error(['parameters specified in param_names (' ...
                strjoin(pn(~pn_found_cellstr), ', ') ...
                ') were not found in the equation(s) to be estimated'])
        end
    end
end
end