2020-02-03 18:12:14 +01:00
|
|
|
function [ds, json] = evaluate(ds, eqtags, firstperiod, lastperiod, json)
|
2019-03-19 07:07:47 +01:00
|
|
|
|
|
|
|
% Copyright (C) 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
global M_
|
|
|
|
|
2020-02-03 18:12:14 +01:00
|
|
|
debug = false;
|
|
|
|
|
2019-04-08 11:01:34 +02:00
|
|
|
if ischar(eqtags)
|
|
|
|
eqtags = {eqtags};
|
|
|
|
end
|
2019-03-19 07:07:47 +01:00
|
|
|
|
2019-04-08 11:01:34 +02:00
|
|
|
list_of_expression_tokens = {'+', '-', '*', '/', '^', ...
|
|
|
|
'exp(', 'log(', 'sqrt(', 'abs(', 'sign(', ...
|
|
|
|
'sin(', 'cos(', 'tan(', 'asin(', 'acos(', 'atan(', ...
|
|
|
|
'min(', 'max(', ...
|
|
|
|
'normcdf(', 'normpdf(', 'erf(', ...
|
|
|
|
'diff(', 'adl(', ')'};
|
2019-03-19 07:07:47 +01:00
|
|
|
|
2020-02-03 18:12:14 +01:00
|
|
|
if ismember(nargin, [4, 5])
|
|
|
|
if isempty(firstperiod) && isempty(lastperiod)
|
|
|
|
range = ds.dates(1):ds.dates(end);
|
|
|
|
elseif isempty(firstperiod) && ~isempty(lastperiod)
|
|
|
|
range = ds.dates(1):lastperiod;
|
|
|
|
elseif ~isempty(firstperiod) && isempty(lastperiod)
|
|
|
|
range = firstperiod:ds.dates(end);
|
|
|
|
else
|
|
|
|
range = firstperiod:lastperiod;
|
|
|
|
end
|
|
|
|
elseif isequal(nargin, 3)
|
|
|
|
if isempty(firstperiod)
|
|
|
|
range = ds.dates(1):ds.dates(end);
|
|
|
|
else
|
|
|
|
range = firstperiod:ds.dates(end);
|
|
|
|
end
|
|
|
|
elseif isequal(nargin, 2)
|
2019-04-08 11:01:34 +02:00
|
|
|
range = ds.dates(1):ds.dates(end);
|
|
|
|
else
|
2020-02-03 18:12:14 +01:00
|
|
|
error('This routine admits 2, 3, 4, or 5 input arguments.')
|
2019-03-19 07:07:47 +01:00
|
|
|
end
|
|
|
|
|
2019-04-08 11:01:34 +02:00
|
|
|
for i=1:length(eqtags)
|
|
|
|
% Get equation
|
2020-02-03 18:12:14 +01:00
|
|
|
if isequal(i, 1)
|
|
|
|
if nargin<5
|
|
|
|
[LHS, RHS, json] = get_lhs_and_rhs(eqtags{i}, M_, true);
|
|
|
|
else
|
|
|
|
[LHS, RHS] = get_lhs_and_rhs(eqtags{i}, M_, true, json);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
[LHS, RHS] = get_lhs_and_rhs(eqtags{i}, M_, true, json);
|
|
|
|
end
|
2019-04-08 11:01:34 +02:00
|
|
|
% Parse equation and return list of parameters, endogenous and exogenous variables.
|
|
|
|
[pnames, enames, xnames] = get_variables_and_parameters_in_equation(LHS, RHS, M_);
|
|
|
|
% Load parameter values.
|
|
|
|
if ~isempty(pnames)
|
2019-07-17 17:59:21 +02:00
|
|
|
commands = sprintf('%s = %s;', pnames{1}, num2str(M_.params(strcmp(pnames{1}, M_.param_names)), 16));
|
2019-04-08 11:01:34 +02:00
|
|
|
for j=2:length(pnames)
|
2019-07-17 17:59:21 +02:00
|
|
|
commands = sprintf('%s %s = %s;', commands, pnames{j}, num2str(M_.params(strcmp(pnames{j}, M_.param_names)), 16));
|
2019-04-08 11:01:34 +02:00
|
|
|
end
|
|
|
|
eval(commands)
|
|
|
|
end
|
|
|
|
% Remove repetitions in enames
|
|
|
|
enames = unique(enames);
|
|
|
|
% Test if LHS is an endogenous variable
|
|
|
|
is_lhs_expression = ~ismember(LHS, enames);
|
|
|
|
if is_lhs_expression
|
|
|
|
variable = strsplit(LHS, list_of_expression_tokens);
|
|
|
|
variable(cellfun(@(x) all(isempty(x)), variable)) = [];
|
|
|
|
if length(variable)>1
|
|
|
|
error('It is not possible to have an expression with more than one variable on the LHS (%s).', LHS)
|
|
|
|
else
|
|
|
|
if isequal(LHS, sprintf('log(%s)', variable{1}))
|
|
|
|
transform = {'exp'};
|
|
|
|
elseif isequal(LHS, sprintf('diff(%s)', variable{1}))
|
|
|
|
transform = {'cumsum'};
|
|
|
|
elseif isequal(LHS, sprintf('diff(log(%s))', variable{1}))
|
|
|
|
transform = {'cumsum', 'exp'};
|
|
|
|
elseif isequal(LHS, sprintf('diff(diff(%s))', variable{1}))
|
|
|
|
transform = {'cumsum', 'cumsum'};
|
|
|
|
elseif isequal(LHS, sprintf('diff(diff(log(%s)))', variable{1}))
|
|
|
|
transform = {'cumsum', 'cumsum', 'exp'};
|
|
|
|
else
|
|
|
|
error('Cannot proceed with provided LHS (%s in %s)', LHS, eqtags{i})
|
|
|
|
end
|
|
|
|
lhs = variable{1};
|
|
|
|
end
|
2019-03-19 07:07:47 +01:00
|
|
|
else
|
2019-04-08 11:01:34 +02:00
|
|
|
lhs = LHS;
|
|
|
|
transform = {};
|
2019-03-19 07:07:47 +01:00
|
|
|
end
|
2019-04-08 11:01:34 +02:00
|
|
|
% Throw an error if the equation is dynamic.
|
|
|
|
if exactcontains(RHS, lhs)
|
|
|
|
error('RHS cannot contain LHS variable (%s in %s)', lhs, eqtags{i})
|
|
|
|
end
|
|
|
|
% Substitute endogenous variable x with ds.
|
|
|
|
for j=1:length(enames)
|
|
|
|
if ismember(enames{j}, ds.name)
|
|
|
|
RHS = exactstrrep(RHS, enames{j}, sprintf('ds(range).%s', enames{j}));
|
2019-10-02 10:38:56 +02:00
|
|
|
else
|
2020-02-12 17:21:00 +01:00
|
|
|
RHS = exactstrrep(RHS, sprintf('(%s\\((\\-)*\\d\\)|%s)', enames{j}, enames{j}), '0');
|
2020-02-03 18:12:14 +01:00
|
|
|
if debug
|
|
|
|
warning off backtrace
|
|
|
|
warning('Endogenous variable %s is unknown in dseries objet. Assign zero value.', enames{j})
|
|
|
|
warning on backtrace
|
|
|
|
end
|
2019-03-19 07:07:47 +01:00
|
|
|
end
|
2019-04-08 11:01:34 +02:00
|
|
|
end
|
|
|
|
% Substitute exogenous variable x with ds.x, except if
|
|
|
|
if ~isfield(M_, 'simulation_exo_names')
|
|
|
|
M_.simulation_exo_names = M_.exo_names;
|
|
|
|
end
|
|
|
|
xnames = unique(xnames);
|
|
|
|
for j=1:length(xnames)
|
|
|
|
if ismember(xnames{j}, M_.simulation_exo_names)
|
|
|
|
if ismember(xnames{j}, ds.name)
|
|
|
|
RHS = exactstrrep(RHS, xnames{j}, sprintf('ds(range).%s', xnames{j}));
|
|
|
|
else
|
|
|
|
RHS = exactstrrep(RHS, xnames{j}, '0');
|
2020-02-03 18:12:14 +01:00
|
|
|
if debug
|
|
|
|
warning off backtrace
|
|
|
|
warning('Exogenous variable %s is unknown in dseries objet. Assign zero value.', xnames{j})
|
|
|
|
warning on backtrace
|
|
|
|
end
|
2019-04-08 11:01:34 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
RHS = regexprep(RHS, sprintf('(\\ *)(+)(\\ *)%s', xnames{j}), '');
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if isempty(transform)
|
2019-04-11 10:13:34 +02:00
|
|
|
ds{LHS} = eval(RHS);
|
2019-03-19 07:07:47 +01:00
|
|
|
else
|
2019-04-08 11:01:34 +02:00
|
|
|
tmp = eval(RHS);
|
|
|
|
switch length(transform)
|
|
|
|
case 1
|
|
|
|
if isequal(transform{1}, 'cumsum')
|
2019-04-11 10:13:34 +02:00
|
|
|
ds{lhs} = cumsum(tmp)+ds{lhs}(range(1)-1).data;
|
2019-04-08 11:01:34 +02:00
|
|
|
else
|
2019-04-11 10:13:34 +02:00
|
|
|
ds{lhs} = feval(transform{1}, tmp);
|
2019-04-08 11:01:34 +02:00
|
|
|
end
|
|
|
|
case 2
|
|
|
|
if isequal(transform{2}, 'cumsum')
|
|
|
|
% Squared first difference.
|
|
|
|
t2 = zeros(length(range), 1);
|
|
|
|
for t = 1:length(range)
|
|
|
|
t2(t) = 2*ds{lhs}(range(t)-1).data-ds{lhs}(range(t)-2).data+tmp(range(t)).data;
|
|
|
|
end
|
2019-04-11 10:13:34 +02:00
|
|
|
ds{lhs} = dseries(t2, range(1));
|
2019-04-08 11:01:34 +02:00
|
|
|
else
|
|
|
|
t2 = zeros(length(range), 1);
|
|
|
|
for t = 1:length(range)
|
|
|
|
t1 = feval(transform{2}, log(ds{lhs}(range(t)-1))+tmp(range(t)).data );
|
|
|
|
t2(t) = t1.data;
|
|
|
|
end
|
2019-04-11 10:13:34 +02:00
|
|
|
ds{lhs} = dseries(t2, range(1));
|
2019-04-08 11:01:34 +02:00
|
|
|
% $$$ % The commented version below is more efficient but the discrepancy with what is returned by simulating
|
|
|
|
% $$$ % the model is much bigger (see pac/trend-component-28/example4.mod).
|
|
|
|
% $$$ tmp = cumsum(tmp)+log(ds{lhs}(range(1)-1).data);
|
2019-04-11 10:13:34 +02:00
|
|
|
% $$$ ds{lhs} = feval(transform{2}, tmp);
|
2019-04-08 11:01:34 +02:00
|
|
|
end
|
|
|
|
case 3
|
|
|
|
t2 = zeros(length(range), 1);
|
|
|
|
for t = 1:length(range)
|
|
|
|
t2(t) = feval(transform{3}, 2*log(ds{lhs}(range(t)-1).data)-log(ds{lhs}(range(t)-2).data)+tmp(range(t)).data);
|
|
|
|
end
|
2019-04-11 10:13:34 +02:00
|
|
|
ds{lhs} = dseries(t2, range(1));
|
2019-04-08 11:01:34 +02:00
|
|
|
otherwise
|
|
|
|
error('More than 3 unary ops. in LHS not implemented.')
|
|
|
|
end
|
2019-03-19 07:07:47 +01:00
|
|
|
end
|
2019-04-08 11:01:34 +02:00
|
|
|
end
|