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
|
2021-06-09 17:33:48 +02:00
|
|
|
|
* along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
2019-06-19 14:34:30 +02:00
|
|
|
|
*/
|
2009-09-08 15:55:19 +02:00
|
|
|
|
|
|
|
|
|
#ifndef DYNARE3_H
|
|
|
|
|
#define DYNARE3_H
|
|
|
|
|
|
2019-01-08 16:09:25 +01:00
|
|
|
|
#include "../tl/cc/t_container.hh"
|
|
|
|
|
#include "../tl/cc/sparse_tensor.hh"
|
2019-01-04 16:29:57 +01:00
|
|
|
|
#include "../kord/decision_rule.hh"
|
|
|
|
|
#include "../kord/dynamic_model.hh"
|
2009-09-08 15:55:19 +02:00
|
|
|
|
|
2019-01-08 17:12:05 +01:00
|
|
|
|
#include "dynare_model.hh"
|
|
|
|
|
#include "nlsolve.hh"
|
2009-09-08 15:55:19 +02:00
|
|
|
|
|
|
|
|
|
#include <vector>
|
2019-04-23 18:57:52 +02:00
|
|
|
|
#include <memory>
|
2009-09-08 15:55:19 +02:00
|
|
|
|
|
2012-08-23 18:30:27 +02:00
|
|
|
|
#include <matio.h>
|
|
|
|
|
|
2009-09-08 15:55:19 +02:00
|
|
|
|
class Dynare;
|
|
|
|
|
|
2017-05-16 16:30:27 +02:00
|
|
|
|
class DynareNameList : public NameList
|
|
|
|
|
{
|
2019-03-06 18:40:19 +01:00
|
|
|
|
std::vector<std::string> names;
|
2009-09-08 15:55:19 +02:00
|
|
|
|
public:
|
2017-05-16 16:30:27 +02:00
|
|
|
|
DynareNameList(const Dynare &dynare);
|
|
|
|
|
int
|
2019-01-09 16:26:42 +01:00
|
|
|
|
getNum() const override
|
2017-05-16 16:30:27 +02:00
|
|
|
|
{
|
2019-03-06 18:40:19 +01:00
|
|
|
|
return static_cast<int>(names.size());
|
2017-05-16 16:30:27 +02:00
|
|
|
|
}
|
2019-03-06 18:40:19 +01:00
|
|
|
|
const std::string &
|
2019-01-09 16:26:42 +01:00
|
|
|
|
getName(int i) const override
|
2017-05-16 16:30:27 +02:00
|
|
|
|
{
|
|
|
|
|
return names[i];
|
|
|
|
|
}
|
2019-06-19 17:33:01 +02:00
|
|
|
|
/* This for each string of the input vector calculates its index in the
|
|
|
|
|
names. And returns the resulting vector of indices. If the name cannot be
|
|
|
|
|
found, then an exception is raised. */
|
2019-03-06 18:40:19 +01:00
|
|
|
|
std::vector<int> selectIndices(const std::vector<std::string> &ns) const;
|
2009-09-08 15:55:19 +02:00
|
|
|
|
};
|
|
|
|
|
|
2017-05-16 16:30:27 +02:00
|
|
|
|
class DynareExogNameList : public NameList
|
|
|
|
|
{
|
2019-03-06 18:40:19 +01:00
|
|
|
|
std::vector<std::string> names;
|
2009-09-08 15:55:19 +02:00
|
|
|
|
public:
|
2017-05-16 16:30:27 +02:00
|
|
|
|
DynareExogNameList(const Dynare &dynare);
|
|
|
|
|
int
|
2019-01-09 16:26:42 +01:00
|
|
|
|
getNum() const override
|
2017-05-16 16:30:27 +02:00
|
|
|
|
{
|
2019-03-06 18:40:19 +01:00
|
|
|
|
return static_cast<int>(names.size());
|
2017-05-16 16:30:27 +02:00
|
|
|
|
}
|
2019-03-06 18:40:19 +01:00
|
|
|
|
const std::string &
|
2019-01-09 16:26:42 +01:00
|
|
|
|
getName(int i) const override
|
2017-05-16 16:30:27 +02:00
|
|
|
|
{
|
|
|
|
|
return names[i];
|
|
|
|
|
}
|
2009-09-08 15:55:19 +02:00
|
|
|
|
};
|
|
|
|
|
|
2017-05-16 16:30:27 +02:00
|
|
|
|
class DynareStateNameList : public NameList
|
|
|
|
|
{
|
2019-03-06 18:40:19 +01:00
|
|
|
|
std::vector<std::string> names;
|
2009-09-08 15:55:19 +02:00
|
|
|
|
public:
|
2017-05-16 16:30:27 +02:00
|
|
|
|
DynareStateNameList(const Dynare &dynare, const DynareNameList &dnl,
|
|
|
|
|
const DynareExogNameList &denl);
|
|
|
|
|
int
|
2019-01-09 16:26:42 +01:00
|
|
|
|
getNum() const override
|
2017-05-16 16:30:27 +02:00
|
|
|
|
{
|
2019-03-06 18:40:19 +01:00
|
|
|
|
return static_cast<int>(names.size());
|
2017-05-16 16:30:27 +02:00
|
|
|
|
}
|
2019-03-06 18:40:19 +01:00
|
|
|
|
const std::string &
|
2019-01-09 16:26:42 +01:00
|
|
|
|
getName(int i) const override
|
2017-05-16 16:30:27 +02:00
|
|
|
|
{
|
|
|
|
|
return names[i];
|
|
|
|
|
}
|
2009-09-08 15:55:19 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// The following only implements DynamicModel with help of ogdyn::DynareModel
|
|
|
|
|
|
|
|
|
|
class DynareJacobian;
|
2017-05-16 16:30:27 +02:00
|
|
|
|
class Dynare : public DynamicModel
|
|
|
|
|
{
|
|
|
|
|
friend class DynareNameList;
|
|
|
|
|
friend class DynareExogNameList;
|
|
|
|
|
friend class DynareStateNameList;
|
|
|
|
|
friend class DynareJacobian;
|
|
|
|
|
Journal &journal;
|
2019-04-23 18:57:52 +02:00
|
|
|
|
std::unique_ptr<ogdyn::DynareModel> model;
|
|
|
|
|
std::unique_ptr<Vector> ysteady;
|
2017-05-16 16:30:27 +02:00
|
|
|
|
TensorContainer<FSSparseTensor> md;
|
2019-04-23 18:57:52 +02:00
|
|
|
|
std::unique_ptr<DynareNameList> dnl;
|
|
|
|
|
std::unique_ptr<DynareExogNameList> denl;
|
|
|
|
|
std::unique_ptr<DynareStateNameList> dsnl;
|
|
|
|
|
std::unique_ptr<ogp::FormulaEvaluator> fe;
|
|
|
|
|
std::unique_ptr<ogp::FormulaDerEvaluator> fde;
|
2017-05-16 16:30:27 +02:00
|
|
|
|
const double ss_tol;
|
2009-09-08 15:55:19 +02:00
|
|
|
|
public:
|
2019-06-19 17:33:01 +02:00
|
|
|
|
/* Parses the given model file and uses the given order to
|
|
|
|
|
override order from the model file (if it is ≠ −1). */
|
2019-04-23 18:57:52 +02:00
|
|
|
|
Dynare(const std::string &modname, int ord, double sstol, Journal &jr);
|
2017-05-16 16:30:27 +02:00
|
|
|
|
/** Parses the given equations with explicitly given names. */
|
2019-04-23 18:57:52 +02:00
|
|
|
|
Dynare(const std::vector<std::string> &endo,
|
|
|
|
|
const std::vector<std::string> &exo,
|
|
|
|
|
const std::vector<std::string> &par,
|
2019-04-24 14:52:30 +02:00
|
|
|
|
const std::string &equations, int ord,
|
2017-05-16 16:30:27 +02:00
|
|
|
|
double sstol, Journal &jr);
|
2019-06-19 17:33:01 +02:00
|
|
|
|
/* Makes a deep copy of the object. */
|
2017-05-16 16:30:27 +02:00
|
|
|
|
Dynare(const Dynare &dyn);
|
2019-04-23 18:57:52 +02:00
|
|
|
|
Dynare(Dynare &&) = default;
|
2019-03-06 18:40:19 +01:00
|
|
|
|
std::unique_ptr<DynamicModel>
|
2019-01-09 16:26:42 +01:00
|
|
|
|
clone() const override
|
2017-05-16 16:30:27 +02:00
|
|
|
|
{
|
2019-03-06 18:40:19 +01:00
|
|
|
|
return std::make_unique<Dynare>(*this);
|
2017-05-16 16:30:27 +02:00
|
|
|
|
}
|
2019-12-20 14:36:20 +01:00
|
|
|
|
|
2019-04-23 18:57:52 +02:00
|
|
|
|
~Dynare() override = default;
|
2017-05-16 16:30:27 +02:00
|
|
|
|
int
|
2019-01-09 16:26:42 +01:00
|
|
|
|
nstat() const override
|
2017-05-16 16:30:27 +02:00
|
|
|
|
{
|
|
|
|
|
return model->getAtoms().nstat();
|
|
|
|
|
}
|
|
|
|
|
int
|
2019-01-09 16:26:42 +01:00
|
|
|
|
nboth() const override
|
2017-05-16 16:30:27 +02:00
|
|
|
|
{
|
|
|
|
|
return model->getAtoms().nboth();
|
|
|
|
|
}
|
|
|
|
|
int
|
2019-01-09 16:26:42 +01:00
|
|
|
|
npred() const override
|
2017-05-16 16:30:27 +02:00
|
|
|
|
{
|
|
|
|
|
return model->getAtoms().npred();
|
|
|
|
|
}
|
|
|
|
|
int
|
2019-01-09 16:26:42 +01:00
|
|
|
|
nforw() const override
|
2017-05-16 16:30:27 +02:00
|
|
|
|
{
|
|
|
|
|
return model->getAtoms().nforw();
|
|
|
|
|
}
|
|
|
|
|
int
|
2019-01-09 16:26:42 +01:00
|
|
|
|
nexog() const override
|
2017-05-16 16:30:27 +02:00
|
|
|
|
{
|
|
|
|
|
return model->getAtoms().nexo();
|
|
|
|
|
}
|
|
|
|
|
int
|
|
|
|
|
nys() const
|
|
|
|
|
{
|
|
|
|
|
return model->getAtoms().nys();
|
|
|
|
|
}
|
|
|
|
|
int
|
|
|
|
|
nyss() const
|
|
|
|
|
{
|
|
|
|
|
return model->getAtoms().nyss();
|
|
|
|
|
}
|
|
|
|
|
int
|
|
|
|
|
ny() const
|
|
|
|
|
{
|
|
|
|
|
return model->getAtoms().ny();
|
|
|
|
|
}
|
|
|
|
|
int
|
2019-01-09 16:26:42 +01:00
|
|
|
|
order() const override
|
2017-05-16 16:30:27 +02:00
|
|
|
|
{
|
|
|
|
|
return model->getOrder();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const NameList &
|
2019-01-09 16:26:42 +01:00
|
|
|
|
getAllEndoNames() const override
|
2017-05-16 16:30:27 +02:00
|
|
|
|
{
|
|
|
|
|
return *dnl;
|
|
|
|
|
}
|
|
|
|
|
const NameList &
|
2019-01-09 16:26:42 +01:00
|
|
|
|
getStateNames() const override
|
2017-05-16 16:30:27 +02:00
|
|
|
|
{
|
|
|
|
|
return *dsnl;
|
|
|
|
|
}
|
|
|
|
|
const NameList &
|
2019-01-09 16:26:42 +01:00
|
|
|
|
getExogNames() const override
|
2017-05-16 16:30:27 +02:00
|
|
|
|
{
|
|
|
|
|
return *denl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TwoDMatrix &
|
|
|
|
|
getVcov()
|
|
|
|
|
{
|
|
|
|
|
return model->getVcov();
|
|
|
|
|
}
|
|
|
|
|
const TwoDMatrix &
|
2019-01-09 16:26:42 +01:00
|
|
|
|
getVcov() const override
|
2017-05-16 16:30:27 +02:00
|
|
|
|
{
|
|
|
|
|
return model->getVcov();
|
|
|
|
|
}
|
|
|
|
|
Vector &
|
|
|
|
|
getParams()
|
|
|
|
|
{
|
|
|
|
|
return model->getParams();
|
|
|
|
|
}
|
|
|
|
|
const Vector &
|
|
|
|
|
getParams() const
|
|
|
|
|
{
|
|
|
|
|
return model->getParams();
|
|
|
|
|
}
|
|
|
|
|
void
|
|
|
|
|
setInitOuter(const Vector &x)
|
|
|
|
|
{
|
|
|
|
|
model->setInitOuter(x);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const TensorContainer<FSSparseTensor> &
|
2019-01-09 16:26:42 +01:00
|
|
|
|
getModelDerivatives() const override
|
2017-05-16 16:30:27 +02:00
|
|
|
|
{
|
|
|
|
|
return md;
|
|
|
|
|
}
|
|
|
|
|
const Vector &
|
2019-01-09 16:26:42 +01:00
|
|
|
|
getSteady() const override
|
2017-05-16 16:30:27 +02:00
|
|
|
|
{
|
|
|
|
|
return *ysteady;
|
|
|
|
|
}
|
|
|
|
|
Vector &
|
2019-01-09 16:26:42 +01:00
|
|
|
|
getSteady() override
|
2017-05-16 16:30:27 +02:00
|
|
|
|
{
|
|
|
|
|
return *ysteady;
|
|
|
|
|
}
|
|
|
|
|
const ogdyn::DynareModel &
|
|
|
|
|
getModel() const
|
|
|
|
|
{
|
|
|
|
|
return *model;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// here is true public interface
|
|
|
|
|
void solveDeterministicSteady(Vector &steady);
|
|
|
|
|
void
|
2019-01-09 16:26:42 +01:00
|
|
|
|
solveDeterministicSteady() override
|
2017-05-16 16:30:27 +02:00
|
|
|
|
{
|
|
|
|
|
solveDeterministicSteady(*ysteady);
|
|
|
|
|
}
|
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 evaluateSystem(Vector &out, const ConstVector &yy, const Vector &xx) override;
|
|
|
|
|
void evaluateSystem(Vector &out, const ConstVector &yym, const ConstVector &yy,
|
|
|
|
|
const ConstVector &yyp, const Vector &xx) override;
|
2017-05-16 16:30:27 +02:00
|
|
|
|
void calcDerivatives(const Vector &yy, const Vector &xx);
|
2019-01-09 16:26:42 +01:00
|
|
|
|
void calcDerivativesAtSteady() override;
|
2017-05-16 16:30:27 +02:00
|
|
|
|
|
2019-04-23 18:57:52 +02:00
|
|
|
|
void writeMat(mat_t *fd, const std::string &prefix) const;
|
2017-05-16 16:30:27 +02:00
|
|
|
|
void writeDump(const std::string &basename) const;
|
2009-09-08 15:55:19 +02:00
|
|
|
|
private:
|
2017-05-16 16:30:27 +02:00
|
|
|
|
void writeModelInfo(Journal &jr) const;
|
2009-09-08 15:55:19 +02:00
|
|
|
|
};
|
|
|
|
|
|
2017-05-16 16:30:27 +02:00
|
|
|
|
class DynareEvalLoader : public ogp::FormulaEvalLoader, public Vector
|
|
|
|
|
{
|
2009-09-08 15:55:19 +02:00
|
|
|
|
public:
|
2017-05-16 16:30:27 +02:00
|
|
|
|
DynareEvalLoader(const ogp::FineAtoms &a, Vector &out);
|
|
|
|
|
void
|
2019-01-09 16:26:42 +01:00
|
|
|
|
load(int i, double res) override
|
2017-05-16 16:30:27 +02:00
|
|
|
|
{
|
|
|
|
|
operator[](i) = res;
|
|
|
|
|
}
|
2009-09-08 15:55:19 +02:00
|
|
|
|
};
|
|
|
|
|
|
2017-05-16 16:30:27 +02:00
|
|
|
|
class DynareDerEvalLoader : public ogp::FormulaDerEvalLoader
|
|
|
|
|
{
|
2009-09-08 15:55:19 +02:00
|
|
|
|
protected:
|
2017-05-16 16:30:27 +02:00
|
|
|
|
const ogp::FineAtoms &atoms;
|
|
|
|
|
TensorContainer<FSSparseTensor> &md;
|
2009-09-08 15:55:19 +02:00
|
|
|
|
public:
|
2017-05-16 16:30:27 +02:00
|
|
|
|
DynareDerEvalLoader(const ogp::FineAtoms &a, TensorContainer<FSSparseTensor> &mod_ders,
|
|
|
|
|
int order);
|
2019-01-09 16:26:42 +01:00
|
|
|
|
void load(int i, int iord, const int *vars, double res) override;
|
2009-09-08 15:55:19 +02:00
|
|
|
|
};
|
|
|
|
|
|
2017-05-16 16:30:27 +02:00
|
|
|
|
class DynareJacobian : public ogu::Jacobian, public ogp::FormulaDerEvalLoader
|
|
|
|
|
{
|
2009-09-08 15:55:19 +02:00
|
|
|
|
protected:
|
2017-05-16 16:30:27 +02:00
|
|
|
|
Dynare &d;
|
2009-09-08 15:55:19 +02:00
|
|
|
|
public:
|
2017-05-16 16:30:27 +02:00
|
|
|
|
DynareJacobian(Dynare &dyn);
|
2019-04-23 18:57:52 +02:00
|
|
|
|
~DynareJacobian() override = default;
|
2019-01-09 16:26:42 +01:00
|
|
|
|
void load(int i, int iord, const int *vars, double res) override;
|
|
|
|
|
void eval(const Vector &in) override;
|
2009-09-08 15:55:19 +02:00
|
|
|
|
};
|
|
|
|
|
|
2017-05-16 16:30:27 +02:00
|
|
|
|
class DynareVectorFunction : public ogu::VectorFunction
|
|
|
|
|
{
|
2009-09-08 15:55:19 +02:00
|
|
|
|
protected:
|
2017-05-16 16:30:27 +02:00
|
|
|
|
Dynare &d;
|
2009-09-08 15:55:19 +02:00
|
|
|
|
public:
|
2017-05-16 16:30:27 +02:00
|
|
|
|
DynareVectorFunction(Dynare &dyn)
|
|
|
|
|
: d(dyn)
|
|
|
|
|
{
|
|
|
|
|
}
|
2019-04-23 18:57:52 +02:00
|
|
|
|
~DynareVectorFunction() override = default;
|
2017-05-16 16:30:27 +02:00
|
|
|
|
int
|
2019-01-09 16:26:42 +01:00
|
|
|
|
inDim() const override
|
2017-05-16 16:30:27 +02:00
|
|
|
|
{
|
|
|
|
|
return d.ny();
|
|
|
|
|
}
|
|
|
|
|
int
|
2019-01-09 16:26:42 +01:00
|
|
|
|
outDim() const override
|
2017-05-16 16:30:27 +02:00
|
|
|
|
{
|
|
|
|
|
return d.ny();
|
|
|
|
|
}
|
2019-01-09 16:26:42 +01:00
|
|
|
|
void eval(const ConstVector &in, Vector &out) override;
|
2009-09-08 15:55:19 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|