File: islagof.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 (129 lines) | stat: -rw-r--r-- 3,745 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
128
129
function b = islagof(v1, v2)

% Returns true if and only if variable v1 is a lag of variable v2.
%
% INPUTS
% - v1        [string]     Variable name.
% - v2        [string]     Variable name.
%
% OUTPUTS
% - b         [logical]

% Copyright © 2018 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_

if ~ischar(v1) && isscalar(v1) && isnumeric(v1) && isint(v1)
    id1 = v1;
    if id1>M_.endo_nbr
        error('First input must be an integer between 1 and %u', M_.endo_nbr)
    end
end

if ~ischar(v2) && isscalar(v2) && isnumeric(v2) && isint(v2)
    id2 = v2;
    if id2>M_.endo_nbr
        error('First input must be an integer between 1 and %u', M_.endo_nbr)
    end
end

% Set output default value
b = false;

% Get index of v1
if ischar(v1)
    id1 = find(strcmp(v1, M_.endo_names));
end

if isempty(id1)
    error('First input must be a variable name.')
end

% Get index of v2
if ischar(v2)
    id2 = find(strcmp(v2, M_.endo_names));
end

if isempty(id2)
    error('Second input must be a variable name.')
end

% A variable cannot be a lag of itself.
if isequal(id1, id2)
    return
end

% Are v1 and v2 auxiliary variables?
v1_is_an_auxiliary_variable = id1>M_.orig_endo_nbr;
v2_is_an_auxiliary_variable = id2>M_.orig_endo_nbr;

% At least one of the variables must be an auxiliary variable
if ~v1_is_an_auxiliary_variable && ~v2_is_an_auxiliary_variable
    return
end

% If v1 and v2 are auxiliary variables
if v1_is_an_auxiliary_variable && v2_is_an_auxiliary_variable
    auxinfo1 = M_.aux_vars(get_aux_variable_id(id1));
    auxinfo2 = M_.aux_vars(get_aux_variable_id(id2));
    isleadlag1 = ismember(auxinfo1.type, [1,0]);
    isleadlag2 = ismember(auxinfo2.type, [1,0]);
    % If v1 and v2 are lead/lag auxiliary variables
    if isleadlag1 && isleadlag2
        % If v1 and v2 are lead/lag of a common endogenous variable.
        if auxinfo1.orig_index && auxinfo2.orig_index
            if auxinfo1.orig_lead_lag<auxinfo2.orig_lead_lag
                b = true;
            end
        end
        return
    end
    isdiff1 = ismember(auxinfo1.type, [8,9]);
    isdiff2 = ismember(auxinfo1.type, [8,9]);
    if isdiff1 && isdiff2
        if isequal(auxinfo1.type, 9) && isequal(auxinfo2.type, 8)
            while isequal(auxinfo1.type, 9)
                if isequal(auxinfo1.orig_index, id2)
                    b = true;
                end
                auxinfo1 = M_.aux_vars(get_aux_variable_id(auxinfo1.orig_index));
            end
        end
        return
    end
end

if v1_is_an_auxiliary_variable && ~v2_is_an_auxiliary_variable
    auxinfo1 = M_.aux_vars(get_aux_variable_id(id1));
    % If v1 is not an auxiliary of type 1, it cannot be a lag of v2
    if isequal(auxinfo1.type, 1)
        if isequal(auxinfo1.orig_index, id2)
            b = true;
        end
    end
end

if ~v1_is_an_auxiliary_variable && v2_is_an_auxiliary_variable
    auxinfo2 = M_.aux_vars(get_aux_variable_id(id2));
    % If v2 is not an auxiliary of type 0, v1 cannot be a lag of v2
    if isequal(auxinfo2.type, 0)
        if isequal(auxinfo2.orig_index, id1)
            b = true;
        end
    end
end