Commit Graph

324 Commits (master)

Author SHA1 Message Date
Sébastien Villemot 83693e4427
Bytecode: stop using unsigned integers where signed ones avoid unnecessary casts 2022-06-20 17:49:31 +02:00
Sébastien Villemot 5cd5676c8e
Bytecode: directly use several enums inside instruction classes 2022-06-20 15:19:08 +02:00
Sébastien Villemot a85730313c
Split CodeInterpreter.hh into two headers
– a generic one: CommonEnums.hh
– and a bytecode-specific one: Bytecode.hh

By the way, rename global constant “near_zero” into “power_deriv_near_zero”,
for clarity.
2022-06-16 17:53:23 +02:00
Sébastien Villemot 49277dbbf2
StaticModel::getDerivID() now throws an exception when arg is not endo or parameter
Previously it would return -1, which is bad practice.
2022-06-08 14:54:09 +02:00
Sébastien Villemot c94dfb848c
C++20 modernization: use initialization within range-based for loop
In particular, use this feature in many loops which feature a special treatment
for the first iteration, using a boolean variable (replacing iterator
manipulation). By the way, also use std::exchange() to simultaneously test the
value of this variable and update it.
2022-06-07 14:35:59 +02:00
Sébastien Villemot 9ace2dc413
C++17 modernization: take advantage of class template argument deduction 2022-06-07 14:35:26 +02:00
Sébastien Villemot 0b5c88109f
Various simplifications 2022-06-07 14:34:30 +02:00
Sébastien Villemot cec47cc78c
Implement bytecode compilation of 2nd deriv of external functions 2022-05-20 12:35:38 +02:00
Sébastien Villemot d1a3b227fd
Fix bug in bytecode compilation of 1st deriv of external functions 2022-05-20 12:35:23 +02:00
Sébastien Villemot 171cd65566
Fix bytecode compilation of external function nodes 2022-05-20 11:43:02 +02:00
Sébastien Villemot 701db61e09
JSON output: fix several bugs related to switch/case fallthrough 2022-05-18 16:33:42 +02:00
Sébastien Villemot 3e94250582
C++17 modernization: use std::optional in ExprNode::getPacOptimizingShareAndExprNodes() 2022-05-16 19:10:16 +02:00
Sébastien Villemot 0eb11d3323
C++17 modernization: use std::optional in expression matching functions 2022-05-16 19:10:16 +02:00
Sébastien Villemot fa7a926143
C++17 modernization: use std::optional for the storage of orig_symb_id and orig_lead_lag in SymbolTable
For the diff and unaryOp auxvar types, these value may be set or unset
depending on the complexity of the expression represented by the auxvar.
2022-05-16 19:10:15 +02:00
Sébastien Villemot 0b51294994
C++17 modernization: use std::optional for trend variables in TCM 2022-05-16 19:10:15 +02:00
Sébastien Villemot b42ee40b55
C++20 modernization: use contains() method on containers 2022-05-04 17:37:57 +02:00
Sébastien Villemot ef02b79486
Various minor simplifications 2022-05-04 17:36:59 +02:00
Sébastien Villemot 621134b8d8
Julia: more adaptation of DynamicSetAuxiliarySeries.jl for TimeDataFrame objects
– also use vectorized versions for .+ and .-
– add a space before vectorized operators to avoid syntactical ambiguity

This commits complements bfdcc546ec.
2022-04-08 15:06:29 +02:00
Sébastien Villemot bfdcc546ec
Julia: adapt DynamicSetAuxiliarySeries.jl for TimeDataFrame objects 2022-04-01 17:34:37 +02:00
Sébastien Villemot 71edfd05e4
Add new “log” option to “var” statement
When an endogenous is declared with “var(log)”, say “y”:
– creates an auxiliary named “LOG_y”
– replaces “y(±l)” everywhere by “exp(LOG_y(±l))”
– adds a new auxiliary equation “y=exp(LOG_y)”
– adds a new definition “LOG_y=log(y)” in set_auxiliary_variables.m and
  dynamic_set_auxiliary_series.m files

This option also works in conjunction with “deflator=…”, such as “var(log,
deflator=…)” (the “log” must appear befor “deflator”). There are no provisions
for combining “log” with “log_deflator”, because that would not make much sense
from an economic point of view (amounts to taking the log two times).

Ref. dynare#349
2022-03-30 17:46:07 +02:00
Sébastien Villemot adab6c7f93
Comment improvement + cosmetics 2022-01-28 17:24:48 +01:00
Sébastien Villemot 01bea3f5e7
TCM: remove useless 3rd dimension of A0 and A0star (supposedly for lags) 2022-01-28 16:38:50 +01:00
Sébastien Villemot 1e77f7c5a7
SymbolTable::getOrigSymbIdForAuxVar() now also works on unaryOp and diffForward auxvars
This is a more natural semantics.

