Commit 9aab3aa7 authored by Houtan Bastani's avatar Houtan Bastani

update blog post

parent 5b0f01a5
......@@ -8,8 +8,9 @@ afv2013table1/*.mat
afv2013table1/*.json
afv2013table1/afv2013table1/*
sw2007/Smets_Wouters_2007/*
sw2007/Smets_Wouters_2007*.m
sw2007/Smets_Wouters_2007*.mat
sw2007/Smets_Wouters_2007*.log
sw2007/Smets_Wouters_2007*.json
content/sw2007/Smets_Wouters_2007/*
content/sw2007/+Smets_Wouters_2007/*
content/sw2007/*.log
content/sw2007/*.png
content/sw2007/*.dat
content/sw2007/*.mat
[submodule "dynare"]
path = dynare
url = url = ../../Dynare/dynare.git
url = https://git.dynare.org/Dynare/dynare.git
[submodule "theme"]
path = theme
url = https://git.nomics.world/macro/theme-bootstrap3.git
......
......@@ -11,6 +11,6 @@ To run the code described in the model, do the following:
- Download the latest [snapshot of dynare](http://www.dynare.org/snapshot/) for your system. The code below will work with any version of Dynare newer than the version pointed to by the submodule in this repository.
- Open Matlab or Octave and type:
- `addpath <<path to dynare/matlab folder>>` where `dynare` points to the snapshot you downloaded
- `addpath <<path to obsmacro-dynare-json/ols>>`
- `addpath <<path to obsmacro-dynare-json/content/ols>>`
- `cd <<path to obsmacro-dynare-json/afv2013table1/>>`
- `dynare afv2013table1.mod`
This diff is collapsed.
function [Y, lhssub, X, startdates, enddates, residnames] = common_parsing(ds, ast, overlapping_dates, param_names)
%function [Y, lhssub, X, startdates, enddates, residnames] = common_parsing(ds, ast, overlapping_dates, param_names)
%
% Code common to sur.m and pooled_ols.m
%
% INPUTS
% ds [dseries] dataset
% ast [cell array] JSON representation of abstract syntax tree
% overlapping_dates [boolean] if true, dates are same across equations
%
% OUTPUTS
% Y [cell array] dependent variables
% lhssub [cell array] RHS subtracted from LHS
% X [cell array] regressors
% startdates [cell array] first observed period for each
% equation
% enddates [cell array] last observed period for each
% equation
% startidxs [vector] rows corresponding to each
% equation's observations
% residnames [cell array] name of residual in each equation
% param_names [cellstr or cell] list of parameters to estimate (if
% empty, estimate all)
%
% 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 <http://www.gnu.org/licenses/>.
global M_
%% Initialize variables
neqs = length(ast);
Y = cell(neqs, 1);
lhssub = cell(neqs, 1);
X = cell(neqs, 1);
startdates = cell(neqs, 1);
enddates = cell(neqs, 1);
residnames = cell(neqs, 1);
%% Loop over equations
for i = 1:neqs
[Y{i}, lhssub{i}, X{i}, residnames{i}, startdates{i}, enddates{i}] = ...
parse_ols_style_equation(ds, ast{i});
end
if overlapping_dates
maxfp = max([startdates{:}]);
minlp = min([enddates{:}]);
for i = 1:neqs
Y{i} = Y{i}(maxfp:minlp);
if ~isempty(X{i})
X{i} = X{i}(maxfp:minlp);
end
if ~isempty(lhssub{i})
lhssub{i} = lhssub{i}(maxfp:minlp);
end
startdates{i} = maxfp;
enddates{i} = minlp;
end
end
if ~isempty(param_names)
if iscell(param_names) && ~iscellstr(param_names)
assert(length(param_names) == neqs, 'error w param_names arg');
else
pn = param_names;
pn_found_cellstr = false(length(param_names), 1);
end
for i = 1:neqs
if iscell(param_names) && ~iscellstr(param_names)
pn = param_names{i};
pn_found_cellstr_i = false(length(pn), 1);
end
names = X{i}.name;
newlhssub = dseries();
for j = 1:length(names)
idx = find(strcmp(names{j}, pn));
if isempty(idx)
pval = M_.params(strcmp(names{j}, M_.param_names));
if isnan(pval) || isinf(pval)
error(['Could not find param init value for ' names{j}])
end
newlhssub = newlhssub + pval * X{i}.(names{j});
X{i} = X{i}.remove(names{j});
else
if iscell(param_names) && ~iscellstr(param_names)
pn_found_cellstr_i = true;
else
pn_found_cellstr(idx) = true;
end
end
end
Y{i} = Y{i} - newlhssub;
lhssub{i} = lhssub{i} + newlhssub;
if iscell(param_names) && ~iscellstr(param_names)
if ~all(pn_found_cellstr_i)
error(['parameters specified in param_names (' ...
strjoin(pn(~pn_found_cellstr_i), ', ') ...
') were not found in eq ' num2str(i) ' to be estimated'])
end
end
end
if ~(iscell(param_names) && ~iscellstr(param_names))
if ~all(pn_found_cellstr)
error(['parameters specified in param_names (' ...
strjoin(pn(~pn_found_cellstr), ', ') ...
') were not found in the equation(s) to be estimated'])
end
end
end
end
This diff is collapsed.
......@@ -17,7 +17,7 @@ function dyn_table(title, preamble, afterward, rows, cols, indent, data)
% SPECIAL REQUIREMENTS
% none
% Copyright (C) 2017 Dynare Team
% Copyright (C) 2017-2019 Dynare Team
%
% This file is part of Dynare.
%
......@@ -53,20 +53,20 @@ colrow = sprintf('%s', colbegin);
colrow2 = colrow;
format = [' %-' num2str(maxrowstrlen) 's'];
for i = 1:length(cols)
precision = 12;
if colstrlens(i) < precision
colrow = [colrow repmat(' ', 1, floor((precision-mod(colstrlens(i), precision))/2)) cols{i} repmat(' ', 1, ceil((precision-mod(colstrlens(i), precision))/2))];
colrow2 = [colrow2 repmat('_', 1, precision)];
field_width = 16;
if colstrlens(i) < field_width
colrow = [colrow repmat(' ', 1, floor((field_width-mod(colstrlens(i), field_width))/2)) cols{i} repmat(' ', 1, ceil((field_width-mod(colstrlens(i), field_width))/2))];
colrow2 = [colrow2 repmat('_', 1, field_width)];
else
colrow = [colrow cols{i}];
colrow2 = [colrow2 repmat('_', 1, colstrlens(i))];
precision = colstrlens(i);
field_width = colstrlens(i);
end
if i ~= length(cols)
colrow = [colrow repmat(' ', 1, indent)];
colrow2 = [colrow2 repmat(' ', 1, indent)];
end
format = [format repmat(' ', 1, indent) '%' sprintf('%d.5f', precision)];
format = [format repmat(' ', 1, indent) '%' sprintf('%d.5g', field_width)];
end
% Center title
......
function [jsonmodel] = getEquationsByTags(jsonmodel, tagname, tagvalue)
%function [jsonmodel] = getEquationsByTags(jsonmodel, tagname, tagvalue)
% Return the jsonmodel structure with the matching tags
function [ast] = getEquationsByTags(ast, tagname, tagvalue)
%function [ast] = getEquationsByTags(ast, tagname, tagvalue)
% Return the ast structure with the matching tags
%
% INPUTS
% jsonmodel [cell array] JSON representation of model block
% tagname [string] The name of the tag whos values are to
% be selected
% tagvalue [string] The values to be selected for the
% provided tagname
% ast [cell array] JSON representation of model block
% tagname [string] The name of the tag whos values are to
% be selected
% tagvalue [string] The values to be selected for the
% provided tagname
%
% OUTPUTS
% jsonmodel [cell array] JSON representation of model block,
% with equations removed that don't match
% eqtags
% ast [cell array] JSON representation of model block,
% with equations removed that don't match
% eqtags
%
% SPECIAL REQUIREMENTS
% none
% Copyright (C) 2017-2018 Dynare Team
% Copyright (C) 2017-2019 Dynare Team
%
% This file is part of Dynare.
%
......@@ -34,24 +34,34 @@ function [jsonmodel] = getEquationsByTags(jsonmodel, tagname, tagvalue)
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
assert(nargin == 3, 'Incorrect number of arguments passed to getEquationsByTags');
assert(iscell(jsonmodel) && ~isempty(jsonmodel), ...
'the first argument must be a cell array of structs');
assert(ischar(tagname), 'Tag name must be a string');
assert(ischar(tagvalue) || iscell(tagvalue), 'Tag value must be a string or a cell string array');
if nargin ~= 3
error('Incorrect number of arguments passed to function')
end
if isempty(ast) || ~iscell(ast)
error('the first argument must be a cell array of structs');
end
if ~ischar(tagname)
error('Tag name must be a string');
end
if ~ischar(tagvalue) && ~iscell(tagvalue)
error('Tag value must be a string or a cell string array');
end
if ischar(tagvalue)
tagvalue = {tagvalue};
end
idx2keep = [];
for i=1:length(tagvalue)
for i = 1:length(tagvalue)
found = false;
for j=1:length(jsonmodel)
assert(isstruct(jsonmodel{j}), 'Every entry in jsonmodel must be a struct');
if isfield(jsonmodel{j}, 'tags') && ...
isfield(jsonmodel{j}.tags, tagname) && ...
strcmp(jsonmodel{j}.tags.(tagname), tagvalue{i})
for j=1:length(ast)
assert(isstruct(ast{j}), 'Every entry in the ast must be a struct');
if isfield(ast{j}, 'tags') && ...
isfield(ast{j}.tags, tagname) && ...
strcmp(ast{j}.tags.(tagname), tagvalue{i})
idx2keep = [idx2keep; j];
found = true;
break
......@@ -62,5 +72,5 @@ for i=1:length(tagvalue)
end
end
assert(~isempty(idx2keep), 'getEquationsByTags: no equations selected');
jsonmodel = jsonmodel(unique(idx2keep, 'stable'));
ast = ast(unique(idx2keep, 'stable'));
end
function retval = getStrMoveRight(str)
%function retval = getStrMoveRight(str)
% Helper function common to OLS routines. Given a string finds expr
% moving from left to right (potentially contained in parenthesis) until
% it gets to the + or -. e.g., given: str =
% '(log(DE_SIN(-1))-log(DE_SIN))+anotherparam1*log(DE_SIN)', returns
% '(log(DE_SIN(-1))-log(DE_SIN))'
function [ast, jsonmodel] = get_ast(eqtags)
% Returns the Abstract Syntax Tree the JSON output of preprocessor for the
% given equation tags. Equations are ordered in eqtag order
%
% INPUTS
% str [string] string
% - eqtags [cellstr] names of equation tags for which to get info.
% If empty, get all equations
%
% OUTPUTS
% none
% - ast [cell array] JSON representation of the abstract syntax tree
% - jsonmodel [cell array] JSON representation of the equations.
%
% SPECIAL REQUIREMENTS
% none
% Copyright (C) 2017 Dynare Team
% Copyright (C) 2019 Dynare Team
%
% This file is part of Dynare.
%
......@@ -32,38 +31,28 @@ function retval = getStrMoveRight(str)
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
if isempty(str)
retval = '';
return
global M_
if ~isempty(eqtags) && ~(ischar(eqtags) || iscell(eqtags))
error('eqtags not passed correctly')
end
mathidxs = regexp(str, '[\+\-]');
openidxs = strfind(str, '(');
if isempty(openidxs) ...
|| (~isempty(mathidxs) ...
&& min(mathidxs) < min(openidxs))
if isempty(mathidxs)
retval = str;
else
retval = str(1:min(mathidxs)-1);
end
else
openidxs = [(1:length(openidxs))' openidxs'];
closedidxs = strfind(str, ')');
closedidxs = [(1:length(closedidxs))' closedidxs'];
assert(rows(openidxs) == rows(closedidxs));
for i = 1:rows(closedidxs)
closedparenidx = sum(openidxs(:, 2) < closedidxs(i, 2));
if openidxs(closedparenidx, 1) == closedidxs(i, 1)
break;
end
end
retval = str(1:closedidxs(closedparenidx, 2));
if length(str) > closedidxs(closedparenidx, 2) + 1
if any(strfind('*^/', str(closedidxs(closedparenidx, 2) + 1)))
retval = [retval ...
str(closedidxs(closedparenidx, 2) + 1) ...
getStrMoveRight(str(closedidxs(closedparenidx, 2) + 2:end))];
end
end
jsonfile = sprintf('%s%smodel%sjson%smodfile-original.json', M_.fname, filesep(), filesep(), filesep());
if ~exist(jsonfile, 'file')
error('Could not find %s! Please use the json=compute option (see the Dynare invocation section in the reference manual).', jsonfile)
end
tmp = loadjson(jsonfile);
ast = tmp.abstract_syntax_tree;
if nargout>1
jsonmodel = tmp.model;
end
if ~isempty(eqtags)
ast = getEquationsByTags(ast, 'name', eqtags);
if nargout>1
jsonmodel = getEquationsByTags(jsonmodel, 'name', eqtags);
end
end
\ No newline at end of file
This diff is collapsed.
function retval = getStrMoveLeft(str)
%function retval = getStrMoveLeft(str)
% Helper function common to OLS routines. Given a string finds expr
% moving from right to left (potentially contained in parenthesis) until
% it gets to the + or -. e.g., given: str =
% 'res_9+DE_SIN*param+log(DE_SIN(-1))', returns 'log(DE_SIN)'
function write_param_init_inc_file(subfolder, fnameroot, idxs, estimated_params)
%function write_param_init_inc_file(fname, idxs, estimated_params)
% Write .inc files that initialize parameters
%
% INPUTS
% str [string] string
% subfolder [string] subfolder in which to place file
% fnameroot [string] root of filename
% idxs [vector] indexes in M_.params of estimated parameters
% estimated_params [vector] estimated parameters
%
% OUTPUTS
% none
......@@ -14,7 +14,7 @@ function retval = getStrMoveLeft(str)
% SPECIAL REQUIREMENTS
% none
% Copyright (C) 2017 Dynare Team
% Copyright (C) 2019 Dynare Team
%
% This file is part of Dynare.
%
......@@ -31,45 +31,55 @@ function retval = getStrMoveLeft(str)
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
if isempty(str)
retval = '';
return
global M_
%% Check inputs
if nargin ~= 4
error('function takes 4 arguments')
end
if ~ischar(subfolder)
error([subfolder ' must be a string']);
end
if ~ischar(fnameroot)
error([fnameroot ' must be a string']);
end
if ~isvector(idxs) ...
|| ~isvector(estimated_params) ...
|| length(idxs) ~= length(estimated_params)
error('the two arguments must be vectors of the same length')
end
mathidxs = regexp(str, '[\+\-]');
closedidxs = strfind(str, ')');
if isempty(closedidxs) ...
|| (~isempty(mathidxs) ...
&& max(mathidxs) > max(closedidxs))
if isempty(mathidxs)
retval = str;
else
if str(max(mathidxs)) == '-'
retval = str(max(mathidxs):end);
else
retval = str(max(mathidxs)+1:end);
end
end
else
closedidxs = [(length(closedidxs):-1:1)' closedidxs'];
openidxs = strfind(str, '(');
openidxs = [(length(openidxs):-1:1)' openidxs'];
assert(rows(closedidxs) == rows(openidxs));
for i = rows(openidxs):-1:1
openparenidx = find(openidxs(i, 2) < closedidxs(:, 2), 1, 'first');
if openidxs(i, 1) == closedidxs(openparenidx, 1)
break
end
end
retval = str(openidxs(openparenidx, 2):end);
if openidxs(openparenidx, 2) ~= 1
if isempty(regexp(str(openidxs(openparenidx, 2) - 1), '[\+\-]', 'once'))
retval = [getStrMoveLeft(str(1:openidxs(openparenidx, 2) - 2)) ...
str(openidxs(openparenidx, 2) - 1) ...
retval];
end
end
if strfind(str, ['-' retval])
retval = ['-' retval];
end
%% Write file
% Open
filepath = [M_.fname filesep 'model' filesep subfolder];
if ~exist(filepath, 'dir')
mkdir(filepath)
end
fname = [filepath filesep fnameroot '-declare.inc'];
fid = fopen(fname, 'w');
if fid < 0
error(['couldn''t open file' fname])
end
% Write declaration
fprintf(fid, 'parameters');
for i = 1:length(idxs)
fprintf(fid, ' %s', M_.param_names{idxs(i)});
end
fprintf(fid, ';\n');
fclose(fid);
fname = [filepath filesep fnameroot '-initialize.inc'];
fid = fopen(fname, 'w');
if fid < 0
error(['couldn''t open file' fname])
end
for i = 1:length(idxs)
fprintf(fid, '%s = %f;\n', M_.param_names{idxs(i)}, estimated_params(i));
end
fclose(fid);
end
......@@ -248,7 +248,7 @@ ds1 = dseries();
ds1.r = dseries(oo_.FilteredVariables.r);
ds1.pinf = dseries(oo_.FilteredVariables.pinf);
ds1.ygap = dseries(oo_.FilteredVariables.ygap);
dyn_ols(ds1, {}, {'taylor_rule'});
dyn_ols(ds1, {}, {'taylor_rule'}, {'second'});
figure
plot(ds1.ygap.data)
......
Subproject commit 71996fd077d5b6f0a546e9638af6046ad3ad0443
Subproject commit 87ba69644e8c9024b26dded9dbbbe3b648fc518f
function lhssub = getRhsToSubFromLhs(ds, rhs, regex, splits, pnames)
%function lhssub = getRhsToSubFromLhs(ds, rhs, regex, splits, pnames)
% Helper function that identifies variables on RHS that need to be
% subtracted from LHS of OLS-style equation
%
% INPUTS
% ds [dseries] data
% rhs [string] RHS as a string
% regex [string] regex expressing valid list of variables
% splits [cell string] strings to split out of equation on RHS
% pnames [cell string] parameter names
%
% OUTPUTS
% lhssub [dseries] summed data to subtract from LHS
%
% SPECIAL REQUIREMENTS
% none
% Copyright (C) 2017-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 <http://www.gnu.org/licenses/>.
global M_
assert(isdseries(ds), 'The first argument must be a dseries');
assert(ischar(rhs), 'The second argument must be a string');
assert(ischar(regex), 'The third argument must be a string');
assert(iscellstr(splits), 'The fourth argument must be a cell');
assert(iscellstr(splits), 'The fourth argument must be a cell');
lhssub = dseries();
rhs_ = strsplit(rhs, splits);
for j = 1:length(rhs_)
rhsj = rhs_{j};
while ~isempty(rhsj)
minusstr = '';
if strcmp(rhsj(1), '-') || strcmp(rhsj(1), '+')
if length(rhsj) == 1
break
end
if strcmp(rhsj(1), '-')
minusstr = '-';
end
rhsj = rhsj(2:end);
end
str = getStrMoveRight(rhsj);
if ~isempty(str)
try
lhssub = lhssub + eval(regexprep([minusstr str], regex, 'ds.$&'));
catch
if ~any(strcmp(M_.exo_names, str)) && ~any(strcmp(pnames, str))
error(['getRhsToSubFromLhs: problem evaluating ' minusstr str]);
end
end
rhsj = rhsj(length(str)+1:end);
end
end
end
if ~isempty(lhssub)
assert(lhssub.vobs == 1, 'error in getRhsToSubFromLhs');
lhssub.rename_(lhssub.name{:}, 'summed_rhs');
end
end
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment