2023-03-04 22:31:54 +01:00
|
|
|
function search(variablename, option)
|
2019-10-31 13:33:46 +01:00
|
|
|
|
|
|
|
% Prints equations where the variable appears in.
|
|
|
|
%
|
|
|
|
% INPUTS
|
2023-03-04 17:49:33 +01:00
|
|
|
% - variablename [string] Name of the variable to be traced.
|
2023-03-04 22:31:54 +01:00
|
|
|
% - option [string] if equal to 'withparamvalues' reports the values of the parameters, otherwise throws an error.
|
2019-10-31 13:33:46 +01:00
|
|
|
%
|
|
|
|
% OUTPUTS
|
|
|
|
% None
|
|
|
|
|
2023-03-04 17:13:45 +01:00
|
|
|
% Copyright © 2019-2023 Dynare Team
|
2019-10-31 13:33:46 +01:00
|
|
|
%
|
|
|
|
% 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
|
2021-06-09 17:33:48 +02:00
|
|
|
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
2019-10-31 13:33:46 +01:00
|
|
|
|
|
|
|
global M_
|
|
|
|
|
2023-03-04 17:49:33 +01:00
|
|
|
withexpansion = true;
|
|
|
|
|
|
|
|
if ~nargin
|
|
|
|
error('Provide endogenous variable name as first input argument.');
|
2019-10-31 13:33:46 +01:00
|
|
|
end
|
|
|
|
|
2023-03-04 18:03:59 +01:00
|
|
|
jname = sprintf('%s%smodel%sjson%smodfile-original.json', M_.fname, filesep, filesep, filesep);
|
|
|
|
|
2019-10-31 13:33:46 +01:00
|
|
|
% Check if corresponding JSON file exists.
|
2023-03-04 18:03:59 +01:00
|
|
|
if exist(jname, 'file') ~= 2
|
|
|
|
error('Please use the json option (See the Dynare invocation section in the reference manual).');
|
2019-10-31 13:33:46 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
% Check that the first input is a character array.
|
2023-03-04 17:49:33 +01:00
|
|
|
if ~ischar(variablename)
|
2019-10-31 13:33:46 +01:00
|
|
|
error('First input argument must be a string.');
|
|
|
|
end
|
2023-03-04 17:13:45 +01:00
|
|
|
|
2019-10-31 13:33:46 +01:00
|
|
|
% Check that the variable is actually a variable in the model.
|
2023-03-04 17:49:33 +01:00
|
|
|
if ~ismember(variablename, [M_.exo_names; M_.endo_names])
|
|
|
|
error('There is no variable named %s!', variablename);
|
2019-10-31 13:33:46 +01:00
|
|
|
end
|
|
|
|
|
2023-03-04 22:31:54 +01:00
|
|
|
withparamvalues = false; % By default the values of the parameters are not printed
|
|
|
|
|
|
|
|
if nargin==2
|
|
|
|
if ischar(option)
|
|
|
|
if strcmp(option, 'withparamvalues')
|
|
|
|
withparamvalues = true;
|
|
|
|
else
|
|
|
|
error('Unknown option %s.', option)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
error('Second input argument must be a row character array.')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if nargin>2
|
|
|
|
error('Too many input arguments.')
|
|
|
|
end
|
|
|
|
|
2019-10-31 13:33:46 +01:00
|
|
|
% Load the JSON file.
|
2023-03-04 18:03:59 +01:00
|
|
|
jsonfile = loadjson_(jname);
|
2019-10-31 13:33:46 +01:00
|
|
|
model = jsonfile.model;
|
|
|
|
|
|
|
|
% Print the equations the variable appears in.
|
2023-03-04 17:49:33 +01:00
|
|
|
for it = 1:length(M_.mapping.(variablename).eqidx)
|
|
|
|
if M_.mapping.(variablename).eqidx(it)>length(model)
|
2021-11-18 12:24:23 +01:00
|
|
|
% Equations appended by the preprocessor for auxiliary variables are not displayed, except if it is an aux variable
|
|
|
|
% for the PAC expectation.
|
|
|
|
% TODO Probably should do the same for VAR expectation.
|
2023-03-04 17:49:33 +01:00
|
|
|
if isfield(M_, 'lhs') && length(M_.lhs{M_.mapping.(variablename).eqidx(it)})>15 && isequal(M_.lhs{M_.mapping.(variablename).eqidx(it)}(1:16), 'pac_expectation_')
|
|
|
|
id = M_.mapping.(M_.lhs{M_.mapping.(variablename).eqidx(it)}).eqidx(1);
|
2021-11-18 12:24:23 +01:00
|
|
|
else
|
|
|
|
continue
|
|
|
|
end
|
|
|
|
else
|
2023-03-04 17:49:33 +01:00
|
|
|
id = M_.mapping.(variablename).eqidx(it);
|
2021-11-18 12:24:23 +01:00
|
|
|
end
|
|
|
|
rhs = model{id}.rhs;
|
2019-10-31 13:33:46 +01:00
|
|
|
if withexpansion
|
|
|
|
if isfield(M_, 'pac') && contains(rhs, 'pac_expectation')
|
|
|
|
% Get the index of the equation's PAC model.
|
|
|
|
models = fieldnames(M_.pac);
|
|
|
|
idx = find(~cellfun('isempty',cellfun(@(s)find(contains(rhs,s)),models,'uni',0)));
|
|
|
|
% Get the expanded PAC_EXPECTATION term.
|
2021-11-18 12:24:23 +01:00
|
|
|
[pac_expression, growthneutralitycorrection] = write_expectations(models{idx}, 'pac', true);
|
2019-10-31 13:33:46 +01:00
|
|
|
expression = [sprintf('\n\t + %s', growthneutralitycorrection) TransformExpandedExpr(pac_expression)];
|
|
|
|
rhs = strrep(rhs, ['+pac_expectation(model_name = ' models{idx} ')'], expression);
|
|
|
|
elseif isfield(M_, 'var_expectation') && contains(rhs, 'var_expectation')
|
|
|
|
% Get the index of the equation's VAR model.
|
|
|
|
models = fieldnames(M_.var_expectation);
|
|
|
|
idx = find(~cellfun('isempty',cellfun(@(s)find(contains(rhs,s)),models,'uni',0)));
|
|
|
|
% Get the expanded VAR_EXPECTATION term.
|
2021-11-18 12:24:23 +01:00
|
|
|
expression = write_expectations(models{idx}, 'var', true);
|
2019-10-31 13:33:46 +01:00
|
|
|
expression = TransformExpandedExpr(expression);
|
|
|
|
rhs = strrep(rhs, ['+var_expectation(model_name = ' models{idx} ')'], expression);
|
|
|
|
elseif ~isfield(M_, 'pac') && ~isfield(M_, 'var_expectation')
|
|
|
|
withexpansion = false;
|
|
|
|
end
|
|
|
|
end
|
2023-03-04 22:31:54 +01:00
|
|
|
lhs = model{id}.lhs;
|
|
|
|
if withparamvalues
|
|
|
|
pnames = get_variables_and_parameters_in_equation(lhs, rhs, M_);
|
|
|
|
if ~isempty(pnames)
|
|
|
|
for ip = 1:length(pnames)
|
|
|
|
iP = find(strcmp(M_.param_names, pnames{ip}));
|
|
|
|
if isnan(M_.params(iP))
|
|
|
|
continue
|
|
|
|
end
|
|
|
|
value = M_.params(iP);
|
|
|
|
rhs = regexprep(rhs, sprintf('\\<%s\\>', pnames{ip}), num2str(value, 6));
|
|
|
|
lhs = regexprep(lhs, sprintf('\\<%s\\>', pnames{ip}), num2str(value, 6));
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-03-04 18:03:59 +01:00
|
|
|
skipline()
|
2023-03-04 22:31:54 +01:00
|
|
|
fprintf('%s = %s;\n', lhs, rhs);
|
2019-10-31 13:33:46 +01:00
|
|
|
end
|
|
|
|
|
2023-03-04 18:03:59 +01:00
|
|
|
skipline()
|
|
|
|
|
2019-10-31 13:33:46 +01:00
|
|
|
function [transformed_expression] = TransformExpandedExpr(expression)
|
2023-03-04 22:31:54 +01:00
|
|
|
transformed_expression = splitlines(expression);
|
|
|
|
transformed_expression{1} = sprintf(' + %s', transformed_expression{1});
|
|
|
|
transformed_expression = sprintf('\n\t%s', transformed_expression{:});
|