2019-06-19 14:34:30 +02:00
|
|
|
|
/*
|
|
|
|
|
* Copyright © 2005 Ondra Kamenik
|
|
|
|
|
* Copyright © 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/>.
|
|
|
|
|
*/
|
2019-01-04 16:29:57 +01:00
|
|
|
|
|
|
|
|
|
// Global check
|
|
|
|
|
|
|
|
|
|
/* The purpose of this file is to provide classes for checking error of
|
2019-06-11 16:45:09 +02:00
|
|
|
|
approximation. If yₜ=g(y*ₜ₋₁,u) is an approximate solution, then we check
|
|
|
|
|
for the error of residuals of the system equations. Let
|
|
|
|
|
F(y*,u,u′)=f(g**(g*(y*,u′),u),g(y*,u),y*,u), then we calculate the integral:
|
|
|
|
|
|
|
|
|
|
𝔼ₜ[F(y*,u,u′)]
|
|
|
|
|
|
|
|
|
|
which we want to be zero for all y* and u.
|
|
|
|
|
|
|
|
|
|
There are a few possibilities how and where the integral is evaluated.
|
|
|
|
|
Currently we offer the following ones:
|
|
|
|
|
|
|
|
|
|
— Along shocks. The y* is set to the steady state, and u is set to zero but
|
|
|
|
|
one element is going from minus through plus shocks in few steps. The user
|
|
|
|
|
gives the scaling factor, for instance the interval [−3σ,3σ] (where σ is
|
|
|
|
|
one standard error of the shock), and a number of steps. This is repeated
|
|
|
|
|
for each shock (element of the u vector).
|
|
|
|
|
|
|
|
|
|
— Along simulation. Some random simulation is run, and for each realization
|
|
|
|
|
of y* and u along the path we evaluate the residual.
|
|
|
|
|
|
|
|
|
|
— On ellipse. Let V=AAᵀ be a covariance matrix of the predetermined
|
|
|
|
|
variables y* based on linear approximation, then we calculate integral for
|
|
|
|
|
points on the ellipse { Ax | ‖x‖₂=1 }. The points are selected by means of
|
|
|
|
|
low discrepancy method and polar transformation. The shock u are zeros.
|
|
|
|
|
|
|
|
|
|
— Unconditional distribution.
|
|
|
|
|
*/
|
2019-01-04 16:29:57 +01:00
|
|
|
|
|
|
|
|
|
#ifndef GLOBAL_CHECK_H
|
|
|
|
|
#define GLOBAL_CHECK_H
|
|
|
|
|
|
|
|
|
|
#include <matio.h>
|
|
|
|
|
|
2019-01-14 16:09:49 +01:00
|
|
|
|
#include <memory>
|
|
|
|
|
|
2019-01-04 17:27:23 +01:00
|
|
|
|
#include "vector_function.hh"
|
|
|
|
|
#include "quadrature.hh"
|
2019-01-04 16:29:57 +01:00
|
|
|
|
|
|
|
|
|
#include "dynamic_model.hh"
|
|
|
|
|
#include "journal.hh"
|
|
|
|
|
#include "approximation.hh"
|
|
|
|
|
|
2019-06-11 16:45:09 +02:00
|
|
|
|
/* This is a class for implementing the VectorFunction interface evaluating the
|
|
|
|
|
residual of equations, this is F(y*,u,u′)=f(g**(g*(y*,u),u′),y*,u) is
|
|
|
|
|
written as a function of u′.
|
2019-01-04 16:29:57 +01:00
|
|
|
|
|
2019-06-11 16:45:09 +02:00
|
|
|
|
When the object is constructed, one has to specify (y*,u), this is done by
|
|
|
|
|
the setYU() method. The object has basically two states. One is after
|
|
|
|
|
construction and before the call to setYU(). The second is after the call to
|
|
|
|
|
setYU(). We distinguish between the two states, an object in the second
|
|
|
|
|
state contains ‘yplus’, ‘ystar’, ‘u’, and ‘hss’.
|
2019-01-04 16:29:57 +01:00
|
|
|
|
|
2019-06-11 16:45:09 +02:00
|
|
|
|
The vector ‘yplus’ is g*(y*,u). ‘ystar’ is y*, and polynomial ‘hss’ is
|
|
|
|
|
partially evaluated g**(yplus, u).
|
2019-01-04 16:29:57 +01:00
|
|
|
|
|
2019-06-11 16:45:09 +02:00
|
|
|
|
The pointer to DynamicModel is important, since the DynamicModel evaluates
|
|
|
|
|
the function f. When copying the object, we have to make also a copy of
|
|
|
|
|
DynamicModel. */
|
2019-01-04 16:29:57 +01:00
|
|
|
|
|
|
|
|
|
class ResidFunction : public VectorFunction
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
const Approximation ≈
|
2019-03-06 18:40:19 +01:00
|
|
|
|
std::unique_ptr<DynamicModel> model;
|
|
|
|
|
std::unique_ptr<Vector> yplus, ystar, u;
|
|
|
|
|
std::unique_ptr<FTensorPolynomial> hss;
|
2019-01-04 16:29:57 +01:00
|
|
|
|
public:
|
|
|
|
|
ResidFunction(const Approximation &app);
|
|
|
|
|
ResidFunction(const ResidFunction &rf);
|
2019-12-20 14:36:20 +01:00
|
|
|
|
|
2019-01-14 16:09:49 +01:00
|
|
|
|
std::unique_ptr<VectorFunction>
|
2019-01-09 16:26:42 +01:00
|
|
|
|
clone() const override
|
2019-01-04 16:29:57 +01:00
|
|
|
|
{
|
2019-01-14 16:09:49 +01:00
|
|
|
|
return std::make_unique<ResidFunction>(*this);
|
2019-01-04 16:29:57 +01:00
|
|
|
|
}
|
2019-01-09 16:26:42 +01:00
|
|
|
|
void eval(const Vector &point, const ParameterSignal &sig, Vector &out) override;
|
Dynare++ / sylvester equation solver: refactor Vector and ConstVector classes
- these classes now encapsulate a std::shared_ptr<{const, }double>, so that
they do not perform memory management, and several {Const,}Vector instances
can transparently share the same underlying data
- make converting constructor from ConstVector to Vector explicit, since that
entails memory allocation (but the reverse conversion is almost costless, so
keep it implicit); do the same for GeneralMatrix/ConstGeneralMatrix,
TwoDMatrix/ConstTwoDMatrix
- remove the constructors that were extracting a row/column from a matrix, and
replace them by getRow() and getCol() methods on {Const,}GeneralMatrix
- rename and change the API of the complex version Vector::add(), so that it is
explicit that it deals with complex numbers
- add constructors that take a MATLAB mxArray
2019-01-22 16:07:44 +01:00
|
|
|
|
void setYU(const ConstVector &ys, const ConstVector &xx);
|
2019-01-04 16:29:57 +01:00
|
|
|
|
};
|
|
|
|
|
|
2019-06-11 16:45:09 +02:00
|
|
|
|
/* This is a ResidFunction wrapped with GaussConverterFunction. */
|
2019-01-04 16:29:57 +01:00
|
|
|
|
|
|
|
|
|
class GResidFunction : public GaussConverterFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
GResidFunction(const Approximation &app)
|
2019-01-14 16:09:49 +01:00
|
|
|
|
: GaussConverterFunction(std::make_unique<ResidFunction>(app), app.getModel().getVcov())
|
2019-01-04 16:29:57 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
2019-01-14 16:09:49 +01:00
|
|
|
|
std::unique_ptr<VectorFunction>
|
2019-01-09 16:26:42 +01:00
|
|
|
|
clone() const override
|
2019-01-04 16:29:57 +01:00
|
|
|
|
{
|
2019-01-14 16:09:49 +01:00
|
|
|
|
return std::make_unique<GResidFunction>(*this);
|
2019-01-04 16:29:57 +01:00
|
|
|
|
}
|
|
|
|
|
void
|
Dynare++ / sylvester equation solver: refactor Vector and ConstVector classes
- these classes now encapsulate a std::shared_ptr<{const, }double>, so that
they do not perform memory management, and several {Const,}Vector instances
can transparently share the same underlying data
- make converting constructor from ConstVector to Vector explicit, since that
entails memory allocation (but the reverse conversion is almost costless, so
keep it implicit); do the same for GeneralMatrix/ConstGeneralMatrix,
TwoDMatrix/ConstTwoDMatrix
- remove the constructors that were extracting a row/column from a matrix, and
replace them by getRow() and getCol() methods on {Const,}GeneralMatrix
- rename and change the API of the complex version Vector::add(), so that it is
explicit that it deals with complex numbers
- add constructors that take a MATLAB mxArray
2019-01-22 16:07:44 +01:00
|
|
|
|
setYU(const ConstVector &ys, const ConstVector &xx)
|
2019-01-04 16:29:57 +01:00
|
|
|
|
{
|
2019-03-06 18:40:19 +01:00
|
|
|
|
dynamic_cast<ResidFunction *>(func)->setYU(ys, xx);
|
2019-01-04 16:29:57 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-06-11 16:45:09 +02:00
|
|
|
|
/* This is a class encapsulating checking algorithms. Its core routine is
|
|
|
|
|
check(), which calculates integral 𝔼[F(y*,u,u′) | y*,u] for given
|
|
|
|
|
realizations of y* and u. The both are given in matrices. The methods
|
|
|
|
|
checking along shocks, on ellipse and anlong a simulation path, just fill
|
|
|
|
|
the matrices and call the core check().
|
2019-01-04 16:29:57 +01:00
|
|
|
|
|
2019-06-11 16:45:09 +02:00
|
|
|
|
The method checkUnconditionalAndSave() evaluates unconditional 𝔼[F(y,u,u′)].
|
2019-01-04 16:29:57 +01:00
|
|
|
|
|
2019-06-11 16:45:09 +02:00
|
|
|
|
The object also maintains a set of GResidFunction functions ‘vfs’ in order
|
|
|
|
|
to save (possibly expensive) copying of DynamicModel’s. */
|
2019-01-04 16:29:57 +01:00
|
|
|
|
|
|
|
|
|
class GlobalChecker
|
|
|
|
|
{
|
|
|
|
|
const Approximation ≈
|
|
|
|
|
const DynamicModel &model;
|
|
|
|
|
Journal &journal;
|
|
|
|
|
GResidFunction rf;
|
|
|
|
|
VectorFunctionSet vfs;
|
|
|
|
|
public:
|
|
|
|
|
GlobalChecker(const Approximation &app, int n, Journal &jr)
|
|
|
|
|
: approx(app), model(approx.getModel()), journal(jr),
|
|
|
|
|
rf(approx), vfs(rf, n)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
void check(int max_evals, const ConstTwoDMatrix &y,
|
|
|
|
|
const ConstTwoDMatrix &x, TwoDMatrix &out);
|
2019-03-06 18:40:19 +01:00
|
|
|
|
void checkAlongShocksAndSave(mat_t *fd, const std::string &prefix,
|
2019-01-04 16:29:57 +01:00
|
|
|
|
int m, double mult, int max_evals);
|
2019-03-06 18:40:19 +01:00
|
|
|
|
void checkOnEllipseAndSave(mat_t *fd, const std::string &prefix,
|
2019-01-04 16:29:57 +01:00
|
|
|
|
int m, double mult, int max_evals);
|
2019-03-06 18:40:19 +01:00
|
|
|
|
void checkAlongSimulationAndSave(mat_t *fd, const std::string &prefix,
|
2019-01-04 16:29:57 +01:00
|
|
|
|
int m, int max_evals);
|
2019-03-06 18:40:19 +01:00
|
|
|
|
void checkUnconditionalAndSave(mat_t *fd, const std::string &prefix,
|
2019-01-04 16:29:57 +01:00
|
|
|
|
int m, int max_evals);
|
|
|
|
|
protected:
|
|
|
|
|
void check(const Quadrature &quad, int level,
|
|
|
|
|
const ConstVector &y, const ConstVector &x, Vector &out);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Signalled resid function. Not implemented yet. todo: */
|
|
|
|
|
|
|
|
|
|
class ResidFunctionSig : public ResidFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
ResidFunctionSig(const Approximation &app, const Vector &ys, const Vector &xx);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|