Incidentally, this fixes a bug in the variable mapping (M_.mapping) where some
endogenous, appearing in a log() in a VAR or TCM, would not be mentioned (e.g.
in the var-expectations/7/example1.mod test, and many others).
2022-01-28 16:30:11 +01:00
Sébastien Villemot a93e264c2c
Harmonize “diffForward” auxvar with “diff” auxvar by giving it an orig_lead_lag as well
By the way:
– Fix and improve the explanation of the purpose of the orig_symb_id and
  orig_lead_lag fields for auxvars
– Factorize the code that prints those fields in MATLAB and JSON output
2022-01-28 15:53:41 +01:00
Sébastien Villemot 289b4773d7
Fix matching of diff leads/lags in TCM and PAC models
The logic was flawed in several ways. In particular, the test files
pac/trend-component-{3,10,11}/example1.mod would return A0 and A0star matrices
where the (2,2) element was incorrectly zero.
2022-01-28 15:53:41 +01:00
Sébastien Villemot 2c84d500d9
Add GDB pretty-printer for ExprNode class hierarchy 2022-01-26 15:40:40 +01:00
Sébastien Villemot 96e2a8f971
Fix orig_lead_lag field for diff lag aux vars 2022-01-19 15:52:46 +01:00
Sébastien Villemot a71100b971
Fix comment 2022-01-19 15:41:37 +01:00
Sébastien Villemot 0d7d5f1ae7
In case of nested unary ops (e.g. log(log())), ensure that the definition of the external aux var references the internal aux var 2022-01-19 15:40:52 +01:00
Sébastien Villemot 064a3dcd77
Fix orig_lead_lag field for unary op auxiliary variables
Ref. dynare#1843
2022-01-19 15:40:24 +01:00
Sébastien Villemot ea6fb40db7
PAC: new “pac_target_info” block and “pac_target_nonstationary” operator
Ref. Madysson/estimation-codes#5
2021-12-16 15:40:47 +01:00
Sébastien Villemot 342c4faf8d
New var_remove statement 2021-12-15 12:20:29 +01:00
Sébastien Villemot 1f004584e9
New model_remove statement and model_replace block
Factorize the implementation with the existing code for the include_eqs and
exclude_eqs options (which has been refactored by the way).

Ref. #19
2021-12-15 12:11:25 +01:00
MichelJuillard d047878333 Julia specific interface for normcdf and normpdf 2021-12-10 10:17:06 +01:00
Sébastien Villemot c0ea8d7203
Add hyperbolic primitives (cosh, sinh, tanh, acosh, asinh, atanh)
Everything was already in place (since ages!), except that the parser interface
was missing.

Also fix the derivation formula for atanh, which was incorrect.
2021-12-07 18:03:33 +01:00
Sébastien Villemot d15b2110a0
Add erfc() primitive
Closes: #85
2021-12-07 15:19:40 +01:00
Sébastien Villemot 81abea042b
Occbin: use element-by-element multiply, divide and power in occbin_difference.m
This file operates on vectors.

Ref. dynare#1826
2021-12-07 12:22:59 +01:00
Sébastien Villemot 5fe94ed606
Simplify constant equations also in equations marked [static]
The simplifyEquations() methods had to be moved to DynamicModel, in order to
access the static_only_equations member.
2021-12-06 17:17:37 +01:00
Sébastien Villemot 1cc512962c
No longer consider an equation of the form “x(+1) = 0” as defining x to be a constant
Fix this by restricting the detection of constant equations to those where the
variable appears without a lead or lag.

Closes: #83
2021-11-19 18:01:11 +01:00
Sébastien Villemot 6ad3d02ec6
Minor simplification in ExprNode::matchLinearCombinationOfVariables() 2021-10-29 15:39:40 +02:00
Sébastien Villemot c4e9432eef
Remove implementation of various methods of PacExpectationNode
Those implementations are dummy and are normally never used. Rather error out
on those, factorizing them within SubModelNode.
2021-10-29 14:11:30 +02:00
Sébastien Villemot 3e242dd481
New class SubModelNode to factorize between VarExpectationNode and PacExpectationNode 2021-10-29 14:11:30 +02:00
Sébastien Villemot 6ae3d3131f
Occbin: handle external functions in “occbin_constraints” 2021-07-21 16:33:19 +02:00
Sébastien Villemot 0d6eb053d4
Ensure that “pac_expectation” operators are never inserted into a static model
The implementation of PacExpectationNode::toStatic() did not make sense. It
should normally never be called, hence we error out at this point.

