1function b = islagof(v1, v2)
2
3% Returns true if and only if variable v1 is a lag of variable v2.
4%
5% INPUTS
6% - v1        [string]     Variable name.
7% - v2        [string]     Variable name.
8%
9% OUTPUTS
10% - b         [logical]
11
12% Copyright (C) 2018 Dynare Team
13%
14% This file is part of Dynare.
15%
16% Dynare is free software: you can redistribute it and/or modify
17% it under the terms of the GNU General Public License as published by
18% the Free Software Foundation, either version 3 of the License, or
19% (at your option) any later version.
20%
21% Dynare is distributed in the hope that it will be useful,
22% but WITHOUT ANY WARRANTY; without even the implied warranty of
23% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24% GNU General Public License for more details.
25%
26% You should have received a copy of the GNU General Public License
27% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
28
29global M_
30
31if ~ischar(v1) && isscalar(v1) && isnumeric(v1) && isint(v1)
32    id1 = v1;
33    if id1>M_.endo_nbr
34        error('First input must be an integer between 1 and %u', M_.endo_nbr)
35    end
36end
37
38if ~ischar(v2) && isscalar(v2) && isnumeric(v2) && isint(v2)
39    id2 = v2;
40    if id2>M_.endo_nbr
41        error('First input must be an integer between 1 and %u', M_.endo_nbr)
42    end
43end
44
45% Set output default value
46b = false;
47
48% Get index of v1
49if ischar(v1)
50    id1 = find(strcmp(v1, M_.endo_names));
51end
52
53if isempty(id1)
54    error('First input must be a variable name.')
55end
56
57% Get index of v2
58if ischar(v2)
59    id2 = find(strcmp(v2, M_.endo_names));
60end
61
62if isempty(id2)
63    error('Second input must be a variable name.')
64end
65
66% A variable cannot be a lag of itself.
67if isequal(id1, id2)
68    return
69end
70
71% Are v1 and v2 auxiliary variables?
72v1_is_an_auxiliary_variable = id1>M_.orig_endo_nbr;
73v2_is_an_auxiliary_variable = id2>M_.orig_endo_nbr;
74
75% At least one of the variables must be an auxiliary variable
76if ~v1_is_an_auxiliary_variable && ~v2_is_an_auxiliary_variable
77    return
78end
79
80% If v1 and v2 are auxiliary variables
81if v1_is_an_auxiliary_variable && v2_is_an_auxiliary_variable
82    auxinfo1 = M_.aux_vars(get_aux_variable_id(id1));
83    auxinfo2 = M_.aux_vars(get_aux_variable_id(id2));
84    isleadlag1 = ismember(auxinfo1.type, [1,0]);
85    isleadlag2 = ismember(auxinfo2.type, [1,0]);
86    % If v1 and v2 are lead/lag auxiliary variables
87    if isleadlag1 && isleadlag2
88        % If v1 and v2 are lead/lag of a common endogenous variable.
89        if auxinfo1.orig_index && auxinfo2.orig_index
90            if auxinfo1.orig_lead_lag<auxinfo2.orig_lead_lag
91                b = true;
92            end
93        end
94        return
95    end
96    isdiff1 = ismember(auxinfo1.type, [8,9]);
97    isdiff2 = ismember(auxinfo1.type, [8,9]);
98    if isdiff1 && isdiff2
99        if isequal(auxinfo1.type, 9) && isequal(auxinfo2.type, 8)
100            while isequal(auxinfo1.type, 9)
101                if isequal(auxinfo1.orig_index, id2)
102                    b = true;
103                end
104                auxinfo1 = M_.aux_vars(get_aux_variable_id(auxinfo1.orig_index));
105            end
106        end
107        return
108    end
109end
110
111if v1_is_an_auxiliary_variable && ~v2_is_an_auxiliary_variable
112    auxinfo1 = M_.aux_vars(get_aux_variable_id(id1));
113    % If v1 is not an auxiliary of type 1, it cannot be a lag of v2
114    if isequal(auxinfo1.type, 1)
115        if isequal(auxinfo1.orig_index, id2)
116            b = true;
117        end
118    end
119end
120
121if ~v1_is_an_auxiliary_variable && v2_is_an_auxiliary_variable
122    auxinfo2 = M_.aux_vars(get_aux_variable_id(id2));
123    % If v2 is not an auxiliary of type 0, v1 cannot be a lag of v2
124    if isequal(auxinfo2.type, 0)
125        if isequal(auxinfo2.orig_index, id1)
126            b = true;
127        end
128    end
129end