diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 582b3642a5e1e5f212e0d2355d09ffb73d8886cc..5e2fec121079707586ae257306442ff33d34f234 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -2,7 +2,7 @@ variables: GIT_SUBMODULE_STRATEGY: recursive TERM: linux MATLAB_VERSION: R2020a - OLD_MATLAB_VERSION: R2009b + OLD_MATLAB_VERSION: R2014a # The next stanza creates the version number used for the source tarball and the # binary packages. Here are the following possible cases: @@ -85,12 +85,10 @@ pkg_windows: cache: key: "$CI_JOB_NAME-$CI_COMMIT_REF_SLUG" paths: - - windows/deps/sources32/ - windows/deps/sources64/ - - windows/deps/lib32/ - windows/deps/lib64/ - # We do not cache lib{32,64}-msys2, mingw{32,64}, octave{32,64} and - # matlab{32,64}, because those are simply extracted from a tarball. It + # We do not cache lib64-msys2, mingw64, octave64 and + # matlab64, because those are simply extracted from a tarball. It # would be a waste of space and of (re-compression) time. artifacts: paths: @@ -232,7 +230,7 @@ deploy_release_enterprise: - pkg_windows - pkg_macOS script: - - f=(windows/exe/*) && osslsigncode sign -pkcs12 ~/dynare-object-signing.p12 -n Dynare -i https://www.dynare.org -t http://timestamp.digicert.com -in ${f[0]} -out ${f[0]}.signed && mv ${f[0]}.signed ${f[0]} + - f=(windows/exe/*) && osslsigncode sign -pkcs12 ~/cepremap-comodo-sectigo-code-signing.p12 -n Dynare -i https://www.dynare.org -t http://timestamp.comodoca.com -in ${f[0]} -out ${f[0]}.signed && mv ${f[0]}.signed ${f[0]} - cp *.tar.xz /srv/www.dynare.org/release_ecb/source/ - cp windows/exe/* /srv/www.dynare.org/release_ecb/windows/ - cp windows/7z/* /srv/www.dynare.org/release_ecb/windows-7z/ diff --git a/NEWS.md b/NEWS.md index 8ca144dfddcee1b37b1aea882b2156be4547aa80..1e48180a19b411b5f71ec81ba584c2cfd1591926 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,84 @@ +Announcement for Dynare 4.6.2 (on 2020-09-07) +============================================= + +We are pleased to announce the release of Dynare 4.6.2. + +This maintenance release fixes various bugs. + +The Windows, macOS and source packages are already available for download at +[the Dynare website](https://www.dynare.org/download/). + +All users are strongly encouraged to upgrade. + +This release is compatible with MATLAB versions ranging from 7.9 (R2009b) to +9.8 (R2020a), and with GNU Octave versions 5.2.0 (under Windows) and 4.4.1 +(under macOS). + +*Note for Windows users:* upon launching the Dynare installer, you may get a +warning emitted by Windows Defender SmartScreen, saying that this is an +unrecognized app and that it was prevented from starting. You can safely ignore +this warning, as long as you can verify on the next screen that CEPREMAP is the +editor of the software. This security warning is due to the fact that we had to +renew our code signing certificate (which had expired), and it takes some time +to rebuild our reputation as a software editor using the new certificate. + +Here is a list of the problems identified in version 4.6.1 and that have been +fixed in version 4.6.2: + +* Perfect foresight simulations of purely backward models could deliver an + incorrect result if some exogenous variable appeared with a lag of 2 or more + (and neither `block` nor `bytecode` option was used) +* Perfect foresight simulations of linear models could deliver an incorrect + result if the following four conditions were met: + + the model was actually declared as linear through the `linear` option + + there was an exogenous variable with a lead or a lag + + `stack_solve_algo` was equal to 0 (the default) or 7 + + neither `block` nor `bytecode` option was used +* In stochastic simulations, for variables that actually do not leave the + steady state, reported simulated moments could be spurious (due to division + by zero) +* Displayed variance decompositions would only take into account measurement + errors if measurement errors were present for all observed variables +* The posterior variance decompositions with measurement errors computed with + `moments_varendo` were incorrect +* `moments_varendo` would not update `oo_.PosteriorTheoreticalMoments` if it + was already present, from *e.g.* an earlier run of `estimation` +* Identification would in some cases compute wrong Jacobian of moments +* Identification would display incorrect results if parameter dependence was + implemented via a steady state file +* `generate_trace_plots` would crash when measurement errors were present +* `estimation` would crash for correlated measurement errors +* Parallel execution/testing could crash instead of aborting with a proper + error message +* Under macOS, Dynare would incorrectly claim that it is compiled for Octave + 5.2.0 (it is actually compiled for Octave 4.4.1) +* Using external functions in a model local variable would crash the + preprocessor +* Tolerance criteria for steady state computations were inconsistently set +* `stoch_simul` with its default `order=2` would crash with a message about + `hessian_eq_zero` not existing if an explicit `order=1` was present somewhere + else in the `.mod` file +* Model local variables were not written to the `modfile.json` JSON file +* Model local variables names would have two spurious underscores at their + point of definition in the `dynamic.json` and `static.json` files (but only + in the definition, not when they were used, which is inconsistent) +* The `solve_algo=9` option was not accessible. The `solve_algo=10` and + `solve_algo=11` options were not accessible with `block` (without `bytecode`) +* Under certain circumstances, `extended_path` would crash when used in + conjunction with the `block` option +* `extended_path` was not working with the `bytecode` option +* `shock_decomposition` was not accepting the options of `estimation` related + to smoothing +* `conditional_forecast` would display a warning even if the simulation was + successful +* The `prior_trunc` option of `identification` was not working +* The `rand_multivariate_student` value of the `proposal_distribution` option + was not working when used with the + `tailored_random_block_metropolis_hastings` posterior sampling method +* Perfect foresight simulations of backward models would crash if convergence + failed with complex-valued residuals +* The diffuse Kalman smoother would crash if `Finf` became singular + Announcement for Dynare 4.6.1 (on 2020-03-13) ============================================= diff --git a/doc/bvar-a-la-sims.tex b/doc/bvar-a-la-sims.tex index b58b83b75e431e7ee71832b94a1d97074db406e6..6bc0e849303ae01ef08a6dde2a1a0614d639d5af 100644 --- a/doc/bvar-a-la-sims.tex +++ b/doc/bvar-a-la-sims.tex @@ -26,7 +26,7 @@ }} \author{S\'ebastien Villemot\thanks{Paris School of Economics and - CEPREMAP.} \and Johannes Pfeifer\thanks{University of Cologne. E-mail: \href{mailto:jpfeifer@uni-koeln.de}{\texttt{jpfeifer@uni-koeln.de}}.}} + CEPREMAP.} \and Johannes Pfeifer\thanks{Universität der Bundeswehr München. E-mail: \href{mailto:johannes.pfeifer@unibw.de}{\texttt{johannes.pfeifer@unibw.de}}.}} \date{First version: September 2007 \hspace{1cm} This version: May 2017} \maketitle diff --git a/doc/manual/source/index.rst b/doc/manual/source/index.rst index 2df8469ff13979e968b830c8e4ab6350a4aec1f1..8c01b1220238c3f098e6e5bdfac2559c2d3d9408 100644 --- a/doc/manual/source/index.rst +++ b/doc/manual/source/index.rst @@ -4,7 +4,7 @@ The Dynare Reference Manual, version |version| Currently the development team of Dynare is composed of: * Stéphane Adjemian (Université du Maine, Gains) -* Houtan Bastani (CEPREMAP) +* Houtan Bastani * Michel Juillard (Banque de France) * Sumudu Kankanamge (Toulouse School of Economics) * Frédéric Karamé (Université du Maine, Gains and CEPREMAP) @@ -12,7 +12,7 @@ Currently the development team of Dynare is composed of: * Junior Maih (Norges Bank) * Ferhat Mihoubi (Université Paris-Est Créteil, Érudite and CEPREMAP) * Willi Mutschler (University of Münster) -* Johannes Pfeifer (University of Cologne) +* Johannes Pfeifer (Universität der Bundeswehr München) * Marco Ratto (European Commission, Joint Research Centre - JRC) * Sébastien Villemot (CEPREMAP) diff --git a/doc/manual/source/installation-and-configuration.rst b/doc/manual/source/installation-and-configuration.rst index 49651c57992b41cc1320b7f66a6ba1cf547ec67d..0de229401d8fe8ec4d86025fe8118fee4e4153b0 100644 --- a/doc/manual/source/installation-and-configuration.rst +++ b/doc/manual/source/installation-and-configuration.rst @@ -14,7 +14,7 @@ are necessary in that case. In order to run Dynare, you need one of the following: -* MATLAB version 7.9 (R2009b) or above; +* MATLAB version 8.3 (R2014a) or above; * GNU Octave version 4.4 or above, with the statistics package from `Octave-Forge`_. Note however that the Dynare installers for Windows and macOS require a more specific version of Octave, as indicated on the download diff --git a/doc/manual/source/running-dynare.rst b/doc/manual/source/running-dynare.rst index d8bd2b45e503d7dd8877a8def5413da16f220900..48ffbbb37d0f08aa1fd03ef1bf595c34a5ea684a 100644 --- a/doc/manual/source/running-dynare.rst +++ b/doc/manual/source/running-dynare.rst @@ -229,7 +229,8 @@ by the ``dynare`` command. .. option:: json = parse|check|transform|compute Causes the preprocessor to output a version of the ``.mod`` file in - JSON format. When the JSON output is created depends on the value + JSON format to ``<<M_.dname>>/model/json/``. + When the JSON output is created depends on the value passed. These values represent various steps of processing in the preprocessor. @@ -257,7 +258,8 @@ by the ``dynare`` command. .. option:: jsonstdout Instead of writing output requested by ``json`` to files, - write to standard out. + write to standard out, i.e. to the MATLAB/Octave command window + (and the log-file). .. option:: onlyjson diff --git a/doc/manual/source/the-model-file.rst b/doc/manual/source/the-model-file.rst index bfc1428d13380bea8d8fcabc0256186db68dfa94..8cfc1a14869141e5e7b70ddccdb0df394e886d06 100644 --- a/doc/manual/source/the-model-file.rst +++ b/doc/manual/source/the-model-file.rst @@ -740,7 +740,9 @@ required for external functions used in an EXPRESSION outside of a provided as the only output of the M-/MEX file given as the option argument. If NAME is not provided, this tells Dynare that the M-/MEX file specified by the argument passed to NAME - returns the Jacobian as its second output argument. + returns the Jacobian as its second output argument. When this option is + not provided, Dynare will use finite difference approximations for + computing the derivatives of the function, whenever needed. .. option:: second_deriv_provided [= NAME] @@ -750,7 +752,9 @@ required for external functions used in an EXPRESSION outside of a that the M-/MEX file specified by the argument passed to NAME returns the Hessian as its third output argument. NB: This option can only be used if the ``first_deriv_provided`` option - is used in the same ``external_function`` command. + is used in the same ``external_function`` command. When this option is + not provided, Dynare will use finite difference approximations for + computing the Hessian derivatives of the function, whenever needed. *Example* @@ -1502,7 +1506,7 @@ in this case ``initval`` is used to specify the terminal conditions. perfect_foresight_setup(periods=200); perfect_foresight_solver; - + In this example, the problem is finding the optimal path for consumption and capital for the periods :math:`t=1` to @@ -1769,7 +1773,7 @@ in this case ``initval`` is used to specify the terminal conditions. in the last ``initval`` or ``endval`` block (or the steady state file if you provided one, see :ref:`st-st`). -.. command:: initval_file (filename = FILENAME); +.. command:: initval_file (OPTIONS...); |br| In a deterministic setup, this command is used to specify a path for all endogenous and exogenous variables. The length of @@ -1786,33 +1790,534 @@ in this case ``initval`` is used to specify the terminal conditions. by the path for endogenous variables for the simulation periods (excluding initial and terminal conditions) - The command accepts three file formats: + In perfect foresight and stochastic contexts, ``steady`` uses the + first observation loaded by ``initval_file`` as guess value to + solve for the steady state of the model. This first observation is + determined by the ``first_obs`` option when it is used. + + Don’t mix ``initval_file`` with ``initval`` statements. However, + after ``initval_file``, you can modify the historical initial + values with ``histval`` or ``histval_file`` statement. + + There can be several ``initval_file`` statements in a model + file. Each statement resets ``oo_.initval_series``. + + *Options* + + .. option:: datafile = FILENAME + filename = FILENAME (deprecated) + + The name of the file containing the data. It must be included in quotes if the filename + contains a path or an extension. The command accepts the following file formats: * M-file (extension ``.m``): for each endogenous and exogenous variable, the file must contain a row or column vector of - the same name. Their length must be ``periods + - M_.maximum_lag + M_.maximum_lead`` + the same name. * MAT-file (extension ``.mat``): same as for M-files. * Excel file (extension ``.xls`` or ``.xlsx``): for each - endogenous and exogenous, the file must contain a column of - the same name. NB: Octave only supports the ``.xlsx`` file - extension and must have the `io`_ package installed (easily - done via octave by typing ‘``pkg install -forge io``’). + endogenous and exogenous variable, the file must contain a + column of the same name. NB: Octave only supports the + ``.xlsx`` file extension and must have the `io`_ package + installed (easily done via octave by typing ‘``pkg + install -forge io``’). The first column may contain the date + of each observation. + * CSV files (extension ``.csv``): for each endogenous and + exogenous variable, the file must contain a column of the + same name. The first column may contain the date of each + observation. + + .. option:: first_obs = {INTEGER | DATE} + + The observation number or the date (see + :ref:`dates-members`) of the first observation to be used in the file + + .. option:: first_simulation_period = {INTEGER | DATE} + + The observation number in the file or the date (see + :ref:`dates <dates-members>`) at which the simulation (or the forecast) is + starting. This option avoids to have to compute the maximum + number of lags in the model. The observation corresponding to + the first period of simulation doesn’t need to exist in the + file as the only dates necessary for initialization are before + that date. + + .. option:: last_obs = {INTEGER | DATE} + + The observaton number or the date (see + :ref:`dates-members`) of the last observation to be used in + the file. + + .. option:: nobs = INTEGER + + The number of observations to be used in the file (starting + with first of ``first_obs`` observation). + + .. option:: series = DSERIES NAME + + The name of a DSERIES containing the data (see :ref:`dseries-members`) + + *Example 1* + + :: + + var c x; + varexo e; + parameters a b c d; + + a = 1.5; + b = -0,6; + c = 0.5; + d = 0.5; + + model; + x = a*x(-1) + b*x(-2) + e; + log(c) = c*x + d*log(c(+1)); + end; + + initval_file(datafile=mydata.csv); + + perfect_foresight_setup(periods=200); + perfect_foresight_solver; + + The initial and terminal values are taken from file + ``mydata.csv`` (nothing guarantees that these vales are the + steady state of the model). The guess value for the + trajectories are also taken from the file. The file must + contain at least 203 observations of variables ``c``, ``x`` + and ``e``. If there are more than 203 observations available + in the file, the first 203 are used by + ``perfect_foresight_setup(periods=200)``. + Note that the values for the auxiliary variable corresponding + to ``x(-2)`` are automatically computed by ``initval_file``. + + *Example 2* + + :: + + var c x; + varexo e; + parameters a b c d; + + a = 1.5; + b = -0,6; + c = 0.5; + d = 0.5; + + model; + x = a*x(-1) + b*x(-2) + e; + log(c) = c*x + d*log(c(+1)); + end; + + initval_file(datafile=mydata.csv, + first_obs=10); + + perfect_foresight_setup(periods=200); + perfect_foresight_solver; + + The initial and terminal values are taken from file + ``mydata.csv`` starting with the 10th observation in the + file. There must be at least 212 observations in the file. + + *Example 3* + + :: + + var c x; + varexo e; + parameters a b c d; + + a = 1.5; + b = -0,6; + c = 0.5; + d = 0.5; + + model; + x = a*x(-1) + b*x(-2) + e; + log(c) = c*x + d*log(c(+1)); + end; + + ds = dseries(mydata.csv); + lds = log(ds); + + initval_file(series=lds, + first_obs=2010Q1); + + perfect_foresight_setup(periods=200); + perfect_foresight_solver; + + The initial and terminal values are taken from dseries + ``lds``. All observations are loaded starting with the 1st quarter of + 2010 until the end of the file. There must be data available + at least until 2050Q3. + + *Example 4* + + :: - .. warning:: The extension must be omitted in the command - argument. Dynare will automatically figure out the - extension and select the appropriate file type. If - there are several files with the same name but different - extensions, then the order of precedence is as follows: - first ``.m``, then ``.mat``, ``.xls`` and finally ``.xlsx``. + var c x; + varexo e; + parameters a b c d; + + a = 1.5; + b = -0,6; + c = 0.5; + d = 0.5; + + model; + x = a*x(-1) + b*x(-2) + e; + log(c) = c*x + d*log(c(+1)); + end; + + initval_file(datafile=mydata.csv, + first_simulation_period=2010Q1); + + perfect_foresight_setup(periods=200); + perfect_foresight_solver; + The initial and terminal values are taken from file + ``mydata.csv``. The observations in the file must have + dates. All observations are loaded from the 3rd quarter of + 2009 until the end of the file. There must be data available + in the file at least until 2050Q1. -.. command:: histval_file (filename = FILENAME); + *Example 5* + + :: + + var c x; + varexo e; + parameters a b c d; + + a = 1.5; + b = -0,6; + c = 0.5; + d = 0.5; + + model; + x = a*x(-1) + b*x(-2) + e; + log(c) = c*x + d*log(c(+1)); + end; + + initval_file(datafile=mydata.csv, + last_obs = 212); + + perfect_foresight_setup(periods=200); + perfect_foresight_solver; + + The initial and terminal values are taken from file + ``mydata.csv``. The first 212 observations are loaded and the + first 203 observations will be used by + ``perfect_foresight_setup(periods=200)``. + + *Example 6* + + :: + + var c x; + varexo e; + parameters a b c d; + + a = 1.5; + b = -0,6; + c = 0.5; + d = 0.5; + + model; + x = a*x(-1) + b*x(-2) + e; + log(c) = c*x + d*log(c(+1)); + end; + + initval_file(datafile=mydata.csv, + first_obs = 10, + nobs = 203); + + perfect_foresight_setup(periods=200); + perfect_foresight_solver; + + The initial and terminal values are taken from file + ``mydata.csv``. Observations 10 to 212 are loaded. + + *Example 7* + + :: + + var c x; + varexo e; + parameters a b c d; + + a = 1.5; + b = -0,6; + c = 0.5; + d = 0.5; + + model; + x = a*x(-1) + b*x(-2) + e; + log(c) = c*x + d*log(c(+1)); + end; + + initval_file(datafile=mydata.csv, + first_obs = 10); + + steady; + + The values of the 10th observation of ``mydata.csv`` are used + as guess value to compute the steady state. The exogenous + variables are set to values found in the file or zero if these + variables aren't present. + +.. command:: histval_file (OPTIONS...); |br| This command is equivalent to ``histval``, except that it reads its input from a file, and is typically used in conjunction with ``smoother2histval``. + *Options* + + .. option:: datafile = FILENAME + filename = FILENAME (deprecated) + + The name of the file containing the data. The command accepts + the following file formats: + + * M-file (extension ``.m``): for each endogenous and exogenous + variable, the file must contain a row or column vector of + the same name. + * MAT-file (extension ``.mat``): same as for M-files. + * Excel file (extension ``.xls`` or ``.xlsx``): for each + endogenous and exogenous variable, the file must contain a + column of the same name. NB: Octave only supports the + ``.xlsx`` file extension and must have the `io`_ package + installed (easily done via octave by typing ‘``pkg + install -forge io``’). The first column may contain the + date of each observation. + * CSV files (extension ``.csv``): for each endogenous and + exogenous variable, the file must contain a column of the + same name. The first column may contain the date of each + observation. + + .. option:: first_obs = {INTEGER | DATE} + + The observation number or the date (see :ref:`dates-members`) of + the first observation to be used in the file + + .. option:: first_simulation_period = {INTEGER | DATE} + + The observation number in the file or the date (see + :ref:`dates-members`) at which the simulation (or the forecast) is + starting. This option avoids to have to compute the maximum + number of lags in the model. The observation corresponding to + the first period of simulation doesn’t need to exist in the + file as the only dates necessary for initialization are before + that date. + + .. option:: last_obs = {INTEGER | DATE} + + The observation number or the date (see :ref:`dates-members`) of the + last observation to be used in the file. + + .. option:: nobs = INTEGER + + The number of observations to be used in the file (starting + with first of ``first_obs`` observation). + + .. option:: series = DSERIES NAME + + The name of a DSERIES containing the data (see :ref:`dseries-members`) + + *Example 1* + + :: + + var c x; + varexo e; + parameters a b c d; + + a = 1.5; + b = -0,6; + c = 0.5; + d = 0.5; + + model; + x = a*x(-1) + b*x(-2) + e; + log(c) = c*x + d*log(c(+1)); + end; + + steady_state_model; + x = 0; + c = exp(c*x/(1 - d)); + end; + + histval_file(datafile=mydata.csv); + + stoch_simul(order=1,periods=100); + + The initial values for the stochastic simulation are taken + from the two first rows of file ``mydata.csv``. + + *Example 2* + + :: + + var c x; + varexo e; + parameters a b c d; + + a = 1.5; + b = -0,6; + c = 0.5; + d = 0.5; + + model; + x = a*x(-1) + b*x(-2) + e; + log(c) = c*x + d*log(c(+1)); + end; + + histval_file(datafile=mydata.csv, + first_obs=10); + + stoch_simul(order=1,periods=100); + + The initial values for the stochastic simulation are taken + from rows 10 and 11 of file ``mydata.csv``. + + + *Example 3* + + :: + + var c x; + varexo e; + parameters a b c d; + + a = 1.5; + b = -0,6; + c = 0.5; + d = 0.5; + + model; + x = a*x(-1) + b*x(-2) + e; + log(c) = c*x + d*log(c(+1)); + end; + + histval_file(datafile=mydata.csv, + first_obs=2010Q1); + + stoch_simul(order=1,periods=100); + + The initial values for the stochastic simulation are taken + from observations 2010Q1 and 2010Q2 of file ``mydata.csv``. + + *Example 4* + + :: + + var c x; + varexo e; + parameters a b c d; + + a = 1.5; + b = -0,6; + c = 0.5; + d = 0.5; + + model; + x = a*x(-1) + b*x(-2) + e; + log(c) = c*x + d*log(c(+1)); + end; + + histval_file(datafile=mydata.csv, + first_simulation_period=2010Q1) + + stoch_simul(order=1,periods=100); + + The initial values for the stochastic simulation are taken + from observations 2009Q3 and 2009Q4 of file ``mydata.csv``. + + *Example 5* + + :: + + var c x; + varexo e; + parameters a b c d; + + a = 1.5; + b = -0,6; + c = 0.5; + d = 0.5; + + model; + x = a*x(-1) + b*x(-2) + e; + log(c) = c*x + d*log(c(+1)); + end; + + histval_file(datafile=mydata.csv, + last_obs = 4); + + stoch_simul(order=1,periods=100); + + The initial values for the stochastic simulation are taken + from the two first rows of file ``mydata.csv``. + + *Example 6* + + :: + + var c x; + varexo e; + parameters a b c d; + + a = 1.5; + b = -0,6; + c = 0.5; + d = 0.5; + + model; + x = a*x(-1) + b*x(-2) + e; + log(c) = c*x + d*log(c(+1)); + end; + + initval_file(datafile=mydata.csv, + first_obs = 10, + nobs = 4); + + stoch_simul(order=1,periods=100); + + The initial values for the stochastic simulation are taken + from rows 10 and 11 of file ``mydata.csv``. + + *Example 7* + + :: + + var c x; + varexo e; + parameters a b c d; + + a = 1.5; + b = -0,6; + c = 0.5; + d = 0.5; + + model; + x = a*x(-1) + b*x(-2) + e; + log(c) = c*x + d*log(c(+1)); + end; + + initval_file(datafile=mydata.csv, + first_obs=10); + + histval_file(datafile=myotherdata.csv); + + perfect_foresight_setup(periods=200); + perfect_foresight_solver; + + Historical initial values for the simulation are taken from + the two first rows of file ``myotherdata.csv``. + + Terminal values and guess values for the simulation are taken + from file ``mydata.csv`` starting with the 12th observation in + the file. There must be at least 212 observations in the file. .. _shocks-exo: @@ -3334,7 +3839,8 @@ Computing the stochastic solution :mvar:`oo_.conditional_variance_decomposition_ME`). The variance decomposition is only conducted, if theoretical moments are requested, *i.e.* using the ``periods=0``-option. - In case of ``order=2``, Dynare provides a second-order accurate + Only available at ``order<3``. In case of ``order=2``, + Dynare provides a second-order accurate approximation to the true second moments based on the linear terms of the second-order solution (see *Kim, Kim, Schaumburg and Sims (2008)*). Note that the unconditional @@ -3530,9 +4036,10 @@ Computing the stochastic solution |br| After a run of ``stoch_simul``, contains the variance-covariance of the endogenous variables. Contains - theoretical variance if the ``periods`` option is not present (or - an approximation thereof for ``order=2``), and simulated variance - otherwise. The variables are arranged in declaration order. + theoretical variance if the ``periods`` option is not present and simulated variance + otherwise. Only available for ``order<4``. At ``order=2`` it will be be + a second-order accurate approximation. At ``order=3``, theoretical moments + are only available with ``pruning``. The variables are arranged in declaration order. .. matvar:: oo_.var_list @@ -3559,9 +4066,10 @@ Computing the stochastic solution number of the matrix in the cell array corresponds to the order of autocorrelation. The option ar specifies the number of autocorrelation matrices available. Contains theoretical - autocorrelations if the ``periods`` option is not present (or an - approximation thereof for ``order=2``), and simulated - autocorrelations otherwise. The field is only created if + autocorrelations if the ``periods`` option is not present and simulated + autocorrelations otherwise. Only available for ``order<4``. At ``order=2`` it will be be + a second-order accurate approximation. At ``order=3``, theoretical moments + are only available with ``pruning``. The field is only created if stationary variables are present. The element ``oo_.autocorr{i}(k,l)`` is equal to the correlation @@ -3599,9 +4107,10 @@ Computing the stochastic solution If a second order approximation has been requested, contains the vector of the mean correction terms. - In case ``order=2``, the theoretical second moments are a - second order accurate approximation of the true second - moments, see conditional_variance_decomposition. + Only available at ``order<4``. In case ``order=2``, the + theoretical second moments are a second order accurate + approximation of the true second moments. See conditional_variance_decomposition. + At ``order=3``, theoretical moments are only available with ``pruning``. .. matvar:: oo_.variance_decomposition @@ -3669,8 +4178,10 @@ Computing the stochastic solution |br| After a run of ``stoch_simul`` with the ``contemporaneous_correlation option``, contains theoretical contemporaneous correlations if the ``periods`` option is not - present (or an approximation thereof for ``order=2``), and - simulated contemporaneous correlations otherwise. The variables + present, and simulated contemporaneous correlations otherwise. + Only available for ``order<4``. At ``order=2`` it will be be + a second-order accurate approximation. At ``order=3``, theoretical moments + are only available with ``pruning``. The variables are arranged in declaration order. .. matvar:: oo_.SpectralDensity @@ -7037,25 +7548,25 @@ Shock Decomposition .. option:: prefilter = INTEGER See :opt:`prefilter <prefilter = INTEGER>`. - + .. option:: loglinear - + See :opt:`loglinear <loglinear>`. .. option:: diffuse_kalman_tol = DOUBLE See :opt:`diffuse_kalman_tol <diffuse_kalman_tol = DOUBLE>`. - + .. option:: diffuse_filter - + See :opt:`diffuse_filter <diffuse_filter>`. - + .. option:: xls_sheet = NAME See :opt:`xls_sheet <xls_sheet = NAME>`. .. option:: xls_range = RANGE - + See :opt:`xls_range <xls_range = RANGE>`. .. option:: use_shock_groups [= STRING] @@ -8343,11 +8854,11 @@ Optimal policy under commitment (Ramsey) This command only creates the expanded model, it doesn’t perform any computations. It needs to be followed by other instructions to - actually perform desired computations. Examples are calls to ``steady`` - to compute the steady state of the Ramsey economy, to ``stoch_simul`` - with various approximation orders to conduct stochastic simulations based on + actually perform desired computations. Examples are calls to ``steady`` + to compute the steady state of the Ramsey economy, to ``stoch_simul`` + with various approximation orders to conduct stochastic simulations based on perturbation solutions, to ``estimation`` in order to estimate models - under optimal policy with commitment, and to perfect foresight simulation + under optimal policy with commitment, and to perfect foresight simulation routines. See :ref:`aux-variables`, for an explanation of how Lagrange @@ -8387,12 +8898,12 @@ Optimal policy under commitment (Ramsey) ``steady_state_model`` block or in a ``_steadystate.m`` file). In this case, it is necessary to provide a steady state solution CONDITIONAL on the value of the instruments in the optimal policy - problem and declared with the option ``instruments``. The initial value - of the instrument for steady state finding in this case is set with - ``initval``. Note that computing and displaying steady state values - using the ``steady``-command or calls to ``resid`` must come after + problem and declared with the option ``instruments``. The initial value + of the instrument for steady state finding in this case is set with + ``initval``. Note that computing and displaying steady state values + using the ``steady``-command or calls to ``resid`` must come after the ``ramsey_model`` statement and the ``initval``-block. - + Note that choosing the instruments is partly a matter of interpretation and you can choose instruments that are handy from a mathematical point of view but different from the instruments you would refer @@ -8416,8 +8927,8 @@ Optimal policy under commitment (Ramsey) .. command:: evaluate_planner_objective ; - This command computes, displays, and stores the value of the - planner objective function + This command computes, displays, and stores the value of the + planner objective function under Ramsey policy in ``oo_.planner_objective_value``, given the initial values of the endogenous state variables. If not specified with ``histval``, they are taken to be at their steady state @@ -8443,7 +8954,7 @@ Optimal policy under commitment (Ramsey) ramsey_policy (OPTIONS...) [VARIABLE_NAME...]; |br| This command is formally equivalent to the calling sequence - + :: ramsey_model; @@ -8513,7 +9024,7 @@ Optimal policy under commitment (Ramsey) See :comm:`Ramsey steady state <ramsey_model>`. -Optimal policy under discretion +Optimal policy under discretion ------------------------------- .. command:: discretionary_policy [VARIABLE_NAME...]; @@ -9214,7 +9725,7 @@ Performing identification analysis as in *Ratto and Iskrev (2011)* Note that for orders 2 and 3, all identification checks are based on the pruned - state space system as in *Mutschler (2015)*. That is, theoretical moments and + state space system as in *Mutschler (2015)*. That is, theoretical moments and spectrum are computed from the pruned ABCD-system, whereas the minimal system criteria is based on the first-order system, but augmented by the theoretical (pruned) mean at order 2 or 3. @@ -11100,8 +11611,8 @@ Macro directives @#ifdef MACRO_VARIABLE @#ifndef MACRO_VARIABLE @#elseif MACRO_EXPRESSION - @#else ​ - @#endif ​ + @#else + @#endif |br| Conditional inclusion of some part of the ``.mod`` file. The lines between ``@#if``, ``@#ifdef``, or ``@#ifndef`` and the next ``@#elseif``, @@ -11189,7 +11700,7 @@ Macro directives @#for MACRO_VARIABLE in MACRO_EXPRESSION when MACRO_EXPRESSION @#for MACRO_TUPLE in MACRO_EXPRESSION @#for MACRO_TUPLE in MACRO_EXPRESSION when MACRO_EXPRESSION - @#endfor ​ + @#endfor |br| Loop construction for replicating portions of the ``.mod`` file. Note that this construct can enclose variable/parameters @@ -11262,7 +11773,7 @@ Macro directives |br| Asks the preprocessor to display some error message on standard output and to abort. The argument must evaluate to a string. -.. macrodir:: @#echomacrovars ​ +.. macrodir:: @#echomacrovars @#echomacrovars MACRO_VARIABLE_LIST @#echomacrovars(save) MACRO_VARIABLE_LIST diff --git a/doc/manual/utils/dynare_dom.py b/doc/manual/utils/dynare_dom.py index 82d83ead5cfdade10ea2366eb6ae81710d11fe76..765625b825f1f90187d252e4f59971a6bd32808d 100644 --- a/doc/manual/utils/dynare_dom.py +++ b/doc/manual/utils/dynare_dom.py @@ -34,7 +34,7 @@ from docutils.parsers.rst import Directive, directives from sphinx import addnodes from sphinx.domains import Domain, ObjType -from sphinx.locale import l_, _ +from sphinx.locale import _ from sphinx.directives import ObjectDescription from sphinx.roles import XRefRole from sphinx.util.nodes import make_refnode @@ -140,14 +140,14 @@ class DynCallable(DynObject): has_arguments = True doc_field_types = [ - TypedField('arguments', label=l_('Arguments'), + TypedField('arguments', label=_('Arguments'), names=('argument', 'arg', 'parameter', 'param'), typerolename='func', typenames=('paramtype', 'type')), - Field('returnvalue', label=l_('Returns'), has_arg=False, + Field('returnvalue', label=_('Returns'), has_arg=False, names=('returns', 'return')), - Field('returntype', label=l_('Return type'), has_arg=False, + Field('returntype', label=_('Return type'), has_arg=False, names=('rtype',)), - Field('example', label=l_('Example'), has_arg=False, + Field('example', label=_('Example'), has_arg=False, names=('ex',)), ] @@ -157,10 +157,10 @@ class DynClass(DynObject): allow_nesting = True doc_field_types = [ - TypedField('members', label=l_('Members'), + TypedField('members', label=_('Members'), names=('argument', 'arg', ), typerolename='func', typenames=('type', )), - Field('example', label=l_('Example'), has_arg=False, + Field('example', label=_('Example'), has_arg=False, names=('ex',)), ] @@ -291,22 +291,22 @@ class DynareDomain(Domain): name = 'dynare' label = 'Dynare' object_types = { - 'function': ObjType(l_('function'), 'func'), - 'datesmethod': ObjType(l_('method'), 'datmeth'), - 'dseriesmethod': ObjType(l_('method'), 'dsermeth'), - 'reportingmethod': ObjType(l_('method'), 'repmeth'), - 'matcomm': ObjType(l_('matlab command'), 'mcomm'), - 'command': ObjType(l_('command'), 'comm'), - 'class': ObjType(l_('class'), 'class'), - 'block': ObjType(l_('block'), 'bck'), - 'confblock': ObjType(l_('config block'), 'cbck'), - 'macrodir': ObjType(l_('macro directive'), 'mdir'), - 'construct': ObjType(l_('constructor'), 'cstr'), - 'matvar': ObjType(l_('matlab variable'), 'mvar'), - 'specvar': ObjType(l_('special variable'), 'svar'), - 'operator': ObjType(l_('operator'), 'op'), - 'constant': ObjType(l_('constant'), 'const'), - 'option': ObjType(l_('option'), 'opt'), + 'function': ObjType(_('function'), 'func'), + 'datesmethod': ObjType(_('method'), 'datmeth'), + 'dseriesmethod': ObjType(_('method'), 'dsermeth'), + 'reportingmethod': ObjType(_('method'), 'repmeth'), + 'matcomm': ObjType(_('matlab command'), 'mcomm'), + 'command': ObjType(_('command'), 'comm'), + 'class': ObjType(_('class'), 'class'), + 'block': ObjType(_('block'), 'bck'), + 'confblock': ObjType(_('config block'), 'cbck'), + 'macrodir': ObjType(_('macro directive'), 'mdir'), + 'construct': ObjType(_('constructor'), 'cstr'), + 'matvar': ObjType(_('matlab variable'), 'mvar'), + 'specvar': ObjType(_('special variable'), 'svar'), + 'operator': ObjType(_('operator'), 'op'), + 'constant': ObjType(_('constant'), 'const'), + 'option': ObjType(_('option'), 'opt'), } directives = { 'function': DynFunction, diff --git a/dynare++/doc/dynare++-tutorial.tex b/dynare++/doc/dynare++-tutorial.tex index eb75136104999d86c8824a8a8aae70f677f5a0c1..d94475a2a1da844d8e999fabdbe14604712846ae 100644 --- a/dynare++/doc/dynare++-tutorial.tex +++ b/dynare++/doc/dynare++-tutorial.tex @@ -10,7 +10,7 @@ \author{Ondra Kamen\'\i k} -\date{February 2011, updated August 2016} +\date{First version: February 2011 \\ This version: September 2020} \maketitle \tableofcontents @@ -18,7 +18,7 @@ \section{Setup} The Dynare++ setup procedure is pretty straightforward as Dynare++ is included in the Dynare installation -packages which can be downloaded from \url{http://www.dynare.org}. Take the following steps: +packages which can be downloaded from \url{https://www.dynare.org}. Take the following steps: \begin{enumerate} \item Add the {\tt dynare++} subdirectory of the root Dynare installation directory to the your operating system path. This ensures that your OS will find the {\tt dynare++} executable. @@ -159,10 +159,7 @@ file {\tt example1.jnl} and a MATLAB MAT-4 {\tt example1.mat}. The journal file contains information about time, memory and processor resources needed for all steps of solution. The output file is more interesting. It contains various simulation results. It can be loaded -into MATLAB or Scilab and examined.% -\footnote{For MATLAB {\tt load example1.mat}, for Scilab {\tt -mtlb\_load example1.mat}} The following examples are done in MATLAB, -everything would be very similar in Scilab. +into MATLAB or Octave and examined. Let us first examine the contents of the MAT file: {\small @@ -431,7 +428,7 @@ vcov = [ } After this model file has been run, we can load the resulting MAT-file -into the MATLAB (or Scilab) and examine its contents: +into the MATLAB and examine its contents: {\small \begin{verbatim} >> load kp1980_2.mat @@ -628,7 +625,11 @@ reported. They might be useful for confidence interval calculations. For each shock, Dynare++ calculates IRF for two impulses, positive and negative. Size of an impulse is one standard error of a respective -shock. +shock.\footnote{Note that if the exogenous shocks are correlated, Dynare++ will + ignore the correlation when computing the IRFs, and simulate the impulse on + each shock independently of the others. Note that Dynare behaves differently + in this case, and computes IRFs after performing an orthogonalization of the + shocks (via a Cholesky decomposition of the variance-covariance matrix).} The rest of this subsection is divided to three parts giving account on real-time simulations, conditional simulations, and on the way how @@ -924,7 +925,7 @@ This section deals with Dynare++ input. The first subsection \ref{dynpp_opts} provides a list of command line options, next subsection \ref{dynpp_mod} deals with a format of Dynare++ model file, and the last subsection discusses incompatibilities between Dynare -MATLAB and Dynare++. +and Dynare++. \subsection{Command Line Options} \label{dynpp_opts} @@ -1143,7 +1144,7 @@ $t+1$. The realization of $u_t$ is included in the information set of $E_t$. See an explanation of Dynare++ timing on page \pageref{timing}. \end{itemize} -The model equations are formulated in the same way as in MATLAB +The model equations are formulated in the same way as in Dynare. The time indexes different from $t$ are put to round parenthesis in this way: {\tt C(-1)}, {\tt C}, {\tt C(+1)}. @@ -1181,16 +1182,16 @@ Y-Y_SS = rho*(Y(-1)-Y_SS)+EPS; \end{verbatim} } -\subsection{Incompatibilities with MATLAB Dynare} +\subsection{Incompatibilities with Dynare} This section provides a list of incompatibilities between a model file -for Dy\-na\-re++ and MATLAB Dynare. These must be considered when a model -file for MATLAB Dynare is being migrated to Dynare++. The list is the +for Dy\-na\-re++ and for Dynare. These must be considered when a model +file for Dynare is being migrated to Dynare++. The list is the following: \begin{itemize} \item There is no {\tt periods} keyword. -\item The parameters cannot be lagged or leaded, I think that Dynare -MATLAB allows it, but the semantics is the same (parameter is a +\item The parameters cannot be lagged or leaded; Dynare +allows it, though the semantics is the same (parameter is a constant). \item There are no commands like {\tt steady}, {\tt check}, {\tt simul}, {\tt stoch\_simul}, etc. diff --git a/examples/NK_baseline.mod b/examples/NK_baseline.mod index 805f19e86e73154cc27040f469fbea2e6a7fcdd2..ad035e1b370a461bfb77a0b2ba56f475c81ae334 100644 --- a/examples/NK_baseline.mod +++ b/examples/NK_baseline.mod @@ -1,33 +1,33 @@ /* - * This file implements the Baseline New Keynesian DSGE model described in - * much detail in Jesús Fernández-Villaverde and Juan F. Rubio-Ramírez (2006): "A Baseline DSGE - * Model", available at http://economics.sas.upenn.edu/~jesusfv/benchmark_DSGE.pdf + * This file implements the Baseline New Keynesian DSGE model described in + * much detail in Jesús Fernández-Villaverde and Juan F. Rubio-RamÃrez (2006): “A Baseline DSGE + * Modelâ€, available at http://economics.sas.upenn.edu/~jesusfv/benchmark_DSGE.pdf * - * The parametrization is based on the estimated version of this model in - * Jesús Fernández-Villaverde (2010): "The econometrics of DSGE models", + * The parametrization is based on the estimated version of this model in + * Jesús Fernández-Villaverde (2010): “The econometrics of DSGE modelsâ€, * SERIEs, Vol. 1, pp. 3-49, DOI 10.1007/s13209-009-0014-7 * * This implementation was written by Benjamin Born and Johannes Pfeifer. In * case you spot mistakes, email us at jpfeifer@gmx.de * - * This mod-file implements a non-linearized version of the New Keynesian - * model based on a recursive formulation of the price and wage setting - * equations. Moreover, it makes use of a steady state file to i) set + * This mod-file implements a non-linearized version of the New Keynesian + * model based on a recursive formulation of the price and wage setting + * equations. Moreover, it makes use of a steady state file to i) set * parameters that depend on other parameters that are potentially estimated * and ii) solve a nonlinear equation using a numerical solver to find the steady * state of labor. It provides an example on how the steady state file can be used * to circumvent some of the limitation of Dynare mod-file by accessing an external - * file that allows calling general MATLAB routines. These capacities will mostly be - * interesting for power users. If one just wants to provide analytical steady state + * file that allows calling general MATLAB routines. These capacities will mostly be + * interesting for power users. If one just wants to provide analytical steady state * values and update parameters, the steady_state_model-block allows an easy and convenient * alternative. It even allows calling numerical solvers like fsolve. For an example, see * example3.mod - * + * * The model is written in the beginning of period stock notation. To make the model - * conform with Dynare's end of period stock notation, we use the + * conform with Dynare’s end of period stock notation, we use the * predetermined_variables-command. * - * Please note that the following copyright notice only applies to this Dynare + * Please note that the following copyright notice only applies to this Dynare * implementation of the model. */ @@ -79,7 +79,7 @@ var d (long_name='preference shock') phi (long_name='labor disutility shock') F (long_name='firm profits') ; - + varexo epsd (long_name='Innovation preference shock') epsphi (long_name='Innovation labor disutility shock') epsmu_I (long_name='Innovation investment-specific technology') @@ -125,7 +125,7 @@ parameters h (long_name='consumption habits') //Note that the parameter naming in FV(2010) differs from FV(2006) //Fixed parameters, taken from FV(2010), Table 2, p. 37 -delta=0.025; +delta=0.025; epsilon=10; eta= 10; Phi=0; @@ -162,7 +162,7 @@ LambdaA = 2.8e-3; /* -The following parameters are set in the steady state file as they depend on other +The following parameters are set in the steady state file as they depend on other deep parameters (some were estimated in the original study). Setting them in the steady state file means they are updated for every parameter draw in the MCMC algorithm, while the parameters initialized here are only set once for the initial @@ -181,7 +181,7 @@ The following model equations are the stationary model equations, taken from FV(2006), p. 20, section 3.2. */ -model; +model; [name='FOC consumption'] d*(c-h*c(-1)*mu_z^(-1))^(-1)-h*betta*d(+1)*(c(+1)*mu_z(+1)-h*c)^(-1)=lambda; [name='Euler equation'] @@ -221,7 +221,7 @@ yd=c+x+mu_z^(-1)*mu_I^(-1)*(gammma1*(u-1)+gammma2/2*(u-1)^2)*k; [name='Aggregate production'] yd=(mu_A*mu_z^(-1)*(u*k)^alppha*ld^(1-alppha)-Phi)/vp; [name='Aggregate labor market'] -l=vw*ld; +l=vw*ld; [name='LOM Price dispersion term'] vp=thetap*(PI(-1)^chi/PI)^(-epsilon)*vp(-1)+(1-thetap)*PIstar^(-epsilon); [name='LOM Wage dispersion term'] @@ -241,7 +241,7 @@ log(phi)=rhophi*log(phi(-1))+epsphi; [name='Investment specific technology'] log(mu_I)=Lambdamu+epsmu_I; [name='Neutral technology'] -log(mu_A)=LambdaA+epsA; +log(mu_A)=LambdaA+epsA; [name='Defininition composite technology'] mu_z=mu_A^(1/(1-alppha))*mu_I^(alppha/(1-alppha)); diff --git a/examples/agtrend.mod b/examples/agtrend.mod index 8fb600b4feede07265a590a873662e0a5c527839..dd5ef1e8618bac13a39a09b3373ab05efa514c3d 100644 --- a/examples/agtrend.mod +++ b/examples/agtrend.mod @@ -1,7 +1,7 @@ /* * This file replicates the model studied in: - * Aguiar, Mark and Gopinath, Gita (2004): "Emerging Market Business Cycles: - * The Cycle is the Trend" (NBER WP 10734). It is different from version published + * Aguiar, Mark and Gopinath, Gita (2004): “Emerging Market Business Cycles: + * The Cycle is the Trend†(NBER WP 10734). It is different from version published * in the Journal of Political Economy. * * This model file is intended to show the capabilities of the Dynare macro @@ -15,7 +15,7 @@ * The mod-file reproduces Figure 4 of the working paper, which displays the * model response to 1 percent shock to trend and cyclical TFP. * - * This implementation was written by Sébastien Villemot and Johannes Pfeifer. + * This implementation was written by Sébastien Villemot and Johannes Pfeifer. * Please note that the following copyright notice only applies to this Dynare * implementation of the model. */ diff --git a/license.txt b/license.txt index 5bda147d27e882bd75f85fa3aeef2b4f513fd681..18e5e35bb9553a4a336c594d0915c2f89ed8594f 100644 --- a/license.txt +++ b/license.txt @@ -207,12 +207,6 @@ Copyright: 1993-1996 Kurt Hornik 2016-2017 Dynare Team License: GPL-3+ -Files: matlab/missing/strjoin/strjoin.m -Copyright: 2013-2019 Ben Abbott - 2007 Muthiah Annamalai - 2019 Dynare Team -License: GPL-3+ - Files: matlab/lmmcp/catstruct.m Copyright: 2005 Jos van der Geest <jos@jasen.nl> 2013 Christophe Gouel diff --git a/m4/ax_mexopts.m4 b/m4/ax_mexopts.m4 index 890397b61ce46f051da6d29680bc72f76cd813fd..60fda3441a371de8fb6094a18942d905bb846858 100644 --- a/m4/ax_mexopts.m4 +++ b/m4/ax_mexopts.m4 @@ -22,7 +22,7 @@ AC_REQUIRE([AX_MATLAB_ARCH]) AC_REQUIRE([AX_MATLAB_VERSION]) AC_REQUIRE([AC_PROG_SED]) -AX_COMPARE_VERSION([$MATLAB_VERSION], [lt], [7.9], [AC_MSG_ERROR([Your MATLAB is too old, please upgrade to 7.9 (R2009b) at least.])]) +AX_COMPARE_VERSION([$MATLAB_VERSION], [lt], [8.3], [AC_MSG_ERROR([Your MATLAB is too old, please upgrade to 8.3 (R2014a) at least.])]) AC_MSG_CHECKING([for options to compile MEX for MATLAB]) @@ -80,10 +80,6 @@ case ${MATLAB_ARCH} in ;; esac -# Kludge for incompatibility of older MATLABs (≤ R2011a) with recent gcc -# Include <uchar.h>, because matrix.h needs char16_t -AX_COMPARE_VERSION([$MATLAB_VERSION], [le], [7.12], [MATLAB_CFLAGS="$MATLAB_CFLAGS -include uchar.h"]) - # Converts the MATLAB version number into comparable integers with only major and minor version numbers # For example, 7.4.2 will become 0704 ax_matlab_ver=$(echo "$MATLAB_VERSION" | $SED -e 's/\([[0-9]]*\)\.\([[0-9]]*\).*/Z\1ZZ\2Z/' \ diff --git a/macOS/build.sh b/macOS/build.sh index 55b78f7caae97af2742885bc932b176f1aa6c1d1..a661fd486a3e6a09d49ef85bb929cf76432dcd4d 100755 --- a/macOS/build.sh +++ b/macOS/build.sh @@ -22,8 +22,8 @@ set -ex ROOTDIR=$(pwd)/.. # Set the compilers -CC=gcc-9 -CXX=g++-9 +CC=gcc-10 +CXX=g++-10 # Set the number of threads NTHREADS=$(nproc) @@ -88,7 +88,7 @@ make -j"$NTHREADS" NAME=dynare-"$VERSION" PKGFILES="$ROOTDIR"/macOS/pkg/"$NAME" mkdir -p \ - "$PKGFILES"/mex/matlab/maci64-7.9-9.3 \ + "$PKGFILES"/mex/matlab/maci64-8.3-9.3 \ "$PKGFILES"/mex/matlab/maci64-9.4-9.8 \ "$PKGFILES"/mex/octave \ "$PKGFILES"/doc/dynare++ \ @@ -107,7 +107,7 @@ cp -p "$ROOTDIR"/license.txt "$PKGFILES" cp -pr "$ROOTDIR"/matlab "$PKGFILES" cp -pr "$ROOTDIR"/examples "$PKGFILES" -cp -L "$ROOTDIR"/mex/matlab/* "$PKGFILES"/mex/matlab/maci64-7.9-9.3 +cp -L "$ROOTDIR"/mex/matlab/* "$PKGFILES"/mex/matlab/maci64-8.3-9.3 cp -p "$ROOTDIR"/scripts/dynare.el "$PKGFILES"/scripts cp -pr "$ROOTDIR"/contrib/ms-sbvar/TZcode/MatlabFiles "$PKGFILES"/contrib/ms-sbvar/TZcode @@ -166,7 +166,7 @@ PATH="$OCTAVE_BIN_DIR:$PATH" CC=$CC CXX=$CXX ./configure \ --with-slicot="$LIB64"/Slicot/with-underscore PATH="$OCTAVE_BIN_DIR:$PATH" make -j"$NTHREADS" cp -L "$ROOTDIR"/mex/octave/* "$PKGFILES"/mex/octave -echo -e "function v = supported_octave_version\nv=\"$(octave --eval "disp(OCTAVE_VERSION)")\";\nend" > "$PKGFILES"/matlab/supported_octave_version.m +echo -e "function v = supported_octave_version\nv=\"$OCTAVE_VERSION\";\nend" > "$PKGFILES"/matlab/supported_octave_version.m ## @@ -175,12 +175,12 @@ echo -e "function v = supported_octave_version\nv=\"$(octave --eval "disp(OCTAVE cd "$ROOTDIR"/macOS/pkg # Dynare option -pkgbuild --root "$PKGFILES" --identifier com.cepremap.dynare --version "$VERSION" --install-location /Applications/Dynare/"$LOCATION" "$NAME".pkg +pkgbuild --root "$PKGFILES" --identifier org.dynare --version "$VERSION" --install-location /Applications/Dynare/"$LOCATION" "$NAME".pkg # GCC option # Create dummy payload for GCC package; otherwise the size is displayed as 0 bytes in the installer dd if=/dev/zero of="$ROOTDIR"/macOS/brewfiles/dummy bs=1m count=800 -pkgbuild --root "$ROOTDIR"/macOS/brewfiles --identifier com.cepremap.dynare.gcc --version "$VERSION" --scripts "$ROOTDIR"/macOS/scripts --install-location /Applications/Dynare/"$LOCATION" "$NAME"-gcc.pkg +pkgbuild --root "$ROOTDIR"/macOS/brewfiles --identifier org.dynare.gcc --version "$VERSION" --scripts "$ROOTDIR"/macOS/scripts --install-location /Applications/Dynare/"$LOCATION" "$NAME"-gcc.pkg # Replace variables in displayed files sed "s/VERSION_READ/$VERSION/g" "$ROOTDIR"/macOS/distribution_template.xml > distribution_tmp.xml diff --git a/macOS/deps/Makefile b/macOS/deps/Makefile index 35977b1b44c50316f902b44e372b89882b73e000..277186f0db092fc9a63342d23ded3f03dae2b876 100644 --- a/macOS/deps/Makefile +++ b/macOS/deps/Makefile @@ -1,4 +1,4 @@ -# Copyright © 2019 Dynare Team +# Copyright © 2019-2020 Dynare Team # # This file is part of Dynare. # @@ -144,7 +144,7 @@ sources64/x13as-$(X13AS_VERSION): tarballs/x13assrc_V$(X13AS_VERSION).tar.gz lib64/x13as/x13as: sources64/x13as-$(X13AS_VERSION) cd $< && sed -i '' 's/-static//g' makefile.gf - make -C $< -f makefile.gf FC=gfortran LINKER=ld FFLAGS="-O2" LDFLAGS="-macosx_version_min 10.14.0 -lSystem $(shell gfortran -print-file-name=libgfortran.a) $(shell gfortran -print-file-name=libquadmath.a) $(shell gfortran -print-libgcc-file-name) $(shell gfortran -print-file-name=libgcc_eh.a)" PROGRAM=x13as + make -C $< -f makefile.gf FC=gfortran LINKER=ld FFLAGS="-O2 -std=legacy" LDFLAGS="-macosx_version_min 10.14.0 -lSystem $(shell gfortran -print-file-name=libgfortran.a) $(shell gfortran -print-file-name=libquadmath.a) $(shell gfortran -print-libgcc-file-name) $(shell gfortran -print-file-name=libgcc_eh.a)" PROGRAM=x13as strip $</x13as mkdir -p $(dir $@) cp $</x13as $@ diff --git a/matlab/DsgeSmoother.m b/matlab/DsgeSmoother.m index ee1546af50d5fe280641e199cfcc0a681cf5532f..14961800064cc5904ae0133c985275ee74c2870d 100644 --- a/matlab/DsgeSmoother.m +++ b/matlab/DsgeSmoother.m @@ -233,8 +233,10 @@ if kalman_algo == 1 || kalman_algo == 3 options_.nk,kalman_tol,diffuse_kalman_tol,options_.filter_decomposition,options_.smoothed_state_uncertainty); if isinf(alphahat) if kalman_algo == 1 + fprintf('\nDsgeSmoother: Switching to univariate filter. This may be a sign of stochastic singularity.\n') kalman_algo = 2; elseif kalman_algo == 3 + fprintf('\nDsgeSmoother: Switching to univariate filter. This may be a sign of stochastic singularity.\n') kalman_algo = 4; else error('This case shouldn''t happen') diff --git a/matlab/PlotPosteriorDistributions.m b/matlab/PlotPosteriorDistributions.m index 99b81a01be0a0e74cba598d2755b37de63d05e04..aac95569fbb470d821fb01b2a1f462bc56ac70f8 100644 --- a/matlab/PlotPosteriorDistributions.m +++ b/matlab/PlotPosteriorDistributions.m @@ -66,17 +66,6 @@ for i=1:npar hfig=dyn_figure(options_.nodisplay, 'Name', figurename); end [nam,texnam] = get_the_name(i, TeX, M_, estim_params_, options_); - if subplotnum == 1 - NAMES = {nam}; - if TeX - TeXNAMES = {texnam}; - end - else - NAMES = [NAMES; {nam}]; - if TeX - TeXNAMES = [TeXNAMES; {texnam}]; - end - end [x2, f2, abscissa, dens, binf2, bsup2] = draw_prior_density(i, bayestopt_); top2 = max(f2); if i <= nvx @@ -148,16 +137,17 @@ for i=1:npar end box on axis([borneinf bornesup 0 1.1*top0]) - title(nam, 'Interpreter', 'none') + if TeX + title(texnam, 'Interpreter', 'latex') + else + title(nam, 'Interpreter', 'none') + end hold off drawnow if subplotnum == MaxNumberOfPlotPerFigure || i == npar dyn_saveas(hfig,[OutputDirectoryName '/' M_.fname '_PriorsAndPosteriors' int2str(figunumber)], options_.nodisplay, options_.graph_format); if TeX && any(strcmp('eps', cellstr(options_.graph_format))) fprintf(fidTeX, '\\begin{figure}[H]\n'); - for j = 1:size(NAMES, 1) - fprintf(fidTeX, '\\psfrag{%s}[1][][0.5][0]{%s}\n', NAMES{j}, TeXNAMES{j}); - end fprintf(fidTeX, '\\centering\n'); fprintf(fidTeX, '\\includegraphics[width=%2.2f\\textwidth]{%s/%s_PriorsAndPosteriors%s}\n', ... options_.figures.textwidth*min(subplotnum/nn,1), OutputDirectoryName, M_.fname, int2str(figunumber)); diff --git a/matlab/PosteriorIRF.m b/matlab/PosteriorIRF.m index 6f6deb913b489d45bdc72bbdda1aaa8be94fd32b..07b11383c020eb3e1d5dc21e5927e894603a90b5 100644 --- a/matlab/PosteriorIRF.m +++ b/matlab/PosteriorIRF.m @@ -391,7 +391,6 @@ if ~options_.nograph && ~options_.no_graph.posterior % The files .TeX are genereted in sequential way always! - % The files .TeX are generated in sequential way always! subplotnum = 0; titTeX(M_.exo_names_orig_ord) = M_.exo_names_tex; if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format))) @@ -411,9 +410,6 @@ if ~options_.nograph && ~options_.no_graph.posterior if subplotnum == 1 fprintf(fidTeX,'\\begin{figure}[H]\n'); end - name = varlist{jj}; - texname = varlist_TeX{jj}; - fprintf(fidTeX,['\\psfrag{%s}[1][][0.5][0]{%s}\n'],name,['$' texname '$']); end if subplotnum == MaxNumberOfPlotPerFigure || (jj == nvar && subplotnum> 0) figunumber = figunumber+1; @@ -438,8 +434,6 @@ if ~options_.nograph && ~options_.no_graph.posterior % The others file format are generated in parallel by PosteriorIRF_core2! - - % Comment for testing! if ~isoctave if isnumeric(options_.parallel) || (M_.exo_nbr*ceil(length(varlist)/MaxNumberOfPlotPerFigure))<8 [fout] = PosteriorIRF_core2(localVars,1,M_.exo_nbr,0); diff --git a/matlab/PosteriorIRF_core1.m b/matlab/PosteriorIRF_core1.m index 6c021576f6e0a7d21dd3c3cd3369edee5fdc1e3a..9cfdc8e244c6c9e7923505ca918b53f0662945cd 100644 --- a/matlab/PosteriorIRF_core1.m +++ b/matlab/PosteriorIRF_core1.m @@ -162,6 +162,8 @@ while fpar<B errordef = 'Indeterminacy'; elseif info(1) == 5 errordef = 'Rank condition is not satisfied'; + else + errordef = get_error_message(info, options_); end if strcmpi(type,'prior') disp(['PosteriorIRF :: Dynare is unable to solve the model (' errordef ')']) diff --git a/matlab/PosteriorIRF_core2.m b/matlab/PosteriorIRF_core2.m index 2b95c570fd085089a6baf08c315cf7455493d54a..522888e3894ac9865325c8927529481a17e4b5cc 100644 --- a/matlab/PosteriorIRF_core2.m +++ b/matlab/PosteriorIRF_core2.m @@ -142,8 +142,11 @@ for i=fpar:npar remove_fractional_xticks; hold off end - name = varlist{j}; - title(name,'Interpreter','none') + if options_.TeX + title(['$' varlist_TeX{j} '$'],'Interpreter','latex') + else + title(varlist{j},'Interpreter','none') + end else if options_.debug fprintf('POSTERIOR_IRF: The IRF of %s to %s is smaller than the irf_plot_threshold of %4.3f and will not be displayed.\n',varlist{j},tit{i},options_.impulse_responses.plot_threshold) diff --git a/matlab/Q6_plication.m b/matlab/Q6_plication.m index 0692364f1357c8a0bd7d2269cade9c229f947327..153ee8ccd85ea9552b4225e368fcb36b8b3da08d 100644 --- a/matlab/Q6_plication.m +++ b/matlab/Q6_plication.m @@ -63,7 +63,9 @@ for i1=1:p end end end +if nargout==2 DP6inv = (transpose(DP6)*DP6)\transpose(DP6); +end function m = mue(p,i1,i2,i3,i4,i5,i6) % Auxiliary expression, see page 122 of Meijer (2005) diff --git a/matlab/add_path_to_mex_files.m b/matlab/add_path_to_mex_files.m index 85f2593030c39eb281f00e31036a3f885b4637d9..f91a7b5b9daa4c0e5b9a494347f6d3a85ffd342c 100644 --- a/matlab/add_path_to_mex_files.m +++ b/matlab/add_path_to_mex_files.m @@ -1,6 +1,6 @@ function mexpath = add_path_to_mex_files(dynareroot, modifypath) -% Copyright (C) 2015-2019 Dynare Team +% Copyright (C) 2015-2020 Dynare Team % % This file is part of Dynare. % @@ -25,13 +25,7 @@ if isoctave % Add specific paths for Dynare Windows package if ispc if strcmpi(computer(), 'i686-w64-mingw32') - tmp = [dynareroot '../mex/octave/win32/']; - if exist(tmp, 'dir') - mexpath = tmp; - if modifypath - addpath(mexpath); - end - end + warning('MEX files not available for 32-bit Octave') else tmp = [dynareroot '../mex/octave/win64/']; if exist(tmp, 'dir') @@ -52,20 +46,13 @@ if isoctave addpath([dynareroot '../mex/octave/']); end else - % Add win32 specific paths for Dynare Windows package if strcmp(computer, 'PCWIN') - tmp = [dynareroot '../mex/matlab/win32-7.9-8.6/']; - if exist(tmp, 'dir') - mexpath = tmp; - if modifypath - addpath(mexpath); - end - end + warning('MEX files not available for 32-bit MATLAB') end % Add win64 specific paths for Dynare Windows package if strcmp(computer, 'PCWIN64') if matlab_ver_less_than('9.4') - tmp = [dynareroot '../mex/matlab/win64-7.9-9.3/']; + tmp = [dynareroot '../mex/matlab/win64-8.3-9.3/']; if exist(tmp, 'dir') mexpath = tmp; if modifypath @@ -85,7 +72,7 @@ else % Add macOS paths for Dynare Mac package if strcmp(computer, 'MACI64') if matlab_ver_less_than('9.4') - tmp = [dynareroot '../mex/matlab/maci64-7.9-9.3/']; + tmp = [dynareroot '../mex/matlab/maci64-8.3-9.3/']; if exist(tmp, 'dir') mexpath = tmp; if modifypath diff --git a/matlab/block_mfs_steadystate.m b/matlab/block_mfs_steadystate.m index a0793e674d99e79e15125a8a530f85d5ef3b80b5..ceccf3555f0ad8c50f43a3b5767316abeec085b2 100644 --- a/matlab/block_mfs_steadystate.m +++ b/matlab/block_mfs_steadystate.m @@ -1,8 +1,8 @@ -function [r, g1] = block_mfs_steadystate(y, b, y_all, exo, params, M) +function [r, g1] = block_mfs_steadystate(y, b, y_all, exo, params, T, M) % Wrapper around the *_static.m file, for use with dynare_solve, % when block_mfs option is given to steady. -% Copyright (C) 2009-2012 Dynare Team +% Copyright (C) 2009-2020 Dynare Team % % This file is part of Dynare. % @@ -21,5 +21,5 @@ function [r, g1] = block_mfs_steadystate(y, b, y_all, exo, params, M) y_all(M.block_structure_stat.block(b).variable) = y; -eval(['[r,g1] = ' M.fname '.static(b, y_all, exo, params);']); +eval(['[r,~,~,g1] = ' M.fname '.static(b, y_all, exo, params, T);']); g1 = full(g1); diff --git a/matlab/check_bounds_and_definiteness_estimation.m b/matlab/check_bounds_and_definiteness_estimation.m new file mode 100644 index 0000000000000000000000000000000000000000..8876664dd2163d00a217ba852c665a37d7847854 --- /dev/null +++ b/matlab/check_bounds_and_definiteness_estimation.m @@ -0,0 +1,106 @@ +function [fval,info,exit_flag,Q,H]=check_bounds_and_definiteness_estimation(xparam1, M_, estim_params_, bayestopt_) +% function [fval,info,exit_flag]=check_bounds_and_definiteness_estimation(xparam1, M_, estim_params_, bayestopt_) +% Checks whether parameter vector satisfies +% +% INPUTS +% - xparam1 [double] n by 1 vector, estimated parameters. +% - M_ [struct] Matlab's structure describing the Model. +% - estim_params_ [struct] Matlab's structure describing the estimated_parameters. +% - bayestopt_ [struct] Matlab's structure specifying the bounds on the paramater values (initialized by dynare,aka bayesopt_). +% +% OUTPUTS +% - fval [double] scalar, value of the likelihood or posterior kernel. +% - info [integer] 4 by 1 vector, informations resolution of the model and evaluation of the likelihood. +% - exit_flag [integer] scalar, equal to 1 (no issues when evaluating the likelihood) or 0 (not able to evaluate the likelihood). +% - Q [matrix] Covariance matrix of structural shocks +% - H [matrix] Covariance matrix of measurement errors + +% Copyright (C) 2020 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/>. + +fval = []; +exit_flag = 1; +info = zeros(4,1); +Q=[]; +H=[]; +% Return, with endogenous penalty, if some parameters are smaller than the lower bound of the prior domain. +if any(xparam1<bayestopt_.lb) + k = find(xparam1(:) < bayestopt_.lb); + fval = Inf; + exit_flag = 0; + info(1) = 41; + info(4) = sum((bayestopt_.lb(k)-xparam1(k)).^2); + return +end + +% Return, with endogenous penalty, if some parameters are greater than the upper bound of the prior domain. +if any(xparam1>bayestopt_.ub) + k = find(xparam1(:)>bayestopt_.ub); + fval = Inf; + exit_flag = 0; + info(1) = 42; + info(4) = sum((xparam1(k)-bayestopt_.ub(k)).^2); + return +end + +Q = M_.Sigma_e; +H = M_.H; + +if ~issquare(Q) || estim_params_.ncx || isfield(estim_params_,'calibrated_covariances') + [Q_is_positive_definite, penalty] = ispd(Q(estim_params_.Sigma_e_entries_to_check_for_positive_definiteness,estim_params_.Sigma_e_entries_to_check_for_positive_definiteness)); + if ~Q_is_positive_definite + fval = Inf; + exit_flag = 0; + info(1) = 43; + info(4) = penalty; + return + end + if isfield(estim_params_,'calibrated_covariances') + correct_flag=check_consistency_covariances(Q); + if ~correct_flag + penalty = sum(Q(estim_params_.calibrated_covariances.position).^2); + fval = Inf; + exit_flag = 0; + info(1) = 71; + info(4) = penalty; + return + end + end + +end + +if ~issquare(H) || estim_params_.ncn || isfield(estim_params_,'calibrated_covariances_ME') + [H_is_positive_definite, penalty] = ispd(H(estim_params_.H_entries_to_check_for_positive_definiteness,estim_params_.H_entries_to_check_for_positive_definiteness)); + if ~H_is_positive_definite + fval = Inf; + exit_flag = 0; + info(1) = 44; + info(4) = penalty; + return + end + if isfield(estim_params_,'calibrated_covariances_ME') + correct_flag=check_consistency_covariances(H); + if ~correct_flag + penalty = sum(H(estim_params_.calibrated_covariances_ME.position).^2); + fval = Inf; + exit_flag = 0; + info(1) = 72; + info(4) = penalty; + return + end + end +end \ No newline at end of file diff --git a/matlab/check_valid_ver.m b/matlab/check_valid_ver.m deleted file mode 100644 index ea3d6997cb117d70c1835ac7ec4ed6d24dbcc8b4..0000000000000000000000000000000000000000 --- a/matlab/check_valid_ver.m +++ /dev/null @@ -1,41 +0,0 @@ -function check_valid_ver(ver) -%function check_valid_ver(ver) -% Checks that ver is valid -% -% INPUTS -% ver [string] dynare version number -% -% OUTPUTS -% none -% -% SPECIAL REQUIREMENTS -% none - -% Copyright (C) 2015 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/>. - -test_ver = strsplit(ver, {'.', '-'}); -errmsg = 'check_valid_ver: the desired version must be in the proper format'; - -assert((length(test_ver) == 2 || length(test_ver) == 3) ... - && ~isempty(str2double(test_ver{1})) ... - && ~isempty(str2double(test_ver{2})), ... - errmsg); -if length(test_ver) == 3 && isnan(str2double(test_ver{3})) - assert(strcmp(test_ver{3}, 'unstable'), errmsg); -end -end diff --git a/matlab/compute_moments_varendo.m b/matlab/compute_moments_varendo.m index 4bfabce34076182da35633e8285e31b4ff4c973c..d4a35758b6330b15936528e88785d2322887c9b4 100644 --- a/matlab/compute_moments_varendo.m +++ b/matlab/compute_moments_varendo.m @@ -17,7 +17,7 @@ function oo_ = compute_moments_varendo(type, options_, M_, oo_, var_list_) % SPECIAL REQUIREMENTS % none -% Copyright (C) 2008-2018 Dynare Team +% Copyright (C) 2008-2020 Dynare Team % % This file is part of Dynare. % @@ -42,6 +42,9 @@ if strcmpi(type,'posterior') if nargin==4 var_list_ = options_.varobs; end + if isfield(oo_,'PosteriorTheoreticalMoments') + oo_=rmfield(oo_,'PosteriorTheoreticalMoments'); + end elseif strcmpi(type,'prior') posterior = 0; if nargin==4 @@ -50,6 +53,9 @@ elseif strcmpi(type,'prior') options_.prior_analysis_var_list = options_.varobs; end end + if isfield(oo_,'PriorTheoreticalMoments') + oo_=rmfield(oo_,'PriorTheoreticalMoments'); + end else error('compute_moments_varendo:: Unknown type!') end @@ -144,8 +150,8 @@ if M_.exo_nbr > 1 skipline(); end skipline(); - if ~all(M_.H==0) - if isoctave || matlab_ver_less_than('8.1') + if ~all(diag(M_.H)==0) + if isoctave [observable_name_requested_vars, varlist_pos] = intersect_stable(var_list_, options_.varobs); else [observable_name_requested_vars, varlist_pos] = intersect(var_list_, options_.varobs, 'stable'); @@ -231,7 +237,7 @@ if M_.exo_nbr > 1 end end skipline(); - if ~all(M_.H==0) + if ~all(diag(M_.H)==0) if ~isempty(observable_name_requested_vars) NumberOfObservedEndogenousVariables = length(observable_name_requested_vars); temp=NaN(NumberOfObservedEndogenousVariables,NumberOfExogenousVariables+1,length(Steps)); diff --git a/matlab/conditional_variance_decomposition.m b/matlab/conditional_variance_decomposition.m index 4ef81da5537b64492d4359b61b1a8f81bd69e043..f249a7c7a9e17b7de25b061c2ae30bdbe3cbaa35 100644 --- a/matlab/conditional_variance_decomposition.m +++ b/matlab/conditional_variance_decomposition.m @@ -17,7 +17,7 @@ function [ConditionalVarianceDecomposition, ConditionalVarianceDecomposition_ME] % h is the number of Steps % p is the number of state innovations and -% Copyright (C) 2010-2017 Dynare Team +% Copyright (C) 2010-2020 Dynare Team % % This file is part of Dynare. % @@ -88,8 +88,8 @@ end % get intersection of requested variables and observed variables with % Measurement error -if ~all(StateSpaceModel.measurement_error==0) - if isoctave || matlab_ver_less_than('8.1') +if ~all(diag(StateSpaceModel.measurement_error)==0) + if isoctave [observable_pos,index_subset,index_observables]=intersect_stable(SubsetOfVariables,StateSpaceModel.observable_pos); else [observable_pos,index_subset,index_observables]=intersect(SubsetOfVariables,StateSpaceModel.observable_pos,'stable'); diff --git a/matlab/conditional_variance_decomposition_ME_mc_analysis.m b/matlab/conditional_variance_decomposition_ME_mc_analysis.m index cfe9a1b1553ed5bed3c53ef67b8d92f34cadbd0f..a0b5ffdd7a1df2e0fa2127091bd78466ffb90027 100644 --- a/matlab/conditional_variance_decomposition_ME_mc_analysis.m +++ b/matlab/conditional_variance_decomposition_ME_mc_analysis.m @@ -22,7 +22,7 @@ function oo_ = ... % OUTPUTS % oo_ [structure] Dynare structure where the results are saved. -% Copyright (C) 2017-2018 Dynare Team +% Copyright (C) 2017-2020 Dynare Team % % This file is part of Dynare. % @@ -63,7 +63,7 @@ if isempty(exogenous_variable_index) end end -if isoctave || matlab_ver_less_than('8.1') +if isoctave [observable_pos_requested_vars,index_subset,index_observables]=intersect_stable(var_list,options_.varobs); else [observable_pos_requested_vars,index_subset,index_observables]=intersect(var_list,options_.varobs,'stable'); diff --git a/matlab/convergence_diagnostics/McMCDiagnostics.m b/matlab/convergence_diagnostics/McMCDiagnostics.m index 44c4f872dfee70f25c1c319f41bd7f4248cbb981..69d368291273c46831bc056d4039df33f8de7e9b 100644 --- a/matlab/convergence_diagnostics/McMCDiagnostics.m +++ b/matlab/convergence_diagnostics/McMCDiagnostics.m @@ -306,31 +306,24 @@ for i = 1:pages plt2 = UDIAG(:,6,k); namnam = [nam , ' (m3)']; end - if TeX - if j==1 - NAMES = deblank(namnam); - TEXNAMES = deblank(namtex); - else - NAMES = char(NAMES,deblank(namnam)); - TEXNAMES = char(TEXNAMES,deblank(namtex)); - end - end subplot(3,3,boxplot); plot(xx,plt1,'-b'); % Pooled hold on; plot(xx,plt2,'-r'); % Within (mean) hold off; xlim([xx(1) xx(NumberOfLines)]) - title(namnam,'Interpreter','none') + if TeX + title(namtex,'interpreter','latex') + else + title(namnam,'Interpreter','none') + end + boxplot = boxplot + 1; end end dyn_saveas(h,[OutputFolder '/' ModelName '_udiag' int2str(i)],options_.nodisplay,options_.graph_format); if TeX && any(strcmp('eps',cellstr(options_.graph_format))) fprintf(fidTeX,'\\begin{figure}[H]\n'); - for jj = 1:size(NAMES,1) - fprintf(fidTeX,'\\psfrag{%s}[1][][0.5][0]{%s}\n',deblank(NAMES(jj,:)),deblank(TEXNAMES(jj,:))); - end fprintf(fidTeX,'\\centering \n'); fprintf(fidTeX,'\\includegraphics[width=%2.2f\\textwidth]{%s_udiag%s}\n',options_.figures.textwidth*min((boxplot-1)/3,1),[OutputFolder '/' ModelName],int2str(i)); fprintf(fidTeX,'\\caption{Univariate convergence diagnostics for the Metropolis-Hastings.\n'); @@ -360,22 +353,22 @@ if reste plt1 = UDIAG(:,1,k); plt2 = UDIAG(:,2,k); namnam = [nam , ' (Interval)']; + if TeX + namnamtex = [namtex , ' (Interval)']; + end elseif crit == 2 plt1 = UDIAG(:,3,k); plt2 = UDIAG(:,4,k); namnam = [nam , ' (m2)']; + if TeX + namnamtex = [namtex , ' (m2)']; + end elseif crit == 3 plt1 = UDIAG(:,5,k); plt2 = UDIAG(:,6,k); namnam = [nam , ' (m3)']; - end - if TeX - if j==1 - NAMES = deblank(namnam); - TEXNAMES = deblank(namtex); - else - NAMES = char(NAMES,deblank(namnam)); - TEXNAMES = char(TEXNAMES,deblank(namtex)); + if TeX + namnamtex = [namtex , ' (m3)']; end end subplot(nr,nc,boxplot); @@ -384,16 +377,17 @@ if reste plot(xx,plt2,'-r'); % Within (mean) hold off; xlim([xx(1) xx(NumberOfLines)]); - title(namnam,'Interpreter','none'); + if TeX + title(namnamtex,'Interpreter','latex'); + else + title(namnam,'Interpreter','none'); + end boxplot = boxplot + 1; end end dyn_saveas(h,[ OutputFolder '/' ModelName '_udiag' int2str(pages+1)],options_.nodisplay,options_.graph_format); if TeX && any(strcmp('eps',cellstr(options_.graph_format))) fprintf(fidTeX,'\\begin{figure}[H]\n'); - for jj = 1:size(NAMES,1) - fprintf(fidTeX,'\\psfrag{%s}[1][][0.5][0]{%s}\n',deblank(NAMES(jj,:)),deblank(TEXNAMES(jj,:))); - end fprintf(fidTeX,'\\centering \n'); fprintf(fidTeX,'\\includegraphics[width=%2.2f\\textwidth]{%s_udiag%s}\n',options_.figures.textwidth*min((boxplot-1)/nc,1),[OutputFolder '/' ModelName],int2str(pages+1)); if reste == 2 @@ -477,13 +471,6 @@ for crit = 1:3 plt2 = MDIAG(:,6); namnam = 'm3'; end - if TeX - if crit == 1 - NAMES = deblank(namnam); - else - NAMES = char(NAMES,deblank(namnam)); - end - end subplot(3,1,boxplot); plot(xx,plt1,'-b'); % Pooled hold on @@ -497,9 +484,6 @@ dyn_saveas(h,[ OutputFolder '/' ModelName '_mdiag'],options_.nodisplay,options_. if TeX && any(strcmp('eps',cellstr(options_.graph_format))) fprintf(fidTeX,'\\begin{figure}[H]\n'); - for jj = 1:3 - fprintf(fidTeX,'\\psfrag{%s}[1][][0.5][0]{%s}\n',deblank(NAMES(jj,:)),' '); - end fprintf(fidTeX,'\\centering \n'); fprintf(fidTeX,'\\includegraphics[width=0.8\\textwidth]{%s_mdiag}\n',[OutputFolder '/' ModelName]); fprintf(fidTeX,'\\caption{Multivariate convergence diagnostics for the Metropolis-Hastings.\n'); diff --git a/matlab/default_option_values.m b/matlab/default_option_values.m index 797a0df65d83da0bbb1ec326b2c40301c949dc02..f00b79b4e8247182f7de6e94ed2f3008d9ecdf3d 100644 --- a/matlab/default_option_values.m +++ b/matlab/default_option_values.m @@ -151,6 +151,7 @@ end % IRFs & other stoch_simul output options_.irf = 40; options_.impulse_responses.plot_threshold=1e-10; +options_.zero_moments_tolerance=1e-10; options_.relative_irf = false; options_.ar = 5; options_.hp_filter = 0; @@ -512,7 +513,7 @@ newrat.tolerance.f=1e-5; newrat.tolerance.f_analytic=1e-7; newrat.maxiter=1000; newrat.verbosity=1; -newrat.Save_files=1; +newrat.Save_files=0; options_.newrat=newrat; diff --git a/matlab/disp_identification.m b/matlab/disp_identification.m index 29142c2fa8b66bb1b230921f254d4abcf70d910a..cb51885c1c2aef1d1b346ac032cf2ca5470ce5c5 100644 --- a/matlab/disp_identification.m +++ b/matlab/disp_identification.m @@ -142,9 +142,9 @@ for jide = 1:4 disp([upper(strTest), ':']) disp(' !!!WARNING!!!'); if SampleSize>1 - disp([' The rank of ', strJacobian, ' (', strMeaning, ') is deficient for ', num2str(length(find(ide.ino))),' out of ',int2str(SampleSize),' MC runs (with tol = ', num2str(tol_rank), ')!' ]), + disp([' The rank of ', strJacobian, ' (', strMeaning, ') is deficient for ', num2str(length(find(ide.ino))),' out of ',int2str(SampleSize),' MC runs!' ]), else - disp([' The rank of ', strJacobian, ' (', strMeaning, ') is deficient by ', num2str(size(Jacob,2)-ide.rank), ' (rank(', strJacobian, ') = ', num2str(ide.rank), ' < ', num2str(size(Jacob,2)), ' with tol = ', num2str(tol_rank), ')!']), + disp([' The rank of ', strJacobian, ' (', strMeaning, ') is deficient!']), end skipline() for j=1:totparam_nbr @@ -192,9 +192,9 @@ for jide = 1:4 disp([upper(strTest), ':']) disp(' !!!WARNING!!!'); if SampleSize>1 - disp([' The rank of ', strJacobian, ' (', strMeaning, ') is deficient for ', num2str(length(find(ide.ino))),' out of ',int2str(SampleSize),' MC runs (with tol = ', num2str(tol_rank), ')!' ]), + disp([' The rank of ', strJacobian, ' (', strMeaning, ') is deficient for ', num2str(length(find(ide.ino))),' out of ',int2str(SampleSize),' MC runs!' ]), else - disp([' The rank of ', strJacobian, ' (', strMeaning, ') is deficient by ', num2str(size(Jacob,2)-ide.rank), ' (rank(', strJacobian, ') = ', num2str(ide.rank), ' < ', num2str(size(Jacob,2)), ' with tol = ', num2str(tol_rank), ')!']), + disp([' The rank of ', strJacobian, ' (', strMeaning, ') is deficient!']), end if all(cellfun(@isempty,ide.problpars)) disp([' No problematic parameter combinations with maximum dimension ', num2str(size(ide.problpars,2)), ' were found. Increase max_dim_subsets_groups.']); diff --git a/matlab/disp_moments.m b/matlab/disp_moments.m index 8223ba4f92df23cc14fbdeb32b9572a47949b89e..3d0cb6aa15739da4eb0480361fda82822219f669 100644 --- a/matlab/disp_moments.m +++ b/matlab/disp_moments.m @@ -11,7 +11,7 @@ function oo_=disp_moments(y,var_list,M_,options_,oo_) % OUTPUTS % oo_ [structure] Dynare's results structure, -% Copyright (C) 2001-2019 Dynare Team +% Copyright (C) 2001-2020 Dynare Team % % This file is part of Dynare. % @@ -40,7 +40,7 @@ ivar=zeros(nvar,1); for i=1:nvar i_tmp = strmatch(var_list{i}, M_.endo_names, 'exact'); if isempty(i_tmp) - error ('One of the variable specified does not exist') ; + error('The variable %s specified is not an endogenous variable',var_list{i}); else ivar(i) = i_tmp; end @@ -50,7 +50,7 @@ y = y(ivar,options_.drop+1:end)'; ME_present=0; if ~all(M_.H==0) - if isoctave || matlab_ver_less_than('8.1') + if isoctave [observable_pos_requested_vars, index_subset, index_observables] = intersect_stable(ivar, options_.varobs_id); else [observable_pos_requested_vars, index_subset, index_observables] = intersect(ivar, options_.varobs_id, 'stable'); @@ -67,6 +67,8 @@ if ~all(M_.H==0) y_ME=get_filtered_time_series(y_ME,m_ME,options_); y_ME_only_filtered=get_filtered_time_series(y_ME_only,mean(y_ME_only),options_); s2_ME = mean(y_ME.*y_ME); + s_ME = sqrt(s2_ME); + zero_variance_ME_var_index=index_subset(abs(s_ME')<options_.zero_moments_tolerance); end end @@ -83,11 +85,20 @@ oo_.var = y'*y/size(y,1); oo_.skewness = (mean(y.^3)./s2.^1.5)'; oo_.kurtosis = (mean(y.^4)./(s2.*s2)-3)'; +zero_variance_var_index=find(abs(s)<options_.zero_moments_tolerance); +oo_.skewness(zero_variance_var_index)=NaN; +oo_.kurtosis(zero_variance_var_index)=NaN; +s(zero_variance_var_index)=0; +s2(zero_variance_var_index)=0; +oo_.var(zero_variance_var_index,:)=0; +oo_.var(:,zero_variance_var_index)=0; + + labels = M_.endo_names(ivar); labels_TeX = M_.endo_names_tex(ivar); if ~options_.nomoments - z = [ m' s' s2' (mean(y.^3)./s2.^1.5)' (mean(y.^4)./(s2.*s2)-3)' ]; + z = [ m' s' s2' oo_.skewness oo_.kurtosis ]; title='MOMENTS OF SIMULATED VARIABLES'; title=add_filter_subtitle(title, options_); headers = {'VARIABLE'; 'MEAN'; 'STD. DEV.'; 'VARIANCE'; 'SKEWNESS'; 'KURTOSIS'}; @@ -99,6 +110,8 @@ end if ~options_.nocorr corr = (y'*y/size(y,1))./(s'*s); + corr(zero_variance_var_index,:)=NaN; + corr(:,zero_variance_var_index)=NaN; if options_.contemporaneous_correlation oo_.contemporaneous_correlation = corr; end @@ -124,6 +137,8 @@ if ar > 0 autocorr = []; for i=1:ar oo_.autocorr{i} = y(ar+1:end,:)'*y(ar+1-i:end-i,:)./((size(y,1)-ar)*std(y(ar+1:end,:))'*std(y(ar+1-i:end-i,:))); + oo_.autocorr{i}(zero_variance_var_index,:)=NaN; + oo_.autocorr{i}(:,zero_variance_var_index)=NaN; autocorr = [ autocorr diag(oo_.autocorr{i}) ]; end if ~options_.noprint @@ -169,12 +184,16 @@ if ~options_.nodecomposition y_sim_one_shock = simult_(M_,options_,y0,oo_.dr,temp_shock_mat,options_.order); y_sim_one_shock=y_sim_one_shock(ivar,1+options_.drop+1:end)'; y_sim_one_shock=get_filtered_time_series(y_sim_one_shock,mean(y_sim_one_shock),options_); - oo_.variance_decomposition(:,i_exo_var(shock_iter))=var(y_sim_one_shock)./s2*100; + oo_.variance_decomposition(:,i_exo_var(shock_iter))=var(y_sim_one_shock)./s2*100; end + oo_.variance_decomposition(zero_variance_var_index,:)=NaN; if ME_present oo_.variance_decomposition_ME=oo_.variance_decomposition(index_subset,:)... .*repmat((s2(index_subset)./s2_ME)',1,length(i_exo_var)); oo_.variance_decomposition_ME(:,end+1)=var(y_ME_only_filtered)./s2_ME*100; + oo_.variance_decomposition_ME(ismember(observable_pos_requested_vars,intersect(zero_variance_ME_var_index,zero_variance_var_index)),:)=NaN; + oo_.variance_decomposition_ME(ismember(observable_pos_requested_vars,setdiff(zero_variance_var_index,zero_variance_ME_var_index)),1:end-1)=0; + oo_.variance_decomposition_ME(ismember(observable_pos_requested_vars,setdiff(zero_variance_var_index,zero_variance_ME_var_index)),end)=1; end if ~options_.noprint %options_.nomoments == 0 skipline() diff --git a/matlab/disp_th_moments.m b/matlab/disp_th_moments.m index 33fdb7100b097f295a6e5a6b4aa26ccef7b9fdae..5ea5040bd1a2410356fa9280a2ab622e85f3ab1b 100644 --- a/matlab/disp_th_moments.m +++ b/matlab/disp_th_moments.m @@ -2,7 +2,7 @@ function oo_ = disp_th_moments(dr, var_list, M_, options_, oo_) % Display theoretical moments of variables -% Copyright (C) 2001-2018 Dynare Team +% Copyright (C) 2001-2020 Dynare Team % % This file is part of Dynare. % @@ -21,7 +21,7 @@ function oo_ = disp_th_moments(dr, var_list, M_, options_, oo_) nodecomposition = options_.nodecomposition; if options_.one_sided_hp_filter - error(['disp_th_moments:: theoretical moments incompatible with one-sided HP filter. Use simulated moments instead']) + error('disp_th_moments:: theoretical moments incompatible with one-sided HP filter. Use simulated moments instead') end if isempty(var_list) var_list = M_.endo_names(1:M_.orig_endo_nbr); @@ -54,8 +54,8 @@ oo_.mean = m; oo_.var = oo_.gamma_y{1}; ME_present=0; -if ~all(M_.H==0) - if isoctave || matlab_ver_less_than('8.1') +if ~all(diag(M_.H)==0) + if isoctave [observable_pos_requested_vars,index_subset,index_observables]=intersect_stable(ivar,options_.varobs_id); else [observable_pos_requested_vars,index_subset,index_observables]=intersect(ivar,options_.varobs_id,'stable'); @@ -105,7 +105,7 @@ if size(stationary_vars, 1) > 0 lh = cellofchararraymaxlength(M_.endo_names(ivar(stationary_vars)))+2; dyntable(options_, title, headers, M_.endo_names(ivar(stationary_vars)), 100*oo_.gamma_y{options_.ar+2}(stationary_vars,:), lh, 8, 2); if ME_present - if isoctave || matlab_ver_less_than('8.1') + if isoctave [stationary_observables, pos_index_subset] = intersect_stable(index_subset, stationary_vars); else [stationary_observables, pos_index_subset] = intersect(index_subset, stationary_vars, 'stable'); @@ -129,7 +129,7 @@ if size(stationary_vars, 1) > 0 end end conditional_variance_steps = options_.conditional_variance_decomposition; - if length(conditional_variance_steps) + if ~isempty(conditional_variance_steps) StateSpaceModel.number_of_state_equations = M_.endo_nbr; StateSpaceModel.number_of_state_innovations = M_.exo_nbr; StateSpaceModel.sigma_e_is_diagonal = M_.sigma_e_is_diagonal; @@ -151,7 +151,7 @@ if size(stationary_vars, 1) > 0 end end -if length(i1) == 0 +if isempty(i1) skipline() disp('All endogenous are constant or non stationary, not displaying correlations and auto-correlations') skipline() @@ -186,7 +186,7 @@ if ~options_.nocorr && size(stationary_vars, 1)>0 end if options_.ar > 0 && size(stationary_vars, 1) > 0 - z=[]; + z=NaN(length(i1),options_.ar); for i=1:options_.ar oo_.autocorr{i} = oo_.gamma_y{i+1}; z(:,i) = diag(oo_.gamma_y{i+1}(i1,i1)); diff --git a/matlab/disp_th_moments_order3.m b/matlab/disp_th_moments_order3.m new file mode 100644 index 0000000000000000000000000000000000000000..dfdc87eaafb37bc145d8c7bd345406274b6cac57 --- /dev/null +++ b/matlab/disp_th_moments_order3.m @@ -0,0 +1,126 @@ +function oo_=disp_th_moments_order3(dr,M_,options_,i_var,oo_) +% oo_=disp_th_moments_order3(dr,M_,options_,i_var,oo_) +% Display theoretical moments of variables based on (third order) pruned +% state-space +% +% INPUTS: +% dr : Dynare decision rules structure +% M_ : Matlab's structure describing the Model (initialized by dynare, see @ref{M_}). +% options_ : Matlab's structure describing the options (initialized by dynare, see @ref{options_}). +% i_var : Index of requested variables in declaration order +% oo_ : Matlab's structure describing the Model (initialized by dynare, see @ref{M_}), containing +% +% OUTPUTS: +% oo_ : Matlab's structure describing the Model (initialized by dynare, see @ref{M_}), containing +% gamma_y [cell] Matlab cell of nar+1 arrays, where nar is the order of the autocorrelation function. +% gamma_y{1} [double] Covariance matrix. +% gamma_y{i+1} [double] Autocorrelation function (for i=1,...,options_.ar). +% mean [vector] Unconditional mean +% var [matrix] Unconditional covariance matrix +% autocorr [cell] Cell storing the theoretical autocorrelation +% contemporaneous_correlation [matrix] matrix of contemporaneous correlations +% +% Copyright (C) 2020 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/>. + + +if options_.one_sided_hp_filter || options_.hp_filter || options_.bandpass.indicator + error(['disp_th_moments:: theoretical moments incompatible with filtering. Use simulated moments instead']) +end + +nvars=length(i_var); +obs_var=NaN(nvars,1); +for i=1:nvars + obs_var(i,1) = find(strcmp(M_.endo_names(i_var(i),:), M_.endo_names(dr.order_var))); +end + +pruned_state_space = pruned_state_space_system(M_, options_, dr, obs_var, options_.ar, 1, 0); + + +m = pruned_state_space.E_y; + +oo_.gamma_y{1} = pruned_state_space.Var_y; + +i1 = find(abs(diag(oo_.gamma_y{1})) > 1e-12); +s2 = diag(oo_.gamma_y{1}); +sd = sqrt(s2); + +z = [ m sd s2 ]; +oo_.mean = m; +oo_.var = oo_.gamma_y{1}; + +if ~options_.noprint %options_.nomoments == 0 + title='THEORETICAL MOMENTS BASED ON PRUNED STATE SPACE'; + headers={'VARIABLE','MEAN','STD. DEV.','VARIANCE'}; + labels = M_.endo_names(i_var,:); + lh = cellofchararraymaxlength(labels)+2; + dyntable(options_,title,headers,labels,z,lh,11,4); + if options_.TeX + labels = M_.endo_names_tex(i_var,:); + lh = cellofchararraymaxlength(labels)+2; + dyn_latex_table(M_,options_,title,'th_moments',headers,labels,z,lh,11,4); + end +end + +if isempty(i1) + disp_verbose(' ',~options_.noprint) + disp_verbose('All endogenous are constant or non stationary, not displaying correlations and auto-correlations',~options_.noprint) + disp_verbose(' ',~options_.noprint) + return; +end + +if options_.nocorr == 0 % && size(stationary_vars, 1) > 0 + corr=pruned_state_space.Corr_y; + if options_.contemporaneous_correlation + oo_.contemporaneous_correlation = corr; + end + if ~options_.noprint + skipline() + title='MATRIX OF CORRELATIONS BASED ON PRUNED STATE SPACE'; + labels = M_.endo_names(i_var,:); + headers = ['Variables';labels]; + lh = cellofchararraymaxlength(labels)+2; + dyntable(options_,title,headers,labels,corr,lh,8,4); + if options_.TeX + labels = M_.endo_names_tex(i_var,:); + headers=['Variables';labels]; + lh = cellofchararraymaxlength(labels)+2; + dyn_latex_table(M_,options_,title,'th_corr_matrix',headers,labels,corr,lh,8,4); + end + end +end +if options_.ar > 0 %&& size(stationary_vars, 1) > 0 + z=NaN(length(i1),options_.ar); + for i=1:options_.ar + oo_.gamma_y{i+1} = pruned_state_space.Corr_yi(:,:,i); + oo_.autocorr{i} = oo_.gamma_y{i+1}; + z(:,i) = diag(oo_.gamma_y{i+1}(i1,i1)); + end + if ~options_.noprint + skipline() + title='COEFFICIENTS OF AUTOCORRELATION BASED ON PRUNED STATE SPACE'; + labels = M_.endo_names(i_var(i1),:); + headers = ['Order ';cellstr(int2str([1:options_.ar]'))]; + lh = cellofchararraymaxlength(labels)+2; + dyntable(options_,title,headers,labels,z,lh,8,4); + if options_.TeX + labels = M_.endo_names_tex(i_var(i1),:); + lh = cellofchararraymaxlength(labels)+2; + dyn_latex_table(M_,options_,title,'th_autocorr_matrix',headers,labels,z,lh,8,4); + end + end +end \ No newline at end of file diff --git a/matlab/distributions/lpdfgweibull.m b/matlab/distributions/lpdfgweibull.m index 7848b35469008e90ece1c9a0bce7338e47b1f621..5d31e23802079b0381d0fff0836106dfee8f1666 100644 --- a/matlab/distributions/lpdfgweibull.m +++ b/matlab/distributions/lpdfgweibull.m @@ -16,7 +16,7 @@ function [ldens,Dldens,D2ldens] = lpdfgweibull(x,a,b,c) % --*-- Unitary tests - % SPECIAL REQUIREMENTS % none -% Copyright (C) 2015-2017 Dynare Team +% Copyright (C) 2015-2020 Dynare Team % % This file is part of Dynare. % @@ -205,7 +205,7 @@ end %$ density = @(x) exp(lpdfgweibull(x,shape,scale)); %$ %$ try -%$ if isoctave || matlab_ver_less_than('7.14') +%$ if isoctave %$ s = quadl(density, .0000000001, 100000, 1e-10); %$ else %$ s = integral(density, 0, 100000); @@ -228,7 +228,7 @@ end %$ density = @(x) exp(lpdfgweibull(x,shape,scale)); %$ %$ try -%$ if isoctave || matlab_ver_less_than('7.14') +%$ if isoctave %$ s = quadl(density, .0000000001, 100000, 1e-10); %$ else %$ s = integral(density, 0, 100000); @@ -251,7 +251,7 @@ end %$ density = @(x) exp(lpdfgweibull(x,shape,scale)); %$ %$ try -%$ if isoctave || matlab_ver_less_than('7.14') +%$ if isoctave %$ s = quadl(density, .0000000001, 100000, 1e-10); %$ else %$ s = integral(density, 0, 100000); @@ -262,7 +262,7 @@ end %$ end %$ %$ if t(1) -%$ if isoctave || matlab_ver_less_than('7.14') +%$ if isoctave %$ t(2) = abs(s-1)<5e-5; %$ else %$ t(2) = abs(s-1)<1e-6; @@ -280,8 +280,6 @@ end %$ try %$ if isoctave %$ s = quadgk(xdens, .0000000001, 100000, 1e-10); -%$ elseif matlab_ver_less_than('7.14') -%$ s = quadgk(xdens, .0000000001, 100000, 'AbsTol', 1e-10); %$ else %$ s = integral(xdens, 0, 100000); %$ end @@ -303,7 +301,7 @@ end %$ xdens = @(x) x.*exp(lpdfgweibull(x,shape,scale)); %$ %$ try -%$ if isoctave || matlab_ver_less_than('7.14') +%$ if isoctave %$ s = quadl(xdens, .0000000001, 100000, 1e-10); %$ else %$ s = integral(xdens, 0, 100000); @@ -326,7 +324,7 @@ end %$ xdens = @(x) x.*exp(lpdfgweibull(x,shape,scale)); %$ %$ try -%$ if isoctave || matlab_ver_less_than('7.14') +%$ if isoctave %$ s = quadl(xdens, .0000000001, 100000, 1e-10); %$ else %$ s = integral(xdens, 0, 100000); @@ -352,7 +350,7 @@ end %$ try %$ s = NaN(n, 1); %$ for i=1:n -%$ if isoctave || matlab_ver_less_than('7.14') +%$ if isoctave %$ s(i) = quadl(density, .0000000001, .1*i, 1e-10); %$ else %$ s(i) = integral(density, 0, .1*i); @@ -383,7 +381,7 @@ end %$ try %$ s = NaN(n, 1); %$ for i=1:n -%$ if isoctave || matlab_ver_less_than('7.14') +%$ if isoctave %$ s(i) = quadl(density, .0000000001, .1*i, 1e-10); %$ else %$ s(i) = integral(density, 0, .1*i); @@ -414,7 +412,7 @@ end %$ try %$ s = NaN(n, 1); %$ for i=1:n -%$ if isoctave || matlab_ver_less_than('7.14') +%$ if isoctave %$ s(i) = quadl(density, .0000000001, .1*i, 1e-10); %$ else %$ s(i) = integral(density, 0, .1*i); @@ -429,7 +427,7 @@ end %$ for i=1:n %$ x = .1*i; %$ q = 1-exp(-(x/scale)^shape); -%$ if isoctave || matlab_ver_less_than('7.14') +%$ if isoctave %$ t(i+1) = abs(s(i)-q)<5e-5; %$ else %$ t(i+1) = abs(s(i)-q)<1e-6; diff --git a/matlab/missing/narginchk/narginchk.m b/matlab/distributions/prior_dist_names.m similarity index 71% rename from matlab/missing/narginchk/narginchk.m rename to matlab/distributions/prior_dist_names.m index 645f3afaa76282c660e3481d02735ae72feba8bb..4d959579fa7773ab90908f9a05affdfb930ca5d5 100644 --- a/matlab/missing/narginchk/narginchk.m +++ b/matlab/distributions/prior_dist_names.m @@ -1,6 +1,7 @@ -function narginchk(minArgs,maxArgs) - -% Copyright (C) 2019 Dynare Team +function pnames=prior_dist_names +%function pnames=prior_dist_names +% Provides the name strings for the prior distribution codes in bayestopt_.pshape +% Copyright (C) 2020 Dynare Team % % This file is part of Dynare. % @@ -17,12 +18,4 @@ function narginchk(minArgs,maxArgs) % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <http://www.gnu.org/licenses/>. -n = evalin('caller', 'nargin;'); - -if n < minArgs - error('narginchk: not enough input arguments'); -end - -if n > maxArgs - error('narginchk: too many input arguments'); -end +pnames={''; 'beta'; 'gamm'; 'norm'; 'invg'; 'unif'; 'invg2'; ''; 'weibl'}; \ No newline at end of file diff --git a/matlab/dr_block.m b/matlab/dr_block.m index d65248b7745ccd275598803f94d790888d7fe0cb..bb51b72610bbb94f35760680f8d37d2a65c17669 100644 --- a/matlab/dr_block.m +++ b/matlab/dr_block.m @@ -70,10 +70,15 @@ else data = M_; Size = 1; end -if (options_.bytecode) - [zz, data]= bytecode('dynamic','evaluate', z, zx, M_.params, dr.ys, 1, data); +if options_.bytecode + [~, data]= bytecode('dynamic','evaluate', z, zx, M_.params, dr.ys, 1, data); else - [r, data] = feval([M_.fname '.dynamic'], options_, M_, oo_, z', zx, M_.params, dr.ys, M_.maximum_lag+1, data); + T=NaN(M_.block_structure.dyn_tmp_nbr, 1); + it_=M_.maximum_lag+1; + y=dynvars_from_endo_simul(z, it_, M_); + for blk = 1:length(M_.block_structure.block) + [~, ~, T, data(blk).g1, data(blk).g1_x, data(blk).g1_xd, data(blk).g1_o]=feval([M_.fname '.dynamic'], blk, y, zx, M_.params, dr.ys, T, it_, true); + end end dr.full_rank = 1; dr.eigval = []; diff --git a/matlab/dsge_likelihood.m b/matlab/dsge_likelihood.m index 7b568277c201d5bc8f86b3fc41d531e896f803c4..917ef8bfb37f07a64187d8078564b1da157d22f1 100644 --- a/matlab/dsge_likelihood.m +++ b/matlab/dsge_likelihood.m @@ -180,86 +180,13 @@ end %------------------------------------------------------------------------------ % 1. Get the structural parameters & define penalties %------------------------------------------------------------------------------ - -% Return, with endogenous penalty, if some parameters are smaller than the lower bound of the prior domain. -if isestimation(DynareOptions) && ~isequal(DynareOptions.mode_compute,1) && any(xparam1<BoundsInfo.lb) - k = find(xparam1<BoundsInfo.lb); - fval = Inf; - exit_flag = 0; - info(1) = 41; - info(4)= sum((BoundsInfo.lb(k)-xparam1(k)).^2); - if analytic_derivation - DLIK=ones(length(xparam1),1); - end - return -end - -% Return, with endogenous penalty, if some parameters are greater than the upper bound of the prior domain. -if isestimation(DynareOptions) && ~isequal(DynareOptions.mode_compute,1) && any(xparam1>BoundsInfo.ub) - k = find(xparam1>BoundsInfo.ub); - fval = Inf; - exit_flag = 0; - info(1) = 42; - info(4)= sum((xparam1(k)-BoundsInfo.ub(k)).^2); - if analytic_derivation - DLIK=ones(length(xparam1),1); - end - return -end - -% Get the diagonal elements of the covariance matrices for the structural innovations (Q) and the measurement error (H). Model = set_all_parameters(xparam1,EstimatedParameters,Model); -Q = Model.Sigma_e; -H = Model.H; - -% Test if Q is positive definite. -if ~issquare(Q) || EstimatedParameters.ncx || isfield(EstimatedParameters,'calibrated_covariances') - [Q_is_positive_definite, penalty] = ispd(Q(EstimatedParameters.Sigma_e_entries_to_check_for_positive_definiteness,EstimatedParameters.Sigma_e_entries_to_check_for_positive_definiteness)); - if ~Q_is_positive_definite - fval = Inf; - exit_flag = 0; - info(1) = 43; - info(4) = penalty; - return - end - if isfield(EstimatedParameters,'calibrated_covariances') - correct_flag=check_consistency_covariances(Q); - if ~correct_flag - penalty = sum(Q(EstimatedParameters.calibrated_covariances.position).^2); - fval = Inf; - exit_flag = 0; - info(1) = 71; - info(4) = penalty; - return - end - end -end - -% Test if H is positive definite. -if ~issquare(H) || EstimatedParameters.ncn || isfield(EstimatedParameters,'calibrated_covariances_ME') - [H_is_positive_definite, penalty] = ispd(H(EstimatedParameters.H_entries_to_check_for_positive_definiteness,EstimatedParameters.H_entries_to_check_for_positive_definiteness)); - if ~H_is_positive_definite - fval = Inf; - info(1) = 44; - info(4) = penalty; - exit_flag = 0; - return - end - if isfield(EstimatedParameters,'calibrated_covariances_ME') - correct_flag=check_consistency_covariances(H); - if ~correct_flag - penalty = sum(H(EstimatedParameters.calibrated_covariances_ME.position).^2); - fval = Inf; - exit_flag = 0; - info(1) = 72; - info(4) = penalty; - return - end - end +[fval,info,exit_flag,Q,H]=check_bounds_and_definiteness_estimation(xparam1, Model, EstimatedParameters, BoundsInfo); +if info(1) + return end - %------------------------------------------------------------------------------ % 2. call model setup & reduction program %------------------------------------------------------------------------------ diff --git a/matlab/dsge_simulated_theoretical_conditional_variance_decomposition.m b/matlab/dsge_simulated_theoretical_conditional_variance_decomposition.m index 540b2e2392257cb05356f0ae7f50c9de8ea17782..22ced1fe5c37d79defee7d76cb1133054750030f 100644 --- a/matlab/dsge_simulated_theoretical_conditional_variance_decomposition.m +++ b/matlab/dsge_simulated_theoretical_conditional_variance_decomposition.m @@ -82,8 +82,8 @@ NumberOfSavedElementsPerSimulation = nvar*M_.exo_nbr*length(Steps); MaXNumberOfConditionalDecompLines = ceil(options_.MaxNumberOfBytes/NumberOfSavedElementsPerSimulation/8); ME_present=0; -if ~all(M_.H==0) - if isoctave || matlab_ver_less_than('8.1') +if ~all(diag(M_.H)==0) + if isoctave [observable_pos_requested_vars,index_subset,index_observables]=intersect_stable(ivar,options_.varobs_id); else [observable_pos_requested_vars,index_subset,index_observables]=intersect(ivar,options_.varobs_id,'stable'); @@ -131,20 +131,20 @@ linea = 0; linea_ME = 0; for file = 1:NumberOfDrawsFiles if posterior - load([M_.dname '/metropolis/' M_.fname '_' type '_draws' num2str(file) ]); + temp=load([M_.dname '/metropolis/' M_.fname '_' type '_draws' num2str(file) ]); else - load([M_.dname '/prior/draws/' type '_draws' num2str(file) ]); + temp=load([M_.dname '/prior/draws/' type '_draws' num2str(file) ]); end - isdrsaved = columns(pdraws)-1; - NumberOfDraws = rows(pdraws); + isdrsaved = columns(temp.pdraws)-1; + NumberOfDraws = rows(temp.pdraws); for linee = 1:NumberOfDraws linea = linea+1; linea_ME = linea_ME+1; if isdrsaved - M_=set_parameters_locally(M_,pdraws{linee,1});% Needed to update the covariance matrix of the state innovations. - dr = pdraws{linee,2}; + M_=set_parameters_locally(M_,temp.pdraws{linee,1});% Needed to update the covariance matrix of the state innovations. + dr = temp.pdraws{linee,2}; else - M_=set_parameters_locally(M_,pdraws{linee,1}); + M_=set_parameters_locally(M_,temp.pdraws{linee,1}); [dr,info,M_,options_,oo_] = resol(0,M_,options_,oo_); end if first_call @@ -163,6 +163,7 @@ for file = 1:NumberOfDrawsFiles end [StateSpaceModel.transition_matrix,StateSpaceModel.impulse_matrix] = kalman_transition_matrix(dr,iv,ic,M_.exo_nbr); StateSpaceModel.state_innovations_covariance_matrix = M_.Sigma_e; + M_ = set_measurement_errors(temp.pdraws{linee,1},temp.estim_params_,M_); StateSpaceModel.measurement_error=M_.H; clear('dr'); [ConditionalVarianceDecomposition, ConditionalVarianceDecomposition_ME]=conditional_variance_decomposition(StateSpaceModel, Steps, ivar); diff --git a/matlab/dsge_simulated_theoretical_correlation.m b/matlab/dsge_simulated_theoretical_correlation.m index fb00d5ddcd537074f918a8cc63c565a5b79da4d4..c32aa9db4226eb65bf8dd821180867ceb76017c7 100644 --- a/matlab/dsge_simulated_theoretical_correlation.m +++ b/matlab/dsge_simulated_theoretical_correlation.m @@ -94,19 +94,19 @@ CorrFileNumber = 1; linea = 0; for file = 1:NumberOfDrawsFiles if posterior - load([M_.dname '/metropolis/' M_.fname '_' type '_draws' num2str(file) ]); + temp=load([M_.dname '/metropolis/' M_.fname '_' type '_draws' num2str(file) ]); else - load([M_.dname '/prior/draws/' type '_draws' num2str(file) ]); + temp=load([M_.dname '/prior/draws/' type '_draws' num2str(file) ]); end - NumberOfDraws = rows(pdraws); - isdrsaved = columns(pdraws)-1; + NumberOfDraws = rows(temp.pdraws); + isdrsaved = columns(temp.pdraws)-1; for linee = 1:NumberOfDraws linea = linea+1; if isdrsaved - M_=set_parameters_locally(M_,pdraws{linee,1});% Needed to update the covariance matrix of the state innovations. - dr = pdraws{linee,2}; + M_=set_parameters_locally(M_,temp.pdraws{linee,1});% Needed to update the covariance matrix of the state innovations. + dr = temp.pdraws{linee,2}; else - M_=set_parameters_locally(M_,pdraws{linee,1}); + M_=set_parameters_locally(M_,temp.pdraws{linee,1}); [dr,info,M_,options_,oo_] = resol(0,M_,options_,oo_); end tmp = th_autocovariances(dr,ivar,M_,options_,nodecomposition); diff --git a/matlab/dsge_simulated_theoretical_covariance.m b/matlab/dsge_simulated_theoretical_covariance.m index 0be948bc3c2f7f01601853278939e3af91058637..bbd4702751335d3d918386a26aa4b7c3a2f17908 100644 --- a/matlab/dsge_simulated_theoretical_covariance.m +++ b/matlab/dsge_simulated_theoretical_covariance.m @@ -93,19 +93,19 @@ CovarFileNumber = 1; linea = 0; for file = 1:NumberOfDrawsFiles if posterior - load([M_.dname '/metropolis/' M_.fname '_' type '_draws' num2str(file) ]); + temp=load([M_.dname '/metropolis/' M_.fname '_' type '_draws' num2str(file) ]); else - load([M_.dname '/prior/draws/' type '_draws' num2str(file) ]); + temp=load([M_.dname '/prior/draws/' type '_draws' num2str(file) ]); end - NumberOfDraws = rows(pdraws); - isdrsaved = columns(pdraws)-1; + NumberOfDraws = rows(temp.pdraws); + isdrsaved = columns(temp.pdraws)-1; for linee = 1:NumberOfDraws linea = linea+1; if isdrsaved - M_=set_parameters_locally(M_,pdraws{linee,1});% Needed to update the covariance matrix of the state innovations. - dr = pdraws{linee,2}; + M_=set_parameters_locally(M_,temp.pdraws{linee,1});% Needed to update the covariance matrix of the state innovations. + dr = temp.pdraws{linee,2}; else - M_=set_parameters_locally(M_,pdraws{linee,1}); + M_=set_parameters_locally(M_,temp.pdraws{linee,1}); [dr,info,M_,options_,oo_] = resol(0,M_,options_,oo_); end tmp = th_autocovariances(dr,ivar,M_,options_,nodecomposition); diff --git a/matlab/dsge_simulated_theoretical_variance_decomposition.m b/matlab/dsge_simulated_theoretical_variance_decomposition.m index 7e6942c1943c25348728b2ff0d53d88a2449a80c..b93aa246ed7f5370f8dc7bec05aef5eb4be4a41a 100644 --- a/matlab/dsge_simulated_theoretical_variance_decomposition.m +++ b/matlab/dsge_simulated_theoretical_variance_decomposition.m @@ -85,8 +85,8 @@ NumberOfSavedElementsPerSimulation = nvar*(nexo+1); MaXNumberOfDecompLines = ceil(options_.MaxNumberOfBytes/NumberOfSavedElementsPerSimulation/8); ME_present=0; -if ~all(M_.H==0) - if isoctave || matlab_ver_less_than('8.1') +if ~all(diag(M_.H)==0) + if isoctave [observable_pos_requested_vars,index_subset,index_observables]=intersect_stable(ivar,options_.varobs_id); else [observable_pos_requested_vars,index_subset,index_observables]=intersect(ivar,options_.varobs_id,'stable'); @@ -130,20 +130,20 @@ linea_ME = 0; only_non_stationary_vars=0; for file = 1:NumberOfDrawsFiles if posterior - load([M_.dname '/metropolis/' M_.fname '_' type '_draws' num2str(file) ]); + temp=load([M_.dname '/metropolis/' M_.fname '_' type '_draws' num2str(file) ]); else - load([M_.dname '/prior/draws/' type '_draws' num2str(file) ]); + temp=load([M_.dname '/prior/draws/' type '_draws' num2str(file) ]); end - isdrsaved = columns(pdraws)-1; - NumberOfDraws = rows(pdraws); + isdrsaved = columns(temp.pdraws)-1; + NumberOfDraws = rows(temp.pdraws); for linee = 1:NumberOfDraws linea = linea+1; linea_ME = linea_ME+1; if isdrsaved - M_=set_parameters_locally(M_,pdraws{linee,1});% Needed to update the covariance matrix of the state innovations. - dr = pdraws{linee,2}; + M_=set_parameters_locally(M_,temp.pdraws{linee,1});% Needed to update the covariance matrix of the state innovations. + dr = temp.pdraws{linee,2}; else - M_=set_parameters_locally(M_,pdraws{linee,1}); + M_=set_parameters_locally(M_,temp.pdraws{linee,1}); [dr,info,M_,options_,oo_] = resol(0,M_,options_,oo_); end if file==1 && linee==1 @@ -164,6 +164,7 @@ for file = 1:NumberOfDrawsFiles end end if ME_present + M_ = set_measurement_errors(temp.pdraws{linee,1},temp.estim_params_,M_); ME_Variance=diag(M_.H); tmp_ME=NaN(nobs_ME,nexo+1); tmp_ME(:,1:end-1)=tmp{2}(index_subset,:).*repmat(diag(tmp{1}(index_subset,index_subset))./(diag(tmp{1}(index_subset,index_subset))+ME_Variance(index_observables)),1,nexo); diff --git a/matlab/dsge_var_likelihood.m b/matlab/dsge_var_likelihood.m index d68ed2149fa0fe7bf05cfb9a2816022d3f458825..596136ce860fae2d114315f01c0a2047fc2842a0 100644 --- a/matlab/dsge_var_likelihood.m +++ b/matlab/dsge_var_likelihood.m @@ -101,38 +101,13 @@ mYX = evalin('base', 'mYX'); mXY = evalin('base', 'mXY'); mXX = evalin('base', 'mXX'); -% Return, with endogenous penalty, if some dsge-parameters are smaller than the lower bound of the prior domain. -if isestimation(DynareOptions) && DynareOptions.mode_compute ~= 1 && any(xparam1 < BoundsInfo.lb) - k = find(xparam1 < BoundsInfo.lb); - fval = Inf; - exit_flag = 0; - info(1) = 41; - info(4)= sum((BoundsInfo.lb(k)-xparam1(k)).^2); - return -end +Model = set_all_parameters(xparam1,EstimatedParameters,Model); -% Return, with endogenous penalty, if some dsge-parameters are greater than the upper bound of the prior domain. -if isestimation(DynareOptions) && DynareOptions.mode_compute ~= 1 && any(xparam1 > BoundsInfo.ub) - k = find(xparam1 > BoundsInfo.ub); - fval = Inf; - exit_flag = 0; - info(1) = 42; - info(4) = sum((xparam1(k)-BoundsInfo.ub(k)).^2); +[fval,info,exit_flag,Q]=check_bounds_and_definiteness_estimation(xparam1, Model, EstimatedParameters, BoundsInfo); +if info(1) return end -% Get the variance of each structural innovation. -Q = Model.Sigma_e; -for i=1:EstimatedParameters.nvx - k = EstimatedParameters.var_exo(i,1); - Q(k,k) = xparam1(i)*xparam1(i); -end -offset = EstimatedParameters.nvx; - -% Update Model.params and Model.Sigma_e. -Model.params(EstimatedParameters.param_vals(:,1)) = xparam1(offset+1:end); -Model.Sigma_e = Q; - % Get the weight of the dsge prior. dsge_prior_weight = Model.params(dsge_prior_weight_idx); diff --git a/matlab/dyn_latex_table.m b/matlab/dyn_latex_table.m index d55a2c7dc0a546f66778df1654e461894ab87c35..5431145ccb97861a972f046177739d28437e7feb 100644 --- a/matlab/dyn_latex_table.m +++ b/matlab/dyn_latex_table.m @@ -26,7 +26,7 @@ if length(headers) < 2 error('headers length must be >= 2') end -OutputDirectoryName = CheckPath('Output',M_.dname); +OutputDirectoryName = CheckPath('latex',M_.dname); % Set width of label column if isempty(label_width) diff --git a/matlab/dyn_risky_steadystate_solver.m b/matlab/dyn_risky_steadystate_solver.m index 93c75da4dc4d2b98b92f3f9fafca2668fe0b888e..7fa8a59ffecc9c07644f9f83a805ecbe69ac3617 100644 --- a/matlab/dyn_risky_steadystate_solver.m +++ b/matlab/dyn_risky_steadystate_solver.m @@ -62,7 +62,7 @@ function [dr,info] = dyn_risky_steadystate_solver(ys0,M, ... %! @end deftypefn %@eod: -% Copyright (C) 2001-2018 Dynare Team +% Copyright (C) 2001-2020 Dynare Team % % This file is part of Dynare. % @@ -165,12 +165,6 @@ if ~isreal(d1) || ~isreal(d2) pause end -if options.use_dll - % In USE_DLL mode, the hessian is in the 3-column sparse representation - d2 = sparse(d2(:,1), d2(:,2), d2(:,3), ... - size(d1, 1), size(d1, 2)*size(d1, 2)); -end - if isfield(options,'portfolio') && options.portfolio == 1 pm = portfolio_model_structure(M,options); x = ys(pm.v_p); @@ -224,12 +218,6 @@ if ~isreal(d1) || ~isreal(d2) pause end -if options.use_dll - % In USE_DLL mode, the hessian is in the 3-column sparse representation - d2 = sparse(d2(:,1), d2(:,2), d2(:,3), ... - size(d1, 1), size(d1, 2)*size(d1, 2)); -end - gu1 = dr_np.ghu(pm.i_fwrd_g,:); @@ -257,12 +245,6 @@ if ~isreal(d1) || ~isreal(d2) pause end -if options.use_dll - % In USE_DLL mode, the hessian is in the 3-column sparse representation - d2 = sparse(d2(:,1), d2(:,2), d2(:,3), ... - size(d1, 1), size(d1, 2)*size(d1, 2)); -end - d1_np = d1(pm.eq_np,pm.i_d1_np); d2_np = d2(pm.eq_np,pm.i_d2_np); diff --git a/matlab/dynare.m b/matlab/dynare.m index 32ea530d9a612c3145adb8a5487e200d03ebfd9d..f2460990155d604c713fe2590d0bbf688e8ec46e 100644 --- a/matlab/dynare.m +++ b/matlab/dynare.m @@ -82,10 +82,10 @@ if isoctave skipline() end else - if matlab_ver_less_than('7.9') % Should match the test in mex/build/matlab/configure.ac + if matlab_ver_less_than('8.3') % Should match the test in mex/build/matlab/configure.ac % and in m4/ax_mexopts.m4 skipline() - warning('This version of Dynare has only been tested on MATLAB 7.9 (R2009b) and above. Since your MATLAB version is older than that, Dynare may fail to run, or give unexpected results. Consider upgrading your MATLAB installation, or switch to Octave.'); + warning('This version of Dynare has only been tested on MATLAB 8.3 (R2014a) and above. Since your MATLAB version is older than that, Dynare may fail to run, or give unexpected results. Consider upgrading your MATLAB installation, or switch to Octave.'); skipline() end end @@ -137,14 +137,6 @@ if fnamelength + length('.set_auxiliary_variables') > namelengthmax() error('Dynare: the name of your .mod file is too long, please shorten it') end -% Workaround for a strange bug with Octave: if there is any call to exist(fname) -% before the call to the preprocessor, then Octave will use the old copy of -% the .m instead of the newly generated one. Deleting the .m beforehand -% fixes the problem. -if isoctave && length(dir([fname(1:(end-4)) '.m'])) > 0 - delete([fname(1:(end-4)) '.m']) -end - if ~isempty(strfind(fname,filesep)) fprintf('\nIt seems you are trying to call a .mod file not located in the "Current Folder". This is not possible (the %s symbol is not allowed in the name of the .mod file).\n', filesep) [pathtomodfile,basename] = fileparts(fname); @@ -200,14 +192,8 @@ end if isempty(strfind(arch, '64')) arch_ext = '32'; - if preprocessoroutput - disp('Using 32-bit preprocessor'); - end else arch_ext = '64'; - if preprocessoroutput - disp('Using 64-bit preprocessor'); - end end if preprocessoroutput @@ -238,6 +224,16 @@ if ~isempty(varargin) command = [command ' ' dynare_varargin]; end +% On MATLAB+Windows, the +folder may be locked by MATLAB, preventing its +% removal by the preprocessor. +% Trying to delete it here will actually fail, but surprisingly this allows +% the preprocessor to actually remove the folder (see ModFile::writeOutputFiles()) +% For an instance of this bug, see: +% https://forum.dynare.org/t/issue-with-dynare-preprocessor-4-6-1/15448/1 +if ispc && ~isoctave && exist(['+',fname(1:end-4)],'dir') + [~,~]=rmdir(['+', fname(1:end-4)],'s'); +end + % Under Windows, make sure the MEX file is unloaded (in the use_dll case), % otherwise the preprocessor can't recompile it if isoctave diff --git a/matlab/dynare_config.m b/matlab/dynare_config.m index 58a3ba3f338f2f5b708b38202368eff81d1ab390..a513b0474bd1927b90b9fba36b8375151034d7fb 100644 --- a/matlab/dynare_config.m +++ b/matlab/dynare_config.m @@ -61,8 +61,9 @@ p = {'/distributions/' ; ... '/cli/' ; ... '/lmmcp/' ; ... '/optimization/' ; ... - '/ols/'; ... - '/pac-tools/'; ... + '/ols/' ; ... + '/pac-tools/' ; ... + '/method_of_moments/' ; ... '/discretionary_policy/' ; ... '/accessors/' ; ... '/modules/dseries/src/' ; ... @@ -89,8 +90,8 @@ if isoctave && octave_ver_less_than('5') p{end+1} = '/missing/ordeig'; end -%% intersect(…, 'stable') doesn't exist in Octave and in MATLAB < R2013a -if isoctave || matlab_ver_less_than('8.1') +%% intersect(…, 'stable') doesn't exist in Octave +if isoctave p{end+1} = '/missing/intersect_stable'; end @@ -118,33 +119,12 @@ if (isoctave && octave_ver_less_than('5')) || (~isoctave && matlab_ver_less_than p{end+1} = '/missing/isfile'; end -% strsplit and strjoin are missing in MATLAB < R2013a -if ~isoctave && matlab_ver_less_than('8.1') - p{end+1} = '/missing/strsplit'; - p{end+1} = '/missing/strjoin'; -end - % contains and splitlines don't exist in Octave and in MATLAB < R2016b if isoctave || matlab_ver_less_than('9.1') p{end+1} = '/missing/contains'; p{end+1} = '/missing/splitlines'; end -% isrow, iscolumn and ismatrix are missing in Matlab<R2010b -if ~isoctave && matlab_ver_less_than('7.11') - p{end+1} = '/missing/is-row-column-matrix'; -end - -%% isdiag is missing in MATLAB < R2014a -if ~isoctave && matlab_ver_less_than('8.3') - p{end+1} = '/missing/isdiag'; -end - -%% narginchk is missing in MATLAB < R2011b -if ~isoctave && matlab_ver_less_than('7.13') - p{end+1} = '/missing/narginchk'; -end - P = cellfun(@(c)[dynareroot(1:end-1) c], p, 'uni',false); % Get mex files folder(s) diff --git a/matlab/dynare_estimation_1.m b/matlab/dynare_estimation_1.m index e27931d1d73a8e7078ab72b8a09387ac1e37ed7e..ca97cd30f569daec4b0c021a452904f265f7dad0 100644 --- a/matlab/dynare_estimation_1.m +++ b/matlab/dynare_estimation_1.m @@ -149,9 +149,6 @@ ncn = estim_params_.ncn; % Covariance of the measurement innovations (number of np = estim_params_.np ; % Number of deep parameters. nx = nvx+nvn+ncx+ncn+np; % Total number of parameters to be estimated. -% Set the names of the priors. -pnames = {''; 'beta'; 'gamm'; 'norm'; 'invg'; 'unif'; 'invg2'; ''; 'weibl'}; - dr = oo_.dr; if ~isempty(estim_params_) @@ -268,7 +265,7 @@ if ~isequal(options_.mode_compute,0) && ~options_.mh_posterior_mode_estimation if compute_hessian crit = options_.newrat.tolerance.f; newratflag = newratflag>0; - hh = reshape(mr_hessian(xparam1,objective_function,fval,newratflag,crit,new_rat_hess_info,dataset_, dataset_info, options_,M_,estim_params_,bayestopt_,bounds,oo_), nx, nx); + hh = reshape(mr_hessian(xparam1,objective_function,fval,newratflag,crit,new_rat_hess_info,[bounds.lb bounds.ub],bayestopt_.p2,dataset_, dataset_info, options_,M_,estim_params_,bayestopt_,bounds,oo_), nx, nx); end options_.kalman_algo = kalman_algo0; end @@ -357,7 +354,7 @@ end if any(bayestopt_.pshape > 0) && ~options_.mh_posterior_mode_estimation % display results table and store parameter estimates and standard errors in results - oo_ = display_estimation_results_table(xparam1, stdh, M_, options_, estim_params_, bayestopt_, oo_, pnames, 'Posterior', 'posterior'); + oo_ = display_estimation_results_table(xparam1, stdh, M_, options_, estim_params_, bayestopt_, oo_, prior_dist_names, 'Posterior', 'posterior'); % Laplace approximation to the marginal log density: if options_.cova_compute estim_params_nbr = size(xparam1,1); @@ -378,7 +375,7 @@ if any(bayestopt_.pshape > 0) && ~options_.mh_posterior_mode_estimation end elseif ~any(bayestopt_.pshape > 0) && ~options_.mh_posterior_mode_estimation - oo_=display_estimation_results_table(xparam1, stdh, M_, options_, estim_params_, bayestopt_, oo_, pnames, 'Maximum Likelihood', 'mle'); + oo_=display_estimation_results_table(xparam1, stdh, M_, options_, estim_params_, bayestopt_, oo_, prior_dist_names, 'Maximum Likelihood', 'mle'); end if np > 0 @@ -490,7 +487,7 @@ if (any(bayestopt_.pshape >0 ) && options_.mh_replic) || ... if options_.mh_replic || (options_.load_mh_file && ~options_.load_results_after_load_mh) [marginal,oo_] = marginal_density(M_, options_, estim_params_, oo_, bayestopt_); % Store posterior statistics by parameter name - oo_ = GetPosteriorParametersStatistics(estim_params_, M_, options_, bayestopt_, oo_, pnames); + oo_ = GetPosteriorParametersStatistics(estim_params_, M_, options_, bayestopt_, oo_, prior_dist_names); if ~options_.nograph oo_ = PlotPosteriorDistributions(estim_params_, M_, options_, bayestopt_, oo_); end @@ -589,11 +586,6 @@ if (~((any(bayestopt_.pshape > 0) && options_.mh_replic) || (any(bayestopt_.psha plot(1:gend,innov(k,:),marker_string{2,1},'linewidth',1) hold off name = M_.exo_names{k}; - if isempty(NAMES) - NAMES = name; - else - NAMES = char(NAMES,name); - end if ~isempty(options_.XTick) set(gca,'XTick',options_.XTick) set(gca,'XTickLabel',options_.XTickLabel) @@ -602,21 +594,14 @@ if (~((any(bayestopt_.pshape > 0) && options_.mh_replic) || (any(bayestopt_.psha xlim([1 gend]) end if options_.TeX - texname = M_.exo_names_tex{k}; - if isempty(TeXNAMES) - TeXNAMES = ['$ ' deblank(texname) ' $']; - else - TeXNAMES = char(TeXNAMES,['$ ' deblank(texname) ' $']); - end + title(['$' M_.exo_names_tex{k} '$'],'Interpreter','latex') + else + title(name,'Interpreter','none') end - title(name,'Interpreter','none') end dyn_saveas(fh,[M_.fname, '/graphs/' M_.fname '_SmoothedShocks' int2str(plt)],options_.nodisplay,options_.graph_format); if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format))) fprintf(fidTeX,'\\begin{figure}[H]\n'); - for jj = 1:nstar0 - fprintf(fidTeX,'\\psfrag{%s}[1][][0.5][0]{%s}\n',deblank(NAMES(jj,:)),deblank(TeXNAMES(jj,:))); - end fprintf(fidTeX,'\\centering \n'); fprintf(fidTeX,'\\includegraphics[width=%2.2f\\textwidth]{%s_SmoothedShocks%s}\n',options_.figures.textwidth*min(i/nc,1),[M_.fname, '/graphs/' M_.fname],int2str(plt)); fprintf(fidTeX,'\\caption{Smoothed shocks.}'); @@ -650,8 +635,6 @@ if (~((any(bayestopt_.pshape > 0) && options_.mh_replic) || (any(bayestopt_.psha end for plt = 1:nbplt fh = dyn_figure(options_.nodisplay,'Name','Smoothed observation errors'); - NAMES = []; - if options_.TeX, TeXNAMES = []; end nstar0=min(nstar,number_of_plots_to_draw-(plt-1)*nstar); if gend==1 marker_string{1,1}='-ro'; @@ -671,32 +654,20 @@ if (~((any(bayestopt_.pshape > 0) && options_.mh_replic) || (any(bayestopt_.psha if gend>1 xlim([1 gend]) end - if isempty(NAMES) - NAMES = name; - else - NAMES = char(NAMES,name); - end if ~isempty(options_.XTick) set(gca,'XTick',options_.XTick) set(gca,'XTickLabel',options_.XTickLabel) end if options_.TeX idx = strmatch(options_.varobs{index(k)}, M_.endo_names, 'exact'); - texname = M_.endo_names_tex{idx}; - if isempty(TeXNAMES) - TeXNAMES = ['$ ' texname ' $']; - else - TeXNAMES = char(TeXNAMES,['$ ' texname ' $']); - end + title(['$' M_.endo_names_tex{idx} '$'],'Interpreter','latex') + else + title(name,'Interpreter','none') end - title(name,'Interpreter','none') end dyn_saveas(fh,[M_.fname, '/graphs/' M_.fname '_SmoothedObservationErrors' int2str(plt)],options_.nodisplay,options_.graph_format); if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format))) fprintf(fidTeX,'\\begin{figure}[H]\n'); - for jj = 1:nstar0 - fprintf(fidTeX,'\\psfrag{%s}[1][][0.5][0]{%s}\n',deblank(NAMES(jj,:)),deblank(TeXNAMES(jj,:))); - end fprintf(fidTeX,'\\centering \n'); fprintf(fidTeX,'\\includegraphics[width=%2.2f\\textwidth]{%s_SmoothedObservationErrors%s}\n',options_.figures.textwidth*min(i/nc,1),[M_.fname, '/graphs/' M_.fname],int2str(plt)); fprintf(fidTeX,'\\caption{Smoothed observation errors.}'); @@ -726,7 +697,6 @@ if (~((any(bayestopt_.pshape > 0) && options_.mh_replic) || (any(bayestopt_.psha for plt = 1:nbplt fh = dyn_figure(options_.nodisplay,'Name','Historical and smoothed variables'); NAMES = []; - if options_.TeX, TeXNAMES = []; end nstar0=min(nstar,n_varobs-(plt-1)*nstar); if gend==1 marker_string{1,1}='-ro'; @@ -743,11 +713,6 @@ if (~((any(bayestopt_.pshape > 0) && options_.mh_replic) || (any(bayestopt_.psha plot(1:gend,rawdata(:,k),marker_string{2,1},'linewidth',1) hold off name = options_.varobs{k}; - if isempty(NAMES) - NAMES = name; - else - NAMES = char(NAMES,name); - end if ~isempty(options_.XTick) set(gca,'XTick',options_.XTick) set(gca,'XTickLabel',options_.XTickLabel) @@ -757,21 +722,14 @@ if (~((any(bayestopt_.pshape > 0) && options_.mh_replic) || (any(bayestopt_.psha end if options_.TeX idx = strmatch(options_.varobs{k}, M_.endo_names,'exact'); - texname = M_.endo_names_tex{idx}; - if isempty(TeXNAMES) - TeXNAMES = ['$ ' texname ' $']; - else - TeXNAMES = char(TeXNAMES,['$ ' texname ' $']); - end + title(['$' M_.endo_names_tex{idx} '$'],'Interpreter','latex') + else + title(name,'Interpreter','none') end - title(name,'Interpreter','none') end dyn_saveas(fh,[M_.fname, '/graphs/' M_.fname '_HistoricalAndSmoothedVariables' int2str(plt)],options_.nodisplay,options_.graph_format); if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format))) fprintf(fidTeX,'\\begin{figure}[H]\n'); - for jj = 1:nstar0 - fprintf(fidTeX,'\\psfrag{%s}[1][][0.5][0]{%s}\n',deblank(NAMES(jj,:)),deblank(TeXNAMES(jj,:))); - end fprintf(fidTeX,'\\centering \n'); fprintf(fidTeX,'\\includegraphics[width=%2.2f\\textwidth]{%s_HistoricalAndSmoothedVariables%s}\n',options_.figures.textwidth*min(i/nc,1),[M_.fname, '/graphs/' M_.fname],int2str(plt)); fprintf(fidTeX,'\\caption{Historical and smoothed variables.}'); diff --git a/matlab/dynare_estimation_init.m b/matlab/dynare_estimation_init.m index 3495a7d04a04825ca72b49aabca0880bc5f1d056..27cc95b34de93c8f74799e04293dc5938a429e2b 100644 --- a/matlab/dynare_estimation_init.m +++ b/matlab/dynare_estimation_init.m @@ -501,11 +501,15 @@ if options_.analytic_derivation error('analytic derivation is incompatible with diffuse filter') end options_.analytic_derivation = 1; - if estim_params_.np + if estim_params_.np || isfield(options_,'identification_check_endogenous_params_with_no_prior') % check if steady state changes param values M=M_; - M.params(estim_params_.param_vals(:,1)) = xparam1(estim_params_.nvx+estim_params_.ncx+estim_params_.nvn+estim_params_.ncn+1:end); %set parameters - M.params(estim_params_.param_vals(:,1)) = M.params(estim_params_.param_vals(:,1))*1.01; %vary parameters + if isfield(options_,'identification_check_endogenous_params_with_no_prior') + M.params = M.params*1.01; %vary parameters + else + M.params(estim_params_.param_vals(:,1)) = xparam1(estim_params_.nvx+estim_params_.ncx+estim_params_.nvn+estim_params_.ncn+1:end); %set parameters + M.params(estim_params_.param_vals(:,1)) = M.params(estim_params_.param_vals(:,1))*1.01; %vary parameters + end if options_.diffuse_filter || options_.steadystate.nocheck steadystate_check_flag = 0; else @@ -622,28 +626,7 @@ end %% get the non-zero rows and columns of Sigma_e and H -H_non_zero_rows=find(~all(M_.H==0,1)); -H_non_zero_columns=find(~all(M_.H==0,2)); -if ~isequal(H_non_zero_rows,H_non_zero_columns') - error('Measurement error matrix not symmetric') -end -if isfield(estim_params_,'nvn_observable_correspondence') - estim_params_.H_entries_to_check_for_positive_definiteness=union(H_non_zero_rows,estim_params_.nvn_observable_correspondence(:,1)); -else - estim_params_.H_entries_to_check_for_positive_definiteness=H_non_zero_rows; -end - -Sigma_e_non_zero_rows=find(~all(M_.Sigma_e==0,1)); -Sigma_e_non_zero_columns=find(~all(M_.Sigma_e==0,2)); -if ~isequal(Sigma_e_non_zero_rows,Sigma_e_non_zero_columns') - error('Structual error matrix not symmetric') -end -if isfield(estim_params_,'var_exo') && ~isempty(estim_params_.var_exo) - estim_params_.Sigma_e_entries_to_check_for_positive_definiteness=union(Sigma_e_non_zero_rows,estim_params_.var_exo(:,1)); -else - estim_params_.Sigma_e_entries_to_check_for_positive_definiteness=Sigma_e_non_zero_rows; -end - +estim_params_= get_matrix_entries_for_psd_check(M_,estim_params_); if options_.load_results_after_load_mh if ~exist([M_.fname '_results.mat'],'file') diff --git a/matlab/dynare_identification.m b/matlab/dynare_identification.m index b864e46d7b4b2a3ed015c7c49ed3e85c078f92b3..063f4e451c91fc7dd117707f8f41695aafe4d792 100644 --- a/matlab/dynare_identification.m +++ b/matlab/dynare_identification.m @@ -191,15 +191,12 @@ else if options_.diffuse_filter==1 %warning if estimation with diffuse filter was done, but not passed fprintf('WARNING IDENTIFICATION: Previously the diffuse_filter option was used, but it was not passed to the identification command. This may result in problems if your model contains unit roots.\n'); end - if isfield(options_ident,'lik_init') - options_.lik_init=options_ident.lik_init; %make options_ inherit lik_init - if options_ident.lik_init==3 %user specified diffuse filter using the lik_init option - options_ident.analytic_derivation=0; %diffuse filter not compatible with analytic derivation - options_.analytic_derivation=0; %diffuse filter not compatible with analytic derivation - end - end end options_ident = set_default_option(options_ident,'lik_init',1); +options_.lik_init=options_ident.lik_init; %make options_ inherit lik_init +if options_ident.lik_init==3 %user specified diffuse filter using the lik_init option + options_ident.analytic_derivation=0; %diffuse filter not compatible with analytic derivation +end % Type of initialization of Kalman filter: % 1: stationary models: initial matrix of variance of error of forecast is set equal to the unconditional variance of the state variables % 2: nonstationary models: wide prior is used with an initial matrix of variance of the error of forecast diagonal with 10 on the diagonal (follows the suggestion of Harvey and Phillips(1979)) @@ -273,6 +270,7 @@ if isempty(estim_params_) %reset some options options_ident.prior_mc = 1; options_ident.prior_range = 0; + options_.identification_check_endogenous_params_with_no_prior = true; %needed to trigger endogenous steady state parameter check in dynare_estimation_init else prior_exist = 1; parameters = options_ident.parameter_set; @@ -283,7 +281,6 @@ options_.order = options_ident.order; if options_ident.order > 1 %order>1 is not compatible with analytic derivation in dsge_likelihood.m options_ident.analytic_derivation=0; - options_.analytic_derivation=0; %order>1 is based on pruned state space system options_.pruning = true; end @@ -294,7 +291,7 @@ options_.ar = options_ident.ar; options_.prior_mc = options_ident.prior_mc; options_.Schur_vec_tol = 1.e-8; options_.nomoments = 0; -options_ = set_default_option(options_,'analytic_derivation',1); %if option was not already set +options_.analytic_derivation=options_ident.analytic_derivation; % 1: analytic derivation of gradient and hessian of likelihood in dsge_likelihood.m, only works for stationary models, i.e. kalman_algo<3 options_ = set_default_option(options_,'datafile',''); options_.mode_compute = 0; diff --git a/matlab/dynare_solve.m b/matlab/dynare_solve.m index e50ea6670527f163a3bc8eb11a0bea7d37b327b2..3bb7ecc03ab5141e52f92f92f16aca0be73be924 100644 --- a/matlab/dynare_solve.m +++ b/matlab/dynare_solve.m @@ -45,8 +45,10 @@ else end if strcmp(caller_file_name,'solve_stacked_problem.m') tolf = options.dynatol.f; + tolx = options.dynatol.x; else tolf = options.solve_tolf; + tolx = options.solve_tolx; end if strcmp(caller_file_name,'dyn_ramsey_static.m') @@ -224,11 +226,11 @@ if options.solve_algo == 0 end elseif options.solve_algo==1 [x, errorflag] = solve1(f, x, 1:nn, 1:nn, jacobian_flag, options.gstep, ... - tolf, options.solve_tolx, ... + tolf, tolx, ... maxit, options.debug, arguments{:}); elseif options.solve_algo==9 [x, errorflag] = trust_region(f, x, 1:nn, 1:nn, jacobian_flag, options.gstep, ... - tolf, options.solve_tolx, ... + tolf, tolx, ... maxit, options.debug, arguments{:}); elseif ismember(options.solve_algo, [2, 12, 4, 14]) if ismember(options.solve_algo, [2, 12]) diff --git a/matlab/dynare_solve_block_or_bytecode.m b/matlab/dynare_solve_block_or_bytecode.m index f991f54f53e725f248fb0e5244fcbaa2581265b3..072e43cdfa3a15e0d28999a8aef4530ad8ae7985 100644 --- a/matlab/dynare_solve_block_or_bytecode.m +++ b/matlab/dynare_solve_block_or_bytecode.m @@ -19,14 +19,15 @@ function [x,info] = dynare_solve_block_or_bytecode(y, exo, params, options, M) info = 0; x = y; if options.block && ~options.bytecode + T = NaN(M.block_structure_stat.tmp_nbr, 1); for b = 1:length(M.block_structure_stat.block) ss = x; if M.block_structure_stat.block(b).Simulation_Type ~= 1 && ... M.block_structure_stat.block(b).Simulation_Type ~= 2 - if options.solve_algo <= 4 + if options.solve_algo <= 4 || options.solve_algo >= 9 [y, check] = dynare_solve('block_mfs_steadystate', ... ss(M.block_structure_stat.block(b).variable), ... - options, b, ss, exo, params, M); + options, b, ss, exo, params, T, M); if check ~= 0 % error(['STEADY: convergence % problems in block ' int2str(b)]) @@ -36,18 +37,19 @@ if options.block && ~options.bytecode ss(M.block_structure_stat.block(b).variable) = y; else n = length(M.block_structure_stat.block(b).variable); - [ss, check] = solve_one_boundary([M.fname '.block.static_' int2str(b)], ss, exo, ... - params, [], M.block_structure_stat.block(b).variable, n, 1, 0, b, 0, options.simul.maxit, ... - options.solve_tolf, ... - options.slowc, 0, options.solve_algo, 1, 0, 0,M,options); + [ss, T, ~, check] = solve_one_boundary([M.fname '.static' ], ss, exo, ... + params, [], T, M.block_structure_stat.block(b).variable, n, 1, false, b, 0, options.simul.maxit, ... + options.solve_tolf, ... + options.slowc, 0, options.solve_algo, true, false, false, M, options, []); if check info = 1; return end end end - [r, g1, x] = feval([M.fname '.static'], b, ss, ... - exo, params); + % Compute endogenous if the block is of type evaluate forward/backward + % Also update the temporary terms vector (needed for the dynare_solve case) + [~, x, T, g1] = feval([M.fname '.static'], b, ss, exo, params, T); end elseif options.bytecode if options.solve_algo > 4 diff --git a/matlab/missing/strsplit/private/ischarint.m b/matlab/dynvars_from_endo_simul.m similarity index 56% rename from matlab/missing/strsplit/private/ischarint.m rename to matlab/dynvars_from_endo_simul.m index d325015d4d05a60b063dbd6b17767041c4e02441..c8508f5352e83ebd22d0ca84a76ffaa2a45c54cd 100644 --- a/matlab/missing/strsplit/private/ischarint.m +++ b/matlab/dynvars_from_endo_simul.m @@ -1,8 +1,11 @@ -function l = ischarint(x) +function y2 = dynvars_from_endo_simul(y, it_, M_) +% Given the matrix y of paths for all endogenous (same format as +% oo_.endo_simul), and an iteration number (first simulation period corresponds +% to it_=M_.maximum_lag+1), return a vector of endogenous values in the format +% expected by the dynamic.m file (i.e. whose indices are described by +% M_.lead_lag_incidence) -% Returns true if and only if char x represents an integer. - -% Copyright © 2018 DynareTeam +% Copyright (C) 2020 Dynare Team % % This file is part of Dynare. % @@ -19,9 +22,5 @@ function l = ischarint(x) % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <http://www.gnu.org/licenses/>. -s = warning; -warning off; - -l = isint(str2double(x)); - -warning(s); \ No newline at end of file +y2 = y(:, it_+(-M_.maximum_endo_lag:M_.maximum_endo_lead)); +y2 = y2(find(M_.lead_lag_incidence')); diff --git a/matlab/ep/extended_path_core.m b/matlab/ep/extended_path_core.m index eb8b2454dd7d0a220c2d35443994491be8147654..99575734277ed5f0c45421fa7b92f001c0c1e1f5 100644 --- a/matlab/ep/extended_path_core.m +++ b/matlab/ep/extended_path_core.m @@ -42,7 +42,7 @@ end if bytecode_flag && ~ep.stochastic.order try - tmp = bytecode('dynamic', endo_simul, exo_simul, M_.params, endo_simul, periods); + tmp = bytecode('dynamic', endo_simul, exo_simul, M.params, endo_simul, periods); flag = false; catch ME disp(ME.message); diff --git a/matlab/ep/extended_path_initialization.m b/matlab/ep/extended_path_initialization.m index fb8581bc687c0c265ef21214cfbb922ab851abbe..33d76339152049ece7675b5ceca8da13ed460a01 100644 --- a/matlab/ep/extended_path_initialization.m +++ b/matlab/ep/extended_path_initialization.m @@ -16,7 +16,7 @@ function [initial_conditions, innovations, pfm, ep, verbosity, DynareOptions, Dy % % SPECIAL REQUIREMENTS -% Copyright (C) 2016-2017 Dynare Team +% Copyright (C) 2016-2020 Dynare Team % % This file is part of Dynare. % @@ -35,6 +35,9 @@ function [initial_conditions, innovations, pfm, ep, verbosity, DynareOptions, Dy ep = DynareOptions.ep; +% Set bytecode flag (see #1717) +ep.use_bytecode = DynareOptions.bytecode; + % Set verbosity levels. DynareOptions.verbosity = ep.verbosity; verbosity = ep.verbosity+ep.debug; diff --git a/matlab/evaluate_static_model.m b/matlab/evaluate_static_model.m index fbcfa235cd30edc8b3b68cc19209b96ae93d6030..a4aa46ddeaf8a68e56c6526c8b4667e100259824 100644 --- a/matlab/evaluate_static_model.m +++ b/matlab/evaluate_static_model.m @@ -45,17 +45,19 @@ else fh_static = str2func([M.fname '.static']); if options.block residuals = zeros(M.endo_nbr,1); + T = NaN(M.block_structure_stat.tmp_nbr, 1); for b = 1:length(M.block_structure_stat.block) mfsb = M.block_structure_stat.block(b).variable; % blocks that can be directly evaluated (mfsb is empty) % have zero residuals by construction if M.block_structure_stat.block(b).Simulation_Type ~= 1 && ... M.block_structure_stat.block(b).Simulation_Type ~= 2 - residuals(mfsb) = feval(fh_static,b,ys,exo_ss,params); + [r, ~, T] = feval(fh_static,b,ys,exo_ss,params,T); + residuals(mfsb) = r; else %need to evaluate the recursive blocks to compute the %temporary terms - feval(fh_static,b,ys,exo_ss,params); + [~, ~, T] = feval(fh_static,b,ys,exo_ss,params,T); end end else diff --git a/matlab/evaluate_steady_state_file.m b/matlab/evaluate_steady_state_file.m index 7a299f0fad12de0a27aa0ed29748dbbe047eb17d..c503f6db2d4f358eeae2d8696aa2a4a357c7da76 100644 --- a/matlab/evaluate_steady_state_file.m +++ b/matlab/evaluate_steady_state_file.m @@ -111,7 +111,7 @@ if steady_state_checkflag info(2) = check; % to be improved return end - if max(abs(residuals)) > options.dynatol.f + if max(abs(residuals)) > options.solve_tolf info(1) = 19; info(2) = residuals'*residuals; return diff --git a/matlab/forecast_graphs.m b/matlab/forecast_graphs.m index e2815c76c5e0b0587c28ea219b530c229aba60b7..00006d0e261affc93a488175cd4118dc3face4eb 100644 --- a/matlab/forecast_graphs.m +++ b/matlab/forecast_graphs.m @@ -87,7 +87,12 @@ for j= 1:nvar plot([NaN(obs,1); hpdinf.(vn)],'b-'); hold on plot([NaN(obs,1); hpdsup.(vn)],'b-'); - title(vn,'Interpreter','none'); + if options_.TeX + temp=strmatch(vn,M_.endo_names,'exact'); + title(['$' M_.endo_names_tex{temp} '$'],'Interpreter','latex'); + else + title(vn,'Interpreter','none'); + end xlim([1 obs+length(hpdsup.(vn))]) hold off m = m + 1; @@ -145,7 +150,12 @@ if isfield(oo_.forecast,'HPDinf_ME') plot([NaN(obs,1); oo_.forecast.HPDinf_ME.(vn)],'b-'); hold on plot([NaN(obs,1); oo_.forecast.HPDsup_ME.(vn)],'b-'); - title(vn,'Interpreter','none'); + if options_.TeX + temp=strmatch(vn,M_.endo_names,'exact'); + title(['$' M_.endo_names_tex{temp} '$'],'Interpreter','latex'); + else + title(vn,'Interpreter','none'); + end xlim([1 obs+length(oo_.forecast.HPDsup_ME.(vn))]) hold off m = m + 1; diff --git a/matlab/get_dynare_random_generator_state.m b/matlab/get_dynare_random_generator_state.m index bf01db4a5fde06f0c745981de538050544f43c10..9aef9a288e0dd18299681b352ac44482071e0b3b 100644 --- a/matlab/get_dynare_random_generator_state.m +++ b/matlab/get_dynare_random_generator_state.m @@ -6,7 +6,7 @@ function [state_u,state_n] = get_dynare_random_generator_state() % For backward compatibility, we return two vectors, but, in recent % versions of Matlab and in Octave, we return two identical vectors. -% Copyright (C) 2010-2017 Dynare Team +% Copyright (C) 2010-2020 Dynare Team % % This file is part of Dynare. % @@ -23,14 +23,8 @@ function [state_u,state_n] = get_dynare_random_generator_state() % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <http://www.gnu.org/licenses/>. -matlab_random_streams = ~isoctave; - -if matlab_random_streams% Use new matlab interface. - if matlab_ver_less_than('7.12') - s = RandStream.getDefaultStream(); - else - s = RandStream.getGlobalStream(); - end +if ~isoctave + s = RandStream.getGlobalStream(); if isequal(s.Type,'legacy') state_u = rand('state'); state_n = randn('state'); @@ -38,7 +32,7 @@ if matlab_random_streams% Use new matlab interface. state_u = s.State; state_n = state_u; end -else% Use old matlab interface. +else state_u = rand('state'); state_n = randn('state'); -end \ No newline at end of file +end diff --git a/matlab/get_identification_jacobians.m b/matlab/get_identification_jacobians.m index 7e7443bfdc230c6554ce7140ac3423fba52d0533..453cb4ef4fde031bdf996e0853ba4bdc915109a2 100644 --- a/matlab/get_identification_jacobians.m +++ b/matlab/get_identification_jacobians.m @@ -241,12 +241,11 @@ if order == 1 end %% Compute dMOMENTS -if ~no_identification_moments - if useautocorr - E_yy = pruned.Corr_y; dE_yy = pruned.dCorr_y; +if ~no_identification_moments + E_yy = pruned.Var_y; dE_yy = pruned.dVar_y; + if useautocorr E_yyi = pruned.Corr_yi; dE_yyi = pruned.dCorr_yi; - else - E_yy = pruned.Var_y; dE_yy = pruned.dVar_y; + else E_yyi = pruned.Var_yi; dE_yyi = pruned.dVar_yi; end MOMENTS = [MEAN; dyn_vech(E_yy)]; diff --git a/matlab/get_matrix_entries_for_psd_check.m b/matlab/get_matrix_entries_for_psd_check.m new file mode 100644 index 0000000000000000000000000000000000000000..1ddd80d3c53f4f032d833b87724733c9d4a9cfba --- /dev/null +++ b/matlab/get_matrix_entries_for_psd_check.m @@ -0,0 +1,55 @@ +function estim_params_= get_matrix_entries_for_psd_check(M_,estim_params_) +% function estim_params_= get_matrix_entries_for_psd_check(M_) +% Get entries of Sigma_e and H to check for positive definiteness +% +% INPUTS +% M_: structure storing the model information +% estim_params_: structure storing information about estimated +% parameters +% OUTPUTS +% estim_params_: structure storing information about estimated +% parameters +% +% SPECIAL REQUIREMENTS +% none + +% Copyright (C) 2020 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/>. + +%% get the non-zero rows and columns of Sigma_e and H + +H_non_zero_rows=find(~all(M_.H==0,1)); +H_non_zero_columns=find(~all(M_.H==0,2)); +if ~isequal(H_non_zero_rows,H_non_zero_columns') || (any(any(M_.H-M_.H'>1e-10))) + error('Measurement error matrix not symmetric') +end +if isfield(estim_params_,'nvn_observable_correspondence') + estim_params_.H_entries_to_check_for_positive_definiteness=union(H_non_zero_rows,estim_params_.nvn_observable_correspondence(:,1)); +else + estim_params_.H_entries_to_check_for_positive_definiteness=H_non_zero_rows; +end + +Sigma_e_non_zero_rows=find(~all(M_.Sigma_e==0,1)); +Sigma_e_non_zero_columns=find(~all(M_.Sigma_e==0,2)); +if ~isequal(Sigma_e_non_zero_rows,Sigma_e_non_zero_columns') || (any(any(M_.Sigma_e-M_.Sigma_e'>1e-10))) + error('Structual error matrix not symmetric') +end +if isfield(estim_params_,'var_exo') && ~isempty(estim_params_.var_exo) + estim_params_.Sigma_e_entries_to_check_for_positive_definiteness=union(Sigma_e_non_zero_rows,estim_params_.var_exo(:,1)); +else + estim_params_.Sigma_e_entries_to_check_for_positive_definiteness=Sigma_e_non_zero_rows; +end \ No newline at end of file diff --git a/matlab/get_the_name.m b/matlab/get_the_name.m index 7da451d11764fee9a6079db8783c8de00df2a5ac..d7dddee6d0b1f3781eaac8d3196cb2a8adf136eb 100644 --- a/matlab/get_the_name.m +++ b/matlab/get_the_name.m @@ -70,14 +70,14 @@ if k <= nvx nam = sprintf('SE_%s', vname); if TeX tname = M_.exo_names_tex{estim_params_.var_exo(k,1)}; - texnam = sprintf('$ SE_{%s} $', tname); + texnam = sprintf('$ \\sigma_{%s} $', tname); end elseif k <= (nvx+nvn) vname = options_.varobs{estim_params_.nvn_observable_correspondence(k-estim_params_.nvx,1)}; nam = sprintf('SE_EOBS_%s', vname); if TeX tname = M_.endo_names_tex{estim_params_.var_endo(k-estim_params_.nvx,1)}; - texnam = sprintf('$ EOBS SE_{%s} $', tname); + texnam = sprintf('$ \\sigma^{ME}_{%s} $', tname); end elseif k <= (nvx+nvn+ncx) jj = k - (nvx+nvn); @@ -87,7 +87,7 @@ elseif k <= (nvx+nvn+ncx) nam = sprintf('CC_%s', vname); if TeX tname = sprintf('%s,%s', M_.exo_names_tex{k1}, M_.exo_names_tex{k2}); - texnam = sprintf('$ CC_{%s} $', tname); + texnam = sprintf('$ \\rho_{%s} $', tname); end elseif k <= (nvx+nvn+ncx+ncn) jj = k - (nvx+nvn+ncx); @@ -97,7 +97,7 @@ elseif k <= (nvx+nvn+ncx+ncn) nam = sprintf('CC_EOBS_%s', vname); if TeX tname = sprintf('%s,%s', M_.endo_names_tex{k1}, M_.endo_names_tex{k2}); - texnam = sprintf('$ EOBS CC_{%s} $', tname); + texnam = sprintf('$ \\rho^{ME}_{%s} $', tname); end else jj = k - (nvx+nvn+ncx+ncn); diff --git a/matlab/global_initialization.m b/matlab/global_initialization.m index a5fca4e23e7c3528930f2d893c4f6222635cd5b0..b3c4d268854d8d7d414a692e8f9788ff059f8496 100644 --- a/matlab/global_initialization.m +++ b/matlab/global_initialization.m @@ -92,6 +92,7 @@ oo_.dr = []; oo_.exo_steady_state = []; oo_.exo_det_steady_state = []; oo_.exo_det_simul = []; +oo_.initval_series = dseries(); oo_.gui.ran_estimation = false; oo_.gui.ran_stoch_simul = false; diff --git a/matlab/hess_element.m b/matlab/hess_element.m index 9881e322efbced548b1f2c0311b6f3f42ee25f16..5af38ecd2a112d6f66d0f1b806c0e4b53bee9622 100644 --- a/matlab/hess_element.m +++ b/matlab/hess_element.m @@ -14,7 +14,7 @@ function d=hess_element(func,element1,element2,args) % SPECIAL REQUIREMENTS % none -% Copyright (C) 2010-2011 Dynare Team +% Copyright (C) 2010-2020 Dynare Team % % This file is part of Dynare. % @@ -31,32 +31,29 @@ function d=hess_element(func,element1,element2,args) % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <http://www.gnu.org/licenses/>. +assert(element1 <= length(args) && element2 <= length(args)); + func = str2func(func); -h=10e-6; +h=1e-6; p10 = args; p01 = args; m10 = args; m01 = args; p11 = args; m11 = args; -for i=1:size(args,2) - if i==element1 - p10{i} = p10{i} + h; - m10{i} = m10{i} - h; - p11{i} = p11{i} + h; - m11{i} = m11{i} - h; - end +p10{element1} = p10{element1} + h; +m10{element1} = m10{element1} - h; - if i==element2 - p01{i} = p01{i} + h; - m01{i} = m01{i} - h; +p11{element1} = p11{element1} + h; +m11{element1} = m11{element1} - h; + +p01{element2} = p01{element2} + h; +m01{element2} = m01{element2} - h; - p11{i} = p11{i} + h; - m11{i} = m11{i} - h; - end -end +p11{element2} = p11{element2} + h; +m11{element2} = m11{element2} - h; % From Abramowitz and Stegun. Handbook of Mathematical Functions (1965) % formulas 25.3.24 and 25.3.27 p. 884 @@ -75,3 +72,4 @@ else -func(p11{:})... -func(m11{:}))/(-2*h^2); end +end diff --git a/matlab/histvalf.m b/matlab/histvalf.m index f1f9fb0983aa96ef544e00108be77c58beb921a8..f15e72511e4c2bd269c7075929c367f33669de40 100644 --- a/matlab/histvalf.m +++ b/matlab/histvalf.m @@ -1,5 +1,5 @@ -function histvalf(fname) -%function histvalf(fname) +function [endo_histval, exo_histval, exo_det_histval] = histvalf(M, options) +%function [endo_histval, exo_histval, exo_det_histval] = histvalf(M, options) % Sets initial values for simulation using values contained in `fname`, a % file possibly created by a call to `smoother2histval` % @@ -13,7 +13,7 @@ function histvalf(fname) % none -% Copyright (C) 2014-2019 Dynare Team +% Copyright (C) 2014-2020 Dynare Team % % This file is part of Dynare. % @@ -30,72 +30,41 @@ function histvalf(fname) % 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_ oo_ ex0_ - -if ~exist(fname, 'file') - error(['Can''t find datafile: ' fname ]); +if ~isfield(options, 'nobs') || isempty(options.nobs) + options.nobs = M.orig_maximum_lag; end -M_.endo_histval = repmat(oo_.steady_state, 1, M_.maximum_endo_lag); - -% Also fill in oo_.exo_simul: necessary if we are in deterministic context, -% since aux vars for lagged exo are not created in this case -if isempty(oo_.exo_simul) - if isempty(ex0_) - oo_.exo_simul = repmat(oo_.exo_steady_state',M_.maximum_lag,1); +if ~isfield(options, 'first_obs') || isempty(options.first_obs) + if isfield(options, 'first_simulation_period') + options.first_obs = options.first_simulation_period ... + - options.nobs; else - oo_.exo_simul = repmat(ex0_',M_.maximum_lag,1); + options.first_obs = 1; + end +elseif isfield(options, 'first_simulation_period') + nobs = options.first_simulation_period - opions_.first_obs; + if options.nobs ~= nobs + error(sprintf(['HISTVALF: first_obs = %d and', ... + ' first_simulation_period = %d', ... + ' don''t provide for the number of' ... + ' lags in the model.'], ... + options.first_obs, ... + options.first_simulation_period)) end end -S = load(fname); - -outvars = fieldnames(S); +series = histvalf_initvalf('HISTVAL', M, options); +% capture the difference between stochastic and +% perfect foresight setup +k = M.orig_maximum_lag - M.maximum_lag + 1; +endo_histval = series{M.endo_names{:}}.data(k:end, :)'; -for i = 1:length(outvars) - ov_ = outvars{i}; - if ov_(end) == '_' - ov = ov_(1:end-1); - j = strmatch(ov, M_.endo_names, 'exact'); - if isempty(j) - warning(['smoother2histval: output variable ' ov ' does not exist.']) - end - else - % Lagged endogenous or exogenous, search through aux vars - undidx = find(ov_ == '_', 1, 'last'); % Index of last underscore in name - ov = ov_(1:(undidx-1)); - lead_lag = ov_((undidx+1):end); - lead_lag = regexprep(lead_lag,'l','-'); - lead_lag = str2num(lead_lag); - j = []; - for i = 1:length(M_.aux_vars) - if M_.aux_vars(i).type ~= 1 && M_.aux_vars(i).type ~= 3 - continue - end - if M_.aux_vars(i).type == 1 - % Endogenous - orig_var = M_.endo_names{M_.aux_vars(i).orig_index}; - else - % Exogenous - orig_var = M_.exo_names{M_.aux_vars(i).orig_index}; - end - if strcmp(orig_var, ov) && M_.aux_vars(i).orig_lead_lag == lead_lag - j = M_.aux_vars(i).endo_index; - end - end - if isempty(j) - % There is no aux var corresponding to (orig_var, lead_lag). - % If this is an exogenous variable, then it means we should put - % the value in oo_.exo_simul (we are probably in deterministic - % context). - k = strmatch(ov, M_.exo_names); - if isempty(k) - warning(['smoother2histval: output variable ' ov '(' lead_lag ') does not exist.']) - else - oo_.exo_simul((M_.maximum_lag-M_.maximum_endo_lag+1):M_.maximum_lag, k) = S.(ov_); - end - continue - end - end - M_.endo_histval(j, :) = S.(ov_); +exo_histval = []; +if M.exo_nbr + exo_histval = series{M.exo_names{:}}.data(k:end, :)'; end +exo_det_histval = []; +if M.exo_det_nbr + exo_det_histval = series{M.exo_names{:}}.data(k:end, :)'; +end + diff --git a/matlab/histvalf_initvalf.m b/matlab/histvalf_initvalf.m new file mode 100644 index 0000000000000000000000000000000000000000..e65779c493129a3d732c9ecd4e4afca72421f4c0 --- /dev/null +++ b/matlab/histvalf_initvalf.m @@ -0,0 +1,223 @@ +function series = histvalf_initvalf(caller, M, options) +% function initvalf(M) +% +% handles options for histvalf_initvalf() and initvalf() +% +% INPUTS +% caller: string, name of calling function +% M: model structure +% options: options specific to initivalf +% +% OUTPUTS +% series: dseries containing selected data from a file or a dseries +% + +% Copyright (C) 2003-2020 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/>. + + +% dseries +if isfield(options, 'series') + series = options.series; + dseries_ispresent = true; +else + dseries_ispresent = false; +end + +% file +datafile = ''; +if isfield(options, 'filename') + warning([caller, '_FILE: option FILENAME is deprecated, please use', ... + ' option DATAFILE']) + if dseries_ispresent + error([caller, '_FILE: you can''t use option FILENAME and option SERIES', ... + ' at the same time']) + end + if isfield(options, 'datafile') + error([caller, '_FILE: you can''t use option DATAFILE and option FILENAME', ... + ' at the same time']) + end + datafile = options.filename; +end + +if isfield(options, 'datafile') + if dseries_ispresent + error([caller, '_FILE: you can''t use option DATAFILE and option SERIES', ... + ' at the same time']) + end + datafile = options.datafile; +end + +if datafile + [directory,basename,extension] = fileparts(datafile); + % Auto-detect extension if not provided + if isempty(extension) + if exist([basename '.m'],'file') + extension = '.m'; + elseif exist([basename '.mat'],'file') + extension = '.mat'; + elseif exist([basename '.xls'],'file') + extension = '.xls'; + elseif exist([basename '.xlsx'],'file') + extension = '.xlsx'; + else + error([caller, '_FILE: Can''t find datafile: ' basename '.{m,mat,xls,xlsx}']); + end + end + + fullname = [basename extension]; + series = dseries(fullname); +end + +% checking that all variable are present +error_flag = false; +for i = 1:M.orig_endo_nbr + if ~series.exist(M.endo_names{i}) + disp(sprintf('%s_FILE: endogenous variable %s is missing', ... + caller, M.endo_names{i})) + error_flag = true; + end +end + +for i = 1:M.exo_nbr + if ~series.exist(M.exo_names{i}) + disp(sprintf('%s_FILE: exogenous variable %s is missing', ... + caller, M.exo_names{i})) + error_flag = true; + end +end + +for i = 1:M.exo_det_nbr + if ~series.exist(M.exo_det_names{i}) + disp(sprintf('%s_FILE: exo_det variable %s is missing', ... + caller, M.exo_det_names{i})) + error_flag = true; + end +end + +if error_flag + error([caller, '_FILE: some variables are missing']) +end + +if exist(sprintf('+%s/dynamic_set_auxiliary_series', M.fname), 'file') + series = feval(sprintf('%s.dynamic_set_auxiliary_series', M.fname), series, M.params); +end + +% selecting observations +if isfield(options, 'nobs') + nobs = options.nobs; +else + nobs = 0; +end + +periods = series.dates; +nobs0 = series.nobs; + +first_obs_ispresent = false; +last_obs_ispresent = false; +if isfield(options, 'first_obs') + i = options.first_obs; + if i < 1 + error([caller, '_FILE: the first requested period is before available', ... + ' data.']) + elseif i > nobs0 + error([caller, '_FILE: the first requested period is after available', ... + ' data.']) + end + first_obs = periods(i); + if nobs > 0 + last_obs = first_obs + nobs - 1; + last_obs_ispresent = true; + end + first_obs_ispresent = true; +elseif isfield(options, 'firstobs') + first_obs = options.firstobs; + if nobs > 0 + last_obs = first_obs + nobs - 1; + last_obs_ispresent = true; + end + first_obs_ispresent = true; +end + +if last_obs_ispresent + if isfield(options, 'last_obs') + i = options.last_obs; + if i < 1 + error([caller, '_FILE: the last requested period is before available', ... + ' data.']) + elseif i > nobs0 + error([caller, '_FILE: the last requested period is after available', ... + ' data.']) + end + if last_obs ~= periods(i) + error([caller, '_FILE: FIST_OBS, LAST_OBS and NOBS contain', ... + ' inconsistent information. Use only two of these', ... + ' options.']) + end + elseif isfield(options, 'lastobs') + if last_obs ~= options.lastobs + error([caller, '_FILE: FIST_OBS, LAST_OBS and NOBS contain', ... + ' inconsistent information. Use only two of these', ... + ' options.']) + end + end +elseif isfield(options, 'last_obs') + i = options.last_obs; + if i < 1 + error([caller, '_FILE: the last requested period is before available', ... + ' data.']) + elseif i > nobs0 + error([caller, '_FILE: the last requested period is after available', ... + ' data.']) + end + last_obs = periods(i); + if nobs > 0 + first_obs = last_obs - nobs + 1; + first_obs_ispresent = true; + end + last_obs_ispresent = true; +elseif isfield(options, 'lastobs') + last_obs = options.lastobs; + if nobs > 0 + first_obs = last_obs - nobs + 1; + first_obs_ispresent = true; + end + last_obs_ispresent = true; +end + +if ~first_obs_ispresent + first_obs = periods(1); +end + +if ~last_obs_ispresent + if nobs > 0 + last_obs = first_obs + nobs - 1; + else + last_obs = periods(end); + end +end + +if first_obs < series.init + error([caller, '_FILE: the first requested period is before available', ... + ' data.']) +elseif last_obs > series.last + error([caller, '_FILE: the last requested period is after available', ... + ' data.']) +else + series = series(first_obs:last_obs); +end + diff --git a/matlab/identification_analysis.m b/matlab/identification_analysis.m index 75d429182bdb5319a527d0aa8b553aea6a028c24..6262fb347a6bba5fd9d62242eb9b8d5ec92d76b1 100644 --- a/matlab/identification_analysis.m +++ b/matlab/identification_analysis.m @@ -205,10 +205,13 @@ if info(1) == 0 %no errors in solution warning_MOMENTS = [warning_MOMENTS ' The number of moments with non-zero derivative is smaller than the number of parameters up to 10 lags.\n']; warning_MOMENTS = [warning_MOMENTS ' Either reduce the list of parameters, or further increase ar, or increase number of observables.\n']; warning_MOMENTS = [warning_MOMENTS ' Skip identification analysis based on moments.\n']; + warning_MOMENTS = [warning_MOMENTS ' Skip identification strenght analysis.\n']; fprintf(warning_MOMENTS); %reset options to neither display nor plot dMOMENTS anymore no_identification_moments = 1; options_ident.no_identification_moments = 1; + no_identification_strength = 1; + options_ident.no_identification_strength = 1; end end if ~no_identification_minimal @@ -327,7 +330,7 @@ if info(1) == 0 %no errors in solution cmm = simulated_moment_uncertainty(ind_dMOMENTS, periods, replic,options_,M_,oo_); %covariance matrix of moments sd = sqrt(diag(cmm)); cc = cmm./(sd*sd'); - if isoctave || matlab_ver_less_than('8.3') + if isoctave [VV,DD] = eig(cc); %fix for older Matlab versions that do not support computing left eigenvalues, see http://mathworks.com/help/releases/R2012b/matlab/ref/eig.html [WW,~] = eig(cc.'); @@ -536,4 +539,4 @@ if info(1) == 0 %no errors in solution identification_checks(tilda_dSPECTRUM, 3, tol_rank, tol_sv, totparam_nbr); end end -end \ No newline at end of file +end diff --git a/matlab/identification_numerical_objective.m b/matlab/identification_numerical_objective.m index 48ba28bfc71e839e72919ff075c55c6d8ae65570..66112ca6b3960abe936cb355125a92cb9135d806 100644 --- a/matlab/identification_numerical_objective.m +++ b/matlab/identification_numerical_objective.m @@ -82,11 +82,7 @@ pruned = pruned_state_space_system(M, options, oo.dr, indvar, nlags, useautocorr %% out = [vech(cov(Y_t,Y_t)); vec(cov(Y_t,Y_{t-1}); ...; vec(cov(Y_t,Y_{t-nlags})] of indvar variables, in DR order. This is Iskrev (2010)'s J matrix. if outputflag == 1 - if useautocorr - out = dyn_vech(pruned.Corr_y); - else - out = dyn_vech(pruned.Var_y); - end + out = dyn_vech(pruned.Var_y); for i = 1:nlags if useautocorr out = [out;vec(pruned.Corr_yi(:,:,i))]; diff --git a/matlab/imcforecast.m b/matlab/imcforecast.m index 0cf282b7b775c73dbc368f9a865738dfe26c250b..c34ebfb57d56c80a1b9deeaef70c81d155d4072d 100644 --- a/matlab/imcforecast.m +++ b/matlab/imcforecast.m @@ -3,7 +3,7 @@ function imcforecast(constrained_paths, constrained_vars, options_cond_fcst) % Computes conditional forecasts. % % INPUTS -% - consnstrained_paths [double] m*p array, where m is the number of constrained endogenous variables and p is the number of constrained periods. +% - constrained_paths [double] m*p array, where m is the number of constrained endogenous variables and p is the number of constrained periods. % - constrained_vars [integer] m*1 array, indices in M_.endo_names of the constrained variables. % - options_cond_fcst [structure] containing the options. The fields are: % diff --git a/matlab/initvalf.m b/matlab/initvalf.m index 767786936dc2bed8c7fe1a191365b102f213fa86..c6c9449c00e5571905aa125f1a4f69a507045774 100644 --- a/matlab/initvalf.m +++ b/matlab/initvalf.m @@ -1,19 +1,18 @@ -function initvalf(fname_) -% function initvalf(fname_) +function series = initvalf(M, options) +% function initvalf(M) % -% Reads an initial path from the 'fname_' file for exogenous and endogenous variables +% handles options for histvalf() and initvalf() % % INPUTS -% fname_: name of the function or file containing the data +% caller: string, name of calling function +% M: model structure +% options: options specific to initivalf % % OUTPUTS -% none +% series: dseries containing selected data from a file or a dseries % -% SPECIAL REQUIREMENTS -% All variables local to this function have an underscore appended to -% their name, to minimize clashes with model variables loaded by this function. -% Copyright (C) 2003-2018 Dynare Team +% Copyright (C) 2003-2020 Dynare Team % % This file is part of Dynare. % @@ -30,97 +29,4 @@ function initvalf(fname_) % 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_ oo_ options_ - -series_ = 1; - -[directory, basename, extension] = fileparts(fname_); - -% Auto-detect extension if not provided -if isempty(extension) - if exist([basename '.m'],'file') - extension = '.m'; - elseif exist([basename '.mat'],'file') - extension = '.mat'; - elseif exist([basename '.xls'],'file') - extension = '.xls'; - elseif exist([basename '.xlsx'],'file') - extension = '.xlsx'; - else - error(['Can''t find datafile: ' basename '.{m,mat,xls,xlsx}']); - end -end - -if isempty(directory) - directory = '.'; -end - -fullname = [directory '/' basename extension]; - -if ~exist(fullname, 'file') - error(['Can''t find datafile: ' fullname ]); -end - -options_.initval_file = true; -oo_.endo_simul = []; -oo_.exo_simul = []; - -if ismember(extension, {'.mat', '.m'}) && isdseries(fullname) - data = dseries(fullname); - % Add auxiliary variables if any. - data = feval(sprintf('%s.dynamic_set_auxiliary_series', M_.fname), data, M_.params); - % Remove NaNs in the first periods. - data = data(data.firstobservedperiod:data.lastobservedperiod); - % Fill endo_simul. - oo_.endo_simul = transpose(data{M_.endo_names{:}}.data); - % Fill exo_simul. - oo_.exo_simul = data{M_.exo_names{:}}.data; - % Update number of periods for the deterministic simulation. - options_.periods = data.nobs-M_.maximum_lag-M_.maximum_lead; -else - switch (extension) - case '.m' - eval(basename); - case '.mat' - load(basename); - case { '.xls', '.xlsx' } - [data_,names_v_]=xlsread(fullname); % Octave needs the extension explicitly - series_=0; - otherwise - error(['Unsupported extension for datafile: ' extension]) - end - for i_=1:length(M_.endo_names) - if series_ == 1 - x_ = eval(M_.endo_names{i_}); - if size(x_,2)>size(x_,1) %oo_.endo_simul must be collection of row vectors - oo_.endo_simul = [oo_.endo_simul; x_]; - else %transpose if column vector - oo_.endo_simul = [oo_.endo_simul; x_']; - end - else - k_ = strmatch(M_.endo_names{i_}, names_v_, 'exact'); - if isempty(k_) - error(['INITVAL_FILE: ' M_.endo_names{i_} ' not found']) - end - x_ = data_(:,k_); - oo_.endo_simul = [oo_.endo_simul; x_']; - end - end - for i_=1:length(M_.exo_names) - if series_ == 1 - x_ = eval(M_.exo_names{i_}); - if size(x_,2)>size(x_,1) %oo_.endo_simul must be collection of row vectors - oo_.exo_simul = [oo_.exo_simul x_']; - else %if column vector - oo_.exo_simul = [oo_.exo_simul x_]; - end - else - k_ = strmatch(M_.exo_names{i_}, names_v_, 'exact'); - if isempty(k_) - error(['INITVAL_FILE: ' M_.exo_names{i_} ' not found']) - end - x_ = data_(:,k_); - oo_.exo_simul = [oo_.exo_simul x_]; - end - end -end \ No newline at end of file +series = histvalf_initvalf('INITVALF', M, options); \ No newline at end of file diff --git a/matlab/jacob_element.m b/matlab/jacob_element.m index c86e6e7591c8c8c9e650bc4e10d81e38815a66ba..4f49cdbab917e1b291092770612e36f2e7368ed9 100644 --- a/matlab/jacob_element.m +++ b/matlab/jacob_element.m @@ -13,7 +13,7 @@ function d=jacob_element(func,element,args) % SPECIAL REQUIREMENTS % none -% Copyright (C) 2010-2011 Dynare Team +% Copyright (C) 2010-2020 Dynare Team % % This file is part of Dynare. % @@ -30,17 +30,15 @@ function d=jacob_element(func,element,args) % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <http://www.gnu.org/licenses/>. +assert(element <= length(args)); + func = str2func(func); -h=10e-6; -pargs=args; +h=1e-6; margs=args; -% length(args) is used instead of size(args, 2) to avoid to transpose column vectors -for i=1:length(args) - if i==element - pargs{i} = pargs{i} + h; - margs{i} = margs{i} - h; - end + +args{element} = args{element} + h; +margs{element} = margs{element} - h; + +d=(func(args{:})-func(margs{:}))/(2*h); end -d=(func(pargs{:})... - -func(margs{:}))/(2*h); diff --git a/matlab/list_of_functions_to_be_cleared.m b/matlab/list_of_functions_to_be_cleared.m index 0e161fdcfe3119ce08ef191d99588f66403768d6..82ee94b5a81d035866ab83a943d29b5c34cd9885 100644 --- a/matlab/list_of_functions_to_be_cleared.m +++ b/matlab/list_of_functions_to_be_cleared.m @@ -1 +1 @@ -list_of_functions = {'discretionary_policy_1', 'dsge_var_likelihood', 'dyn_first_order_solver', 'dyn_waitbar', 'ep_residuals', 'evaluate_likelihood', 'prior_draw_gsa', 'identification_analysis', 'computeDLIK', 'univariate_computeDLIK', 'metropolis_draw', 'flag_implicit_skip_nan', 'moment_function', 'mr_hessian', 'masterParallel', 'auxiliary_initialization', 'auxiliary_particle_filter', 'conditional_filter_proposal', 'conditional_particle_filter', 'gaussian_filter', 'gaussian_filter_bank', 'gaussian_mixture_filter', 'gaussian_mixture_filter_bank', 'Kalman_filter', 'online_auxiliary_filter', 'sequential_importance_particle_filter', 'solve_model_for_online_filter', 'perfect_foresight_simulation', 'prior_draw', 'priordens', 'smm_objective'}; \ No newline at end of file +list_of_functions = {'discretionary_policy_1', 'dsge_var_likelihood', 'dyn_first_order_solver', 'dyn_waitbar', 'ep_residuals', 'evaluate_likelihood', 'prior_draw_gsa', 'identification_analysis', 'computeDLIK', 'univariate_computeDLIK', 'metropolis_draw', 'flag_implicit_skip_nan', 'moment_function', 'mr_hessian', 'masterParallel', 'auxiliary_initialization', 'auxiliary_particle_filter', 'conditional_filter_proposal', 'conditional_particle_filter', 'gaussian_filter', 'gaussian_filter_bank', 'gaussian_mixture_filter', 'gaussian_mixture_filter_bank', 'Kalman_filter', 'online_auxiliary_filter', 'pruned_state_space_system', 'sequential_importance_particle_filter', 'solve_model_for_online_filter', 'perfect_foresight_simulation', 'prior_draw', 'priordens'}; diff --git a/matlab/lnsrch1_wrapper_one_boundary.m b/matlab/lnsrch1_wrapper_one_boundary.m index 8ac9c14d0cb8c0d16cf2234d3fd908cfbc273dd3..700bade4b13a5af8b4a6c09ef3fcdca3cd8af913 100644 --- a/matlab/lnsrch1_wrapper_one_boundary.m +++ b/matlab/lnsrch1_wrapper_one_boundary.m @@ -1,16 +1,17 @@ -function r = lnsrch1_wrapper_one_boundary(ya, y_index, fname, y, x, params, steady_state, it_) +function r = lnsrch1_wrapper_one_boundary(ya, ll_index, fname, blk, y, x, params, steady_state, T, it_) % wrapper for solve_one_boundary m-file when it is used with a dynamic % model % % INPUTS % ya [vector] The endogenous of the current block -% y_index [vector of int] The index of the endogenous variables of -% the block -% fname [string] name of the file containing the block -% to simulate -% y [matrix] All the endogenous variables of the model +% ll_index [vector] M_.lead_lag_incidence(M_.maximum_endo_lag+1, :) +% fname [string] name of the static/dynamic file +% blk [int] block number +% y [vector] Dynamic endogenous variables of the model % x [matrix] All the exogenous variables of the model % params [vector] All the parameters of the model +% steady_state [vector] steady state of the model +% T [vector] Temporary terms % OUTPUTS % r [vector] The residuals of the current block % @@ -21,7 +22,7 @@ function r = lnsrch1_wrapper_one_boundary(ya, y_index, fname, y, x, params, stea % none. % -% Copyright (C) 2009-2017 Dynare Team +% Copyright (C) 2009-2020 Dynare Team % % This file is part of Dynare. % @@ -38,6 +39,5 @@ function r = lnsrch1_wrapper_one_boundary(ya, y_index, fname, y, x, params, stea % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <http://www.gnu.org/licen -%reshape the input arguments of the dynamic function -y(it_, :) = ya; -[r, y, g1, g2, g3]=feval(fname, y, x, params, steady_state, it_, 0); +y2(nonzeros(ll_index)) = ya(find(ll_index)); +[r, ~, ~, g1]=feval(fname, blk, y, x, params, steady_state, T, it_, false); diff --git a/matlab/lnsrch1_wrapper_two_boundaries.m b/matlab/lnsrch1_wrapper_two_boundaries.m index 05c396c77fcd32f93d4437cd4fe3c8f8509fe9bd..989cd107e8f5c21635c0d02bfa72532692bedf5b 100644 --- a/matlab/lnsrch1_wrapper_two_boundaries.m +++ b/matlab/lnsrch1_wrapper_two_boundaries.m @@ -1,5 +1,6 @@ -function ra = lnsrch1_wrapper_two_boundaries(ya, fname, y, y_index, x, ... - params, steady_state, periods, y_kmin, y_size,Periods) +function ra = lnsrch1_wrapper_two_boundaries(ya, fname, blk, y, y_index, x, ... + params, steady_state, T, periods, ... + y_size, M_) % wrapper for solve_one_boundary m-file when it is used with a dynamic % model % @@ -7,15 +8,18 @@ function ra = lnsrch1_wrapper_two_boundaries(ya, fname, y, y_index, x, ... % ya [vector] The endogenous of the current block % y_index [vector of int] The index of the endogenous variables of % the block -% fname [string] name of the file containing the block -% to simulate +% fname [string] name of the dynamic file +% blk [int] block number % y [matrix] All the endogenous variables of the model % x [matrix] All the exogenous variables of the model % params [vector] All the parameters of the model +% steady_state [vector] steady state of the model +% T [matrix] Temporary terms % periods [int] The number of periods -% y_kmin [int] The maximum number of lag on en endogenous variables % y_size [int] The number of endogenous variables % in the current block +% M_ Model description structure +% % OUTPUTS % ra [vector] The residuals of the current block % @@ -26,7 +30,7 @@ function ra = lnsrch1_wrapper_two_boundaries(ya, fname, y, y_index, x, ... % none. % -% Copyright (C) 2009-2017 Dynare Team +% Copyright (C) 2009-2020 Dynare Team % % This file is part of Dynare. % @@ -44,6 +48,8 @@ function ra = lnsrch1_wrapper_two_boundaries(ya, fname, y, y_index, x, ... % along with Dynare. If not, see <http://www.gnu.org/licen %reshape the input arguments of the dynamic function -y(y_kmin+1:y_kmin+periods, y_index) = reshape(ya',length(y_index),periods)'; -[r, y, g1, g2, g3, b]=feval(fname, y, x, params, steady_state, periods, 0, y_kmin, y_size, Periods); -ra = reshape(r(:, y_kmin+1:periods+y_kmin),periods*y_size, 1); +y(y_index, M_.maximum_lag+(1:periods)) = reshape(ya',length(y_index),periods); +ra = NaN(periods*y_size, 1); +for it_ = M_.maximum_lag+(1:periods) + [ra((it_-M_.maximum_lag-1)*y_size+(1:y_size)), ~, ~, g1]=feval(fname, blk, dynvars_from_endo_simul(y, it_, M_), x, params, steady_state, T(:, it_), it_, false); +end diff --git a/matlab/method_of_moments/method_of_moments.m b/matlab/method_of_moments/method_of_moments.m new file mode 100644 index 0000000000000000000000000000000000000000..28ec839c943e553dca316fa54788c46ef9b77f00 --- /dev/null +++ b/matlab/method_of_moments/method_of_moments.m @@ -0,0 +1,918 @@ +function [oo_, options_mom_, M_] = method_of_moments(bayestopt_, options_, oo_, estim_params_, M_, options_mom_) +%function [oo_, options_mom_, M_] = method_of_moments(bayestopt_, options_, oo_, estim_params_, M_, options_mom_) +% ------------------------------------------------------------------------- +% This function performs a method of moments estimation with the following steps: +% Step 0: Check if required structures and options exist +% Step 1: - Prepare options_mom_ structure +% - Carry over options from the preprocessor +% - Initialize other options +% - Get variable orderings and state space representation +% Step 2: Checks and transformations for matched moments structure +% Step 3: Checks and transformations for estimated parameters, priors, and bounds +% Step 4: Checks and transformations for data +% Step 5: Checks for steady state at initial parameters +% Step 6: Checks for objective function at initial parameters +% Step 7: Iterated method of moments estimation +% Step 8: J-Test +% Step 9: Clean up +% ------------------------------------------------------------------------- +% This function is inspired by replication codes accompanied to the following papers: +% o Andreasen, Fernández-Villaverde, Rubio-RamÃrez (2018): "The Pruned State-Space System for Non-Linear DSGE Models: Theory and Empirical Applications", Review of Economic Studies, 85(1):1-49. +% o Born, Pfeifer (2014): "Risk Matters: Comment", American Economic Review, 104(12):4231-4239. +% o Mutschler (2018): "Higher-order statistics for DSGE models", Econometrics and Statistics, 6:44-56. +% ========================================================================= +% INPUTS +% o bayestopt_: [structure] information about priors +% o options_: [structure] information about global options +% o oo_: [structure] storage for results +% o estim_params_: [structure] information about estimated parameters +% o M_: [structure] information about model with +% o matched_moments: [cell] information about selected moments to match in estimation +% vars: matched_moments{:,1}); +% lead/lags: matched_moments{:,2}; +% powers: matched_moments{:,3}; +% o options_mom_: [structure] information about settings specified by the user +% ------------------------------------------------------------------------- +% OUTPUTS +% o oo_: [structure] storage for results (oo_) +% o options_mom_: [structure] information about all (user-specified and updated) settings used in estimation (options_mom_) +% ------------------------------------------------------------------------- +% This function is called by +% o driver.m +% ------------------------------------------------------------------------- +% This function calls +% o check_for_calibrated_covariances.m +% o check_prior_bounds.m +% o do_parameter_initialization.m +% o dynare_minimize_objective.m +% o evaluate_steady_state +% o get_all_parameters.m +% o get_matrix_entries_for_psd_check.m +% o makedataset.m +% o method_of_moments_data_moments.m +% o method_of_moments_mode_check.m +% o method_of_moments_objective_function.m +% o method_of_moments_optimal_weighting_matrix +% o method_of_moments_standard_errors +% o plot_priors.m +% o print_info.m +% o prior_bounds.m +% o set_default_option.m +% o set_prior.m +% o set_state_space.m +% o set_all_parameters.m +% o test_for_deep_parameters_calibration.m +% ========================================================================= +% Copyright (C) 2020 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/>. +% ------------------------------------------------------------------------- +% Author(s): +% o Willi Mutschler (willi@mutschler.eu) +% o Johannes Pfeifer (jpfeifer@uni-koeln.de) +% ========================================================================= + +%% TO DO LIST +% - [ ] why does lsqnonlin take less time in Andreasen toolbox? +% - [ ] test user-specified weightning matrix +% - [ ] which qz_criterium value? +% - [ ] document that in method_of_moments_data_moments.m NaN are replaced by mean of moment +% - [ ] add IRF matching +% - [ ] test estimated_params_bounds block +% - [ ] test what happens if all parameters will be estimated but some/all are not calibrated +% - [ ] speed up lyapunov equation by using doubling with old initial values +% - [ ] check smm at order > 3 without pruning +% - [ ] provide option to use analytical derivatives to compute std errors (similar to what we already do in identification) +% - [ ] add Bayesian GMM/SMM estimation +% - [ ] useautocorr +% - [ ] do we need dirname? +% - [ ] decide on default weighting matrix scheme, I would propose 2 stage with Diagonal of optimal matrix +% - [ ] check smm with product moments greater than 2 +% ------------------------------------------------------------------------- +% Step 0: Check if required structures and options exist +% ------------------------------------------------------------------------- +if isempty(estim_params_) % structure storing the info about estimated parameters in the estimated_params block + if ~(isfield(estim_params_,'nvx') && (size(estim_params_.var_exo,1)+size(estim_params_.var_endo,1)+size(estim_params_.corrx,1)+size(estim_params_.corrn,1)+size(estim_params_.param_vals,1))==0) + error('method_of_moments: You need to provide an ''estimated_params'' block') + else + error('method_of_moments: The ''estimated_params'' block must not be empty') + end +end +if isempty(M_.matched_moments) % structure storing the moments used for the method of moments estimation + error('method_of_moments: You need to provide a ''matched_moments'' block') +end +if ~isempty(bayestopt_) && any(bayestopt_.pshape==0) && any(bayestopt_.pshape~=0) + error('method_of_moments: Estimation must be either fully classical or fully Bayesian. Maybe you forgot to specify a prior distribution.') +end + +if options_.logged_steady_state || options_.loglinear + error('method_of_moments: The loglinear option is not supported. Please append the required logged variables as auxiliary equations.\n') +else + options_mom_.logged_steady_state = 0; + options_mom_.loglinear = false; +end + +fprintf('\n==== Method of Moments (%s) Estimation ====\n\n',options_mom_.mom.mom_method) + +% ------------------------------------------------------------------------- +% Step 1a: Prepare options_mom_ structure +% ------------------------------------------------------------------------- +% options_mom_ is local and contains default and user-specified values for +% all settings needed for the method of moments estimation. Some options, +% though, are set by the preprocessor into options_ and we copy these over. +% The idea is to be independent of options_ and have full control of the +% estimation instead of possibly having to deal with options chosen somewhere +% else in the mod file. + +% Method of Moments estimation options that can be set by the user in the mod file, otherwise default values are provided +if strcmp(options_mom_.mom.mom_method,'GMM') || strcmp(options_mom_.mom.mom_method,'SMM') + options_mom_.mom = set_default_option(options_mom_.mom,'bartlett_kernel_lag',20); % bandwith in optimal weighting matrix + options_mom_.mom = set_default_option(options_mom_.mom,'penalized_estimator',false); % include deviation from prior mean as additional moment restriction and use prior precision as weight + options_mom_.mom = set_default_option(options_mom_.mom,'verbose',false); % display and store intermediate estimation results + options_mom_.mom = set_default_option(options_mom_.mom,'weighting_matrix',{'DIAGONAL'; 'DIAGONAL'}); % weighting matrix in moments distance objective function at each iteration of estimation; cell of strings with + % possible values are 'OPTIMAL', 'IDENTITY_MATRIX' ,'DIAGONAL' or a filename. Size of cell determines stages in iterated estimation. + options_mom_.mom = set_default_option(options_mom_.mom,'weighting_matrix_scaling_factor',1); % scaling of weighting matrix + options_mom_.mom = set_default_option(options_mom_.mom,'se_tolx',1e-5); % step size for numerical computation of standard errors + options_mom_ = set_default_option(options_mom_,'order',1); % order of Taylor approximation in perturbation + options_mom_ = set_default_option(options_mom_,'pruning',true); % use pruned state space system at higher-order + % Checks for perturbation order + if options_mom_.order < 1 + error('method_of_moments:: The order of the Taylor approximation cannot be 0!') + end +end +if strcmp(options_mom_.mom.mom_method,'SMM') + options_mom_.mom = set_default_option(options_mom_.mom,'burnin',500); % number of periods dropped at beginning of simulation + options_mom_.mom = set_default_option(options_mom_.mom,'bounded_shock_support',false); % trim shocks in simulation to +- 2 stdev + options_mom_.mom = set_default_option(options_mom_.mom,'seed',24051986); % seed used in simulations + options_mom_.mom = set_default_option(options_mom_.mom,'simulation_multiple',5); % multiple of the data length used for simulation + if options_mom_.mom.simulation_multiple < 1 + fprintf('The simulation horizon is shorter than the data. Dynare resets the simulation_multiple to 5.\n') + options_mom_.mom.simulation_multiple = 5; + end +end +if strcmp(options_mom_.mom.mom_method,'GMM') + % Check for pruning with GMM at higher order + if options_mom_.order > 1 && ~options_mom_.pruning + fprintf('GMM at higher order only works with pruning, so we set pruning option to 1.\n'); + options_mom_.pruning = true; + end +end + + +% General options that can be set by the user in the mod file, otherwise default values are provided +options_mom_ = set_default_option(options_mom_,'dirname',M_.fname); % directory in which to store estimation output +options_mom_ = set_default_option(options_mom_,'graph_format','eps'); % specify the file format(s) for graphs saved to disk +options_mom_ = set_default_option(options_mom_,'nodisplay',false); % do not display the graphs, but still save them to disk +options_mom_ = set_default_option(options_mom_,'nograph',false); % do not create graphs (which implies that they are not saved to the disk nor displayed) +options_mom_ = set_default_option(options_mom_,'noprint',false); % do not print output to console +options_mom_ = set_default_option(options_mom_,'plot_priors',true); % control plotting of priors +options_mom_ = set_default_option(options_mom_,'prior_trunc',1e-10); % probability of extreme values of the prior density that is ignored when computing bounds for the parameters +options_mom_ = set_default_option(options_mom_,'TeX',false); % print TeX tables and graphics + +% Data and model options that can be set by the user in the mod file, otherwise default values are provided +options_mom_ = set_default_option(options_mom_,'first_obs',1); % number of first observation +options_mom_ = set_default_option(options_mom_,'logdata',false); % if data is already in logs +options_mom_ = set_default_option(options_mom_,'nobs',NaN); % number of observations +options_mom_ = set_default_option(options_mom_,'prefilter',false); % demean each data series by its empirical mean and use centered moments +options_mom_ = set_default_option(options_mom_,'xls_sheet',1); % name of sheet with data in Excel +options_mom_ = set_default_option(options_mom_,'xls_range',''); % range of data in Excel sheet +% Recursive estimation and forecast are not supported +if numel(options_mom_.nobs)>1 + error('method_of_moments: Recursive estimation and forecast for samples is not supported. Please set an integer as ''nobs''.'); +end +if numel(options_mom_.first_obs)>1 + error('method_of_moments: Recursive estimation and forecast for samples is not supported. Please set an integer as ''first_obs''.'); +end + +% Optimization options that can be set by the user in the mod file, otherwise default values are provided +options_mom_ = set_default_option(options_mom_,'huge_number',1e7); % value for replacing the infinite bounds on parameters by finite numbers. Used by some optimizers for numerical reasons +options_mom_ = set_default_option(options_mom_,'mode_compute',13); % specifies the optimizer for minimization of moments distance +options_mom_ = set_default_option(options_mom_,'additional_optimizer_steps',[]); % vector of additional mode-finders run after mode_compute +options_mom_ = set_default_option(options_mom_,'optim_opt',[]); % a list of NAME and VALUE pairs to set options for the optimization routines. Available options depend on mode_compute +options_mom_ = set_default_option(options_mom_,'silent_optimizer',false); % run minimization of moments distance silently without displaying results or saving files in between +% Mode_check plot options that can be set by the user in the mod file, otherwise default values are provided +options_mom_.mode_check.nolik = false; % we don't do likelihood (also this initializes mode_check substructure) +options_mom_.mode_check = set_default_option(options_mom_.mode_check,'status',false); % plot the target function for values around the computed mode for each estimated parameter in turn. This is helpful to diagnose problems with the optimizer. +options_mom_.mode_check = set_default_option(options_mom_.mode_check,'neighbourhood_size',.5); % width of the window around the mode to be displayed on the diagnostic plots. This width is expressed in percentage deviation. The Inf value is allowed, and will trigger a plot over the entire domain +options_mom_.mode_check = set_default_option(options_mom_.mode_check,'symmetric_plots',true); % ensure that the check plots are symmetric around the mode. A value of 0 allows to have asymmetric plots, which can be useful if the posterior mode is close to a domain boundary, or in conjunction with mode_check_neighbourhood_size = Inf when the domain is not the entire real line +options_mom_.mode_check = set_default_option(options_mom_.mode_check,'number_of_points',20); % number of points around the mode where the target function is evaluated (for each parameter) + +% Numerical algorithms options that can be set by the user in the mod file, otherwise default values are provided +options_mom_ = set_default_option(options_mom_,'aim_solver',false); % use AIM algorithm to compute perturbation approximation instead of mjdgges +options_mom_ = set_default_option(options_mom_,'k_order_solver',false); % use k_order_perturbation instead of mjdgges +options_mom_ = set_default_option(options_mom_,'dr_cycle_reduction',false); % use cycle reduction algorithm to solve the polynomial equation for retrieving the coefficients associated to the endogenous variables in the decision rule +options_mom_ = set_default_option(options_mom_,'dr_cycle_reduction_tol',1e-7); % convergence criterion used in the cycle reduction algorithm +options_mom_ = set_default_option(options_mom_,'dr_logarithmic_reduction',false); % use logarithmic reduction algorithm to solve the polynomial equation for retrieving the coefficients associated to the endogenous variables in the decision rule +options_mom_ = set_default_option(options_mom_,'dr_logarithmic_reduction_maxiter',100); % maximum number of iterations used in the logarithmic reduction algorithm +options_mom_ = set_default_option(options_mom_,'dr_logarithmic_reduction_tol',1e-12); % convergence criterion used in the cycle reduction algorithm +options_mom_ = set_default_option(options_mom_,'lyapunov_db',false); % doubling algorithm (disclyap_fast) to solve Lyapunov equation to compute variance-covariance matrix of state variables +options_mom_ = set_default_option(options_mom_,'lyapunov_fp',false); % fixed-point algorithm to solve Lyapunov equation to compute variance-covariance matrix of state variables +options_mom_ = set_default_option(options_mom_,'lyapunov_srs',false); % square-root-solver (dlyapchol) algorithm to solve Lyapunov equation to compute variance-covariance matrix of state variables +options_mom_ = set_default_option(options_mom_,'lyapunov_complex_threshold',1e-15); % complex block threshold for the upper triangular matrix in symmetric Lyapunov equation solver +options_mom_ = set_default_option(options_mom_,'lyapunov_fixed_point_tol',1e-10); % convergence criterion used in the fixed point Lyapunov solver +options_mom_ = set_default_option(options_mom_,'lyapunov_doubling_tol',1e-16); % convergence criterion used in the doubling algorithm +options_mom_ = set_default_option(options_mom_,'sylvester_fp',false); % determines whether to use fixed point algorihtm to solve Sylvester equation (gensylv_fp), faster for large scale models +options_mom_ = set_default_option(options_mom_,'sylvester_fixed_point_tol',1e-12); % convergence criterion used in the fixed point Sylvester solver +options_mom_ = set_default_option(options_mom_,'qz_criterium',1-1e-6); % value used to split stable from unstable eigenvalues in reordering the Generalized Schur decomposition used for solving first order problems [IS THIS CORRET @wmutschl] +options_mom_ = set_default_option(options_mom_,'qz_zero_threshold',1e-6); % value used to test if a generalized eigenvalue is 0/0 in the generalized Schur decomposition +if options_mom_.order > 2 + fprintf('Dynare will use ''k_order_solver'' as the order>2\n'); + options_mom_.k_order_solver = true; +end + +% ------------------------------------------------------------------------- +% Step 1b: Options that are set by the preprocessor and need to be carried over +% ------------------------------------------------------------------------- + +% options related to VAROBS +if ~isfield(options_,'varobs') + error('method_of_moments: VAROBS statement is missing!') +else + options_mom_.varobs = options_.varobs; % observable variables in declaration order + options_mom_.obs_nbr = length(options_mom_.varobs); % number of observed variables + % Check that each declared observed variable is also an endogenous variable + for i = 1:options_mom_.obs_nbr + if ~any(strcmp(options_mom_.varobs{i},M_.endo_names)) + error(['method_of_moments: Unknown variable (' options_mom_.varobs{i} ')!']) + end + end + + % Check that a variable is not declared as observed more than once + if length(unique(options_mom_.varobs))<length(options_mom_.varobs) + for i = 1:options_mom_.obs_nbr + if sum(strcmp(options_mom_.varobs{i},options_mom_.varobs))>1 + error(['method_of_moments: A variable cannot be declared as observed more than once (' options_mom_.varobs{i} ')!']) + end + end + end +end + +% options related to variable declarations +if isfield(options_,'trend_coeffs') + error('method_of_moments: %s does not allow for trend in data',options_mom_.mom.mom_method) +end + +% options related to estimated_params and estimated_params_init +options_mom_.use_calibration_initialization = options_.use_calibration_initialization; + +% options related to model block +options_mom_.linear = options_.linear; +options_mom_.use_dll = options_.use_dll; +options_mom_.block = options_.block; +options_mom_.bytecode = options_.bytecode; + +% options related to steady command +options_mom_.homotopy_force_continue = options_.homotopy_force_continue; +options_mom_.homotopy_mode = options_.homotopy_mode; +options_mom_.homotopy_steps = options_.homotopy_steps; +options_mom_.markowitz = options_.markowitz; +options_mom_.solve_algo = options_.solve_algo; +options_mom_.solve_tolf = options_.solve_tolf; +options_mom_.solve_tolx = options_.solve_tolx; +options_mom_.steady = options_.steady; +options_mom_.steadystate = options_.steadystate; +options_mom_.steadystate_flag = options_.steadystate_flag; + +% options related to dataset +options_mom_.dataset = options_.dataset; +options_mom_.initial_period = options_.initial_period; + +% options related to endogenous prior restrictions are not supported +options_mom_.endogenous_prior_restrictions.irf = {}; +options_mom_.endogenous_prior_restrictions.moment = {}; +if ~isempty(options_.endogenous_prior_restrictions.irf) && ~isempty(options_.endogenous_prior_restrictions.moment) + fprintf('Endogenous prior restrictions are not supported yet and will be skipped.\n') +end + +% ------------------------------------------------------------------------- +% Step 1c: Options related to optimizers +% ------------------------------------------------------------------------- +% mode_compute = 1, 3, 7, 11, 102, 11, 13 +% nothing to be done +% mode_compute = 2 +options_mom_.saopt = options_.saopt; +% mode_compute = 4 +options_mom_.csminwel = options_.csminwel; +% mode_compute = 5 +options_mom_.newrat = options_.newrat; +options_mom_.gstep = options_.gstep; +% mode_compute = 6 +options_mom_.gmhmaxlik = options_.gmhmaxlik; +options_mom_.mh_jscale = options_.mh_jscale; +% mode_compute = 8 +options_mom_.simplex = options_.simplex; +% mode_compute = 9 +options_mom_.cmaes = options_.cmaes; +% mode_compute = 10 +options_mom_.simpsa = options_.simpsa; +% mode_compute = 12 +options_mom_.particleswarm = options_.particleswarm; +% mode_compute = 101 +options_mom_.solveopt = options_.solveopt; + +options_mom_.gradient_method = options_.gradient_method; +options_mom_.gradient_epsilon = options_.gradient_epsilon; +options_mom_.analytic_derivation = 0; + +options_mom_.vector_output= false; % specifies whether the objective function returns a vector + +% ------------------------------------------------------------------------- +% Step 1d: Other options that need to be initialized +% ------------------------------------------------------------------------- +options_mom_.initialize_estimated_parameters_with_the_prior_mode = 0; % needed by set_prior.m +options_mom_.figures.textwidth = 0.8; %needed by plot_priors.m +options_mom_.ramsey_policy = 0; % needed by evaluate_steady_state +options_mom_.debug = false; %needed by resol.m +options_mom_.risky_steadystate = false; %needed by resol +options_mom_.threads = options_.threads; %needed by resol +options_mom_.jacobian_flag = true; +options_mom_.gstep = options_.gstep; + +% options_mom.dsge_var = false; %needed by check_list_of_variables +% options_mom.bayesian_irf = false; %needed by check_list_of_variables +% options_mom.moments_varendo = false; %needed by check_list_of_variables +% options_mom.smoother = false; %needed by check_list_of_variables +% options_mom.filter_step_ahead = []; %needed by check_list_of_variables +% options_mom.forecast = 0; +%options_mom_ = set_default_option(options_mom_,'endo_vars_for_moment_computations_in_estimation',[]); + +% ------------------------------------------------------------------------- +% Step 1e: Get variable orderings and state space representation +% ------------------------------------------------------------------------- +oo_.dr = set_state_space(oo_.dr,M_,options_mom_); +% Get index of observed variables in DR order +oo_.dr.obs_var = []; +for i=1:options_mom_.obs_nbr + oo_.dr.obs_var = [oo_.dr.obs_var; find(strcmp(options_mom_.varobs{i}, M_.endo_names(oo_.dr.order_var)))]; +end + +% ------------------------------------------------------------------------- +% Step 2: Checks and transformations for matched moments structure (preliminary) +% ------------------------------------------------------------------------- +% Note that we do not have a preprocessor interface yet for this, so this +% will need much improvement later on. @wmutschl + +% Initialize indices +options_mom_.mom.index.E_y = false(options_mom_.obs_nbr,1); %unconditional first order product moments +options_mom_.mom.index.E_yy = false(options_mom_.obs_nbr,options_mom_.obs_nbr); %unconditional second order product moments +options_mom_.mom.index.E_yyt = false(options_mom_.obs_nbr,options_mom_.obs_nbr,0); %unconditional temporal second order product moments +options_mom_.mom.index.E_y_pos = zeros(options_mom_.obs_nbr,1); %position in matched moments block +options_mom_.mom.index.E_yy_pos = zeros(options_mom_.obs_nbr,options_mom_.obs_nbr); %position in matched moments block +options_mom_.mom.index.E_yyt_pos = zeros(options_mom_.obs_nbr,options_mom_.obs_nbr,0); %position in matched moments block + +for jm=1:size(M_.matched_moments,1) + % higher-order product moments not supported yet for GMM + if strcmp(options_mom_.mom.mom_method, 'GMM') && sum(M_.matched_moments{jm,3}) > 2 + error('method_of_moments: GMM does not yet support product moments higher than 2. Change row %d in ''matched_moments'' block.',jm); + end + % Check if declared variables are also observed (needed as otherwise the dataset variables won't coincide) + if any(~ismember(oo_.dr.inv_order_var(M_.matched_moments{jm,1})', oo_.dr.obs_var)) + error('method_of_moments: Variables in row %d in ''matched_moments'' block need to be declared as VAROBS.', jm) + end + + if strcmp(options_mom_.mom.mom_method, 'GMM') + % Check (for now) that only lags are declared + if any(M_.matched_moments{jm,2}>0) + error('method_of_moments: Leads in row %d in the ''matched_moments'' block are not supported for GMM, shift the moments and declare only lags.', jm) + end + % Check (for now) that first declared variable has zero lag + if M_.matched_moments{jm,2}(1)~=0 + error('method_of_moments: The first variable declared in row %d in the ''matched_moments'' block is not allowed to have a lead or lag for GMM;\n reorder the variables in the row such that the first variable has zero lag!',jm) + end + end + vars = oo_.dr.inv_order_var(M_.matched_moments{jm,1})'; + if sum(M_.matched_moments{jm,3}) == 1 + % First-order product moment + vpos = (oo_.dr.obs_var == vars); + options_mom_.mom.index.E_y(vpos,1) = true; + options_mom_.mom.index.E_y_pos(vpos,1) = jm; + M_.matched_moments{jm,4}=['E(',M_.endo_names{M_.matched_moments{jm,1}},')']; + M_.matched_moments{jm,5}=['$E(',M_.endo_names_tex{M_.matched_moments{jm,1}},')$']; + elseif sum(M_.matched_moments{jm,3}) == 2 + % Second-order product moment + idx1 = (oo_.dr.obs_var == vars(1)); + idx2 = (oo_.dr.obs_var == vars(2)); + lag1 = M_.matched_moments{jm,2}(1); + lag2 = M_.matched_moments{jm,2}(2); + if lag1==0 && lag2==0 % contemporaneous covariance matrix + options_mom_.mom.index.E_yy(idx1,idx2) = true; + options_mom_.mom.index.E_yy(idx2,idx1) = true; + options_mom_.mom.index.E_yy_pos(idx1,idx2) = jm; + options_mom_.mom.index.E_yy_pos(idx2,idx1) = jm; + M_.matched_moments{jm,4}=['E(',M_.endo_names{M_.matched_moments{jm,1}(1)},',',M_.endo_names{M_.matched_moments{jm,1}(2)},')']; + M_.matched_moments{jm,5}=['$E({',M_.endo_names_tex{M_.matched_moments{jm,1}(1)},'}_t,{',M_.endo_names_tex{M_.matched_moments{jm,1}(1)},'}_t)$']; + elseif lag1==0 && lag2 < 0 + options_mom_.mom.index.E_yyt(idx1,idx2,-lag2) = true; + options_mom_.mom.index.E_yyt_pos(idx1,idx2,-lag2) = jm; + M_.matched_moments{jm,4}=['E(',M_.endo_names{M_.matched_moments{jm,1}(1)},',',M_.endo_names{M_.matched_moments{jm,1}(2)},'(',num2str(lag2),'))']; + M_.matched_moments{jm,5}=['$E({',M_.endo_names_tex{M_.matched_moments{jm,1}(1)},'}_t\times{',M_.endo_names_tex{M_.matched_moments{jm,1}(1)},'_{t',num2str(lag2) ,'})$']; + end + end +end + + +% @wmutschl: add check for duplicate moments by using the cellfun and unique functions +%Remove duplicate elements +UniqueMomIdx = [nonzeros(options_mom_.mom.index.E_y_pos); nonzeros(tril(options_mom_.mom.index.E_yy_pos)); nonzeros(options_mom_.mom.index.E_yyt_pos)]; +DuplicateMoms = setdiff(1:size(M_.matched_moments,1),UniqueMomIdx); +if ~isempty(DuplicateMoms) + fprintf('Found and removed duplicate declared moments in ''matched_moments'' block in rows: %s.\n',num2str(DuplicateMoms)) +end +%reorder M_.matched_moments to be compatible with options_mom_.mom.index +M_.matched_moments = M_.matched_moments(UniqueMomIdx,:); +if strcmp(options_mom_.mom.mom_method,'SMM') + options_mom_.mom=rmfield(options_mom_.mom,'index'); +end + +% Check if both prefilter and first moments were specified +options_mom_.mom.first_moment_indicator = find(cellfun(@(x) sum(abs(x))==1,M_.matched_moments(:,3)))'; +if options_mom_.prefilter && ~isempty(options_mom_.mom.first_moment_indicator) + fprintf('Centered moments requested (prefilter option is set); therefore, ignore declared first moments in ''matched_moments'' block in rows: %u.\n',options_mom_.mom.first_moment_indicator'); + M_.matched_moments(options_mom_.mom.first_moment_indicator,:)=[]; %remove first moments entries + options_mom_.mom.first_moment_indicator = []; +end +options_mom_.mom.mom_nbr = size(M_.matched_moments,1); + +% Get maximum lag number for autocovariances/autocorrelations +options_mom_.ar = max(cellfun(@max,M_.matched_moments(:,2))) - min(cellfun(@min,M_.matched_moments(:,2))); + +% ------------------------------------------------------------------------- +% Step 3: Checks and transformations for estimated parameters, priors, and bounds +% ------------------------------------------------------------------------- + +% Set priors and bounds over the estimated parameters +[xparam0, estim_params_, bayestopt_, lb, ub, M_] = set_prior(estim_params_, M_, options_mom_); + +% Check measurement errors +if (estim_params_.nvn || estim_params_.ncn) && strcmp(options_mom_.mom.mom_method, 'GMM') + error('method_of_moments: GMM estimation does not support measurement error(s) yet. Please specifiy them as a structural shock.') +end + +% Check if enough moments for estimation +if options_mom_.mom.mom_nbr < length(xparam0) + fprintf('\n'); + error('method_of_moments: We must have at least as many moments as parameters for a method of moments estimation.') +end +fprintf('\n\n') + +% Check if a _prior_restrictions.m file exists +if exist([M_.fname '_prior_restrictions.m'],'file') + options_mom_.prior_restrictions.status = 1; + options_mom_.prior_restrictions.routine = str2func([M_.fname '_prior_restrictions']); +end + +bayestopt_laplace=bayestopt_; + +% Check on specified priors and penalized estimation +if any(bayestopt_laplace.pshape > 0) % prior specified, not ML + if ~options_mom_.mom.penalized_estimator + fprintf('\nPriors were specified, but the penalized_estimator-option was not set.\n') + fprintf('Dynare sets penalized_estimator to 1. Conducting %s with penalty.\n',options_mom_.mom.mom_method) + options_mom_.mom.penalized_estimator=1; + end + if any(setdiff([0;bayestopt_laplace.pshape],[0,3])) + fprintf('\nNon-normal priors specified. %s with penalty uses a Laplace type of approximation.\n',options_mom_.mom.mom_method) + fprintf('Only the prior mean and standard deviation are relevant, all other shape information, except for the parameter bounds, is ignored.\n\n') + non_normal_priors=bayestopt_laplace.pshape~=3; + bayestopt_laplace.pshape(non_normal_priors) = 3; + bayestopt_laplace.p3(non_normal_priors) = -Inf*ones(sum(non_normal_priors),1); + bayestopt_laplace.p4(non_normal_priors) = Inf*ones(sum(non_normal_priors),1); + bayestopt_laplace.p6(non_normal_priors) = bayestopt_laplace.p1(non_normal_priors); + bayestopt_laplace.p7(non_normal_priors) = bayestopt_laplace.p2(non_normal_priors); + bayestopt_laplace.p5(non_normal_priors) = bayestopt_laplace.p1(non_normal_priors); + end + if any(isinf(bayestopt_laplace.p2)) %find infinite variance priors + inf_var_pars=bayestopt_laplace.name(isinf(bayestopt_laplace.p2)); + disp_string=[inf_var_pars{1,:}]; + for ii=2:size(inf_var_pars,1) + disp_string=[disp_string,', ',inf_var_pars{ii,:}]; + end + fprintf('The parameter(s) %s have infinite prior variance. This implies a flat prior\n',disp_string) + fprintf('Dynare disables the matrix singularity warning\n') + if isoctave + warning('off','Octave:singular-matrix'); + else + warning('off','MATLAB:singularMatrix'); + end + end +end + +% Check for calibrated covariances before updating parameters +estim_params_ = check_for_calibrated_covariances(xparam0,estim_params_,M_); + +% Checks on parameter calibration and initialization +xparam1_calib = get_all_parameters(estim_params_,M_); %get calibrated parameters +if ~any(isnan(xparam1_calib)) %all estimated parameters are calibrated + estim_params_.full_calibration_detected=1; +else + estim_params_.full_calibration_detected=0; +end +if options_mom_.use_calibration_initialization %set calibration as starting values + if ~isempty(bayestopt_laplace) && all(bayestopt_laplace.pshape==0) && any(all(isnan([xparam1_calib xparam0]),2)) + error('method_of_moments: When using the use_calibration option with %s without prior, the parameters must be explicitly initialized.',options_mom_.mom.mom_method) + else + [xparam0,estim_params_]=do_parameter_initialization(estim_params_,xparam1_calib,xparam0); %get explicitly initialized parameters that have precedence over calibrated values + end +end + +% Check initialization +if ~isempty(bayestopt_laplace) && all(bayestopt_laplace.pshape==0) && any(isnan(xparam0)) + error('method_of_moments: %s without penalty requires all estimated parameters to be initialized, either in an estimated_params or estimated_params_init-block ',options_mom_.mom.mom_method) +end + +% Set and check parameter bounds +if ~isempty(bayestopt_laplace) && any(bayestopt_laplace.pshape > 0) + % Plot prior densities + if ~options_mom_.nograph && options_mom_.plot_priors + plot_priors(bayestopt_,M_,estim_params_,options_mom_) + plot_priors(bayestopt_laplace,M_,estim_params_,options_mom_,'Laplace approximated priors') + end + % Set prior bounds + Bounds = prior_bounds(bayestopt_laplace, options_mom_.prior_trunc); + Bounds.lb = max(Bounds.lb,lb); + Bounds.ub = min(Bounds.ub,ub); +else % estimated parameters but no declared priors + % No priors are declared so Dynare will estimate the parameters + % with inequality constraints for the parameters. + Bounds.lb = lb; + Bounds.ub = ub; + if options_mom_.mom.penalized_estimator + fprintf('Penalized estimation turned off as you did not declare priors\n') + options_mom_.mom.penalized_estimator = 0; + end +end +% Set correct bounds for standard deviations and corrrelations +param_of_interest=(1:length(xparam0))'<=estim_params_.nvx+estim_params_.nvn; +LB_below_0=(Bounds.lb<0 & param_of_interest); +Bounds.lb(LB_below_0)=0; +param_of_interest=(1:length(xparam0))'> estim_params_.nvx+estim_params_.nvn & (1:length(xparam0))'<estim_params_.nvx+estim_params_.nvn +estim_params_.ncx + estim_params_.ncn; +LB_below_minus_1=(Bounds.lb<-1 & param_of_interest); +UB_above_1=(Bounds.ub>1 & param_of_interest); +Bounds.lb(LB_below_minus_1)=-1; +Bounds.ub(UB_above_1)=1; + +clear('bayestopt_','LB_below_0','LB_below_minus_1','UB_above_1','param_of_interest');%make sure stale structure cannot be used + +% Test if initial values of the estimated parameters are all between the prior lower and upper bounds +if options_mom_.use_calibration_initialization + try + check_prior_bounds(xparam0,Bounds,M_,estim_params_,options_mom_,bayestopt_laplace) + catch last_error + fprintf('Cannot use parameter values from calibration as they violate the prior bounds.') + rethrow(last_error); + end +else + check_prior_bounds(xparam0,Bounds,M_,estim_params_,options_mom_,bayestopt_laplace) +end + +estim_params_= get_matrix_entries_for_psd_check(M_,estim_params_); + +% Set sigma_e_is_diagonal flag (needed if the shocks block is not declared in the mod file). +M_.sigma_e_is_diagonal = true; +if estim_params_.ncx || any(nnz(tril(M_.Correlation_matrix,-1))) || isfield(estim_params_,'calibrated_covariances') + M_.sigma_e_is_diagonal = false; +end + +% storing prior parameters in MoM info structure for penalized minimization +oo_.prior.mean = bayestopt_laplace.p1; +oo_.prior.variance = diag(bayestopt_laplace.p2.^2); + +% Set all parameters +M_ = set_all_parameters(xparam0,estim_params_,M_); + +%provide warning if there is NaN in parameters +test_for_deep_parameters_calibration(M_); + +% ------------------------------------------------------------------------- +% Step 4: Checks and transformations for data +% ------------------------------------------------------------------------- + +% Check if datafile has same name as mod file +[~,name,~] = fileparts(options_mom_.datafile); +if strcmp(name,M_.fname) + error('method_of_moments: Data-file and mod-file are not allowed to have the same name. Please change the name of the data file.') +end + +% Build dataset +dataset_ = makedataset(options_mom_); + +% set options for old interface from the ones for new interface +if ~isempty(dataset_) + options_mom_.nobs = dataset_.nobs; +end + +% provide info on missing observations +if any(any(isnan(dataset_.data))) + fprintf('missing observations will be replaced by the sample mean of the corresponding moment') +end + +% Check length of data for estimation of second moments +if options_mom_.ar > options_mom_.nobs+1 + error('method_of_moments: Data set is too short to compute second moments'); +end + +% Get data moments for the method of moments +[oo_.mom.data_moments, oo_.mom.m_data] = method_of_moments_data_moments(dataset_.data, oo_, M_.matched_moments, options_mom_); + +% Get shock series for SMM and set variance correction factor +if strcmp(options_mom_.mom.mom_method,'SMM') + options_mom_.mom.long = round(options_mom_.mom.simulation_multiple*options_mom_.nobs); + options_mom_.mom.variance_correction_factor = (1+1/options_mom_.mom.simulation_multiple); + % draw shocks for SMM + smmstream = RandStream('mt19937ar','Seed',options_mom_.mom.seed); + temp_shocks = randn(smmstream,options_mom_.mom.long+options_mom_.mom.burnin,M_.exo_nbr); + temp_shocks_ME = randn(smmstream,options_mom_.mom.long,length(M_.H)); + if options_mom_.mom.bounded_shock_support == 1 + temp_shocks(temp_shocks>2) = 2; + temp_shocks(temp_shocks<-2) = -2; + temp_shocks_ME(temp_shocks_ME<-2) = -2; + temp_shocks_ME(temp_shocks_ME<-2) = -2; + end + options_mom_.mom.shock_series = temp_shocks; + options_mom_.mom.ME_shock_series = temp_shocks_ME; +end + +% ------------------------------------------------------------------------- +% Step 5: checks for steady state at initial parameters +% ------------------------------------------------------------------------- + +% setting steadystate_check_flag option +if options_mom_.steadystate.nocheck + steadystate_check_flag = 0; +else + steadystate_check_flag = 1; +end + +old_steady_params=M_.params; %save initial parameters for check if steady state changes param values +% Check steady state at initial model parameter values +[oo_.steady_state, new_steady_params, info] = evaluate_steady_state(oo_.steady_state,M_,options_mom_,oo_,steadystate_check_flag); +if info(1) + fprintf('\nmethod_of_moments: The steady state at the initial parameters cannot be computed.\n') + print_info(info, 0, options_mom_); +end + +% check whether steady state file changes estimated parameters +if isfield(estim_params_,'param_vals') && ~isempty(estim_params_.param_vals) + Model_par_varied=M_; %store M_ structure + + Model_par_varied.params(estim_params_.param_vals(:,1))=Model_par_varied.params(estim_params_.param_vals(:,1))*1.01; %vary parameters + [~, new_steady_params_2] = evaluate_steady_state(oo_.steady_state,Model_par_varied,options_mom_,oo_,1); + + changed_par_indices=find((old_steady_params(estim_params_.param_vals(:,1))-new_steady_params(estim_params_.param_vals(:,1))) ... + | (Model_par_varied.params(estim_params_.param_vals(:,1))-new_steady_params_2(estim_params_.param_vals(:,1)))); + + if ~isempty(changed_par_indices) + fprintf('\nThe steady state file internally changed the values of the following estimated parameters:\n') + disp(char(M_.param_names(estim_params_.param_vals(changed_par_indices,1)))) + fprintf('This will override parameter values and may lead to wrong results.\n') + fprintf('Check whether this is really intended.\n') + warning('The steady state file internally changes the values of the estimated parameters.') + end +end + +% display warning if some parameters are still NaN +test_for_deep_parameters_calibration(M_); + +% If steady state of observed variables is non zero, set noconstant equal 0 +if all(abs(oo_.steady_state(oo_.dr.order_var(oo_.dr.obs_var)))<1e-9) + options_mom_.noconstant = 1; +else + options_mom_.noconstant = 0; +end + +% ------------------------------------------------------------------------- +% Step 6: checks for objective function at initial parameters +% ------------------------------------------------------------------------- +objective_function = str2func('method_of_moments_objective_function'); +try + % Check for NaN or complex values of moment-distance-funtion evaluated + % at initial parameters and identity weighting matrix + oo_.mom.Sw = eye(options_mom_.mom.mom_nbr); + tic_id = tic; + [fval, info, ~, ~, ~, oo_, M_] = feval(objective_function, xparam0, Bounds, oo_, estim_params_, M_, options_mom_); + elapsed_time = toc(tic_id); + if isnan(fval) + error('method_of_moments: The initial value of the objective function is NaN') + elseif imag(fval) + error('method_of_moments: The initial value of the objective function is complex') + end + if info(1) > 0 + disp('method_of_moments: Error in computing the objective function for initial parameter values') + print_info(info, options_mom_.noprint, options_mom_) + end + fprintf('Initial value of the moment objective function with %4.1f times identity weighting matrix: %6.4f \n\n', options_mom_.mom.weighting_matrix_scaling_factor, fval); + fprintf('Time required to compute objective function once: %5.4f seconds \n', elapsed_time); + +catch last_error% if check fails, provide info on using calibration if present + if estim_params_.full_calibration_detected %calibrated model present and no explicit starting values + skipline(1); + fprintf('There was an error in computing the moments for initial parameter values.\n') + fprintf('If this is not a problem with the setting of options (check the error message below),\n') + fprintf('you should try using the calibrated version of the model as starting values. To do\n') + fprintf('this, add an empty estimated_params_init-block with use_calibration option immediately before the estimation\n') + fprintf('command (and after the estimated_params-block so that it does not get overwritten):\n'); + skipline(2); + end + rethrow(last_error); +end + +if options_mom_.mode_compute == 0 %We only report value of moments distance at initial value of the parameters + fprintf('No minimization of moments distance due to ''mode_compute=0''\n') + return +end + +% ------------------------------------------------------------------------- +% Step 7a: Method of moments estimation: print some info +% ------------------------------------------------------------------------- +fprintf('\n---------------------------------------------------\n') +if strcmp(options_mom_.mom.mom_method,'SMM') + fprintf('Simulated method of moments with'); +elseif strcmp(options_mom_.mom.mom_method,'GMM') + fprintf('General method of moments with'); +end +if options_mom_.prefilter + fprintf('\n - centered moments (prefilter=1)'); +else + fprintf('\n - uncentered moments (prefilter=0)'); +end +if options_mom_.mom.penalized_estimator + fprintf('\n - penalized estimation using deviation from prior mean and weighted with prior precision'); +end +if options_mom_.mode_compute == 1; fprintf('\n - optimizer (mode_compute=1): fmincon'); +elseif options_mom_.mode_compute == 2; fprintf('\n - optimizer (mode_compute=2): continuous simulated annealing'); +elseif options_mom_.mode_compute == 3; fprintf('\n - optimizer (mode_compute=3): fminunc'); +elseif options_mom_.mode_compute == 4; fprintf('\n - optimizer (mode_compute=4): csminwel'); +elseif options_mom_.mode_compute == 5; fprintf('\n - optimizer (mode_compute=5): newrat'); +elseif options_mom_.mode_compute == 6; fprintf('\n - optimizer (mode_compute=6): gmhmaxlik'); +elseif options_mom_.mode_compute == 7; fprintf('\n - optimizer (mode_compute=7): fminsearch'); +elseif options_mom_.mode_compute == 8; fprintf('\n - optimizer (mode_compute=8): Dynare Nelder-Mead simplex'); +elseif options_mom_.mode_compute == 9; fprintf('\n - optimizer (mode_compute=9): CMA-ES'); +elseif options_mom_.mode_compute == 10; fprintf('\n - optimizer (mode_compute=10): simpsa'); +elseif options_mom_.mode_compute == 11; fprintf('\n - optimizer (mode_compute=11): online_auxiliary_filter'); +elseif options_mom_.mode_compute == 12; fprintf('\n - optimizer (mode_compute=12): particleswarm'); +elseif options_mom_.mode_compute == 101; fprintf('\n - optimizer (mode_compute=101): SolveOpt'); +elseif options_mom_.mode_compute == 102; fprintf('\n - optimizer (mode_compute=102): simulannealbnd'); +elseif options_mom_.mode_compute == 13; fprintf('\n - optimizer (mode_compute=13): lsqnonlin'); +elseif ischar(minimizer_algorithm); fprintf(['\n - user-defined optimizer: ' minimizer_algorithm]); +else + error('method_of_moments: Unknown optimizer, please contact the developers ') +end +if options_mom_.silent_optimizer + fprintf(' (silent)'); +end +fprintf('\n - perturbation order: %d', options_mom_.order) +if options_mom_.order > 1 && options_mom_.pruning + fprintf(' (with pruning)') +end +fprintf('\n - number of matched moments: %d', options_mom_.mom.mom_nbr); +fprintf('\n - number of parameters: %d\n\n', length(xparam0)); + +% ------------------------------------------------------------------------- +% Step 7b: Iterated method of moments estimation +% ------------------------------------------------------------------------- +if size(options_mom_.mom.weighting_matrix,1)>1 && ~(any(strcmpi('diagonal',options_mom_.mom.weighting_matrix)) || any(strcmpi('optimal',options_mom_.mom.weighting_matrix))) + fprintf('\nYou did not specify the use of an optimal or diagonal weighting matrix. There is no point in running an iterated method of moments.\n') +end + +optimizer_vec=[options_mom_.mode_compute,options_mom_.additional_optimizer_steps]; % at each stage one can possibly use different optimizers sequentially + +for stage_iter=1:size(options_mom_.mom.weighting_matrix,1) + fprintf('Estimation stage %u\n',stage_iter); + Woptflag = false; + switch lower(options_mom_.mom.weighting_matrix{stage_iter}) + case 'identity_matrix' + fprintf(' - identity weighting matrix\n'); + weighting_matrix = eye(options_mom_.mom.mom_nbr); + case 'diagonal' + fprintf(' - diagonal of optimal weighting matrix (Bartlett kernel with %d lags)\n', options_mom_.mom.bartlett_kernel_lag); + if stage_iter == 1 + fprintf(' and using data-moments as initial estimate of model-moments\n'); + weighting_matrix = diag(diag( method_of_moments_optimal_weighting_matrix(oo_.mom.m_data, oo_.mom.data_moments, options_mom_.mom.bartlett_kernel_lag) )); + else + fprintf(' and using previous stage estimate of model-moments\n'); + weighting_matrix = diag(diag( method_of_moments_optimal_weighting_matrix(oo_.mom.m_data, oo_.mom.model_moments, options_mom_.mom.bartlett_kernel_lag) )); + end + case 'optimal' + fprintf(' - optimal weighting matrix (Bartlett kernel with %d lags)\n', options_mom_.mom.bartlett_kernel_lag); + if stage_iter == 1 + fprintf(' and using data-moments as initial estimate of model-moments\n'); + weighting_matrix = method_of_moments_optimal_weighting_matrix(oo_.mom.m_data, oo_.mom.data_moments, options_mom_.mom.bartlett_kernel_lag); + else + fprintf(' and using previous stage estimate of model-moments\n'); + weighting_matrix = method_of_moments_optimal_weighting_matrix(oo_.mom.m_data, oo_.mom.model_moments, options_mom_.mom.bartlett_kernel_lag); + Woptflag = true; + end + otherwise %user specified matrix in file + fprintf(' - user-specified weighting matrix\n'); + try + load(options_mom_.mom.weighting_matrix{stage_iter},'weighting_matrix') + catch + error(['method_of_moments: No matrix named ''weighting_matrix'' could be found in ',options_mom_.mom.weighting_matrix{stage_iter},'.mat']) + end + [nrow, ncol] = size(weighting_matrix); + if ~isequal(nrow,ncol) || ~isequal(nrow,length(oo_.mom.data_moments)) %check if square and right size + error(['method_of_moments: weighting_matrix must be square and have ',num2str(length(oo_.mom.data_moments)),' rows and columns']) + end + end + try %check for positive definiteness of weighting_matrix + oo_.mom.Sw = chol(weighting_matrix); + catch + error('method_of_moments: Specified weighting_matrix is not positive definite. Check whether your model implies stochastic singularity.') + end + + for optim_iter= 1:length(optimizer_vec) + if optimizer_vec(optim_iter)==13 + options_mom_.vector_output = true; + else + options_mom_.vector_output = false; + end + [xparam1, fval, exitflag] = dynare_minimize_objective(objective_function, xparam0, optimizer_vec(optim_iter), options_mom_, [Bounds.lb Bounds.ub], bayestopt_laplace.name, bayestopt_laplace, [],... + Bounds, oo_, estim_params_, M_, options_mom_); + if options_mom_.vector_output + fval = fval'*fval; + end + fprintf('\nStage %d Iteration %d: value of minimized moment distance objective function: %12.10f.\n',stage_iter,optim_iter,fval) + if options_mom_.mom.verbose + oo_.mom=display_estimation_results_table(xparam1,NaN(size(xparam1)),M_,options_mom_,estim_params_,bayestopt_laplace,oo_.mom,prior_dist_names,sprintf('%s (STAGE %d ITERATION %d) VERBOSE',options_mom_.mom.mom_method,stage_iter,optim_iter),sprintf('verbose_%s_stage_%d_iter_%d',lower(options_mom_.mom.mom_method),stage_iter,optim_iter)); + end + xparam0=xparam1; + end + options_mom_.vector_output = false; + % Update M_ and DynareResults (in particular to get oo_.mom.model_moments) + M_ = set_all_parameters(xparam1,estim_params_,M_); + [fval, ~, ~,~,~, oo_] = feval(objective_function, xparam1, Bounds, oo_, estim_params_, M_, options_mom_); + % Compute Standard errors + SE = method_of_moments_standard_errors(xparam1, objective_function, Bounds, oo_, estim_params_, M_, options_mom_, Woptflag); + + % Store results in output structure + oo_.mom = display_estimation_results_table(xparam1,SE,M_,options_mom_,estim_params_,bayestopt_laplace,oo_.mom,prior_dist_names,sprintf('%s (STAGE %u)',options_mom_.mom.mom_method,stage_iter),sprintf('%s_stage_%u',lower(options_mom_.mom.mom_method),stage_iter)); +end + +% ------------------------------------------------------------------------- +% Step 8: J test +% ------------------------------------------------------------------------- +if options_mom_.mom.mom_nbr > length(xparam1) + %get optimal weighting matrix for J test, if necessary + if ~Woptflag + W_opt = method_of_moments_optimal_weighting_matrix(oo_.mom.m_data, oo_.mom.model_moments, options_mom_.mom.bartlett_kernel_lag); + oo_j=oo_; + oo_j.mom.Sw = chol(W_opt); + [fval] = feval(objective_function, xparam1, Bounds, oo_j, estim_params_, M_, options_mom_); + end + + % Compute J statistic + if strcmp(options_mom_.mom.mom_method,'SMM') + Variance_correction_factor = options_mom_.mom.variance_correction_factor; + elseif strcmp(options_mom_.mom.mom_method,'GMM') + Variance_correction_factor=1; + end + oo_.mom.J_test.j_stat = dataset_.nobs*Variance_correction_factor*fval/options_mom_.mom.weighting_matrix_scaling_factor; + oo_.mom.J_test.degrees_freedom = length(oo_.mom.model_moments)-length(xparam1); + oo_.mom.J_test.p_val = 1-chi2cdf(oo_.mom.J_test.j_stat, oo_.mom.J_test.degrees_freedom); + fprintf('\nvalue of J-test statistic: %f\n',oo_.mom.J_test.j_stat) + fprintf('p-value of J-test statistic: %f\n',oo_.mom.J_test.p_val) +end + +% ------------------------------------------------------------------------- +% Step 9: Display estimation results +% ------------------------------------------------------------------------- +title = ['Data moments and model moments (',options_mom_.mom.mom_method,')']; +headers = {'Moment','Data','Model','% dev. target'}; +labels= M_.matched_moments(:,4); +data_mat=[oo_.mom.data_moments oo_.mom.model_moments 100*abs((oo_.mom.model_moments-oo_.mom.data_moments)./oo_.mom.data_moments)]; +dyntable(options_mom_, title, headers, labels, data_mat, cellofchararraymaxlength(labels)+2, 10, 7); +if options_mom_.TeX + lh = cellofchararraymaxlength(labels)+2; + labels_TeX = M_.matched_moments(:,5); + dyn_latex_table(M_, options_mom_, title, 'sim_corr_matrix', headers, labels_TeX, data_mat, lh, 10, 7); +end + +if options_mom_.mode_check.status + method_of_moments_mode_check(objective_function,xparam1,SE,options_mom_,M_,estim_params_,Bounds,bayestopt_laplace,... + Bounds, oo_, estim_params_, M_, options_mom_) +end + +fprintf('\n==== Method of Moments Estimation (%s) Completed ====\n\n',options_mom_.mom.mom_method) + +% ------------------------------------------------------------------------- +% Step 9: Clean up +% ------------------------------------------------------------------------- +%reset warning state +if isoctave + warning('on') +else + warning on +end diff --git a/matlab/method_of_moments/method_of_moments_data_moments.m b/matlab/method_of_moments/method_of_moments_data_moments.m new file mode 100644 index 0000000000000000000000000000000000000000..38d205a272f7e6155e52b81e4c9aa3c7a067a24d --- /dev/null +++ b/matlab/method_of_moments/method_of_moments_data_moments.m @@ -0,0 +1,71 @@ +function [dataMoments, m_data] = method_of_moments_data_moments(data, oo_, matched_moments_, options_mom_) +% [dataMoments, m_data] = method_of_moments_data_moments(data, oo_, matched_moments_, options_mom_) +% This function computes the user-selected empirical moments from data +% ========================================================================= +% INPUTS +% o data [T x varobs_nbr] data set +% o oo_: [structure] storage for results +% o matched_moments_: [structure] information about selected moments to match in estimation +% o options_mom_: [structure] information about all settings (specified by the user, preprocessor, and taken from global options_) +% ------------------------------------------------------------------------- +% OUTPUTS +% o dataMoments [numMom x 1] mean of selected empirical moments +% o m_data [T x numMom] selected empirical moments at each point in time +% ------------------------------------------------------------------------- +% This function is called by +% o method_of_moments.m +% o method_of_moments_objective_function.m +% ========================================================================= +% Copyright (C) 2020 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/>. +% ------------------------------------------------------------------------- +% Author(s): +% o Willi Mutschler (willi@mutschler.eu) +% o Johannes Pfeifer (jpfeifer@uni-koeln.de) +% ========================================================================= + +% Initialization +T = size(data,1); % Number of observations (T) +dataMoments = NaN(options_mom_.mom.mom_nbr,1); +m_data = NaN(T,options_mom_.mom.mom_nbr); +% Product moment for each time period, i.e. each row t contains y_t1(l1)^p1*y_t2(l2)^p2*... +% note that here we already are able to treat leads and lags and any power product moments +for jm = 1:options_mom_.mom.mom_nbr + vars = oo_.dr.inv_order_var(matched_moments_{jm,1})'; + leadlags = matched_moments_{jm,2}; % lags are negative numbers and leads are positive numbers + powers = matched_moments_{jm,3}; + for jv = 1:length(vars) + jvar = (oo_.dr.obs_var == vars(jv)); + y = NaN(T,1); %Take care of T_eff instead of T for lags and NaN via mean with 'omitnan' option below + y( (1-min(leadlags(jv),0)) : (T-max(leadlags(jv),0)), 1) = data( (1+max(leadlags(jv),0)) : (T+min(leadlags(jv),0)), jvar).^powers(jv); + if jv==1 + m_data_tmp = y; + else + m_data_tmp = m_data_tmp.*y; + end + end + % We replace NaN (due to leads and lags and missing values) with the corresponding mean + dataMoments(jm,1) = mean(m_data_tmp,'omitnan'); + m_data_tmp(isnan(m_data_tmp)) = dataMoments(jm,1); + m_data(:,jm) = m_data_tmp; +end + + +end %function end + + + diff --git a/matlab/method_of_moments/method_of_moments_mode_check.m b/matlab/method_of_moments/method_of_moments_mode_check.m new file mode 100644 index 0000000000000000000000000000000000000000..1df8af84f11105a0431bd222d5c70a0662539b4e --- /dev/null +++ b/matlab/method_of_moments/method_of_moments_mode_check.m @@ -0,0 +1,185 @@ +function method_of_moments_mode_check(fun,xparam,SE_vec,options_,M_,estim_params_,Bounds,bayestopt_,varargin) +% Checks the estimated ML mode or Posterior mode. + + +% Copyright (C) 2020 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/>. + +TeX = options_.TeX; +if ~isempty(SE_vec) + [ s_min, k ] = min(SE_vec); +end + +fval = feval(fun,xparam,varargin{:}); + +if ~isempty(SE_vec) + skipline() + disp('MODE CHECK') + skipline() + fprintf('Fval obtained by the minimization routine: %f', fval); + skipline() + if s_min<eps + fprintf('Most negative variance %f for parameter %d (%s = %f)', s_min, k , bayestopt_.name{k}, xparam(k)) + end +end + +[nbplt,nr,nc,lr,lc,nstar] = pltorg(length(xparam)); + +if ~exist([M_.fname filesep 'graphs'],'dir') + mkdir(M_.fname,'graphs'); +end +if TeX && any(strcmp('eps',cellstr(options_.graph_format))) + fidTeX = fopen([M_.fname, '/graphs/', M_.fname '_MoMCheckPlots.tex'],'w'); + fprintf(fidTeX,'%% TeX eps-loader file generated by method_of_moments_mode_check.m (Dynare).\n'); + fprintf(fidTeX,['%% ' datestr(now,0) '\n']); + fprintf(fidTeX,' \n'); +end + +ll = options_.mode_check.neighbourhood_size; +if isinf(ll) + options_.mode_check.symmetric_plots = false; +end + +mcheck = struct('cross',struct(),'emode',struct()); + +for plt = 1:nbplt + if TeX + NAMES = []; + TeXNAMES = []; + end + hh = dyn_figure(options_.nodisplay,'Name','Mode check plots'); + for k=1:min(nstar,length(xparam)-(plt-1)*nstar) + subplot(nr,nc,k) + kk = (plt-1)*nstar+k; + [name,texname] = get_the_name(kk,TeX,M_,estim_params_,options_); + xx = xparam; + if xparam(kk)~=0 || ~isinf(Bounds.lb(kk)) || ~isinf(Bounds.lb(kk)) + l1 = max(Bounds.lb(kk),(1-sign(xparam(kk))*ll)*xparam(kk)); m1 = 0; %lower bound + l2 = min(Bounds.ub(kk),(1+sign(xparam(kk))*ll)*xparam(kk)); %upper bound + else + %size info for 0 parameter is missing, use prior standard + %deviation + upper_bound=Bounds.lb(kk); + if isinf(upper_bound) + upper_bound=-1e-6*options_.huge_number; + end + lower_bound=Bounds.ub(kk); + if isinf(lower_bound) + lower_bound=-1e-6*options_.huge_number; + end + l1 = max(lower_bound,-bayestopt_.p2(kk)); m1 = 0; %lower bound + l2 = min(upper_bound,bayestopt_.p2(kk)); %upper bound + end + binding_lower_bound=0; + binding_upper_bound=0; + if isequal(xparam(kk),Bounds.lb(kk)) + binding_lower_bound=1; + bound_value=Bounds.lb(kk); + elseif isequal(xparam(kk),Bounds.ub(kk)) + binding_upper_bound=1; + bound_value=Bounds.ub(kk); + end + if options_.mode_check.symmetric_plots && ~binding_lower_bound && ~binding_upper_bound + if l2<(1+ll)*xparam(kk) %test whether upper bound is too small due to prior binding + l1 = xparam(kk) - (l2-xparam(kk)); %adjust lower bound to become closer + m1 = 1; + end + if ~m1 && (l1>(1-ll)*xparam(kk)) && (xparam(kk)+(xparam(kk)-l1)<Bounds.ub(kk)) % if lower bound was truncated and using difference from lower bound does not violate upper bound + l2 = xparam(kk) + (xparam(kk)-l1); %set upper bound to same distance as lower bound + end + end + z1 = l1:((xparam(kk)-l1)/(options_.mode_check.number_of_points/2)):xparam(kk); + z2 = xparam(kk):((l2-xparam(kk))/(options_.mode_check.number_of_points/2)):l2; + z = union(z1,z2); + if options_.mom.penalized_estimator + y = zeros(length(z),2); + dy=(xx-bayestopt_.p1)'/diag(bayestopt_.p2.^2)*(xx-bayestopt_.p1); + else + y = zeros(length(z),1); + end + for i=1:length(z) + xx(kk) = z(i); + [fval, info, exit_flag] = feval(fun,xx,varargin{:}); + if exit_flag + y(i,1) = fval; + else + y(i,1) = NaN; + if options_.debug + fprintf('mode_check:: could not solve model for parameter %s at value %4.3f, error code: %u\n',name,z(i),info(1)) + end + end + if options_.mom.penalized_estimator + prior=(xx-bayestopt_.p1)'/diag(bayestopt_.p2.^2)*(xx-bayestopt_.p1); + y(i,2) = (y(i,1)+prior-dy); + end + end + mcheck.cross = setfield(mcheck.cross, name, [transpose(z), -y]); + mcheck.emode = setfield(mcheck.emode, name, xparam(kk)); + fighandle=plot(z,-y); + hold on + yl=get(gca,'ylim'); + plot( [xparam(kk) xparam(kk)], yl, 'c', 'LineWidth', 1) + NaN_index = find(isnan(y(:,1))); + zNaN = z(NaN_index); + yNaN = yl(1)*ones(size(NaN_index)); + plot(zNaN,yNaN,'o','MarkerEdgeColor','r','MarkerFaceColor','r','MarkerSize',6); + if TeX + title(texname,'interpreter','latex') + else + title(name,'interpreter','none') + end + + axis tight + if binding_lower_bound || binding_upper_bound + xl=get(gca,'xlim'); + plot( [bound_value bound_value], yl, 'r--', 'LineWidth', 1) + xlim([xl(1)-0.5*binding_lower_bound*(xl(2)-xl(1)) xl(2)+0.5*binding_upper_bound*(xl(2)-xl(1))]) + end + hold off + drawnow + end + if options_.mom.penalized_estimator + if isoctave + axes('outerposition',[0.3 0.93 0.42 0.07],'box','on'), + else + axes('position',[0.3 0.01 0.42 0.05],'box','on'), + end + line_color=get(fighandle,'color'); + plot([0.48 0.68],[0.5 0.5],'color',line_color{2}) + hold on, plot([0.04 0.24],[0.5 0.5],'color',line_color{1}) + set(gca,'xlim',[0 1],'ylim',[0 1],'xtick',[],'ytick',[]) + text(0.25,0.5,'log-post') + text(0.69,0.5,'log-lik kernel') + end + dyn_saveas(hh,[M_.fname, '/graphs/', M_.fname '_MoMCheckPlots' int2str(plt) ],options_.nodisplay,options_.graph_format); + if TeX && any(strcmp('eps',cellstr(options_.graph_format))) + % TeX eps loader file + fprintf(fidTeX,'\\begin{figure}[H]\n'); + fprintf(fidTeX,'\\centering \n'); + fprintf(fidTeX,'\\includegraphics[width=%2.2f\\textwidth]{%_MoMCheckPlots%s}\n',options_.figures.textwidth*min(k/nc,1),[M_.fname, '/graphs/',M_.fname],int2str(plt)); + fprintf(fidTeX,'\\caption{Method of Moments check plots.}'); + fprintf(fidTeX,'\\label{Fig:MoMCheckPlots:%s}\n',int2str(plt)); + fprintf(fidTeX,'\\end{figure}\n'); + fprintf(fidTeX,' \n'); + end +end +if TeX && any(strcmp('eps',cellstr(options_.graph_format))) + fclose(fidTeX); +end + +OutputDirectoryName = CheckPath('modecheck',M_.dname); +save([OutputDirectoryName '/MoM_check_plot_data.mat'],'mcheck'); diff --git a/matlab/method_of_moments/method_of_moments_objective_function.m b/matlab/method_of_moments/method_of_moments_objective_function.m new file mode 100644 index 0000000000000000000000000000000000000000..8a15ed749b2cf74bc2a351f23d0350f7182294ee --- /dev/null +++ b/matlab/method_of_moments/method_of_moments_objective_function.m @@ -0,0 +1,214 @@ +function [fval, info, exit_flag, junk1, junk2, oo_, M_, options_mom_] = method_of_moments_objective_function(xparam1, Bounds, oo_, estim_params_, M_, options_mom_) +% [fval, info, exit_flag, junk1, junk2, oo_, M_, options_mom_] = method_of_moments_objective_function(xparam1, Bounds, oo_, estim_params_, M_, options_mom_) +% ------------------------------------------------------------------------- +% This function evaluates the objective function for GMM/SMM estimation +% ========================================================================= +% INPUTS +% o xparam1: current value of estimated parameters as returned by set_prior() +% o Bounds: structure containing parameter bounds +% o oo_: structure for results +% o estim_params_: structure describing the estimated_parameters +% o M_ structure describing the model +% o options_mom_: structure information about all settings (specified by the user, preprocessor, and taken from global options_) +% ------------------------------------------------------------------------- +% OUTPUTS +% o fval: value of the quadratic form of the moment difference (except for lsqnonlin, where this is done implicitly) +% o info: vector storing error code and penalty +% o exit_flag: 0 if error, 1 if no error +% o junk1: empty matrix required for optimizer interface +% o junk2: empty matrix required for optimizer interface +% o oo_: structure containing the results with the following updated fields: +% - mom.model_moments [numMom x 1] vector with model moments +% - mom.Q value of the quadratic form of the moment difference +% o M_: Matlab's structure describing the model +% ------------------------------------------------------------------------- +% This function is called by +% o method_of_moments.m +% ------------------------------------------------------------------------- +% This function calls +% o check_bounds_and_definiteness_estimation +% o pruned_state_space_system +% o resol +% o set_all_parameters +% ========================================================================= +% Copyright (C) 2020 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/>. +% ------------------------------------------------------------------------- +% Author(s): +% o Willi Mutschler (willi@mutschler.eu) +% o Johannes Pfeifer (jpfeifer@uni-koeln.de) +% ========================================================================= + +%------------------------------------------------------------------------------ +% 0. Initialization of the returned variables and others... +%------------------------------------------------------------------------------ + +junk1 = []; +junk2 = []; + +%-------------------------------------------------------------------------- +% 1. Get the structural parameters & define penalties +%-------------------------------------------------------------------------- + +M_ = set_all_parameters(xparam1, estim_params_, M_); + +[fval,info,exit_flag]=check_bounds_and_definiteness_estimation(xparam1, M_, estim_params_, Bounds); +if info(1) + if options_mom_.vector_output == 1 % lsqnonlin requires vector output + fval = ones(size(oo_.mom.data_moments,1),1)*options_mom_.huge_number; + end + return +end + +%-------------------------------------------------------------------------- +% 2. call resol to compute steady state and model solution +%-------------------------------------------------------------------------- + +% Compute linear approximation around the deterministic steady state +[dr, info, M_, options_mom_, oo_] = resol(0, M_, options_mom_, oo_); + +% Return, with endogenous penalty when possible, if resol issues an error code +if info(1) + if info(1) == 3 || info(1) == 4 || info(1) == 5 || info(1)==6 ||info(1) == 19 ||... + info(1) == 20 || info(1) == 21 || info(1) == 23 || info(1) == 26 || ... + info(1) == 81 || info(1) == 84 || info(1) == 85 || info(1) == 86 + %meaningful second entry of output that can be used + fval = Inf; + info(4) = info(2); + exit_flag = 0; + if options_mom_.vector_output == 1 % lsqnonlin requires vector output + fval = ones(size(oo_.mom.data_moments,1),1)*options_mom_.huge_number; + end + return + else + fval = Inf; + info(4) = 0.1; + exit_flag = 0; + if options_mom_.vector_output == 1 % lsqnonlin requires vector output + fval = ones(size(oo_.mom.data_moments,1),1)*options_mom_.huge_number; + end + return + end +end + +if strcmp(options_mom_.mom.mom_method,'GMM') + %-------------------------------------------------------------------------- + % 3. Set up pruned state-space system and compute model moments + %-------------------------------------------------------------------------- + pruned_state_space = pruned_state_space_system(M_, options_mom_, dr, oo_.dr.obs_var, options_mom_.ar, 0, 0); + + oo_.mom.model_moments = NaN(options_mom_.mom.mom_nbr,1); + offset = 0; + % First moments + if ~options_mom_.prefilter && isfield(options_mom_.mom.index,'E_y') && nnz(options_mom_.mom.index.E_y) > 0 + E_y = pruned_state_space.E_y; + E_y_nbr = nnz(options_mom_.mom.index.E_y); + oo_.mom.model_moments(offset+1:E_y_nbr,1) = E_y(options_mom_.mom.index.E_y); + offset = offset + E_y_nbr; + end + % Second moments + % Contemporaneous covariance + if isfield(options_mom_.mom.index,'E_yy') && nnz(options_mom_.mom.index.E_yy) > 0 + if options_mom_.prefilter + E_yy = pruned_state_space.Var_y; + else + E_yy = pruned_state_space.Var_y + pruned_state_space.E_y*pruned_state_space.E_y'; + end + E_yy_nbr = nnz(tril(options_mom_.mom.index.E_yy)); + oo_.mom.model_moments(offset+(1:E_yy_nbr),1) = E_yy(tril(options_mom_.mom.index.E_yy)); + offset = offset + E_yy_nbr; + end + % Lead/lags covariance + if isfield(options_mom_.mom.index,'E_yyt') && nnz(options_mom_.mom.index.E_yyt) > 0 + if options_mom_.prefilter + E_yyt = pruned_state_space.Var_yi; + else + E_yyt = pruned_state_space.Var_yi + repmat(pruned_state_space.E_y*pruned_state_space.E_y',[1 1 size(pruned_state_space.Var_yi,3)]); + end + E_yyt_nbr = nnz(options_mom_.mom.index.E_yyt); + oo_.mom.model_moments(offset+(1:E_yyt_nbr),1) = E_yyt(options_mom_.mom.index.E_yyt); + end + +elseif strcmp(options_mom_.mom.mom_method,'SMM') + %------------------------------------------------------------------------------ + % 3. Compute Moments of the model solution for normal innovations + %------------------------------------------------------------------------------ + + % create shock series with correct covariance matrix from iid standard normal shocks + i_exo_var = setdiff(1:M_.exo_nbr, find(diag(M_.Sigma_e) == 0 )); %find singular entries in covariance + chol_S = chol(M_.Sigma_e(i_exo_var,i_exo_var)); + scaled_shock_series = zeros(size(options_mom_.mom.shock_series)); %initialize + scaled_shock_series(:,i_exo_var) = options_mom_.mom.shock_series(:,i_exo_var)*chol_S; %set non-zero entries + + % simulate series + y_sim = simult_(M_, options_mom_, dr.ys, dr, scaled_shock_series, options_mom_.order); + % provide meaningful penalty if data is nan or inf + if any(any(isnan(y_sim))) || any(any(isinf(y_sim))) + if options_mom_.mode_compute==13 + fval = Inf(size(oo_.mom.Sw,1),1); + else + fval = Inf; + end + info(1)=180; + info(4) = 0.1; + exit_flag = 0; + if options_mom_.mode_compute == 13 + fval = ones(size(oo_.mom.dataMoments,1),1)*options_mom_.huge_number; + end + return + end + + % Remove burn-in and focus on observables (note that y_sim is in declaration order) + y_sim = y_sim(oo_.dr.order_var(oo_.dr.obs_var) , end-options_mom_.mom.long+1:end)'; + + if ~all(diag(M_.H)==0) + i_ME = setdiff([1:size(M_.H,1)],find(diag(M_.H) == 0)); % find ME with 0 variance + chol_S = chol(M_.H(i_ME,i_ME)); %decompose rest + shock_mat=zeros(size(options_mom_.mom.ME_shock_series)); %initialize + shock_mat(:,i_ME)=options_mom_.mom.ME_shock_series(:,i_exo_var)*chol_S; + y_sim = y_sim+shock_mat; + end + + % Remove mean if centered moments + if options_mom_.prefilter + y_sim = bsxfun(@minus, y_sim, mean(y_sim,1)); + end + oo_.mom.model_moments = method_of_moments_data_moments(y_sim, oo_, M_.matched_moments, options_mom_); + +end + +%-------------------------------------------------------------------------- +% 4. Compute quadratic target function +%-------------------------------------------------------------------------- +moments_difference = oo_.mom.data_moments - oo_.mom.model_moments; +residuals = sqrt(options_mom_.mom.weighting_matrix_scaling_factor)*oo_.mom.Sw*moments_difference; +oo_.mom.Q = residuals'*residuals; +if options_mom_.vector_output == 1 % lsqnonlin requires vector output + fval = residuals; + if options_mom_.mom.penalized_estimator + fval=[fval;(xparam1-oo_.prior.mean)./sqrt(diag(oo_.prior.variance))]; + end +else + fval = oo_.mom.Q; + if options_mom_.mom.penalized_estimator + fval=fval+(xparam1-oo_.prior.mean)'/oo_.prior.variance*(xparam1-oo_.prior.mean); + end +end + + +end%main function end + diff --git a/matlab/method_of_moments/method_of_moments_optimal_weighting_matrix.m b/matlab/method_of_moments/method_of_moments_optimal_weighting_matrix.m new file mode 100644 index 0000000000000000000000000000000000000000..7dde93568d45b011e081e63b615c5ffbcb9749be --- /dev/null +++ b/matlab/method_of_moments/method_of_moments_optimal_weighting_matrix.m @@ -0,0 +1,79 @@ +function W_opt = method_of_moments_optimal_weighting_matrix(m_data, moments, q_lag) +% W_opt = method_of_moments_optimal_weighting_matrix(m_data, moments, q_lag) +% ------------------------------------------------------------------------- +% This function computes the optimal weigthing matrix by a Bartlett kernel with maximum lag q_lag +% Adapted from replication codes of +% o Andreasen, Fernández-Villaverde, Rubio-Ramírez (2018): "The Pruned State-Space System for Non-Linear DSGE Models: Theory and Empirical Applications", Review of Economic Studies, 85(1):1-49. +% ========================================================================= +% INPUTS +% o m_data [T x numMom] selected data moments at each point in time +% o moments [numMom x 1] selected estimated moments (either data_moments or estimated model_moments) +% o q_lag [integer] Bartlett kernel maximum lag order +% ------------------------------------------------------------------------- +% OUTPUTS +% o W_opt [numMom x numMom] optimal weighting matrix +% ------------------------------------------------------------------------- +% This function is called by +% o method_of_moments.m +% ------------------------------------------------------------------------- +% This function calls: +% o CorrMatrix (embedded) +% ========================================================================= +% Copyright (C) 2020 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/>. +% ------------------------------------------------------------------------- +% Author(s): +% o Willi Mutschler (willi@mutschler.eu) +% o Johannes Pfeifer (jpfeifer@uni-koeln.de) +% ========================================================================= + +% Initialize +[T,num_Mom] = size(m_data); %note that in m_data NaN values (due to leads or lags in matched_moments and missing data) were replaced by the mean + +% center around moments (could be either data_moments or model_moments) +h_Func = m_data - repmat(moments',T,1); + +% The required correlation matrices +GAMA_array = zeros(num_Mom,num_Mom,q_lag); +GAMA0 = Corr_Matrix(h_Func,T,num_Mom,0); +if q_lag > 0 + for ii=1:q_lag + GAMA_array(:,:,ii) = Corr_Matrix(h_Func,T,num_Mom,ii); + end +end + +% The estimate of S +S = GAMA0; +if q_lag > 0 + for ii=1:q_lag + S = S + (1-ii/(q_lag+1))*(GAMA_array(:,:,ii) + GAMA_array(:,:,ii)'); + end +end + +% The estimate of W +W_opt = S\eye(size(S,1)); + +end + +% The correlation matrix +function GAMA_corr = Corr_Matrix(h_Func,T,num_Mom,v) + GAMA_corr = zeros(num_Mom,num_Mom); + for t = 1+v:T + GAMA_corr = GAMA_corr + h_Func(t-v,:)'*h_Func(t,:); + end + GAMA_corr = GAMA_corr/T; +end \ No newline at end of file diff --git a/matlab/method_of_moments/method_of_moments_standard_errors.m b/matlab/method_of_moments/method_of_moments_standard_errors.m new file mode 100644 index 0000000000000000000000000000000000000000..888ccf01f59e9650a5a3472c146d7254eaf67e8b --- /dev/null +++ b/matlab/method_of_moments/method_of_moments_standard_errors.m @@ -0,0 +1,103 @@ +function [SE_values, Asympt_Var] = method_of_moments_standard_errors(xparam, objective_function, Bounds, oo_, estim_params_, M_, options_mom_, Wopt_flag) +% [SE_values, Asympt_Var] = method_of_moments_standard_errors(xparam, objective_function, Bounds, oo_, estim_params_, M_, options_mom_, Wopt_flag) +% ------------------------------------------------------------------------- +% This function computes standard errors to the method of moments estimates +% Adapted from replication codes of +% o Andreasen, Fernández-Villaverde, Rubio-Ramírez (2018): "The Pruned State-Space System for Non-Linear DSGE Models: Theory and Empirical Applications", Review of Economic Studies, 85(1):1-49. +% ========================================================================= +% INPUTS +% o xparam: value of estimated parameters as returned by set_prior() +% o objective_function string of objective function, either method_of_moments_GMM.m or method_of_moments_SMM.m +% o Bounds: structure containing parameter bounds +% o oo_: structure for results +% o estim_params_: structure describing the estimated_parameters +% o M_ structure describing the model +% o options_mom_: structure information about all settings (specified by the user, preprocessor, and taken from global options_) +% o Wopt_flag: indicator whether the optimal weighting is actually used +% ------------------------------------------------------------------------- +% OUTPUTS +% o SE_values [nparam x 1] vector of standard errors +% o Asympt_Var [nparam x nparam] asymptotic covariance matrix +% ------------------------------------------------------------------------- +% This function is called by +% o method_of_moments.m +% ------------------------------------------------------------------------- +% This function calls: +% o get_the_name +% o get_error_message +% o GMM_objective_function +% o SMM_objective_function.m +% o method_of_moments_optimal_weighting_matrix +% ========================================================================= +% Copyright (C) 2020 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/>. +% ------------------------------------------------------------------------- +% Author(s): +% o Willi Mutschler (willi@mutschler.eu) +% o Johannes Pfeifer (jpfeifer@uni-koeln.de) +% ========================================================================= + +% Some dimensions +num_mom = size(oo_.mom.model_moments,1); +dim_params = size(xparam,1); +D = zeros(num_mom,dim_params); +eps_value = options_mom_.mom.se_tolx; + +for i=1:dim_params + %Positive step + xparam_eps_p = xparam; + xparam_eps_p(i,1) = xparam_eps_p(i) + eps_value; + [~, info_p, ~, ~,~, oo__p] = feval(objective_function, xparam_eps_p, Bounds, oo_, estim_params_, M_, options_mom_); + + % Negative step + xparam_eps_m = xparam; + xparam_eps_m(i,1) = xparam_eps_m(i) - eps_value; + [~, info_m, ~, ~,~, oo__m] = feval(objective_function, xparam_eps_m, Bounds, oo_, estim_params_, M_, options_mom_); + + % The Jacobian: + if nnz(info_p)==0 && nnz(info_m)==0 + D(:,i) = (oo__p.mom.model_moments - oo__m.mom.model_moments)/(2*eps_value); + else + problpar = get_the_name(i,options_mom_.TeX, M_, estim_params_, options_mom_); + message_p = get_error_message(info_p, options_mom_); + message_m = get_error_message(info_m, options_mom_); + + warning('method_of_moments:info','Cannot compute the Jacobian for parameter %s - no standard errors available\n %s %s\nCheck your bounds and/or priors, or use a different optimizer.\n',problpar, message_p, message_m) + Asympt_Var = NaN(length(xparam),length(xparam)); + SE_values = NaN(length(xparam),1); + return + end +end + +T = options_mom_.nobs; %Number of observations +if isfield(options_mom_,'variance_correction_factor') + T = T*options_mom_.variance_correction_factor; +end + +WW = oo_.mom.Sw'*oo_.mom.Sw; +if Wopt_flag + % We have the optimal weighting matrix + Asympt_Var = 1/T*((D'*WW*D)\eye(dim_params)); +else + % We do not have the optimal weighting matrix yet + WWopt = method_of_moments_optimal_weighting_matrix(oo_.mom.m_data, oo_.mom.model_moments, options_mom_.mom.bartlett_kernel_lag); + S = WWopt\eye(size(WWopt,1)); + AA = (D'*WW*D)\eye(dim_params); + Asympt_Var = 1/T*AA*D'*WW*S*WW*D*AA; +end + +SE_values = sqrt(diag(Asympt_Var)); \ No newline at end of file diff --git a/matlab/missing/is-row-column-matrix/iscolumn.m b/matlab/missing/is-row-column-matrix/iscolumn.m deleted file mode 100644 index 0264efe198287e63a59ace53cd068b8376ceefda..0000000000000000000000000000000000000000 --- a/matlab/missing/is-row-column-matrix/iscolumn.m +++ /dev/null @@ -1,20 +0,0 @@ -function r = iscolumn(V) -% Copyright (C) 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/>. - -sz = size(V); -r = (length(sz) == 2) && (sz(2) == 1); diff --git a/matlab/missing/is-row-column-matrix/ismatrix.m b/matlab/missing/is-row-column-matrix/ismatrix.m deleted file mode 100644 index e9ed4ef2dbab191420bc7fa851e6aba8744f8de3..0000000000000000000000000000000000000000 --- a/matlab/missing/is-row-column-matrix/ismatrix.m +++ /dev/null @@ -1,19 +0,0 @@ -function r = ismatrix(V) -% Copyright (C) 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/>. - -r = (ndims(V) == 2); diff --git a/matlab/missing/is-row-column-matrix/isrow.m b/matlab/missing/is-row-column-matrix/isrow.m deleted file mode 100644 index ef53534ede0596453d55e1d7fc36ed66499816b4..0000000000000000000000000000000000000000 --- a/matlab/missing/is-row-column-matrix/isrow.m +++ /dev/null @@ -1,20 +0,0 @@ -function r = isrow(V) -% Copyright (C) 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/>. - -sz = size(V); -r = (length(sz) == 2) && (sz(1) == 1); diff --git a/matlab/missing/isdiag/isdiag.m b/matlab/missing/isdiag/isdiag.m deleted file mode 100644 index 89dcec2f16bbb2822bf6490f146865898c8b72c0..0000000000000000000000000000000000000000 --- a/matlab/missing/isdiag/isdiag.m +++ /dev/null @@ -1,62 +0,0 @@ -function b = isdiag(A) % --*-- Unitary tests --*-- - -% Copyright (C) 2014-2017 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/>. - -if isnumeric(A) - if isquare(A) - % Find non zero elements in matrix A... - [ir, ic] = find(A); - % If the non zero elements are on the diagonal, the corresponding elements - % in ir and ic (row and column numbers) should be equal. - b = isequal(ir, ic); - else - error('isdiag: Input must be a square matrix!') - end -else - error('isdiag: Input must be numeric!') -end - -%@test:1 -%$ A = zeros(3,3); -%$ t = isdiag(A); -%$ T = all(t); -%@eof:1 - -%@test:2 -%$ A = zeros(3,3); A(1,3) = 1; -%$ t = ~isdiag(A); -%$ T = all(t); -%@eof:2 - -%@test:3 -%$ A = randn(3,3); -%$ t = ~isdiag(A); -%$ T = all(t); -%@eof:3 - -%@test:4 -%$ A = diag(randn(3,1)); -%$ t = isdiag(A); -%$ T = all(t); -%@eof:4 - -%@test:5 -%$ A = diag(randn(3,1)); A(1,1) = 0; -%$ t = isdiag(A); -%$ T = all(t); -%@eof:5 diff --git a/matlab/partial_information/disclyap_fast.m b/matlab/missing/mex/disclyap_fast/disclyap_fast.m similarity index 75% rename from matlab/partial_information/disclyap_fast.m rename to matlab/missing/mex/disclyap_fast/disclyap_fast.m index 5dfcfe4b5657ec806d4a6953fae971d7b80efb40..359cde1a4adc15777d0e4dccd7bb6f258909a4d7 100644 --- a/matlab/partial_information/disclyap_fast.m +++ b/matlab/missing/mex/disclyap_fast/disclyap_fast.m @@ -1,10 +1,12 @@ -function [X,exitflag]=disclyap_fast(G,V,tol,check_flag) -% function X=disclyap_fast(G,V,ch) +function [X,exitflag]=disclyap_fast(G,V,tol,check_flag,max_iter) +% [X,exitflag]=disclyap_fast(G,V,tol,check_flag) % Inputs: % - G [double] first input matrix % - V [double] second input matrix % - tol [scalar] tolerance criterion -% - check_flag empty of non-empty if non-empty: check positive-definiteness +% - check_flag [boolean] if true: check positive-definiteness +% - max_iter [scalar] maximum number of iterations + % Outputs: % - X [double] solution matrix % - exitflag [scalar] 0 if solution is found, 1 otherwise @@ -19,7 +21,7 @@ function [X,exitflag]=disclyap_fast(G,V,tol,check_flag) % Joe Pearlman and Alejandro Justiniano % 3/5/2005 -% Copyright (C) 2010-2017 Dynare Team +% Copyright (C) 2010-2020 Dynare Team % % This file is part of Dynare. % @@ -36,10 +38,11 @@ function [X,exitflag]=disclyap_fast(G,V,tol,check_flag) % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <http://www.gnu.org/licenses/>. -if nargin <= 3 || isempty( check_flag ) == 1 - flag_ch = 0; -else - flag_ch = 1; +if nargin <= 3 || isempty(check_flag) + check_flag = 0; +end +if nargin <=4 + max_iter=2000; end exitflag=0; @@ -48,7 +51,7 @@ A0=G; matd=1; iter=1; -while matd > tol && iter< 2000 +while matd > tol && iter< max_iter P1=P0+A0*P0*A0'; A1=A0*A0; matd=max( max( abs( P1 - P0 ) ) ); @@ -56,20 +59,18 @@ while matd > tol && iter< 2000 A0=A1; iter=iter+1; end -if iter==5000 - X=NaN(P0); +if iter==max_iter + X=NaN(size(P0)); exitflag=1; return end -clear A0 A1 P1; X=(P0+P0')/2; % Check that X is positive definite -if flag_ch==1 - [C,p]=chol(X); +if check_flag==1 + [~,p]=chol(X); if p ~= 0 exitflag=1; - error('X is not positive definite') end end \ No newline at end of file diff --git a/matlab/missing/stats/wblinv.m b/matlab/missing/stats/wblinv.m index 105a6cf7e8073e7939bdf53f9685f0019eae62a0..af537a7c380df89f8b7db1cf7b2bf93e21b7539a 100644 --- a/matlab/missing/stats/wblinv.m +++ b/matlab/missing/stats/wblinv.m @@ -10,7 +10,7 @@ function t = wblinv(proba, scale, shape) % --*-- Unitary tests --*-- % OUTPUTS % - t [double] scalar such that P(X<=t)=proba -% Copyright (C) 2015-2017 Dynare Team +% Copyright (C) 2015-2020 Dynare Team % % This file is part of Dynare. % @@ -147,7 +147,7 @@ t = exp(log(scale)+log(-log(1-proba))/shape); %$ if debug %$ [shape, scale, x(k-1)] %$ end -%$ if isoctave || matlab_ver_less_than('7.14') +%$ if isoctave %$ s = quadv(density, 0, x(k-1),1e-10); %$ else %$ s = integral(density, 0, x(k-1)); @@ -155,7 +155,7 @@ t = exp(log(scale)+log(-log(1-proba))/shape); %$ if debug %$ [s, abs(p-s)] %$ end -%$ if isoctave || matlab_ver_less_than('7.14') +%$ if isoctave %$ t(k) = abs(p-s)<1e-9; %$ else %$ t(k) = abs(p-s)<1e-12; diff --git a/matlab/missing/strjoin/strjoin.m b/matlab/missing/strjoin/strjoin.m deleted file mode 100644 index 47f21287fcd1755d9e2807008f674330510e0e62..0000000000000000000000000000000000000000 --- a/matlab/missing/strjoin/strjoin.m +++ /dev/null @@ -1,62 +0,0 @@ -function rval = strjoin (cstr, delimiter) - -% Adapted from Octave's implementation of strjoin -% -% Limitation: escaped characters (e.g. '\n') in delimiters will not be -% interpreted as the characters they represent. - -% Copyright (C) 2013-2019 Ben Abbott -% Copyright (C) 2007 Muthiah Annamalai -% 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/>. - -if nargin == 1 - delimiter = ' '; -elseif nargin < 1 || nargin > 2 - error('strjoin: must have either one or two arguments') -end -if ~(iscellstr(cstr) && (ischar(delimiter) || iscellstr(delimiter))) - error('strjoin: first argument must be a cell array, second array either a char array or a cell array') -end - -if numel(cstr) == 1 - rval = cstr{1}; - return; -end - -if ischar(delimiter) - % There is no equivalent to do_string_escapes in MATLAB - %delimiter = do_string_escapes(delimiter); - delimiter = {delimiter}; -end - -num = numel(cstr); -if numel(delimiter) == 1 && num > 1 - delimiter = repmat(delimiter, 1, num); - delimiter(end) = {''}; -elseif num > 0 && numel(delimiter) ~= num - 1 - error('strjoin: the number of delimiters does not match the number of strings'); -else - delimiter(end+1) = {''}; -end - -if num == 0 - rval = ''; -else - tmp = [cstr(:).'; delimiter(:).']; - rval = [tmp{:}]; -end diff --git a/matlab/missing/strsplit/private/ischarnum.m b/matlab/missing/strsplit/private/ischarnum.m deleted file mode 100644 index 57b7c7e8cd308c6e400b76f49b88b61fad83dad3..0000000000000000000000000000000000000000 --- a/matlab/missing/strsplit/private/ischarnum.m +++ /dev/null @@ -1,35 +0,0 @@ -function l = ischarnum(x) - -% Returns true if and only if char x represents a real number. - -% Copyright © 2018 DynareTeam -% -% 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/>. - -l = false; - -s = warning; -warning off; - -number = str2double(x); - -warning(s); - -if ~isempty(number) - if isreal(number) - l = true; - end -end \ No newline at end of file diff --git a/matlab/missing/strsplit/strsplit.m b/matlab/missing/strsplit/strsplit.m deleted file mode 100644 index 926bb50f389c6a7bb95818f3005856cab2e853fb..0000000000000000000000000000000000000000 --- a/matlab/missing/strsplit/strsplit.m +++ /dev/null @@ -1,90 +0,0 @@ -function tok = strsplit(str, delimiters) - -% Splits a string into multiple terms. -% -% INPUTS -% - str [char] String to be splitted. -% - delimiters [char, cell(char)] Delimiters. -% -% OUTPUTS -% - tok [cell(char)] Terms. - -% Copyright © 2018 DynareTeam -% -% 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/>. - -remove_empty = true; -remove_numbers = false; - -% Check first input arguments -assert(ischar(str) && ndims(str)==2 && size(str,1)<=1, 'The first arugment has to be a row char array!'); - -% Set default value for second input arguments -if nargin<2 - delimiters = {' '}; -end - -% If second input argument is a char transform it into a sigleton cell of char -if nargin>1 - if ischar(delimiters) - assert(ndims(delimiters)==2 && size(delimiters,1)==1, 'The second input argument has to be be a char string!'); - delimiters = {delimiters}; - end -end - -% Check that `delimiters` is a one dimensional cell -assert(ndim(delimiters)<=1, 'The second input argument has to be a one dimensional cell array!') - -% Check that `delimiters` is a cell of row char arrays -assert(all(cellfun(@ischar, delimiters)) && all(cellfun(@rows, delimiters)==1), 'The second input argument has to be a cell of row char arrays!') - -% If space is one of the delimiters obtain the index in `delimiters` -idspace = strmatch(' ', delimiters); - -% Get the number of delimiters -n = length(delimiters); - -% Remove unnecessary spaces -delimiters(setdiff(1:n, idspace)) = strtrim(delimiters(setdiff(1:n, idspace))); - -% Join all the delimiters (strjoin is not available with matlab version less than R2013a) -if n>1 - delimiter = ''; - for i=1:n - if isspace(delimiters{i}) - delimiter = horzcat(delimiter, '\s'); - else - delimiter = horzcat(delimiter, delimiters{i}); - end - delimiter = horzcat(delimiter,'|'); - end - delimiter = horzcat(delimiter, '\W'); -else - delimiter = delimiters{1}; -end - -% Get tokens -tok = regexp(str, delimiter, 'split'); - -if remove_empty - % Remove empty tokens - tok = tok(find(~cellfun(@isempty, tok))); -end - -if remove_numbers - % Remove numbers - tok = tok(find(~cellfun(@ischarnum, tok))); -end \ No newline at end of file diff --git a/matlab/mode_check.m b/matlab/mode_check.m index 5a9970981dbb9130ae8a1cef97dc763058804334..ada266663ab826cee03b1923e9517abf65f78d62 100644 --- a/matlab/mode_check.m +++ b/matlab/mode_check.m @@ -104,15 +104,6 @@ for plt = 1:nbplt subplot(nr,nc,k) kk = (plt-1)*nstar+k; [name,texname] = get_the_name(kk,TeX,Model,EstimatedParameters,DynareOptions); - if TeX - if isempty(NAMES) - NAMES = name; - TeXNAMES = texname; - else - NAMES = char(NAMES,name); - TeXNAMES = char(TeXNAMES,texname); - end - end xx = x; if x(kk)~=0 || ~isinf(BoundsInfo.lb(kk)) || ~isinf(BoundsInfo.lb(kk)) l1 = max(BoundsInfo.lb(kk),(1-sign(x(kk))*ll)*x(kk)); m1 = 0; %lower bound @@ -182,7 +173,12 @@ for plt = 1:nbplt zNaN = z(NaN_index); yNaN = yl(1)*ones(size(NaN_index)); plot(zNaN,yNaN,'o','MarkerEdgeColor','r','MarkerFaceColor','r','MarkerSize',6); - title(name,'interpreter','none') + if TeX + title(texname,'interpreter','latex') + else + title(name,'interpreter','none') + end + axis tight if binding_lower_bound || binding_upper_bound xl=get(gca,'xlim'); @@ -209,9 +205,6 @@ for plt = 1:nbplt if TeX && any(strcmp('eps',cellstr(DynareOptions.graph_format))) % TeX eps loader file fprintf(fidTeX,'\\begin{figure}[H]\n'); - for jj = 1:min(nstar,length(x)-(plt-1)*nstar) - fprintf(fidTeX,'\\psfrag{%s}[1][][0.5][0]{%s}\n',deblank(NAMES(jj,:)),deblank(TeXNAMES(jj,:))); - end fprintf(fidTeX,'\\centering \n'); fprintf(fidTeX,'\\includegraphics[width=%2.2f\\textwidth]{%s_CheckPlots%s}\n',DynareOptions.figures.textwidth*min(k/nc,1),[Model.fname, '/graphs/',Model.fname],int2str(plt)); fprintf(fidTeX,'\\caption{Check plots.}'); diff --git a/matlab/model_diagnostics.m b/matlab/model_diagnostics.m index 636713047b9f45f30c708056ef1350b0ee5715c6..2bb2b1990449a96db91df276592b983054c19ed5 100644 --- a/matlab/model_diagnostics.m +++ b/matlab/model_diagnostics.m @@ -221,11 +221,6 @@ if ~options.block exo_simul, ... M.params, dr.ys, it_); end - if options.use_dll - % In USE_DLL mode, the hessian is in the 3-column sparse representation - hessian1 = sparse(hessian1(:,1), hessian1(:,2), hessian1(:,3), ... - size(jacobia_, 1), size(jacobia_, 2)*size(jacobia_, 2)); - end end if any(any(isinf(jacobia_) | isnan(jacobia_))) diff --git a/matlab/modules/reporting b/matlab/modules/reporting index a25d3abdb6312d619352a7b276c70ec98b3d5f00..13c98b256d8d42f62692aa9d48d379e897f2ca5e 160000 --- a/matlab/modules/reporting +++ b/matlab/modules/reporting @@ -1 +1 @@ -Subproject commit a25d3abdb6312d619352a7b276c70ec98b3d5f00 +Subproject commit 13c98b256d8d42f62692aa9d48d379e897f2ca5e diff --git a/matlab/name2index.m b/matlab/name2index.m index 7645c11a29992d3ad6312ad642c0ba8cfa4afff4..3f75b5c8a0965538bf71189fa5bd1323efcf1269 100644 --- a/matlab/name2index.m +++ b/matlab/name2index.m @@ -87,7 +87,7 @@ end if strcmpi(type,'MeasurementError') if nargin<6% Covariance matrix diagonal term - i = nvx + strmatch(name1, M_.endo_names{estim_params_.var_endo(:,1)}, 'exact'); + i = nvx + strmatch(name1, M_.endo_names(estim_params_.var_endo(:,1)), 'exact'); if isempty(i) disp(['The standard deviation of the measurement error on ' name1 ' is not an estimated parameter!']) return @@ -95,7 +95,7 @@ if strcmpi(type,'MeasurementError') else% Covariance matrix off-diagonal term offset = nvx+nvn+ncx; try - list_of_measurement_errors = { M_.endo_names{estim_params_.corrn(:,1)} , M_.endo_names{estim_params_.corrn(:,2)} }; + list_of_measurement_errors = [M_.endo_names(estim_params_.corrn(:,1),1) , M_.endo_names(estim_params_.corrn(:,2),1)]; k1 = strmatch(name1,list_of_measurement_errors(:,1),'exact'); k2 = strmatch(name2,list_of_measurement_errors(:,2),'exact'); i = offset+intersect(k1,k2); diff --git a/matlab/non_linear_dsge_likelihood.m b/matlab/non_linear_dsge_likelihood.m index 42d563a9bc58adc3a0c4717c7bad0adbc812cb89..2af54fac8eaf9b558b37cd55fa72eb5e0a756770 100644 --- a/matlab/non_linear_dsge_likelihood.m +++ b/matlab/non_linear_dsge_likelihood.m @@ -63,75 +63,11 @@ end % 1. Get the structural parameters & define penalties %------------------------------------------------------------------------------ -% Return, with endogenous penalty, if some parameters are smaller than the lower bound of the prior domain. -if isestimation(DynareOptions) && (DynareOptions.mode_compute~=1) && any(xparam1<BoundsInfo.lb) - k = find(xparam1(:) < BoundsInfo.lb); - fval = Inf; - exit_flag = 0; - info(1) = 41; - info(4) = sum((BoundsInfo.lb(k)-xparam1(k)).^2); - return -end - -% Return, with endogenous penalty, if some parameters are greater than the upper bound of the prior domain. -if isestimation(DynareOptions) && (DynareOptions.mode_compute~=1) && any(xparam1>BoundsInfo.ub) - k = find(xparam1(:)>BoundsInfo.ub); - fval = Inf; - exit_flag = 0; - info(1) = 42; - info(4) = sum((xparam1(k)-BoundsInfo.ub(k)).^2); - return -end - Model = set_all_parameters(xparam1,EstimatedParameters,Model); -Q = Model.Sigma_e; -H = Model.H; - -if ~issquare(Q) || EstimatedParameters.ncx || isfield(EstimatedParameters,'calibrated_covariances') - [Q_is_positive_definite, penalty] = ispd(Q(EstimatedParameters.Sigma_e_entries_to_check_for_positive_definiteness,EstimatedParameters.Sigma_e_entries_to_check_for_positive_definiteness)); - if ~Q_is_positive_definite - fval = Inf; - exit_flag = 0; - info(1) = 43; - info(4) = penalty; - return - end - if isfield(EstimatedParameters,'calibrated_covariances') - correct_flag=check_consistency_covariances(Q); - if ~correct_flag - penalty = sum(Q(EstimatedParameters.calibrated_covariances.position).^2); - fval = Inf; - exit_flag = 0; - info(1) = 71; - info(4) = penalty; - return - end - end - -end - -if ~issquare(H) || EstimatedParameters.ncn || isfield(EstimatedParameters,'calibrated_covariances_ME') - [H_is_positive_definite, penalty] = ispd(H(EstimatedParameters.H_entries_to_check_for_positive_definiteness,EstimatedParameters.H_entries_to_check_for_positive_definiteness)); - if ~H_is_positive_definite - fval = Inf; - exit_flag = 0; - info(1) = 44; - info(4) = penalty; - return - end - if isfield(EstimatedParameters,'calibrated_covariances_ME') - correct_flag=check_consistency_covariances(H); - if ~correct_flag - penalty = sum(H(EstimatedParameters.calibrated_covariances_ME.position).^2); - fval = Inf; - exit_flag = 0; - info(1) = 72; - info(4) = penalty; - return - end - end - +[fval,info,exit_flag,Q,H]=check_bounds_and_definiteness_estimation(xparam1, Model, EstimatedParameters, BoundsInfo); +if info(1) + return end %------------------------------------------------------------------------------ diff --git a/matlab/optimization/dynare_minimize_objective.m b/matlab/optimization/dynare_minimize_objective.m index 13d1eae6e0d5c80b2699ef5da3eb819e7e192d22..475392f593fbd46a1a0800af847ebd056544f263 100644 --- a/matlab/optimization/dynare_minimize_objective.m +++ b/matlab/optimization/dynare_minimize_objective.m @@ -51,7 +51,7 @@ if isempty(bounds) end if isempty(parameter_names) - parameter_names=[repmat('parameter ',n_params,1),num2str((1:n_params)')]; + parameter_names=cellstr([repmat('parameter ',n_params,1),num2str((1:n_params)')]); end %% initialize function outputs @@ -222,6 +222,9 @@ switch minimizer_algorithm csminwel1(objective_function, start_par_value, H0, analytic_grad, crit, nit, numgrad, epsilon, Verbose, Save_files, varargin{:}); hessian_mat=inv(inverse_hessian_mat); case 5 + if isempty(prior_information) %mr_hessian requires it, but can be NaN + prior_information.p2=NaN(n_params,1); + end if options_.analytic_derivation==-1 %set outside as code for use of analytic derivation analytic_grad=1; crit = options_.newrat.tolerance.f_analytic; @@ -265,9 +268,11 @@ switch minimizer_algorithm hess_info.gstep=options_.gstep; hess_info.htol = 1.e-4; hess_info.h1=options_.gradient_epsilon*ones(n_params,1); - [opt_par_values,hessian_mat,gg,fval,invhess,new_rat_hess_info] = newrat(objective_function,start_par_value,bounds,analytic_grad,crit,nit,0,Verbose, Save_files,hess_info,varargin{:}); - %hessian_mat is the plain outer product gradient Hessian + [opt_par_values,hessian_mat,gg,fval,invhess,new_rat_hess_info] = newrat(objective_function,start_par_value,bounds,analytic_grad,crit,nit,0,Verbose,Save_files,hess_info,prior_information.p2,options_.gradient_epsilon,parameter_names,varargin{:}); %hessian_mat is the plain outer product gradient Hessian case 6 + if isempty(prior_information) %Inf will be reset + prior_information.p2=Inf(n_params,1); + end [opt_par_values, hessian_mat, Scale, fval] = gmhmaxlik(objective_function, start_par_value, ... Initial_Hessian, options_.mh_jscale, bounds, prior_information.p2, options_.gmhmaxlik, options_.optim_opt, varargin{:}); case 7 @@ -423,7 +428,7 @@ switch minimizer_algorithm case 12 if isoctave error('Option mode_compute=12 is not available under Octave') - elseif ~user_has_matlab_license('global_optimization_toolbox') + elseif ~user_has_matlab_license('GADS_Toolbox') error('Option mode_compute=12 requires the Global Optimization Toolbox') end [LB, UB] = set_bounds_to_finite_values(bounds, options_.huge_number); @@ -518,6 +523,21 @@ switch minimizer_algorithm end func = @(x)objective_function(x,varargin{:}); [opt_par_values,fval,exitflag,output] = simulannealbnd(func,start_par_value,bounds(:,1),bounds(:,2),optim_options); + case 13 + % Matlab's lsqnonlin (Optimization toolbox needed). + if isoctave && ~user_has_octave_forge_package('optim') + error('Option mode_compute=13 requires the optim package') + elseif ~isoctave && ~user_has_matlab_license('optimization_toolbox') + error('Option mode_compute=13 requires the Optimization Toolbox') + end + optim_options = optimset('display','iter','MaxFunEvals',5000,'MaxIter',5000,'TolFun',1e-6,'TolX',1e-6); + if ~isempty(options_.optim_opt) + eval(['optim_options = optimset(optim_options,' options_.optim_opt ');']); + end + if options_.silent_optimizer + optim_options = optimset(optim_options,'display','off'); + end + [opt_par_values,Resnorm,fval,exitflag,OUTPUT,LAMBDA,JACOB] = lsqnonlin(objective_function,start_par_value,bounds(:,1),bounds(:,2),optim_options,varargin{:}); otherwise if ischar(minimizer_algorithm) if exist(minimizer_algorithm) diff --git a/matlab/optimization/mr_gstep.m b/matlab/optimization/mr_gstep.m index 0836a7a87efb99a604844ab967a06000466d1e82..0486014845b3844cf4bdbb17242c87bbec8cf809 100644 --- a/matlab/optimization/mr_gstep.m +++ b/matlab/optimization/mr_gstep.m @@ -1,5 +1,5 @@ -function [f0, x, ig] = mr_gstep(h1,x,bounds,func0,penalty,htol0,Verbose,Save_files,varargin) -% [f0, x, ig] = mr_gstep(h1,x,bounds,func0,penalty,htol0,Verbose,Save_files,varargin) +function [f0, x, ig] = mr_gstep(h1,x,bounds,func0,penalty,htol0,Verbose,Save_files,gradient_epsilon,parameter_names,varargin) +% [f0, x, ig] = mr_gstep(h1,x,bounds,func0,penalty,htol0,Verbose,Save_files,gradient_epsilon,parameter_names,varargin) % % Gibbs type step in optimisation % @@ -11,7 +11,7 @@ function [f0, x, ig] = mr_gstep(h1,x,bounds,func0,penalty,htol0,Verbose,Save_fil % varargin{6} --> BayesInfo % varargin{1} --> DynareResults -% Copyright (C) 2006-2017 Dynare Team +% Copyright (C) 2006-2020 Dynare Team % % This file is part of Dynare. % @@ -30,7 +30,7 @@ function [f0, x, ig] = mr_gstep(h1,x,bounds,func0,penalty,htol0,Verbose,Save_fil n=size(x,1); if isempty(h1) - h1=varargin{3}.gradient_epsilon*ones(n,1); + h1=gradient_epsilon*ones(n,1); end @@ -72,10 +72,10 @@ while i<n gg(i)=(f1(i)'-f_1(i)')./(2.*h1(i)); hh(i) = 1/max(1.e-9,abs( (f1(i)+f_1(i)-2*f0)./(h1(i)*h1(i)) )); if gg(i)*(hh(i)*gg(i))/2 > htol(i) - [f0, x, fc, retcode] = csminit1(func0,x,penalty,f0,gg,0,diag(hh),Verbose,varargin{:}); + [f0, x, ~, ~] = csminit1(func0,x,penalty,f0,gg,0,diag(hh),Verbose,varargin{:}); ig(i)=1; if Verbose - fprintf(['Done for param %s = %8.4f\n'],varargin{6}.name{i},x(i)) + fprintf(['Done for param %s = %8.4f\n'],parameter_names{i},x(i)) end end xh1=x; diff --git a/matlab/optimization/mr_hessian.m b/matlab/optimization/mr_hessian.m index ec8ac1e900781028153a65e5c8a5594208df96e7..d1ab01e7ba68f8fee6c8c8008f8abefddd93b197 100644 --- a/matlab/optimization/mr_hessian.m +++ b/matlab/optimization/mr_hessian.m @@ -1,5 +1,5 @@ -function [hessian_mat, gg, htol1, ihh, hh_mat0, hh1, hess_info] = mr_hessian(x,func,penalty,hflag,htol0,hess_info,varargin) -% function [hessian_mat, gg, htol1, ihh, hh_mat0, hh1, hess_info] = mr_hessian(x,func,penalty,hflag,htol0,hess_info,varargin) +function [hessian_mat, gg, htol1, ihh, hh_mat0, hh1, hess_info] = mr_hessian(x,func,penalty,hflag,htol0,hess_info,bounds,prior_std,varargin) +% function [hessian_mat, gg, htol1, ihh, hh_mat0, hh1, hess_info] = mr_hessian(x,func,penalty,hflag,htol0,hess_info,bounds,prior_std,varargin) % numerical gradient and Hessian, with 'automatic' check of numerical % error % @@ -24,7 +24,10 @@ function [hessian_mat, gg, htol1, ihh, hh_mat0, hh1, hess_info] = mr_hessian(x,f % derivatives % - hess_info structure storing the step sizes for % computation of Hessian -% - varargin other inputs: +% - bounds prior bounds of parameters +% - prior_std prior standard devation of parameters (can be NaN) +% - varargin other inputs +% e.g. in dsge_likelihood % varargin{1} --> DynareDataset % varargin{2} --> DatasetInfo % varargin{3} --> DynareOptions @@ -64,9 +67,9 @@ function [hessian_mat, gg, htol1, ihh, hh_mat0, hh1, hess_info] = mr_hessian(x,f n=size(x,1); [f0,exit_flag, ff0]=penalty_objective_function(x,func,penalty,varargin{:}); -h2=varargin{7}.ub-varargin{7}.lb; -hmax=varargin{7}.ub-x; -hmax=min(hmax,x-varargin{7}.lb); +h2=bounds(:,2)-bounds(:,1); +hmax=bounds(:,2)-x; +hmax=min(hmax,x-bounds(:,1)); if isempty(ff0) outer_product_gradient=0; else @@ -240,7 +243,7 @@ if outer_product_gradient sd=sqrt(diag(ihh)); %standard errors sdh=sqrt(1./diag(hh)); %diagonal standard errors for j=1:length(sd) - sd0(j,1)=min(varargin{6}.p2(j), sd(j)); %prior std + sd0(j,1)=min(prior_std(j), sd(j)); %prior std sd0(j,1)=10^(0.5*(log10(sd0(j,1))+log10(sdh(j,1)))); end ihh=ihh./(sd*sd').*(sd0*sd0'); %inverse outer product with modified std's diff --git a/matlab/optimization/newrat.m b/matlab/optimization/newrat.m index 9f14d9f1c7b0322e0a0a7a2d0334e1b64f597c0c..e94d565540aef541026fa7203ed5df127c5f68fb 100644 --- a/matlab/optimization/newrat.m +++ b/matlab/optimization/newrat.m @@ -1,5 +1,5 @@ -function [xparam1, hh, gg, fval, igg, hess_info] = newrat(func0, x, bounds, analytic_derivation, ftol0, nit, flagg, Verbose, Save_files, hess_info, varargin) -% [xparam1, hh, gg, fval, igg, hess_info] = newrat(func0, x, bounds, analytic_derivation, ftol0, nit, flagg, Verbose, Save_files, hess_info, varargin) +function [xparam1, hh, gg, fval, igg, hess_info] = newrat(func0, x, bounds, analytic_derivation, ftol0, nit, flagg, Verbose, Save_files, hess_info, prior_std, gradient_epsilon, parameter_names, varargin) +% [xparam1, hh, gg, fval, igg, hess_info] = newrat(func0, x, bounds, analytic_derivation, ftol0, nit, flagg, Verbose, Save_files, hess_info, gradient_epsilon, parameter_names, varargin) % % Optimiser with outer product gradient and with sequences of univariate steps % uses Chris Sims subroutine for line search @@ -8,6 +8,7 @@ function [xparam1, hh, gg, fval, igg, hess_info] = newrat(func0, x, bounds, anal % - func0 name of the function that also outputs the single contributions at times t=1,...,T % of the log-likelihood to compute outer product gradient % - x starting guess +% - bounds prior bounds of parameters % - analytic_derivation 1 if analytic derivatives, 0 otherwise % - ftol0 termination criterion for function change % - nit maximum number of iterations @@ -21,7 +22,12 @@ function [xparam1, hh, gg, fval, igg, hess_info] = newrat(func0, x, bounds, anal % - Save_files 1 if intermediate output is to be saved % - hess_info structure storing the step sizes for % computation of Hessian -% - varargin other inputs: +% - prior_std prior standard devation of parameters (can be NaN); +% passed to mr_hessian +% - gradient_epsilon [double] step size in gradient +% - parameter_names [cell] names of parameters for error messages +% - varargin other inputs +% e.g. in dsge_likelihood and others: % varargin{1} --> DynareDataset % varargin{2} --> DatasetInfo % varargin{3} --> DynareOptions @@ -69,7 +75,6 @@ ftol=ftol0; gtol=1.e-3; htol=htol_base; htol0=htol_base; -gibbstol=length(varargin{6}.pshape)/50; %25; % force fcn, grad to function handle if ischar(func0) @@ -85,7 +90,7 @@ fval=fval0; outer_product_gradient=1; if isempty(hh) - [dum, gg, htol0, igg, hhg, h1, hess_info]=mr_hessian(x,func0,penalty,flagit,htol,hess_info,varargin{:}); + [dum, gg, htol0, igg, hhg, h1, hess_info]=mr_hessian(x,func0,penalty,flagit,htol,hess_info,bounds,prior_std,varargin{:}); if isempty(dum) outer_product_gradient=0; igg = 1e-4*eye(nx); @@ -164,7 +169,7 @@ while norm(gg)>gtol && check==0 && jit<nit [fvala,x0,fc,retcode] = csminit1(func0,x0,penalty,fval,ggx,0,iggx,Verbose,varargin{:}); end x0 = check_bounds(x0,bounds); - [fvala, x0, ig] = mr_gstep(h1,x0,bounds,func0,penalty,htol0,Verbose,Save_files,varargin{:}); + [fvala, x0, ig] = mr_gstep(h1,x0,bounds,func0,penalty,htol0,Verbose,Save_files,gradient_epsilon, parameter_names,varargin{:}); x0 = check_bounds(x0,bounds); nig=[nig ig]; disp_verbose('Sequence of univariate steps!!',Verbose) @@ -203,7 +208,7 @@ while norm(gg)>gtol && check==0 && jit<nit if flagit==2 hh=hh0; elseif flagg>0 - [dum, gg, htol0, igg, hhg, h1, hess_info]=mr_hessian(xparam1,func0,penalty,flagg,ftol0,hess_info,varargin{:}); + [dum, gg, htol0, igg, hhg, h1, hess_info]=mr_hessian(xparam1,func0,penalty,flagg,ftol0,hess_info,bounds,prior_std,varargin{:}); if flagg==2 hh = reshape(dum,nx,nx); ee=eig(hh); @@ -243,7 +248,7 @@ while norm(gg)>gtol && check==0 && jit<nit save('m1.mat','x','fval0','nig') end end - [dum, gg, htol0, igg, hhg, h1, hess_info]=mr_hessian(xparam1,func0,penalty,flagit,htol,hess_info,varargin{:}); + [dum, gg, htol0, igg, hhg, h1, hess_info]=mr_hessian(xparam1,func0,penalty,flagit,htol,hess_info,bounds,prior_std,varargin{:}); if isempty(dum) outer_product_gradient=0; end diff --git a/matlab/parallel/AnalyseComputationalEnvironment.m b/matlab/parallel/AnalyseComputationalEnvironment.m index 08bf0d2ed117dbf13b47f6b86d65a05cb313c495..e680a46b76933851aab0180b936dc515c1cc7512 100644 --- a/matlab/parallel/AnalyseComputationalEnvironment.m +++ b/matlab/parallel/AnalyseComputationalEnvironment.m @@ -522,13 +522,16 @@ for Node=1:length(DataInput) % To obtain a recoursive function remove the 'for' if (DataInput(Node).Local == 1) if Environment if ~ismac - [si0, de0] = system('nproc'); + command_string = 'nproc'; + [si0, de0] = system(command_string); else - [si0, de0] = system('sysctl -n hw.ncpu'); + command_string = 'sysctl -n hw.ncpu'; + [si0, de0] = system(command_string); Environment1 = 2; end else - [si0, de0] = system(['psinfo \\']); + command_string = ['psinfo \\']; + [si0, de0] = system(command_string); end else if Environment diff --git a/matlab/partial_information/PCL_resol.m b/matlab/partial_information/PCL_resol.m index 9ddda8c13c33c635dcf910c6d5cb8df6620f01d9..52e418e2ed5d10b02747af8ff3fdbf81fa38e9cb 100644 --- a/matlab/partial_information/PCL_resol.m +++ b/matlab/partial_information/PCL_resol.m @@ -88,7 +88,7 @@ else if options_.linear == 0 % nonlinear models if max(abs(feval(fh,dr.ys,[oo_.exo_steady_state; ... - oo_.exo_det_steady_state], M_.params))) > options_.dynatol.f + oo_.exo_det_steady_state], M_.params))) > options_.solve_tolf opt = options_; opt.jacobian_flag = false; [dr.ys,check1] = dynare_solve(fh,dr.ys,opt,... diff --git a/matlab/perfect-foresight-models/make_ex_.m b/matlab/perfect-foresight-models/make_ex_.m index 6bbb9659762e544e0c637609b67ea928f43dd020..e445989523b597229e7b67bf5577413e52b2a3f0 100644 --- a/matlab/perfect-foresight-models/make_ex_.m +++ b/matlab/perfect-foresight-models/make_ex_.m @@ -1,5 +1,6 @@ -function oo_=make_ex_(M_,options_,oo_) -% forms oo_.exo_simul and oo_.exo_det_simul +function oo_ = make_ex_(M_, options_, oo_) + +% Forms oo_.exo_simul and oo_.exo_det_simul % % INPUTS % M_: Dynare model structure @@ -8,13 +9,8 @@ function oo_=make_ex_(M_,options_,oo_) % % OUTPUTS % oo_: Dynare results structure -% -% ALGORITHM -% -% SPECIAL REQUIREMENTS -% -% Copyright (C) 1996-2017 Dynare Team +% Copyright (C) 1996-2020 Dynare Team % % This file is part of Dynare. % @@ -41,19 +37,33 @@ if M_.exo_det_nbr > 1 && isempty(oo_.exo_det_steady_state) end % Initialize oo_.exo_simul -if isempty(M_.exo_histval) - if options_.initval_file - % exo_simul provided by initval file - elseif isempty(ex0_) - oo_.exo_simul = repmat(oo_.exo_steady_state',M_.maximum_lag+options_.periods+M_.maximum_lead,1); +if isempty(oo_.initval_series) + if isempty(M_.exo_histval) + if isempty(ex0_) + oo_.exo_simul = repmat(oo_.exo_steady_state',M_.maximum_lag+options_.periods+M_.maximum_lead,1); + else + oo_.exo_simul = [ repmat(ex0_',M_.maximum_lag,1) ; repmat(oo_.exo_steady_state',options_.periods+M_.maximum_lead,1) ]; + end else - oo_.exo_simul = [ repmat(ex0_',M_.maximum_lag,1) ; repmat(oo_.exo_steady_state',options_.periods+M_.maximum_lead,1) ]; + if isempty(ex0_) + oo_.exo_simul = [M_.exo_histval'; repmat(oo_.exo_steady_state',options_.periods+M_.maximum_lead,1)]; + else + error('histval and endval cannot be used simultaneously') + end end -else - if isempty(ex0_) - oo_.exo_simul = [M_.exo_histval'; repmat(oo_.exo_steady_state',options_.periods+M_.maximum_lead,1)]; - else - error('histval and endval cannot be used simultaneously') +elseif M_.exo_nbr > 0 + x = oo_.initval_series{M_.exo_names{:}}.data; + oo_.exo_simul = x(1:M_.maximum_lag + options_.periods + M_.maximum_lead,:); + if ~isempty(M_.exo_histval) + oo_.exo_simul(1:M_.maximum_lag, :) ... + = M_.exo_histval(:, 1:M_.maximum_lag)'; + end +elseif M_.exo_det_nbr > 0 + x_det = oo_.initval_series{M_.exo_det_names{:}}.data; + oo_.exo_det_simul = x_det(1:M_.maximum_lag + options_.periods + M_.maximum_lead,:); + if ~isempty(M_.exo_det_histval) + oo_.exo_det_simul(1:M_.maximum_lag, :) ... + = M_.exo_det_histval(:, 1:M_.maximum_lag)'; end end diff --git a/matlab/perfect-foresight-models/make_y_.m b/matlab/perfect-foresight-models/make_y_.m index abd48a84f3df6a2fbd81d629bb429ef54d0a5609..d36dd746ca5f8a8423364acd5cdd438a47929bdd 100644 --- a/matlab/perfect-foresight-models/make_y_.m +++ b/matlab/perfect-foresight-models/make_y_.m @@ -45,18 +45,31 @@ if isempty(oo_.steady_state) oo_.steady_state = zeros(M_.endo_nbr,1); end -if isempty(M_.endo_histval) - if isempty(ys0_) - oo_.endo_simul = repmat(oo_.steady_state, 1, M_.maximum_lag+options_.periods+M_.maximum_lead); +if isempty(oo_.initval_series) + if isempty(M_.endo_histval) + if isempty(ys0_) + oo_.endo_simul = repmat(oo_.steady_state, 1, M_.maximum_lag+options_.periods+M_.maximum_lead); + else + oo_.endo_simul = [repmat(ys0_, 1, M_.maximum_lag) repmat(oo_.steady_state, 1,options_.periods+M_.maximum_lead)]; + end else - oo_.endo_simul = [repmat(ys0_, 1, M_.maximum_lag) repmat(oo_.steady_state, 1,options_.periods+M_.maximum_lead)]; + if ~isempty(ys0_) + error('histval and endval cannot be used simultaneously') + end + % the first NaNs take care of the case where there are lags > 1 on + % exogenous variables + oo_.endo_simul = [M_.endo_histval ... + repmat(oo_.steady_state, 1, options_.periods+M_.maximum_lead)]; end else - if ~isempty(ys0_) - error('histval and endval cannot be used simultaneously') + y = oo_.initval_series{M_.endo_names{:}}.data; + oo_.endo_simul = y(1:M_.maximum_lag + options_.periods + ... + M_.maximum_lead, :)'; + if ~isempty(M_.endo_histval) + if ~isempty(ys0_) + error('histval and endval cannot be used simultaneously') + end + oo_.endo_simul(:,1:M_.maximum_lag) ... + = M_.endo_histval(:, 1:M_.maximum_lag); end - % the first NaNs take care of the case where there are lags > 1 on - % exogenous variables - oo_.endo_simul = [M_.endo_histval ... - repmat(oo_.steady_state, 1, options_.periods+M_.maximum_lead)]; -end +end \ No newline at end of file diff --git a/matlab/perfect-foresight-models/perfect_foresight_setup.m b/matlab/perfect-foresight-models/perfect_foresight_setup.m index 8f59e4e9bafdbc7d9d9d6f3991a01af53ae6bade..9a8877e872cc79260e1ec114b3c20f81ed9840b8 100644 --- a/matlab/perfect-foresight-models/perfect_foresight_setup.m +++ b/matlab/perfect-foresight-models/perfect_foresight_setup.m @@ -65,7 +65,4 @@ if ~isempty(M_.det_shocks) && options_.periods<max([M_.det_shocks.periods]) end oo_ = make_ex_(M_,options_,oo_); - -if ~options_.initval_file - oo_ = make_y_(M_,options_,oo_); -end +oo_ = make_y_(M_,options_,oo_); \ No newline at end of file diff --git a/matlab/perfect-foresight-models/perfect_foresight_solver_core.m b/matlab/perfect-foresight-models/perfect_foresight_solver_core.m index 267545d69247cef70c66adaee222edf8068040ad..ff7616d32c3d572e2c5c31fd3c66e707c6af9ff8 100644 --- a/matlab/perfect-foresight-models/perfect_foresight_solver_core.m +++ b/matlab/perfect-foresight-models/perfect_foresight_solver_core.m @@ -60,7 +60,7 @@ if options_.block oo_.deterministic_simulation.status = false; end else - oo_ = feval([M_.fname '.dynamic'], options_, M_, oo_); + oo_ = solve_block_decomposed_problem(options_, M_, oo_); end else if options_.bytecode diff --git a/matlab/perfect-foresight-models/sim1.m b/matlab/perfect-foresight-models/sim1.m index 3bd956c91401d00bb59194e0c3122c56bf897ee2..d37afbf7e272cfc09644cba7cb033fbe09764f8d 100644 --- a/matlab/perfect-foresight-models/sim1.m +++ b/matlab/perfect-foresight-models/sim1.m @@ -119,7 +119,9 @@ if options.endogenous_terminal_period end if stop - if any(any(isnan(endogenousvariables))) || any(any(isinf(endogenousvariables))) + % initial or terminal observations may contain + % harmless NaN or Inf. We test only values computed above + if any(any(isnan(y))) || any(any(isinf(y))) info.status = false;% NaN or Inf occurred info.error = err; info.iterations = iter; diff --git a/matlab/perfect-foresight-models/sim1_linear.m b/matlab/perfect-foresight-models/sim1_linear.m index 2a68ac0b57301c32fa56deb4cc69cb79d4b3f99f..6f63ca1fa189aca98146da41f96c478f0a1f9966 100644 --- a/matlab/perfect-foresight-models/sim1_linear.m +++ b/matlab/perfect-foresight-models/sim1_linear.m @@ -120,9 +120,13 @@ if max(abs(d1))>options.solve_tolf error('Jacobian is not evaluated at the steady state!') end +% current variables [r0,c0,v0] = find(jacobian(:,jc)); +% current and predetermined [rT,cT,vT] = find(jacobian(:,jpc)); +% current and jump variables [r1,c1,v1] = find(jacobian(:,jcn)); +% all endogenous variables [rr,cc,vv] = find(jacobian(:,jendo)); iv0 = 1:length(v0); @@ -155,9 +159,16 @@ for it = (maximum_lag+1):(maximum_lag+periods) nv = length(vv); iA(iv+m,:) = [i_rows(rr),i_cols_A(cc),vv]; end - z(jendo) = Y(i_cols); - z(jexog) = transpose(exogenousvariables(it,:)); - res(i_rows) = jacobian*z; + if M.maximum_exo_lag > 0 + % needed as jacobian for lagged exogenous variables is wrong + % in current version of Dynare + zz = Y(i_cols); + res(i_rows) = dynamicmodel(zz, exogenousvariables, params, steadystate_y, it); + else + z(jendo) = Y(i_cols); + z(jexog) = transpose(exogenousvariables(it,:)); + res(i_rows) = jacobian*z; + end m = m + nv; i_rows = i_rows + ny; i_cols = i_cols + ny; diff --git a/matlab/perfect-foresight-models/sim1_purely_backward.m b/matlab/perfect-foresight-models/sim1_purely_backward.m index b3d88a7123674ba40893e0f2fe615cd7dfd55f0d..689d4f310d8e490eaca3bf48d5e195a92c1cd9db 100644 --- a/matlab/perfect-foresight-models/sim1_purely_backward.m +++ b/matlab/perfect-foresight-models/sim1_purely_backward.m @@ -38,13 +38,13 @@ dynamicmodel = str2func([M.fname,'.dynamic']); info.status = 1; -for it = 2:options.periods+1 +for it = M.maximum_lag + (1:options.periods) yb = endogenousvariables(:,it-1); % Values at previous period, also used as guess value for current period yb1 = yb(iyb); [tmp, check] = solve1(dynamicmodel, [yb1; yb], 1:M.endo_nbr, nyb+1:nyb+M.endo_nbr, ... 1, options.gstep, options.dynatol.f, options.dynatol.x, ... options.simul.maxit, options.debug, exogenousvariables, ... - M.params, steadystate, it+M.maximum_lag-1); + M.params, steadystate, it); if check info.status = 0; end diff --git a/matlab/perfect-foresight-models/solve_block_decomposed_problem.m b/matlab/perfect-foresight-models/solve_block_decomposed_problem.m new file mode 100644 index 0000000000000000000000000000000000000000..be248f27f2a5ff2a34c38e965936807996f9671d --- /dev/null +++ b/matlab/perfect-foresight-models/solve_block_decomposed_problem.m @@ -0,0 +1,93 @@ +function oo_ = solve_block_decomposed_problem(options_, M_, oo_) +% Computes deterministic simulation with block option without bytecode + +% Copyright (C) 2020 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/>. + +cutoff = 1e-15; + +if options_.stack_solve_algo==0 + mthd='Sparse LU'; +elseif options_.stack_solve_algo==1 + mthd='Relaxation'; +elseif options_.stack_solve_algo==2 + mthd='GMRES'; +elseif options_.stack_solve_algo==3 + mthd='BICGSTAB'; +elseif options_.stack_solve_algo==4 + mthd='OPTIMPATH'; +else + mthd='UNKNOWN'; +end +if options_.verbosity + printline(41) + disp(sprintf('MODEL SIMULATION (method=%s):',mthd)) + skipline() +end + +y=oo_.endo_simul; +T=NaN(M_.block_structure.dyn_tmp_nbr, options_.periods+M_.maximum_lag+M_.maximum_lead); +oo_.deterministic_simulation.status = 0; + +funcname = [ M_.fname '.dynamic']; + +for blk = 1:length(M_.block_structure.block) + recursive_size = M_.block_structure.block(blk).endo_nbr - M_.block_structure.block(blk).mfs; + y_index = M_.block_structure.block(blk).variable((recursive_size+1):end); + + if M_.block_structure.block(blk).Simulation_Type == 1 || ... % evaluateForward + M_.block_structure.block(blk).Simulation_Type == 2 % evaluateBackward + oo_.deterministic_simulation.status = true; + oo_.deterministic_simulation.error = 0; + oo_.deterministic_simulation.iterations = 0; + oo_.deterministic_simulation.block(blk).status = true; + oo_.deterministic_simulation.block(blk).error = 0; + oo_.deterministic_simulation.block(blk).iterations = 0; + if M_.block_structure.block(blk).Simulation_Type == 1 + range = M_.maximum_lag+1:M_.maximum_lag+options_.periods; + else + range = M_.maximum_lag+options_.periods:-1:M_.maximum_lag+1; + end + for it_ = range + y2 = dynvars_from_endo_simul(y, it_, M_); + [~, y2, T(:, it_)] = feval(funcname, blk, y2, oo_.exo_simul, M_.params, oo_.steady_state, T(:, it_), it_, false); + y(find(M_.lead_lag_incidence(M_.maximum_endo_lag+1, :)), it_) = y2(nonzeros(M_.lead_lag_incidence(M_.maximum_endo_lag+1, :))); + end + elseif M_.block_structure.block(blk).Simulation_Type == 3 || ... % solveForwardSimple + M_.block_structure.block(blk).Simulation_Type == 4 || ... % solveBackwardSimple + M_.block_structure.block(blk).Simulation_Type == 6 || ... % solveForwardComplete + M_.block_structure.block(blk).Simulation_Type == 7 % solveBackwardComplete + [y, T, oo_] = solve_one_boundary(funcname, y, oo_.exo_simul, M_.params, oo_.steady_state, T, y_index, M_.block_structure.block(blk).NNZDerivatives, options_.periods, M_.block_structure.block(blk).is_linear, blk, M_.maximum_lag, options_.simul.maxit, options_.solve_tolf, options_.slowc, cutoff, options_.stack_solve_algo, true, true, false, M_, options_, oo_); + elseif M_.block_structure.block(blk).Simulation_Type == 5 || ... % solveTwoBoundariesSimple + M_.block_structure.block(blk).Simulation_Type == 8 % solveTwoBoundariesComplete + [y, T, oo_] = solve_two_boundaries(funcname, y, oo_.exo_simul, M_.params, oo_.steady_state, T, y_index, M_.block_structure.block(blk).NNZDerivatives, options_.periods, M_.block_structure.block(blk).maximum_lag, M_.block_structure.block(blk).maximum_lead, M_.block_structure.block(blk).is_linear, blk, M_.maximum_lag, options_.simul.maxit, options_.solve_tolf, options_.slowc, cutoff, options_.stack_solve_algo, options_, M_, oo_); + end + + tmp = y(M_.block_structure.block(blk).variable, :); + if any(isnan(tmp) | isinf(tmp)) + disp(['Inf or Nan value during the resolution of block ' num2str(blk)]); + oo_.deterministic_simulation.status = false; + oo_.deterministic_simulation.error = 100; + oo_.deterministic_simulation.block(blk).status = false; + oo_.deterministic_simulation.block(blk).error = 100; + end + if ~oo_.deterministic_simulation.status + return + end +end + +oo_.endo_simul = y; diff --git a/matlab/plot_priors.m b/matlab/plot_priors.m index cbe288d2ca8590d608bc4f491a50cbd86d801643..7d8aec8f7c3616807da5d229c4d1d38f92682dfd 100644 --- a/matlab/plot_priors.m +++ b/matlab/plot_priors.m @@ -1,19 +1,21 @@ -function plot_priors(bayestopt_,M_,estim_params_,options_) +function plot_priors(bayestopt_,M_,estim_params_,options_,optional_title) % function plot_priors % plots prior density % % INPUTS -% o bayestopt_ [structure] -% o M_ [structure] -% o options_ [structure] -% +% o bayestopt_ [structure] +% o M_ [structure] +% o estim_params_ [structure] +% o options_ [structure] +% o optional_title [string] + % OUTPUTS % None % % SPECIAL REQUIREMENTS % None -% Copyright (C) 2004-2017 Dynare Team +% Copyright (C) 2004-2020 Dynare Team % % This file is part of Dynare. % @@ -31,8 +33,11 @@ function plot_priors(bayestopt_,M_,estim_params_,options_) % along with Dynare. If not, see <http://www.gnu.org/licenses/>. TeX = options_.TeX; - -figurename = 'Priors'; +if nargin<5 + figurename = 'Priors'; +else + figurename = optional_title; +end npar = length(bayestopt_.p1); [nbplt,nr,nc,lr,lc,nstar] = pltorg(npar); @@ -57,28 +62,20 @@ for plt = 1:nbplt i = (plt-1)*nstar + index; [x,f,abscissa,dens,binf,bsup] = draw_prior_density(i,bayestopt_); [nam,texnam] = get_the_name(i,TeX,M_,estim_params_,options_); - if TeX - if index==1 - TeXNAMES = texnam; - NAMES = nam; - else - TeXNAMES = char(TeXNAMES,texnam); - NAMES = char(NAMES,nam); - end - end subplot(nr,nc,index) hh = plot(x,f,'-k','linewidth',2); set(hh,'color',[0.7 0.7 0.7]); box on - title(nam,'Interpreter','none') + if TeX + title(texnam,'Interpreter','latex') + else + title(nam,'Interpreter','none') + end drawnow end dyn_saveas(hplt,[M_.fname, '/graphs/' M_.fname '_Priors' int2str(plt)],options_.nodisplay,options_.graph_format); if TeX && any(strcmp('eps',cellstr(options_.graph_format))) fprintf(fidTeX,'\\begin{figure}[H]\n'); - for jj = 1:nstar0 - fprintf(fidTeX,'\\psfrag{%s}[1][][0.5][0]{%s}\n',deblank(NAMES(jj,:)),deblank(TeXNAMES(jj,:))); - end fprintf(fidTeX,'\\centering\n'); fprintf(fidTeX,'\\includegraphics[width=%2.2f\\textwidth]{%s_Priors%s}\n',options_.figures.textwidth*min(index/nc,1),[M_.fname, '/graphs/' M_.fname],int2str(plt)); fprintf(fidTeX,'\\caption{Priors.}'); diff --git a/matlab/pm3.m b/matlab/pm3.m index f32468b40e7a09a8fd7022a76dd49f82c09d2892..3d93b04c1bcb87d22e09798a28dec05795d951a1 100644 --- a/matlab/pm3.m +++ b/matlab/pm3.m @@ -307,6 +307,9 @@ if ~options_.nograph && ~options_.no_graph.posterior localVars.n2=n2; localVars.Distrib=Distrib; localVars.varlist=varlist; + if options_.TeX + localVars.varlist_TeX=varlist_TeX; + end localVars.MaxNumberOfPlotsPerFigure=MaxNumberOfPlotsPerFigure; localVars.name3=name3; localVars.tit3=tit3; @@ -352,28 +355,14 @@ if ~options_.nograph && ~options_.no_graph.posterior i=0; for j=1:length(nvar0) - NAMES = []; - TEXNAMES = []; nvar=nvar0(j); while i<nvar i=i+1; if max(abs(Mean(:,i))) > 10^(-6) subplotnum = subplotnum+1; - name = varlist{i}; - texname = varlist_TeX{i}; - if subplotnum==1 - NAMES = name; - TEXNAMES = ['$' texname '$']; - else - NAMES = char(NAMES,name); - TEXNAMES = char(TEXNAMES,['$' texname '$']); - end end if subplotnum == MaxNumberOfPlotsPerFigure || i == nvar fprintf(fidTeX,'\\begin{figure}[H]\n'); - for jj = 1:size(TEXNAMES,1) - fprintf(fidTeX,['\\psfrag{%s}[1][][0.5][0]{%s}\n'],deblank(NAMES(jj,:)),deblank(TEXNAMES(jj,:))); - end fprintf(fidTeX,'\\centering \n'); fprintf(fidTeX,['\\includegraphics[width=%2.2f\\textwidth]{%s/Output/%s_' name3 '_%s}\n'],options_.figures.textwidth*min(subplotnum/nn,1),M_.dname,M_.fname, tit3{i}); fprintf(fidTeX,'\\label{Fig:%s:%s}\n',name3,tit3{i}); @@ -381,8 +370,6 @@ if ~options_.nograph && ~options_.no_graph.posterior fprintf(fidTeX,'\\end{figure}\n'); fprintf(fidTeX,' \n'); subplotnum = 0; - NAMES = []; - TEXNAMES = []; end end end diff --git a/matlab/pm3_core.m b/matlab/pm3_core.m index b75950937687690c09cd7e23cd4d858a22ef3455..a820f6ad452698fef15bd86a92ad3fd163b92c8a 100644 --- a/matlab/pm3_core.m +++ b/matlab/pm3_core.m @@ -42,6 +42,7 @@ nn=myinputs.nn; n2=myinputs.n2; Distrib=myinputs.Distrib; varlist=myinputs.varlist; + MaxNumberOfPlotsPerFigure=myinputs.MaxNumberOfPlotsPerFigure; name3=myinputs.name3; tit3=myinputs.tit3; @@ -54,6 +55,9 @@ end global options_ M_ oo_ +if options_.TeX + varlist_TeX=myinputs.varlist_TeX; +end if whoiam prct0={0,whoiam,Parallel(ThisMatlab)}; @@ -89,8 +93,11 @@ for i=fpar:nvar plot(1:n2,Mean(:,i),'-k','linewidth',1); xlim([1 n2]); hold off; - name = deblank(varlist(i,:)); - title(name,'Interpreter','none') + if options_.TeX + title(['$' varlist_TeX{i,:} '$'],'Interpreter','latex') + else + title(varlist(i,:),'Interpreter','none') + end yticklabels=get(gca,'yticklabel'); if size(char(yticklabels),2)>5 %make sure yticks do not screw up figure yticks=get(gca,'ytick'); diff --git a/matlab/posterior_analysis.m b/matlab/posterior_analysis.m index 16088823d28bd637be5a2945ca963904a1e7fa45..03bff87b8733cae15839ba2351a97d1c797db88a 100644 --- a/matlab/posterior_analysis.m +++ b/matlab/posterior_analysis.m @@ -1,6 +1,6 @@ function oo_ = posterior_analysis(type,arg1,arg2,arg3,options_,M_,oo_) -% Copyright (C) 2008-2018 Dynare Team +% Copyright (C) 2008-2020 Dynare Team % % This file is part of Dynare. % @@ -64,15 +64,15 @@ switch type end oo_ = variance_decomposition_mc_analysis(SampleSize,'posterior',M_.dname,M_.fname,... M_.exo_names,arg2,vartan,arg1,options_.mh_conf_sig,oo_,options_); - if ~all(M_.H==0) + if ~all(diag(M_.H)==0) if strmatch(arg1,options_.varobs,'exact') - if isoctave || matlab_ver_less_than('8.1') + if isoctave [observable_name_requested_vars,index_subset,index_observables]=intersect_stable(vartan,options_.varobs); else [observable_name_requested_vars,index_subset,index_observables]=intersect(vartan,options_.varobs,'stable'); end oo_ = variance_decomposition_ME_mc_analysis(SampleSize,'posterior',M_.dname,M_.fname,... - M_.exo_names,arg2,observable_name_requested_vars,arg1,options_.mh_conf_sig,oo_,options_); + [M_.exo_names;'ME'],arg2,observable_name_requested_vars,arg1,options_.mh_conf_sig,oo_,options_); end end case 'correlation' @@ -89,10 +89,10 @@ switch type end oo_ = conditional_variance_decomposition_mc_analysis(SampleSize,'posterior',M_.dname,M_.fname,... arg3,M_.exo_names,arg2,vartan,arg1,options_.mh_conf_sig,oo_,options_); - if ~all(M_.H==0) + if ~all(diag(M_.H)==0) if strmatch(arg1,options_.varobs,'exact') oo_ = conditional_variance_decomposition_ME_mc_analysis(SampleSize,'posterior',M_.dname,M_.fname,... - arg3,M_.exo_names,arg2,vartan,arg1,options_.mh_conf_sig,oo_,options_); + arg3,[M_.exo_names;'ME'],arg2,vartan,arg1,options_.mh_conf_sig,oo_,options_); end end otherwise diff --git a/matlab/prior_analysis.m b/matlab/prior_analysis.m index 0c945c3ebd6d603fd8927bfe598a979bb8731116..3a621a9a8237dd88486f392926c8e0e7128de91f 100644 --- a/matlab/prior_analysis.m +++ b/matlab/prior_analysis.m @@ -78,7 +78,7 @@ switch type end oo_ = conditional_variance_decomposition_mc_analysis(SampleSize,'prior',M_.dname,M_.fname,... arg3,M_.exo_names,arg2,vartan,arg1,options_.mh_conf_sig,oo_,options_); - if ~all(M_.H==0) + if ~all(diag(M_.H)==0) if strmatch(vartan(arg1,:),options_.varobs,'exact') oo_ = conditional_variance_decomposition_ME_mc_analysis(SampleSize,'prior',M_.dname,M_.fname,... arg3,M_.exo_names,arg2,vartan,arg1,options_.mh_conf_sig,oo_,options_); diff --git a/matlab/prior_posterior_statistics.m b/matlab/prior_posterior_statistics.m index 3a638882d5e73f630759bd5aed389282d88b2958..c52dd7701028207be9f9abef1dab327d876ec197 100644 --- a/matlab/prior_posterior_statistics.m +++ b/matlab/prior_posterior_statistics.m @@ -354,7 +354,7 @@ if options_.smoother if nvn for obs_iter=1:length(options_.varobs) meas_error_names{obs_iter,1}=['SE_EOBS_' M_.endo_names{strmatch(options_.varobs{obs_iter},M_.endo_names,'exact')}]; - texnames{obs_iter,1}=['SE_EOBS_' M_.endo_names_tex{strmatch(options_.varobs{obs_iter},M_.endo_names,'exact')}]; + texnames{obs_iter,1}=['\sigma^{ME}_' M_.endo_names_tex{strmatch(options_.varobs{obs_iter},M_.endo_names,'exact')}]; end pm3(meas_err_nbr,gend,ifil(3),B,'Smoothed measurement errors',... '',meas_error_names,texnames,meas_error_names,... diff --git a/matlab/prior_sampler.m b/matlab/prior_sampler.m index 2506f91c306aef73867c2b982ebd8736b0e3615a..c09ff8c66de7a427480afed64aa86efcd24ef48a 100644 --- a/matlab/prior_sampler.m +++ b/matlab/prior_sampler.m @@ -150,7 +150,7 @@ while iteration < NumberOfSimulations end if ( file_line_number==TableOfInformations(file_indx_number+1,2) ) file_indx_number = file_indx_number + 1; - save([ PriorDirectoryName '/prior_draws' int2str(file_indx_number) '.mat' ],'pdraws'); + save([ PriorDirectoryName '/prior_draws' int2str(file_indx_number) '.mat' ],'pdraws','estim_params_'); if file_indx_number<NumberOfFiles if drsave pdraws = cell(TableOfInformations(file_indx_number+1,2),drsave+2); diff --git a/matlab/pruned_state_space_system.m b/matlab/pruned_state_space_system.m index df5cf5ab46e340e3428e98c888911721bf24175c..1f180b9ae575e1f1bcd18cb17d0f9112828b6983 100644 --- a/matlab/pruned_state_space_system.m +++ b/matlab/pruned_state_space_system.m @@ -10,12 +10,13 @@ function pruned_state_space = pruned_state_space_system(M, options, dr, indy, nl % Econometrics and Statistics, Volume 6, Pages 44-56. % ========================================================================= % INPUTS -% M: [structure] storing the model information -% options: [structure] storing the options -% dr: [structure] storing the results from perturbation approximation -% indy: [vector] index of control variables in DR order -% nlags: [integer] number of lags in autocovariances and autocorrelations -% useautocorr: [boolean] true: compute autocorrelations +% M: [structure] storing the model information +% options: [structure] storing the options +% dr: [structure] storing the results from perturbation approximation +% indy: [vector] index of control variables in DR order +% nlags: [integer] number of lags in autocovariances and autocorrelations +% useautocorr: [boolean] true: compute autocorrelations +% compute_derivs: [boolean] true: compute derivatives % ------------------------------------------------------------------------- % OUTPUTS % pruned_state_space: [structure] with the following fields: @@ -85,7 +86,7 @@ function pruned_state_space = pruned_state_space_system(M, options, dr, indy, nl % This function calls % * allVL1.m % * commutation.m -% * disclyap_fast.m +% * disclyap_fast (MEX) % * duplication.m % * lyapunov_symm.m % * prodmom @@ -240,6 +241,7 @@ function pruned_state_space = pruned_state_space_system(M, options, dr, indy, nl % See code below how z and inov are defined at first, second, and third order, % and how to set up A, B, C, D and compute unconditional first and second moments of inov, z and y +persistent QPu COMBOS4 Q6Pu COMBOS6 K_u_xx K_u_ux K_xx_x %% Auxiliary indices and objects order = options.order; @@ -418,8 +420,10 @@ if order > 1 %Compute unique fourth order product moments of u, i.e. unique(E[kron(kron(kron(u,u),u),u)],'stable') u_nbr4 = u_nbr*(u_nbr+1)/2*(u_nbr+2)/3*(u_nbr+3)/4; - QPu = quadruplication(u_nbr); - COMBOS4 = flipud(allVL1(u_nbr, 4)); %all possible (unique) combinations of powers that sum up to four + if isempty(QPu) + QPu = quadruplication(u_nbr); + COMBOS4 = flipud(allVL1(u_nbr, 4)); %all possible (unique) combinations of powers that sum up to four + end E_u_u_u_u = zeros(u_nbr4,1); %only unique entries if compute_derivs && (stderrparam_nbr+corrparam_nbr>0) dE_u_u_u_u = zeros(u_nbr4,stderrparam_nbr+corrparam_nbr); @@ -588,8 +592,11 @@ if order > 1 if order > 2 % Some common and useful objects for order > 2 - K_u_xx = commutation(u_nbr,x_nbr^2,1); - K_u_ux = commutation(u_nbr,u_nbr*x_nbr,1); + if isempty(K_u_xx) + K_u_xx = commutation(u_nbr,x_nbr^2,1); + K_u_ux = commutation(u_nbr,u_nbr*x_nbr,1); + K_xx_x = commutation(x_nbr^2,x_nbr); + end hx_hss2 = kron(hx,1/2*hss); hu_hss2 = kron(hu,1/2*hss); hx_hxx2 = kron(hx,1/2*hxx); @@ -658,9 +665,11 @@ if order > 1 end % Compute unique sixth-order product moments of u, i.e. unique(E[kron(kron(kron(kron(kron(u,u),u),u),u),u)],'stable') - u_nbr6 = u_nbr*(u_nbr+1)/2*(u_nbr+2)/3*(u_nbr+3)/4*(u_nbr+4)/5*(u_nbr+5)/6; - Q6Pu = Q6_plication(u_nbr); - COMBOS6 = flipud(allVL1(u_nbr, 6)); %all possible (unique) combinations of powers that sum up to six + u_nbr6 = u_nbr*(u_nbr+1)/2*(u_nbr+2)/3*(u_nbr+3)/4*(u_nbr+4)/5*(u_nbr+5)/6; + if isempty(Q6Pu) + Q6Pu = Q6_plication(u_nbr); + COMBOS6 = flipud(allVL1(u_nbr, 6)); %all possible (unique) combinations of powers that sum up to six + end E_u_u_u_u_u_u = zeros(u_nbr6,1); %only unique entries if compute_derivs && (stderrparam_nbr+corrparam_nbr>0) dE_u_u_u_u_u_u = zeros(u_nbr6,stderrparam_nbr+corrparam_nbr); @@ -798,7 +807,7 @@ if order > 1 E_inovzlag1 = zeros(inov_nbr,z_nbr); % Attention: E[inov*z(-1)'] is not equal to zero for a third-order approximation due to kron(kron(xf(-1),u),u) E_inovzlag1(id_inov6_xf_u_u , id_z1_xf ) = kron(E_xfxf,E_uu(:)); E_inovzlag1(id_inov6_xf_u_u , id_z4_xrd ) = kron(E_xrdxf',E_uu(:)); - E_inovzlag1(id_inov6_xf_u_u , id_z5_xf_xs ) = kron(reshape(commutation(x_nbr^2,x_nbr)*vec(E_xsxf_xf),x_nbr,x_nbr^2),vec(E_uu)) ; + E_inovzlag1(id_inov6_xf_u_u , id_z5_xf_xs ) = kron(reshape(K_xx_x*vec(E_xsxf_xf),x_nbr,x_nbr^2),vec(E_uu)) ; E_inovzlag1(id_inov6_xf_u_u , id_z6_xf_xf_xf ) = kron(reshape(E_xf_xfxf_xf,x_nbr,x_nbr^3),E_uu(:)); Binovzlag1A= B*E_inovzlag1*transpose(A); @@ -980,7 +989,7 @@ if order > 1 dE_inovzlag1(id_inov6_xf_u_u , id_z1_xf , jp3) = kron(dE_xfxf_jp3,E_uu(:)) + kron(E_xfxf,dE_uu_jp3(:)); dE_inovzlag1(id_inov6_xf_u_u , id_z4_xrd , jp3) = kron(dE_xrdxf_jp3',E_uu(:)) + kron(E_xrdxf',dE_uu_jp3(:)); - dE_inovzlag1(id_inov6_xf_u_u , id_z5_xf_xs , jp3) = kron(reshape(commutation(x_nbr^2,x_nbr)*vec(dE_xsxf_xf_jp3),x_nbr,x_nbr^2),vec(E_uu)) + kron(reshape(commutation(x_nbr^2,x_nbr)*vec(E_xsxf_xf),x_nbr,x_nbr^2),vec(dE_uu_jp3)) ; + dE_inovzlag1(id_inov6_xf_u_u , id_z5_xf_xs , jp3) = kron(reshape(K_xx_x*vec(dE_xsxf_xf_jp3),x_nbr,x_nbr^2),vec(E_uu)) + kron(reshape(K_xx_x*vec(E_xsxf_xf),x_nbr,x_nbr^2),vec(dE_uu_jp3)) ; dE_inovzlag1(id_inov6_xf_u_u , id_z6_xf_xf_xf , jp3) = kron(reshape(dE_xf_xfxf_xf_jp3,x_nbr,x_nbr^3),E_uu(:)) + kron(reshape(E_xf_xfxf_xf,x_nbr,x_nbr^3),dE_uu_jp3(:)); dBinovzlag1A_jp3 = dB(:,:,jp3)*E_inovzlag1*transpose(A) + B*dE_inovzlag1(:,:,jp3)*transpose(A) + B*E_inovzlag1*transpose(dA(:,:,jp3)); @@ -1029,12 +1038,12 @@ else + C(stationary_vars,:)*transpose(E_inovzlag1)*D(stationary_vars,:)'... + D(stationary_vars,:)*Varinov*D(stationary_vars,:)'; end -indzeros = find(abs(Var_y) < 1e-12); %find values that are numerical zero -Var_y(indzeros) = 0; + +Var_y(abs(Var_y) < 1e-12) = 0; %find values that are numerical zero if useautocorr - sy = sqrt(diag(Var_y)); %theoretical standard deviation - sy = sy(stationary_vars); - sy = sy*sy'; %cross products of standard deviations + sdy = sqrt(diag(Var_y)); %theoretical standard deviation + sdy = sdy(stationary_vars); + sy = sdy*sdy'; %cross products of standard deviations Corr_y = NaN*ones(y_nbr,y_nbr); Corr_y(stationary_vars,stationary_vars) = Var_y(stationary_vars,stationary_vars)./sy; Corr_yi = NaN*ones(y_nbr,y_nbr,nlags); @@ -1048,21 +1057,20 @@ if compute_derivs end for jpV=1:totparam_nbr if order < 3 - dVar_y(stationary_vars,stationary_vars,jpV) = dC(stationary_vars,:,jpV)*Var_z*C(stationary_vars,:)' + C(stationary_vars,:)*dVar_z(:,:,jpV)*C(stationary_vars,:)' + C(stationary_vars,:)*Var_z*dC(stationary_vars,:,jpV)'... + dVar_y_tmp = dC(stationary_vars,:,jpV)*Var_z*C(stationary_vars,:)' + C(stationary_vars,:)*dVar_z(:,:,jpV)*C(stationary_vars,:)' + C(stationary_vars,:)*Var_z*dC(stationary_vars,:,jpV)'... + dD(stationary_vars,:,jpV)*Varinov*D(stationary_vars,:)' + D(stationary_vars,:)*dVarinov(:,:,jpV)*D(stationary_vars,:)' + D(stationary_vars,:)*Varinov*dD(stationary_vars,:,jpV)'; else - dVar_y(stationary_vars,stationary_vars,jpV) = dC(stationary_vars,:,jpV)*Var_z*C(stationary_vars,:)' + C(stationary_vars,:)*dVar_z(:,:,jpV)*C(stationary_vars,:)' + C(stationary_vars,:)*Var_z*dC(stationary_vars,:,jpV)'... + dVar_y_tmp = dC(stationary_vars,:,jpV)*Var_z*C(stationary_vars,:)' + C(stationary_vars,:)*dVar_z(:,:,jpV)*C(stationary_vars,:)' + C(stationary_vars,:)*Var_z*dC(stationary_vars,:,jpV)'... + dD(stationary_vars,:,jpV)*E_inovzlag1*C(stationary_vars,:)' + D(stationary_vars,:)*dE_inovzlag1(:,:,jpV)*C(stationary_vars,:)' + D(stationary_vars,:)*E_inovzlag1*dC(stationary_vars,:,jpV)'... + dC(stationary_vars,:,jpV)*transpose(E_inovzlag1)*D(stationary_vars,:)' + C(stationary_vars,:)*transpose(dE_inovzlag1(:,:,jpV))*D(stationary_vars,:)' + C(stationary_vars,:)*transpose(E_inovzlag1)*dD(stationary_vars,:,jpV)'... + dD(stationary_vars,:,jpV)*Varinov*D(stationary_vars,:)' + D(stationary_vars,:)*dVarinov(:,:,jpV)*D(stationary_vars,:)' + D(stationary_vars,:)*Varinov*dD(stationary_vars,:,jpV)'; end - [indzerosrow,indzeroscol] = find(abs(dVar_y(:,:,jpV)) < 1e-12); %find values that are numerical zero - dVar_y(indzerosrow,indzeroscol,jpV) = 0; + dVar_y_tmp(abs(dVar_y_tmp) < 1e-12) = 0; %find values that are numerical zero + dVar_y(stationary_vars,stationary_vars,jpV) = dVar_y_tmp; if useautocorr - %is this correct?[@wmutschl] - dsy = 1/2./sy.*diag(dVar_y(:,:,jpV)); + dsy = 1/2./sdy.*diag(dVar_y(:,:,jpV)); dsy = dsy(stationary_vars); - dsy = dsy*sy'+sy*dsy'; + dsy = dsy*sdy'+sdy*dsy'; dCorr_y(stationary_vars,stationary_vars,jpV) = (dVar_y(stationary_vars,stationary_vars,jpV).*sy-dsy.*Var_y(stationary_vars,stationary_vars))./(sy.*sy); dCorr_y(stationary_vars,stationary_vars,jpV) = dCorr_y(stationary_vars,stationary_vars,jpV)-diag(diag(dCorr_y(stationary_vars,stationary_vars,jpV)))+diag(diag(dVar_y(stationary_vars,stationary_vars,jpV))); end @@ -1089,7 +1097,7 @@ for i = 1:nlags E_inovzlagi = zeros(inov_nbr,z_nbr); E_inovzlagi(id_inov6_xf_u_u , id_z1_xf ) = kron(hxi*E_xfxf,E_uu(:)); E_inovzlagi(id_inov6_xf_u_u , id_z4_xrd ) = kron(hxi*E_xrdxf',E_uu(:)); - E_inovzlagi(id_inov6_xf_u_u , id_z5_xf_xs ) = kron(hxi*reshape(commutation(x_nbr^2,x_nbr)*vec(E_xsxf_xf),x_nbr,x_nbr^2),vec(E_uu)); + E_inovzlagi(id_inov6_xf_u_u , id_z5_xf_xs ) = kron(hxi*reshape(K_xx_x*vec(E_xsxf_xf),x_nbr,x_nbr^2),vec(E_uu)); E_inovzlagi(id_inov6_xf_u_u , id_z6_xf_xf_xf ) = kron(hxi*reshape(E_xf_xfxf_xf,x_nbr,x_nbr^3),E_uu(:)); Var_yi(stationary_vars,stationary_vars,i) = C(stationary_vars,:)*Var_zi*C(stationary_vars,:)' + C(stationary_vars,:)*Ai*tmp + D(stationary_vars,:)*E_inovzlagi*C(stationary_vars,:)'; end @@ -1125,18 +1133,21 @@ if compute_derivs E_inovzlagi = zeros(inov_nbr,z_nbr); E_inovzlagi(id_inov6_xf_u_u , id_z1_xf ) = kron(hxi*E_xfxf,E_uu(:)); E_inovzlagi(id_inov6_xf_u_u , id_z4_xrd ) = kron(hxi*E_xrdxf',E_uu(:)); - E_inovzlagi(id_inov6_xf_u_u , id_z5_xf_xs ) = kron(hxi*reshape(commutation(x_nbr^2,x_nbr)*vec(E_xsxf_xf),x_nbr,x_nbr^2),vec(E_uu)); + E_inovzlagi(id_inov6_xf_u_u , id_z5_xf_xs ) = kron(hxi*reshape(K_xx_x*vec(E_xsxf_xf),x_nbr,x_nbr^2),vec(E_uu)); E_inovzlagi(id_inov6_xf_u_u , id_z6_xf_xf_xf ) = kron(hxi*reshape(E_xf_xfxf_xf,x_nbr,x_nbr^3),E_uu(:)); dE_inovzlagi_jpVi = zeros(inov_nbr,z_nbr); dE_inovzlagi_jpVi(id_inov6_xf_u_u , id_z1_xf ) = kron(dhxi_jpVi*E_xfxf,E_uu(:)) + kron(hxi*dE_xfxf(:,:,jpVi),E_uu(:)) + kron(hxi*E_xfxf,vec(dE_uu(:,:,jpVi))); dE_inovzlagi_jpVi(id_inov6_xf_u_u , id_z4_xrd ) = kron(dhxi_jpVi*E_xrdxf',E_uu(:)) + kron(hxi*dE_xrdxf(:,:,jpVi)',E_uu(:)) + kron(hxi*E_xrdxf',vec(dE_uu(:,:,jpVi))); - dE_inovzlagi_jpVi(id_inov6_xf_u_u , id_z5_xf_xs ) = kron(dhxi_jpVi*reshape(commutation(x_nbr^2,x_nbr)*vec(E_xsxf_xf),x_nbr,x_nbr^2),vec(E_uu)) + kron(hxi*reshape(commutation(x_nbr^2,x_nbr)*vec(dE_xsxf_xf(:,:,jpVi)),x_nbr,x_nbr^2),vec(E_uu)) + kron(hxi*reshape(commutation(x_nbr^2,x_nbr)*vec(E_xsxf_xf),x_nbr,x_nbr^2),vec(dE_uu(:,:,jpVi))); + dE_inovzlagi_jpVi(id_inov6_xf_u_u , id_z5_xf_xs ) = kron(dhxi_jpVi*reshape(K_xx_x*vec(E_xsxf_xf),x_nbr,x_nbr^2),vec(E_uu)) + kron(hxi*reshape(K_xx_x*vec(dE_xsxf_xf(:,:,jpVi)),x_nbr,x_nbr^2),vec(E_uu)) + kron(hxi*reshape(K_xx_x*vec(E_xsxf_xf),x_nbr,x_nbr^2),vec(dE_uu(:,:,jpVi))); dE_inovzlagi_jpVi(id_inov6_xf_u_u , id_z6_xf_xf_xf ) = kron(dhxi_jpVi*reshape(E_xf_xfxf_xf,x_nbr,x_nbr^3),E_uu(:)) + kron(hxi*reshape(dE_xf_xfxf_xf(:,:,jpVi),x_nbr,x_nbr^3),E_uu(:)) + kron(hxi*reshape(E_xf_xfxf_xf,x_nbr,x_nbr^3),vec(dE_uu(:,:,jpVi))); dVar_yi(stationary_vars,stationary_vars,i,jpVi) = dC(stationary_vars,:,jpVi)*Var_zi*C(stationary_vars,:)' + C(stationary_vars,:)*dVar_zi_jpVi*C(stationary_vars,:)' + C(stationary_vars,:)*Var_zi*dC(stationary_vars,:,jpVi)'... + dC(stationary_vars,:,jpVi)*Ai*tmp + C(stationary_vars,:)*dAi_jpVi*tmp + C(stationary_vars,:)*Ai*dtmp_jpVi... + dD(stationary_vars,:,jpVi)*E_inovzlagi*C(stationary_vars,:)' + D(stationary_vars,:)*dE_inovzlagi_jpVi*C(stationary_vars,:)' + D(stationary_vars,:)*E_inovzlagi*dC(stationary_vars,:,jpVi)'; end if useautocorr + dsy = 1/2./sdy.*diag(dVar_y(:,:,jpVi)); + dsy = dsy(stationary_vars); + dsy = dsy*sdy'+sdy*dsy'; dCorr_yi(stationary_vars,stationary_vars,i,jpVi) = (dVar_yi(stationary_vars,stationary_vars,i,jpVi).*sy-dsy.*Var_yi(stationary_vars,stationary_vars,i))./(sy.*sy); end dAi_jpVi = dAi_jpVi*A + Ai*dA(:,:,jpVi); @@ -1176,7 +1187,7 @@ if compute_derivs end end end -non_stationary_vars = setdiff(1:y_nbr,stationary_vars); +non_stationary_vars = ~ismember((1:y_nbr)',stationary_vars); E_y(non_stationary_vars) = NaN; if compute_derivs dE_y(non_stationary_vars,:) = NaN; @@ -1192,7 +1203,7 @@ pruned_state_space.D = D; pruned_state_space.c = c; pruned_state_space.d = d; pruned_state_space.Varinov = Varinov; -pruned_state_space.Var_z = Var_z; %remove in future [@wmutschl] +% pruned_state_space.Var_z = Var_z; % pruned_state_space.Var_y = Var_y; pruned_state_space.Var_yi = Var_yi; if useautocorr diff --git a/matlab/resid.m b/matlab/resid.m index 1f3743a072e7c590931eb1e9f0986414b672738a..d28fd7655c4fe6ad194e7690213acd50258fe781 100644 --- a/matlab/resid.m +++ b/matlab/resid.m @@ -69,12 +69,18 @@ end % Compute the residuals if options_.block && ~options_.bytecode z = zeros(M_.endo_nbr,1); + T = NaN(M_.block_structure_stat.tmp_nbr, 1); for i = 1:length(M_.block_structure_stat.block) - [r, g, yy, var_indx] = feval([M_.fname '.static'],... - i,... - oo_.steady_state,... - [oo_.exo_steady_state; ... - oo_.exo_det_steady_state], M_.params); + [r, yy, T, g] = feval([M_.fname '.static'],... + i,... + oo_.steady_state,... + [oo_.exo_steady_state; ... + oo_.exo_det_steady_state], M_.params, T); + if M_.block_structure_stat.block(i).Simulation_Type == 1 || ... % evaluateForward + M_.block_structure_stat.block(i).Simulation_Type == 2 % evaluateBackward + vidx = M_.block_structure_stat.block(i).variable; + r = yy(vidx) - oo_.steady_state(vidx); + end idx = M_.block_structure_stat.block(i).equation; z(idx) = r; end @@ -97,7 +103,7 @@ if nargout == 0 disp('Residuals of the static equations:') skipline() for i=1:M_.orig_endo_nbr - if abs(z(i)) < options_.dynatol.f/100 + if abs(z(i)) < options_.solve_tolf/100 tmp = 0; else tmp = z(i); diff --git a/matlab/rplot.m b/matlab/rplot.m index 76a606d1a7efdf9183ce7be4da74443c172020f9..2605e7f5a485b87804e940f4006fb0782c34483b 100644 --- a/matlab/rplot.m +++ b/matlab/rplot.m @@ -54,9 +54,11 @@ for k = 1:length(s1) error ('rplot: One of the variables specified does not exist') ; else y = [y; oo_.exo_simul(:, strcmp(s1{k}, M_.exo_names))'] ; + s1_TeX(k,1)=M_.exo_names_tex(strcmp(s1{k}, M_.exo_names)); end else y = [y; oo_.endo_simul(strcmp(s1{k}, M_.endo_names), :)]; + s1_TeX(k,1)=M_.endo_names_tex(strcmp(s1{k}, M_.endo_names)); end end @@ -75,15 +77,21 @@ end if options_.rplottype == 0 hh=dyn_figure(options_.nodisplay,'Name', 'Simulated Trajectory'); plot(ix(i),y(:,i)) ; - title (['Plot of ' strjoin(s1, ' ')],'Interpreter','none') ; + if options_.TeX + title (['Plot of $' strjoin(s1_TeX, '~') '$'],'Interpreter','latex') ; + else + title (['Plot of ' strjoin(s1, ' ')],'Interpreter','none') ; + end xlabel('Periods') ; xlim([min(ix(i)) max(ix(i))]) if length(s1) > 1 - if isoctave - legend(s1); + if options_.TeX + for k = 1:length(s1) + s1_TeX(k,1)={['$' s1_TeX{k,1} '$']}; + end + legend(s1_TeX,'interpreter','latex'); else - h = legend(s1); - set(h, 'Interpreter', 'none'); + legend(s1,'interpreter','none'); end end dyn_saveas(hh,[M_.fname, filesep, 'graphs', filesep, 'SimulatedTrajectory_' s1{1}],options_.nodisplay,options_.graph_format) @@ -95,7 +103,11 @@ elseif options_.rplottype == 1 hh=dyn_figure(options_.nodisplay,'Name', 'Simulated Trajectory'); plot(ix(i),y(j,i)) ; xlim([min(ix(i)) max(ix(i))]) - title(['Plot of ' s1{j}],'Interpreter','none') ; + if options_.TeX + title(['Plot of $' s1_TeX{j} '$'],'Interpreter','latex') ; + else + title(['Plot of ' s1{j}],'Interpreter','none') ; + end xlabel('Periods') ; dyn_saveas(hh,[M_.fname, filesep, 'graphs', filesep, 'SimulatedTrajectory_' s1{j}],options_.nodisplay,options_.graph_format) if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format))) @@ -116,8 +128,13 @@ elseif options_.rplottype == 2 plot(ix(i),oo_.exo_steady_state(strcmp(s1{j}, M_.exo_names))*ones(1,size(i,1)),'r:') ; end xlabel('Periods') ; - ylabel([s1{j}],'Interpreter','none') ; - title(['Plot of ' s1{j}],'Interpreter','none') ; + if options_.TeX + ylabel(['$' s1_TeX{j} '$'],'Interpreter','latex') ; + title(['Plot of $' s1_TeX{j} '$'],'Interpreter','latex') ; + else + ylabel([s1{j}],'Interpreter','none') ; + title(['Plot of ' s1{j}],'Interpreter','none') ; + end axis tight; end dyn_saveas(hh,[M_.fname, filesep, 'graphs', filesep, 'SimulatedTrajectory_' s1{1}],options_.nodisplay,options_.graph_format) diff --git a/matlab/selec_posterior_draws.m b/matlab/selec_posterior_draws.m index 11d9a4dad16e6388f81134ba135670f8e91839ce..c42e1ced80fdc769dfe10a731cc0e2a45483c7e9 100644 --- a/matlab/selec_posterior_draws.m +++ b/matlab/selec_posterior_draws.m @@ -122,7 +122,7 @@ if info old_mhblck = mhblck; end clear('x2') - save([BaseName '_posterior_draws1.mat'],'pdraws') + save([BaseName '_posterior_draws1.mat'],'pdraws','estim_params_') else% The posterior draws are saved in xx files. NumberOfDrawsPerFile = fix(MAX_mega_bytes/drawsize); NumberOfFiles = ceil(SampleSize*drawsize/MAX_mega_bytes); @@ -149,7 +149,7 @@ if info old_mhblck = mhblck; if fnum < NumberOfFiles && linee == NumberOfDrawsPerFile linee = 0; - save([BaseName '_posterior_draws' num2str(fnum) '.mat'],'pdraws') + save([BaseName '_posterior_draws' num2str(fnum) '.mat'],'pdraws','estim_params_') fnum = fnum+1; if fnum < NumberOfFiles pdraws = cell(NumberOfDrawsPerFile,info); @@ -158,6 +158,6 @@ if info end end end - save([BaseName '_posterior_draws' num2str(fnum) '.mat'],'pdraws') + save([BaseName '_posterior_draws' num2str(fnum) '.mat'],'pdraws','estim_params_') end end \ No newline at end of file diff --git a/matlab/set_dynare_random_generator_state.m b/matlab/set_dynare_random_generator_state.m index 8854576d9e6b83d4e8716f0a9e7a2fa48ff8f845..03df1cb51fbd4e5dad80a0921d413541ccc3b5bd 100644 --- a/matlab/set_dynare_random_generator_state.m +++ b/matlab/set_dynare_random_generator_state.m @@ -6,7 +6,7 @@ function [state_u,state_n] = set_dynare_random_generator_state(state_u,state_n) % For backward compatibility, we return two vectors, but, in recent % versions of Matlab and in Octave, we return two identical vectors. % -% Copyright (C) 2010-2017 Dynare Team +% Copyright (C) 2010-2020 Dynare Team % % This file is part of Dynare. % @@ -23,14 +23,8 @@ function [state_u,state_n] = set_dynare_random_generator_state(state_u,state_n) % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <http://www.gnu.org/licenses/>. -matlab_random_streams = ~isoctave; - -if matlab_random_streams% Use new matlab interface. - if matlab_ver_less_than('7.12') - s = RandStream.getDefaultStream(); - else - s = RandStream.getGlobalStream(); - end +if ~isoctave + s = RandStream.getGlobalStream(); if isequal(s.Type,'legacy') rand('state',state_u); randn('state',state_n); @@ -47,13 +41,9 @@ if matlab_random_streams% Use new matlab interface. 'please, contact Dynare''s development team.']) end s.State = state_u; - if matlab_ver_less_than('7.12') - RandStream.setDefaultStream(s); - else - RandStream.setGlobalStream(s); - end + RandStream.setGlobalStream(s); end -else% Use old matlab interface. +else rand('state',state_u); randn('state',state_n); -end \ No newline at end of file +end diff --git a/matlab/set_dynare_seed.m b/matlab/set_dynare_seed.m index 89feb1961f28fce46a1a36fbd9290e4647f1481c..dd6b117f2f3e63f38cb9496b68c8e69640c3a3de 100644 --- a/matlab/set_dynare_seed.m +++ b/matlab/set_dynare_seed.m @@ -2,7 +2,7 @@ function set_dynare_seed(a,b) % Set seeds depending on matlab (octave) version. This routine is called in dynare_config and can be called by the % user in the mod file. % -% Copyright (C) 2010-2017 Dynare Team +% Copyright (C) 2010-2020 Dynare Team % % This file is part of Dynare. % @@ -32,20 +32,12 @@ if matlab_random_streams% Use new matlab interface. options_.DynareRandomStreams.algo = 'mt19937ar'; options_.DynareRandomStreams.seed = 0; s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed); - if matlab_ver_less_than('7.12') - reset(RandStream.setDefaultStream(s)); - else - reset(RandStream.setGlobalStream(s)); - end + reset(RandStream.setGlobalStream(s)); return end if ischar(a) && strcmpi(a,'reset') s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed); - if matlab_ver_less_than('7.12') - reset(RandStream.setDefaultStream(s)); - else - reset(RandStream.setGlobalStream(s)); - end + reset(RandStream.setGlobalStream(s)); return end if ~ischar(a) || (ischar(a) && strcmpi(a, 'clock')) @@ -56,11 +48,7 @@ if matlab_random_streams% Use new matlab interface. options_.DynareRandomStreams.seed = a; end s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed); - if matlab_ver_less_than('7.12') - reset(RandStream.setDefaultStream(s)); - else - reset(RandStream.setGlobalStream(s)); - end + reset(RandStream.setGlobalStream(s)); return end error('set_dynare_seed:: something is wrong in the calling sequence!') @@ -84,11 +72,7 @@ if matlab_random_streams% Use new matlab interface. options_.DynareRandomStreams.algo = a; options_.DynareRandomStreams.seed = b; s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed); - if matlab_ver_less_than('7.12') - reset(RandStream.setDefaultStream(s)); - else - reset(RandStream.setGlobalStream(s)); - end + reset(RandStream.setGlobalStream(s)); end else% Use old matlab interface. if nargin==1 @@ -122,4 +106,4 @@ else% Use old matlab interface. else error('set_dynare_seed:: Cannot use more than one input argument with your version of Matlab/Octave!') end -end \ No newline at end of file +end diff --git a/matlab/set_measurement_errors.m b/matlab/set_measurement_errors.m new file mode 100644 index 0000000000000000000000000000000000000000..c51d7c4772db0fa1ce37e4610bb183d83ccea85c --- /dev/null +++ b/matlab/set_measurement_errors.m @@ -0,0 +1,71 @@ +function M_ = set_measurement_errors(xparam1,estim_params_,M_) +% function M_=set_measurement_errors(xparam1,estim_params_,M_) +% Sets parameters value (except measurement errors) +% This is called for computations such as IRF and forecast +% when measurement errors aren't taken into account; in contrast to +% set_parameters.m, the global M_-structure is not altered +% +% INPUTS +% xparam1: vector of parameters to be estimated (initial values) +% M_: Dynare model-structure +% +% OUTPUTS +% M_: Dynare model-structure +% +% SPECIAL REQUIREMENTS +% none + +% Copyright (C) 2017 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/>. + +H = M_.H; +Correlation_matrix_ME = M_.Correlation_matrix_ME; + +% setting measument error variance; on the diagonal of Covariance matrix; used later +% for updating covariances +offset = estim_params_.nvx; + +if estim_params_.nvn + for i=1:estim_params_.nvn + k = estim_params_.nvn_observable_correspondence(i,1); + H(k,k) = xparam1(i+offset)^2; + end +end + +% update offset +offset = estim_params_.nvx+estim_params_.nvn+estim_params_.ncx; + +% setting measurement error covariances +if estim_params_.ncn + corrn_observable_correspondence = estim_params_.corrn_observable_correspondence; + for i=1:estim_params_.ncn + k1 = corrn_observable_correspondence(i,1); + k2 = corrn_observable_correspondence(i,2); + Correlation_matrix_ME(k1,k2) = xparam1(i+offset); + Correlation_matrix_ME(k2,k1) = Correlation_matrix_ME(k1,k2); + end +end +%build covariance matrix from correlation matrix and variances already on +%diagonal +H = diag(sqrt(diag(H)))*Correlation_matrix_ME*diag(sqrt(diag(H))); +%if calibrated covariances, set them now to their stored value +if isfield(estim_params_,'calibrated_covariances_ME') + H(estim_params_.calibrated_covariances_ME.position)=estim_params_.calibrated_covariances_ME.cov_value; +end + +M_.H = H; +M_.Correlation_matrix_ME=Correlation_matrix_ME; diff --git a/matlab/set_parameters_locally.m b/matlab/set_parameters_locally.m index 492024daa8e503ecfd913624378656662e275013..008abc3a0498192daed277ba0d586a5daf6612b3 100644 --- a/matlab/set_parameters_locally.m +++ b/matlab/set_parameters_locally.m @@ -1,6 +1,6 @@ function M_=set_parameters_locally(M_,xparam1) -% function M_out=set_parameters(M_,xparam1) +% function M_=set_parameters_locally(M_,xparam1) % Sets parameters value (except measurement errors) % This is called for computations such as IRF and forecast % when measurement errors aren't taken into account; in contrast to diff --git a/matlab/set_prior.m b/matlab/set_prior.m index a8a5c9e7e352c044aca6d8e08724d4ff114590ec..38a545661f57de8c1c6266b385b309987dfb35ca 100644 --- a/matlab/set_prior.m +++ b/matlab/set_prior.m @@ -133,9 +133,7 @@ if ncn bayestopt_.p4 = [ bayestopt_.p4; estim_params_.corrn(:,10)]; %take generalized distribution into account bayestopt_.jscale = [ bayestopt_.jscale; estim_params_.corrn(:,11)]; baseid = length(bayestopt_.name); - bayestopt_.name = [bayestopt_.name; cell(ncn, 1)]; [bayestopt_.name; cellstr([repmat('corr ',ncn,1) ... - M_.endo_names{estim_params_.corrn(:,1)} ... - repmat(', ',ncn,1) , M_.endo_names{estim_params_.corrn(:,2)}])]; + bayestopt_.name = [bayestopt_.name; cell(ncn, 1)];; for i=1:ncn k1 = estim_params_.corrn(i,1); k2 = estim_params_.corrn(i,2); diff --git a/matlab/simulated_moments_estimation.m b/matlab/simulated_moments_estimation.m deleted file mode 100644 index 4a99ec77ea55a1d07bad2459cd1591dfc6611dd8..0000000000000000000000000000000000000000 --- a/matlab/simulated_moments_estimation.m +++ /dev/null @@ -1,302 +0,0 @@ -function [param,sigma] = simulated_moments_estimation(dataset,options,parallel) -% Performs estimation by Simulated Moments Method. -% -% INPUTS: -% xparam [double] p*1 vector of initial values for the estimated parameters. -% dataset [ ] Structure describing the data set. -% options [ ] Structure defining options for SMM. -% parallel [ ] Structure defining the parallel mode settings (optional). -% -% OUTPUTS: -% param [double] p*1 vector of point estimates for the parameters. -% sigma [double] p*p covariance matrix of the SMM estimates. -% -% SPECIAL REQUIREMENTS -% The user has to provide a file where the moment conditions are defined. - -% Copyright (C) 2010-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_ options_ oo_ estim_params_ - -% Load the dataset. -eval(dataset.name); -dataset.data = []; -for v = 1:dataset.number_of_observed_variables - eval(['dataset.data = [ dataset.data , ' dataset.variables(v,:) ' ];']) -end -data = dataset.data(dataset.first_observation:dataset.first_observation+dataset.number_of_observations,:); - -% Compute sample moments and the weighting matrix. -eval(['[sample_moments,long_run_covariance] = ' M_.fname '_moments;']) -weighting_matrix = inv(long_run_covariance); - -% Initialize output. -sigma = []; -param = []; - -% Set options and initial condition. -options.estimated_parameters.list = []; -xparam = []; -if ~isempty(estim_params_.var_exo) - options.estimated_variances.idx = estim_params_.var_exo(:,1); - options.estimated_parameters.list = char(M_.exo_names(options.estimated_variances.idx)); - options.estimated_parameters.nv = rows(estim_params_.var_exo); - xparam = [xparam; estim_params_.var_exo(:,2)]; -end -if ~isempty(estim_params_.param_vals) - options.estimated_parameters.idx = estim_params_.param_vals(:,1); - if isempty(options.estimated_parameters.list) - options.estimated_parameters.list = char(M_.param_names(options.estimated_parameters.idx)); - else - options.estimated_parameters.list = char(options.estimated_parameters.list,... - M_.param_names(options.estimated_parameters.idx)); - end - options.estimated_parameters.np = rows(estim_params_.param_vals); - xparam = [xparam; estim_params_.param_vals(:,2)]; -end - -options.estimated_parameters.nb = rows(options.estimated_parameters.list); - -options.estimated_parameters.lower_bound = NaN(options.estimated_parameters.nb,1); -options.estimated_parameters.upper_bound = NaN(options.estimated_parameters.nb,1); - - -options.estimated_parameters.lower_bound = []; -options.estimated_parameters.lower_bound = [options.estimated_parameters.lower_bound; ... - estim_params_.var_exo(:,3); ... - estim_params_.param_vals(:,3) ]; -options.estimated_parameters.upper_bound = []; -options.estimated_parameters.upper_bound = [options.estimated_parameters.upper_bound; ... - estim_params_.var_exo(:,4); ... - estim_params_.param_vals(:,4) ]; - -options.number_of_simulated_sample = 0; -for i=1:length(parallel) - options.number_of_simulated_sample = options.number_of_simulated_sample + parallel(i).number_of_jobs*parallel(i).number_of_simulations; -end - -options.observed_variables_idx = dataset.observed_variables_idx; - -% Set up parallel mode if needed. -if nargin>2 - if ~isunix - error('The parallel version of SMM estimation is not implemented for non unix platforms!') - end - [~,hostname] = unix('hostname --fqdn'); - hostname = deblank(hostname); - master_is_running_a_job = 0; - for i=1:length(parallel) - if strcmpi(hostname,parallel(i).machine) - master_is_running_a_job = 1; - break - end - end - if ~master_is_running_a_job - error('Master has to run one job!'); - end - if options.optimization_routine>0 - estimated_parameters_optimization_path = [NaN;xparam]; - save('optimization_path.mat','estimated_parameters_optimization_path'); - end - skipline() - disp('Master talks to its slaves...') - skipline() - % Save the workspace. - save('master_variables.mat','options_','M_','oo_'); - % Send the workspace to each remote computer. - skipline() - for i = 1:length(parallel) - if ~strcmpi(hostname,parallel(i).machine) - unix(['scp master_variables.mat ' , parallel(i).login , '@' , parallel(i).machine , ':' parallel(i).folder]); - end - end - for i=1:length(parallel) - % Write a bash script file to execute matlab scripts in the background - fid = fopen('call_matlab_session.sh','w'); - fprintf(fid,'#!/bin/sh\n'); - fprintf(fid,'unset DISPLAY\n'); - fprintf(fid,['cd ' parallel(i).folder '\n']); - fprintf(fid,['nohup ' parallel(i).matlab '/matlab -nodesktop -nodisplay -nojvm < $1 > /dev/null 2>&1\n']); - fprintf(fid,'exit'); - fclose(fid); - % Set the permission for this file (has to be executable) - %fileattrib('call_matlab_session.sh','+x','u'); - unix(['chmod u+x call_matlab_session.sh']); - % Send the script file on each remote computer - if ~strcmpi(hostname,parallel(i).machine) - unix(['scp call_matlab_session.sh ' , parallel(i).login , '@' , parallel(i).machine , ':~/' ]); - else - unix(['cp call_matlab_session.sh ~/call_matlab_session.sh']); - end - end - % Send the files to each remote computer. - for i = 1:length(parallel) - if ~strcmpi(hostname,parallel(i).machine) - unix(['scp ' M_.fname '_steadystate.m ' , parallel(i).login , '@' , parallel(i).machine , ':' parallel(i).folder]); - unix(['scp ' M_.fname '_moments.m ' , parallel(i).login , '@' , parallel(i).machine , ':' parallel(i).folder]); - unix(['scp ' M_.fname '_static.m ' , parallel(i).login , '@' , parallel(i).machine , ':' parallel(i).folder]); - if exist([M_.fname '_dynamic.c']) - use_dll_flag = 1; - unix(['scp ' M_.fname '_dynamic.c ' , parallel(i).login , '@' , parallel(i).machine , ':' parallel(i).folder]); - else - use_dll_flag = 0; - unix(['scp ' M_.fname '_dynamic.m ' , parallel(i).login , '@' , parallel(i).machine , ':' parallel(i).folder]); - end - end - end - % If needed, compile dynamic model mex file on each remote computer - if ~strcmpi(hostname,parallel(i).machine) && use_dll_flag - % Write a matlab script that will trigger the compilation of the mex file. - fid = fopen('compile_model.m', 'w'); - fprintf(fid,[' eval(''mex -O LDFLAGS=''''-pthread -shared -Wl,--no-undefined'''' ' M_.fname '_dynamic.c'') ']); - fprintf(fid, '\n exit'); - fclose(fid); - for i = 1:length(parallel) - if ~strcmpi(hostname,parallel(i).machine) - % Send the generated matlab script to the remote computer. - unix(['scp compile_model.m ' , parallel(i).login , '@' , parallel(i).machine , ':' parallel(i).folder]); - % Compile the mex file on the remote computer. - unix(['ssh ' parallel(i).login , '@' , parallel(i).machine ' ./call_matlab_session.sh compile_model.m']); - end - end - end - % Write the matlab script files for the evaluation of the simulated moment conditions - job = 0; - for i=1:length(parallel) - for j=1:parallel(i).number_of_jobs - job = job+1; - % Create random number streams - write_job(hostname, parallel(i).machine, parallel(i).dynare, ... - options.simulated_sample_size, length(sample_moments), ... - dataset.observed_variables_idx, options.estimated_variances.idx', options.estimated_parameters.idx', options.burn_in_periods, [M_.fname '_moments'], parallel(i).number_of_simulations, ... - parallel(i).number_of_threads_per_job, job, j, options.estimated_parameters.nb, options.estimated_parameters.nv, ... - options.estimated_parameters.np); - if ~strcmpi(hostname,parallel(i).machine) - unix(['scp ' , 'job' , int2str(job) , '.m ' , parallel(i).login , '@' , parallel(i).machine , ':' parallel(i).folder ]); - end - end - end - skipline() - disp('... And slaves do as ordered.') - skipline() - if exist('intermediary_results_from_master_and_slaves','dir') - unix('rm -rf intermediary_results_from_master_and_slaves'); - end - unix('mkdir intermediary_results_from_master_and_slaves'); - unix('chmod -R u+x intermediary_results_from_master_and_slaves'); -end - -skipline() - -if options.optimization_routine==1 - % Set options for csminwel. - H0 = 1e-4*eye(options.estimated_parameters.nb); - ct = 1e-4; - it = 1000; - vb = 2; - % Minimization of the objective function. - if nargin==2 - [fval,param,grad,hessian_csminwel,itct,fcount,retcodehat] = ... - csminwel1('smm_objective',xparam,H0,[],ct,it,2,options_.gradient_epsilon,sample_moments,weighting_matrix,options); - elseif nargin>2 - [fval,param,grad,hessian_csminwel,itct,fcount,retcodehat] = ... - csminwel1('smm_objective',xparam,H0,[],ct,it,2,options_.gradient_epsilon,sample_moments,weighting_matrix,options,parallel); - end -elseif options.optimization_routine==2 - optim_options = optimset('display','iter','MaxFunEvals',1000000,'MaxIter',6000,'TolFun',1e-4,'TolX',1e-4); - if isfield(options_,'optim_opt') - eval(['optim_options = optimset(optim_options,' options_.optim_opt ');']); - end - if nargin==2 - [param,fval,exitflag] = fminsearch('smm_objective',xparam,optim_options,sample_moments,weighting_matrix,options); - else - [param,fval,exitflag] = fminsearch('smm_objective',xparam,optim_options,sample_moments,weighting_matrix,options,parallel); - end -elseif options.optimization_routine==0% Compute the variance of the SMM estimator - load('optimization_path.mat'); - tmp = sortrows(estimated_parameters_optimization_path',1); - param = tmp(1,2:end)'; - % Compute gradient of the moment function (distance between sample and simulated moments). - [F,G] = dynare_gradient('moment_function',param,options_.gradient_epsilon,sample_moments,dataset,options,parallel); - V = (1+1/options.number_of_simulated_sample)*G'*long_run_covariance*G; - [param,diag(V)] -elseif options.optimization_routine<0 - T = -options.optimization_routine;% length of the simulated time series. - time_series = extended_path(oo_.steady_state,T,1); - save time_series.mat; -end - - -function write_job(hostname, remotename, dynare_path, sample_size, number_of_moments, observed_variables_idx, variance_idx, parameters_idx, burn_in_periods, moments_file_name, number_of_simulations,threads_per_job, slave_number, job_number,nb,nv,np) - -fid = fopen(['job' int2str(slave_number) '.m'],'w'); - -fprintf(fid,['% Generated by ' hostname '.\n\n']); - -if ( strcmpi(hostname,remotename) && (job_number>1) ) || ~strcmpi(hostname,remotename) - fprintf(fid,'load(''master_variables'');\n'); - fprintf(fid,'assignin(''base'',''M_'',M_);\n'); - fprintf(fid,'assignin(''base'',''oo_'',oo_);\n'); - fprintf(fid,'assignin(''base'',''options_'',options_);\n\n'); -end - -if ( strcmpi(hostname,remotename) && (job_number>1) ) || ~strcmpi(hostname,remotename) - fprintf(fid,['addpath ' dynare_path '\n']); - fprintf(fid,['dynare_config;\n\n']); -end - -fprintf(fid,['simulated_moments = zeros(' int2str(number_of_moments) ',1);\n\n']); - -fprintf(fid,'load(''estimated_parameters.mat'');\n'); -fprintf(fid,['M_.params([' num2str(parameters_idx) ']) = xparams(' int2str(nv) '+1:' int2str(nb) ');\n\n']); -fprintf(fid,'tmp = diag(M_.Sigma_e);') -fprintf(fid,['tmp([' num2str(variance_idx) ']) = xparams(1:' int2str(nv) ').^2;\n\n']); -fprintf(fid,'M_.Sigma_e = diag(tmp);') - -fprintf(fid,['stream=RandStream(''mt19937ar'',''Seed'',' int2str(slave_number) ');\n']); -if matlab_ver_less_than('7.12') - fprintf(fid,['RandStream.setDefaultStream(stream);\n\n']); -else - fprintf(fid,['RandStream.setGlobalStream(stream);\n\n']); -end - -fprintf(fid,['maxNumCompThreads(' int2str(threads_per_job) ');\n\n']); - -fprintf(fid,['for s = 1:' int2str(number_of_simulations) '\n'] ); -fprintf(fid,[' time_series = extended_path([],' int2str(sample_size) ',1);\n']); -fprintf(fid,[' data = time_series([' int2str(observed_variables_idx) '],' int2str(burn_in_periods) '+1:' int2str(sample_size) ');\n']); -fprintf(fid,[' eval(''tmp = ' moments_file_name '(data);'');\n']); -fprintf(fid,[' simulated_moments = simulated_moments + tmp;\n']); -fprintf(fid,['end\n\n']); - -fprintf(fid,['simulated_moments = simulated_moments/' int2str(number_of_simulations) ';\n']); -fprintf(fid,['save(''simulated_moments_slave_' int2str(slave_number) '.dat'',''simulated_moments'',''-ascii'');\n']); - -if ~strcmpi(hostname,remotename) - fprintf(fid,['unix(''scp simulated_moments_slave_' int2str(slave_number) '.dat ' hostname ':' pwd '/intermediary_results_from_master_and_slaves '');\n']); - fprintf(fid,['unix(''rm simulated_moments_slave_' int2str(slave_number) '.dat'');\n']); -else - fprintf(fid,['unix(''cp simulated_moments_slave_' int2str(slave_number) '.dat ' 'intermediary_results_from_master_and_slaves '');\n']); - fprintf(fid,['unix(''rm simulated_moments_slave_' int2str(slave_number) '.dat'');\n']); -end - -if ((job_number>1) && strcmpi(hostname,remotename)) || ~strcmpi(hostname,remotename) - fprintf(fid,'exit'); -end - -fclose(fid); \ No newline at end of file diff --git a/matlab/smm_objective.m b/matlab/smm_objective.m deleted file mode 100644 index 9ff0e338a88ccd0018c885085487cfe24accb47d..0000000000000000000000000000000000000000 --- a/matlab/smm_objective.m +++ /dev/null @@ -1,159 +0,0 @@ -function [r,flag] = smm_objective(xparams,sample_moments,weighting_matrix,options,parallel) -% Evaluates the objective of the Simulated Moments Method. -% -% INPUTS: -% xparams [double] p*1 vector of estimated parameters. -% sample_moments [double] n*1 vector of sample moments (n>=p). -% weighting_matrix [double] n*n symetric, positive definite matrix. -% options [ ] Structure defining options for SMM. -% parallel [ ] Structure defining the parallel mode settings (optional). -% -% OUTPUTS: -% r [double] scalar, the value of the objective function. -% junk [ ] empty matrix. -% -% SPECIAL REQUIREMENTS -% The user has to provide a file where the moment conditions are defined. - -% Copyright (C) 2010-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_ options_ oo_ -persistent mainStream mainState -persistent priorObjectiveValue - -flag = 1; - -if nargin<5 - if isempty(mainStream) - if matlab_ver_less_than('7.12') - mainStream = RandStream.getDefaultStream; - else - mainStream = RandStream.getGlobalStream; - end - mainState = mainStream.State; - else - mainStream.State = mainState; - end -end - -if isempty(priorObjectiveValue) - priorObjectiveValue = Inf; -end - -penalty = 0; -for i=1:options.estimated_parameters.nb - if ~isnan(options.estimated_parameters.upper_bound(i)) && xparams(i)>options.estimated_parameters.upper_bound(i) - penalty = penalty + (xparams(i)-options.estimated_parameters.upper_bound(i))^2; - end - if ~isnan(options.estimated_parameters.lower_bound(i)) && xparams(i)<options.estimated_parameters.lower_bound(i) - penalty = penalty + (xparams(i)-options.estimated_parameters.lower_bound(i))^2; - end -end - -if penalty>0 - flag = 0; - r = priorObjectiveValue + penalty; - return -end - -save('estimated_parameters.mat','xparams'); - -% Check for local determinacy of the deterministic steady state. -noprint = options_.noprint; options_.noprint = 1; -[~,local_determinacy_and_stability,info] = check(M_,options_,oo_); options_.noprint = noprint; -if ~local_determinacy_and_stability - r = priorObjectiveValue * (1+info(2)); - flag = 0; - return -end - -simulated_moments = zeros(size(sample_moments)); - -% Just to be sure that things don't mess up with persistent variables... -clear perfect_foresight_simulation; - -if nargin<5 - for s = 1:options.number_of_simulated_sample - time_series = extended_path([],options.simulated_sample_size,1); - data = time_series(options.observed_variables_idx,options.burn_in_periods+1:options.simulated_sample_size); - eval(['tmp = ' options.moments_file_name '(data);']) - simulated_moments = simulated_moments + tmp; - simulated_moments = simulated_moments / options.number_of_simulated_sample; - end -else% parallel mode. - if ~isunix - error('The parallel version of SMM estimation is not implemented for non unix platforms!') - end - job_number = 1;% Remark. First job is for the master. - [~,hostname] = unix('hostname --fqdn'); - hostname = deblank(hostname); - for i=1:length(parallel) - machine = deblank(parallel(i).machine); - if ~strcmpi(hostname,machine) - % For the slaves on a remote computer. - unix(['scp estimated_parameters.mat ' , parallel(i).login , '@' , machine , ':' parallel(i).folder ' > /dev/null']); - else - if ~strcmpi(pwd,parallel(i).folder) - % For the slaves on this computer but not in the same directory as the master. - unix(['cp estimated_parameters.mat ' , parallel(i).folder]); - end - end - for j=1:parallel(i).number_of_jobs - if (strcmpi(hostname,machine) && j>1) || ~strcmpi(hostname,machine) - job_number = job_number + 1; - unix(['ssh -A ' parallel(i).login '@' machine ' ./call_matlab_session.sh job' int2str(job_number) '.m &']); - end - end - end - % Finally the Master do its job - tStartMasterJob = clock; - eval('job1;') - tElapsedMasterJob = etime(clock, tStartMasterJob); - TimeLimit = tElapsedMasterJob*1.2; - % Master waits for the slaves' output... - tStart = clock; - tElapsed = 0; - while tElapsed<TimeLimit - if ( length(dir('./intermediary_results_from_master_and_slaves/simulated_moments_slave_*.dat'))==job_number ) - break - end - tElapsed = etime(clock, tStart); - end - try - tmp = zeros(length(sample_moments),1); - for i=1:job_number - simulated_moments = load(['./intermediary_results_from_master_and_slaves/simulated_moments_slave_' int2str(i) '.dat'],'-ascii'); - tmp = tmp + simulated_moments; - end - simulated_moments = tmp / job_number; - catch - r = priorObjectiveValue*1.1; - flag = 0; - return - end -end - -r = transpose(simulated_moments-sample_moments)*weighting_matrix*(simulated_moments-sample_moments); -priorObjectiveValue = r; - -if (options.optimization_routine>0) && exist('optimization_path.mat') - load('optimization_path.mat'); - new_state = [ r; xparams]; - estimated_parameters_optimization_path = [ estimated_parameters_optimization_path , new_state ]; - save('optimization_path.mat','estimated_parameters_optimization_path'); -end \ No newline at end of file diff --git a/matlab/smoother2histval.m b/matlab/smoother2histval.m index 6e1eb2c437ace4805c80bf2843f18ca7f342d89a..3ef7e438ea76cfa13285a63c267009f45ae04e5a 100644 --- a/matlab/smoother2histval.m +++ b/matlab/smoother2histval.m @@ -163,13 +163,15 @@ end % Initialize outputs if ~isfield(opts, 'outfile') % Output to M_.endo_histval - M_.endo_histval = repmat(oo_.steady_state, 1, M_.maximum_endo_lag); + M_.endo_histval = repmat(oo_.steady_state, 1, M_.maximum_lag); else % Output to a file - o = struct(); + o = dseries(); end % Handle all endogenous variables to be copied +data = zeros(M_.orig_maximum_endo_lag, length(invars)); +k = M_.orig_maximum_endo_lag - M_.maximum_endo_lag + 1: M_.orig_maximum_lag for i = 1:length(invars) if isempty(strmatch(invars{i}, M_.endo_names)) % Skip exogenous @@ -177,61 +179,68 @@ for i = 1:length(invars) end s = smoothedvars.(invars{i}); j = strmatch(invars{i}, M_.endo_names, 'exact'); - v = s((period-M_.maximum_endo_lag+1):period);% + steady_state(j); + v = s((period-M_.orig_maximum_endo_lag+1):period);% + steady_state(j); if ~isfield(opts, 'outfile') j = strmatch(outvars{i}, M_.endo_names, 'exact'); if isempty(j) error(['smoother2histval: output variable ' outvars{i} ' does not exist.']) else - M_.endo_histval(j, :) = v; + M_.endo_histval(j, :) = v(k); end else - % When saving to a file, x(-1) is in the variable called "x_" - o.([ outvars{i} '_' ]) = v; + data(:, i) = v'; end end - -% Handle auxiliary variables for lags (both on endogenous and exogenous) -for i = 1:length(M_.aux_vars) - if ~ ismember(M_.endo_names{M_.aux_vars(i).endo_index},invars) - if M_.aux_vars(i).type ~= 1 && M_.aux_vars(i).type ~= 3 - continue - end - if M_.aux_vars(i).type == 1 - % Endogenous - orig_var = M_.endo_names{M_.aux_vars(i).orig_index}; - else - % Exogenous - orig_var = M_.exo_names{M_.aux_vars(i).orig_index}; - end - [m, k] = ismember(orig_var, outvars); - if m - if ~isempty(strmatch(invars{k}, M_.endo_names)) - s = smoothedvars.(invars{k}); - else - s = smoothedshocks.(invars{k}); - end - l = M_.aux_vars(i).orig_lead_lag; - if period-M_.maximum_endo_lag+1+l < 1 - error('The period that you indicated is too small to construct initial conditions') - end - j = M_.aux_vars(i).endo_index; - v = s((period-M_.maximum_endo_lag+1+l):(period+l)); %+steady_state(j); - if ~isfield(opts, 'outfile') - M_.endo_histval(j, :) = v; - else - % When saving to a file, x(-2) is in the variable called "x_l2" - lead_lag = num2str(l); - lead_lag = regexprep(lead_lag, '-', 'l'); - o.([ orig_var '_' lead_lag ]) = v; - end - end - end +if isfield(opts, 'outfile') + o = dseries(data, '1Y', invars); end +% $$$ % Handle auxiliary variables for lags (both on endogenous and exogenous) +% $$$ for i = 1:length(M_.aux_vars) +% $$$ if ~ ismember(M_.endo_names{M_.aux_vars(i).endo_index},invars) +% $$$ if M_.aux_vars(i).type ~= 1 && M_.aux_vars(i).type ~= 3 +% $$$ continue +% $$$ end +% $$$ if M_.aux_vars(i).type == 1 +% $$$ % Endogenous +% $$$ orig_var = M_.endo_names{M_.aux_vars(i).orig_index}; +% $$$ else +% $$$ % Exogenous +% $$$ orig_var = M_.exo_names{M_.aux_vars(i).orig_index}; +% $$$ end +% $$$ [m, k] = ismember(orig_var, outvars); +% $$$ if m +% $$$ if ~isempty(strmatch(invars{k}, M_.endo_names)) +% $$$ s = smoothedvars.(invars{k}); +% $$$ else +% $$$ s = smoothedshocks.(invars{k}); +% $$$ end +% $$$ l = M_.aux_vars(i).orig_lead_lag; +% $$$ if period-M_.maximum_endo_lag+1+l < 1 +% $$$ error('The period that you indicated is too small to construct initial conditions') +% $$$ end +% $$$ j = M_.aux_vars(i).endo_index; +% $$$ v = s((period-M_.maximum_endo_lag+1+l):(period+l)); %+steady_state(j); +% $$$ if ~isfield(opts, 'outfile') +% $$$ M_.endo_histval(j, :) = v; +% $$$ else +% $$$ % When saving to a file, x(-2) is in the variable called "x_l2" +% $$$ lead_lag = num2str(l); +% $$$ lead_lag = regexprep(lead_lag, '-', 'l'); +% $$$ o.([ orig_var '_' lead_lag ]) = v; +% $$$ end +% $$$ end +% $$$ end +% $$$ end + % Finalize output if isfield(opts, 'outfile') - save(opts.outfile, '-struct', 'o') + [dir, fname, ext] = fileparts(opts.outfile); + if ~strcmp(ext,'.mat') && ~isempty(ext) + error(['smoother2hisvtval: if outfile has an extension, it must ' ... + 'be .mat']) + end + o.save([dir fname]); end end diff --git a/matlab/solve_one_boundary.m b/matlab/solve_one_boundary.m index f36f0e3488184828aa1d31ddfd04c1d424fb411c..af9b6559254036cb66925e1816dbea40a5c4549e 100644 --- a/matlab/solve_one_boundary.m +++ b/matlab/solve_one_boundary.m @@ -1,5 +1,5 @@ -function [y, info] = solve_one_boundary(fname, y, x, params, steady_state, ... - y_index_eq, nze, periods, is_linear, Block_Num, y_kmin, maxit_, solve_tolf, lambda, cutoff, stack_solve_algo, forward_backward, is_dynamic, verbose, M, options, oo) +function [y, T, oo_, info] = solve_one_boundary(fname, y, x, params, steady_state, T, ... + y_index_eq, nze, periods, is_linear, Block_Num, y_kmin, maxit_, solve_tolf, lambda, cutoff, stack_solve_algo, is_forward, is_dynamic, verbose, M, options, oo_) % Computes the deterministic simulation of a block of equation containing % lead or lag variables % @@ -10,13 +10,13 @@ function [y, info] = solve_one_boundary(fname, y, x, params, steady_state, ... % x [matrix] All the exogenous variables of the model % params [vector] All the parameters of the model % steady_state [vector] steady state of the model +% T [matrix] Temporary terms % y_index_eq [vector of int] The index of the endogenous variables of % the block % nze [integer] number of non-zero elements in the % jacobian matrix % periods [integer] number of simulation periods -% is_linear [integer] if is_linear=1 the block is linear -% if is_linear=0 the block is not linear +% is_linear [logical] whether the block is linear % Block_Num [integer] block number % y_kmin [integer] maximum number of lag in the model % maxit_ [integer] maximum number of iteration in Newton @@ -31,20 +31,18 @@ function [y, info] = solve_one_boundary(fname, y, x, params, steady_state, ... % - 2 GMRES % - 3 BicGStab % - 4 Optimal path length -% forward_backward [integer] The block has to be solve forward -% (1) or backward (0) -% is_dynamic [integer] (1) The block belong to the dynamic +% is_forward [logical] Whether the block has to be solved forward +% If false, the block is solved backward +% is_dynamic [logical] If true, the block belongs to the dynamic file % file and the oo_.deterministic_simulation field has to be uptated -% (0) The block belong to the static -% file and th oo_.deteerminstic +% If false, the block belongs to the static +% file and the oo_.detereministic_simulation % field remains unchanged -% verbose [integer] (0) iterations are not printed -% (1) iterations are printed -% indirect_call [integer] (0) direct call to the fname -% (1) indirect call via the -% local_fname wrapper +% verbose [logical] Whether iterations are to be printed +% % OUTPUTS % y [matrix] All endogenous variables of the model +% T [matrix] Temporary terms % info [integer] >=0 no error % <0 error % @@ -55,7 +53,7 @@ function [y, info] = solve_one_boundary(fname, y, x, params, steady_state, ... % none. % -% Copyright (C) 1996-2018 Dynare Team +% Copyright (C) 1996-2020 Dynare Team % % This file is part of Dynare. % @@ -74,13 +72,11 @@ function [y, info] = solve_one_boundary(fname, y, x, params, steady_state, ... Blck_size=size(y_index_eq,2); -g2 = []; -g3 = []; correcting_factor=0.01; ilu_setup.droptol=1e-10; max_resa=1e100; reduced = 0; -if forward_backward +if is_forward incr = 1; start = y_kmin+1; finish = periods+y_kmin; @@ -96,19 +92,19 @@ for it_=start:incr:finish g1=spalloc( Blck_size, Blck_size, nze); while ~(cvg==1 || iter>maxit_) if is_dynamic - [r, y, g1, g2, g3] = feval(fname, y, x, params, steady_state, it_, 0); + [r, ~, T(:, it_), g1] = feval(fname, Block_Num, dynvars_from_endo_simul(y, it_, M), x, params, steady_state, T(:, it_), it_, false); else - [r, y, g1] = feval(fname, y, x, params); + [r, ~, T, g1] = feval(fname, Block_Num, y, x, params, T); end if ~isreal(r) max_res=(-(max(max(abs(r))))^2)^0.5; else max_res=max(max(abs(r))); end - if verbose==1 + if verbose disp(['iteration : ' int2str(iter+1) ' => ' num2str(max_res) ' time = ' int2str(it_)]) if is_dynamic - disp([M.endo_names{y_index_eq} num2str([y(it_,y_index_eq)' r g1])]) + disp([M.endo_names{y_index_eq} num2str([y(y_index_eq, it_) r g1])]) else disp([M.endo_names{y_index_eq} num2str([y(y_index_eq) r g1])]) end @@ -139,7 +135,7 @@ for it_=start:incr:finish disp([' correcting_factor=' num2str(correcting_factor,'%f') ' max(Jacobian)=' num2str(full(max_factor),'%f')]) end dx = - r/(g1+correcting_factor*speye(Blck_size)); - y(it_,y_index_eq)=ya_save+lambda*dx; + y(y_index_eq, it_)=ya_save+lambda*dx; continue else if verbose @@ -156,7 +152,7 @@ for it_=start:incr:finish disp(['reducing the path length: lambda=' num2str(lambda,'%f')]) end if is_dynamic - y(it_,y_index_eq)=ya_save-lambda*dx; + y(y_index_eq, it_)=ya_save-lambda*dx; else y(y_index_eq)=ya_save-lambda*dx; end @@ -170,10 +166,10 @@ for it_=start:incr:finish end end if is_dynamic - oo_.deterministic_simulation.status = 0; + oo_.deterministic_simulation.status = false; oo_.deterministic_simulation.error = max_res; oo_.deterministic_simulation.iterations = iter; - oo_.deterministic_simulation.block(Block_Num).status = 0;% Convergency failed. + oo_.deterministic_simulation.block(Block_Num).status = false;% Convergency failed. oo_.deterministic_simulation.block(Block_Num).error = max_res; oo_.deterministic_simulation.block(Block_Num).iterations = iter; end @@ -187,52 +183,13 @@ for it_=start:incr:finish end end if is_dynamic - ya = y(it_,y_index_eq)'; + ya = y(y_index_eq, it_); else ya = y(y_index_eq); end ya_save=ya; g1a=g1; - if ~is_dynamic && options.solve_algo==0 - if verbose - disp('steady: fsolve') - end - if ~isoctave - if ~user_has_matlab_license('optimization_toolbox') - error('SOLVE_ONE_BOUNDARY: you can''t use solve_algo=0 since you don''t have MATLAB''s Optimization Toolbox') - end - end - options=optimset('fsolve'); - options.MaxFunEvals = 50000; - options.MaxIter = 2000; - options.TolFun=1e-8; - options.Display = 'iter'; - options.Jacobian = 'on'; - if ~isoctave - [yn,fval,exitval,output] = fsolve(@local_fname, y(y_index_eq), ... - options, x, params, steady_state, y, y_index_eq, fname, 0); - else - % Under Octave, use a wrapper, since fsolve() does not have a 4th arg - func = @(z) local_fname(z, x, params, steady_state, y, y_index_eq, fname, 0); - % The Octave version of fsolve does not converge when it starts from the solution - fvec = feval(func,y(y_index_eq)); - if max(abs(fvec)) >= options.solve_tolf - [yn,fval,exitval,output] = fsolve(func,y(y_index_eq),options); - else - yn = y(y_index_eq); - exitval = 3; - end - end - y(y_index_eq) = yn; - if exitval > 0 - info = 0; - else - info = -Block_Num*10; - end - elseif (~is_dynamic && options.solve_algo==2) || (is_dynamic && stack_solve_algo==4) - if verbose==1 && ~is_dynamic - disp('steady: LU + lnsrch1') - end + if is_dynamic && stack_solve_algo==4 lambda=1; stpmx = 100 ; if is_dynamic @@ -244,43 +201,27 @@ for it_=start:incr:finish g = (r'*g1)'; f = 0.5*r'*r; p = -g1\r ; - if is_dynamic - [ya,f,r,check]=lnsrch1(y(it_,:)',f,g,p,stpmax, ... - 'lnsrch1_wrapper_one_boundary',nn, ... - y_index_eq, options.solve_tolx, y_index_eq, fname, y, x, params, steady_state, it_); - dx = ya' - y(it_, :); - else - [ya,f,r,check]=lnsrch1(y,f,g,p,stpmax,fname,nn,y_index_eq, options.solve_tolx, x, ... - params, steady_state,0); - dx = ya - y(y_index_eq); - end - if is_dynamic - y(it_,:) = ya'; - else - y = ya'; - end - elseif ~is_dynamic && options.solve_algo==3 - if verbose==1 - disp('steady: csolve') - end - [yn,info] = csolve(@local_fname, y(y_index_eq),@ ... - local_fname,1e-6,500, x, params, steady_state, y, y_index_eq, fname, 1); - dx = ya - yn; - y(y_index_eq) = yn; - elseif (stack_solve_algo==1 && is_dynamic) || (stack_solve_algo==0 && is_dynamic) || (~is_dynamic && (options.solve_algo==1 || options.solve_algo==6)) - if verbose==1 && ~is_dynamic + [ya,f,r,check]=lnsrch1(ya,f,g,p,stpmax, ... + 'lnsrch1_wrapper_one_boundary',nn, ... + y_index_eq, options.solve_tolx, M.lead_lag_incidence(M.maximum_endo_lag+1, :), fname, Block_Num, dynvars_from_endo_simul(y, it_, M), x, params, steady_state, T(:, it_), it_); + %% Recompute temporary terms, since they are not given as output of lnsrch1 + [~, ~, T(:, it_)] = feval(fname, Block_Num, dynvars_from_endo_simul(y, it_, M), x, params, steady_state, T(:, it_), it_, false); + dx = ya' - y(index_eq, it_); + y(index_eq, it_) = ya; + elseif (is_dynamic && (stack_solve_algo==1 || stack_solve_algo==0)) || (~is_dynamic && options.solve_algo==6) + if verbose && ~is_dynamic disp('steady: Sparse LU ') end dx = g1\r; ya = ya - lambda*dx; if is_dynamic - y(it_,y_index_eq) = ya'; + y(y_index_eq, it_) = ya; else y(y_index_eq) = ya; end elseif (stack_solve_algo==2 && is_dynamic) || (options.solve_algo==7 && ~is_dynamic) flag1=1; - if verbose == 1 && ~is_dynamic + if verbose && ~is_dynamic disp('steady: GMRES ') end while flag1>0 @@ -301,7 +242,7 @@ for it_=start:incr:finish else ya = ya + lambda*dx; if is_dynamic - y(it_,y_index_eq) = ya'; + y(y_index_eq, it_) = ya; else y(y_index_eq) = ya'; end @@ -309,22 +250,22 @@ for it_=start:incr:finish end elseif (stack_solve_algo==3 && is_dynamic) || (options.solve_algo==8 && ~is_dynamic) flag1=1; - if verbose == 1 && ~is_dynamic + if verbose && ~is_dynamic disp('steady: BiCGStab') end while flag1>0 [L1, U1]=ilu(g1,ilu_setup); phat = ya - U1 \ (L1 \ r); if is_dynamic - y(it_,y_index_eq) = phat; + y(y_index_eq, it_) = phat; else y(y_index_eq) = phat; end if is_dynamic - [r, y, g1, g2, g3] = feval(fname, y, x, params, ... - steady_state, it_, 0); + [r, ~, T(:, it_), g1] = feval(fname, Block_Num, dynvars_from_endo_simul(y, it_, M), x, params, ... + steady_state, T(:, it_), it_, false); else - [r, y, g1] = feval(fname, y, x, params); + [r, ~, T, g1] = feval(fname, Block_Num, y, x, params, T); end if max(abs(r))>=options.solve_tolf [dx,flag1] = bicgstab(g1,-r,1e-7,Blck_size,L1,U1); @@ -347,7 +288,7 @@ for it_=start:incr:finish else ya = ya + lambda*dx; if is_dynamic - y(it_,y_index_eq) = ya'; + y(y_index_eq, it_) = ya; else y(y_index_eq) = ya'; end @@ -377,11 +318,11 @@ for it_=start:incr:finish fprintf('Error in simul: Convergence not achieved in block %d, at time %d, after %d iterations.\n Increase "options_.simul.maxit" or set "cutoff=0" in model options.\n',Block_Num, it_,iter); end end - if(is_dynamic) - oo_.deterministic_simulation.status = 0; + if is_dynamic + oo_.deterministic_simulation.status = false; oo_.deterministic_simulation.error = max_res; oo_.deterministic_simulation.iterations = iter; - oo_.deterministic_simulation.block(Block_Num).status = 0;% Convergency failed. + oo_.deterministic_simulation.block(Block_Num).status = false;% Convergency failed. oo_.deterministic_simulation.block(Block_Num).error = max_res; oo_.deterministic_simulation.block(Block_Num).iterations = iter; end @@ -392,19 +333,12 @@ end if is_dynamic info = 1; - oo_.deterministic_simulation.status = 1; + oo_.deterministic_simulation.status = true; oo_.deterministic_simulation.error = max_res; oo_.deterministic_simulation.iterations = iter; - oo_.deterministic_simulation.block(Block_Num).status = 1; + oo_.deterministic_simulation.block(Block_Num).status = true; oo_.deterministic_simulation.block(Block_Num).error = max_res; oo_.deterministic_simulation.block(Block_Num).iterations = iter; else info = 0; end - -function [err, G]=local_fname(yl, x, params, steady_state, y, y_index_eq, fname, is_csolve) -y(y_index_eq) = yl; -[err, y, G] = feval(fname, y, x, params, steady_state, 0); -if(is_csolve) - G = full(G); -end \ No newline at end of file diff --git a/matlab/solve_two_boundaries.m b/matlab/solve_two_boundaries.m index 3cfb7626b23e324dd401aafa6a8528c53eada660..d1c595f6b6e21d6f117d51dbd9db144fdf7e9995 100644 --- a/matlab/solve_two_boundaries.m +++ b/matlab/solve_two_boundaries.m @@ -1,4 +1,4 @@ -function [y, oo]= solve_two_boundaries(fname, y, x, params, steady_state, y_index, nze, periods, y_kmin_l, y_kmax_l, is_linear, Block_Num, y_kmin, maxit_, solve_tolf, lambda, cutoff, stack_solve_algo,options,M, oo) +function [y, T, oo]= solve_two_boundaries(fname, y, x, params, steady_state, T, y_index, nze, periods, y_kmin_l, y_kmax_l, is_linear, Block_Num, y_kmin, maxit_, solve_tolf, lambda, cutoff, stack_solve_algo,options,M, oo) % Computes the deterministic simulation of a block of equation containing % both lead and lag variables using relaxation methods % @@ -9,6 +9,7 @@ function [y, oo]= solve_two_boundaries(fname, y, x, params, steady_state, y_inde % x [matrix] All the exogenous variables of the model % params [vector] All the parameters of the model % steady_state [vector] steady state of the model +% T [matrix] Temporary terms % y_index [vector of int] The index of the endogenous variables of % the block % nze [integer] number of non-zero elements in the @@ -16,8 +17,7 @@ function [y, oo]= solve_two_boundaries(fname, y, x, params, steady_state, y_inde % periods [integer] number of simulation periods % y_kmin_l [integer] maximum number of lag in the block % y_kmax_l [integer] maximum number of lead in the block -% is_linear [integer] if is_linear=1 the block is linear -% if is_linear=0 the block is not linear +% is_linear [logical] Whether the block is linear % Block_Num [integer] block number % y_kmin [integer] maximum number of lag in the model % maxit_ [integer] maximum number of iteration in Newton @@ -37,6 +37,7 @@ function [y, oo]= solve_two_boundaries(fname, y, x, params, steady_state, y_inde % % OUTPUTS % y [matrix] All endogenous variables of the model +% T [matrix] Temporary terms % oo [structure] Results % % ALGORITHM @@ -46,7 +47,7 @@ function [y, oo]= solve_two_boundaries(fname, y, x, params, steady_state, y_inde % none. % -% Copyright (C) 1996-2018 Dynare Team +% Copyright (C) 1996-2020 Dynare Team % % This file is part of Dynare. % @@ -68,8 +69,6 @@ verbose = options.verbosity; cvg=0; iter=0; Per_u_=0; -g2 = []; -g3 = []; Blck_size=size(y_index,2); correcting_factor=0.01; ilu_setup.droptol=1e-10; @@ -79,23 +78,33 @@ ilu_setup.milu = 'off'; ilu_setup.thresh = 1; ilu_setup.udiag = 0; max_resa=1e100; -Jacobian_Size=Blck_size*(y_kmin+y_kmax_l +periods); -g1=spalloc( Blck_size*periods, Jacobian_Size, nze*periods); reduced = 0; while ~(cvg==1 || iter>maxit_) - [r, y, g1, g2, g3, b]=feval(fname, y, x, params, steady_state, periods, 0, y_kmin, Blck_size,options.periods); + r = NaN(Blck_size, periods); + g1a = spalloc(Blck_size*periods, Blck_size*periods, nze*periods); + for it_ = y_kmin+(1:periods) + [r(:, it_-y_kmin), ~, T(:, it_), g1]=feval(fname, Block_Num, dynvars_from_endo_simul(y, it_, M), x, params, steady_state, T(:, it_), it_, false); + if periods == 1 + g1a = g1(:, Blck_size+(1:Blck_size)); + elseif it_ == y_kmin+1 + g1a(1:Blck_size, 1:Blck_size*2) = g1(:, Blck_size+1:end); + elseif it_ == y_kmin+periods + g1a((periods-1)*Blck_size+1:end, (periods-2)*Blck_size+1:end) = g1(:, 1:2*Blck_size); + else + g1a((it_-y_kmin-1)*Blck_size+(1:Blck_size), (it_-y_kmin-2)*Blck_size+(1:3*Blck_size)) = g1; + end + end preconditioner = 2; - g1a=g1(:, y_kmin*Blck_size+1:(periods+y_kmin)*Blck_size); - term1 = g1(:, 1:y_kmin_l*Blck_size)*reshape(y(1+y_kmin-y_kmin_l:y_kmin,y_index)',1,y_kmin_l*Blck_size)'; - term2 = g1(:, (periods+y_kmin_l)*Blck_size+1:(periods+y_kmin_l+y_kmax_l)*Blck_size)*reshape(y(periods+y_kmin+1:periods+y_kmin+y_kmax_l,y_index)',1,y_kmax_l*Blck_size)'; - b = b - term1 - term2; + ya = reshape(y(y_index, y_kmin+(1:periods)), 1, periods*Blck_size)'; + ra = reshape(r, periods*Blck_size, 1); + b=-ra+g1a*ya; [max_res, max_indx]=max(max(abs(r'))); if ~isreal(r) max_res = (-max_res^2)^0.5; end if ~isreal(max_res) || isnan(max_res) cvg = 0; - elseif(is_linear && iter>0) + elseif is_linear && iter>0 cvg = 1; else cvg=(max_res<solve_tolf); @@ -121,8 +130,8 @@ while ~(cvg==1 || iter>maxit_) disp([' trying to correct the Jacobian matrix:']); disp([' correcting_factor=' num2str(correcting_factor,'%f') ' max(Jacobian)=' num2str(full(max_factor),'%f')]); end - dx = (g1aa+correcting_factor*speye(periods*Blck_size))\ba- ya; - y(1+y_kmin:periods+y_kmin,y_index)=reshape((ya_save+lambda*dx)',length(y_index),periods)'; + dx = (g1aa+correcting_factor*speye(periods*Blck_size))\ba- ya_save; + y(y_index, y_kmin+(1:periods))=reshape((ya_save+lambda*dx)',length(y_index),periods); continue else disp('The singularity of the jacobian matrix could not be corrected'); @@ -135,7 +144,7 @@ while ~(cvg==1 || iter>maxit_) if verbose disp(['reducing the path length: lambda=' num2str(lambda,'%f')]); end - y(1+y_kmin:periods+y_kmin,y_index)=reshape((ya_save+lambda*dx)',length(y_index),periods)'; + y(y_index, y_kmin+(1:periods))=reshape((ya_save+lambda*dx)',length(y_index),periods); continue else if verbose @@ -145,10 +154,10 @@ while ~(cvg==1 || iter>maxit_) fprintf('Error in simul: Convergence not achieved in block %d, after %d iterations.\n Increase "options_.simul.maxit" or set "cutoff=0" in model options.\n',Block_Num, iter); end end - oo.deterministic_simulation.status = 0; + oo.deterministic_simulation.status = false; oo.deterministic_simulation.error = max_res; oo.deterministic_simulation.iterations = iter; - oo.deterministic_simulation.block(Block_Num).status = 0;% Convergency failed. + oo.deterministic_simulation.block(Block_Num).status = false;% Convergency failed. oo.deterministic_simulation.block(Block_Num).error = max_res; oo.deterministic_simulation.block(Block_Num).iterations = iter; return @@ -159,7 +168,6 @@ while ~(cvg==1 || iter>maxit_) end end end - ya = reshape(y(y_kmin+1:y_kmin+periods,y_index)',1,periods*Blck_size)'; ya_save=ya; g1aa=g1a; ba=b; @@ -167,7 +175,7 @@ while ~(cvg==1 || iter>maxit_) if stack_solve_algo==0 dx = g1a\b- ya; ya = ya + lambda*dx; - y(1+y_kmin:periods+y_kmin,y_index)=reshape(ya',length(y_index),periods)'; + y(y_index, y_kmin+(1:periods))=reshape(ya',length(y_index),periods); elseif stack_solve_algo==1 for t=1:periods first_elem = (t-1)*Blck_size+1; @@ -205,7 +213,7 @@ while ~(cvg==1 || iter>maxit_) y_Elem = Blck_size * (t-1)+1:Blck_size * (t); dx(y_Elem) = za - ya(y_Elem); ya(y_Elem) = ya(y_Elem) + lambda*dx(y_Elem); - y(y_kmin + t, y_index) = ya(y_Elem); + y(y_index, y_kmin + t) = ya(y_Elem); end elseif stack_solve_algo==2 flag1=1; @@ -253,7 +261,7 @@ while ~(cvg==1 || iter>maxit_) else dx = za - ya; ya = ya + lambda*dx; - y(1+y_kmin:periods+y_kmin,y_index)=reshape(ya',length(y_index),periods)'; + y(y_index, y_kmin+(1:periods))=reshape(ya',length(y_index),periods); end end elseif stack_solve_algo==3 @@ -296,20 +304,19 @@ while ~(cvg==1 || iter>maxit_) else dx = za - ya; ya = ya + lambda*dx; - y(1+y_kmin:periods+y_kmin,y_index)=reshape(ya',length(y_index),periods)'; + y(y_index, y_kmin+(1:periods))=reshape(ya',length(y_index),periods); end end elseif stack_solve_algo==4 - ra = reshape(r(:, y_kmin+1:periods+y_kmin),periods*Blck_size, 1); stpmx = 100 ; stpmax = stpmx*max([sqrt(ya'*ya);size(y_index,2)]); nn=1:size(ra,1); g = (ra'*g1a)'; f = 0.5*ra'*ra; p = -g1a\ra; - [yn,f,ra,check]=lnsrch1(ya,f,g,p,stpmax,'lnsrch1_wrapper_two_boundaries',nn,nn, options.solve_tolx, fname, y, y_index,x, params, steady_state, periods, y_kmin, Blck_size,options.periods); + [yn,f,ra,check]=lnsrch1(ya,f,g,p,stpmax,'lnsrch1_wrapper_two_boundaries',nn,nn, options.solve_tolx, fname, Block_Num, y, y_index,x, params, steady_state, T, periods, Blck_size, M); dx = ya - yn; - y(1+y_kmin:periods+y_kmin,y_index)=reshape(yn',length(y_index),periods)'; + y(y_index, y_kmin+(1:periods))=reshape(yn',length(y_index),periods); end end iter=iter+1; @@ -323,18 +330,18 @@ if (iter>maxit_) printline(41) %disp(['No convergence after ' num2str(iter,'%4d') ' iterations in Block ' num2str(Block_Num,'%d')]) end - oo.deterministic_simulation.status = 0; + oo.deterministic_simulation.status = false; oo.deterministic_simulation.error = max_res; oo.deterministic_simulation.iterations = iter; - oo.deterministic_simulation.block(Block_Num).status = 0;% Convergency failed. + oo.deterministic_simulation.block(Block_Num).status = false;% Convergency failed. oo.deterministic_simulation.block(Block_Num).error = max_res; oo.deterministic_simulation.block(Block_Num).iterations = iter; return end -oo.deterministic_simulation.status = 1; +oo.deterministic_simulation.status = true; oo.deterministic_simulation.error = max_res; oo.deterministic_simulation.iterations = iter; -oo.deterministic_simulation.block(Block_Num).status = 1;% Convergency obtained. +oo.deterministic_simulation.block(Block_Num).status = true;% Convergency obtained. oo.deterministic_simulation.block(Block_Num).error = max_res; -oo.deterministic_simulation.block(Block_Num).iterations = iter; \ No newline at end of file +oo.deterministic_simulation.block(Block_Num).iterations = iter; diff --git a/matlab/steady_.m b/matlab/steady_.m index a689d46db8610d34999949e83dcb8a40e0f7e530..08890ee61eee3eebf47b26246bcaf7476832ce95 100644 --- a/matlab/steady_.m +++ b/matlab/steady_.m @@ -18,7 +18,7 @@ function [steady_state,params,info] = steady_(M_,options_,oo_) % SPECIAL REQUIREMENTS % none -% Copyright (C) 2001-2017 Dynare Team +% Copyright (C) 2001-2020 Dynare Team % % This file is part of Dynare. % @@ -40,8 +40,8 @@ if options_.solve_algo < 0 || options_.solve_algo > 12 end if ~options_.bytecode && ~options_.block && options_.solve_algo > 4 && ... - options_.solve_algo < 10 - error('STEADY: you can''t use solve_algo > 4 without block nor bytecode options') + options_.solve_algo < 9 + error('STEADY: you can''t use solve_algo = {5,6,7,8} without block nor bytecode options') end if ~options_.bytecode && options_.block && options_.solve_algo == 5 @@ -52,4 +52,4 @@ if isoctave && options_.solve_algo == 11 error(['SIMUL: you can''t use solve_algo = %u under Octave'],options_.solve_algo) end -[steady_state,params,info] = evaluate_steady_state(oo_.steady_state,M_,options_,oo_,~options_.steadystate.nocheck); \ No newline at end of file +[steady_state,params,info] = evaluate_steady_state(oo_.steady_state,M_,options_,oo_,~options_.steadystate.nocheck); diff --git a/matlab/stoch_simul.m b/matlab/stoch_simul.m index 3418770fd5d42b5fcb635e3710fb73dda7718aed..ce517a8af738f32a09e1cfbc04cc0306432f6aeb 100644 --- a/matlab/stoch_simul.m +++ b/matlab/stoch_simul.m @@ -124,7 +124,7 @@ if ~options_.noprint lh = cellofchararraymaxlength(labels)+2; dyn_latex_table(M_, options_, my_title, 'covar_ex_shocks', headers, labels, M_.Sigma_e, lh, 10, 6); end - if ~all(M_.H==0) + if ~all(diag(M_.H)==0) my_title='MATRIX OF COVARIANCE OF MEASUREMENT ERRORS'; labels = cellfun(@(x) horzcat('SE_', x), options_.varobs, 'UniformOutput', false); headers = vertcat('Variables', labels); @@ -189,9 +189,11 @@ if ~options_.nomoments if PI_PCL_solver PCL_Part_info_moments(0, PCL_varobs, oo_.dr, i_var); elseif options_.periods == 0 - % There is no code for theoretical moments at 3rd order if options_.order <= 2 oo_=disp_th_moments(oo_.dr,var_list,M_,options_,oo_); + elseif options_.order == 3 && options_.pruning + % There is no code for theoretical moments at 3rd order without pruning + oo_=disp_th_moments_order3(oo_.dr,M_,options_,i_var,oo_); end else oo_=disp_moments(oo_.endo_simul,var_list,M_,options_,oo_); @@ -292,14 +294,15 @@ if options_.irf hold off xlim([1 options_.irf]); remove_fractional_xticks; - title(deblank(mylist(j,:)),'Interpreter','none'); + if TeX + title(['$' deblank(mylistTeX(j,:)) '$'],'Interpreter','latex'); + else + title(deblank(mylist(j,:)),'Interpreter','none'); + end end dyn_saveas(hh,[M_.fname, '/graphs/' M_.fname '_IRF_' tit{i}],options_.nodisplay,options_.graph_format); if TeX && any(strcmp('eps',cellstr(options_.graph_format))) fprintf(fidTeX,'\\begin{figure}[H]\n'); - for j = 1:number_of_plots_to_draw - fprintf(fidTeX,'\\psfrag{%s}[1][][0.5][0]{$%s$}\n',deblank(mylist(j,:)),deblank(mylistTeX(j,:))); - end fprintf(fidTeX,'\\centering \n'); fprintf(fidTeX,'\\includegraphics[width=%2.2f\\textwidth]{%s_IRF_%s}\n',options_.figures.textwidth*min(j/nc,1),[M_.fname, '/graphs/' M_.fname],tit{i}); fprintf(fidTeX,'\\caption{Impulse response functions (orthogonalized shock to $%s$).}\n',titTeX{i}); @@ -324,14 +327,15 @@ if options_.irf hold off xlim([1 options_.irf]); remove_fractional_xticks - title(deblank(mylist((fig-1)*nstar+plt,:)),'Interpreter','none'); + if TeX + title(['$' deblank(mylistTeX((fig-1)*nstar+plt,:)) '$'],'Interpreter','latex'); + else + title(deblank(mylist((fig-1)*nstar+plt,:)),'Interpreter','none'); + end end dyn_saveas(hh,[M_.fname, '/graphs/' M_.fname '_IRF_' tit{i} int2str(fig)],options_.nodisplay,options_.graph_format); if TeX && any(strcmp('eps',cellstr(options_.graph_format))) fprintf(fidTeX,'\\begin{figure}[H]\n'); - for j = 1:nstar - fprintf(fidTeX,'\\psfrag{%s}[1][][0.5][0]{$%s$}\n',deblank(mylist((fig-1)*nstar+j,:)),deblank(mylistTeX((fig-1)*nstar+j,:))); - end fprintf(fidTeX,'\\centering \n'); fprintf(fidTeX,'\\includegraphics[width=%2.2f\\textwidth]{%s_IRF_%s%s}\n',options_.figures.textwidth*min(plt/nc,1),[M_.fname, '/graphs/' M_.fname],tit{i},int2str(fig)); if options_.relative_irf @@ -355,14 +359,15 @@ if options_.irf hold off xlim([1 options_.irf]); remove_fractional_xticks - title(deblank(mylist((nbplt-1)*nstar+plt,:)),'Interpreter','none'); + if TeX + title(['$' deblank(mylistTeX((nbplt-1)*nstar+plt,:)) '$'],'Interpreter','latex'); + else + title(deblank(mylist((nbplt-1)*nstar+plt,:)),'Interpreter','none'); + end end dyn_saveas(hh,[M_.fname, '/graphs/' M_.fname '_IRF_' tit{i} int2str(nbplt) ],options_.nodisplay,options_.graph_format); if TeX && any(strcmp('eps',cellstr(options_.graph_format))) fprintf(fidTeX,'\\begin{figure}[H]\n'); - for j = 1:m - fprintf(fidTeX,'\\psfrag{%s}[1][][0.5][0]{$%s$}\n',deblank(mylist((nbplt-1)*nstar+j,:)),deblank(mylistTeX((nbplt-1)*nstar+j,:))); - end fprintf(fidTeX,'\\centering \n'); fprintf(fidTeX,'\\includegraphics[width=%2.2f\\textwidth]{%s_IRF_%s%s}\n',options_.figures.textwidth*min(m/lc,1),[M_.fname, '/graphs/' M_.fname],tit{i},int2str(nbplt)); if options_.relative_irf diff --git a/matlab/stochastic_solvers.m b/matlab/stochastic_solvers.m index 87d88bacf128e8f6cbebe51320344cced1a7d693..85398fa94ce51372471a1cb11204ef90c68d8e11 100644 --- a/matlab/stochastic_solvers.m +++ b/matlab/stochastic_solvers.m @@ -128,11 +128,6 @@ elseif local_order == 2 exo_simul, ... M_.params, dr.ys, it_); end - if options_.use_dll - % In USE_DLL mode, the hessian is in the 3-column sparse representation - hessian1 = sparse(hessian1(:,1), hessian1(:,2), hessian1(:,3), ... - size(jacobia_, 1), size(jacobia_, 2)*size(jacobia_, 2)); - end [infrow,infcol]=find(isinf(hessian1)); if options_.debug if ~isempty(infrow) diff --git a/matlab/user_has_matlab_license.m b/matlab/user_has_matlab_license.m index 6f8b3c2261a4e90eab381f14779977586f59ad73..de5475ae06cb46153d92f638bebf5e00bc4fd88d 100644 --- a/matlab/user_has_matlab_license.m +++ b/matlab/user_has_matlab_license.m @@ -11,7 +11,7 @@ function [hasLicense] = user_has_matlab_license(toolbox) % SPECIAL REQUIREMENTS % none -% Copyright (C) 2012-2017 Dynare Team +% Copyright (C) 2012-2020 Dynare Team % % This file is part of Dynare. % @@ -28,11 +28,8 @@ function [hasLicense] = user_has_matlab_license(toolbox) % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <http://www.gnu.org/licenses/>. -if matlab_ver_less_than('7.12') - hasLicense = license('test', toolbox); -else - [hasLicense, ~] = license('checkout',toolbox); -end +[hasLicense, ~] = license('checkout',toolbox); + if ~hasLicense return end @@ -68,4 +65,4 @@ else hasInstallation=1; end end -end \ No newline at end of file +end diff --git a/matlab/utilities/general/isestimation.m b/matlab/utilities/general/isestimation.m deleted file mode 100644 index 9e17d4b03a70a847067ba735e87f69f51df1f434..0000000000000000000000000000000000000000 --- a/matlab/utilities/general/isestimation.m +++ /dev/null @@ -1,26 +0,0 @@ -function b = isestimation(option) - -% Returns true if we are currently estimating a model. - -% Copyright (C) 2017 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/>. - -b = false; - -if ischar(option.mode_compute) || option.mode_compute || option.mh_replic - b = true; -end \ No newline at end of file diff --git a/matlab/utilities/general/isinestimationobjective.m b/matlab/utilities/general/isinestimationobjective.m index 023e4cb14e77c29bffbeff747d3003ae15425651..690a186691fb96e8329539cc948efb48bb6d8c3a 100644 --- a/matlab/utilities/general/isinestimationobjective.m +++ b/matlab/utilities/general/isinestimationobjective.m @@ -2,7 +2,7 @@ function b = isinestimationobjective() % Returns true iff the caller function is called by a routine defining the objective of an estimation algorithm. -% Copyright (C) 2014-2017 Dynare Team +% Copyright (C) 2014-2020 Dynare Team % % This file is part of Dynare. % @@ -26,7 +26,6 @@ list_of_previously_called_routines = transpose(tmp(2,:)); if ~isempty(strmatch('dsge_likelihood', list_of_previously_called_routines, 'exact')) || ... ~isempty(strmatch('dsge_var_likelihood', list_of_previously_called_routines, 'exact')) || ... - ~isempty(strmatch('non_linear_dsge_likelihood', list_of_previously_called_routines, 'exact')) || ... - ~isempty(strmatch('simulated_moments_estimation', list_of_previously_called_routines, 'exact')) + ~isempty(strmatch('non_linear_dsge_likelihood', list_of_previously_called_routines, 'exact')) b = true; end diff --git a/matlab/utilities/tests b/matlab/utilities/tests index 371f3f0e6ec209b4277a2fd7911ccf80570d7473..bc4df1c1ec89a338424b5eb617fa568a76f14b45 160000 --- a/matlab/utilities/tests +++ b/matlab/utilities/tests @@ -1 +1 @@ -Subproject commit 371f3f0e6ec209b4277a2fd7911ccf80570d7473 +Subproject commit bc4df1c1ec89a338424b5eb617fa568a76f14b45 diff --git a/matlab/ver_greater_than.m b/matlab/ver_greater_than.m deleted file mode 100644 index 7bd973f45befa4d3d0c873456b5f2f78107a07db..0000000000000000000000000000000000000000 --- a/matlab/ver_greater_than.m +++ /dev/null @@ -1,65 +0,0 @@ -function tf = ver_greater_than(ver1, ver2) -%function tf = ver_greater_than(ver1, ver2) -% ver1 > ver2 ? 1 : 0; -% -% INPUTS -% ver1 [string] software version number -% ver2 [string] software version number -% -% OUTPUTS -% tf [bool] true if ver1 > ver2 -% -% SPECIAL REQUIREMENTS -% none - -% Copyright (C) 2015-2017 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/>. - -tf = true; -ver1 = strsplit(ver1, {'.', '-'}); -ver2 = strsplit(ver2, {'.', '-'}); - -maj_ver1 = str2double(ver1{1}); -maj_ver2 = str2double(ver2{1}); -if maj_ver1 > maj_ver2 - return -end - -min_ver1 = str2double(ver1{2}); -min_ver2 = str2double(ver2{2}); -if (maj_ver1 == maj_ver2) && (min_ver1 > min_ver2) - return -end - -if (length(ver1) == length(ver2) && length(ver1) == 3) - ismaster1 = isnan(str2double(ver1{3})); - ismaster2 = isnan(str2double(ver2{3})); - if (maj_ver1 == maj_ver2) && (min_ver1 == min_ver2) && (ismaster1 && ~ismaster2) - return - end - - if ~ismaster1 && ~ismaster2 - rev_ver1 = str2double(ver1{3}); - rev_ver2 = str2double(ver2{3}); - if (maj_ver1 == maj_ver2) && (min_ver1 == min_ver2) && (rev_ver1 > rev_ver2) - return - end - end -end - -tf = false; -end diff --git a/matlab/ver_greater_than_equal.m b/matlab/ver_greater_than_equal.m deleted file mode 100644 index 84c54040a4ca4a946395aa2e8733bb9975dbcde4..0000000000000000000000000000000000000000 --- a/matlab/ver_greater_than_equal.m +++ /dev/null @@ -1,33 +0,0 @@ -function tf = ver_greater_than_equal(ver1, ver2) -%function tf = ver_greater_than_equal(ver1, ver2) -% ver1 >= ver2 ? 1 : 0; -% -% INPUTS -% ver1 [string] software version number -% ver2 [string] software version number -% -% OUTPUTS -% tf [bool] true if ver1 > ver2 -% -% SPECIAL REQUIREMENTS -% none - -% Copyright (C) 2015 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/>. - -tf = ver_greater_than(ver1, ver2) || strcmp(ver1, ver2); -end \ No newline at end of file diff --git a/matlab/ver_less_than.m b/matlab/ver_less_than.m deleted file mode 100644 index c71d163bee7000433fa2a8b23bc204274f7d953d..0000000000000000000000000000000000000000 --- a/matlab/ver_less_than.m +++ /dev/null @@ -1,65 +0,0 @@ -function tf = ver_less_than(ver1, ver2) -%function tf = ver_less_than(ver1, ver2) -% ver1 < ver2 ? 1 : 0; -% -% INPUTS -% ver1 [string] software version number -% ver2 [string] software version number -% -% OUTPUTS -% tf [bool] true if ver1 < ver2 -% -% SPECIAL REQUIREMENTS -% none - -% Copyright (C) 2015-2017 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/>. - -tf = true; -ver1 = strsplit(ver1, {'.', '-'}); -ver2 = strsplit(ver2, {'.', '-'}); - -maj_ver1 = str2double(ver1{1}); -maj_ver2 = str2double(ver2{1}); -if maj_ver1 < maj_ver2 - return -end - -min_ver1 = str2double(ver1{2}); -min_ver2 = str2double(ver2{2}); -if (maj_ver1 == maj_ver2) && (min_ver1 < min_ver2) - return -end - -if (length(ver1) == length(ver2) && length(ver1) == 3) - ismaster1 = isnan(str2double(ver1{3})); - ismaster2 = isnan(str2double(ver2{3})); - if (maj_ver1 == maj_ver2) && (min_ver1 == min_ver2) && (~ismaster1 && ismaster2) - return - end - - if ~ismaster1 && ~ismaster2 - rev_ver1 = str2double(ver1{3}); - rev_ver2 = str2double(ver2{3}); - if (maj_ver1 == maj_ver2) && (min_ver1 == min_ver2) && (rev_ver1 < rev_ver2) - return - end - end -end - -tf = false; -end diff --git a/mex/build/disclyap_fast.am b/mex/build/disclyap_fast.am new file mode 100644 index 0000000000000000000000000000000000000000..011d4e0eebdf386c8b8f531492425737d9eeff56 --- /dev/null +++ b/mex/build/disclyap_fast.am @@ -0,0 +1,11 @@ +mex_PROGRAMS = disclyap_fast + +nodist_disclyap_fast_SOURCES = disclyap_fast.f08 matlab_mex.F08 blas_lapack.F08 + +BUILT_SOURCES = $(nodist_disclyap_fast_SOURCES) +CLEANFILES = $(nodist_disclyap_fast_SOURCES) + +disclyap_fast.o : matlab_mex.mod lapack.mod + +%.f08: $(top_srcdir)/../../sources/disclyap_fast/%.f08 + $(LN_S) -f $< $@ diff --git a/mex/build/matlab/Makefile.am b/mex/build/matlab/Makefile.am index bce39c39c4651128a226884d4e8ec0d73d0440ad..e84baee6ed388fe01510b7a2d7a63d0e045b98b4 100644 --- a/mex/build/matlab/Makefile.am +++ b/mex/build/matlab/Makefile.am @@ -1,6 +1,6 @@ ACLOCAL_AMFLAGS = -I ../../../m4 -SUBDIRS = mjdgges kronecker bytecode block_kalman_filter sobol perfect_foresight_problem num_procs block_trust_region +SUBDIRS = mjdgges kronecker bytecode block_kalman_filter sobol perfect_foresight_problem num_procs block_trust_region disclyap_fast # libdynare++ must come before gensylv, k_order_perturbation, dynare_simul_ if ENABLE_MEX_DYNAREPLUSPLUS diff --git a/mex/build/matlab/configure.ac b/mex/build/matlab/configure.ac index 95f4c5380b7aa5f995f4f760bf5b312b6f65207b..24f0b2b13ab37d68657e7ffcbba17fe1d78ca6d9 100644 --- a/mex/build/matlab/configure.ac +++ b/mex/build/matlab/configure.ac @@ -1,6 +1,6 @@ dnl Process this file with autoconf to produce a configure script. -dnl Copyright © 2009-2019 Dynare Team +dnl Copyright © 2009-2020 Dynare Team dnl dnl This file is part of Dynare. dnl @@ -35,7 +35,7 @@ if test "$ax_enable_matlab" != yes -o "$ax_matlab_version_ok" != yes -o "$ax_mex AC_MSG_ERROR([MATLAB cannot be found]) fi -AX_COMPARE_VERSION([$MATLAB_VERSION], [lt], [7.9], [AC_MSG_ERROR([Your MATLAB is too old, please upgrade to version 7.9 (R2009b) at least (or disable MATLAB support with --disable-matlab).])]) +AX_COMPARE_VERSION([$MATLAB_VERSION], [lt], [8.3], [AC_MSG_ERROR([Your MATLAB is too old, please upgrade to version 8.3 (R2014a) at least (or disable MATLAB support with --disable-matlab).])]) case ${host_os} in *cygwin*) @@ -169,10 +169,11 @@ AC_CONFIG_FILES([Makefile kalman_steady_state/Makefile ms_sbvar/Makefile block_kalman_filter/Makefile - sobol/Makefile - local_state_space_iterations/Makefile + sobol/Makefile + local_state_space_iterations/Makefile perfect_foresight_problem/Makefile num_procs/Makefile - block_trust_region/Makefile]) + block_trust_region/Makefile + disclyap_fast/Makefile]) AC_OUTPUT diff --git a/mex/build/matlab/disclyap_fast/Makefile.am b/mex/build/matlab/disclyap_fast/Makefile.am new file mode 100644 index 0000000000000000000000000000000000000000..ddf0a49b5f9a8f467aa3fb361f20037ee3bdc2ab --- /dev/null +++ b/mex/build/matlab/disclyap_fast/Makefile.am @@ -0,0 +1,2 @@ +include ../mex.am +include ../../disclyap_fast.am diff --git a/mex/build/octave/Makefile.am b/mex/build/octave/Makefile.am index a2b6fa1245a79d843723d5332cabc9aacf63b2b7..6726be71e928d36dc9cac5cb8deebed59ebf1b60 100644 --- a/mex/build/octave/Makefile.am +++ b/mex/build/octave/Makefile.am @@ -1,6 +1,6 @@ ACLOCAL_AMFLAGS = -I ../../../m4 -SUBDIRS = mjdgges kronecker bytecode block_kalman_filter sobol perfect_foresight_problem num_procs block_trust_region +SUBDIRS = mjdgges kronecker bytecode block_kalman_filter sobol perfect_foresight_problem num_procs block_trust_region disclyap_fast # libdynare++ must come before gensylv, k_order_perturbation, dynare_simul_ if ENABLE_MEX_DYNAREPLUSPLUS diff --git a/mex/build/octave/configure.ac b/mex/build/octave/configure.ac index 3d7c23536331b265d707bb67bd9bb2e327934159..cc7edaaa5f6913c91879a356394b647df5ac1262 100644 --- a/mex/build/octave/configure.ac +++ b/mex/build/octave/configure.ac @@ -37,7 +37,7 @@ AC_CANONICAL_HOST case ${host_os} in darwin*) CXXFLAGS="$($MKOCTFILE -p ALL_CXXFLAGS) -Wall -Wno-parentheses -Wold-style-cast -O2" - LDFLAGS+="$($MKOCTFILE -p OCTAVE_LIBS)" + LDFLAGS+=" $($MKOCTFILE -p OCTAVE_LIBS)" ;; esac @@ -134,10 +134,11 @@ AC_CONFIG_FILES([Makefile kalman_steady_state/Makefile ms_sbvar/Makefile block_kalman_filter/Makefile - sobol/Makefile - local_state_space_iterations/Makefile + sobol/Makefile + local_state_space_iterations/Makefile perfect_foresight_problem/Makefile num_procs/Makefile - block_trust_region/Makefile]) + block_trust_region/Makefile + disclyap_fast/Makefile]) AC_OUTPUT diff --git a/mex/build/octave/disclyap_fast/Makefile.am b/mex/build/octave/disclyap_fast/Makefile.am new file mode 100644 index 0000000000000000000000000000000000000000..929f34ace63ebf01030942b1a333801c3ec98ea3 --- /dev/null +++ b/mex/build/octave/disclyap_fast/Makefile.am @@ -0,0 +1,3 @@ +EXEEXT = .mex +include ../mex.am +include ../../disclyap_fast.am diff --git a/mex/sources/Makefile.am b/mex/sources/Makefile.am index fe1c951371773f33837ce54cb3876fb085e60c80..3fca510decf6c3826ff2a9ea512e84e32d251b9a 100644 --- a/mex/sources/Makefile.am +++ b/mex/sources/Makefile.am @@ -19,7 +19,8 @@ EXTRA_DIST = \ dynare_simul_ \ perfect_foresight_problem \ num_procs \ - block_trust_region + block_trust_region \ + disclyap_fast clean-local: rm -rf `find mex/sources -name *.o` diff --git a/mex/sources/blas_lapack.F08 b/mex/sources/blas_lapack.F08 index a93aff118b738bfc23d200e9cd3ecbe7dbdda30a..dfbff5cb8e0228f52ff74423d17a62e085dd8cdb 100644 --- a/mex/sources/blas_lapack.F08 +++ b/mex/sources/blas_lapack.F08 @@ -56,7 +56,7 @@ module lapack alphar, alphai, beta, vsl, ldvsl, vsr, ldvsr, work, lwork, bwork, & info) import :: blint, bllog, real64 - character :: jobvsl, jobvsr, sort + character, intent(in) :: jobvsl, jobvsr, sort interface logical(bllog) function selctg(alphar, alphai, beta) import :: bllog, real64 @@ -70,4 +70,14 @@ module lapack integer(blint), intent(out) :: sdim, info end subroutine dgges end interface + + interface + subroutine dpotrf(uplo, n, a, lda, info) + import :: blint, real64 + character, intent(in) :: uplo + integer(blint), intent(in) :: n, lda + real(real64), dimension(*), intent(inout) :: a + integer(blint), intent(out) :: info + end subroutine dpotrf + end interface end module lapack diff --git a/mex/sources/bytecode/ErrorHandling.hh b/mex/sources/bytecode/ErrorHandling.hh index fbded6d6fbb16805cb6a9c66957137ca98d064d8..f9e2489d52d2c419d939661f2f146ae7fbabc605 100644 --- a/mex/sources/bytecode/ErrorHandling.hh +++ b/mex/sources/bytecode/ErrorHandling.hh @@ -1,5 +1,5 @@ /* - * Copyright © 2007-2017 Dynare Team + * Copyright © 2007-2020 Dynare Team * * This file is part of Dynare. * @@ -1789,7 +1789,7 @@ public: unsigned int nb_add_input_arguments = fc->get_nb_add_input_arguments(); function_type = fc->get_function_type(); #ifdef DEBUG - mexPrintf("function_type=%d ExternalFunctionWithoutDerivative=%d\n", function_type, ExternalFunctionWithoutDerivative); + mexPrintf("function_type=%d ExternalFunctionWithoutDerivative=%d\n", function_type, ExternalFunctionType::withoutDerivative); mexEvalString("drawnow;"); #endif mxArray **input_arguments; diff --git a/mex/sources/bytecode/Evaluate.cc b/mex/sources/bytecode/Evaluate.cc index a488a03509604077007a49bff96b66c36ba1fef7..576edff9e2f26de3e1c479e0fdae68ecc62926fa 100644 --- a/mex/sources/bytecode/Evaluate.cc +++ b/mex/sources/bytecode/Evaluate.cc @@ -1,5 +1,5 @@ /* - * Copyright © 2013-2017 Dynare Team + * Copyright © 2013-2020 Dynare Team * * This file is part of Dynare. * @@ -455,7 +455,7 @@ Evaluate::compute_block_time(const int Per_u_, const bool evaluate, /*const int //load a temporary variable in the processor var = static_cast<FLDT_ *>(it_code->second)->get_pos(); #ifdef DEBUG - mexPrintf("T[it_=%d var=%d, y_kmin=%d, y_kmax=%d == %d]=>%f\n", it_, var, y_kmin, y_kmax, var*(periods+y_kmin+y_kmax)+it_, var); + mexPrintf("FLDT T[it_=%d var=%d, y_kmin=%d, y_kmax=%d == %d]=>%f\n", it_, var, y_kmin, y_kmax, var*(periods+y_kmin+y_kmax)+it_, T[var*(periods+y_kmin+y_kmax)+it_]); tmp_out << " T[" << it_ << ", " << var << "](" << T[var*(periods+y_kmin+y_kmax)+it_] << ")"; #endif Stack.push(T[var*(periods+y_kmin+y_kmax)+it_]); @@ -783,11 +783,11 @@ Evaluate::compute_block_time(const int Per_u_, const bool evaluate, /*const int tmp << " in compute_block_time, variable " << EQN_type << " not used yet\n"; throw FatalExceptionHandling(tmp.str()); } -#ifdef DEBUG - tmp_out << "=>"; - mexPrintf(" g1[%d](%f)=%s\n", var, g1[var], tmp_out.str().c_str()); - tmp_out.str(""); -#endif +// #ifdef DEBUG +// tmp_out << "=>"; +// mexPrintf(" g1[%d](%f)=%s\n", var, g1[var], tmp_out.str().c_str()); +// tmp_out.str(""); +// #endif Stack.pop(); break; @@ -1160,7 +1160,7 @@ Evaluate::compute_block_time(const int Per_u_, const bool evaluate, /*const int unsigned int nb_add_input_arguments = fc->get_nb_add_input_arguments(); function_type = fc->get_function_type(); #ifdef DEBUG - mexPrintf("function_type=%d ExternalFunctionWithoutDerivative=%d\n", function_type, ExternalFunctionWithoutDerivative); + mexPrintf("function_type=%d ExternalFunctionWithoutDerivative=%d\n", function_type, ExternalFunctionType::withoutDerivative); mexEvalString("drawnow;"); #endif mxArray **input_arguments; diff --git a/mex/sources/bytecode/Interpreter.cc b/mex/sources/bytecode/Interpreter.cc index a9f919885afad912745764fda6f9a13ae753b9b4..a3aa0bbe32786bd12e5c2f649cd7566fb7abab41 100644 --- a/mex/sources/bytecode/Interpreter.cc +++ b/mex/sources/bytecode/Interpreter.cc @@ -604,7 +604,7 @@ Interpreter::ReadCodeFile(string file_name, CodeLoad &code) void Interpreter::check_for_controlled_exo_validity(FBEGINBLOCK_ *fb, vector<s_plan> sconstrained_extended_path) { - vector<unsigned int> exogenous = fb->get_exogenous(); + vector<int> exogenous = fb->get_exogenous(); vector<int> endogenous = fb->get_endogenous(); for (vector<s_plan>::iterator it = sconstrained_extended_path.begin(); it != sconstrained_extended_path.end(); it++) { @@ -627,8 +627,8 @@ Interpreter::check_for_controlled_exo_validity(FBEGINBLOCK_ *fb, vector<s_plan> throw FatalExceptionHandling(tmp.str()); } } - for (vector<unsigned int>::iterator it = exogenous.begin(); it != exogenous.end(); it++) - previous_block_exogenous.push_back(*it); + for (auto it : exogenous) + previous_block_exogenous.push_back(it); } bool diff --git a/mex/sources/bytecode/SparseMatrix.cc b/mex/sources/bytecode/SparseMatrix.cc index 4af1ffa2099d643e2d022a55ffd35204b7c3af6b..0473d91c1f619cc7c3f5a7f42515af6b9788c408 100644 --- a/mex/sources/bytecode/SparseMatrix.cc +++ b/mex/sources/bytecode/SparseMatrix.cc @@ -1,5 +1,5 @@ /* - * Copyright © 2007-2017 Dynare Team + * Copyright © 2007-2020 Dynare Team * * This file is part of Dynare. * @@ -1083,23 +1083,23 @@ dynSparseMatrix::Init_UMFPACK_Sparse(int periods, int y_kmin, int y_kmax, int Si while (it4 != IM.end()) { var = it4->first.first.first; -#ifdef DEBUG - if (var < 0 || var >= Size) - { - ostringstream tmp; - tmp << " in Init_UMFPACK_Sparse, var (" << var << ") out of range\n"; - throw FatalExceptionHandling(tmp.str()); - } -#endif +// #ifdef DEBUG +// if (var < 0 || var >= Size) +// { +// ostringstream tmp; +// tmp << " in Init_UMFPACK_Sparse, var (" << var << ") out of range\n"; +// throw FatalExceptionHandling(tmp.str()); +// } +// #endif eq = it4->first.second+Size*t; -#ifdef DEBUG - if (eq < 0 || eq >= Size) - { - ostringstream tmp; - tmp << " in Init_UMFPACK_Sparse, eq (" << eq << ") out of range\n"; - throw FatalExceptionHandling(tmp.str()); - } -#endif +// #ifdef DEBUG +// if (eq < 0 || eq >= Size) +// { +// ostringstream tmp; +// tmp << " in Init_UMFPACK_Sparse, eq (" << eq << ") out of range\n"; +// throw FatalExceptionHandling(tmp.str()); +// } +// #endif lag = -it4->first.first.second; int index = it4->second+ (t-lag) * u_count_init; if (var != last_var) @@ -1181,12 +1181,12 @@ dynSparseMatrix::Init_UMFPACK_Sparse(int periods, int y_kmin, int y_kmax, int Si if (lag <= ti_new_y_kmax && lag >= ti_new_y_kmin) /*Build the index for sparse matrix containing the jacobian : u*/ { #ifdef DEBUG - if (index < 0 || index >= u_count_alloc || index > Size + Size*Size) - { - ostringstream tmp; - tmp << " in Init_UMFPACK_Sparse, index (" << index << ") out of range for u vector max = " << Size+Size*Size << " allocated = " << u_count_alloc << "\n"; - throw FatalExceptionHandling(tmp.str()); - } + // if (index < 0 || index >= u_count_alloc || index > Size + Size*Size) + // { + // ostringstream tmp; + // tmp << " in Init_UMFPACK_Sparse, index (" << index << ") out of range for u vector max = " << Size+Size*Size << " allocated = " << u_count_alloc << "\n"; + // throw FatalExceptionHandling(tmp.str()); + // } if (NZE >= max_nze) { ostringstream tmp; @@ -1275,7 +1275,7 @@ dynSparseMatrix::Init_UMFPACK_Sparse(int periods, int y_kmin, int y_kmax, int Si (*Ap)[Size*periods] = NZE; #ifdef DEBUG mexPrintf("*Ax = ["); - for (int i = 0; i < NZE; i++) + for (int i = 0; i < static_cast<int>(NZE); i++) mexPrintf("%f ", (*Ax)[i]); mexPrintf("]\n"); @@ -1285,7 +1285,7 @@ dynSparseMatrix::Init_UMFPACK_Sparse(int periods, int y_kmin, int y_kmax, int Si mexPrintf("]\n"); mexPrintf("*Ai = ["); - for (int i = 0; i < NZE; i++) + for (int i = 0; i < static_cast<int>(NZE); i++) mexPrintf("%d ", (*Ai)[i]); mexPrintf("]\n"); #endif @@ -6294,9 +6294,9 @@ dynSparseMatrix::Simulate_One_Boundary(int block_num, int y_size, int y_kmin, in break; } if (select) - mexPrintf("-> variable %s (%d) at time %d = %f direction = %f\n", get_variable(eEndogenous, j).c_str(), j+1, it_, y[j+it_*y_size], direction[j+it_*y_size]); + mexPrintf("-> variable %s (%d) at time %d = %f direction = %f\n", get_variable(SymbolType::endogenous, j).c_str(), j+1, it_, y[j+it_*y_size], direction[j+it_*y_size]); else - mexPrintf(" variable %s (%d) at time %d = %f direction = %f\n", get_variable(eEndogenous, j).c_str(), j+1, it_, y[j+it_*y_size], direction[j+it_*y_size]); + mexPrintf(" variable %s (%d) at time %d = %f direction = %f\n", get_variable(SymbolType::endogenous, j).c_str(), j+1, it_, y[j+it_*y_size], direction[j+it_*y_size]); } #endif if (steady_state) diff --git a/mex/sources/disclyap_fast/disclyap_fast.f08 b/mex/sources/disclyap_fast/disclyap_fast.f08 new file mode 100644 index 0000000000000000000000000000000000000000..7ed5ae4b35887f300094db84159cf2b41293d613 --- /dev/null +++ b/mex/sources/disclyap_fast/disclyap_fast.f08 @@ -0,0 +1,161 @@ +! Solve the discrete Lyapunov Equation (X = G·X·Gáµ€ + V) using the Doubling Algorithm +! +! Syntax: +! [X, error_flag] = disclyap_fast(G, V, tol, check_flag) +! +! Inputs: +! G [double] (n×n) first input matrix +! V [double] (n×n) second input matrix +! tol [double] scalar, tolerance criterion +! check_flag [boolean] if true: check positive-definiteness (optional) +! max_iter [integer] scalar, maximum number of iterations (optional) +! +! Outputs: +! X [double] solution matrix +! error_flag [boolean] true if solution is found, false otherwise (optional) +! +! If check_flag is true, then the code will check if the resulting X +! is positive definite and generate an error message if it is not. +! +! This is a Fortran translation of a code originally written by Joe Pearlman +! and Alejandro Justiniano. + +! Copyright © 2020 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/>. + +subroutine mexFunction(nlhs, plhs, nrhs, prhs) bind(c, name='mexFunction') + use iso_fortran_env + use ieee_arithmetic + use matlab_mex + use lapack + implicit none + + type(c_ptr), dimension(*), intent(in), target :: prhs + type(c_ptr), dimension(*), intent(out) :: plhs + integer(c_int), intent(in), value :: nlhs, nrhs + + integer(c_size_t) :: n + real(real64) :: tol, max_iter + logical :: check_flag + + real(real64), dimension(:, :), allocatable :: P0, P1, A0, A1, Ptmp + real(real64) :: matd + integer :: iter + integer (blint) :: n_bl + + real(real64), dimension(:, :), pointer :: X + + if (nlhs < 1 .or. nlhs > 2 .or. nrhs < 3 .or. nrhs > 5) then + call mexErrMsgTxt("disclyap_fast: requires between 3 and 5 input arguments, and 1 or 2 output arguments") + return + end if + + n = mxGetM(prhs(1)) + if (.not. mxIsDouble(prhs(1)) .or. mxIsComplex(prhs(1)) & + .or. .not. mxIsDouble(prhs(2)) .or. mxIsComplex(prhs(2)) & + .or. mxGetN(prhs(1)) /= n .or. mxGetM(prhs(2)) /= n .or. mxGetN(prhs(2)) /= n) then + call mexErrMsgTxt("disclyap_fast: first two arguments should be real matrices of the same dimension") + return + end if + + if (.not. (mxIsScalar(prhs(3)) .and. mxIsNumeric(prhs(3)))) then + call mexErrMsgTxt("disclyap_fast: third argument (tol) should be a numeric scalar") + return + end if + tol = mxGetScalar(prhs(3)) + + if (nrhs >= 4) then + if (.not. (mxIsLogicalScalar(prhs(4)))) then + call mexErrMsgTxt("disclyap_fast: fourth argument (check_flag) should be a logical scalar") + return + end if + check_flag = mxGetScalar(prhs(4)) == 1_c_double + else + check_flag = .false. + end if + + if (nrhs >= 5) then + if (.not. (mxIsScalar(prhs(5)) .and. mxIsNumeric(prhs(5)))) then + call mexErrMsgTxt("disclyap_fast: fifth argument (max_iter) should be a numeric scalar") + return + end if + max_iter = int(mxGetScalar(prhs(5))) + else + max_iter = 2000 + end if + + ! Allocate and initialize temporary variables + allocate(P0(n,n), P1(n,n), A0(n,n), A1(n,n), Ptmp(n,n)) + associate (G => mxGetPr(prhs(1)), V => mxGetPr(prhs(2))) + P0 = reshape(V, [n, n]) + A0 = reshape(G, [n, n]) + end associate + iter = 1 + + n_bl = int(n, blint) + do + ! We don't use matmul() for the time being because -fuse-external-blas does + ! not work as expected under gfortran 8 + + ! Ptmp = A0·P0 + call dgemm("N", "N", n_bl, n_bl, n_bl, 1._real64, A0, n_bl, P0, n_bl, 0._real64, Ptmp, n_bl) + ! P1 = P0+Ptmp·A0áµ€ + P1 = P0 + call dgemm("N", "T", n_bl, n_bl, n_bl, 1._real64, Ptmp, n_bl, A0, n_bl, 1._real64, P1, n_bl) + ! A1 = A0·A0 + call dgemm("N", "N", n_bl, n_bl, n_bl, 1._real64, A0, n_bl, A0, n_bl, 0._real64, A1, n_bl) + + matd = maxval(abs(P1-P0)) + + P0 = P1 + A0 = A1 + iter = iter + 1 + + if (matd <= tol .or. iter == max_iter) exit + end do + + ! Allocate and set outputs + plhs(1) = mxCreateDoubleMatrix(n, n, mxREAL) + X(1:n, 1:n) => mxGetPr(plhs(1)) + if (nlhs > 1) plhs(2) = mxCreateLogicalScalar(.false._mxLogical) + + if (iter == max_iter) then + X = ieee_value(X, ieee_quiet_nan) + if (nlhs > 1) then + call mxDestroyArray(plhs(2)) + plhs(2) = mxCreateLogicalScalar(.true._mxLogical) + end if + return + end if + + X = (P0+transpose(P0))/2._real64 + + ! Check that X is positive definite + if (check_flag .and. nlhs > 1) then + block + real(real64), dimension(n, n) :: X2 + integer(blint) :: info + ! X2=chol(X) + X2 = X + call dpotrf("L", n_bl, X2, n_bl, info) + if (info /= 0) then + call mxDestroyArray(plhs(2)) + plhs(2) = mxCreateLogicalScalar(.true._mxLogical) + end if + end block + end if +end subroutine mexFunction diff --git a/mex/sources/dynblas.h b/mex/sources/dynblas.h index 384b2e26fea236b05382493190cd6f7b2f0275dc..8215dd2ad31fe6467a26931a214d99d6b8fb7dce 100644 --- a/mex/sources/dynblas.h +++ b/mex/sources/dynblas.h @@ -8,7 +8,7 @@ * and MATLAB_VERSION (for version 7.4, define it to 0x0704). * * - * Copyright © 2009-2013 Dynare Team + * Copyright © 2009-2020 Dynare Team * * This file is part of Dynare. * @@ -29,8 +29,7 @@ #ifndef _DYNBLAS_H #define _DYNBLAS_H -/* Starting from version 7.8, MATLAB BLAS expects ptrdiff_t arguments for integers */ -#if defined(MATLAB_MEX_FILE) && MATLAB_VERSION >= 0x0708 +#if defined(MATLAB_MEX_FILE) # ifdef __cplusplus # include <cstddef> # else diff --git a/mex/sources/dynlapack.h b/mex/sources/dynlapack.h index 7e953e8637d98af431dcf48d2be1bf537bd0d228..c824570a7e4f38474c0ebf4076f873f209e404f2 100644 --- a/mex/sources/dynlapack.h +++ b/mex/sources/dynlapack.h @@ -8,7 +8,7 @@ * and MATLAB_VERSION (for version 7.4, define it to 0x0704). * * - * Copyright © 2009-2011 Dynare Team + * Copyright © 2009-2020 Dynare Team * * This file is part of Dynare. * @@ -29,8 +29,7 @@ #ifndef _DYNLAPACK_H #define _DYNLAPACK_H -/* Starting from version 7.8, MATLAB LAPACK expects ptrdiff_t arguments for integers */ -#if defined(MATLAB_MEX_FILE) && MATLAB_VERSION >= 0x0708 +#if defined(MATLAB_MEX_FILE) # ifdef __cplusplus # include <cstddef> # else diff --git a/mex/sources/k_order_perturbation/dynamic_dll.cc b/mex/sources/k_order_perturbation/dynamic_dll.cc index 4688d66b5bc47d55754b66685195cbc036567e4a..c9a44c6d29d7dd47e798462ecdd8abe85bb29aca 100644 --- a/mex/sources/k_order_perturbation/dynamic_dll.cc +++ b/mex/sources/k_order_perturbation/dynamic_dll.cc @@ -1,5 +1,5 @@ /* - * Copyright © 2008-2019 Dynare Team + * Copyright © 2008-2020 Dynare Team * * This file is part of Dynare. * @@ -19,8 +19,6 @@ #include "dynamic_dll.hh" -#include "dynare_exception.hh" - #include <iostream> #include <cassert> @@ -45,34 +43,14 @@ DynamicModelDLL::DynamicModelDLL(const std::string &modName, int ntt_arg, int or #endif ); - for (int i = 0; i <= order; i++) - { - std::string funcname = "dynamic_" + (i == 0 ? "resid" : "g" + std::to_string(i)); - dynamic_deriv_fct deriv; - dynamic_tt_fct tt; -#if defined(__CYGWIN32__) || defined(_WIN32) - deriv = reinterpret_cast<dynamic_deriv_fct>(GetProcAddress(dynamicHinstance, funcname.c_str())); - tt = reinterpret_cast<dynamic_tt_fct>(GetProcAddress(dynamicHinstance, (funcname + "_tt").c_str())); -#else - deriv = reinterpret_cast<dynamic_deriv_fct>(dlsym(dynamicHinstance, funcname.c_str())); - tt = reinterpret_cast<dynamic_tt_fct>(dlsym(dynamicHinstance, (funcname + "_tt").c_str())); -#endif - if (!deriv || !tt) - { -#if defined(__CYGWIN32__) || defined(_WIN32) - FreeLibrary(dynamicHinstance); -#else - dlclose(dynamicHinstance); -#endif - throw DynareException(__FILE__, __LINE__, "Error when loading symbols from " + fName -#if !defined(__CYGWIN32__) && !defined(_WIN32) - + ": " + dlerror() -#endif - ); - } - dynamic_deriv.push_back(deriv); - dynamic_tt.push_back(tt); - } + dynamic_tt.resize(order+1); + + std::tie(dynamic_resid, dynamic_tt[0]) = getSymbolsFromDLL<dynamic_resid_or_g1_fct>("dynamic_resid", fName); + std::tie(dynamic_g1, dynamic_tt[1]) = getSymbolsFromDLL<dynamic_resid_or_g1_fct>("dynamic_g1", fName); + + dynamic_higher_deriv.resize(std::max(0, order-1)); + for (int i = 2; i <= order; i++) + std::tie(dynamic_higher_deriv[i-2], dynamic_tt[i]) = getSymbolsFromDLL<dynamic_higher_deriv_fct>("dynamic_g" + std::to_string(i), fName); tt = std::make_unique<double[]>(ntt); } @@ -95,11 +73,16 @@ void DynamicModelDLL::eval(const Vector &y, const Vector &x, const Vector &modParams, const Vector &ySteady, Vector &residual, std::vector<TwoDMatrix> &md) noexcept(false) { - assert(md.size() == dynamic_deriv.size()-1); + assert(md.size() == dynamic_tt.size()-1); - for (size_t i = 0; i < dynamic_deriv.size(); i++) + for (size_t i = 0; i < dynamic_tt.size(); i++) { dynamic_tt[i](y.base(), x.base(), 1, modParams.base(), ySteady.base(), 0, tt.get()); - dynamic_deriv[i](y.base(), x.base(), 1, modParams.base(), ySteady.base(), 0, tt.get(), i == 0 ? residual.base() : md[i-1].base()); + if (i == 0) + dynamic_resid(y.base(), x.base(), 1, modParams.base(), ySteady.base(), 0, tt.get(), residual.base()); + else if (i == 1) + dynamic_g1(y.base(), x.base(), 1, modParams.base(), ySteady.base(), 0, tt.get(), md[0].base()); + else + dynamic_higher_deriv[i-2](y.base(), x.base(), 1, modParams.base(), ySteady.base(), 0, tt.get(), &md[i-1].get(0, 0), &md[i-1].get(0, 1), &md[i-1].get(0, 2)); } } diff --git a/mex/sources/k_order_perturbation/dynamic_dll.hh b/mex/sources/k_order_perturbation/dynamic_dll.hh index 693e8b2b9d9a7aa583496fe5d25baed02bdafbf0..e9d9aa8a6ff9c3afb7242139944db24ee8432b15 100644 --- a/mex/sources/k_order_perturbation/dynamic_dll.hh +++ b/mex/sources/k_order_perturbation/dynamic_dll.hh @@ -1,5 +1,5 @@ /* - * Copyright © 2008-2019 Dynare Team + * Copyright © 2008-2020 Dynare Team * * This file is part of Dynare. * @@ -31,11 +31,15 @@ #include <string> #include <memory> +#include <utility> + +#include "dynare_exception.hh" #include "dynamic_abstract_class.hh" using dynamic_tt_fct = void (*)(const double *y, const double *x, int nb_row_x, const double *params, const double *steady_state, int it_, double *T); -using dynamic_deriv_fct = void (*)(const double *y, const double *x, int nb_row_x, const double *params, const double *steady_state, int it_, const double *T, double *deriv); +using dynamic_resid_or_g1_fct = void (*)(const double *y, const double *x, int nb_row_x, const double *params, const double *steady_state, int it_, const double *T, double *resid_or_g1); +using dynamic_higher_deriv_fct = void (*)(const double *y, const double *x, int nb_row_x, const double *params, const double *steady_state, int it_, const double *T, double *g_i, double *g_j, double *g_v); /** * creates pointer to Dynamic function inside <model>_dynamic.dll @@ -45,7 +49,8 @@ class DynamicModelDLL : public DynamicModelAC { private: std::vector<dynamic_tt_fct> dynamic_tt; - std::vector<dynamic_deriv_fct> dynamic_deriv; + dynamic_resid_or_g1_fct dynamic_resid, dynamic_g1; + std::vector<dynamic_higher_deriv_fct> dynamic_higher_deriv; // Index 0 is g2 #if defined(_WIN32) || defined(__CYGWIN32__) HINSTANCE dynamicHinstance; // DLL instance pointer in Windows #else @@ -53,6 +58,34 @@ private: #endif std::unique_ptr<double[]> tt; // Vector of temporary terms + template<typename T> + std::pair<T, dynamic_tt_fct> + getSymbolsFromDLL(const std::string &funcname, const std::string &fName) + { + dynamic_tt_fct tt; + T deriv; +#if defined(__CYGWIN32__) || defined(_WIN32) + deriv = reinterpret_cast<T>(GetProcAddress(dynamicHinstance, funcname.c_str())); + tt = reinterpret_cast<dynamic_tt_fct>(GetProcAddress(dynamicHinstance, (funcname + "_tt").c_str())); +#else + deriv = reinterpret_cast<T>(dlsym(dynamicHinstance, funcname.c_str())); + tt = reinterpret_cast<dynamic_tt_fct>(dlsym(dynamicHinstance, (funcname + "_tt").c_str())); +#endif + if (!deriv || !tt) + { +#if defined(__CYGWIN32__) || defined(_WIN32) + FreeLibrary(dynamicHinstance); +#else + dlclose(dynamicHinstance); +#endif + throw DynareException(__FILE__, __LINE__, "Error when loading symbols from " + fName +#if !defined(__CYGWIN32__) && !defined(_WIN32) + + ": " + dlerror() +#endif + ); + } + return { deriv, tt }; + } public: // construct and load Dynamic model DLL explicit DynamicModelDLL(const std::string &fname, int ntt_arg, int order); diff --git a/mex/sources/matlab_mex.F08 b/mex/sources/matlab_mex.F08 index 7a853f710534b5bbcede2b9a9e1936f86d3ae7c5..755b6d5403a20412fb854f9ebcb26d65e7385bed 100644 --- a/mex/sources/matlab_mex.F08 +++ b/mex/sources/matlab_mex.F08 @@ -12,16 +12,19 @@ ! • API_VER2 is for functions which were versioned _730 before 9.4 (R2018a) ! when not using the MX_COMPAT_32 mode ! For each function, the information can be retrieved from either matrix.h or -! mex.h from R2009b +! mex.h from R2014a ! — C passes arguments by value, so the “value†keyword is often needed ! — Strings passed to C must be null terminated (hence a wrapper is needed to ! append c_null_char) +! — We follow the Fortran convention that indices start at one. Hence, when +! interfacing a function with a mwIndex argument, it is necessary to write +! a glue code that substracts one to the index ! — When writing glue code, using the pure C interface as a starting point: ! • remove the “use†declarations ! • remove the “value†keywords ! • convert input character arrays to character(kind=c_char, len=*) -! Copyright © 2019 Dynare Team +! Copyright © 2019-2020 Dynare Team ! ! This file is part of Dynare. ! @@ -193,6 +196,11 @@ module matlab_mat end function mxIsChar ! Logical + logical(c_bool) function mxIsLogical(pm) bind(c, name="mxIsLogical"//API_VER) + use iso_c_binding + type(c_ptr), intent(in), value :: pm + end function mxIsLogical + logical(c_bool) function mxIsLogicalScalar(array_ptr) bind(c, name="mxIsLogicalScalar"//API_VER) use iso_c_binding type(c_ptr), intent(in), value :: array_ptr @@ -204,6 +212,11 @@ module matlab_mat logical(mxLogical), intent(in), value :: value end function mxCreateLogicalScalar + type(c_ptr) function mxGetLogicals_internal(array_ptr) bind(c, name="mxGetLogicals"//API_VER) + use iso_c_binding + type(c_ptr), intent(in), value :: array_ptr + end function mxGetLogicals_internal + ! Object logical(c_bool) function mxIsClass_internal(pm, classname) bind(c, name="mxIsClass"//API_VER) use iso_c_binding @@ -212,6 +225,11 @@ module matlab_mat end function mxIsClass_internal ! Structure + logical(c_bool) function mxIsStruct(pm) bind(c, name="mxIsStruct"//API_VER) + use iso_c_binding + type(c_ptr), intent(in), value :: pm + end function mxIsStruct + type(c_ptr) function mxGetField_internal(pm, index, fieldname) bind(c, name="mxGetField"//API_VER2) use iso_c_binding import :: mwIndex @@ -220,6 +238,24 @@ module matlab_mat character(c_char), dimension(*), intent(in) :: fieldname end function mxGetField_internal + integer(c_int) function mxGetNumberOfFields(pm) bind(c, name="mxGetNumberOfFields"//API_VER) + use iso_c_binding + type(c_ptr), intent(in), value :: pm + end function mxGetNumberOfFields + + ! Cell + logical(c_bool) function mxIsCell(pm) bind(c, name="mxIsCell"//API_VER) + use iso_c_binding + type(c_ptr), intent(in), value :: pm + end function mxIsCell + + type(c_ptr) function mxGetCell_internal(pm, index) bind(c, name="mxGetCell"//API_VER2) + use iso_c_binding + import :: mwIndex + type(c_ptr), intent(in), value :: pm + integer(mwIndex), intent(in), value :: index + end function mxGetCell_internal + !! Delete and Duplicate mxArray subroutine mxDestroyArray(pm) bind(c, name="mxDestroyArray"//API_VER) use iso_c_binding @@ -246,14 +282,14 @@ contains function mxGetDoubles(pm) type(c_ptr), intent(in) :: pm real(real64), dimension(:), pointer :: mxGetDoubles - call c_f_pointer(mxGetDoubles_internal(pm) , mxGetDoubles, [ mxGetNumberOfElements(pm) ]) + call c_f_pointer(mxGetDoubles_internal(pm), mxGetDoubles, [ mxGetNumberOfElements(pm) ]) end function mxGetDoubles #endif function mxGetPr(pm) type(c_ptr), intent(in) :: pm real(real64), dimension(:), pointer :: mxGetPr - call c_f_pointer(mxGetPr_internal(pm) , mxGetPr, [ mxGetNumberOfElements(pm) ]) + call c_f_pointer(mxGetPr_internal(pm), mxGetPr, [ mxGetNumberOfElements(pm) ]) end function mxGetPr #if MX_HAS_INTERLEAVED_COMPLEX @@ -278,6 +314,12 @@ contains end function mxGetPi #endif + function mxGetLogicals(array_ptr) + type(c_ptr), intent(in) :: array_ptr + logical(mxLogical), dimension(:), pointer :: mxGetLogicals + call c_f_pointer(mxGetLogicals_internal(array_ptr), mxGetLogicals, [ mxGetNumberOfElements(array_ptr) ]) + end function mxGetLogicals + logical(c_bool) function mxIsClass(pm, classname) type(c_ptr), intent(in) :: pm character(kind=c_char, len=*), intent(in) :: classname @@ -288,13 +330,26 @@ contains type(c_ptr), intent(in) :: pm integer(mwIndex), intent(in) :: index character(kind=c_char, len=*), intent(in) :: fieldname - mxGetField = mxGetField_internal(pm, index, fieldname // c_null_char) + mxGetField = mxGetField_internal(pm, index-1, fieldname // c_null_char) end function mxGetField + type(c_ptr) function mxGetCell(pm, index) + type(c_ptr), intent(in) :: pm + integer(mwIndex), intent(in) :: index + mxGetCell = mxGetCell_internal(pm, index-1) + end function mxGetCell + function mxArrayToString(pm) type(c_ptr), intent(in) :: pm - character(c_char), dimension(:), pointer :: mxArrayToString - call c_f_pointer(mxArrayToString_internal(pm), mxArrayToString, [ mxGetNumberOfElements(pm) ]) + character(kind=c_char, len=:), allocatable :: mxArrayToString + character(kind=c_char), dimension(:), pointer :: chararray + integer :: i + call c_f_pointer(mxArrayToString_internal(pm), chararray, [ mxGetNumberOfElements(pm) ]) + ! Convert the character array into a character scalar (of length > 1) + allocate(character(kind=c_char, len=size(chararray)) :: mxArrayToString) + do i=1,size(chararray) + mxArrayToString(i:i) = chararray(i) + end do end function mxArrayToString end module matlab_mat @@ -367,4 +422,10 @@ contains character(kind=c_char, len=*), intent(in) :: message call mexPrintf_internal(message // c_null_char) end subroutine mexPrintf + + ! Same as mexPrintf(), but trims trailing whitespace, and adds a new line + subroutine mexPrintf_trim_newline(message) + character(kind=c_char, len=*), intent(in) :: message + call mexPrintf_internal(trim(message) // new_line(message) // c_null_char) + end subroutine mexPrintf_trim_newline end module matlab_mex diff --git a/mex/sources/mjdgges/mjdgges.F08 b/mex/sources/mjdgges/mjdgges.F08 index 5209c01d1d3340772f10e3cfeb063092aff307a7..75c3f658dc06b08907cc17e79377c7f7b8962c2f 100644 --- a/mex/sources/mjdgges/mjdgges.F08 +++ b/mex/sources/mjdgges/mjdgges.F08 @@ -1,3 +1,23 @@ +! Wrapper around LAPACK’s dgges (generalized Schur decomposition) that gives a +! better access to error conditions than does MATLAB’s qz. +! +! Syntax: +! [ss, tt, zz, sdim, eigval, info] = mjdgges(e, d, qz_criterium, zhreshold) +! +! Inputs: +! e [double] real square (n×n) matrix +! d [double] real square (n×n) matrix +! qz_criterium [double] scalar (of the form 1+ε) +! zhreshold [double] used for detecting eigenvalues too close to 0÷0 +! +! Outputs: +! ss [double] (n×n) quasi-triangular matrix +! tt [double] (n×n) quasi-triangular matrix +! zz [double] (n×n) orthogonal matrix +! sdim [integer] scalar, number of stable eigenvalues +! eigval [complex] (n×1) vector of generalized eigenvalues +! info [integer] scalar, error code of dgges (or 30 if eigenvalue close to 0÷0) + ! Copyright © 2006-2020 Dynare Team ! ! This file is part of Dynare. @@ -44,7 +64,7 @@ subroutine mexFunction(nlhs, plhs, nrhs, prhs) bind(c, name='mexFunction') type(c_ptr), dimension(*), intent(out) :: plhs integer(c_int), intent(in), value :: nlhs, nrhs - integer(c_size_t) :: m1, n1, m2, n2 + integer(c_size_t) :: n real(real64) :: zhreshold integer(blint) :: n_bl, lwork, info_bl, sdim_bl real(real64), dimension(:), allocatable :: alpha_r, alpha_i, beta, work @@ -55,51 +75,47 @@ subroutine mexFunction(nlhs, plhs, nrhs, prhs) bind(c, name='mexFunction') #else real(real64), dimension(:), pointer :: gev_r, gev_i #endif -#if defined(MATLAB_MEX_FILE) && MATLAB_VERSION < 0x0904 - ! Workaround for MKL bug, see below - real(real64), dimension(:), allocatable, target :: vsl_target -#endif if (nrhs < 2 .or. nrhs > 4 .or. nlhs /= 6) then call mexErrMsgTxt("MJDGGES: takes 2, 3 or 4 input arguments and exactly 6 output arguments.") return end if - m1 = mxGetM(prhs(1)) - n1 = mxGetN(prhs(1)) - m2 = mxGetM(prhs(2)) - n2 = mxGetN(prhs(2)) - + n = mxGetM(prhs(1)) if (.not. mxIsDouble(prhs(1)) .or. mxIsComplex(prhs(1)) & .or. .not. mxIsDouble(prhs(2)) .or. mxIsComplex(prhs(2)) & - .or. m1 /= n1 .or. m2 /= n1 .or. m2 /= n2) then - call mexErrMsgTxt("MJDGGES requires two square real matrices of the same dimension.") + .or. mxGetN(prhs(1)) /= n .or. mxGetM(prhs(2)) /= n .or. mxGetN(prhs(2)) /= n) then + call mexErrMsgTxt("MJDGGES: first two arguments should be real matrices of the same dimension") return end if ! Set criterium for stable eigenvalues if (nrhs >= 3 .and. mxGetM(prhs(3)) > 0) then - associate (crit_arg => mxGetPr(prhs(3))) - criterium = crit_arg(1) - end associate + if (.not. (mxIsScalar(prhs(3)) .and. mxIsNumeric(prhs(3)))) then + call mexErrMsgTxt("MJDGGES: third argument (qz_criterium) should be a numeric scalar") + return + end if + criterium = mxGetScalar(prhs(3)) else criterium = 1_real64 + 1e-6_real64 end if ! set criterium for 0/0 generalized eigenvalues */ if (nrhs == 4 .and. mxGetM(prhs(4)) > 0) then - associate (zhresh_arg => mxGetPr(prhs(4))) - zhreshold = zhresh_arg(1) - end associate + if (.not. (mxIsScalar(prhs(4)) .and. mxIsNumeric(prhs(4)))) then + call mexErrMsgTxt("MJDGGES: fourth argument (zhreshold) should be a numeric scalar") + return + end if + zhreshold = mxGetScalar(prhs(4)) else zhreshold = 1e-6_real64 end if - plhs(1) = mxCreateDoubleMatrix(n1, n1, mxREAL) - plhs(2) = mxCreateDoubleMatrix(n1, n1, mxREAL) - plhs(3) = mxCreateDoubleMatrix(n1, n1, mxREAL) + plhs(1) = mxCreateDoubleMatrix(n, n, mxREAL) + plhs(2) = mxCreateDoubleMatrix(n, n, mxREAL) + plhs(3) = mxCreateDoubleMatrix(n, n, mxREAL) plhs(4) = mxCreateDoubleMatrix(1_mwSize, 1_mwSize, mxREAL) - plhs(5) = mxCreateDoubleMatrix(n1, 1_mwSize, mxCOMPLEX) + plhs(5) = mxCreateDoubleMatrix(n, 1_mwSize, mxCOMPLEX) plhs(6) = mxCreateDoubleMatrix(1_mwSize, 1_mwSize, mxREAL) s => mxGetPr(plhs(1)) @@ -113,19 +129,7 @@ subroutine mexFunction(nlhs, plhs, nrhs, prhs) bind(c, name='mexFunction') #endif info => mxGetPr(plhs(6)) z => mxGetPr(plhs(3)) -#if defined(MATLAB_MEX_FILE) && MATLAB_VERSION < 0x0904 - ! The left Schur vectors (VSL) are normally not computed, since JOBVSL="N". - ! But old MKL versions (at least the one shipped with MATLAB R2009b/7.9, - ! which is MKL 10.1) are buggy, and passing nullptr for VSL leads to a crash. - ! Hence we need to allocate space for it. - ! The bug seems to be fixed in MATLAB R2010a/7.10 (MKL 10.2), but we use the - ! workaround for all versions < R2018a/9.4, since those share the same - ! ABI and hence the same executables. - allocate(vsl_target(n1*n1)) - vsl => vsl_target -#else vsl => null() -#endif ! Copy input matrices, since we can’t modify them associate (a => mxGetPr(prhs(1)), b => mxGetPr(prhs(2))) @@ -133,7 +137,7 @@ subroutine mexFunction(nlhs, plhs, nrhs, prhs) bind(c, name='mexFunction') t = b end associate - n_bl = int(n1, blint) + n_bl = int(n, blint) lwork = 16*n_bl + 16 allocate(alpha_r(n_bl), alpha_i(n_bl), beta(n_bl), bwork(n_bl), work(lwork)) diff --git a/preprocessor b/preprocessor index e137e026bde2119027719b4d66f437158523fcbc..de65e74c8f9e433c0f94c30e1b0aff27c22dc6cd 160000 --- a/preprocessor +++ b/preprocessor @@ -1 +1 @@ -Subproject commit e137e026bde2119027719b4d66f437158523fcbc +Subproject commit de65e74c8f9e433c0f94c30e1b0aff27c22dc6cd diff --git a/scripts/dynare.el b/scripts/dynare.el index 9b04dc7e2be4c68dc3b4957f7822966ce8f09550..a25179886301905ad8d9218dac995a7f4ff51be5 100644 --- a/scripts/dynare.el +++ b/scripts/dynare.el @@ -1,7 +1,7 @@ ;;; dynare.el --- major mode for editing Dynare mod files ;; Copyright © 2010 Yannick Kalantzis -;; Copyright © 2019 Dynare Team +;; Copyright © 2019-2020 Dynare Team ;; ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by @@ -72,7 +72,7 @@ "squeeze_shock_decomposition" "sbvar" "ms_estimation" "ms_simulation" "ms_compute_mdd" "ms_compute_probabilities" "ms_forecast" "ms_irf" "ms_variance_decomposition" "conditional_forecast" - "plot_conditional_forecast" "gmm_estimation" "smm_estimation" + "plot_conditional_forecast" "method_of_moments" "markov_switching" "svar" "svar_global_identification_check" "external_function" "calib_smoother" "model_diagnostics" "extended_path" "smoother2histval" "perfect_foresight_setup" "perfect_foresight_solver" @@ -98,7 +98,7 @@ "observation_trends" "optim_weights" "homotopy_setup" "conditional_forecast_paths" "svar_identification" "moment_calibration" "irf_calibration" "ramsey_constraints" "restrictions" "generate_irfs" - "verbatim") + "matched_moments" "verbatim") "Dynare block keywords.")) ;; Mathematical functions and operators used in model equations (see "hand_side" in Bison file) diff --git a/tests/.gitignore b/tests/.gitignore index 3a531c3c945f7fa93808e5063b267668cfac3855..d14f64f9274241b950d51a602027584183e61701 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -25,6 +25,9 @@ wsOct /run_test_matlab_output.txt /block_bytecode/ls2003_tmp.mod +/histval_initval_file/data.csv +/histval_initval_file/data.xls +/histval_initval_file/data.xlsx /partial_information/PItest3aHc0PCLsimModPiYrVarobsAll_PCL* /partial_information/PItest3aHc0PCLsimModPiYrVarobsCNR_PCL* @@ -47,6 +50,8 @@ wsOct !/ep/mean_preserving_spread.m !/ep/rbcii_steady_state.m !/estimation/fsdat_simul.m +!/estimation/method_of_moments/RBC_MoM_steady_helper.m +!/estimation/method_of_moments/RBC_Andreasen_Data_2.mat !/expectations/expectation_ss_old_steadystate.m !/external_function/extFunDeriv.m !/external_function/extFunNoDerivs.m @@ -67,10 +72,11 @@ wsOct !/gsa/ls2003scr_mode.mat !/gsa/ls2003scr_results.mat !/gsa/morris/nk_est_data.m +!/histval_initval_file/histval_initval_file_unit_tests.m +!/histval_initval_file/ramst_initval_file_data.m !/identification/as2007/as2007_steadystate.m !/identification/as2007/G_QT.mat !/identification/kim/kim2_steadystate.m -!/initval_file/ramst_initval_file_data.m !/internals/tests.m !/k_order_perturbation/run_fs2000kplusplus.m !/kalman/likelihood/compare_kalman_routines.m diff --git a/tests/Makefile.am b/tests/Makefile.am index e7096b71a36a9a8df951bcb6232f4ef8e5c4dd52..9da3c18dc6bc31b8213ff0d99a59db8cd29eb409 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -48,6 +48,10 @@ MODFILES = \ estimation/MH_recover/fs2000_recover_3.mod \ estimation/t_proposal/fs2000_student.mod \ estimation/tune_mh_jscale/fs2000.mod \ + estimation/method_of_moments/AnScho_MoM.mod \ + estimation/method_of_moments/RBC_MoM_Andreasen.mod \ + estimation/method_of_moments/RBC_MoM_SMM_ME.mod \ + estimation/method_of_moments/RBC_MoM_prefilter.mod \ moments/example1_var_decomp.mod \ moments/example1_bp_test.mod \ moments/test_AR1_spectral_density.mod \ @@ -105,9 +109,12 @@ MODFILES = \ discretionary_policy/dennis_1.mod \ discretionary_policy/dennis_1_estim.mod \ discretionary_policy/Gali_discretion.mod \ - initval_file/ramst_initval_file.mod \ - initval_file/ramst_datafile.mod \ - ramst_normcdf_and_friends.mod \ + histval_initval_file/ramst_initval_file.mod \ + histval_initval_file/ramst_data.mod \ + histval_initval_file/ramst_datafile.mod \ + histval_initval_file/sim_exo_lead_lag.mod \ + histval_initval_file/sim_exo_lead_lag_initvalf.mod \ + ramst_normcdf_and_friends.mod \ ramst_vec.mod \ example1_varexo_det.mod \ predetermined_variables.mod \ @@ -182,6 +189,7 @@ MODFILES = \ dsge-var/simul_hybrid.mod \ dsge-var/dsgevar_forward_calibrated_lambda.mod \ dsge-var/dsgevar_forward_estimated_lambda.mod \ + external_function/benchmark.mod \ external_function/first_deriv_given.mod \ external_function/first_deriv_given_dll.mod \ external_function/first_and_2nd_deriv_given.mod \ @@ -190,6 +198,7 @@ MODFILES = \ external_function/first_and_2nd_deriv_given_dll_namespace.mod \ external_function/no_deriv_given.mod \ external_function/no_deriv_given_dll.mod \ + external_function/extfun_in_mlv.mod \ seeds.mod \ minimal_state_space_system/as2007_minimal.mod \ minimal_state_space_system/sw_minimal.mod \ @@ -530,7 +539,8 @@ XFAIL_MODFILES = ramst_xfail.mod \ estimation/tune_mh_jscale/fs2000_1_xfail.mod \ estimation/tune_mh_jscale/fs2000_2_xfail.mod -MFILES = initval_file/ramst_initval_file_data.m +MFILES = histval_initval_file/ramst_initval_file_data.m \ + histval_initval_file_unit_tests.m # Dependencies example1_use_dll.m.trs: example1.m.trs @@ -614,6 +624,25 @@ dsge-var/dsgevar_forward_estimated_lambda.m.trs: dsge-var/simul_hybrid.m.trs dsge-var/dsgevar_forward_calibrated_lambda.o.trs: dsge-var/simul_hybrid.o.trs dsge-var/dsgevar_forward_estimated_lambda.o.trs: dsge-var/simul_hybrid.o.trs +external_function/first_deriv_given.m.trs: external_function/benchmark.m.trs +external_function/first_deriv_given.o.trs: external_function/benchmark.o.trs +external_function/first_deriv_given_dll.m.trs: external_function/benchmark.m.trs +external_function/first_deriv_given_dll.o.trs: external_function/benchmark.o.trs +external_function/first_and_2nd_deriv_given.m.trs: external_function/benchmark.m.trs +external_function/first_and_2nd_deriv_given.o.trs: external_function/benchmark.o.trs +external_function/first_and_2nd_deriv_given_dll.m.trs: external_function/benchmark.m.trs +external_function/first_and_2nd_deriv_given_dll.o.trs: external_function/benchmark.o.trs +external_function/first_and_2nd_deriv_given_namespace.m.trs: external_function/benchmark.m.trs +external_function/first_and_2nd_deriv_given_namespace.o.trs: external_function/benchmark.o.trs +external_function/first_and_2nd_deriv_given_dll_namespace.m.trs: external_function/benchmark.m.trs +external_function/first_and_2nd_deriv_given_dll_namespace.o.trs: external_function/benchmark.o.trs +external_function/no_deriv_given.m.trs: external_function/benchmark.m.trs +external_function/no_deriv_given.o.trs: external_function/benchmark.o.trs +external_function/no_deriv_given_dll.m.trs: external_function/benchmark.m.trs +external_function/no_deriv_given_dll.o.trs: external_function/benchmark.o.trs +external_function/extfun_in_mlv.m.trs: external_function/benchmark.m.trs +external_function/extfun_in_mlv.o.trs: external_function/benchmark.o.trs + smoother2histval/fs2000_simul.m.trs: smoother2histval/fs2000_smooth.m.trs smoother2histval/fs2000_simul.o.trs: smoother2histval/fs2000_smooth.o.trs @@ -628,10 +657,15 @@ deterministic_simulations/rbc_det_stack_solve_algo_7_exo_lead.o.trs: determinist deterministic_simulations/rbc_det_stack_solve_algo_7_exo_lag.m.trs: deterministic_simulations/rbc_det.m.trs deterministic_simulations/rbc_det_stack_solve_algo_7_exo_lag.o.trs: deterministic_simulations/rbc_det.o.trs -initval_file/ramst_initval_file.m.trs: initval_file/ramst_initval_file_data.m.tls -initval_file/ramst_initval_file.o.trs: initval_file/ramst_initval_file_data.o.tls -initval_file/ramst_datafile.m.trs: initval_file/ramst_initval_file_data.m.tls -initval_file/ramst_datafile.o.trs: initval_file/ramst_initval_file_data.o.tls +histval_initval_file/ramst_initval_file.m.trs: histval_initval_file/ramst_initval_file_data.m.tls histval_initval_file/ramst_data.m.trs +histval_initval_file/ramst_initval_file.o.trs: histval_initval_file/ramst_initval_file_data.o.tls histval_initval_file/ramst_data.o.trs +histval_initval_file/ramst_datafile.m.trs: histval_initval_file/ramst_initval_file_data.m.tls +histval_initval_file/ramst_datafile.o.trs: histval_initval_file/ramst_initval_file_data.o.tls +histval_initval_file/sim_exo_lead_lag_initvalf.m.trs: histval_initval_file/sim_exo_lead_lag.m.trs +histval_initval_file/sim_exo_lead_lag_initvalf.o.trs: histval_initval_file/sim_exo_lead_lag.o.trs +histval_initval_file_unit_tests.m.trs: histval_initval_file/ramst_data.m.trs +histval_initval_file_unit_tests.o.trs: histval_initval_file/ramst_data.o.trs + identification/rbc_ident/rbc_ident_varexo_only.m.trs: identification/rbc_ident/rbc_ident_std_as_structural_par.m.trs identification/rbc_ident/rbc_ident_varexo_only.o.trs: identification/rbc_ident/rbc_ident_std_as_structural_par.o.trs @@ -958,13 +992,22 @@ M_OLS_TRS_FILES = $(patsubst %.mod, %.m.trs, $(OLS_MODFILES)) # Matlab TRS Files M_TRS_FILES = $(patsubst %.mod, %.m.trs, $(MODFILES)) -M_TRS_FILES += run_block_byte_tests_matlab.m.trs run_reporting_test_matlab.m.trs run_all_unitary_tests.m.trs -M_TRS_FILES += test_aggregate_routine_1_2.m.trs test_aggregate_routine_1_2_3.m.trs +M_TRS_FILES += run_block_byte_tests_matlab.m.trs \ + run_reporting_test_matlab.m.trs \ + run_all_unitary_tests.m.trs \ + histval_initval_file_unit_tests.m.trs \ + test_aggregate_routine_1_2.m.trs \ + test_aggregate_routine_1_2_3.m.trs + M_XFAIL_TRS_FILES = $(patsubst %.mod, %.m.trs, $(XFAIL_MODFILES)) # Octave TRS Files O_TRS_FILES = $(patsubst %.mod, %.o.trs, $(MODFILES)) -O_TRS_FILES += run_block_byte_tests_octave.o.trs run_reporting_test_octave.o.trs run_all_unitary_tests.o.trs +O_TRS_FILES += run_block_byte_tests_octave.o.trs \ + run_reporting_test_octave.o.trs \ + run_all_unitary_tests.o.trs \ + histval_initval_file_unit_tests.o.trs + O_XFAIL_TRS_FILES = $(patsubst %.mod, %.o.trs, $(XFAIL_MODFILES)) # Matlab TLS Files @@ -1009,7 +1052,6 @@ EXTRA_DIST = \ block_bytecode/run_ls2003.m \ bvar_a_la_sims/bvar_sample.m \ dates/fsdat_simul.m \ - dates/data_uav.xls \ dates/data_uav.xlsx \ external_function/extFunDeriv.m \ external_function/extFunNoDerivs.m \ @@ -1019,7 +1061,6 @@ EXTRA_DIST = \ expectations/expectation_ss_old_steadystate.m \ filter_step_ahead/trend_cycle_decomposition_data.m \ steady_state/walsh1_old_ss_steadystate.m \ - data/test.xls \ data/test.xlsx \ gsa/morris/nk_est_data.m \ gsa/data_ca1.m \ @@ -1054,7 +1095,6 @@ EXTRA_DIST = \ ms-sbvar/archive-files/specification_2v2c.dat \ recursive/data_ca1.m \ recursive/data_ca1_csv.csv \ - recursive/data_ca1_xls.xls \ recursive/data_ca1_xls.xlsx \ kalman_filter_smoother/fsdat_simul.m \ kalman/lik_init/fs2000_common.inc \ @@ -1120,7 +1160,14 @@ EXTRA_DIST = \ ecb/aggregate/3/exogenous.inc \ ecb/aggregate/3/parameters.inc \ ecb/aggregate/3/parameter-values.inc \ - ecb/aggregate/3/model.inc + ecb/aggregate/3/model.inc \ + estimation/method_of_moments/RBC_MoM_common.inc \ + estimation/method_of_moments/RBC_MoM_steady_helper.m \ + histval_initval_file_unit_tests.m \ + histval_initval_file/my_assert.m \ + histval_initval_file/ramst_data.xls \ + histval_initval_file/ramst_data.xlsx \ + histval_initval_file/ramst_initval_file_data.m if ENABLE_MATLAB check-local: check-matlab @@ -1281,7 +1328,7 @@ clean-local: rm -f estimation/test_matrix.mat - rm -f initval_file/ramst_initval_file_data_col_vec_mat.mat initval_file/ramst_initval_file_data_row_vec_mat.mat initval_file/ramst_initval_file_excel.xls + rm -f histval_initval_file/ramst_initval_file_data_col_vec_mat.mat histval_initval_file/ramst_initval_file_data_row_vec_mat.mat histval_initval_file/ramst_initval_file_excel.xls rm -f loglinear/results_exp_histval.mat loglinear/results_exp.mat diff --git a/tests/analytic_derivatives/BrockMirman_PertParamsDerivs.mod b/tests/analytic_derivatives/BrockMirman_PertParamsDerivs.mod index e2b407c49227c238b52c64616d87c1fdbc1b649e..4d2809c678de29a29c072787d44f063a911c88f4 100644 --- a/tests/analytic_derivatives/BrockMirman_PertParamsDerivs.mod +++ b/tests/analytic_derivatives/BrockMirman_PertParamsDerivs.mod @@ -125,10 +125,6 @@ end; calib_params = M_.params; calib_Sigma_e = M_.Sigma_e; -/* Skip test under MATLAB R2009b - MATLAB crashes, most likely due to an internal bug */ -if isoctave || ~matlab_ver_less_than('7.10') - stoch_simul(order=@{ORDER},nograph,irf=0,periods=0); identification(order=@{ORDER},nograph,no_identification_strength); @@ -475,5 +471,3 @@ for jj = 1:2 end end end - -end % Skip test under old MATLAB diff --git a/tests/bgp/nk-1/nk.mod b/tests/bgp/nk-1/nk.mod index aa0c3721a9ab2bb250dfb14b31fe15d0813be09a..22e877532d9816f2750c1588ac9c03327b9602cb 100644 --- a/tests/bgp/nk-1/nk.mod +++ b/tests/bgp/nk-1/nk.mod @@ -21,7 +21,7 @@ end; verbatim; bgp.write(M_); - if isoctave || matlab_ver_less_than('8.1') + if isoctave options = optimset('Display', 'iter', 'MaxFunEvals', 1000000,'MaxIter',100000,'Jacobian','on','TolFun',1e-8,'TolX',1e-8); else options = optimoptions('fsolve','Display','iter','Algorithm','levenberg-marquardt','MaxFunctionEvaluations',1000000,'MaxIterations',100000,'SpecifyObjectiveGradient',true,'FunctionTolerance',1e-8,'StepTolerance',1e-8); diff --git a/tests/bgp/ramsey-1/ramsey.mod b/tests/bgp/ramsey-1/ramsey.mod index 54e71e58378ff072fcfe890e6e451d2acd8c8d08..0a66aa14f190fcdd2d0addcd62d47bfb220cb8b6 100644 --- a/tests/bgp/ramsey-1/ramsey.mod +++ b/tests/bgp/ramsey-1/ramsey.mod @@ -16,7 +16,7 @@ end; verbatim; bgp.write(M_); - if isoctave || matlab_ver_less_than('8.1') + if isoctave options = optimset('Display', 'iter', 'MaxFunEvals', 1000000,'MaxIter',100000,'Jacobian','on','TolFun',1e-6,'TolX',1e-6); else options = optimoptions('fsolve','Display','iter','Algorithm','levenberg-marquardt','MaxFunctionEvaluations',1000000,'MaxIterations',100000,'SpecifyObjectiveGradient',true,'FunctionTolerance',1e-6,'StepTolerance',1e-6); diff --git a/tests/bgp/solow-1/solow.mod b/tests/bgp/solow-1/solow.mod index 7ef45993fc9699ce8f012359d3bc6b1ce4a77cd1..e4d9fa8be824a3d6e22e396d24137a362459ff1b 100644 --- a/tests/bgp/solow-1/solow.mod +++ b/tests/bgp/solow-1/solow.mod @@ -45,7 +45,7 @@ verbatim; GY = NaN(MC,1); GK = NaN(MC,1); EG = NaN(MC,1); - if isoctave || matlab_ver_less_than('8.1') + if isoctave options = optimset('Display', 'off', 'MaxFunEvals', 1000000,'MaxIter',100000,'Jacobian','on','TolFun',1e-8,'TolX',1e-8); else options = optimoptions('fsolve','Display','off','Algorithm','levenberg-marquardt','MaxFunctionEvaluations',1000000,'MaxIterations',100000,'SpecifyObjectiveGradient',true,'FunctionTolerance',1e-8,'StepTolerance',1e-8); diff --git a/tests/block_bytecode/ls2003.mod b/tests/block_bytecode/ls2003.mod index f4fbe7b582a2a006480e5de0d64d978edf8110c7..87fca4a9f7352fa0fa6112c160a761240d8497c8 100644 --- a/tests/block_bytecode/ls2003.mod +++ b/tests/block_bytecode/ls2003.mod @@ -16,18 +16,18 @@ rho_A = 0.2; rho_ys = 0.9; rho_pies = 0.7; -@#if !block && !bytecode +@#if !block && !bytecode && !use_dll model; -@#else -@# if block && !bytecode +@#elseif block && !bytecode && !use_dll model(block, cutoff=0); -@# else -@# if !block && bytecode +@#elseif !block && bytecode model(bytecode); -@# else +@#elseif block && bytecode model(block, bytecode, cutoff=0); -@# endif -@# endif +@#elseif !block && use_dll +model(use_dll); +@#else +model(block, use_dll, cutoff=0); @#endif y = y(+1) - (tau +alpha*(2-alpha)*(1-tau))*(R-pie(+1))-alpha*(tau +alpha*(2-alpha)*(1-tau))*dq(+1) + alpha*(2-alpha)*((1-tau)/tau)*(y_s-y_s(+1))-A(+1); pie = exp(-rr/400)*pie(+1)+alpha*exp(-rr/400)*dq(+1)-alpha*dq+(k/(tau+alpha*(2-alpha)*(1-tau)))*y+alpha*(2-alpha)*(1-tau)/(tau*(tau+alpha*(2-alpha)*(1-tau)))*y_s; diff --git a/tests/block_bytecode/run_ls2003.m b/tests/block_bytecode/run_ls2003.m index 892f842290dbb987fca53d6c07e8e22d303bc731..518150814bb54e9ebb4e774a89b85dab22f4bacc 100644 --- a/tests/block_bytecode/run_ls2003.m +++ b/tests/block_bytecode/run_ls2003.m @@ -1,4 +1,4 @@ -function run_ls2003(block, bytecode, solve_algo, stack_solve_algo) +function run_ls2003(block, storage, solve_algo, stack_solve_algo) % Copyright (C) 2010-2013 Dynare Team % @@ -18,13 +18,15 @@ function run_ls2003(block, bytecode, solve_algo, stack_solve_algo) % along with Dynare. If not, see <http://www.gnu.org/licenses/>. disp(['TEST: ls2003 (block=' num2str(block) ', bytecode=' ... - num2str(bytecode) ', solve_algo=' num2str(solve_algo) ... - ', stack_solve_algo=' num2str(stack_solve_algo) ')...']); + num2str(storage==2) ', use_dll=' num2str(storage==1) ... + ', solve_algo=' num2str(solve_algo) ... + ', stack_solve_algo=' num2str(stack_solve_algo) ')...']); fid = fopen('ls2003_tmp.mod', 'w'); assert(fid > 0); fprintf(fid, ['@#define block = %d\n@#define bytecode = %d\n' ... + '@#define use_dll = %d\n' ... '@#define solve_algo = %d\n@#define stack_solve_algo = %d\n' ... - '@#include \"ls2003.mod\"\n'], block, bytecode, ... + '@#include \"ls2003.mod\"\n'], block, storage==2, storage==1, ... solve_algo, stack_solve_algo); fclose(fid); dynare('ls2003_tmp.mod','console') diff --git a/tests/conditional_variance_decomposition/example1.mod b/tests/conditional_variance_decomposition/example1.mod index e53ea53824776b458206c5d442c9b1fd3dbadd04..039fa99ab478e4996662de23c8322e0c6c490a06 100644 --- a/tests/conditional_variance_decomposition/example1.mod +++ b/tests/conditional_variance_decomposition/example1.mod @@ -76,7 +76,7 @@ for i=1:nvar SubsetOfVariables(i) = i_tmp; end -if isoctave || matlab_ver_less_than('8.1') +if isoctave [observable_pos,index_observables,index_subset]=intersect_stable(SubsetOfVariables,options_.varobs_id); else [observable_pos,index_observables,index_subset]=intersect(SubsetOfVariables,options_.varobs_id,'stable'); @@ -114,7 +114,7 @@ if max(abs(sum(oo_.variance_decomposition,2)-100))>2 error(['Variance decomposition at order ',num2str(options_.order),' does not work']) end -if isoctave || matlab_ver_less_than('8.1') +if isoctave [observable_pos,index_observables,index_subset]=intersect_stable(SubsetOfVariables,options_.varobs_id); else [observable_pos,index_observables,index_subset]=intersect(SubsetOfVariables,options_.varobs_id,'stable'); diff --git a/tests/data/mod1a.mod b/tests/data/mod1a.mod index f475bc52c0b45f3fdaf9dfe5ac01021c3dab41ab..a2f0ff9512d98b37e9b105b9854f12602b2e27aa 100644 --- a/tests/data/mod1a.mod +++ b/tests/data/mod1a.mod @@ -20,8 +20,4 @@ end; varobs dx dy; check; -if isoctave || ~matlab_ver_less_than('7.14') % xlsread is able to read XLSX without Excel installed since R2012a estimation(datafile='test.xlsx',nobs=1000,mh_replic=2000,mh_jscale=1.3); -else -estimation(datafile='test.xls',nobs=1000,mh_replic=2000,mh_jscale=1.3); -end diff --git a/tests/data/test.xls b/tests/data/test.xls deleted file mode 100644 index 0855a60d64b2efed348503f2e765c4378350ac7f..0000000000000000000000000000000000000000 Binary files a/tests/data/test.xls and /dev/null differ diff --git a/tests/dates/data_uav.xls b/tests/dates/data_uav.xls deleted file mode 100644 index d35bdc3ba9e5bc6932d3c5e3b5349f8471bfad41..0000000000000000000000000000000000000000 Binary files a/tests/dates/data_uav.xls and /dev/null differ diff --git a/tests/dates/dseries_interact.mod b/tests/dates/dseries_interact.mod index 50cc5b51db996955c96ce6b302c4a34bcabf704d..929b1b4894a94e60d44cada39621a9aa321ba7b9 100644 --- a/tests/dates/dseries_interact.mod +++ b/tests/dates/dseries_interact.mod @@ -41,17 +41,9 @@ varobs log_nn; %reading Excel sheet from column A on creates quarterly dseries starting in %1950 -if isoctave || ~matlab_ver_less_than('7.14') % xlsread is able to read XLSX without Excel installed since R2012a estimation(first_obs=2,datafile='data_uav.xlsx', xls_sheet=Tabelle1, xls_range=a1:b54, mh_replic=2, mh_nblocks=1, mh_jscale=1.1, mh_drop=0.8, plot_priors=0, smoother) log_nn nn hh ; -else -estimation(first_obs=2,datafile='data_uav.xls', xls_sheet=Tabelle1, xls_range=a1:b54, mh_replic=2, mh_nblocks=1, mh_jscale=1.1, mh_drop=0.8, plot_priors=0, smoother) log_nn nn hh ; -end shock_decomposition( parameter_set=posterior_median ) nn hh; %reading Excel sheet from column B on creates annual dseries starting with 1 -if isoctave || ~matlab_ver_less_than('7.14') % xlsread is able to read XLSX without Excel installed since R2012a estimation(first_obs=2,datafile='data_uav.xlsx', xls_sheet=Tabelle1, xls_range=b1:b54, mh_replic=2, mh_nblocks=1, mh_jscale=1.1, mh_drop=0.8, plot_priors=0, smoother) log_nn nn hh ; -else -estimation(first_obs=2,datafile='data_uav.xls', xls_sheet=Tabelle1, xls_range=b1:b54, mh_replic=2, mh_nblocks=1, mh_jscale=1.1, mh_drop=0.8, plot_priors=0, smoother) log_nn nn hh ; -end shock_decomposition( parameter_set=posterior_median ) nn hh; diff --git a/tests/decision_rules/third_order/FV2011.mod b/tests/decision_rules/third_order/FV2011.mod index 9f0e67963e0fae42b22518f1523a187c8acc5f0c..1d9dda95fd227510891b8de5c7f885372ba197d9 100644 --- a/tests/decision_rules/third_order/FV2011.mod +++ b/tests/decision_rules/third_order/FV2011.mod @@ -116,6 +116,6 @@ options_.solve_tolf=1E-12; steady(solve_algo=3); check; -stoch_simul(order=3,pruning,irf=0,nocorr,nofunctions,nomoments) C I Y H r D K lambda phi; +stoch_simul(order=3,pruning,irf=0,nofunctions,contemporaneous_correlation,TeX) C I Y H r D K lambda phi; comparison_policy_functions_dynare_mathematica; \ No newline at end of file diff --git a/tests/deterministic_simulations/rbc_det_exo_lag_2b.mod b/tests/deterministic_simulations/rbc_det_exo_lag_2b.mod index fb687f4c63300e5fd0379d7d3e48c4253aeca5e4..8f44c36fbb7faa6ab10cc4b124e1f3deea645c2e 100644 --- a/tests/deterministic_simulations/rbc_det_exo_lag_2b.mod +++ b/tests/deterministic_simulations/rbc_det_exo_lag_2b.mod @@ -81,4 +81,4 @@ rplot Capital; O=load('rbc_det_exo_lag_2a_results'); -fataltest(oo_.endo_simul(:,2:end),O.oo_.endo_simul); \ No newline at end of file +fataltest(oo_.endo_simul(:,2:end),O.oo_.endo_simul,1); diff --git a/tests/deterministic_simulations/rbc_det_exo_lag_2c.mod b/tests/deterministic_simulations/rbc_det_exo_lag_2c.mod index c6c8cbd88ed042d3b94f15ef4570a4bb3d89ef89..4d52ea0323f9c4c344cbf75162cef81954eb74b8 100644 --- a/tests/deterministic_simulations/rbc_det_exo_lag_2c.mod +++ b/tests/deterministic_simulations/rbc_det_exo_lag_2c.mod @@ -81,4 +81,4 @@ rplot Capital; O=load('rbc_det_exo_lag_2a_results'); -fataltest(oo_.endo_simul(:,2:end),O.oo_.endo_simul); \ No newline at end of file +fataltest(oo_.endo_simul(:,2:end),O.oo_.endo_simul,1); diff --git a/tests/estimation/fs2000_calibrated_covariance.mod b/tests/estimation/fs2000_calibrated_covariance.mod index 79d309786857d00238fc1fb57a88e1cdf4972b20..a2acc3387567c23ed748063b5981b9afe5bb7b2c 100644 --- a/tests/estimation/fs2000_calibrated_covariance.mod +++ b/tests/estimation/fs2000_calibrated_covariance.mod @@ -80,7 +80,7 @@ corr e_a, e_m, 0.5; stderr gp_obs, 0.5; end; -estimation(order=1,datafile=fsdat_simul,nobs=192, loglinear, mh_replic=2002, mh_nblocks=1, mh_jscale=0.8); +estimation(order=1,datafile=fsdat_simul,nobs=192, loglinear, mh_replic=0, mh_nblocks=1, mh_jscale=0.8,moments_varendo,consider_all_endogenous); if isequal(M_.Sigma_e(2,1),5e-5) || isequal(M_.Sigma_e(1,2),5e-5) error('Problem in overriding calibrated covariance of structural shocks by estimated correlation') @@ -88,3 +88,5 @@ end if ~isequal(M_.H(2,1),5e-5) || ~isequal(M_.H(1,2),5e-5) error('Problem in setting calibrated covariance of measurement errors') end + +stoch_simul(order=1,periods=1000); diff --git a/tests/estimation/method_of_moments/AnScho_MoM.mod b/tests/estimation/method_of_moments/AnScho_MoM.mod new file mode 100644 index 0000000000000000000000000000000000000000..2f9c2c24d83991ff411a3d189ac146de41fa9a0b --- /dev/null +++ b/tests/estimation/method_of_moments/AnScho_MoM.mod @@ -0,0 +1,272 @@ +% DSGE model used in replication files of +% An, Sungbae and Schorfheide, Frank, (2007), Bayesian Analysis of DSGE Models, Econometric Reviews, 26, issue 2-4, p. 113-172. +% Adapted by Willi Mutschler (@wmutschl, willi@mutschler.eu) +% ========================================================================= +% Copyright (C) 2020 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/>. +% ========================================================================= + +% Define testscenario +@#define orderApp = 2 +@#define estimParams = 1 + +% Note that we set the numerical optimization tolerance levels very large to speed up the testsuite +@#define optimizer = 4 + +var c p R g y z INFL INT YGR; +varexo e_r e_g e_z; +parameters tau nu kap cyst psi1 psi2 rhor rhog rhoz rrst pist gamst; + +varobs INT YGR INFL; + +tau = 2; +nu = 0.1; +kap = 0.33; +cyst = 0.85; +psi1 = 1.5; +psi2 = 0.125; +rhor = 0.75; +rhog = 0.95; +rhoz = 0.9; +rrst = 1; +pist = 3.2; +gamst = 0.55; + +model; +#pist2 = exp(pist/400); +#rrst2 = exp(rrst/400); +#bet = 1/rrst2; +#phi = tau*(1-nu)/nu/kap/pist2^2; +#gst = 1/cyst; +#cst = (1-nu)^(1/tau); +#yst = cst*gst; +#dy = y-y(-1); +1 = exp(-tau*c(+1)+tau*c+R-z(+1)-p(+1)); +(1-nu)/nu/phi/(pist2^2)*(exp(tau*c)-1) = (exp(p)-1)*((1-1/2/nu)*exp(p)+1/2/nu) - bet*(exp(p(+1))-1)*exp(-tau*c(+1)+tau*c+y(+1)-y+p(+1)); +exp(c-y) = exp(-g) - phi*pist2^2*gst/2*(exp(p)-1)^2; +R = rhor*R(-1) + (1-rhor)*psi1*p + (1-rhor)*psi2*(dy+z) + e_r/100; +g = rhog*g(-1) + e_g/100; +z = rhoz*z(-1) + e_z/100; +YGR = gamst+100*(dy+z); +INFL = pist+400*p; +INT = pist+rrst+4*gamst+400*R; +end; + +steady_state_model; + z = 0; p = 0; g = 0; r = 0; c = 0; y = 0; + YGR = gamst; INFL = pist; INT = pist + rrst + 4*gamst; +end; + +shocks; + var e_r = 0.20^2; + var e_g = 0.80^2; + var e_z = 0.45^2; + corr e_r,e_g = 0.2; +end; + +@#if estimParams == 0 +% Define only initial values without bounds +estimated_params; + %tau, 1.50; + %kap, 0.15; + psi1, 1.20; + psi2, 0.50; + rhor, 0.50; + %rhog, 0.50; + %rhoz, 0.50; + %rrst, 1.20; + %pist, 3.00; + gamst, 0.75; + stderr e_r, 0.30; + stderr e_g, 0.30; + stderr e_z, 0.30; + corr e_r,e_g, 0.10; +end; +@#endif + +@#if estimParams == 1 +% Define initial values and bounds +estimated_params; + %tau, 1.50, 1e-5, 10; + %kap, 0.15, 1e-5, 10; + psi1, 1.20, 1e-5, 10; + psi2, 0.50, 1e-5, 10; + rhor, 0.50, 1e-5, 0.99999; + %rhog, 0.50, 1e-5, 0.99999; + %rhoz, 0.50, 1e-5, 0.99999; + %rrst, 1.20, 1e-5, 10; + %pist, 3.00, 1e-5, 20; + gamst, 0.75, -5, 5; + stderr e_r, 0.30, 1e-8, 5; + stderr e_g, 0.30, 1e-8, 5; + stderr e_z, 0.30, 1e-8, 5; + corr e_r,e_g, 0.10, -1, 1; +end; +@#endif + +@#if estimParams == 2 +% Define prior distribution +estimated_params; + %tau, 1.50, 1e-5, 10, gamma_pdf, 2.00, 0.50; + %kap, 0.15, 1e-5, 10, gamma_pdf, 0.33, 0.10; + psi1, 1.20, 1e-5, 10, gamma_pdf, 1.50, 0.25; + psi2, 0.50, 1e-5, 10, gamma_pdf, 0.125, 0.25; + rhor, 0.50, 1e-5, 0.99999, beta_pdf, 0.50, 0.20; + %rhog, 0.50, 1e-5, 0.99999, beta_pdf, 0.80, 0.10; + %rhoz, 0.50, 1e-5, 0.99999, beta_pdf, 0.66, 0.15; + %rrst, 1.20, 1e-5, 10, gamma_pdf, 0.50, 0.50; + %pist, 3.00, 1e-5, 20, gamma_pdf, 7.00, 2.00; + gamst, 0.75, -5, 5, normal_pdf, 0.40, 0.20; + stderr e_r, 0.30, 1e-8, 5, inv_gamma_pdf, 0.50, 0.26; + stderr e_g, 0.30, 1e-8, 5, inv_gamma_pdf, 1.25, 0.65; + stderr e_z, 0.30, 1e-8, 5, inv_gamma_pdf, 0.63, 0.33; + corr e_r,e_g, 0.10, -1, 1, uniform_pdf, , , -1, 1; +end; +@#endif + + +% Simulate data +stoch_simul(order=@{orderApp},pruning,nodisplay,nomoments,periods=750,drop=500); +save('AnScho_MoM_data_@{orderApp}.mat', options_.varobs{:} ); +pause(1); + + +%-------------------------------------------------------------------------- +% Method of Moments Estimation +%-------------------------------------------------------------------------- +matched_moments; +YGR; +INFL; +INT; +%second-order contemporenous product moments +YGR*YGR; +YGR*INFL; +YGR*INT; +INFL*INFL; +INFL*INT; +INT*INT; +%second-order temporal product moments +YGR*YGR(-1); +INT*INT(-1); +INFL*INFL(-1); +end; + +% get indices in declaration order +iYGR = strmatch('YGR', M_.endo_names,'exact'); +iINFL = strmatch('INFL', M_.endo_names,'exact'); +iINT = strmatch('INT', M_.endo_names,'exact'); +% first entry: number of variable in declaration order +% second entry: lag +% third entry: power + +matched_moments_ = { + %first-order product moments + [iYGR ] [0 ], [1 ]; + [iINFL ] [0 ], [1 ]; + [iINT ] [0 ], [1 ]; + %second-order contemporenous product moments + [iYGR iYGR ] [0 0], [1 1]; + [iYGR iINFL] [0 0], [1 1]; + [iYGR iINT ] [0 0], [1 1]; + [iINFL iINFL] [0 0], [1 1]; + [iINFL iINT ] [0 0], [1 1]; + [iINT iINT ] [0 0], [1 1]; + %second-order temporal product moments + [iYGR iYGR ] [0 -1], [1 1]; + %[iINT iYGR ] [0 -1], [1 1]; + %[iINFL iYGR ] [0 -1], [1 1]; + %[iYGR iINT ] [0 -1], [1 1]; + [iINT iINT ] [0 -1], [1 1]; + %[iINFL iINT ] [0 -1], [1 1]; + %[iYGR iINFL] [0 -1], [1 1]; + %[iINT iINFL] [0 -1], [1 1]; + [iINFL iINFL] [0 -1], [1 1]; +}; + +if ~isequal(M_.matched_moments,matched_moments_) + error('Translation to matched_moments-block failed') +end + +@#for mommethod in ["GMM", "SMM"] + method_of_moments( + % Necessery options + mom_method = @{mommethod} % method of moments method; possible values: GMM|SMM + , datafile = 'AnScho_MoM_data_@{orderApp}.mat' % name of filename with data + + % Options for both GMM and SMM + % , bartlett_kernel_lag = 20 % bandwith in optimal weighting matrix + , order = @{orderApp} % order of Taylor approximation in perturbation + % , penalized_estimator % use penalized optimization + , pruning % use pruned state space system at higher-order + % , verbose % display and store intermediate estimation results + , weighting_matrix = ['optimal'] % weighting matrix in moments distance objective function; possible values: OPTIMAL|IDENTITY_MATRIX|DIAGONAL|filename + , additional_optimizer_steps = [4] % vector of numbers for the iterations in the 2-step feasible method of moments + % , prefilter=0 % demean each data series by its empirical mean and use centered moments + % + % Options for SMM + % , bounded_shock_support % trim shocks in simulation to +- 2 stdev + % , drop = 500 % number of periods dropped at beginning of simulation + % , seed = 24051986 % seed used in simulations + % , simulation_multiple = 5 % multiple of the data length used for simulation + % + % General options + %, dirname = 'MM' % directory in which to store estimation output + % , graph_format = EPS % specify the file format(s) for graphs saved to disk + % , nodisplay % do not display the graphs, but still save them to disk + % , nograph % do not create graphs (which implies that they are not saved to the disk nor displayed) + % , noprint % do not print stuff to console + % , plot_priors = 1 % control plotting of priors + % , prior_trunc = 1e-10 % probability of extreme values of the prior density that is ignored when computing bounds for the parameters + % , TeX % print TeX tables and graphics + % + % Data and model options + %, first_obs = 501 % number of first observation + % , logdata % if loglinear is set, this option is necessary if the user provides data already in logs, otherwise the log transformation will be applied twice (this may result in complex data) + % , loglinear % computes a log-linear approximation of the model instead of a linear approximation + , nobs = 250 % number of observations + % , xls_sheet = willi % name of sheet with data in Excel + % , xls_range = B2:D200 % range of data in Excel sheet + % + % Optimization options that can be set by the user in the mod file, otherwise default values are provided + % , analytic_derivation % uses analytic derivatives to compute standard errors for GMM + %, huge_number=1D10 % value for replacing the infinite bounds on parameters by finite numbers. Used by some optimizers for numerical reasons + , mode_compute = @{optimizer} % specifies the optimizer for minimization of moments distance, note that by default there is a new optimizer + %, optim = ('TolFun', 1e-5 + % ,'TolX', 1e-6 + % ) % a list of NAME and VALUE pairs to set options for the optimization routines. Available options depend on mode_compute + %, silent_optimizer % run minimization of moments distance silently without displaying results or saving files in between + % , tolf = 1e-5 % convergence criterion on function value for numerical differentiation + % , tolx = 1e-6 % convergence criterion on funciton input for numerical differentiation + % + % % Numerical algorithms options + % , aim_solver % Use AIM algorithm to compute perturbation approximation + % , dr=default % method used to compute the decision rule; possible values are DEFAULT, CYCLE_REDUCTION, LOGARITHMIC_REDUCTION + % , dr_cycle_reduction_tol = 1e-7 % convergence criterion used in the cycle reduction algorithm + % , dr_logarithmic_reduction_maxiter = 100 % maximum number of iterations used in the logarithmic reduction algorithm + % , dr_logarithmic_reduction_tol = 1e-12 % convergence criterion used in the cycle reduction algorithm + % , k_order_solver % use k_order_solver in higher order perturbation approximations + % , lyapunov = DEFAULT % algorithm used to solve lyapunov equations; possible values are DEFAULT, FIXED_POINT, DOUBLING, SQUARE_ROOT_SOLVER + % , lyapunov_complex_threshold = 1e-15 % complex block threshold for the upper triangular matrix in symmetric Lyapunov equation solver + % , lyapunov_fixed_point_tol = 1e-10 % convergence criterion used in the fixed point Lyapunov solver + % , lyapunov_doubling_tol = 1e-16 % convergence criterion used in the doubling algorithm + % , sylvester = default % algorithm to solve Sylvester equation; possible values are DEFAULT, FIXED_POINT + % , sylvester_fixed_point_tol = 1e-12 % convergence criterion used in the fixed point Sylvester solver + % , qz_criterium = 0.999999 % value used to split stable from unstable eigenvalues in reordering the Generalized Schur decomposition used for solving first order problems [IS THIS CORRET @wmutschl] + % , qz_zero_threshold = 1e-6 % value used to test if a generalized eigenvalue is 0/0 in the generalized Schur decomposition + ); +@#endfor + diff --git a/tests/estimation/method_of_moments/RBC_Andreasen_Data_2.mat b/tests/estimation/method_of_moments/RBC_Andreasen_Data_2.mat new file mode 100644 index 0000000000000000000000000000000000000000..0b2ba62defdaab77aa663f2907ae16801837f6b6 Binary files /dev/null and b/tests/estimation/method_of_moments/RBC_Andreasen_Data_2.mat differ diff --git a/tests/estimation/method_of_moments/RBC_MoM_Andreasen.mod b/tests/estimation/method_of_moments/RBC_MoM_Andreasen.mod new file mode 100644 index 0000000000000000000000000000000000000000..ae69840050f5f33165cbf36d2a13c04f84feda1e --- /dev/null +++ b/tests/estimation/method_of_moments/RBC_MoM_Andreasen.mod @@ -0,0 +1,227 @@ +% Tests SMM and GMM routines +% +% Copyright (C) 2020 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/>. +% ========================================================================= + +% Define testscenario +@#define orderApp = 2 +@#define estimParams = 1 + +% Note that we will set the numerical optimization tolerance levels very large to speed up the testsuite +@#define optimizer = 13 + + +@#include "RBC_MoM_common.inc" + +shocks; +var u_a; stderr 0.0072; +end; + +varobs c iv n; + + +@#if estimParams == 0 +estimated_params; + DELTA, 0.025; + BETTA, 0.984; + B, 0.5; + ETAc, 2; + ALFA, 0.667; + RHOA, 0.979; + stderr u_a, 0.0072; +end; +@#endif + +@#if estimParams == 1 +estimated_params; + DELTA, , 0, 1; + BETTA, , 0, 1; + B, , 0, 1; + ETAc, , 0, 10; + ALFA, , 0, 1; + RHOA, , 0, 1; + stderr u_a, , 0, 1; +end; +@#endif + +@#if estimParams == 2 +estimated_params; + DELTA, 0.025, 0, 1, normal_pdf, 0.02, 0.5; + BETTA, 0.98, 0, 1, beta_pdf, 0.90, 0.25; + B, 0.45, 0, 1, normal_pdf, 0.40, 0.5; + %ETAl, 1, 0, 10, normal_pdf, 0.25, 0.0.1; + ETAc, 1.8, 0, 10, normal_pdf, 1.80, 0.5; + ALFA, 0.65, 0, 1, normal_pdf, 0.60, 0.5; + RHOA, 0.95, 0, 1, normal_pdf, 0.90, 0.5; + stderr u_a, 0.01, 0, 1, normal_pdf, 0.01, 0.5; + %THETA, 3.48, 0, 10, normal_pdf, 0.25, 0.0.1; +end; +@#endif + +% Simulate data +%stoch_simul(order=@{orderApp},pruning,nodisplay,nomoments,periods=500); +%save('RBC_MoM_data_@{orderApp}.mat', options_.varobs{:} ); +%pause(1); + + +estimated_params_init(use_calibration); +end; + + +%-------------------------------------------------------------------------- +% Method of Moments Estimation +%-------------------------------------------------------------------------- +matched_moments; +c; +n; +iv; +c*c; +c*iv; +iv*n; +iv*iv; +n*c; +n*n; +c*c(-1); +n*n(-1); +iv*iv(-1); + +c*c(-3); +n*n(-3); +iv*iv(-3); + +c*c(-5); +n*n(-5); +iv*iv(-5); +end; + +% get indices in declaration order +ic = strmatch('c', M_.endo_names,'exact'); +iiv = strmatch('iv', M_.endo_names,'exact'); +in = strmatch('n', M_.endo_names,'exact'); +% first entry: number of variable in declaration order +% second entry: lag +% third entry: power + +matched_moments_ = { + [ic ] [0 ], [1 ]; + [in ] [0 ], [1 ]; + [iiv ] [0 ], [1 ]; + + [ic ic ] [0 0], [1 1]; + [ic iiv] [0 0], [1 1]; + %[ic in ] [0 0], [1 1]; + %[iiv ic ] [0 0], [1 1]; + [in iiv] [0 0], [1 1]; + [iiv iiv] [0 0], [1 1]; + [ic in] [0 0], [1 1]; + %[in iiv] [0 0], [1 1]; + [in in ] [0 0], [1 1]; + + [ic ic ] [0 -1], [1 1]; + [in in ] [0 -1], [1 1]; + [iiv iiv] [0 -1], [1 1]; + + [ic ic ] [0 -3], [1 1]; + [in in ] [0 -3], [1 1]; + [iiv iiv] [0 -3], [1 1]; + + [ic ic ] [0 -5], [1 1]; + [in in ] [0 -5], [1 1]; + [iiv iiv] [0 -5], [1 1]; + +}; + +if ~isequal(M_.matched_moments,matched_moments_) + error('Translation to matched_moments-block failed') +end + + + method_of_moments( + % Necessery options + mom_method = GMM % method of moments method; possible values: GMM|SMM + , datafile = 'RBC_Andreasen_Data_2.mat' % name of filename with data + + % Options for both GMM and SMM + %, bartlett_kernel_lag = 20 % bandwith in optimal weighting matrix + , order = @{orderApp} % order of Taylor approximation in perturbation + %, penalized_estimator % use penalized optimization + %, pruning % use pruned state space system at higher-order + %, verbose % display and store intermediate estimation results + , weighting_matrix = ['DIAGONAL','OPTIMAL'] % weighting matrix in moments distance objective function; possible values: OPTIMAL|IDENTITY_MATRIX|DIAGONAL|filename + %, weighting_matrix_scaling_factor=1 + , additional_optimizer_steps = [13] % vector of additional mode-finders run after mode_compute + %, prefilter=0 % demean each data series by its empirical mean and use centered moments + % + % Options for SMM + %, bounded_shock_support % trim shocks in simulation to +- 2 stdev + %, drop = 500 % number of periods dropped at beginning of simulation + %, seed = 24051986 % seed used in simulations + %, simulation_multiple = 5 % multiple of the data length used for simulation + %, burnin = 200 + % + % General options + %, dirname = 'MM' % directory in which to store estimation output + %, graph_format = EPS % specify the file format(s) for graphs saved to disk + %, nodisplay % do not display the graphs, but still save them to disk + %, nograph % do not create graphs (which implies that they are not saved to the disk nor displayed) + %, noprint % do not print stuff to console + %, plot_priors = 1 % control plotting of priors + %, prior_trunc = 1e-10 % probability of extreme values of the prior density that is ignored when computing bounds for the parameters + , TeX % print TeX tables and graphics + % + % Data and model options + %, first_obs = 501 % number of first observation + %, logdata % if loglinear is set, this option is necessary if the user provides data already in logs, otherwise the log transformation will be applied twice (this may result in complex data) + %, loglinear % computes a log-linear approximation of the model instead of a linear approximation + %, nobs = 50 % number of observations + % , xls_sheet = willi % name of sheet with data in Excel + % , xls_range = B2:D200 % range of data in Excel sheet + % + % Optimization options that can be set by the user in the mod file, otherwise default values are provided + %, analytic_derivation % uses analytic derivatives to compute standard errors for GMM + %, huge_number=1D10 % value for replacing the infinite bounds on parameters by finite numbers. Used by some optimizers for numerical reasons + , mode_compute = 13 % specifies the optimizer for minimization of moments distance, note that by default there is a new optimizer + , optim = ('TolFun', 1D-6 + ,'TolX', 1D-6 + ) % a list of NAME and VALUE pairs to set options for the optimization routines. Available options depend on mode_compute + %, silent_optimizer % run minimization of moments distance silently without displaying results or saving files in between + , se_tolx = 1e-6 % convergence criterion on funciton input for numerical differentiation + % + % % Numerical algorithms options + %, aim_solver % Use AIM algorithm to compute perturbation approximation + %, dr=DEFAULT % method used to compute the decision rule; possible values are DEFAULT, CYCLE_REDUCTION, LOGARITHMIC_REDUCTION + %, dr_cycle_reduction_tol = 1e-7 % convergence criterion used in the cycle reduction algorithm + %, dr_logarithmic_reduction_maxiter = 100 % maximum number of iterations used in the logarithmic reduction algorithm + %, dr_logarithmic_reduction_tol = 1e-12 % convergence criterion used in the cycle reduction algorithm + %, k_order_solver % use k_order_solver in higher order perturbation approximations + %, lyapunov = DEFAULT % algorithm used to solve lyapunov equations; possible values are DEFAULT, FIXED_POINT, DOUBLING, SQUARE_ROOT_SOLVER + %, lyapunov_complex_threshold = 1e-15 % complex block threshold for the upper triangular matrix in symmetric Lyapunov equation solver + %, lyapunov_fixed_point_tol = 1e-10 % convergence criterion used in the fixed point Lyapunov solver + %, lyapunov_doubling_tol = 1e-16 % convergence criterion used in the doubling algorithm + %, sylvester = default % algorithm to solve Sylvester equation; possible values are DEFAULT, FIXED_POINT + %, sylvester_fixed_point_tol = 1e-12 % convergence criterion used in the fixed point Sylvester solver + %, qz_criterium = 0.999999 % value used to split stable from unstable eigenvalues in reordering the Generalized Schur decomposition used for solving first order problems [IS THIS CORRET @wmutschl] + %, qz_zero_threshold = 1e-6 % value used to test if a generalized eigenvalue is 0/0 in the generalized Schur decomposition + , mode_check + %, mode_check_neighbourhood_size=0.5 + %, mode_check_symmetric_plots=0 + %, mode_check_number_of_points=25 + ); + + + diff --git a/tests/estimation/method_of_moments/RBC_MoM_SMM_ME.mod b/tests/estimation/method_of_moments/RBC_MoM_SMM_ME.mod new file mode 100644 index 0000000000000000000000000000000000000000..9648626709d0413dec27ceec9d09232a6b2da556 --- /dev/null +++ b/tests/estimation/method_of_moments/RBC_MoM_SMM_ME.mod @@ -0,0 +1,210 @@ +% +% 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/>. +% ========================================================================= + +% Define testscenario +@#define orderApp = 1 +@#define estimParams = 0 + +% Note that we will set the numerical optimization tolerance levels very large to speed up the testsuite +@#define optimizer = 5 + +@#include "RBC_MoM_common.inc" + +shocks; +var u_a; stderr 0.0072; +var n; stderr 0.01; +end; + +varobs n c iv; + +@#if estimParams == 0 +estimated_params; + DELTA, 0.025; + BETTA, 0.984; + B, 0.5; + %ETAl, 1; + ETAc, 1; + ALFA, 0.667; + RHOA, 0.979; + stderr u_a, 0.0072; + %THETA, 3.48; + stderr n, 0.01; + +end; +@#endif + +@#if estimParams == 1 +estimated_params; + DELTA, 0.02, 0, 1; + BETTA, 0.90, 0, 1; + B, 0.40, 0, 1; + %ETAl, 1, 0, 10; + ETAc, 1.80, 0, 10; + ALFA, 0.60, 0, 1; + RHOA, 0.90, 0, 1; + stderr u_a, 0.01, 0, 1; + stderr n, 0.01, 0, 1; +end; +@#endif + +@#if estimParams == 2 +estimated_params; + DELTA, 0.02, 0, 1, normal_pdf, 0.02, 0.5; + BETTA, 0.90, 0, 1, beta_pdf, 0.90, 0.25; + B, 0.40, 0, 1, normal_pdf, 0.40, 0.5; + %ETAl, 1, 0, 10, normal_pdf, 0.25, 0.0.1; + ETAc, 1.80, 0, 10, normal_pdf, 1.80, 0.5; + ALFA, 0.60, 0, 1, normal_pdf, 0.60, 0.5; + RHOA, 0.90, 0, 1, normal_pdf, 0.90, 0.5; + stderr u_a, 0.01, 0, 1, normal_pdf, 0.01, 0.5; + stderr n, 0.001, 0, 1, normal_pdf, 0.01, 0.5; +end; +@#endif + +% Simulate data +stoch_simul(order=@{orderApp},pruning,nodisplay,nomoments,periods=250); +save('RBC_MoM_data_@{orderApp}.mat', options_.varobs{:} ); +pause(1); + + + +%-------------------------------------------------------------------------- +% Method of Moments Estimation +%-------------------------------------------------------------------------- +% matched_moments blocks : We don't have an interface yet +matched_moments; +c; +n; +iv; + +c*c; +c*iv; +c*n; +iv*c; +iv*iv; +iv*n; +n*n; + +c*c(-1); +n*n(-1); +iv*iv(-1); +end; + +% get indices in declaration order +ic = strmatch('c', M_.endo_names,'exact'); +iiv = strmatch('iv', M_.endo_names,'exact'); +in = strmatch('n', M_.endo_names,'exact'); +% first entry: number of variable in declaration order +% second entry: lag +% third entry: power + +matched_moments_ = { + [ic ] [0 ], [1 ]; + [in ] [0 ], [1 ]; + [iiv ] [0 ], [1 ]; + [ic ic ] [0 0], [1 1]; + [ic iiv] [0 0], [1 1]; + [ic in ] [0 0], [1 1]; + [ic iiv] [0 0], [1 1]; + [iiv iiv] [0 0], [1 1]; + [in iiv] [0 0], [1 1]; +% [in ic ] [0 0], [1 1]; +% [in iiv] [0 0], [1 1]; + [in in ] [0 0], [1 1]; + [ic ic ] [0 -1], [1 1]; + [in in ] [0 -1], [1 1]; + [iiv iiv] [0 -1], [1 1]; +% [iiv iiv] [0 -1], [1 1]; +}; + +if ~isequal(M_.matched_moments,matched_moments_) + error('Translation to matched_moments-block failed') +end + +@#for mommethod in ["SMM"] + method_of_moments( + % Necessery options + mom_method = @{mommethod} % method of moments method; possible values: GMM|SMM + , datafile = 'RBC_MoM_data_@{orderApp}.mat' % name of filename with data + + % Options for both GMM and SMM + % , bartlett_kernel_lag = 20 % bandwith in optimal weighting matrix + , order = @{orderApp} % order of Taylor approximation in perturbation + % , penalized_estimator % use penalized optimization + , pruning % use pruned state space system at higher-order + % , verbose % display and store intermediate estimation results + , weighting_matrix = ['identity_matrix'] % weighting matrix in moments distance objective function; possible values: OPTIMAL|IDENTITY_MATRIX|DIAGONAL|filename + , weighting_matrix_scaling_factor = 10 + , burnin=250 + %, additional_optimizer_steps = [4] % vector of additional mode-finders run after mode_compute + % , prefilter=0 % demean each data series by its empirical mean and use centered moments + % + % Options for SMM + % , bounded_shock_support % trim shocks in simulation to +- 2 stdev + % , drop = 500 % number of periods dropped at beginning of simulation + % , seed = 24051986 % seed used in simulations + % , simulation_multiple = 5 % multiple of the data length used for simulation + % + % General options + %, dirname = 'MM' % directory in which to store estimation output + % , graph_format = EPS % specify the file format(s) for graphs saved to disk + % , nodisplay % do not display the graphs, but still save them to disk + % , nograph % do not create graphs (which implies that they are not saved to the disk nor displayed) + % , noprint % do not print stuff to console + % , plot_priors = 1 % control plotting of priors + % , prior_trunc = 1e-10 % probability of extreme values of the prior density that is ignored when computing bounds for the parameters + % , TeX % print TeX tables and graphics + % + % Data and model options + %, first_obs = 501 % number of first observation + % , logdata % if loglinear is set, this option is necessary if the user provides data already in logs, otherwise the log transformation will be applied twice (this may result in complex data) + % , loglinear % computes a log-linear approximation of the model instead of a linear approximation + %, nobs = 500 % number of observations + % , xls_sheet = willi % name of sheet with data in Excel + % , xls_range = B2:D200 % range of data in Excel sheet + % + % Optimization options that can be set by the user in the mod file, otherwise default values are provided + % , analytic_derivation % uses analytic derivatives to compute standard errors for GMM + %, huge_number=1D10 % value for replacing the infinite bounds on parameters by finite numbers. Used by some optimizers for numerical reasons + , mode_compute = @{optimizer} % specifies the optimizer for minimization of moments distance, note that by default there is a new optimizer + %, optim = ('TolFun', 1e-3 + % ,'TolX', 1e-5 + % ) % a list of NAME and VALUE pairs to set options for the optimization routines. Available options depend on mode_compute + %, silent_optimizer % run minimization of moments distance silently without displaying results or saving files in between + % , tolf = 1e-5 % convergence criterion on function value for numerical differentiation + % , tolx = 1e-6 % convergence criterion on funciton input for numerical differentiation + % + % % Numerical algorithms options + % , aim_solver % Use AIM algorithm to compute perturbation approximation + % , dr=default % method used to compute the decision rule; possible values are DEFAULT, CYCLE_REDUCTION, LOGARITHMIC_REDUCTION + % , dr_cycle_reduction_tol = 1e-7 % convergence criterion used in the cycle reduction algorithm + % , dr_logarithmic_reduction_maxiter = 100 % maximum number of iterations used in the logarithmic reduction algorithm + % , dr_logarithmic_reduction_tol = 1e-12 % convergence criterion used in the cycle reduction algorithm + % , k_order_solver % use k_order_solver in higher order perturbation approximations + % , lyapunov = DEFAULT % algorithm used to solve lyapunov equations; possible values are DEFAULT, FIXED_POINT, DOUBLING, SQUARE_ROOT_SOLVER + % , lyapunov_complex_threshold = 1e-15 % complex block threshold for the upper triangular matrix in symmetric Lyapunov equation solver + % , lyapunov_fixed_point_tol = 1e-10 % convergence criterion used in the fixed point Lyapunov solver + % , lyapunov_doubling_tol = 1e-16 % convergence criterion used in the doubling algorithm + % , sylvester = default % algorithm to solve Sylvester equation; possible values are DEFAULT, FIXED_POINT + % , sylvester_fixed_point_tol = 1e-12 % convergence criterion used in the fixed point Sylvester solver + % , qz_criterium = 0.999999 % value used to split stable from unstable eigenvalues in reordering the Generalized Schur decomposition used for solving first order problems [IS THIS CORRET @wmutschl] + % , qz_zero_threshold = 1e-6 % value used to test if a generalized eigenvalue is 0/0 in the generalized Schur decomposition + ); +@#endfor + + + diff --git a/tests/estimation/method_of_moments/RBC_MoM_common.inc b/tests/estimation/method_of_moments/RBC_MoM_common.inc new file mode 100644 index 0000000000000000000000000000000000000000..d480e35c06253961f95edf3d597a61ef44bd14c8 --- /dev/null +++ b/tests/estimation/method_of_moments/RBC_MoM_common.inc @@ -0,0 +1,80 @@ +% RBC model used in replication files of +% Andreasen, Fernández-Villaverde, Rubio-RamÃrez (2018): "The Pruned State-Space System for Non-Linear DSGE Models: Theory and Empirical Applications", Review of Economic Studies, 85(1):1-49. +% Adapted by Willi Mutschler (@wmutschl, willi@mutschler.eu) +% ========================================================================= +% Copyright (C) 2020 Dynare Team + +var k $K$ + c $C$ + a $A$ + iv $I$ + y $Y$ + la $\lambda$ + n $N$ + rk ${r^k}$ + w $W$ + ; + +predetermined_variables k; + +varexo u_a ${\varepsilon^{a}}$ + ; + +parameters DELTA $\delta$ + BETTA $\beta$ + B $B$ + ETAl $\eta_l$ + ETAc $\eta_c$ + THETA $\theta$ + ALFA $\alpha$ + RHOA $\rho_a$ + ; + +DELTA = 0.025; +BETTA = 0.984; +B = 0.5; +ETAl = 1; +ETAc = 2; +THETA = 3.48; +ALFA = 0.667; +RHOA = 0.979; + +model; +0 = -exp(la) +(exp(c)-B*exp(c(-1)))^(-ETAc) - BETTA*B*(exp(c(+1))-B*exp(c))^(-ETAc); +0 = -THETA*(1-exp(n))^-ETAl + exp(la)*exp(w); +0 = -exp(la) + BETTA*exp(la(+1))*(exp(rk(+1)) + (1-DELTA)); +0 = -exp(a)*(1-ALFA)*exp(k)^(-ALFA)*exp(n)^(ALFA) + exp(rk); +0 = -exp(a)*ALFA*exp(k)^(1-ALFA)*exp(n)^(ALFA-1) + exp(w); +0 = -exp(c) - exp(iv) + exp(y); +0 = -exp(y) + exp(a)*exp(k)^(1-ALFA)*exp(n)^(ALFA); +0 = -exp(k(+1)) + (1-DELTA)*exp(k) + exp(iv); +0 = -log(exp(a)) + RHOA*log(exp(a(-1))) + u_a; +end; + +steady_state_model; +A = 1; +RK = 1/BETTA - (1-DELTA); +K_O_N = (RK/(A*(1-ALFA)))^(-1/ALFA); +W = A*ALFA*(K_O_N)^(1-ALFA); +IV_O_N = DELTA*K_O_N; +Y_O_N = A*K_O_N^(1-ALFA); +C_O_N = Y_O_N - IV_O_N; + +[N, info]=RBC_MoM_steady_helper(THETA,ETAl,ETAc,BETTA,B,C_O_N,W); +C=C_O_N*N; +Y=Y_O_N*N; +IV=IV_O_N*N; +K=K_O_N*N; +LA = (C-B*C)^(-ETAc)-BETTA*B*(C-B*C)^(-ETAc); + +k=log(K); +c=log(C); +a=log(A); +iv=log(IV); +y=log(Y); +la=log(LA); +n=log(N); +rk=log(RK); +w=log(W); + +end; \ No newline at end of file diff --git a/tests/estimation/method_of_moments/RBC_MoM_prefilter.mod b/tests/estimation/method_of_moments/RBC_MoM_prefilter.mod new file mode 100644 index 0000000000000000000000000000000000000000..7fb29f8abfb19ef53e9e6dbc90343b9843705308 --- /dev/null +++ b/tests/estimation/method_of_moments/RBC_MoM_prefilter.mod @@ -0,0 +1,179 @@ +% Tests SMM and GMM routines with prefilter, explicit initialization, and estimated_params_init(use_calibration); +% +% Copyright (C) 2020 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/>. +% ========================================================================= + +% Define testscenario +@#define orderApp = 2 + +% Note that we will set the numerical optimization tolerance levels very large to speed up the testsuite +@#define optimizer = 13 + + +@#include "RBC_MoM_common.inc" + +shocks; +var u_a; stderr 0.0072; +end; + +varobs n c iv; + +% Simulate data +stoch_simul(order=@{orderApp},pruning,nodisplay,nomoments,periods=250,TeX); +save('RBC_MoM_data_@{orderApp}.mat', options_.varobs{:} ); +pause(1); + +set_param_value('DELTA',NaN); + +estimated_params; + DELTA, 0.025, 0, 1; + BETTA, , 0, 1; + B, , 0, 1; + %ETAl, 1, 0, 10; + ETAc, , 0, 10; + ALFA, , 0, 1; + RHOA, , 0, 1; + stderr u_a, , 0, 1; + %THETA, 3.48, 0, 10; +end; + +estimated_params_init(use_calibration); +end; + +%-------------------------------------------------------------------------- +% Method of Moments Estimation +%-------------------------------------------------------------------------- + +matched_moments; +c; +n; +iv; + +c*c; +c*iv; +c*n; +iv*c; +iv*iv; +iv*n; +n*n; + +c*c(-1); +n*n(-1); +iv*iv(-1); +end; + +% get indices in declaration order +ic = strmatch('c', M_.endo_names,'exact'); +iiv = strmatch('iv', M_.endo_names,'exact'); +in = strmatch('n', M_.endo_names,'exact'); +% first entry: number of variable in declaration order +% second entry: lag +% third entry: power + +matched_moments_ = { + [ic ] [0 ], [1 ]; + [in ] [0 ], [1 ]; + [iiv ] [0 ], [1 ]; + [ic ic ] [0 0], [1 1]; + [ic iiv] [0 0], [1 1]; + [ic in ] [0 0], [1 1]; + [ic iiv] [0 0], [1 1]; + [iiv iiv] [0 0], [1 1]; + [in iiv] [0 0], [1 1]; + [in in ] [0 0], [1 1]; + [ic ic ] [0 -1], [1 1]; + [in in ] [0 -1], [1 1]; + [iiv iiv] [0 -1], [1 1]; +}; + +if ~isequal(M_.matched_moments,matched_moments_) + error('Translation to matched_moments-block failed') +end + +weighting_matrix=diag([1000;ones(8,1)]); +save('test_matrix.mat','weighting_matrix') + +@#for mommethod in ["GMM", "SMM"] + method_of_moments( + % Necessery options + mom_method = @{mommethod} % method of moments method; possible values: GMM|SMM + , datafile = 'RBC_MoM_data_@{orderApp}.mat' % name of filename with data + + % Options for both GMM and SMM + % , bartlett_kernel_lag = 20 % bandwith in optimal weighting matrix + , order = @{orderApp} % order of Taylor approximation in perturbation + % , penalized_estimator % use penalized optimization + , pruning % use pruned state space system at higher-order + % , verbose % display and store intermediate estimation results +% , weighting_matrix = 'test_matrix.mat' % weighting matrix in moments distance objective function; possible values: OPTIMAL|IDENTITY_MATRIX|DIAGONAL|filename + , weighting_matrix =['test_matrix.mat','optimal'] + %, weighting_matrix = optimal % weighting matrix in moments distance objective function; possible values: OPTIMAL|IDENTITY_MATRIX|DIAGONAL|filename + %, additional_optimizer_steps = [4] % vector of additional mode-finders run after mode_compute + , prefilter=1 % demean each data series by its empirical mean and use centered moments + , se_tolx=1e-5 + % + % Options for SMM + % , bounded_shock_support % trim shocks in simulation to +- 2 stdev + , burnin = 500 % number of periods dropped at beginning of simulation + % , seed = 24051986 % seed used in simulations + % , simulation_multiple = 5 % multiple of the data length used for simulation + % + % General options + %, dirname = 'MM' % directory in which to store estimation output + % , graph_format = EPS % specify the file format(s) for graphs saved to disk + % , nodisplay % do not display the graphs, but still save them to disk + % , nograph % do not create graphs (which implies that they are not saved to the disk nor displayed) + % , noprint % do not print stuff to console + % , plot_priors = 1 % control plotting of priors + % , prior_trunc = 1e-10 % probability of extreme values of the prior density that is ignored when computing bounds for the parameters + % , TeX % print TeX tables and graphics + % + % Data and model options + %, first_obs = 501 % number of first observation + % , logdata % if loglinear is set, this option is necessary if the user provides data already in logs, otherwise the log transformation will be applied twice (this may result in complex data) + % , loglinear % computes a log-linear approximation of the model instead of a linear approximation + %, nobs = 500 % number of observations + % , xls_sheet = willi % name of sheet with data in Excel + % , xls_range = B2:D200 % range of data in Excel sheet + % + % Optimization options that can be set by the user in the mod file, otherwise default values are provided + % , analytic_derivation % uses analytic derivatives to compute standard errors for GMM + %, huge_number=1D10 % value for replacing the infinite bounds on parameters by finite numbers. Used by some optimizers for numerical reasons + , mode_compute = @{optimizer} % specifies the optimizer for minimization of moments distance, note that by default there is a new optimizer + %, optim = ('TolFun', 1e-3 + % ,'TolX', 1e-5 + % ) % a list of NAME and VALUE pairs to set options for the optimization routines. Available options depend on mode_compute + %, silent_optimizer % run minimization of moments distance silently without displaying results or saving files in between + % + % % Numerical algorithms options + % , aim_solver % Use AIM algorithm to compute perturbation approximation + % , dr=default % method used to compute the decision rule; possible values are DEFAULT, CYCLE_REDUCTION, LOGARITHMIC_REDUCTION + % , dr_cycle_reduction_tol = 1e-7 % convergence criterion used in the cycle reduction algorithm + % , dr_logarithmic_reduction_maxiter = 100 % maximum number of iterations used in the logarithmic reduction algorithm + % , dr_logarithmic_reduction_tol = 1e-12 % convergence criterion used in the cycle reduction algorithm + % , k_order_solver % use k_order_solver in higher order perturbation approximations + % , lyapunov = DEFAULT % algorithm used to solve lyapunov equations; possible values are DEFAULT, FIXED_POINT, DOUBLING, SQUARE_ROOT_SOLVER + % , lyapunov_complex_threshold = 1e-15 % complex block threshold for the upper triangular matrix in symmetric Lyapunov equation solver + % , lyapunov_fixed_point_tol = 1e-10 % convergence criterion used in the fixed point Lyapunov solver + % , lyapunov_doubling_tol = 1e-16 % convergence criterion used in the doubling algorithm + % , sylvester = default % algorithm to solve Sylvester equation; possible values are DEFAULT, FIXED_POINT + % , sylvester_fixed_point_tol = 1e-12 % convergence criterion used in the fixed point Sylvester solver + % , qz_criterium = 0.999999 % value used to split stable from unstable eigenvalues in reordering the Generalized Schur decomposition used for solving first order problems [IS THIS CORRET @wmutschl] + % , qz_zero_threshold = 1e-6 % value used to test if a generalized eigenvalue is 0/0 in the generalized Schur decomposition + ); +@#endfor \ No newline at end of file diff --git a/tests/estimation/method_of_moments/RBC_MoM_steady_helper.m b/tests/estimation/method_of_moments/RBC_MoM_steady_helper.m new file mode 100644 index 0000000000000000000000000000000000000000..b495e27a1a1d5de281d17514c7be527a79c897eb --- /dev/null +++ b/tests/estimation/method_of_moments/RBC_MoM_steady_helper.m @@ -0,0 +1,22 @@ +function [N, info]= RBC_MoM_steady_helper(THETA,ETAl,ETAc,BETTA,B,C_O_N,W) +info=0; +if ~isreal(C_O_N) + info=1; + N=NaN; + return; +end +if ETAc == 1 && ETAl == 1 + N = (1-BETTA*B)*(C_O_N*(1-B))^-1*W/THETA/(1+(1-BETTA*B)*(C_O_N*(1-B))^-1*W/THETA); +else + % No closed-form solution use a fixed-point algorithm + N0 = 1/3; + try + [N, ~, exitflag] = fsolve(@(N) THETA*(1-N)^(-ETAl)*N^ETAc - (1-BETTA*B)*(C_O_N*(1-B))^(-ETAc)*W, N0,optimset('Display','off','TolX',1e-12,'TolFun',1e-12)); + if exitflag<1 + info=1; + end + catch + N=NaN; + info=1; + end +end \ No newline at end of file diff --git a/tests/estimation/method_of_moments/RBC_MoM_steadystate.m b/tests/estimation/method_of_moments/RBC_MoM_steadystate.m new file mode 100644 index 0000000000000000000000000000000000000000..ba4ef9240b522ac5bbf83fc41380a50f3f1805f8 --- /dev/null +++ b/tests/estimation/method_of_moments/RBC_MoM_steadystate.m @@ -0,0 +1,74 @@ +% By Willi Mutschler, September 26, 2016. Email: willi@mutschler.eu +function [ys,params,check] = RBCmodel_steadystate(ys,exo,M_,options_) +%% Step 0: initialize indicator and set options for numerical solver +check = 0; +options = optimset('Display','off','TolX',1e-12,'TolFun',1e-12); +params = M_.params; + +%% Step 1: read out parameters to access them with their name +for ii = 1:M_.param_nbr + eval([ M_.param_names{ii} ' = M_.params(' int2str(ii) ');']); +end + +%% Step 2: Check parameter restrictions +if ETAc*ETAl<1 % parameter violates restriction (here it is artifical) + check=1; %set failure indicator + return; %return without updating steady states +end + +%% Step 3: Enter model equations here +A = 1; +RK = 1/BETTA - (1-DELTA); +K_O_N = (RK/(A*(1-ALFA)))^(-1/ALFA); +if K_O_N <= 0 + check = 1; % set failure indicator + return; % return without updating steady states +end +W = A*ALFA*(K_O_N)^(1-ALFA); +IV_O_N = DELTA*K_O_N; +Y_O_N = A*K_O_N^(1-ALFA); +C_O_N = Y_O_N - IV_O_N; +if C_O_N <= 0 + check = 1; % set failure indicator + return; % return without updating steady states +end + +% The labor level +if ETAc == 1 && ETAl == 1 + N = (1-BETTA*B)*(C_O_N*(1-B))^-1*W/THETA/(1+(1-BETTA*B)*(C_O_N*(1-B))^-1*W/THETA); +else + % No closed-form solution use a fixed-point algorithm + N0 = 1/3; + [N,~,exitflag] = fsolve(@(N) THETA*(1-N)^(-ETAl)*N^ETAc - (1-BETTA*B)*(C_O_N*(1-B))^(-ETAc)*W, N0,options); + if exitflag <= 0 + check = 1; % set failure indicator + return % return without updating steady states + end +end + +C=C_O_N*N; +Y=Y_O_N*N; +IV=IV_O_N*N; +K=K_O_N*N; +LA = (C-B*C)^(-ETAc)-BETTA*B*(C-B*C)^(-ETAc); + +k=log(K); +c=log(C); +a=log(A); +iv=log(IV); +y=log(Y); +la=log(LA); +n=log(N); +rk=log(RK); +w=log(W); +%% Step 4: Update parameters and variables +params=NaN(M_.param_nbr,1); +for iter = 1:M_.param_nbr %update parameters set in the file + eval([ 'params(' num2str(iter) ') = ' M_.param_names{iter} ';' ]) +end + +for ii = 1:M_.orig_endo_nbr %auxiliary variables are set automatically + eval(['ys(' int2str(ii) ') = ' M_.endo_names{ii} ';']); +end + +end diff --git a/tests/external_function/benchmark.mod b/tests/external_function/benchmark.mod new file mode 100644 index 0000000000000000000000000000000000000000..8076fa13a27e602f78df29bec35b334e0b250364 --- /dev/null +++ b/tests/external_function/benchmark.mod @@ -0,0 +1,48 @@ +/* This file has no external function, but is used as a benchmark against with + the other tests are compared. + It is almost the same as example1.mod, except that lagged capital is squared in the + law of motion of capital (in order to have a non-zero Hessian of the + external function) */ +var y, c, k, a, h, b; +varexo e, u; + +parameters beta, rho, alpha, delta, theta, psi, tau; + +alpha = 0.36; +rho = 0.95; +tau = 0.025; +beta = 0.99; +delta = 0.025; +psi = 0; +theta = 2.95; + +phi = 0.1; + +model; +c*theta*h^(1+psi)=(1-alpha)*y; +k = beta*(((exp(b)*c)/(exp(b(+1))*c(+1))) + *(exp(b(+1))*alpha*y(+1)+(1-delta)*k)); +y = exp(a)*(k(-1)^alpha)*(h^(1-alpha)); +k = exp(b)*(y-c)+(1-delta)*k(-1)^2; +a = rho*a(-1)+tau*b(-1) + e; +b = tau*a(-1)+rho*b(-1) + u; +end; + +initval; +y = 1.08068253095672; +c = 0.80359242014163; +h = 0.29175631001732; +k = 11.08360443260358; +a = 0; +b = 0; +e = 0; +u = 0; +end; + +shocks; +var e; stderr 0.009; +var u; stderr 0.009; +var e, u = phi*0.009*0.009; +end; + +stoch_simul; diff --git a/tests/external_function/extfun_in_mlv.mod b/tests/external_function/extfun_in_mlv.mod new file mode 100644 index 0000000000000000000000000000000000000000..1b2947cff521b1431477e1e7ccb6a83a0c656d5a --- /dev/null +++ b/tests/external_function/extfun_in_mlv.mod @@ -0,0 +1,58 @@ +// Tests the presence of an external function call in a model local variable +var y, c, k, a, h, b; +varexo e, u; + +parameters beta, rho, alpha, delta, theta, psi, tau; + +alpha = 0.36; +rho = 0.95; +tau = 0.025; +beta = 0.99; +delta = 0.025; +psi = 0; +theta = 2.95; + +phi = 0.1; + +external_function(nargs=2, name=extFunNoDerivs, first_deriv_provided=extFunDeriv); + +model; +# foo = extFunNoDerivs((1-delta),k(-1)); +c*theta*h^(1+psi)=(1-alpha)*y; +k = beta*(((exp(b)*c)/(exp(b(+1))*c(+1))) + *(exp(b(+1))*alpha*y(+1)+(1-delta)*k)); +y = exp(a)*(k(-1)^alpha)*(h^(1-alpha)); +k = exp(b)*(y-c)+foo; +a = rho*a(-1)+tau*b(-1) + e; +b = tau*a(-1)+rho*b(-1) + u; +end; + +initval; +y = 1.08068253095672; +c = 0.80359242014163; +h = 0.29175631001732; +k = 11.08360443260358; +a = 0; +b = 0; +e = 0; +u = 0; +end; + +shocks; +var e; stderr 0.009; +var u; stderr 0.009; +var e, u = phi*0.009*0.009; +end; + +stoch_simul; + +L = load('benchmark_results.mat'); +if max(max(abs(L.oo_.dr.ghu - oo_.dr.ghu))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghx - oo_.dr.ghx))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.var - oo_.var))) > 1e-12 + error('Failure in external function') +end diff --git a/tests/external_function/first_and_2nd_deriv_given.mod b/tests/external_function/first_and_2nd_deriv_given.mod index 8726ed43c679b28341150fe223d60bb6cc463c51..f62110442842ae49ef4e062237dc4abaa05433f6 100644 --- a/tests/external_function/first_and_2nd_deriv_given.mod +++ b/tests/external_function/first_and_2nd_deriv_given.mod @@ -44,3 +44,26 @@ var e, u = phi*0.009*0.009; end; stoch_simul; + +L = load('benchmark_results.mat'); +if max(max(abs(L.oo_.dr.ghu - oo_.dr.ghu))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghx - oo_.dr.ghx))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghxu - oo_.dr.ghxu))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghxx - oo_.dr.ghxx))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghuu - oo_.dr.ghuu))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghs2 - oo_.dr.ghs2))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.var - oo_.var))) > 1e-12 + error('Failure in external function') +end diff --git a/tests/external_function/first_and_2nd_deriv_given_dll.mod b/tests/external_function/first_and_2nd_deriv_given_dll.mod index eb18b676cd1eadf20471ebf730ff11058767edc8..ac42623c6fc54980704f75ed7c1d974cedee9991 100644 --- a/tests/external_function/first_and_2nd_deriv_given_dll.mod +++ b/tests/external_function/first_and_2nd_deriv_given_dll.mod @@ -44,3 +44,26 @@ var e, u = phi*0.009*0.009; end; stoch_simul; + +L = load('benchmark_results.mat'); +if max(max(abs(L.oo_.dr.ghu - oo_.dr.ghu))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghx - oo_.dr.ghx))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghxu - oo_.dr.ghxu))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghxx - oo_.dr.ghxx))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghuu - oo_.dr.ghuu))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghs2 - oo_.dr.ghs2))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.var - oo_.var))) > 1e-12 + error('Failure in external function') +end diff --git a/tests/external_function/first_and_2nd_deriv_given_dll_namespace.mod b/tests/external_function/first_and_2nd_deriv_given_dll_namespace.mod index a868c4193a5f480db2afadfbae3f50a690fd4754..ca55aea5a94e37f97478a662e4d18c025ccdec15 100644 --- a/tests/external_function/first_and_2nd_deriv_given_dll_namespace.mod +++ b/tests/external_function/first_and_2nd_deriv_given_dll_namespace.mod @@ -46,3 +46,26 @@ var e, u = phi*0.009*0.009; end; stoch_simul; + +L = load('benchmark_results.mat'); +if max(max(abs(L.oo_.dr.ghu - oo_.dr.ghu))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghx - oo_.dr.ghx))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghxu - oo_.dr.ghxu))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghxx - oo_.dr.ghxx))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghuu - oo_.dr.ghuu))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghs2 - oo_.dr.ghs2))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.var - oo_.var))) > 1e-12 + error('Failure in external function') +end diff --git a/tests/external_function/first_and_2nd_deriv_given_namespace.mod b/tests/external_function/first_and_2nd_deriv_given_namespace.mod index 1cefa83999d6acae33564391e0314b28b7a6372e..3729a17d66bcc037dfefa3505db676e30c8c4ac8 100644 --- a/tests/external_function/first_and_2nd_deriv_given_namespace.mod +++ b/tests/external_function/first_and_2nd_deriv_given_namespace.mod @@ -46,3 +46,26 @@ var e, u = phi*0.009*0.009; end; stoch_simul; + +L = load('benchmark_results.mat'); +if max(max(abs(L.oo_.dr.ghu - oo_.dr.ghu))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghx - oo_.dr.ghx))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghxu - oo_.dr.ghxu))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghxx - oo_.dr.ghxx))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghuu - oo_.dr.ghuu))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghs2 - oo_.dr.ghs2))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.var - oo_.var))) > 1e-12 + error('Failure in external function') +end diff --git a/tests/external_function/first_deriv_given.mod b/tests/external_function/first_deriv_given.mod index ee2f3d99f624174d3a1cb413c7d22bf6177c191c..ef7ba0ab4fab68ddc4e96cea083c68ed410ffa57 100644 --- a/tests/external_function/first_deriv_given.mod +++ b/tests/external_function/first_deriv_given.mod @@ -44,3 +44,26 @@ var e, u = phi*0.009*0.009; end; stoch_simul; + +L = load('benchmark_results.mat'); +if max(max(abs(L.oo_.dr.ghu - oo_.dr.ghu))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghx - oo_.dr.ghx))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghxu - oo_.dr.ghxu))) > 1e-4 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghxx - oo_.dr.ghxx))) > 1e-3 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghuu - oo_.dr.ghuu))) > 1e-5 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghs2 - oo_.dr.ghs2))) > 1e-9 + error('Failure in external function') +end +if max(max(abs(L.oo_.var - oo_.var))) > 1e-12 + error('Failure in external function') +end diff --git a/tests/external_function/first_deriv_given_dll.mod b/tests/external_function/first_deriv_given_dll.mod index 054a0cfb8ca85e25f95760549be8eb4bbdf2facb..cfaf4ae519635b4a9b39a88ffc63189b34c57050 100644 --- a/tests/external_function/first_deriv_given_dll.mod +++ b/tests/external_function/first_deriv_given_dll.mod @@ -44,3 +44,26 @@ var e, u = phi*0.009*0.009; end; stoch_simul; + +L = load('benchmark_results.mat'); +if max(max(abs(L.oo_.dr.ghu - oo_.dr.ghu))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghx - oo_.dr.ghx))) > 1e-12 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghxu - oo_.dr.ghxu))) > 1e-4 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghxx - oo_.dr.ghxx))) > 1e-3 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghuu - oo_.dr.ghuu))) > 1e-5 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghs2 - oo_.dr.ghs2))) > 1e-9 + error('Failure in external function') +end +if max(max(abs(L.oo_.var - oo_.var))) > 1e-12 + error('Failure in external function') +end diff --git a/tests/external_function/no_deriv_given.mod b/tests/external_function/no_deriv_given.mod index a35d08b8cd6723ffdba10abb315c65b771b8cc7b..c75cd10c153cb3a7328f83a8bd54ee4f85d03b56 100644 --- a/tests/external_function/no_deriv_given.mod +++ b/tests/external_function/no_deriv_given.mod @@ -44,3 +44,26 @@ var e, u = phi*0.009*0.009; end; stoch_simul; + +L = load('benchmark_results.mat'); +if max(max(abs(L.oo_.dr.ghu - oo_.dr.ghu))) > 1e-9 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghx - oo_.dr.ghx))) > 1e-9 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghxu - oo_.dr.ghxu))) > 1e-4 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghxx - oo_.dr.ghxx))) > 1e-3 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghuu - oo_.dr.ghuu))) > 1e-5 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghs2 - oo_.dr.ghs2))) > 1e-9 + error('Failure in external function') +end +if max(max(abs(L.oo_.var - oo_.var))) > 1e-12 + error('Failure in external function') +end diff --git a/tests/external_function/no_deriv_given_dll.mod b/tests/external_function/no_deriv_given_dll.mod index 6a678357928f1f296b9e15e3366ef2e657573d76..20741684721e44f63acff4f118462033fcb77631 100644 --- a/tests/external_function/no_deriv_given_dll.mod +++ b/tests/external_function/no_deriv_given_dll.mod @@ -44,3 +44,26 @@ var e, u = phi*0.009*0.009; end; stoch_simul; + +L = load('benchmark_results.mat'); +if max(max(abs(L.oo_.dr.ghu - oo_.dr.ghu))) > 1e-9 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghx - oo_.dr.ghx))) > 1e-9 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghxu - oo_.dr.ghxu))) > 1e-4 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghxx - oo_.dr.ghxx))) > 1e-3 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghuu - oo_.dr.ghuu))) > 1e-5 + error('Failure in external function') +end +if max(max(abs(L.oo_.dr.ghs2 - oo_.dr.ghs2))) > 1e-9 + error('Failure in external function') +end +if max(max(abs(L.oo_.var - oo_.var))) > 1e-12 + error('Failure in external function') +end diff --git a/tests/histval_initval_file/my_assert.m b/tests/histval_initval_file/my_assert.m new file mode 100644 index 0000000000000000000000000000000000000000..2ac132e12d72b0c349f2a35cfd8666bf6f3a11a9 --- /dev/null +++ b/tests/histval_initval_file/my_assert.m @@ -0,0 +1,4 @@ +function failed_tests = my_assert(failed_tests, success, test_name) +if ~success + failed_tests = cat(1, test_failed, test_name); +end \ No newline at end of file diff --git a/tests/histval_initval_file/ramst_data.mod b/tests/histval_initval_file/ramst_data.mod new file mode 100644 index 0000000000000000000000000000000000000000..c65b9fc816cb27a8444cec66fadbdb8f128d6568 --- /dev/null +++ b/tests/histval_initval_file/ramst_data.mod @@ -0,0 +1,68 @@ +/* Verify that the “datafile†option of “perfect_foresight_setup†behaves as + “initval_file†(see #1663) */ + +var c k; +varexo x; + +parameters alph gam delt bet aa; +alph=0.5; +gam=0.5; +delt=0.02; +bet=0.05; +aa=0.5; + + +model; +c + k - aa*x*k(-1)^alph - (1-delt)*k(-1); +c^(-gam) - (1+bet)^(-1)*(aa*alph*x(+1)*k^(alph-1) + 1 - delt)*c(+1)^(-gam); +end; + +initval; +x = 1; +k = ((delt+bet)/(1.0*aa*alph))^(1/(alph-1)); +c = aa*k^alph-delt*k; +end; + +steady; + +shocks; + var x; + periods 2; + values 1.1; +end; + +perfect_foresight_setup(periods=200); +perfect_foresight_solver; + +fh = fopen('ramst_data.m', 'w'); +fprintf(fh, 'INIT__ = ''1Y'';\n'); +fprintf(fh, 'NAMES__ = {''c'', ''k'', ''x''};\n'); +fprintf(fh, 'TEX__ = {''c'', ''k'', ''x''};\n'); +fprintf(fh, 'c = ['); +fprintf(fh, '%f ', oo_.endo_simul(1,:)); +fprintf(fh, '];\n'); +fprintf(fh, 'k = ['); +fprintf(fh, '%f ', oo_.endo_simul(2,:)); +fprintf(fh, '];\n'); +fprintf(fh, 'x = ['); +fprintf(fh, '%f ', oo_.exo_simul); +fprintf(fh, '];\n'); +fclose(fh); + +INIT__ = '1Y'; +NAMES__ = {'c', 'k', 'x'}; +TEX__ = {'c', 'k', 'x'}; +eval('c = oo_.endo_simul(1,:);'); +eval('k = oo_.endo_simul(2,:);'); +eval('x = oo_.exo_simul'';'); +save('ramst_data.mat', 'INIT__', 'NAMES__', ... + 'TEX__', 'c', 'k', 'x'); + +fh = fopen('ramst_data.csv', 'w'); +fprintf(fh, 'c,k,x\n'); +for i = 1:size(oo_.endo_simul, 2); + fprintf(fh, '%f, ', oo_.endo_simul(:, i)); + fprintf(fh, '%f\n', oo_.exo_simul(i)); +end; +fclose(fh); + diff --git a/tests/histval_initval_file/ramst_data.xls b/tests/histval_initval_file/ramst_data.xls new file mode 100644 index 0000000000000000000000000000000000000000..050e2166c0db666245d60a99028e4452c68a394d Binary files /dev/null and b/tests/histval_initval_file/ramst_data.xls differ diff --git a/tests/histval_initval_file/ramst_data.xlsx b/tests/histval_initval_file/ramst_data.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..849953eacbb225829a8e34b0440042e1e9f7d238 Binary files /dev/null and b/tests/histval_initval_file/ramst_data.xlsx differ diff --git a/tests/initval_file/ramst_datafile.mod b/tests/histval_initval_file/ramst_datafile.mod similarity index 100% rename from tests/initval_file/ramst_datafile.mod rename to tests/histval_initval_file/ramst_datafile.mod diff --git a/tests/histval_initval_file/ramst_histval_file.mod b/tests/histval_initval_file/ramst_histval_file.mod new file mode 100644 index 0000000000000000000000000000000000000000..8971ba04e0d46fe794aca9ec61c07b4a7b51e4b8 --- /dev/null +++ b/tests/histval_initval_file/ramst_histval_file.mod @@ -0,0 +1,96 @@ +/* Test for the histval_file() command. This file needs ramst_histval_data.m. */ + +var c k; +varexo x; + +parameters alph gam delt bet aa; +alph=0.5; +gam=0.5; +delt=0.02; +bet=0.05; +aa=0.5; + + +model; +c + k - aa*x*k(-1)^alph - (1-delt)*k(-1); +c^(-gam) - (1+bet)^(-1)*(aa*alph*x(+1)*k^(alph-1) + 1 - delt)*c(+1)^(-gam); +end; + +initval; +x = 1; +k = ((delt+bet)/(1.0*aa*alph))^(1/(alph-1)); +c = aa*k^alph-delt*k; +end; + +steady; + +histval_file(datafile = 'ramst_histval_data.m'); +perfect_foresight_setup(periods = 200); +perfect_foresight_solver; +if ~oo_.deterministic_simulation.status + error('Perfect foresight simulation failed') +end +oo_.exo_simul = []; +oo_.endo_simul = []; + +initval_file(filename = ramst_initval_file_data_row_vec_mat); +perfect_foresight_setup(periods=200); +if oo_.exo_simul(2) ~= 1.2 + error('initval_file problem with exogenous variable'); +end +if oo_.endo_simul(2, 2) ~= 13 + error('initval_file option problem with endogenous variable'); +end +perfect_foresight_solver; +if ~oo_.deterministic_simulation.status + error('Perfect foresight simulation failed') +end + +initval_file(datafile = 'ramst_data.m'); +perfect_foresight_setup(periods = 200); +perfect_foresight_solver; +if ~oo_.deterministic_simulation.status + error('Perfect foresight simulation failed') +end +oo_.exo_simul = []; +oo_.endo_simul = []; + +initval_file(datafile = 'ramst_data.mat'); +perfect_foresight_setup(periods = 200); +perfect_foresight_solver; +if ~oo_.deterministic_simulation.status + error('Perfect foresight simulation failed') +end +oo_.exo_simul = []; +oo_.endo_simul = []; + +initval_file(datafile = 'ramst_data.csv'); +perfect_foresight_setup(periods = 200); +perfect_foresight_solver; +if ~oo_.deterministic_simulation.status + error('Perfect foresight simulation failed') +end +oo_.exo_simul = []; +oo_.endo_simul = []; + +initval_file(datafile = 'ramst_data.xlsx'); +perfect_foresight_setup(periods = 200); +perfect_foresight_solver; +if ~oo_.deterministic_simulation.status + error('Perfect foresight simulation failed') +end +oo_.exo_simul = []; +oo_.endo_simul = []; + +if ispc; + initval_file(datafile = 'ramst_data.cls'); + perfect_foresight_setup(periods = 200); + perfect_foresight_solver; + oo_.exo_simul = []; + oo_.endo_simul = []; + + if ~oo_.deterministic_simulation.status + error('Perfect foresight simulation failed') + end; +end; + diff --git a/tests/histval_initval_file/ramst_initval_file.mod b/tests/histval_initval_file/ramst_initval_file.mod new file mode 100644 index 0000000000000000000000000000000000000000..8e72fe7504468eb7ca84ade8584a801c74891de6 --- /dev/null +++ b/tests/histval_initval_file/ramst_initval_file.mod @@ -0,0 +1,113 @@ +/* Test for the initval_file() command. This file needs ramst_initval_file_data.m. It should give results similar to those of ramst.mod */ + +var c k; +varexo x; + +parameters alph gam delt bet aa; +alph=0.5; +gam=0.5; +delt=0.02; +bet=0.05; +aa=0.5; + + +model; +c + k - aa*x*k(-1)^alph - (1-delt)*k(-1); +c^(-gam) - (1+bet)^(-1)*(aa*alph*x(+1)*k^(alph-1) + 1 - delt)*c(+1)^(-gam); +end; + +initval; +x = 1; +k = ((delt+bet)/(1.0*aa*alph))^(1/(alph-1)); +c = aa*k^alph-delt*k; +end; + +steady; + +initval_file(filename = ramst_initval_file_data_row_vec_mat); +perfect_foresight_setup(periods=200); +if oo_.exo_simul(2) ~= 1.2 + error('initval_file problem with exogenous variable'); +end +if oo_.endo_simul(2, 2) ~= 13 + error('initval_file option problem with endogenous variable'); +end +perfect_foresight_solver; +if ~oo_.deterministic_simulation.status + error('Perfect foresight simulation failed') +end + +oo_.exo_simul = []; +oo_.endo_simul = []; + +initval_file(filename = ramst_initval_file_data_col_vec_mat); + +perfect_foresight_setup(periods=200); +if oo_.exo_simul(2) ~= 1.2 + error('initval_file problem with exogenous variable'); +end +if oo_.endo_simul(2, 2) ~= 13 + error('initval_file problem with endogenous variable'); +end +perfect_foresight_solver; +if ~oo_.deterministic_simulation.status + error('Perfect foresight simulation failed') +end + +if ispc() + initval_file(filename = ramst_initval_file_excel); + perfect_foresight_setup(periods=200); + perfect_foresight_solver; + if ~oo_.deterministic_simulation.status + error('Perfect foresight simulation failed'); + end; +end + +initval_file(datafile = 'ramst_data.m'); +perfect_foresight_setup(periods = 200); +perfect_foresight_solver; +if ~oo_.deterministic_simulation.status + error('Perfect foresight simulation failed') +end +oo_.exo_simul = []; +oo_.endo_simul = []; + +initval_file(datafile = 'ramst_data.mat'); +perfect_foresight_setup(periods = 200); +perfect_foresight_solver; +if ~oo_.deterministic_simulation.status + error('Perfect foresight simulation failed') +end +oo_.exo_simul = []; +oo_.endo_simul = []; + +initval_file(datafile = 'ramst_data.csv'); +perfect_foresight_setup(periods = 200); +perfect_foresight_solver; +if ~oo_.deterministic_simulation.status + error('Perfect foresight simulation failed') +end +oo_.exo_simul = []; +oo_.endo_simul = []; + +initval_file(datafile = 'ramst_data.xlsx'); +perfect_foresight_setup(periods = 200); +perfect_foresight_solver; +if ~oo_.deterministic_simulation.status + error('Perfect foresight simulation failed') +end +oo_.exo_simul = []; +oo_.endo_simul = []; + +if ispc; + initval_file(datafile = 'ramst_data.cls'); + perfect_foresight_setup(periods = 200); + perfect_foresight_solver; + oo_.exo_simul = []; + oo_.endo_simul = []; + + if ~oo_.deterministic_simulation.status + error('Perfect foresight simulation failed') + end; +end; + diff --git a/tests/histval_initval_file/ramst_initval_file_data.m b/tests/histval_initval_file/ramst_initval_file_data.m new file mode 100644 index 0000000000000000000000000000000000000000..cc1914ca12367299d7db18040458f34389e1bb54 --- /dev/null +++ b/tests/histval_initval_file/ramst_initval_file_data.m @@ -0,0 +1,14 @@ +x = vertcat([ 1; 1.2 ], repmat(1, 200, 1)); +k = repmat(13, 202, 1); +c = repmat(1.5, 202, 1); +save('ramst_initval_file_data_col_vec_mat.mat','c','k','x'); + +if ispc() + xlswrite('ramst_initval_file_excel',[x k c],1,'A2'); + xlswrite('ramst_initval_file_excel',{'x' 'k' 'c'},1,'A1'); +end + +c=c'; +k=k'; +x=x'; +save('ramst_initval_file_data_row_vec_mat.mat','c','k','x'); diff --git a/tests/histval_initval_file/sim_exo_lead_lag.mod b/tests/histval_initval_file/sim_exo_lead_lag.mod new file mode 100644 index 0000000000000000000000000000000000000000..5a06c1c5cb9b40f8c5419a0e99ccafeeac345890 --- /dev/null +++ b/tests/histval_initval_file/sim_exo_lead_lag.mod @@ -0,0 +1,43 @@ +// Uses autonomous system from sim_base.mod, but adds separate system where exogenous variables have several leads and lags +// Lags and leads on exogenous variables are substituted out by auxiliary variables + +var c cmav k z_backward z_forward; +varexo x; + +parameters alph gam delt bet aa; +alph=0.5; +gam=0.5; +delt=0.02; +bet=0.05; +aa=0.5; + +model; + c + k - aa*x*k(-1)^alph - (1-delt)*k(-1); + c^(-gam) - (1+bet)^(-1)*(aa*alph*x(+1)*k^(alph-1) + 1 - delt)*c(+1)^(-gam); + z_backward=0.1*1+0.9*z_backward(-1) + (x(-4) - 1); + z_forward=0.2*1+0.8*z_forward(+1) + (x(+4) - 1); + cmav = 0.2*(c(-2) + c(-1) + c + c(+1) + c(+2)); +end; + +initval; + c = 1.2; + cmav = 1.2; + k = 12; + x = 1; //set x(0), x(-1), x(-2), x(-3) + z_backward = 1; + z_forward = 1; +end; + +shocks; +var x; //sets x(+2) +periods 2; +values 0.9; +end; + +simul(periods=200); + +if ~oo_.deterministic_simulation.status + error('Perfect foresight simulation failed') +end + + diff --git a/tests/histval_initval_file/sim_exo_lead_lag_histvalf.mod b/tests/histval_initval_file/sim_exo_lead_lag_histvalf.mod new file mode 100644 index 0000000000000000000000000000000000000000..3d9ae12bbc5dcc63b1efdc97a77f66af69404053 --- /dev/null +++ b/tests/histval_initval_file/sim_exo_lead_lag_histvalf.mod @@ -0,0 +1,76 @@ +// Uses autonomous system from sim_base.mod, but adds separate system where exogenous variables have several leads and lags +// Lags and leads on exogenous variables are substituted out by auxiliary variables + +var c cmav k z_backward z_forward; +varexo x; + +parameters alph gam delt bet aa; +alph=0.5; +gam=0.5; +delt=0.02; +bet=0.05; +aa=0.5; + +model; + c + k - aa*x*k(-1)^alph - (1-delt)*k(-1); + c^(-gam) - (1+bet)^(-1)*(aa*alph*x(+1)*k^(alph-1) + 1 - delt)*c(+1)^(-gam); + z_backward=0.1*1+0.9*z_backward(-1) + (x(-4) - 1); + z_forward=0.2*1+0.8*z_forward(+1) + (x(+4) - 1); + cmav = 0.2*(c(-2) + c(-1) + c + c(+1) + c(+2)); +end; + +initval; + x = 1; +end; + +steady_state_model; + k = ((bet + delt)/(aa*alph*x))^(1/(alph - 1)); + c = aa*x*k^alph - delt*k; + z_backward = x; + z_forward = x; + cmav = c; +end; + +steady; + +shocks; + var x; + periods 2; + values 0.9; +end; + +perfect_foresight_setup(periods=200); +perfect_foresight_solver; + +reference = oo_.endo_simul; + +data1 = repmat([oo_.steady_state' 1], 4, 1); +ds = dseries(data1, '1Y', [M_.endo_names; M_.exo_names]); + +histval_file(series = ds); +perfect_foresight_setup(periods=200); +perfect_foresight_solver; + +if ~oo_.deterministic_simulation.status + error('Perfect foresight simulation failed'); +end + +if max(max(abs(reference(1:5,5:end) - oo_.endo_simul(1:5,5:end)))) > 1e-8 + error('Simulation with leads and lags doesn''t match the one with auxiliary variables') +end + +data1 = repmat([oo_.steady_state' 1], 6, 1); +ds1 = dseries(data1, '1Y', [M_.endo_names; M_.exo_names]); + +histval_file(series = ds1, first_obs = 3, last_obs = 6, nobs = 4); + +perfect_foresight_setup(periods=200); +perfect_foresight_solver; + +if ~oo_.deterministic_simulation.status + error('Perfect foresight simulation failed'); +end + +if max(max(abs(reference(1:5,:) - oo_.endo_simul(1:5,:)))) > 1e-8 + error('Simulation with leads and lags doesn''t match the one with auxiliary variables') +end diff --git a/tests/histval_initval_file/sim_exo_lead_lag_initvalf.mod b/tests/histval_initval_file/sim_exo_lead_lag_initvalf.mod new file mode 100644 index 0000000000000000000000000000000000000000..e74b6c9b27a3721bd0a0ab10330f292b9be7ddaa --- /dev/null +++ b/tests/histval_initval_file/sim_exo_lead_lag_initvalf.mod @@ -0,0 +1,56 @@ +// Uses autonomous system from sim_base.mod, but adds separate system where exogenous variables have several leads and lags +// Lags and leads on exogenous variables are substituted out by auxiliary variables + +data1 = repmat([1.2, 1.2, 12, 1, 1, 1], 208, 1); +data1(6, 6) = 0.9; //shock to x in period 2 +ds = dseries(data1, '1Y', {'c', 'cmav', 'k', 'z_backward', 'z_forward', 'x'}); + +var c cmav k z_backward z_forward; +varexo x; + +parameters alph gam delt bet aa; +alph=0.5; +gam=0.5; +delt=0.02; +bet=0.05; +aa=0.5; + +model; + c + k - aa*x*k(-1)^alph - (1-delt)*k(-1); + c^(-gam) - (1+bet)^(-1)*(aa*alph*x(+1)*k^(alph-1) + 1 - delt)*c(+1)^(-gam); + z_backward=0.1*1+0.9*z_backward(-1) + (x(-4) - 1); + z_forward=0.2*1+0.8*z_forward(+1) + (x(+4) - 1); + cmav = 0.2*(c(-2) + c(-1) + c + c(+1) + c(+2)); +end; + +initval_file(series = ds); + +perfect_foresight_setup(periods=200); +perfect_foresight_solver(maxit=100); + +if ~oo_.deterministic_simulation.status + error('Perfect foresight simulation failed'); +end + +base_results=load('sim_exo_lead_lag_results.mat'); +if max(max(abs(base_results.oo_.endo_simul(1:5,:) - oo_.endo_simul(1:5,:)))) > 1e-8 + error('Simulation with leads and lags doesn''t match the one with auxiliary variables') +end + +data1 = repmat([1.2, 1.2, 12, 1, 1, 1], 212, 1); +data1(8, 6) = 0.9; //shock to x in period 2 +ds1 = dseries(data1, '1Y', {'c', 'cmav', 'k', 'z_backward', 'z_forward', 'x'}); + +initval_file(series = ds1, first_obs = 3, last_obs = 210, nobs = 208); + +perfect_foresight_setup(periods=200); +perfect_foresight_solver(maxit=100); + +if ~oo_.deterministic_simulation.status + error('Perfect foresight simulation failed'); +end + +base_results=load('sim_exo_lead_lag_results.mat'); +if max(max(abs(base_results.oo_.endo_simul(1:5,:) - oo_.endo_simul(1:5,:)))) > 1e-8 + error('Simulation with leads and lags doesn''t match the one with auxiliary variables') +end diff --git a/tests/histval_initval_file/sim_histvalf_stoch_simul.mod b/tests/histval_initval_file/sim_histvalf_stoch_simul.mod new file mode 100644 index 0000000000000000000000000000000000000000..924a5f0029a907b94f6522f2c925efc4ba5a9150 --- /dev/null +++ b/tests/histval_initval_file/sim_histvalf_stoch_simul.mod @@ -0,0 +1,86 @@ +// Uses autonomous system from sim_base.mod, but adds separate system where exogenous variables have several leads and lags +// Lags and leads on exogenous variables are substituted out by auxiliary variables + +var c cmav k z_backward z_forward; +varexo x; + +parameters alph gam delt bet aa; +alph=0.5; +gam=0.5; +delt=0.02; +bet=0.05; +aa=0.5; + +model; + c + k - aa*x*k(-1)^alph - (1-delt)*k(-1); + c^(-gam) - (1+bet)^(-1)*(aa*alph*x(+1)*k^(alph-1) + 1 - delt)*c(+1)^(-gam); + z_backward=0.1*1+0.9*z_backward(-1) + (x(-4) - 1); + z_forward=0.2*1+0.8*z_forward(+1) + (x(+4) - 1); + cmav = 0.2*(c(-2) + c(-1) + c + c(+1) + c(+2)); +end; + +initval; + x = 1; +end; + +steady_state_model; + k = ((bet + delt)/(aa*alph*x))^(1/(alph - 1)); + c = aa*x*k^alph - delt*k; + z_backward = x; + z_forward = x; + cmav = c; +end; + +steady; + +shocks; + var x; + stderr 0.01; +end; + +s = rng; +stoch_simul(periods=20, drop=0, irf=0); + +reference = oo_.endo_simul; + +data1 = repmat([oo_.steady_state' 1], 4, 1); +ds = dseries(data1, '1Y', [M_.endo_names; M_.exo_names]); + +histval_file(series = ds); + +rng(s); +stoch_simul(periods=20, drop=0, irf=0); + +if max(max(abs(reference(1:5,5:end) - oo_.endo_simul(1:5,5:end)))) > 1e-8 + error('Simulation with leads and lags doesn''t match the reference') +end + +data1 = repmat([oo_.steady_state' 1], 6, 1); +ds1 = dseries(data1, '1Y', [M_.endo_names; M_.exo_names]); + +histval_file(series = ds1, first_obs = 6, last_obs = 6, nobs = 1); + +rng(s); +stoch_simul(periods=20, drop=0, irf=0); + +if max(max(abs(reference(1:5,:) - oo_.endo_simul(1:5,:)))) > 1e-8 + error('Simulation with leads and lags doesn''t match the reference') +end + +histval_file(series = ds1, first_simulation_period = 7); + +rng(s); +stoch_simul(periods=20, drop=0, irf=0); + +if max(max(abs(reference(1:5,:) - oo_.endo_simul(1:5,:)))) > 1e-8 + error('Simulation with leads and lags doesn''t match the reference') +end + +histval_file(series = ds1, first_simulation_period = 7Y); + +rng(s); +stoch_simul(periods=20, drop=0, irf=0); + +if max(max(abs(reference(1:5,:) - oo_.endo_simul(1:5,:)))) > 1e-8 + error('Simulation with leads and lags doesn''t match the reference') +end diff --git a/tests/histval_initval_file_unit_tests.m b/tests/histval_initval_file_unit_tests.m new file mode 100644 index 0000000000000000000000000000000000000000..4b81f71de5588fd15d26a2a42b4cf78f6d1aaa52 --- /dev/null +++ b/tests/histval_initval_file_unit_tests.m @@ -0,0 +1,220 @@ +top_test_dir = getenv('TOP_TEST_DIR'); +addpath([top_test_dir filesep '..' filesep 'matlab/']); +dynare_config; + +cd('histval_initval_file'); +num_tests = 0; +failed_tests = {}; + +ds = dseries(randn(10,4)); + +M = struct(); +M.fname = ''; +M.endo_nbr = 3; +M.orig_endo_nbr = 3; +M.endo_names = {'Variable_1','Variable_2','Variable_3'}; +M.exo_nbr = 1; +M.exo_names = {'Variable_4'}; +M.exo_det_nbr = 0; + +caller = 'INITVAL'; + +options = struct(); +options.series = ds; +ds1 = histvalf_initvalf(caller, M, options); + +failed_tests = my_assert(failed_tests, all(all(ds1 == ds)), 'basic test'); +num_tests = num_tests + 1; + +options = struct(); +options.series = ds; +options.first_obs = 2; +ds1 = histvalf_initvalf(caller, M, options); +failed_tests = my_assert(failed_tests, ds1.init == dates('2Y'), ... + 'init test 1'); +num_tests = num_tests + 1; + +options = struct(); +options.series = ds; +options.first_obs = 2; +options.last_obs = 9; +ds1 = histvalf_initvalf(caller, M, options); +failed_tests = my_assert(failed_tests, ds1.init == dates('2Y'), ... + 'first_obs last_obs test 1'); +failed_tests = my_assert(failed_tests, ds1.last == dates('9Y'), ... + 'first_obs last_obs test 2'); +num_tests = num_tests + 2; + +options = struct(); +options.series = ds; +options.last_obs = 9; +ds1 = histvalf_initvalf(caller, M, options); +failed_tests = my_assert(failed_tests, ds1.init == dates('1Y'), ... + 'last_obs test 1'); +failed_tests = my_assert(failed_tests, ds1.last == dates('9Y'), ... + 'last_obs test 2'); +num_tests = num_tests + 2; + +options = struct(); +options.series = ds; +options.first_obs = 2; +options.last_obs = 9; +options.nobs = 8; +ds1 = histvalf_initvalf(caller, M, options); +failed_tests = my_assert(failed_tests, ds1.init == dates('2Y'), ... + 'first_obs, last_obs, nobs test 1'); +failed_tests = my_assert(failed_tests, ds1.last == dates('9Y'), ... + 'first_obs, last_obs, nobs test 2'); +num_tests = num_tests + 2; + +options = struct(); +options.series = ds; +options.last_obs = 9; +options.nobs = 8; +ds1 = histvalf_initvalf(caller, M, options); +failed_tests = my_assert(failed_tests, ds1.init == dates('2Y'), ... + 'last_obs, nobs test 1'); +failed_tests = my_assert(failed_tests, ds1.last == dates('9Y'), ... + 'last_obs, nobs test 2'); +num_tests = num_tests + 2; + +options = struct(); +options.series = ds; +options.first_obs = 2; +options.last_obs = 9; +options.nobs = 7; + +try + ds1 = histvalf_initvalf(caller, M, options); + error('This test didn''t catch the error') +catch me + if strcmp(me.message, ['INITVAL_FILE: FIST_OBS, LAST_OBS and NOBS contain', ... + ' inconsistent information. Use only two of these', ... + ' options.']) == false + failed_tests = cat(1, failed_tests, 'Wrong nobs error message' ); + end +end +num_tests = num_tests + 1; + +options = struct(); +options.series = ds; +options.first_obs = -1; + +try + ds1 = histvalf_initvalf(caller, M, options); + error('This test didn''t catch the error') +catch me + if strcmp(me.message, [caller, '_FILE: the first requested period is', ... + ' before available data.']) == false + failed_tests = cat(1, failed_tests, ... + 'Wrong first period error message'); + end +end +num_tests = num_tests + 1; + +options = struct(); +options.series = ds; +options.last_obs = 11; + +try + ds1 = histvalf_initvalf(caller, M, options); + error('This test didn''t catch the error') +catch me + if strcmp(me.message, [caller, '_FILE: the last requested period is', ... + ' after available data.']) == false + failed_tests = cat(1, failed_tests, ... + 'Wrong last period error message'); + end +end +num_tests = num_tests + 1; + +fh = fopen('data.m', 'w'); +init__ = 'INIT__ = ''1Y'';'; +fprintf(fh, [init__ '\n']); +eval(init__); +names__ = 'NAMES__ = {''x'', ''y''};'; +fprintf(fh, [names__ '\n']); +eval(names__); +tex__ = 'TEX__ = {''x'', ''y''};'; +fprintf(fh, [tex__ '\n']); +eval(tex__); +x = randn(10, 1); +fprintf(fh, 'x = ['); +fprintf(fh, '%f ', x); +fprintf(fh, '];\n'); +y = randn(10, 1); +fprintf(fh, 'y = ['); +fprintf(fh, '%f ', y); +fprintf(fh, '];\n'); +fclose(fh); + +M.endo_nbr = 1; +M.orig_endo_nbr = 1; +M.endo_names = {'y'}; +M.exo_nbr = 1; +M.exo_names = {'x'}; +M.exo_det_nbr = 0; + +options = struct(); +options.datafile = 'data.m'; +series = histvalf_initvalf('INITVAL_FILE', M, options); +failed_tests = my_assert(failed_tests, series.init == dates('1Y'), ... + '*.m file first_obs test'); +failed_tests = my_assert(failed_tests, series.nobs == 10, ... + '*.m file nobs test'); + +save('data.mat', 'INIT__', 'NAMES__', 'TEX__', 'x', 'y'); +options = struct(); +options.datafile = 'data.mat'; +series = histvalf_initvalf('INITVAL_FILE', M, options); +failed_tests = my_assert(failed_tests, series.init == dates('1Y'), ... + '*.mat file first_obs test'); +failed_tests = my_assert(failed_tests, series.nobs == 10, ... + '*.mat file nobs test'); + +fh = fopen('data.csv', 'w'); +fprintf(fh, 'x,y\n'); +for i = 1:size(x,1) + fprintf(fh, '%f,%f\n', x(i), y(i)); +end +fclose(fh); + +if ~verLessThan('matlab', '8.2') + writetable(table(x,y), 'data.xlsx') + options = struct(); + options.datafile = 'data.xlsx'; + series = histvalf_initvalf('INITVAL_FILE', M, options); + failed_tests = my_assert(failed_tests, series.init == dates('1Y'), ... + '*.xlsx file first_obs test'); + failed_tests = my_assert(failed_tests, series.nobs == 10, ... + '*.xlsx file nobs test'); + num_tests = num_tests + 2; + + if ispc + writetable(table(x,y), 'data.xls') + options = struct(); + options.datafile = 'data.xls'; + series = histvalf_initvalf('INITVAL_FILE', M, options); + failed_tests = my_assert(failed_tests, series.init == dates('1Y'), ... + '*.xls file first_obs test'); + failed_tests = my_assert(failed_tests, series.nobs == 10, ... + '*.xls file nobs test'); + num_tests = num_tests + 2; + end +end + +cd(getenv('TOP_TEST_DIR')); +fid = fopen('histval_initval_file_unit_tests.m.trs', 'w+'); +num_failed_tests = length(failed_tests) +if num_failed_tests > 0 + fprintf(fid,':test-result: FAIL\n'); + fprintf(fid,':number-tests: %d\n', num_tests); + fprintf(fid,':number-failed-tests: %d\n', num_failed_tests); + fprintf(fid,':list-of-failed-tests: %s\n', failed_tests{:}); +else + fprintf(fid,':test-result: PASS\n'); + fprintf(fid,':number-tests: %d\n', num_tests); + fprintf(fid,':number-failed-tests: 0\n'); +end +fclose(fid); +exit; diff --git a/tests/identification/BrockMirman/BrockMirman.mod b/tests/identification/BrockMirman/BrockMirman.mod index dfb56bd7d5d489125e2ab88ecb4244acc261feb8..46b76aa6167200e4e8e3216ea8af8c2e685719c5 100644 --- a/tests/identification/BrockMirman/BrockMirman.mod +++ b/tests/identification/BrockMirman/BrockMirman.mod @@ -65,8 +65,42 @@ steady; % compute steady state given the starting values resid; % check the starting values for the steady state check; % check Blanchard & Khan rank condition +@#define CORRFLAG = 1 + @#ifdef kronflag -identification(ar=3, useautocorr=1, nodisplay, nograph, parameter_set=calibration, analytic_derivation_mode=@{kronflag}); +identification(ar=3, useautocorr=@{CORRFLAG}, nodisplay, nograph, parameter_set=calibration, analytic_derivation_mode=@{kronflag},tol_rank=1e-8); @#else -identification(ar=3, useautocorr=1, nodisplay, nograph, parameter_set=calibration, analytic_derivation_mode=0); +identification(ar=3, useautocorr=@{CORRFLAG}, nodisplay, nograph, parameter_set=calibration, analytic_derivation_mode=0, tol_rank=1e-8); @#endif + +% Unit test for correct identification results +load('BrockMirman/identification/BrockMirman_Current_params_identif.mat','ide_moments_point', 'ide_spectrum_point', 'ide_minimal_point', 'ide_reducedform_point') +pause(1); +chk.ind0 = [1 1 1 1 1]; +chk.indno = [1 0 0 0 1]; +chk.jweak = [1 0 0 0 1]; +chk.jweak_pair = [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0]; +for strVars = {'ind0' 'indno' 'jweak' 'jweak_pair'} + if ~isequal(ide_moments_point.(strVars{:}) , chk.(strVars{:})) + disp('dMoments:') + disp(ide_moments_point.dMOMENTS); + disp(strVars{:}) + disp(ide_moments_point.(strVars{:})); + error('identification based on moments is wrong for %s',strVars{:}) + end + if ~isequal(ide_spectrum_point.(strVars{:}) , chk.(strVars{:})) + disp('dSPECTRUM'); + disp(ide_spectrum_point.dSPECTRUM); + disp(strVars{:}) + disp(ide_spectrum_point.(strVars{:})); + error('identification based on spectrum is wrong for %s',strVars{:}) + end + if ~isequal(ide_minimal_point.(strVars{:}) , chk.(strVars{:})) + disp('dMINIMAL') + disp(ide_minimal_point.dMINIMAL); + disp(strVars{:}) + disp(ide_minimal_point.(strVars{:})); + error('identification based on minimal system is wrong for %s',strVars{:}) + end +end + diff --git a/tests/identification/LindeTrabandt/LindeTrabandt2019_xfail.mod b/tests/identification/LindeTrabandt/LindeTrabandt2019_xfail.mod index 9494da438e92080c3d237b1a52ac6d23696954bc..1dcf57b2de8d2c1e4be552f04ce536cd160e4a3d 100644 --- a/tests/identification/LindeTrabandt/LindeTrabandt2019_xfail.mod +++ b/tests/identification/LindeTrabandt/LindeTrabandt2019_xfail.mod @@ -370,10 +370,6 @@ steady; resid; check; -/* Skip test under MATLAB R2009b - MATLAB crashes, most likely due to an internal bug */ -if isoctave || ~matlab_ver_less_than('7.10') - identification(order=1,no_identification_strength,analytic_derivation_mode= 0,ar=5); %works %identification(no_identification_strength,analytic_derivation_mode= 1,ar=5); %works, this takes the longest due to Kronecker products options_.dynatol.x = 1e-9; @@ -382,7 +378,3 @@ identification(order=1,no_identification_strength,analytic_derivation_mode=-2,ar options_.dynatol.x = 1e-5; %this is the default value identification(order=1,no_identification_strength,analytic_derivation_mode=-1,ar=5); %works only if GBAR_o_YBAR is uncommented identification(order=1,no_identification_strength,analytic_derivation_mode=-2,ar=5); %works only if GBAR_o_YBAR is uncommented - -else % Skip test under old MATLAB - error('Test failed as expected') -end diff --git a/tests/identification/kim/kim2.mod b/tests/identification/kim/kim2.mod index 48697badd9fa2c982c4b09322ccc8d109dd40580..57a7faff469a70741c7ae86feff62cd030df7f37 100644 --- a/tests/identification/kim/kim2.mod +++ b/tests/identification/kim/kim2.mod @@ -81,17 +81,48 @@ end; varobs c i; -/* Skip test under MATLAB R2009b - MATLAB crashes, most likely due to an internal bug */ -if isoctave || ~matlab_ver_less_than('7.10') - -identification(advanced=1,max_dim_cova_group=3); +identification(advanced=1,max_dim_cova_group=3,tol_rank=1e-8); //varobs c i lam; //to check if observing lam identifies phi and theta //identification(ar=1,advanced=1,max_dim_cova_group=3,prior_mc=250); //identification(prior_mc=100); +% Unit test for analytic_derivation_mode +load('kim2/identification/kim2_prior_mean_identif.mat','store_options_ident') +if store_options_ident.analytic_derivation~=1 && store_options_ident.analytic_derivation_mode~=-2 + error('the steady state file changed parameters and we should switch to numerical derivatives for the steady state, i.e. analytic_derivation_mode=-2') +end +% Unit test for correct identification results +load('kim2/identification/kim2_prior_mean_identif.mat','ide_moments_point', 'ide_spectrum_point', 'ide_minimal_point', 'ide_reducedform_point') +pause(1); +chk.ind0 = [1 1 1 0]; +chk.indno = [0 0 0 1; 0 1 1 0]; +chk.jweak = [0 1 1 0]; +chk.jweak_pair = [0 0 0 0 1 0 0 0 0 0]; +for strVars = {'ind0' 'indno' 'jweak' 'jweak_pair'} + if ~isequal(ide_moments_point.(strVars{:}) , chk.(strVars{:})) + disp('dMoments:') + disp(ide_moments_point.dMOMENTS); + disp(strVars{:}) + disp(ide_moments_point.(strVars{:})); + error('identification based on moments is wrong for %s',strVars{:}) + end + if ~isequal(ide_spectrum_point.(strVars{:}) , chk.(strVars{:})) + disp('dSPECTRUM'); + disp(ide_spectrum_point.dSPECTRUM); + disp(strVars{:}) + disp(ide_spectrum_point.(strVars{:})); + error('identification based on spectrum is wrong for %s',strVars{:}) + end + if ~isequal(ide_minimal_point.(strVars{:}) , chk.(strVars{:})) + disp('dMINIMAL') + disp(ide_minimal_point.dMINIMAL); + disp(strVars{:}) + disp(ide_minimal_point.(strVars{:})); + error('identification based on minimal system is wrong for %s',strVars{:}) + end +end + +% Integration test if identification works without priors estim_params_=[]; identification(advanced=1,max_dim_cova_group=3); - -end diff --git a/tests/initval_file/ramst_initval_file.mod b/tests/initval_file/ramst_initval_file.mod deleted file mode 100644 index 2cc8f91ed9f9cd5efaf80a762bf9c1ec03c080ee..0000000000000000000000000000000000000000 --- a/tests/initval_file/ramst_initval_file.mod +++ /dev/null @@ -1,55 +0,0 @@ -/* Test for the initval_file() command. This file needs ramst_initval_file_data.m. It should give results similar to those of ramst.mod */ - -var c k; -varexo x; - -parameters alph gam delt bet aa; -alph=0.5; -gam=0.5; -delt=0.02; -bet=0.05; -aa=0.5; - - -model; -c + k - aa*x*k(-1)^alph - (1-delt)*k(-1); -c^(-gam) - (1+bet)^(-1)*(aa*alph*x(+1)*k^(alph-1) + 1 - delt)*c(+1)^(-gam); -end; - -initval; -x = 1; -k = ((delt+bet)/(1.0*aa*alph))^(1/(alph-1)); -c = aa*k^alph-delt*k; -end; - -steady; - -initval_file(filename = ramst_initval_file_data_row_vec_mat); -if oo_.exo_simul(2) ~= 1.2 - error('initval_file problem with exogenous variable'); -end -if oo_.endo_simul(2, 2) ~= 13 - error('initval_file option problem with endogenous variable'); -end -perfect_foresight_setup(periods=200); -perfect_foresight_solver; - -oo_.exo_simul = []; -oo_.endo_simul = []; - -initval_file(filename = ramst_initval_file_data_col_vec_mat); -if oo_.exo_simul(2) ~= 1.2 - error('initval_file problem with exogenous variable'); -end -if oo_.endo_simul(2, 2) ~= 13 - error('initval_file problem with endogenous variable'); -end - -perfect_foresight_setup(periods=200); -perfect_foresight_solver; - -if ispc() - initval_file(filename = ramst_initval_file_excel); - perfect_foresight_setup(periods=200); - perfect_foresight_solver; -end diff --git a/tests/kalman_filter_smoother/check_variable_dimensions/fs2000_ML.mod b/tests/kalman_filter_smoother/check_variable_dimensions/fs2000_ML.mod index b333d7ff0773dd523b5e33ef114999a94e353251..19d5c5c1a24da0f247a95628c7450bc1fa4fa02e 100644 --- a/tests/kalman_filter_smoother/check_variable_dimensions/fs2000_ML.mod +++ b/tests/kalman_filter_smoother/check_variable_dimensions/fs2000_ML.mod @@ -116,7 +116,7 @@ corr e_m, e_a, 0; stderr gp_obs, 0.01; end; options_.prior_trunc=0; -estimation(order=1,datafile='../fsdat_simul', nobs=192, loglinear, forecast=8,smoother,filter_covariance,filtered_vars,filter_step_ahead=[1,2,4],filter_decomposition,selected_variables_only) m P c e W R k d y gy_obs; +estimation(order=1,datafile='../fsdat_simul', nobs=192, loglinear, moments_varendo,conditional_variance_decomposition=[1,3],forecast=8,smoother,filter_covariance,filtered_vars,filter_step_ahead=[1,2,4],filter_decomposition,selected_variables_only) m P c e W R k d y gy_obs gp_obs; if size(oo_.FilteredVariablesKStepAhead,3)~=(options_.nobs+max(options_.filter_step_ahead)) || ... diff --git a/tests/measurement_errors/ls2003a.mod b/tests/measurement_errors/ls2003a.mod index 6d0cbee680f62012239292cd5f0ee6d6f3f295b7..253900230e1775e8063bae4c8c781a13ff22bed8 100644 --- a/tests/measurement_errors/ls2003a.mod +++ b/tests/measurement_errors/ls2003a.mod @@ -61,7 +61,10 @@ stderr e_ys,inv_gamma_pdf,1.2533,0.6551; stderr e_pies,inv_gamma_pdf,1.88,0.9827; stderr dq,inv_gamma_pdf,0.001,0.0001; stderr de,inv_gamma_pdf,0.001,0.0001; +stderr pie_obs,inv_gamma_pdf,0.001,0.0001; +corr dq,de, normal_pdf,0,1; +corr pie_obs,de, normal_pdf,0,1; end; -estimation(datafile=data_ca1,first_obs=8,nobs=79,mh_nblocks=10,prefilter=1,mh_jscale=0.5,mh_replic=0,mode_check); - +estimation(datafile=data_ca1,first_obs=8,nobs=79,mh_nblocks=1,prefilter=1,mh_jscale=0.5,mh_replic=3000,mode_check); +generate_trace_plots(1); diff --git a/tests/moments/example1_one_sided_hp_test.mod b/tests/moments/example1_one_sided_hp_test.mod index 1e61f23d26ef407864f1dbfdecfc3bc95f944120..c87763ff06c2a01f9a1572c2945cd56f781d0345 100644 --- a/tests/moments/example1_one_sided_hp_test.mod +++ b/tests/moments/example1_one_sided_hp_test.mod @@ -68,3 +68,14 @@ end; steady(solve_algo=4,maxit=1000); stoch_simul(order=1,nofunctions,one_sided_hp_filter=1600,irf=0,periods=5000,filtered_theoretical_moments_grid=8192); + +varobs k c y; + +shocks; +var e; stderr 0.009; +var u; stderr 0.009; +var e, u = phi*0.009*0.009; +var c; stderr 0.01; +end; + +stoch_simul(order=1,nofunctions,one_sided_hp_filter=1600,irf=0,periods=5000,filtered_theoretical_moments_grid=8192); diff --git a/tests/moments/fs2000_post_moments.mod b/tests/moments/fs2000_post_moments.mod index 3ea15dcef0946954f34f1a85f03927a40fa13c09..fce555f413c5c50a86e4267c5a0ca679ca4509f6 100644 --- a/tests/moments/fs2000_post_moments.mod +++ b/tests/moments/fs2000_post_moments.mod @@ -169,7 +169,7 @@ end conditional_variance_decomposition=mean(conditional_variance_decomposition,4); nvars=M_.orig_endo_nbr; -horizon_size=size(conditional_variance_decomposition,3); +horizon_size=size(conditional_variance_decomposition,2); for var_iter_1=1:nvars for shock_iter=1:M_.exo_nbr for horizon_iter=1:horizon_size @@ -180,6 +180,105 @@ for var_iter_1=1:nvars end end +// case with measurement error +estimated_params; +alp, beta_pdf, 0.356, 0.02; +bet, beta_pdf, 0.993, 0.002; +gam, normal_pdf, 0.0085, 0.003; +mst, normal_pdf, 1.0002, 0.007; +rho, beta_pdf, 0.129, 0.100; +psi, beta_pdf, 0.65, 0.05; +del, beta_pdf, 0.01, 0.005; +stderr e_a, inv_gamma_pdf, 0.035449, inf; +stderr e_m, inv_gamma_pdf, 0.008862, inf; +stderr gp_obs, inv_gamma_pdf, 0.003, inf; +end; + +estimation(order=1,mode_compute=5, datafile='../fs2000/fsdat_simul.m', nobs=192, loglinear, mh_replic=20, mh_nblocks=1, mh_jscale=0.8,moments_varendo, +conditional_variance_decomposition=[2,2000],consider_all_endogenous,sub_draws=2); + +stoch_simul(order=1,conditional_variance_decomposition=[2,2000],noprint,nograph); +par=load([M_.fname filesep 'metropolis' filesep M_.fname '_posterior_draws1']); + +for par_iter=1:size(par.pdraws,1) + M_=set_parameters_locally(M_,par.pdraws{par_iter,1}); + [info, oo_, options_, M_]=stoch_simul(M_, options_, oo_, var_list_); + correlation(:,:,par_iter)=cell2mat(oo_.autocorr); + covariance(:,:,par_iter)=oo_.var; + conditional_variance_decomposition(:,:,:,par_iter)=oo_.conditional_variance_decomposition; + conditional_variance_decomposition_ME(:,:,:,par_iter)=oo_.conditional_variance_decomposition_ME; + variance_decomposition(:,:,par_iter)=oo_.variance_decomposition; + variance_decomposition_ME(:,:,par_iter)=oo_.variance_decomposition_ME; + [~,obs_order]=sort(options_.varobs_id); +end + +correlation=mean(correlation,3); +nvars=M_.orig_endo_nbr; +for var_iter_1=1:nvars + for var_iter_2=1:nvars + if max(abs(correlation(var_iter_1,var_iter_2:nvars:end)'-oo_.PosteriorTheoreticalMoments.dsge.correlation.Mean.(M_.endo_names{var_iter_1}).(M_.endo_names{var_iter_2})))>1e-8 + error('Correlations do not match') + end + end +end + +covariance=mean(covariance,3); +nvars=M_.orig_endo_nbr; +for var_iter_1=1:nvars + for var_iter_2=var_iter_1:nvars + if max(abs(covariance(var_iter_1,var_iter_2)-oo_.PosteriorTheoreticalMoments.dsge.covariance.Mean.(M_.endo_names{var_iter_1}).(M_.endo_names{var_iter_2})))>1e-8 + error('Covariances do not match') + end + end +end + +variance_decomposition=mean(variance_decomposition,3); +nvars=M_.orig_endo_nbr; +for var_iter_1=1:nvars + for shock_iter=1:M_.exo_nbr + if max(abs(variance_decomposition(var_iter_1,shock_iter)/100-oo_.PosteriorTheoreticalMoments.dsge.VarianceDecomposition.Mean.(M_.endo_names{var_iter_1}).(M_.exo_names{shock_iter})))>1e-8 + error('Variance decomposition does not match') + end + end +end + +variance_decomposition_ME=mean(variance_decomposition_ME,3); +nvars=length(options_.varobs); +for var_iter_1=1:nvars + for shock_iter=1:M_.exo_nbr + if max(abs(variance_decomposition_ME(obs_order(var_iter_1),shock_iter)/100-oo_.PosteriorTheoreticalMoments.dsge.VarianceDecompositionME.Mean.(options_.varobs{var_iter_1}).(M_.exo_names{shock_iter})))>1e-8 + error('Variance decomposition does not match') + end + end +end + +conditional_variance_decomposition=mean(conditional_variance_decomposition,4); +nvars=M_.orig_endo_nbr; +horizon_size=size(conditional_variance_decomposition,2); +for var_iter_1=1:nvars + for shock_iter=1:M_.exo_nbr + for horizon_iter=1:horizon_size + if max(abs(conditional_variance_decomposition(var_iter_1,horizon_iter,shock_iter)-oo_.PosteriorTheoreticalMoments.dsge.ConditionalVarianceDecomposition.Mean.(M_.endo_names{var_iter_1}).(M_.exo_names{shock_iter})(horizon_iter)))>1e-8 + error('Conditional Variance decomposition does not match') + end + end + end +end + +conditional_variance_decomposition_ME=mean(conditional_variance_decomposition_ME,4); +exo_names=[M_.exo_names;'ME']; +nvars=length(options_.varobs); +horizon_size=size(conditional_variance_decomposition_ME,2); +for var_iter_1=1:nvars + for shock_iter=1:M_.exo_nbr+1 + for horizon_iter=1:horizon_size + if max(abs(conditional_variance_decomposition_ME(obs_order(var_iter_1),horizon_iter,shock_iter)-oo_.PosteriorTheoreticalMoments.dsge.ConditionalVarianceDecompositionME.Mean.(options_.varobs{var_iter_1}).(exo_names{shock_iter})(horizon_iter)))>1e-8 + error('Conditional Variance decomposition does not match') + end + end + end +end + /* * The following lines were used to generate the data file. If you want to * generate another random data file, comment the "estimation" line and uncomment diff --git a/tests/optimizers/fs2000_3.mod b/tests/optimizers/fs2000_3.mod index 09d2c59a5ed869aaf2d350ba80280fbe2ab9ce6d..5efc98d2a4e87b089c3a0ab7b46a1256733d7111 100644 --- a/tests/optimizers/fs2000_3.mod +++ b/tests/optimizers/fs2000_3.mod @@ -1,7 +1,5 @@ @#include "fs2000.common.inc" -% Skip the test under R2009b, because fminunc fails due to Inf value. -% It remains to be determined in which version it started to work. -if exist('fminunc','file') && (isoctave || ~matlab_ver_less_than('7.10')) +if exist('fminunc','file') estimation(mode_compute=3,order=1, datafile='../fs2000/fsdat_simul', nobs=192, mh_replic=0); end diff --git a/tests/pruning/AnSchorfheide_pruned_state_space.mod b/tests/pruning/AnSchorfheide_pruned_state_space.mod index 9afd133b825973e6b69847a90519c44475c81c10..4858f73182499d0df44536d94b81dc085fb1fdb9 100644 --- a/tests/pruning/AnSchorfheide_pruned_state_space.mod +++ b/tests/pruning/AnSchorfheide_pruned_state_space.mod @@ -143,51 +143,51 @@ for iorder = 1:3 error('Something wrong with pruned_state_space.m compared to Andreasen et al 2018 Toolbox v2 at order %d.',iorder); end end -skipline(); -fprintf('Note that at third order, there is an error in the computation of Var_z in Andreasen et al (2018)''s toolbox, @wmutschl is in contact to clarify this.\n'); -fprintf('EXAMPLE:\n') -fprintf(' Consider Var[kron(kron(xf,xf),xf)] = E[kron(kron(kron(kron(kron(xf,xf),xf),xf),xf),xf)] - E[kron(kron(xf,xf),xf)]*E[kron(kron(xf,xf),xf)].''\n'); -fprintf(' Now note that xf=hx*xf(-1)+hu*u is Gaussian, that is E[kron(kron(xf,xf),xf)]=0, and Var[kron(kron(xf,xf),xf)] are the sixth-order product moments\n'); -fprintf(' which can be computed using the prodmom.m function by providing E[xf*xf''] as covariance matrix.\n'); -fprintf(' In order to replicate this you have to change UnconditionalMoments_3rd_Lyap.m to also output Var_z.\n') - -dynare_nx = M_.nspred; -dynare_E_xf2 = pruned_state_space.order_3.Var_z(1:dynare_nx,1:dynare_nx); -dynare_E_xf6 = pruned_state_space.order_3.Var_z((end-dynare_nx^3+1):end,(end-dynare_nx^3+1):end); -dynare_E_xf6 = dynare_E_xf6(:); - -Andreasen_nx = M_.nspred+M_.exo_nbr; -Andreasen_E_xf2 = outAndreasenetal.order_3.Var_z(1:Andreasen_nx,1:Andreasen_nx); -Andreasen_E_xf6 = outAndreasenetal.order_3.Var_z((end-Andreasen_nx^3+1):end,(end-Andreasen_nx^3+1):end); -Andreasen_E_xf6 = Andreasen_E_xf6(:); - -fprintf('Second-order product moments of xf and u are the same:\n') -norm_E_xf2 = norm(dynare_E_xf2-Andreasen_E_xf2(1:M_.nspred,1:M_.nspred),Inf) -norm_E_uu = norm(M_.Sigma_e-Andreasen_E_xf2(M_.nspred+(1:M_.exo_nbr),M_.nspred+(1:M_.exo_nbr)),Inf) - -% Compute unique sixth-order product moments of xf, i.e. unique(E[kron(kron(kron(kron(kron(xf,xf),xf),xf),xf),xf)],'stable') -dynare_nx6 = dynare_nx*(dynare_nx+1)/2*(dynare_nx+2)/3*(dynare_nx+3)/4*(dynare_nx+4)/5*(dynare_nx+5)/6; -dynare_Q6Px = Q6_plication(dynare_nx); -dynare_COMBOS6 = flipud(allVL1(dynare_nx, 6)); %all possible (unique) combinations of powers that sum up to six -dynare_true_E_xf6 = zeros(dynare_nx6,1); %only unique entries -for j6 = 1:size(dynare_COMBOS6,1) - dynare_true_E_xf6(j6) = prodmom(dynare_E_xf2, 1:dynare_nx, dynare_COMBOS6(j6,:)); -end -dynare_true_E_xf6 = dynare_Q6Px*dynare_true_E_xf6; %add duplicate entries -norm_dynare_E_xf6 = norm(dynare_true_E_xf6 - dynare_E_xf6, Inf); - -Andreasen_nx6 = Andreasen_nx*(Andreasen_nx+1)/2*(Andreasen_nx+2)/3*(Andreasen_nx+3)/4*(Andreasen_nx+4)/5*(Andreasen_nx+5)/6; -Andreasen_Q6Px = Q6_plication(Andreasen_nx); -Andreasen_COMBOS6 = flipud(allVL1(Andreasen_nx, 6)); %all possible (unique) combinations of powers that sum up to six -Andreasen_true_E_xf6 = zeros(Andreasen_nx6,1); %only unique entries -for j6 = 1:size(Andreasen_COMBOS6,1) - Andreasen_true_E_xf6(j6) = prodmom(Andreasen_E_xf2, 1:Andreasen_nx, Andreasen_COMBOS6(j6,:)); -end -Andreasen_true_E_xf6 = Andreasen_Q6Px*Andreasen_true_E_xf6; %add duplicate entries -norm_Andreasen_E_xf6 = norm(Andreasen_true_E_xf6 - Andreasen_E_xf6, Inf); - -fprintf('Sixth-order product moments of xf and u are not the same!\n'); -fprintf(' Dynare maximum absolute deviations of sixth-order product moments of xf: %d\n',norm_dynare_E_xf6) -fprintf(' Andreasen et al maximum absolute deviations of sixth-order product moments of xf: %d\n',norm_Andreasen_E_xf6) -skipline(); -fprintf('Note that the standard deviations are set quite high to make the numerical differences more apparent.\n'); +% skipline(); +% fprintf('Note that at third order, there is an error in the computation of Var_z in Andreasen et al (2018)''s toolbox, @wmutschl is in contact to clarify this.\n'); +% fprintf('EXAMPLE:\n') +% fprintf(' Consider Var[kron(kron(xf,xf),xf)] = E[kron(kron(kron(kron(kron(xf,xf),xf),xf),xf),xf)] - E[kron(kron(xf,xf),xf)]*E[kron(kron(xf,xf),xf)].''\n'); +% fprintf(' Now note that xf=hx*xf(-1)+hu*u is Gaussian, that is E[kron(kron(xf,xf),xf)]=0, and Var[kron(kron(xf,xf),xf)] are the sixth-order product moments\n'); +% fprintf(' which can be computed using the prodmom.m function by providing E[xf*xf''] as covariance matrix.\n'); +% fprintf(' In order to replicate this you have to change UnconditionalMoments_3rd_Lyap.m to also output Var_z.\n') +% +% dynare_nx = M_.nspred; +% dynare_E_xf2 = pruned_state_space.order_3.Var_z(1:dynare_nx,1:dynare_nx); +% dynare_E_xf6 = pruned_state_space.order_3.Var_z((end-dynare_nx^3+1):end,(end-dynare_nx^3+1):end); +% dynare_E_xf6 = dynare_E_xf6(:); +% +% Andreasen_nx = M_.nspred+M_.exo_nbr; +% Andreasen_E_xf2 = outAndreasenetal.order_3.Var_z(1:Andreasen_nx,1:Andreasen_nx); +% Andreasen_E_xf6 = outAndreasenetal.order_3.Var_z((end-Andreasen_nx^3+1):end,(end-Andreasen_nx^3+1):end); +% Andreasen_E_xf6 = Andreasen_E_xf6(:); +% +% fprintf('Second-order product moments of xf and u are the same:\n') +% norm_E_xf2 = norm(dynare_E_xf2-Andreasen_E_xf2(1:M_.nspred,1:M_.nspred),Inf) +% norm_E_uu = norm(M_.Sigma_e-Andreasen_E_xf2(M_.nspred+(1:M_.exo_nbr),M_.nspred+(1:M_.exo_nbr)),Inf) +% +% % Compute unique sixth-order product moments of xf, i.e. unique(E[kron(kron(kron(kron(kron(xf,xf),xf),xf),xf),xf)],'stable') +% dynare_nx6 = dynare_nx*(dynare_nx+1)/2*(dynare_nx+2)/3*(dynare_nx+3)/4*(dynare_nx+4)/5*(dynare_nx+5)/6; +% dynare_Q6Px = Q6_plication(dynare_nx); +% dynare_COMBOS6 = flipud(allVL1(dynare_nx, 6)); %all possible (unique) combinations of powers that sum up to six +% dynare_true_E_xf6 = zeros(dynare_nx6,1); %only unique entries +% for j6 = 1:size(dynare_COMBOS6,1) +% dynare_true_E_xf6(j6) = prodmom(dynare_E_xf2, 1:dynare_nx, dynare_COMBOS6(j6,:)); +% end +% dynare_true_E_xf6 = dynare_Q6Px*dynare_true_E_xf6; %add duplicate entries +% norm_dynare_E_xf6 = norm(dynare_true_E_xf6 - dynare_E_xf6, Inf); +% +% Andreasen_nx6 = Andreasen_nx*(Andreasen_nx+1)/2*(Andreasen_nx+2)/3*(Andreasen_nx+3)/4*(Andreasen_nx+4)/5*(Andreasen_nx+5)/6; +% Andreasen_Q6Px = Q6_plication(Andreasen_nx); +% Andreasen_COMBOS6 = flipud(allVL1(Andreasen_nx, 6)); %all possible (unique) combinations of powers that sum up to six +% Andreasen_true_E_xf6 = zeros(Andreasen_nx6,1); %only unique entries +% for j6 = 1:size(Andreasen_COMBOS6,1) +% Andreasen_true_E_xf6(j6) = prodmom(Andreasen_E_xf2, 1:Andreasen_nx, Andreasen_COMBOS6(j6,:)); +% end +% Andreasen_true_E_xf6 = Andreasen_Q6Px*Andreasen_true_E_xf6; %add duplicate entries +% norm_Andreasen_E_xf6 = norm(Andreasen_true_E_xf6 - Andreasen_E_xf6, Inf); +% +% fprintf('Sixth-order product moments of xf and u are not the same!\n'); +% fprintf(' Dynare maximum absolute deviations of sixth-order product moments of xf: %d\n',norm_dynare_E_xf6) +% fprintf(' Andreasen et al maximum absolute deviations of sixth-order product moments of xf: %d\n',norm_Andreasen_E_xf6) +% skipline(); +% fprintf('Note that the standard deviations are set quite high to make the numerical differences more apparent.\n'); diff --git a/tests/recursive/data_ca1_xls.xls b/tests/recursive/data_ca1_xls.xls deleted file mode 100644 index 062baaeaa9512d5cc7755a6fa06df9aa30f4c27e..0000000000000000000000000000000000000000 Binary files a/tests/recursive/data_ca1_xls.xls and /dev/null differ diff --git a/tests/recursive/ls2003_bayesian_xls.mod b/tests/recursive/ls2003_bayesian_xls.mod index 0fb926123c2db246f786b8a86204e150fb1967f8..c2795e475e561421f5c5509788ef106f212b8c5c 100644 --- a/tests/recursive/ls2003_bayesian_xls.mod +++ b/tests/recursive/ls2003_bayesian_xls.mod @@ -61,8 +61,4 @@ stderr e_ys,inv_gamma_pdf,1.2533,0.6551; stderr e_pies,inv_gamma_pdf,1.88,0.9827; end; -if isoctave || ~matlab_ver_less_than('7.14') % xlsread is able to read XLSX without Excel installed since R2012a estimation(datafile='data_ca1_xls.xlsx',first_obs=8,nobs=[76 79],mh_nblocks=1,prefilter=1,mh_jscale=0.5,mh_replic=2000,forecast=8) y_obs R_obs pie_obs dq de; -else -estimation(datafile='data_ca1_xls.xls',first_obs=8,nobs=[76 79],mh_nblocks=1,prefilter=1,mh_jscale=0.5,mh_replic=2000,forecast=8) y_obs R_obs pie_obs dq de; -end diff --git a/tests/run_all_unitary_tests.m b/tests/run_all_unitary_tests.m index a2d21ba65d45ded2c00e3fdc89a1cf69fdafb4d1..e9722569f8cef9fb2cf00579be0668f9087b89b6 100644 --- a/tests/run_all_unitary_tests.m +++ b/tests/run_all_unitary_tests.m @@ -39,7 +39,7 @@ if isoctave end % Set random seed, for reproducibility -if isoctave || matlab_ver_less_than('7.12') +if isoctave randn('state',1); rand('state',1); else diff --git a/tests/run_block_byte_tests_matlab.m b/tests/run_block_byte_tests_matlab.m index 015f4608f4145258c1cd00d96e65970aa166d1a5..3765ef8572a8998cef1af6f7b2be8cf6efcdd71d 100644 --- a/tests/run_block_byte_tests_matlab.m +++ b/tests/run_block_byte_tests_matlab.m @@ -1,4 +1,4 @@ -% Copyright (C) 2011-2017 Dynare Team +% Copyright (C) 2011-2020 Dynare Team % % This file is part of Dynare. % @@ -40,14 +40,14 @@ cd([top_test_dir filesep 'block_bytecode']); has_optimization_toolbox = user_has_matlab_license('optimization_toolbox'); tic; for blockFlag = 0:1 - for bytecodeFlag = 0:1 + for storageFlag = 0:2 % 0=M-file, 1=use_dll, 2=bytecode default_solve_algo = 2; default_stack_solve_algo = 0; - if ~blockFlag && ~bytecodeFlag - solve_algos = 1:4; + if ~blockFlag && storageFlag ~= 2 + solve_algos = [1:4 9]; stack_solve_algos = [0 6]; - elseif blockFlag && ~bytecodeFlag - solve_algos = [1:4 6:8]; + elseif blockFlag && storageFlag ~= 2 + solve_algos = [1:4 6:9]; stack_solve_algos = 0:4; else solve_algos = 1:8; @@ -59,13 +59,13 @@ for blockFlag = 0:1 for i = 1:length(solve_algos) num_block_tests = num_block_tests + 1; - if ~blockFlag && ~bytecodeFlag && (i == 1) + if ~blockFlag && storageFlag == 0 && (i == 1) % This is the reference simulation path against which all % other simulations will be tested try old_path = path; save wsMat - run_ls2003(blockFlag, bytecodeFlag, solve_algos(i), default_stack_solve_algo) + run_ls2003(blockFlag, storageFlag, solve_algos(i), default_stack_solve_algo) load wsMat path(old_path); y_ref = oo_.endo_simul; @@ -73,31 +73,31 @@ for blockFlag = 0:1 catch exception load wsMat path(old_path); - failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(bytecodeFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')']; - printMakeCheckMatlabErrMsg(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(bytecodeFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')'], exception); + failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')']; + printMakeCheckMatlabErrMsg(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')'], exception); clear exception end else try old_path = path; save wsMat - run_ls2003(blockFlag, bytecodeFlag, solve_algos(i), default_stack_solve_algo) + run_ls2003(blockFlag, storageFlag, solve_algos(i), default_stack_solve_algo) load wsMat path(old_path); % Test against the reference simulation path load('test.mat','y_ref'); diff = oo_.endo_simul - y_ref; - if(abs(diff) > options_.dynatol.x) - failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(bytecodeFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')']; + if abs(diff) > options_.dynatol.x + failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')']; exception = MException('ERROR: simulation path differs from the reference path'); - printMakeCheckMatlabErrMsg(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(bytecodeFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')'], exception); + printMakeCheckMatlabErrMsg(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')'], exception); clear exception end catch exception load wsMat path(old_path); - failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(bytecodeFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')']; - printMakeCheckMatlabErrMsg(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(bytecodeFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')'], exception); + failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')']; + printMakeCheckMatlabErrMsg(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')'], exception); clear exception end end @@ -107,23 +107,23 @@ for blockFlag = 0:1 try old_path = path; save wsMat - run_ls2003(blockFlag, bytecodeFlag, default_solve_algo, stack_solve_algos(i)) + run_ls2003(blockFlag, storageFlag, default_solve_algo, stack_solve_algos(i)) load wsMat path(old_path); % Test against the reference simulation path load('test.mat','y_ref'); diff = oo_.endo_simul - y_ref; - if(abs(diff) > options_.dynatol.x) - failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(bytecodeFlag) ',' num2str(default_solve_algo) ',' num2str(stack_solve_algos(i)) ')']; + if abs(diff) > options_.dynatol.x + failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(default_solve_algo) ',' num2str(stack_solve_algos(i)) ')']; exception = MException('ERROR: simulation path difers from the reference path'); - printMakeCheckMatlabErrMsg(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(bytecodeFlag) ',' num2str(default_solve_algo) ',' num2str(stack_solve_algos(i)) ')'], exception); + printMakeCheckMatlabErrMsg(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(default_solve_algo) ',' num2str(stack_solve_algos(i)) ')'], exception); clear exception end catch exception load wsMat path(old_path); - failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(bytecodeFlag) ',' num2str(default_solve_algo) ',' num2str(stack_solve_algos(i)) ')']; - printMakeCheckMatlabErrMsg(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(bytecodeFlag) ',' num2str(default_solve_algo) ',' num2str(stack_solve_algos(i)) ')'], exception); + failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(default_solve_algo) ',' num2str(stack_solve_algos(i)) ')']; + printMakeCheckMatlabErrMsg(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(default_solve_algo) ',' num2str(stack_solve_algos(i)) ')'], exception); clear exception end end @@ -131,7 +131,7 @@ for blockFlag = 0:1 end ecput = toc; delete('wsMat.mat') -cd(getenv('TOP_TEST_DIR')); +cd(top_test_dir); fid = fopen('run_block_byte_tests_matlab.m.trs', 'w+'); if size(failedBlock,2) > 0 fprintf(fid,':test-result: FAIL\n'); diff --git a/tests/run_block_byte_tests_octave.m b/tests/run_block_byte_tests_octave.m index 3af57728217224d4f53acc93d652e8de54e4be3f..4ff8a1145a0929740bde661f2e4de7544b44285a 100644 --- a/tests/run_block_byte_tests_octave.m +++ b/tests/run_block_byte_tests_octave.m @@ -1,4 +1,4 @@ -## Copyright (C) 2009-2019 Dynare Team +## Copyright (C) 2009-2020 Dynare Team ## ## This file is part of Dynare. ## @@ -42,31 +42,34 @@ num_block_tests = 0; cd([top_test_dir filesep 'block_bytecode']); tic; for blockFlag = 0:1 - for bytecodeFlag = 0:1 + for storageFlag = 0:2 % 0=M-file, 1=use_dll, 2=bytecode default_solve_algo = 2; default_stack_solve_algo = 0; - if !blockFlag && !bytecodeFlag - solve_algos = 0:4; + if !blockFlag && storageFlag != 2 + solve_algos = [0:4 9]; stack_solve_algos = [0 6]; - elseif blockFlag && !bytecodeFlag - solve_algos = [0:4 6:8]; + elseif blockFlag && storageFlag != 2 + solve_algos = [0:4 6:9]; stack_solve_algos = 0:4; else solve_algos = 0:8; stack_solve_algos = 0:5; endif - pause(1) # Workaround for strange race condition related to the _static.m file + # Workaround for strange race condition related to the static/dynamic + # files (especially when we switch to/from use_dll) + rmdir('+ls2003_tmp', 's') + pause(1) for i = 1:length(solve_algos) num_block_tests = num_block_tests + 1; - if !blockFlag && !bytecodeFlag && (i == 1) + if !blockFlag && storageFlag == 0 && (i == 1) ## This is the reference simulation path against which all ## other simulations will be tested try old_path = path; save wsOct - run_ls2003(blockFlag, bytecodeFlag, solve_algos(i), default_stack_solve_algo) + run_ls2003(blockFlag, storageFlag, solve_algos(i), default_stack_solve_algo) load wsOct path(old_path); y_ref = oo_.endo_simul; @@ -74,31 +77,29 @@ for blockFlag = 0:1 catch load wsOct path(old_path); - failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(bytecodeFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')']; - printMakeCheckOctaveErrMsg(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(bytecodeFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')'], lasterror); + failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')']; + printMakeCheckOctaveErrMsg(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')'], lasterror); end_try_catch else try old_path = path; save wsOct - run_ls2003(blockFlag, bytecodeFlag, solve_algos(i), default_stack_solve_algo) + run_ls2003(blockFlag, storageFlag, solve_algos(i), default_stack_solve_algo) load wsOct path(old_path); ## Test against the reference simulation path load('test.mat','y_ref'); diff = oo_.endo_simul - y_ref; - if(abs(diff) > options_.dynatol.x) - failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(bytecodeFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')']; + if abs(diff) > options_.dynatol.x + failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')']; differr.message = ["ERROR: simulation path differs from the reference path" ]; - printMakeCheckOctaveErrMsg(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(bytecodeFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')'], differr); + printMakeCheckOctaveErrMsg(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')'], differr); endif catch load wsOct - e = lasterror(); # The path() command alters the lasterror, because of io package path(old_path); - lasterror(e); - failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(bytecodeFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')']; - printMakeCheckOctaveErrMsg(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(bytecodeFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')'], lasterror); + failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')']; + printMakeCheckOctaveErrMsg(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(solve_algos(i)) ',' num2str(default_stack_solve_algo) ')'], lasterror); end_try_catch endif endfor @@ -107,31 +108,29 @@ for blockFlag = 0:1 try old_path = path; save wsOct - run_ls2003(blockFlag, bytecodeFlag, default_solve_algo, stack_solve_algos(i)) + run_ls2003(blockFlag, storageFlag, default_solve_algo, stack_solve_algos(i)) load wsOct path(old_path); ## Test against the reference simulation path load('test.mat','y_ref'); diff = oo_.endo_simul - y_ref; - if(abs(diff) > options_.dynatol.x) - failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(bytecodeFlag) ',' num2str(default_solve_algo) ',' num2str(stack_solve_algos(i)) ')']; + if abs(diff) > options_.dynatol.x + failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(default_solve_algo) ',' num2str(stack_solve_algos(i)) ')']; differr.message = ["ERROR: simulation path differs from the reference path" ]; - printMakeCheckOctaveErrMsg(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(bytecodeFlag) ',' num2str(default_solve_algo) ',' num2str(stack_solve_algos(i)) ')'], differr); + printMakeCheckOctaveErrMsg(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(default_solve_algo) ',' num2str(stack_solve_algos(i)) ')'], differr); endif catch load wsOct - e = lasterror(); # The path() command alters the lasterror, because of io package path(old_path); - lasterror(e); - failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(bytecodeFlag) ',' num2str(default_solve_algo) ',' num2str(stack_solve_algos(i)) ')']; - printMakeCheckOctaveErrMsg(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(bytecodeFlag) ',' num2str(default_solve_algo) ',' num2str(stack_solve_algos(i)) ')'], lasterror); + failedBlock{size(failedBlock,2)+1} = ['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(default_solve_algo) ',' num2str(stack_solve_algos(i)) ')']; + printMakeCheckOctaveErrMsg(['block_bytecode' filesep 'run_ls2003.m(' num2str(blockFlag) ',' num2str(storageFlag) ',' num2str(default_solve_algo) ',' num2str(stack_solve_algos(i)) ')'], lasterror); end_try_catch endfor endfor endfor ecput = toc; delete('wsOct'); -cd(getenv('TOP_TEST_DIR')); +cd(top_test_dir); fid = fopen('run_block_byte_tests_octave.o.trs', 'w+'); if size(failedBlock,2) > 0 fprintf(fid,':test-result: FAIL\n'); diff --git a/tests/smoother2histval/fs2000_simul.mod b/tests/smoother2histval/fs2000_simul.mod index eb65974e1df9074f25372afdc4ce0f09cd88573a..a682c76c04bbcbb957740d652c13bf2b366a7eb4 100644 --- a/tests/smoother2histval/fs2000_simul.mod +++ b/tests/smoother2histval/fs2000_simul.mod @@ -66,6 +66,10 @@ results_estimation=load('fs2000_smooth_results'); M_.params=results_estimation.M_.params; steady; +OO = load('fs2000_smooth_results.mat'); +M_.params = OO.M_.params; + histval_file(filename = 'fs2000_histval.mat'); -simul(periods = 30); +perfect_foresight_setup(periods = 100); +perfect_foresight_solver; diff --git a/tests/smoother2histval/fs2000_smooth.mod b/tests/smoother2histval/fs2000_smooth.mod index 89447eb57142ae3933b3fc66d92631f63b381210..65bb15b68bcd07d5e6f96aafa18657958a09a13d 100644 --- a/tests/smoother2histval/fs2000_smooth.mod +++ b/tests/smoother2histval/fs2000_smooth.mod @@ -82,6 +82,6 @@ varobs gp_obs gy_obs; options_.solve_tolf = 1e-12; -estimation(order=1,datafile=fsdat_simul,nobs=192,mh_replic=1500,mh_nblocks=1,mh_jscale=0.8,smoother,consider_all_endogenous); +estimation(order=1,datafile=fsdat_simul,mh_replic=1500,mh_nblocks=1,mh_jscale=0.8,smoother,consider_all_endogenous); smoother2histval(period = 5, outfile = 'fs2000_histval.mat'); diff --git a/tests/utils/fataltest.m b/tests/utils/fataltest.m index 6a7b33ba15ee4e457c08b68db7185ee78fee742e..035449cbd76c98e81fa1a02b0bec054572b8f16a 100644 --- a/tests/utils/fataltest.m +++ b/tests/utils/fataltest.m @@ -1,4 +1,4 @@ function fataltest(a,b,n) - if max(max(abs(a)-abs(b))) > 1e-5 + if max(max(abs(a-b))) > 1e-5 error(['Test error in test ' int2str(n)]) end diff --git a/windows/README.txt b/windows/README.txt index f7608c602e328bc40945e938333981dc9c0aaf84..e403fb6f81a61d6eea1447a3311f24a8cd5ae194 100644 --- a/windows/README.txt +++ b/windows/README.txt @@ -21,8 +21,9 @@ Note: Dynare comes with an automated uninstaller, which you can run from the Using Dynare with MATLAB® ───────────────────────── -Dynare requires MATLAB® version 7.9 (R2009b) or above. With older versions of -MATLAB®, it may fail or give unexpected results. +Dynare requires MATLAB® version 8.3 (R2014a) or above. With older versions of +MATLAB®, it may fail or give unexpected results. Only 64-bit versions are +supported. To use Dynare, you just have to add the ‘matlab’ subdirectory of your Dynare installation to MATLAB® path. You have two options for doing that: @@ -52,14 +53,10 @@ Using Dynare with Octave Dynare also works on top of GNU Octave, a free clone of MATLAB® (see <https://www.octave.org>). -This version of Dynare is compiled for Octave 5.2.0 (MinGW, 32-bit and 64-bit), +This version of Dynare is compiled for Octave 5.2.0 (MinGW, 64-bit), and may not work with other versions of Octave. The recommended version of Octave can be downloaded at: - -— For 64-bit systems: https://ftpmirror.gnu.org/gnu/octave/windows/octave-5.2.0_1-w64-installer.exe -— For 32-bit systems: - https://ftpmirror.gnu.org/gnu/octave/windows/octave-5.2.0_1-w32-installer.exe Every time you run Octave, you should type the following command (assuming that you have installed Dynare at the standard location, and replacing ‘4.x.y’ by diff --git a/windows/build.sh b/windows/build.sh index b402d1c2d1c95362e8ef57a138a95d2edad37594..cc507b1727c47ad4619f27103e3084994ce7b9d8 100755 --- a/windows/build.sh +++ b/windows/build.sh @@ -2,7 +2,7 @@ # Produces Windows packages of Dynare (executable installer, 7z and zip archives). # -# The binaries are cross compiled for Windows (32/64bits), Octave and MATLAB +# The binaries are cross compiled for Windows (64-bit), Octave and MATLAB # (all supported versions). # Copyright © 2017-2020 Dynare Team @@ -51,9 +51,7 @@ fi BASENAME=dynare-$VERSION # Set directories for dependencies -LIB32="$ROOT_DIRECTORY"/deps/lib32 LIB64="$ROOT_DIRECTORY"/deps/lib64 -LIB32_MSYS2="$ROOT_DIRECTORY"/deps/lib32-msys2 LIB64_MSYS2="$ROOT_DIRECTORY"/deps/lib64-msys2 # Go to source root directory @@ -80,56 +78,13 @@ fi make -j"$NTHREADS" x86_64-w64-mingw32-strip matlab/preprocessor64/dynare_m.exe x86_64-w64-mingw32-strip dynare++/src/dynare++.exe -mkdir -p dynare++/64-bit/ -cp dynare++/src/dynare++.exe dynare++/64-bit/ - -## Compile 32-bit preprocessor and Dynare++ -# We do not want to clean the doc -for d in preprocessor/src dynare++/integ dynare++/kord dynare++/parser/cc dynare++/src dynare++/sylv dynare++/tl dynare++/utils/cc; do - make -C "$d" clean -done -./configure --host=i686-w64-mingw32 \ - --with-boost="$LIB32_MSYS2" \ - --with-blas="$LIB32_MSYS2"/lib/libopenblas.a \ - --with-lapack="$LIB32_MSYS2"/lib/libopenblas.a \ - --with-matio="$LIB32_MSYS2" \ - --disable-octave \ - --disable-matlab \ - PACKAGE_VERSION="$VERSION" \ - PACKAGE_STRING="dynare $VERSION" -make -j"$NTHREADS" -i686-w64-mingw32-strip matlab/preprocessor32/dynare_m.exe -i686-w64-mingw32-strip dynare++/src/dynare++.exe -mkdir -p dynare++/32-bit/ -cp dynare++/src/dynare++.exe dynare++/32-bit/ ## Define functions for building MEX files ## Note that we do out-of-tree compilation, since we want to do these in ## parallel -# Create Windows 32-bit DLL binaries for MATLAB ≥ R2009b -build_windows_matlab_mex_32 () -{ - mkdir -p "$TMP_DIRECTORY"/matlab-win32/ - cd "$TMP_DIRECTORY"/matlab-win32/ - "$ROOT_DIRECTORY"/../mex/build/matlab/configure \ - --host=i686-w64-mingw32 \ - --with-gsl="$LIB32_MSYS2" \ - --with-matio="$LIB32_MSYS2" \ - --with-slicot="$LIB32"/Slicot/without-underscore \ - --with-matlab="$ROOT_DIRECTORY"/deps/matlab32/R2009b \ - MATLAB_VERSION=R2009b \ - MEXEXT=mexw32 \ - PACKAGE_VERSION="$VERSION" \ - PACKAGE_STRING="dynare $VERSION" - make -j"$NTHREADS" all - i686-w64-mingw32-strip -- **/*.mexw32 - mkdir -p "$ROOT_DIRECTORY"/../mex/matlab/win32-7.9-8.6 - mv -- **/*.mexw32 "$ROOT_DIRECTORY"/../mex/matlab/win32-7.9-8.6 -} - -# Create Windows 64-bit DLL binaries for MATLAB ≥ R2009b and ≤ R2017b +# Create Windows 64-bit DLL binaries for MATLAB ≥ R2014a and ≤ R2017b build_windows_matlab_mex_64_a () { mkdir -p "$TMP_DIRECTORY"/matlab-win64-a/ @@ -139,15 +94,15 @@ build_windows_matlab_mex_64_a () --with-gsl="$LIB64_MSYS2" \ --with-matio="$LIB64_MSYS2" \ --with-slicot="$LIB64"/Slicot/without-underscore \ - --with-matlab="$ROOT_DIRECTORY"/deps/matlab64/R2009b \ - MATLAB_VERSION=R2009b \ + --with-matlab="$ROOT_DIRECTORY"/deps/matlab64/R2014a \ + MATLAB_VERSION=R2014a \ MEXEXT=mexw64 \ PACKAGE_VERSION="$VERSION" \ PACKAGE_STRING="dynare $VERSION" make -j"$NTHREADS" all x86_64-w64-mingw32-strip -- **/*.mexw64 - mkdir -p "$ROOT_DIRECTORY"/../mex/matlab/win64-7.9-9.3 - mv -- **/*.mexw64 "$ROOT_DIRECTORY"/../mex/matlab/win64-7.9-9.3 + mkdir -p "$ROOT_DIRECTORY"/../mex/matlab/win64-8.3-9.3 + mv -- **/*.mexw64 "$ROOT_DIRECTORY"/../mex/matlab/win64-8.3-9.3 } # Create Windows 64-bit DLL binaries for MATLAB ≥ R2018a @@ -171,25 +126,6 @@ build_windows_matlab_mex_64_b () mv -- **/*.mexw64 "$ROOT_DIRECTORY"/../mex/matlab/win64-9.4-9.8 } -# Create Windows DLL binaries for Octave/MinGW (32bit) -build_windows_octave_mex_32 () -{ - mkdir -p "$TMP_DIRECTORY"/octave-32/ - cd "$TMP_DIRECTORY"/octave-32/ - "$ROOT_DIRECTORY"/../mex/build/octave/configure \ - --host=i686-w64-mingw32 \ - --with-gsl="$LIB32_MSYS2" \ - --with-matio="$LIB32_MSYS2" \ - --with-slicot="$LIB32"/Slicot/with-underscore \ - MKOCTFILE="$ROOT_DIRECTORY"/deps/mkoctfile32 \ - PACKAGE_VERSION="$VERSION" \ - PACKAGE_STRING="dynare $VERSION" - make -j"$NTHREADS" all - i686-w64-mingw32-strip -- **/*.mex - mkdir -p "$ROOT_DIRECTORY"/../mex/octave/win32 - mv -- **/*.mex "$ROOT_DIRECTORY"/../mex/octave/win32 -} - # Create Windows DLL binaries for Octave/MinGW (64bit) build_windows_octave_mex_64 () { @@ -211,15 +147,15 @@ build_windows_octave_mex_64 () ## Actually build the MEX files -TASKS=(build_windows_matlab_mex_32 build_windows_matlab_mex_64_a build_windows_matlab_mex_64_b build_windows_octave_mex_32 build_windows_octave_mex_64) -# Reset the number of threads. The mex files for MATLAB/Octave (32-bit and 64-bit) will be built +TASKS=(build_windows_matlab_mex_64_a build_windows_matlab_mex_64_b build_windows_octave_mex_64) +# Reset the number of threads. The mex files for MATLAB/Octave will be built # in parallel, so we need to account for the number of tasks and lower the value of NTHREADS. NTHREADS=$((NTHREADS/${#TASKS[@]})) [[ $NTHREADS -ge 1 ]] || NTHREADS=1 # Ensure that there is at least 1 thread # Build all the mex files (parallel). # Some variables and functions need to be available in subshells. cd "$ROOT_DIRECTORY" -export TMP_DIRECTORY ROOT_DIRECTORY LIB32 LIB32_MSYS2 LIB64 LIB64_MSYS2 VERSION NTHREADS +export TMP_DIRECTORY ROOT_DIRECTORY LIB64 LIB64_MSYS2 VERSION NTHREADS export -f "${TASKS[@]}" parallel "set -ex;shopt -s globstar;" ::: "${TASKS[@]}" @@ -252,7 +188,6 @@ cp -p NEWS.md "$ZIPDIR" cp -p VERSION "$ZIPDIR" cp -p license.txt "$ZIPDIR" cp -p windows/README.txt "$ZIPDIR" -cp -pr windows/deps/mingw32 "$ZIPDIR" cp -pr windows/deps/mingw64 "$ZIPDIR" mkdir -p "$ZIPDIR"/contrib/ms-sbvar/TZcode cp -pr contrib/ms-sbvar/TZcode/MatlabFiles "$ZIPDIR"/contrib/ms-sbvar/TZcode @@ -262,15 +197,13 @@ mkdir "$ZIPDIR"/mex cp -pr mex/octave/ "$ZIPDIR"/mex cp -pr mex/matlab/ "$ZIPDIR"/mex cp -pr matlab "$ZIPDIR" -mkdir -p "$ZIPDIR"/matlab/modules/dseries/externals/x13/windows/32 -cp -p windows/deps/lib32/x13as/x13as.exe "$ZIPDIR"/matlab/modules/dseries/externals/x13/windows/32 mkdir -p "$ZIPDIR"/matlab/modules/dseries/externals/x13/windows/64 cp -p windows/deps/lib64/x13as/x13as.exe "$ZIPDIR"/matlab/modules/dseries/externals/x13/windows/64 cp -pr examples "$ZIPDIR" mkdir -p "$ZIPDIR"/scripts cp -p scripts/dynare.el "$ZIPDIR"/scripts mkdir "$ZIPDIR"/dynare++ -cp -pr dynare++/32-bit/ dynare++/64-bit/ dynare++/dynare_simul/dynare_simul.m "$ZIPDIR"/dynare++ +cp -pr dynare++/src/dynare++.exe dynare++/dynare_simul/dynare_simul.m "$ZIPDIR"/dynare++ mkdir -p "$ZIPDIR"/doc/dynare++ mkdir -p "$ZIPDIR"/doc/dynare-manual.html cp -pr doc/manual/build/html/* "$ZIPDIR"/doc/dynare-manual.html diff --git a/windows/deps/Makefile b/windows/deps/Makefile index e51b9b9fbb48dffafeceab89a8aca7501fe28308..2cbc39b76305595732d49fd8aa412aa7dca0c056 100644 --- a/windows/deps/Makefile +++ b/windows/deps/Makefile @@ -1,4 +1,4 @@ -# Copyright © 2017-2019 Dynare Team +# Copyright © 2017-2020 Dynare Team # # This file is part of Dynare. # @@ -48,18 +48,6 @@ tarballs/slicot-$(SLICOT_VERSION).tar.gz: mkdir -p tarballs wget $(WGET_OPTIONS) -O $@ https://deb.debian.org/debian/pool/main/s/slicot/slicot_$(SLICOT_VERSION).orig.tar.gz -sources32/slicot-$(SLICOT_VERSION)-without-underscore: tarballs/slicot-$(SLICOT_VERSION).tar.gz - rm -rf sources32/slicot-*-without-underscore - mkdir -p $@ - tar xf $< --directory $@ --strip-components=1 - touch $@ - -sources32/slicot-$(SLICOT_VERSION)-with-underscore: tarballs/slicot-$(SLICOT_VERSION).tar.gz - rm -rf sources32/slicot-*-with-underscore - mkdir -p $@ - tar xf $< --directory $@ --strip-components=1 - touch $@ - sources64/slicot-$(SLICOT_VERSION)-with-32bit-integer: tarballs/slicot-$(SLICOT_VERSION).tar.gz rm -rf sources64/slicot-*-with-32bit-integer mkdir -p $@ @@ -84,18 +72,6 @@ sources64/slicot-$(SLICOT_VERSION)-with-64bit-integer-and-underscore: tarballs/s tar xf $< --directory $@ --strip-components=1 touch $@ -lib32/Slicot/without-underscore/lib/libslicot_pic.a: sources32/slicot-$(SLICOT_VERSION)-without-underscore - make -C $< lib SLICOTLIB=../libslicot_pic.a OPTS="-O2 -g -fno-underscoring" FORTRAN=i686-w64-mingw32-gfortran LOADER=i686-w64-mingw32-gfortran ARCH=i686-w64-mingw32-ar - i686-w64-mingw32-strip --strip-debug $</libslicot_pic.a - mkdir -p $(dir $@) - cp $</libslicot_pic.a $@ - -lib32/Slicot/with-underscore/lib/libslicot_pic.a: sources32/slicot-$(SLICOT_VERSION)-with-underscore - make -C $< lib SLICOTLIB=../libslicot_pic.a OPTS="-O2 -g" FORTRAN=i686-w64-mingw32-gfortran LOADER=i686-w64-mingw32-gfortran ARCH=i686-w64-mingw32-ar - i686-w64-mingw32-strip --strip-debug $</libslicot_pic.a - mkdir -p $(dir $@) - cp $</libslicot_pic.a $@ - lib64/Slicot/without-underscore/lib/libslicot_pic.a: sources64/slicot-$(SLICOT_VERSION)-with-32bit-integer make -C $< lib SLICOTLIB=../libslicot_pic.a OPTS="-O2 -g -fno-underscoring" FORTRAN=x86_64-w64-mingw32-gfortran LOADER=x86_64-w64-mingw32-gfortran ARCH=x86_64-w64-mingw32-ar x86_64-w64-mingw32-strip --strip-debug $</libslicot_pic.a @@ -120,9 +96,7 @@ lib64/Slicot/with-underscore/lib/libslicot64_pic.a: sources64/slicot-$(SLICOT_VE mkdir -p $(dir $@) cp $</libslicot64_pic.a $@ -build-slicot: lib32/Slicot/without-underscore/lib/libslicot_pic.a \ - lib32/Slicot/with-underscore/lib/libslicot_pic.a \ - lib64/Slicot/without-underscore/lib/libslicot_pic.a \ +build-slicot: lib64/Slicot/without-underscore/lib/libslicot_pic.a \ lib64/Slicot/without-underscore/lib/libslicot64_pic.a \ lib64/Slicot/with-underscore/lib/libslicot_pic.a \ lib64/Slicot/with-underscore/lib/libslicot64_pic.a @@ -131,8 +105,6 @@ clean-slicot-tar: rm -f tarballs/slicot-$(SLICOT_VERSION).tar.gz clean-slicot-src: - rm -rf sources32/slicot-$(SLICOT_VERSION)-with-underscore - rm -rf sources32/slicot-$(SLICOT_VERSION)-without-underscore rm -rf sources64/slicot-$(SLICOT_VERSION)-with-32bit-integer rm -rf sources64/slicot-$(SLICOT_VERSION)-with-64bit-integer rm -rf sources64/slicot-$(SLICOT_VERSION)-with-32bit-integer-and-underscore @@ -140,7 +112,6 @@ clean-slicot-src: clean-libslicot: rm -rf lib64/Slicot - rm -rf lib32/Slicot clean-slicot-all: clean-slicot-src clean-slicot-tar clean-libslicot @@ -148,23 +119,10 @@ clean-slicot-all: clean-slicot-src clean-slicot-tar clean-libslicot # Octave # -tarballs/octave-${OCTAVE_VERSION}${OCTAVE_W32_BUILD}-w32.7z: - mkdir -p tarballs - wget $(WGET_OPTIONS) -O $@ https://ftp.gnu.org/gnu/octave/windows/$(notdir $@) - tarballs/octave-${OCTAVE_VERSION}${OCTAVE_W64_BUILD}-w64.7z: mkdir -p tarballs wget $(WGET_OPTIONS) -O $@ https://ftp.gnu.org/gnu/octave/windows/$(notdir $@) -octave32: tarballs/octave-${OCTAVE_VERSION}${OCTAVE_W32_BUILD}-w32.7z - rm -rf $@ - 7zr x $< > /dev/null - mv octave-${OCTAVE_VERSION}-w32 $@ - # Clean up MinGW stuff that interferes with our cross-compiler - cd $@/mingw32/lib && rm -f libadvapi32.a libkernel32.a libmingw32.a libmingwex.a libmoldname.a libmsvcrt.a libpthread.dll.a libshell32.a libuser32.a - cd $@/mingw32/include && rm -f $$(grep -l "This file is part of the mingw-w64 runtime package" *.h) - touch $@ - octave64: tarballs/octave-${OCTAVE_VERSION}${OCTAVE_W64_BUILD}-w64.7z rm -rf $@ 7zr x $< > /dev/null @@ -174,13 +132,11 @@ octave64: tarballs/octave-${OCTAVE_VERSION}${OCTAVE_W64_BUILD}-w64.7z cd $@/mingw64/include && rm -f $$(grep -l "This file is part of the mingw-w64 runtime package" *.h) touch $@ -octave: octave32 octave64 +octave: octave64 clean-octave: - rm -rf octave32 rm -rf octave64 rm -f tarballs/octave-${OCTAVE_VERSION}${OCTAVE_W64_BUILD}-w64.7z - rm -f tarballs/octave-${OCTAVE_VERSION}${OCTAVE_W32_BUILD}-w32.7z # # MATLAB @@ -192,24 +148,16 @@ tarballs/matlab%.tar.xz: gpg --output $@ --decrypt $@.gpg rm $@.gpg -matlab32: tarballs/matlab32-$(MATLAB32_VERSION).tar.xz - rm -rf $@ - mkdir -p $@ - tar xf $< --directory $@ --strip-components=1 - touch $@ - matlab64: tarballs/matlab64-$(MATLAB64_VERSION).tar.xz rm -rf $@ mkdir -p $@ - tar xf $< --directory $@ --strip-components=1 + tar xf $< --directory $@ touch $@ -matlab: matlab32 matlab64 +matlab: matlab64 clean-matlab: - rm -f tarballs/matlab32.tar.xz rm -f tarballs/matlab64.tar.xz - rm -rf matlab32 rm -rf matlab64 # @@ -218,45 +166,32 @@ clean-matlab: # - the embedded MinGW compiler (for use_dll) # -msys2: lib32-msys2 lib64-msys2 mingw32 mingw64 +msys2: lib64-msys2 mingw64 MSYS2_EXCLUDES := --exclude .MTREE --exclude .BUILDINFO --exclude .PKGINFO -lib32-msys2: tarballs/mingw-w64-i686-boost-$(MINGW32_BOOST_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-i686-gsl-$(MINGW32_GSL_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-i686-openblas-$(MINGW32_OPENBLAS_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-i686-matio-$(MINGW32_MATIO_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-i686-zlib-$(MINGW32_ZLIB_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-i686-hdf5-$(MINGW32_HDF5_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-i686-szip-$(MINGW32_SZIP_VERSION)-any.pkg.tar.xz - rm -rf $@ - mkdir $@ - for f in $^; do tar xf $$f --directory $@ --strip-components 1 $(MSYS2_EXCLUDES); done - touch $@ - -lib64-msys2: tarballs/mingw-w64-x86_64-boost-$(MINGW64_BOOST_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-x86_64-gsl-$(MINGW64_GSL_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-x86_64-openblas-$(MINGW64_OPENBLAS_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-x86_64-matio-$(MINGW64_MATIO_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-x86_64-zlib-$(MINGW64_ZLIB_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-x86_64-hdf5-$(MINGW64_HDF5_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-x86_64-szip-$(MINGW64_SZIP_VERSION)-any.pkg.tar.xz +lib64-msys2: tarballs/mingw-w64-x86_64-boost-$(MINGW64_BOOST_VERSION)-any.pkg.tar.zst tarballs/mingw-w64-x86_64-gsl-$(MINGW64_GSL_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-x86_64-openblas-$(MINGW64_OPENBLAS_VERSION)-any.pkg.tar.zst tarballs/mingw-w64-x86_64-matio-$(MINGW64_MATIO_VERSION)-any.pkg.tar.zst tarballs/mingw-w64-x86_64-zlib-$(MINGW64_ZLIB_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-x86_64-hdf5-$(MINGW64_HDF5_VERSION)-any.pkg.tar.zst tarballs/mingw-w64-x86_64-szip-$(MINGW64_SZIP_VERSION)-any.pkg.tar.xz rm -rf $@ mkdir $@ for f in $^; do tar xf $$f --directory $@ --strip-components 1 $(MSYS2_EXCLUDES); done touch $@ -mingw32: tarballs/mingw-w64-i686-gcc-$(MINGW32_GCC_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-i686-gcc-libs-$(MINGW32_GCC_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-i686-gmp-$(MINGW32_GMP_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-i686-binutils-$(MINGW32_BINUTILS_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-i686-headers-git-$(MINGW32_HEADERS_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-i686-crt-git-$(MINGW32_CRT_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-i686-winpthreads-git-$(MINGW32_WINPTHREADS_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-i686-libwinpthread-git-$(MINGW32_WINPTHREADS_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-i686-zlib-$(MINGW32_ZLIB_VERSION)-any.pkg.tar.xz +mingw64: tarballs/mingw-w64-x86_64-gcc-$(MINGW64_GCC_VERSION)-any.pkg.tar.zst tarballs/mingw-w64-x86_64-gcc-libs-$(MINGW64_GCC_VERSION)-any.pkg.tar.zst tarballs/mingw-w64-x86_64-gmp-$(MINGW64_GMP_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-x86_64-binutils-$(MINGW64_BINUTILS_VERSION)-any.pkg.tar.zst tarballs/mingw-w64-x86_64-headers-git-$(MINGW64_HEADERS_VERSION)-any.pkg.tar.zst tarballs/mingw-w64-x86_64-crt-git-$(MINGW64_CRT_VERSION)-any.pkg.tar.zst tarballs/mingw-w64-x86_64-winpthreads-git-$(MINGW64_WINPTHREADS_VERSION)-any.pkg.tar.zst tarballs/mingw-w64-x86_64-libwinpthread-git-$(MINGW64_WINPTHREADS_VERSION)-any.pkg.tar.zst tarballs/mingw-w64-x86_64-zlib-$(MINGW64_ZLIB_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-x86_64-zstd-$(MINGW64_ZSTD_VERSION)-any.pkg.tar.zst rm -rf $@ for f in $^; do tar xf $$f $(MSYS2_EXCLUDES); done touch $@ -mingw64: tarballs/mingw-w64-x86_64-gcc-$(MINGW64_GCC_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-x86_64-gcc-libs-$(MINGW64_GCC_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-x86_64-gmp-$(MINGW64_GMP_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-x86_64-binutils-$(MINGW64_BINUTILS_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-x86_64-headers-git-$(MINGW64_HEADERS_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-x86_64-crt-git-$(MINGW64_CRT_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-x86_64-winpthreads-git-$(MINGW64_WINPTHREADS_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-x86_64-libwinpthread-git-$(MINGW64_WINPTHREADS_VERSION)-any.pkg.tar.xz tarballs/mingw-w64-x86_64-zlib-$(MINGW64_ZLIB_VERSION)-any.pkg.tar.xz - rm -rf $@ - for f in $^; do tar xf $$f $(MSYS2_EXCLUDES); done - touch $@ - -tarballs/mingw-w64-i686-%-any.pkg.tar.xz: - wget $(WGET_OPTIONS) -O $@ http://repo.msys2.org/mingw/i686/$(notdir $@) - tarballs/mingw-w64-x86_64-%-any.pkg.tar.xz: wget $(WGET_OPTIONS) -O $@ http://repo.msys2.org/mingw/x86_64/$(notdir $@) +tarballs/mingw-w64-x86_64-%-any.pkg.tar.zst: + wget $(WGET_OPTIONS) -O $@ http://repo.msys2.org/mingw/x86_64/$(notdir $@) + clean-msys2: - rm -rf lib32-msys2 rm -rf lib64-msys2 - rm -rf mingw32 rm -rf mingw64 - rm -f tarballs/mingw-w64-i686-*.tar.xz rm -f tarballs/mingw-w64-x86_64-*.tar.xz + rm -f tarballs/mingw-w64-x86_64-*.tar.zst # # X13AS @@ -266,38 +201,26 @@ tarballs/x13assrc_V$(X13AS_VERSION).tar.gz: mkdir -p tarballs wget $(WGET_OPTIONS) -O $@ https://www.census.gov/ts/x13as/unix/x13assrc_V$(X13AS_VERSION).tar.gz -sources32/x13as-$(X13AS_VERSION): tarballs/x13assrc_V$(X13AS_VERSION).tar.gz - rm -rf sources32/x13as-* - mkdir -p $@ - tar xf $< --directory $@ - sources64/x13as-$(X13AS_VERSION): tarballs/x13assrc_V$(X13AS_VERSION).tar.gz rm -rf sources64/x13as-* mkdir -p $@ tar xf $< --directory $@ -lib32/x13as/x13as.exe: sources32/x13as-$(X13AS_VERSION) - make -C $< -f makefile.gf FC=i686-w64-mingw32-gfortran LINKER=i686-w64-mingw32-gfortran FFLAGS="-O2" PROGRAM=x13as.exe - i686-w64-mingw32-strip $</x13as.exe - mkdir -p $(dir $@) - cp $</x13as.exe $@ - lib64/x13as/x13as.exe: sources64/x13as-$(X13AS_VERSION) - make -C $< -f makefile.gf FC=x86_64-w64-mingw32-gfortran LINKER=x86_64-w64-mingw32-gfortran FFLAGS="-O2" PROGRAM=x13as.exe + make -C $< -f makefile.gf FC=x86_64-w64-mingw32-gfortran LINKER=x86_64-w64-mingw32-gfortran FFLAGS="-O2 -std=legacy" PROGRAM=x13as.exe x86_64-w64-mingw32-strip $</x13as.exe mkdir -p $(dir $@) cp $</x13as.exe $@ -build-x13as: lib32/x13as/x13as.exe lib64/x13as/x13as.exe +build-x13as: lib64/x13as/x13as.exe clean-x13as-tar: rm -f tarballs/x13assrc_V$(X13AS_VERSION).tar.gz clean-x13as-src: - rm -rf sources32/x13as-$(X13AS_VERSION) rm -rf sources64/x13as-$(X13AS_VERSION) clean-x13as-bin: - rm -rf lib32/x13as lib64/x13as + rm -rf lib64/x13as clean-x13as-all: clean-x13as-tar clean-x13as-src clean-x13as-bin diff --git a/windows/deps/mkoctfile32 b/windows/deps/mkoctfile32 deleted file mode 100755 index 5174c4664b40d8cf9626fc25aefc14c3710c80a4..0000000000000000000000000000000000000000 --- a/windows/deps/mkoctfile32 +++ /dev/null @@ -1,541 +0,0 @@ -#! /bin/sh - -## Attention à garder -static-libgcc et -static-libstdc++ dans LDFLAGS et -## DL_LDFLAGS lors des mises à jour de ce fichier -## Garder aussi BINDIR dans DEFAULT_LFLAGS (pour UMFPACK) - -# Exit immediately on any error. -set -e - -: ${SED="/bin/sed"} - -# Get path to the mkoctfile shell script -MKOCTFILE_PATH=$(dirname $(readlink -f "$0")) -OCTAVE_PREFIX="$MKOCTFILE_PATH/octave32/mingw32" - -# Get Octave version -current_octave_version=$(grep "^OCTAVE_VERSION" "$MKOCTFILE_PATH/versions.mk") -eval $(echo "$current_octave_version" | tr -d '[:space:]') - -DEFAULT_BINDIR="$OCTAVE_PREFIX/bin" -DEFAULT_INCLUDEDIR="$OCTAVE_PREFIX/include" -DEFAULT_LIBDIR="$OCTAVE_PREFIX/lib" -DEFAULT_OCTINCLUDEDIR="$OCTAVE_PREFIX/include/octave-${OCTAVE_VERSION}/octave" -DEFAULT_OCTLIBDIR="$OCTAVE_PREFIX/lib/octave/${OCTAVE_VERSION}" - -if [ -n "$OCTAVE_HOME" ]; then - DEFAULT_BINDIR="`echo $DEFAULT_BINDIR | $SED "s,^$OCTAVE_PREFIX,$OCTAVE_HOME,"`" - DEFAULT_INCLUDEDIR="`echo $DEFAULT_INCLUDEDIR | $SED "s,^$OCTAVE_PREFIX,$OCTAVE_HOME,"`" - DEFAULT_LIBDIR="`echo $DEFAULT_LIBDIR | $SED "s,^$OCTAVE_PREFIX,$OCTAVE_HOME,"`" - DEFAULT_OCTINCLUDEDIR="`echo $DEFAULT_OCTINCLUDEDIR | $SED "s,^$OCTAVE_PREFIX,$OCTAVE_HOME,"`" - DEFAULT_OCTLIBDIR="`echo $DEFAULT_OCTLIBDIR | $SED "s,^$OCTAVE_PREFIX,$OCTAVE_HOME,"`" -fi - -: ${BINDIR=$DEFAULT_BINDIR} -: ${INCLUDEDIR=$DEFAULT_INCLUDEDIR} -: ${LIBDIR=$DEFAULT_LIBDIR} -: ${OCTINCLUDEDIR=$DEFAULT_OCTINCLUDEDIR} -: ${OCTLIBDIR=$DEFAULT_OCTLIBDIR} - -DEFAULT_INCFLAGS="-I$OCTINCLUDEDIR/.. -I$OCTINCLUDEDIR" -if [ "$INCLUDEDIR" != /usr/include ]; then - DEFAULT_INCFLAGS="$DEFAULT_INCFLAGS -I$INCLUDEDIR" -fi - -DEFAULT_LFLAGS="-L$OCTLIBDIR -L$BINDIR" -if [ "$LIBDIR" != /usr/lib ]; then - DEFAULT_LFLAGS="$DEFAULT_LFLAGS -L$LIBDIR" -fi - -# Default values for these variables are filled in when Octave is -# compiled. - -: ${EXEEXT=""} - -: ${CPPFLAGS=""} -: ${INCFLAGS=$DEFAULT_INCFLAGS} -: ${F77="i686-w64-mingw32-gfortran"} -: ${FFLAGS="-O"} -: ${FPICFLAG=""} -: ${CC="i686-w64-mingw32-gcc"} -: ${CFLAGS="-g -O2"} -: ${CPICFLAG=""} -: ${CXX="i686-w64-mingw32-g++"} -: ${CXXFLAGS="-g -O2"} -: ${CXXPICFLAG=""} -: ${XTRA_CFLAGS=""} -: ${XTRA_CXXFLAGS=""} - -: ${DEPEND_FLAGS="-M"} -: ${DEPEND_EXTRA_SED_PATTERN=""} - -: ${DL_LD="i686-w64-mingw32-g++"} -: ${DL_LDFLAGS="-shared -Wl,--export-all-symbols -Wl,--enable-auto-import -Wl,--enable-runtime-pseudo-reloc -static-libgcc -static-libstdc++"} - -: ${RLD_FLAG="-Wl,-rpath -Wl,$OCTAVE_PREFIX/lib/octave/${OCTAVE_VERSION}"} -: ${RDYNAMIC_FLAG="-rdynamic"} -: ${LIBOCTAVE=-loctave} -: ${LIBOCTINTERP=-loctinterp} -: ${READLINE_LIBS=-lreadline} -: ${LAPACK_LIBS="-llapack"} -: ${BLAS_LIBS="-lblas -lxerbla"} -: ${FFTW3_LDFLAGS=""} -: ${FFTW3_LIBS="-lfftw3"} -: ${FFTW3F_LDFLAGS=""} -: ${FFTW3F_LIBS="-lfftw3f"} -: ${LIBS="-lm -lgfortran -llapack -lgdi32 -lws2_32 -luser32 -lkernel32"} -: ${FLIBS="-lm -lgfortran -llapack -lmingw32 -lmoldname -lmingwex -lmsvcrt -luser32 -lkernel32 -ladvapi32 -lshell32"} -: ${LD_CXX="i686-w64-mingw32-g++"} -: ${LDFLAGS="-Wl,--enable-auto-import -Wl,--enable-runtime-pseudo-reloc-v2 -Wl,--allow-multiple-definition -static-libgcc -static-libstdc++"} -: ${LD_STATIC_FLAG=""} -: ${LFLAGS=$DEFAULT_LFLAGS} -: ${F77_INTEGER_8_FLAG=""} - -: ${ALL_FFLAGS="$FFLAGS $F77_INTEGER_8_FLAG"} - -: ${ALL_CFLAGS="$INCFLAGS $XTRA_CFLAGS $CFLAGS"} - -: ${ALL_CXXFLAGS="$INCFLAGS $XTRA_CXXFLAGS $CXXFLAGS"} - -: ${ALL_LDFLAGS="$LD_STATIC_FLAG $CPICFLAG $LDFLAGS"} - -: ${OCTAVE_LIBS="$LIBOCTINTERP $LIBOCTAVE $SPECIAL_MATH_LIB"} - -# Local variables. - -FFTW_LIBS="$FFTW3_LDFLAGS $FFTW3_LIBS $FFTW3F_LDFLAGS $FFTW3F_LIBS" - -usage_msg="usage: mkoctfile [options] file ..." - -version_msg="mkoctfile, version $OCTAVE_VERSION" - -cfiles= -ccfiles= -f77files= -objfiles= -libfiles= -octfiles= -octfile= -outputfile= -incflags= -defs= -ldflags= -dbg=: -pass_on_options= -strip=false -no_oct_file_strip_on_this_platform=false -link=true -link_stand_alone=false -output_ext=".oct" -depend=false -compile=true - -if [ $# -eq 0 ]; then - echo $usage_msg 1>&2 - exit 1 -fi - -if [ $# -eq 1 ]; then - case "$1" in - -v | -version | --version) - echo $version_msg 1>&2 - exit 0 - ;; - esac -fi - -while [ $# -gt 0 ]; do - file= - case "$1" in - *.c) - file=$1 - cfiles="$cfiles $file" - ;; - *.cc | *.C | *.cpp) - file=$1 - ccfiles="$ccfiles $file" - ;; - *.f | *.F | *.f90 | *.F90) - file=$1 - f77files="$f77files $file" - ;; - *.o) - file=$1 - objfiles="$objfiles $file" - ;; - *.a) - file=$1 - libfiles="$libfiles $file" - ;; - -d | -debug | --debug | -v | -verbose | --verbose) - dbg=echo - ;; - -h | -\? | -help | --help) - echo $usage_msg 1>&2 - cat << EOF - -Options: - - -h, -?, --help Print this message. - - -IDIR Add -IDIR to compile commands. - - -idirafter DIR Add -idirafter DIR to compile commands. - - -DDEF Add -DDEF to compile commands. - - -lLIB Add -lLIB to link command. - - -LDIR Add -LDIR to link command. - - -RDIR Add -RDIR to link command. - - -g Enable debugging option for all compilers. - - -pthread Add -pthread to link command. - - -W... Pass flags to the compiler such as -Wa,OPTION. - - -Wl,... Pass flags to the linker such as -Wl,-rpath=... - - -M, --depend Generate dependency files (.d) for C and C++ - source files. - - -c, --compile Compile, but do not link. - - --link-stand-alone Link a stand-alone executable file. - - -s, --strip Strip output file. - - --mex Create a MEX file. - Set the default output extension to ".mex". - - -o FILE, --output FILE Output file name. Default extension is .oct - (or .mex if --mex is specified) unless linking - a stand-alone executable. - - -p VAR, --print VAR Print configuration variable VAR. Recognized - variables are: - - ALL_CFLAGS FFTW3F_LDFLAGS - ALL_CXXFLAGS FFTW3F_LIBS - ALL_FFLAGS FLIBS - ALL_LDFLAGS FPICFLAG - BLAS_LIBS INCFLAGS - CC LAPACK_LIBS - CFLAGS LDFLAGS - CPICFLAG LD_CXX - CPPFLAGS LD_STATIC_FLAG - CXX LFLAGS - CXXFLAGS LIBCRUFT - CXXPICFLAG LIBOCTAVE - DEPEND_EXTRA_SED_PATTERN LIBOCTINTERP - DEPEND_FLAGS LIBS - DL_LD OCTAVE_LIBS - DL_LDFLAGS RDYNAMIC_FLAG - EXEEXT READLINE_LIBS - F77 RLD_FLAG - F77_INTEGER_8_FLAG SED - FFLAGS XTRA_CFLAGS - FFTW3_LDFLAGS XTRA_CXXFLAGS - FFTW3_LIBS - - -v, --verbose Echo commands as they are executed. - - FILE Compile or link FILE. Recognized file types are: - - .c C source - .cc C++ source - .C C++ source - .cpp C++ source - .f Fortran source (fixed form) - .F Fortran source (fixed form) - .f90 Fortran source (free form) - .F90 Fortran source (free form) - .o object file - .a library file - -EOF - exit 0 - ;; - -I*) - incflags="$incflags $1" - ;; - -idirafter) - shift - if [ $# -gt 0 ]; then - incflags="$incflags -idirafter $1" - else - echo "mkoctfile: include directory name missing" 1>&2 - fi - ;; - -D*) - defs="$defs $1" - ;; - -[lLR]* | -Wl,*) - ldflags="$ldflags $1" - ;; - -pthread) - ldflags="$ldflags $1" - ;; - -M | -depend | --depend) - depend=true - compile=false - ;; - -o | -ouput | --output) - shift - if [ $# -gt 0 ]; then - outputfile="$1" - else - echo "mkoctfile: output file name missing" 1>&2 - fi - ;; - -p | -print | --print) - shift - if [ $# -gt 0 ]; then - eval echo \${$1} - exit 0 - else - echo "mkoctfile: --print requires argument" 1>&2 - exit 1 - fi - ;; - -s | -strip | --strip) - if $no_oct_file_strip_on_this_platform; then - echo "mkoctfile: stripping disabled on this platform" 1>&2 - else - strip=true - fi - ;; - -c | -compile | --compile) - link=false - ;; - -g) - ALL_CFLAGS="$ALL_CFLAGS -g" - ALL_CXXFLAGS="$ALL_CXXFLAGS -g" - ALL_FFLAGS="$ALL_FFLAGS -g" - ;; - -link-stand-alone | --link-stand-alone) - link_stand_alone=true - ;; - -mex | --mex) - incflags="$incflags -I." - output_ext=".mex" - ;; - -W*) - pass_on_options="$pass_on_options $1" - ;; - *) - echo "mkoctfile: unrecognized argument $1" 1>&2 - exit 1 - ;; - esac - if [ -n "$file" ]; then - if [ -z "$octfile" ]; then - octfile="$file" - fi - fi - shift -done - -if $link_stand_alone; then - if [ -n "$outputfile" ]; then - output_option="-o $outputfile" - fi -else - if [ -n "$outputfile" ]; then - octfile="$outputfile" - case "$octfile" in - *$output_ext) - ;; - *) - octfile="$octfile$output_ext" - ;; - esac - else - octfile=`basename $octfile` - octfile=`echo $octfile | $SED 's,\.[^.]*$,,'`$output_ext - fi -fi - -# Generate dependency files for C and C++ files. - -if $depend; then - if [ -n "$cfiles" ]; then - for f in $cfiles; do - b=`echo $f | $SED 's,\.c$,,'` - d=$b.d - cmd="rm -f $d" - $dbg $cmd - eval $cmd - cmd="$CC $DEPEND_FLAGS $CPPFLAGS $ALL_CFLAGS $incflags $def $f | $SED $DEPEND_EXTRA_SED_PATTERN -e 's,^[^:]*/\(.*\.o\):,\1:,' -e 's,$b\.o,pic/& & $d,g' > $d-t && mv $d-t $d" - $dbg $cmd - eval $cmd - done - fi - - if [ -n "$ccfiles" ]; then - for f in $ccfiles; do - case $f in - *.cc) - b=`echo $f | $SED 's,\.cc$,,'` - ;; - *.C) - b=`echo $f | $SED 's,\.C$,,'` - ;; - *.cpp) - b=`echo $f | $SED 's,\.cpp$,,'` - ;; - esac - d=$b.d - cmd="rm -f $d" - $dbg $cmd - eval $cmd - cmd="$CXX $DEPEND_FLAGS $CPPFLAGS $ALL_CXXFLAGS $incflags $defs $f | $SED $DEPEND_EXTRA_SED_PATTERN -e 's,^[^:]*/\(.*\.o\):,\1:,' -e 's,$b\.o,pic/& & $d,g' > $d-t && mv $d-t $d" - $dbg $cmd - eval $cmd - done - fi - # If generating dependencies, that's all we do. - exit 0 -fi - -# Compile Fortran, C, and C++ files. Add the name of each object file -# that is produced to the overall list of object files. - -if [ -n "$f77files" ]; then - for f in $f77files; do - case $f in - *.f) - b=`echo $f | $SED 's,.*/,,; s,\.f$,,'` - ;; - *.F) - b=`echo $f | $SED 's,.*/,,; s,\.F$,,'` - ;; - *.f90) - b=`echo $f | $SED 's,.*/,,; s,\.f90$,,'` - ;; - *.F90) - b=`echo $f | $SED 's,.*/,,; s,\.F90$,,'` - ;; - esac - if [ -n "$F77" ]; then - if [ -n "$outputfile" ]; then - if $link; then - o=$b.o - else - o=$outputfile - fi - else - o=$b.o - fi - objfiles="$objfiles $o" - cmd="$F77 -c $FPICFLAG $ALL_FFLAGS $incflags $defs $pass_on_options $f -o $o" - $dbg $cmd - eval $cmd - else - echo "mkoctfile: no way to compile Fortran file $f" 1>&2 - fi - done -fi - -if [ -n "$cfiles" ]; then - for f in $cfiles; do - if [ -n "$CC" ]; then - b=`echo $f | $SED 's,.*/,,; s,\.c$,,'` - if [ -n "$outputfile" ]; then - if $link; then - o=$b.o - else - o=$outputfile - fi - else - o=$b.o - fi - objfiles="$objfiles $o" - cmd="$CC -c $CPPFLAGS $CPICFLAG $ALL_CFLAGS $pass_on_options $incflags $defs $f -o $o" - $dbg $cmd - eval $cmd - else - echo "mkoctfile: no way to compile C++ file $f" 1>&2 - fi - done -fi - -if [ -n "$ccfiles" ]; then - for f in $ccfiles; do - if [ -n "$CXX" ]; then - case $f in - *.cc) - b=`echo $f | $SED 's,.*/,,; s,\.cc$,,'` - ;; - *.C) - b=`echo $f | $SED 's,.*/,,; s,\.C$,,'` - ;; - *.cpp) - b=`echo $f | $SED 's,.*/,,; s,\.cpp$,,'` - ;; - esac - if [ -n "$outputfile" ]; then - if $link; then - o=$b.o - else - o=$outputfile - fi - else - o=$b.o - fi - objfiles="$objfiles $o" - cmd="$CXX -c $CPPFLAGS $CXXPICFLAG $ALL_CXXFLAGS $pass_on_options $incflags $defs $f -o $o" - $dbg $cmd - eval $cmd - else - echo "mkoctfile: no way to compile C++ file $f" 1>&2 - fi - done -fi - -## Uncomment the following group of lines if you get `Text file busy' -## errors from ld. This may happen if the .oct file is currently -## running while you are trying to recompile it. We try moving first, -## since on some systems (HP-UX, maybe others) it is possible to -## rename running programs but not remove them. - -## if [ -f "$octfile" ]; then -## cmd="mv $octfile $octfile.bak" -## $dbg $cmd -## eval $cmd -## cmd="rm -f $octfile.bak" -## $dbg $cmd -## eval $cmd -## fi - -# Link all the object files. - -if $link && [ -n "$objfiles" ]; then - if $link_stand_alone; then - if [ -n "$LD_CXX" ]; then - cmd="$LD_CXX $CPPFLAGS $ALL_CXXFLAGS $RDYNAMIC_FLAG $ALL_LDFLAGS $pass_on_options $output_option $objfiles $libfiles $ldflags $LFLAGS $RLD_FLAG $OCTAVE_LIBS $LAPACK_LIBS $BLAS_LIBS $FFTW_LIBS $READLINE_LIBS $LIBS $FLIBS" - $dbg $cmd - eval $cmd - else - echo "mkoctfile: no way to link stand-alone executable file" 1>&2 - exit 1 - fi - else - LINK_DEPS="$LFLAGS $OCTAVE_LIBS $LDFLAGS $LAPACK_LIBS $BLAS_LIBS $FFTW_LIBS $LIBS $FLIBS" - cmd="$DL_LD $DL_LDFLAGS $pass_on_options -o $octfile $objfiles $libfiles $ldflags $LINK_DEPS" - $dbg $cmd - eval $cmd - fi - -# Maybe strip it. - - if $strip; then - cmd="strip $octfile" - $dbg $cmd - eval $cmd - fi -fi - -exit 0 diff --git a/windows/deps/versions.mk b/windows/deps/versions.mk index 811ee558fdac8c678fce60e0ede63596607ba3c0..b2775ea450f1feecc8e5ad437cd335a4fe16f71b 100644 --- a/windows/deps/versions.mk +++ b/windows/deps/versions.mk @@ -2,11 +2,9 @@ SLICOT_VERSION = 5.0+20101122 X13AS_VERSION = 1.1_B39 OCTAVE_VERSION = 5.2.0 -OCTAVE_W32_BUILD = _1 OCTAVE_W64_BUILD = _1 -MATLAB32_VERSION = 20181204 -MATLAB64_VERSION = 20181204 +MATLAB64_VERSION = 20200407 ### MSYS2 packages @@ -19,59 +17,49 @@ MATLAB64_VERSION = 20181204 ## Build dependencies -# pacman -Ss .*-boost$ -MINGW32_BOOST_VERSION = 1.72.0-1 -MINGW64_BOOST_VERSION = 1.72.0-1 +# pacman -Ss mingw-w64-x86_64-boost +MINGW64_BOOST_VERSION = 1.73.0-4 -# pacman -Ss .*-gsl$ -MINGW32_GSL_VERSION = 2.6-1 +# pacman -Ss mingw-w64-x86_64-gsl MINGW64_GSL_VERSION = 2.6-1 -# pacman -Ss .*-openblas$ -MINGW32_OPENBLAS_VERSION = 0.3.9-1 -MINGW64_OPENBLAS_VERSION = 0.3.9-1 +# pacman -Ss mingw-w64-x86_64-openblas +MINGW64_OPENBLAS_VERSION = 0.3.10-2 -# pacman -Ss .*-matio$ -MINGW32_MATIO_VERSION = 1.5.17-1 -MINGW64_MATIO_VERSION = 1.5.17-1 +# pacman -Ss mingw-w64-x86_64-matio +MINGW64_MATIO_VERSION = 1.5.17-2 # Dependency of matio (and of the MinGW compiler) -# pacman -Ss .*-zlib$ -MINGW32_ZLIB_VERSION = 1.2.11-7 +# pacman -Ss mingw-w64-x86_64-zlib MINGW64_ZLIB_VERSION = 1.2.11-7 # Dependency of matio -# pacman -Ss .*-hdf5$ -MINGW32_HDF5_VERSION = 1.10.5-1 -MINGW64_HDF5_VERSION = 1.10.5-1 +# pacman -Ss mingw-w64-x86_64-hdf5 +MINGW64_HDF5_VERSION = 1.12.0-2 # Dependency of HDF5 -# pacman -Ss .*-szip$ -MINGW32_SZIP_VERSION = 2.1.1-2 +# pacman -Ss mingw-w64-x86_64-szip MINGW64_SZIP_VERSION = 2.1.1-2 ## MinGW packages for the embedded compiler -# pacman -Ss mingw-w64-.*-gcc$ -MINGW32_GCC_VERSION = 9.3.0-1 -MINGW64_GCC_VERSION = 9.3.0-1 +# pacman -Ss mingw-w64-x86_64-gcc$ +MINGW64_GCC_VERSION = 10.2.0-1 -# pacman -Ss mingw-w64-.*-gmp$ -MINGW32_GMP_VERSION = 6.2.0-1 +# pacman -Ss mingw-w64-x86_64-gmp MINGW64_GMP_VERSION = 6.2.0-1 -# pacman -Ss mingw-w64-.*-binutils -MINGW32_BINUTILS_VERSION = 2.34-1 -MINGW64_BINUTILS_VERSION = 2.34-1 +# pacman -Ss mingw-w64-x86_64-binutils +MINGW64_BINUTILS_VERSION = 2.35-1 -# pacman -Ss mingw-w64-.*-headers-git -MINGW32_HEADERS_VERSION = 8.0.0.5685.ef310786-1 -MINGW64_HEADERS_VERSION = 8.0.0.5685.ef310786-1 +# pacman -Ss mingw-w64-x86_64-headers-git +MINGW64_HEADERS_VERSION = 8.0.0.5966.f5da805f-1 -# pacman -Ss mingw-w64-.*-crt-git -MINGW32_CRT_VERSION = 8.0.0.5685.ef310786-1 -MINGW64_CRT_VERSION = 8.0.0.5685.ef310786-1 +# pacman -Ss mingw-w64-x86_64-crt-git +MINGW64_CRT_VERSION = 8.0.0.5966.f5da805f-1 -# pacman -Ss mingw-w64-.*-winpthreads-git -MINGW32_WINPTHREADS_VERSION = 8.0.0.5680.0df6b89f-1 -MINGW64_WINPTHREADS_VERSION = 8.0.0.5680.0df6b89f-1 +# pacman -Ss mingw-w64-x86_64-winpthreads-git +MINGW64_WINPTHREADS_VERSION = 8.0.0.5906.c9a21571-1 + +# pacman -Ss mingw-w64-x86_64-zstd +MINGW64_ZSTD_VERSION = 1.4.5-1 diff --git a/windows/dynare.nsi b/windows/dynare.nsi index 04a4278b64e58f8487c566722f05a88910263484..86d1f509421d14afaf6351ba3944d78f1d58d2d8 100644 --- a/windows/dynare.nsi +++ b/windows/dynare.nsi @@ -16,7 +16,6 @@ InstallDir "c:\dynare\${VERSION}" !define MUI_WELCOMEPAGE_TEXT "This wizard will guide you through the installation of Dynare ${VERSION}.$\n$\nDynare is distributed under the GNU General Public License (GPL) version 3.$\n$\nIf you accept the license, click Next button to continue the installation." !insertmacro MUI_PAGE_WELCOME !define MUI_COMPONENTSPAGE_NODESC -!define MUI_COMPONENTSPAGE_TEXT_TOP "Choose the components you want to install.$\nIf you know whether your version of MATLAB or Octave is 64-bit or 32-bit, you can uncheck the component that you don’t need in order to save disk space." !insertmacro MUI_PAGE_COMPONENTS !insertmacro MUI_PAGE_DIRECTORY !insertmacro MUI_PAGE_INSTFILES @@ -55,15 +54,9 @@ Section "Dynare core (preprocessor and M-files)" SetOutPath $INSTDIR\matlab File /r ..\matlab\*.m - SetOutPath $INSTDIR\matlab\preprocessor32 - File ..\matlab\preprocessor32\dynare_m.exe - SetOutPath $INSTDIR\matlab\preprocessor64 File ..\matlab\preprocessor64\dynare_m.exe - SetOutPath $INSTDIR\matlab\modules\dseries\externals\x13\windows\32 - File deps\lib32\x13as\x13as.exe - SetOutPath $INSTDIR\matlab\modules\dseries\externals\x13\windows\64 File deps\lib64\x13as\x13as.exe @@ -89,11 +82,9 @@ Section "Dynare core (preprocessor and M-files)" SectionEnd -SectionGroup "Dynare support for 64-bit MATLAB and Octave" - -Section "MEX files for MATLAB 64-bit, version 7.9 to 9.3 (R2009b to R2017b)" - SetOutPath $INSTDIR\mex\matlab\win64-7.9-9.3 - File ..\mex\matlab\win64-7.9-9.3\*.mexw64 +Section "MEX files for MATLAB 64-bit, version 8.3 to 9.3 (R2014a to R2017b)" + SetOutPath $INSTDIR\mex\matlab\win64-8.3-9.3 + File ..\mex\matlab\win64-8.3-9.3\*.mexw64 SectionEnd Section "MEX files for MATLAB 64-bit, version 9.4 to 9.8 (R2018a to R2020a)" @@ -101,39 +92,16 @@ Section "MEX files for MATLAB 64-bit, version 9.4 to 9.8 (R2018a to R2020a)" File ..\mex\matlab\win64-9.4-9.8\*.mexw64 SectionEnd -Section "MEX files for Octave 5.2.0 (64-bit)" - SetOutPath $INSTDIR\mex\octave\win64 - File ..\mex\octave\win64\* -SectionEnd - Section "MinGW compiler for MATLAB 64-bit" SetOutPath $INSTDIR\mingw64 File /r deps\mingw64\* SectionEnd -SectionGroupEnd - - -SectionGroup "Dynare support for 32-bit MATLAB and Octave" - -Section "MEX files for MATLAB 32-bit, version 7.9 to 8.6 (R2009b to R2015b)" - SetOutPath $INSTDIR\mex\matlab\win32-7.9-8.6 - File ..\mex\matlab\win32-7.9-8.6\*.mexw32 -SectionEnd - -Section "MEX files for Octave 5.2.0 (32-bit)" - SetOutPath $INSTDIR\mex\octave\win32 - File ..\mex\octave\win32\* -SectionEnd - -Section "MinGW compiler for MATLAB 32-bit" - SetOutPath $INSTDIR\mingw32 - File /r deps\mingw32\* +Section "MEX files for Octave 5.2.0 (64-bit)" + SetOutPath $INSTDIR\mex\octave\win64 + File ..\mex\octave\win64\* SectionEnd -SectionGroupEnd - - Section "Documentation and examples" SetOutPath $INSTDIR\doc File ..\doc\manual\build\latex\dynare-manual.pdf ..\doc\guide.pdf ..\doc\bvar-a-la-sims.pdf ..\doc\dr.pdf ..\preprocessor\doc\macroprocessor\macroprocessor.pdf ..\preprocessor\doc\preprocessor\preprocessor.pdf ..\doc\parallel\parallel.pdf ..\doc\gsa\gsa.pdf ..\doc\dseries-and-reporting\dseriesReporting.pdf @@ -152,14 +120,8 @@ SectionEnd Section /o "Dynare++ (standalone executable)" - SetOutPath $INSTDIR\dynare++\32-bit - File ..\dynare++\32-bit\dynare++.exe - - SetOutPath $INSTDIR\dynare++\64-bit - File ..\dynare++\64-bit\dynare++.exe - SetOutPath $INSTDIR\dynare++ - File ..\dynare++\dynare_simul\dynare_simul.m + File ..\dynare++\src\dynare++.exe ..\dynare++\dynare_simul\dynare_simul.m SetOutPath $INSTDIR\doc\dynare++ File ..\dynare++\doc\*.pdf @@ -185,7 +147,6 @@ Section "Uninstall" Rmdir /r $INSTDIR\doc Rmdir /r $INSTDIR\examples Rmdir /r $INSTDIR\scripts - Rmdir /r $INSTDIR\mingw32 Rmdir /r $INSTDIR\mingw64 # We don't force deletion of installation directory (with /r), to avoid deleting important files Rmdir $INSTDIR diff --git a/windows/install-packages.sh b/windows/install-packages.sh index 5b53c511ab40c6832b0a70e815fa0d67e664fd51..db70175fcaa58b231ab3af55c52dc08b4b6a15e2 100755 --- a/windows/install-packages.sh +++ b/windows/install-packages.sh @@ -3,7 +3,7 @@ # On a Debian system, install the packages needed for Windows # cross-compilation, and also setup the cross-compiler alternatives. -# Copyright © 2017-2019 Dynare Team +# Copyright © 2017-2020 Dynare Team # # This file is part of Dynare. # @@ -22,8 +22,8 @@ [[ $(id -u) == 0 ]] || { echo "You must be root" >&2; exit 1; } -PACKAGES=(make xz-utils p7zip bzip2 zip patch wget autoconf automake libtool - mingw-w64 gfortran-mingw-w64 parallel flex bison texlive +PACKAGES=(make xz-utils p7zip bzip2 zip zstd patch wget autoconf automake + libtool mingw-w64 gfortran-mingw-w64 parallel flex bison texlive texlive-publishers texlive-latex-extra texlive-science texlive-fonts-extra lmodern python3-sphinx latexmk nsis) @@ -34,6 +34,3 @@ apt install "${PACKAGES[@]}" update-alternatives --set x86_64-w64-mingw32-gfortran /usr/bin/x86_64-w64-mingw32-gfortran-posix update-alternatives --set x86_64-w64-mingw32-gcc /usr/bin/x86_64-w64-mingw32-gcc-posix update-alternatives --set x86_64-w64-mingw32-g++ /usr/bin/x86_64-w64-mingw32-g++-posix -update-alternatives --set i686-w64-mingw32-gfortran /usr/bin/i686-w64-mingw32-gfortran-posix -update-alternatives --set i686-w64-mingw32-gcc /usr/bin/i686-w64-mingw32-gcc-posix -update-alternatives --set i686-w64-mingw32-g++ /usr/bin/i686-w64-mingw32-g++-posix