Also do a cosmetic change in the PacExpectationNode::clone() methode.
2021-07-21 16:33:14 +02:00
Sébastien Villemot b16ab9b247
Fix preprocessor crash when a [static] equation contains a “var_expectation” operator
Rather error out with a meaningful message.
2021-07-21 16:33:09 +02:00
Sébastien Villemot 81d4fd5d83
Occbin: generate +<basename>/occbin_difference.m
In particular, this implies that steady state values of endogenous in the
“occbin_constraints” block must now be specified using the STEADY_STATE()
operator (and not with a “_ss” suffix).

Moreover:
– make various simplifications to the fields generated under M_
– in the driver file, replace the call to occbin.initialize() by a few explicit operations

Ref. #68
2021-07-20 12:26:11 +02:00
Sébastien Villemot 90b1235a64
New utility to check if an expression contains an exogenous (possibly deterministic) 2021-07-20 12:10:58 +02:00
Sébastien Villemot 365fb27f3d
New “structural” option to “var_model”
As the name implies, this option allows contemporaneous variables on the RHS.

The A0 matrix for contemporaneous variables is added as a second (optional)
output to the generated var_ar.m file. Note that for reduced-form VAR, this
matrix will be the identity.

Also, the user is now allowed to write the VAR models in a more flexible form:
the LHS must still be a single variable, but the RHS can be an arbitrary
expression (as long as it is linear, obviously). Internally, the preprocessor
now uses derivation to compute the coefficients of the AR and A0. This change
applies to both reduced-form and structural VAR models.

Ref. dynare#1785
2021-07-08 12:55:47 +02:00
Sébastien Villemot 67f4777ac3
Remove support for obsolete syntax: var_model(order = 2) X Y Z; 2021-07-07 18:26:09 +02:00
Sébastien Villemot 8df34c1ca5
VAR and trend component models: improve sanity checks on lead/lags on RHS
In particular, forbid lagged exogenous.
2021-07-06 18:52:38 +02:00
Sébastien Villemot 74ef3457b4
Use secure URL for link to GNU licenses 2021-06-09 17:21:00 +02:00
Sébastien Villemot 825b9ee8ef
Fix bugs introduced in d3e115194b
Ref. #65
2021-02-19 20:29:18 +01:00
Sébastien Villemot ca4b1ba4ed
Remove time subscript in LaTeX output of model-local variables
See https://forum.dynare.org/t/tex-output-unwanted-subscript-t/16072
2021-02-19 17:45:58 +01:00
Sébastien Villemot d3e115194b
Fix output write_latex_original_model for model-local variables
Those variables were substituted out by early substitution methods (which are
also called on the original model, for an unknown reason).

