From 4dbbdb2c09d8105186db377782c8ea9836d1bf07 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 15 Jan 2019 11:06:31 +0100 Subject: [PATCH] move common dates to parsing function --- matlab/ols/common_parsing.m | 12 +---- matlab/ols/get_ols_start_end_dates.m | 60 ------------------------ matlab/ols/parse_ols_style_equation.m | 67 ++++++++++++++++++++++----- 3 files changed, 57 insertions(+), 82 deletions(-) delete mode 100644 matlab/ols/get_ols_start_end_dates.m diff --git a/matlab/ols/common_parsing.m b/matlab/ols/common_parsing.m index 5c1422ac4..e5a06f59b 100644 --- a/matlab/ols/common_parsing.m +++ b/matlab/ols/common_parsing.m @@ -53,16 +53,8 @@ residnames = cell(neqs, 1); %% Loop over equations for i = 1:neqs - %% Parse equation i - [Y{i}, lhssub{i}, X{i}, residnames{i}] = parse_ols_style_equation(ds, ast{i}); - - %% Set start and end dates - [startdates{i}, enddates{i}] = get_ols_start_end_dates(Y{i}, lhssub{i}, X{i}, jsonmodel{i}); - Y{i} = Y{i}(startdates{i}:enddates{i}); - X{i} = X{i}(startdates{i}:enddates{i}); - if ~isempty(lhssub{i}) - lhssub{i} = lhssub{i}(startdates{i}:enddates{i}); - end + [Y{i}, lhssub{i}, X{i}, residnames{i}, startdates{i}, enddates{i}] = ... + parse_ols_style_equation(ds, ast{i}, jsonmodel{i}); end if overlapping_dates diff --git a/matlab/ols/get_ols_start_end_dates.m b/matlab/ols/get_ols_start_end_dates.m deleted file mode 100644 index 6f0e4263e..000000000 --- a/matlab/ols/get_ols_start_end_dates.m +++ /dev/null @@ -1,60 +0,0 @@ -function [fp, lp] = get_ols_start_end_dates(Y, lhssub, X, jsonmodel) -% function [fp, lp] = get_ols_start_end_dates(Y, lhssub, X, jsonmodel) -% Find the common first observed date and last observed date for X, Y, and -% lhssub. Impose sample tag if passed to equation -% -% INPUTS -% Y [cell array] dependent variables -% lhssub [cell array] RHS to subtract from Y -% X [cell array] regressors -% jsonmodel [cell array] JSON representation of model block -% -% OUTPUTS -% fp [date] first observed period -% lp [date] last observed period -% -% SPECIAL REQUIREMENTS -% none -% 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 . - -fp = max(Y.firstobservedperiod, X.firstobservedperiod); -lp = min(Y.lastobservedperiod, X.lastobservedperiod); -if ~isempty(lhssub) - fp = max(fp, lhssub.firstobservedperiod); - lp = min(lp, lhssub.lastobservedperiod); -end -if isfield(jsonmodel, 'tags') ... - && isfield(jsonmodel.tags, 'sample') ... - && ~isempty(jsonmodel.tags.sample) - colon_idx = strfind(jsonmodel.tags.sample, ':'); - fsd = dates(jsonmodel.tags.sample(1:colon_idx-1)); - lsd = dates(jsonmodel.tags.sample(colon_idx+1:end)); - if fp > fsd - warning(['The sample over which you want to estimate contains NaNs. '... - 'Adjusting estimation range to begin on: ' fp.char]) - else - fp = fsd; - end - if lp < lsd - warning(['The sample over which you want to estimate contains NaNs. '... - 'Adjusting estimation range to end on: ' lp.char]) - else - lp = lsd; - end -end -end diff --git a/matlab/ols/parse_ols_style_equation.m b/matlab/ols/parse_ols_style_equation.m index 860407c3d..cf7fbd97f 100644 --- a/matlab/ols/parse_ols_style_equation.m +++ b/matlab/ols/parse_ols_style_equation.m @@ -1,17 +1,20 @@ -function [Y, lhssub, X, residual] = parse_ols_style_equation(ds, ast) +function [Y, lhssub, X, residual, fp, lp] = parse_ols_style_equation(ds, ast, jsonmodel) %function X = parse_ols_style_equation() % Run OLS on chosen model equations; unlike olseqs, allow for time t % endogenous variables on LHS % % INPUTS -% ds [dseries] data -% ast [struct] AST representing the equation to be parsed +% ds [dseries] data +% ast [struct] AST representing the equation to be parsed +% jsonmodel [cell array] JSON representation of model block % % OUTPUTS -% Y [dseries] LHS of the equation (with lhssub subtracted) -% lhssub [dseries] RHS subtracted from LHS -% X [dseries] RHS of the equation -% residual [string] name of residual in equation +% Y [dseries] LHS of the equation (with lhssub subtracted) +% lhssub [dseries] RHS subtracted from LHS +% X [dseries] RHS of the equation +% residual [string] name of residual in equation +% fp [date] first common observed period between Y, lhssub, and X +% lp [date] last common observed period between Y, lhssub, and X % % SPECIAL REQUIREMENTS % none @@ -33,7 +36,8 @@ function [Y, lhssub, X, residual] = parse_ols_style_equation(ds, ast) % You should have received a copy of the GNU General Public License % along with Dynare. If not, see . -if nargin ~= 2 +%% Check inputs +if nargin ~= 3 error('parse_ols_style_equation takes 3 arguments') end @@ -41,8 +45,8 @@ if isempty(ds) || ~isdseries(ds) error('parse_ols_style_equation: arg 1 must be a dseries'); end -if ~isstruct(ast) || length(ast) ~= 1 - error('ast must be a celength must be equal to 1'); +if isempty(ast) || ~isstruct(ast) + error('ast must be a struct'); end line = ast.line; @@ -68,11 +72,15 @@ else end end -% Set LHS (Y) +if isempty(jsonmodel) || ~isstruct(jsonmodel) + error('jsonmodel must be a struct'); +end + +%% Set LHS (Y) lhssub = dseries(); Y = evalNode(ds, ast.AST.arg1, line, dseries()); -% Set RHS (X) +%% Set RHS (X) plus_node = ast.AST.arg2; last_node_to_parse = []; residual = ''; @@ -152,6 +160,41 @@ while ~isempty(plus_node) || ~isempty(last_node_to_parse) end end Y = Y - lhssub; + +%% Set start and end dates +fp = max(Y.firstobservedperiod, X.firstobservedperiod); +lp = min(Y.lastobservedperiod, X.lastobservedperiod); +if ~isempty(lhssub) + fp = max(fp, lhssub.firstobservedperiod); + lp = min(lp, lhssub.lastobservedperiod); +end + +% If it exists, account for tag set in mod file +if isfield(jsonmodel, 'tags') ... + && isfield(jsonmodel.tags, 'sample') ... + && ~isempty(jsonmodel.tags.sample) + colon_idx = strfind(jsonmodel.tags.sample, ':'); + fsd = dates(jsonmodel.tags.sample(1:colon_idx-1)); + lsd = dates(jsonmodel.tags.sample(colon_idx+1:end)); + if fp > fsd + warning(['The sample over which you want to estimate contains NaNs. '... + 'Adjusting estimation range to begin on: ' fp.char]) + else + fp = fsd; + end + if lp < lsd + warning(['The sample over which you want to estimate contains NaNs. '... + 'Adjusting estimation range to end on: ' lp.char]) + else + lp = lsd; + end +end + +Y = Y(fp:lp); +X = X(fp:lp); +if ~isempty(lhssub) + lhssub = lhssub(fp:lp); +end end %% Helper Functions