Closes: #65
2021-02-19 17:45:02 +01:00
Sébastien Villemot cfb41d291c
Substitute out model-local variables early in the model transform pass
Incidentally, this makes it possible to use model-local variables with either
the “block” or “bytecode” option. (Ref: dynare#1243)
2020-11-10 18:07:57 +01:00
Sébastien Villemot 56581b1dd4
PAC model: make detection of non-optimizing part more robust
Introduce a new method for decomposing a product of factors, so that we can
identify expressions of the form (1-optim_share)*A*B.

Also enforce that the optim_share parameter be in a factor of the form
1-optim_share (previously it would accept any expression containing the
parameter).

Note that this fix does not yet allow to actually write non-optimizing parts of
the form (1-optim_share)*A*B, since at a later point the preprocessor imposes
that this part be a linear combination of variables (but in the future we could
think of expanding the A*B product into a linear combination if, for example, A
is a paramater or a constant and B is a linear combination).

Closes: #50
2020-10-19 18:32:30 +02:00
Sébastien Villemot d2cbe2a19e
PAC model: fixes to M_.pac.MODEL.ar (information about autoregressive part)
– Fix order of items in this structure. Previously, items were ordered
  according to the declaration order of parameters. Now, items are order
  according to lag order (first lag appears first)
– Gracefully handle the case where there is no autoregressive part
  (Closes: #52)
2020-10-16 16:31:51 +02:00
Sébastien Villemot e736b9234f
Minor simplification 2020-10-07 17:15:53 +02:00
Sébastien Villemot 5979885714
Block decomposition, chain rule derivation: code improvement 2020-10-02 18:31:55 +02:00
Sébastien Villemot d347a92f14
Drop ExprNode::containsExogenous()
Replace it by calls to ExprNode::collectVariables() at the single place where
it was used.
2020-09-29 18:41:31 +02:00
Sébastien Villemot 2424b7358c
Remove unused ExprNode::collectExogenous() 2020-09-29 18:41:31 +02:00
Sébastien Villemot b0bd031a9f
Remove unused ExprNode::containsEndogenous() 2020-09-29 18:41:31 +02:00
Sébastien Villemot 7f05489639
Simplify ExprNode::findConstantEquations() 2020-09-29 18:41:31 +02:00
Sébastien Villemot a377fc8398
Fix various potential bugs with model local variables
Many recursive function on ExprNodes were not correctly recursing into the
definition of model local variables.
2020-09-29 18:41:17 +02:00
Sébastien Villemot e4687bb976
Ensure that negative constants are never marked as temporary terms 2020-09-23 17:28:53 +02:00
Sébastien Villemot 8f31e443d6
SymbolTable::getTypeSpecificID() now throws an exception instead of returning -1 when symbol does not have a type-specific ID
This can potentially avoid some undetected bugs.
2020-09-23 17:09:36 +02:00
Houtan Bastani 7ba77751c3
simplify external function C output 2020-09-18 17:05:54 +02:00
Sébastien Villemot 09054e11dc
Add new “matched_moments” block
Closes: #51
2020-07-28 18:30:24 +02:00
Sébastien Villemot ecf6d67e1c
PAC/MCE model: fix timing of target in PAC expectation term
Also be more strict on the form of the target (must now be X(-1) or log(X(-1))
where X is *not* an auxiliary variable).

By the way, improve some comments in SymbolTable.
2020-07-24 18:26:16 +02:00
Sébastien Villemot 3b3589b958
PAC model: rewrite the algorithm for detecting the target, to make it more robust
Closes: #54
2020-07-23 17:19:16 +02:00
Sébastien Villemot 16f9168fda
PAC MCE: fix incorrect detection of the target variable
The detection of the target EC variable to be used when constructing the
forward-looking expectation variable is rather fragile.

When the PAC model is written with an (non-)optimizing share of agents,
restrict the identification of the target variable to the optimizing
expression, to minimize the risk of wrong identification.

By the way, add a few comments, and a small simplification.
2020-07-13 17:52:42 +02:00
Sébastien Villemot 5b80a4db59
Unary ops substitution: bugfix, actually restrict the substitution to the chosen equations
By default, the preprocessor is supposed to only do the “unary ops”
transformation in the equations of VAR/PAC/trend component models.

However, the implementation was slightly different so far. It would detect
candidates to this transformation in the chosen equations, but it would then
perform the substitution in *all* equations.

This could lead for crashes, for example if the chosen equation contains
log(X(-1)), but another (non-chosen) equation has log(X(-2)). Then this latter
expression, even though it belongs to the same lag-equivalence class, is not
properly handled, causing a segfault.

Also do a few related cosmetic changes.
2020-07-08 15:13:14 +02:00
Sébastien Villemot fd13c03401
use_dll: use fmax() and fmin() from C99 to compute max and min operators 2020-06-23 15:59:35 +02:00
Sébastien Villemot 79763911b2
Block decomposition: in the dynamic file, y and T are now vectors as in non-block mode 2020-06-17 16:49:12 +02:00
Sébastien Villemot 7b9495bb2f
JSON output: when writing the model after computing pass, use TEF terms for (not derived) external functions 2020-06-05 16:10:38 +02:00
Sébastien Villemot faa6666abe
Block decomposition: no longer use global variables for temporary terms in the dynamic M-file
Rather use a single vector as in non-block mode.

By the way, change the order of output arguments in static functions, to be
closer to the dynamic ones.
2020-05-26 16:21:11 +02:00
Sébastien Villemot b9216be277
Block decomposition: fix incorrect check with model local variables
We normally never arrive here, since MLV are not compatible with 'block'
option.
2020-05-25 15:42:16 +02:00
Sébastien Villemot 3033417f5a
Block decomposition: remove ExprNodeOutputType::matlabDynamicSparseSteadyStateOperator
It’s not really needed, since it does the same as
ExprNodeOutputType::matlabDynamicSteadyStateOperator.
2020-05-25 15:35:55 +02:00
Sébastien Villemot a2bea00fee
Block decomposition: another fix related to temporary terms refactoring
Temporary terms need to be computed per equation (as was done previously), and
not simply per block.

It’s necessary to track temporary terms per equation, because some equations
are evaluated instead of solved, and an equation E1 may depend on the value of
an endogenous Y computed by a previously evaluated equation E2; in this case,
if some temporary term TT of equation E2 contains Y, then TT needs to be
computed after E1, but before E2.
2020-05-13 16:58:19 +02:00
Sébastien Villemot 2e6bc7ecaa
Bytecode: fix handling of temporary terms
They were broken by 49036f166d
2020-05-13 12:53:47 +02:00
Sébastien Villemot f8796c67c5
ExprNode::computeTemporaryTerms: minor simplifications, for symmetry with blocks code 2020-05-12 18:39:15 +02:00
Sébastien Villemot 49036f166d
Block decomposition: refactor code for computing temporary terms
In particular, in dynamic models, temporary terms are now computed for
derivatives w.r.t. exogenous, and also w.r.t. endogenous variables that do not
belong to the block.
2020-05-12 18:39:13 +02:00
Sébastien Villemot 45b260cf20
VariableNode::getChainRuleDerivative: do not cache values in ExprNode::derivatives
This field is used for standard derivatives. Using it also for chain rule
derivatives can only lead to wrong results.
2020-04-24 19:06:30 +02:00
Sébastien Villemot e48e761b94
Block decomposition: various minor cleanups 2020-04-23 14:41:18 +02:00
Houtan Bastani 8b6e42559d
fix typo
[skip ci]
2020-04-21 13:08:27 -04:00
Sébastien Villemot daa8d01686
Complete rewrite of the equation normalization symbolic engine 2020-04-02 19:19:57 +02:00
Sébastien Villemot e88c05e3b8
Change prototype of DataTree::AddEqual()
This permits some simplifications.
2020-04-02 19:19:57 +02:00
Sébastien Villemot fb72472ee0
Block decomposition: fix several bugs in computation of renormalized equations
This would only hit for mfs=2 or mfs=3.
2020-03-30 18:39:25 +02:00
Sébastien Villemot 50776a766a
Remove the unsignedness of many integer variables and function arguments
This was only adding unneeded complexity, for no clear reason (we’re very far
from reaching 2³¹ equations, and if we wanted to support models that large, it
would be better to use long integers to avoid being limited to 2³²).
2020-03-24 18:28:03 +01:00
Sébastien Villemot 097bdfde03
Misc simplifications and cosmetics 2020-03-20 18:42:59 +01:00
Stéphane Adjemian (Charybdis) 2479a2a78c
Fixed bug introduced in 985d742.
If a float smaller than one in absolute value is written without a
leading zero in the mod file, for instance as `.5`, we really need to
prefix the number with a zero. The simplest approach is to convert
the strings representing the numerical constants into floats.
2020-01-28 18:11:55 +01:00
Stéphane Adjemian (Charybdis) 985d742381
Removed prefixing of doubles between -1 and 1 with 0.
This resulted in values like `00.018` in the AST's NumConstNode which
cannot be read by some JSON readers (e.g. Mathworks' jsondecode).
2020-01-27 16:45:21 +01:00
Houtan Bastani d2860c4f6b
place braces directly around latex name
issue dynare#263
2020-01-07 15:58:00 +01:00
Sébastien Villemot e2d5a83592
Global reindentation 2019-12-20 16:59:30 +01:00
Sébastien Villemot 8b23a2d575
Fix incorrect static model when “diff” operator is present
The “diff” operator was incorrectly replaced by its argument in the static
model, leading to an incorrect result for the steady state.

This is because the information contained in the “expr_node” field of the
auxiliary variables storage was not consistent across all types of auxiliary
variables: for a “diff()” operator, it would contain the argument of the
operator, instead of the full node. Hence it would not simplify to zero at the
steady state.

A similar inconsistency was also present for the “expectation()” operator,
though it was not leading to an incorrect static model.
2019-12-19 16:50:40 +01:00
Sébastien Villemot 1a08cdf01a
BinaryOpNode::findConstantEquations(): fix bug related to nested if/else
In the absence of braces, the last “else” clause is always associated with the
closest “if”, which is not what was intended here. The indentation was
misleading.
2019-12-18 16:32:59 +01:00
Sébastien Villemot 031c0cff5f
Various code simplifications and modernizations 2019-12-16 19:42:59 +01:00
Houtan Bastani c1b8cdf811
epilogue: simplify _static file
issue #36
2019-12-12 16:32:12 +01:00
Houtan Bastani 44adcbb488
epilogue: replace `dseries__` with `ds` making the file easier to read 2019-12-12 14:46:45 +01:00
Houtan Bastani dfcd381d20
epilogue: fix bug with external functions in `epilogue` block
issue #36
2019-12-12 14:45:49 +01:00