2009-09-08 15:55:19 +02:00
|
|
|
// $Id: dynare3.h 1764 2008-03-31 14:30:55Z kamenik $
|
|
|
|
// Copyright 2005, Ondra Kamenik
|
|
|
|
|
|
|
|
#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>
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
vector<const char *> names;
|
2009-09-08 15:55:19 +02:00
|
|
|
public:
|
2017-05-16 16:30:27 +02:00
|
|
|
DynareNameList(const Dynare &dynare);
|
|
|
|
int
|
|
|
|
getNum() const
|
|
|
|
{
|
|
|
|
return (int) names.size();
|
|
|
|
}
|
|
|
|
const char *
|
|
|
|
getName(int i) const
|
|
|
|
{
|
|
|
|
return names[i];
|
|
|
|
}
|
|
|
|
/** 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. */
|
|
|
|
vector<int> selectIndices(const vector<const char *> &ns) const;
|
2009-09-08 15:55:19 +02:00
|
|
|
};
|
|
|
|
|
2017-05-16 16:30:27 +02:00
|
|
|
class DynareExogNameList : public NameList
|
|
|
|
{
|
|
|
|
vector<const char *> names;
|
2009-09-08 15:55:19 +02:00
|
|
|
public:
|
2017-05-16 16:30:27 +02:00
|
|
|
DynareExogNameList(const Dynare &dynare);
|
|
|
|
int
|
|
|
|
getNum() const
|
|
|
|
{
|
|
|
|
return (int) names.size();
|
|
|
|
}
|
|
|
|
const char *
|
|
|
|
getName(int i) const
|
|
|
|
{
|
|
|
|
return names[i];
|
|
|
|
}
|
2009-09-08 15:55:19 +02:00
|
|
|
};
|
|
|
|
|
2017-05-16 16:30:27 +02:00
|
|
|
class DynareStateNameList : public NameList
|
|
|
|
{
|
|
|
|
vector<const char *> 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
|
|
|
|
getNum() const
|
|
|
|
{
|
|
|
|
return (int) names.size();
|
|
|
|
}
|
|
|
|
const char *
|
|
|
|
getName(int i) const
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
ogdyn::DynareModel *model;
|
|
|
|
Vector *ysteady;
|
|
|
|
TensorContainer<FSSparseTensor> md;
|
|
|
|
DynareNameList *dnl;
|
|
|
|
DynareExogNameList *denl;
|
|
|
|
DynareStateNameList *dsnl;
|
|
|
|
ogp::FormulaEvaluator *fe;
|
|
|
|
ogp::FormulaDerEvaluator *fde;
|
|
|
|
const double ss_tol;
|
2009-09-08 15:55:19 +02:00
|
|
|
public:
|
2017-05-16 16:30:27 +02:00
|
|
|
/** Parses the given model file and uses the given order to
|
|
|
|
* override order from the model file (if it is != -1). */
|
|
|
|
Dynare(const char *modname, int ord, double sstol, Journal &jr);
|
|
|
|
/** Parses the given equations with explicitly given names. */
|
|
|
|
Dynare(const char **endo, int num_endo,
|
|
|
|
const char **exo, int num_exo,
|
|
|
|
const char **par, int num_par,
|
|
|
|
const char *equations, int len, int ord,
|
|
|
|
double sstol, Journal &jr);
|
|
|
|
/** Makes a deep copy of the object. */
|
|
|
|
Dynare(const Dynare &dyn);
|
|
|
|
DynamicModel *
|
|
|
|
clone() const
|
|
|
|
{
|
|
|
|
return new Dynare(*this);
|
|
|
|
}
|
|
|
|
virtual
|
|
|
|
~Dynare();
|
|
|
|
int
|
|
|
|
nstat() const
|
|
|
|
{
|
|
|
|
return model->getAtoms().nstat();
|
|
|
|
}
|
|
|
|
int
|
|
|
|
nboth() const
|
|
|
|
{
|
|
|
|
return model->getAtoms().nboth();
|
|
|
|
}
|
|
|
|
int
|
|
|
|
npred() const
|
|
|
|
{
|
|
|
|
return model->getAtoms().npred();
|
|
|
|
}
|
|
|
|
int
|
|
|
|
nforw() const
|
|
|
|
{
|
|
|
|
return model->getAtoms().nforw();
|
|
|
|
}
|
|
|
|
int
|
|
|
|
nexog() const
|
|
|
|
{
|
|
|
|
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
|
|
|
|
order() const
|
|
|
|
{
|
|
|
|
return model->getOrder();
|
|
|
|
}
|
|
|
|
|
|
|
|
const NameList &
|
|
|
|
getAllEndoNames() const
|
|
|
|
{
|
|
|
|
return *dnl;
|
|
|
|
}
|
|
|
|
const NameList &
|
|
|
|
getStateNames() const
|
|
|
|
{
|
|
|
|
return *dsnl;
|
|
|
|
}
|
|
|
|
const NameList &
|
|
|
|
getExogNames() const
|
|
|
|
{
|
|
|
|
return *denl;
|
|
|
|
}
|
|
|
|
|
|
|
|
TwoDMatrix &
|
|
|
|
getVcov()
|
|
|
|
{
|
|
|
|
return model->getVcov();
|
|
|
|
}
|
|
|
|
const TwoDMatrix &
|
|
|
|
getVcov() const
|
|
|
|
{
|
|
|
|
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> &
|
|
|
|
getModelDerivatives() const
|
|
|
|
{
|
|
|
|
return md;
|
|
|
|
}
|
|
|
|
const Vector &
|
|
|
|
getSteady() const
|
|
|
|
{
|
|
|
|
return *ysteady;
|
|
|
|
}
|
|
|
|
Vector &
|
|
|
|
getSteady()
|
|
|
|
{
|
|
|
|
return *ysteady;
|
|
|
|
}
|
|
|
|
const ogdyn::DynareModel &
|
|
|
|
getModel() const
|
|
|
|
{
|
|
|
|
return *model;
|
|
|
|
}
|
|
|
|
|
|
|
|
// here is true public interface
|
|
|
|
void solveDeterministicSteady(Vector &steady);
|
|
|
|
void
|
|
|
|
solveDeterministicSteady()
|
|
|
|
{
|
|
|
|
solveDeterministicSteady(*ysteady);
|
|
|
|
}
|
|
|
|
void evaluateSystem(Vector &out, const Vector &yy, const Vector &xx);
|
|
|
|
void evaluateSystem(Vector &out, const Vector &yym, const Vector &yy,
|
|
|
|
const Vector &yyp, const Vector &xx);
|
|
|
|
void calcDerivatives(const Vector &yy, const Vector &xx);
|
|
|
|
void calcDerivativesAtSteady();
|
|
|
|
|
|
|
|
void writeMat(mat_t *fd, const char *prefix) const;
|
|
|
|
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
|
|
|
|
load(int i, double res)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
void load(int i, int iord, const int *vars, double res);
|
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);
|
|
|
|
virtual ~DynareJacobian()
|
2019-01-09 16:16:29 +01:00
|
|
|
= default;
|
2017-05-16 16:30:27 +02:00
|
|
|
void load(int i, int iord, const int *vars, double res);
|
|
|
|
void eval(const Vector &in);
|
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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual ~DynareVectorFunction()
|
2019-01-09 16:16:29 +01:00
|
|
|
= default;
|
2017-05-16 16:30:27 +02:00
|
|
|
int
|
|
|
|
inDim() const
|
|
|
|
{
|
|
|
|
return d.ny();
|
|
|
|
}
|
|
|
|
int
|
|
|
|
outDim() const
|
|
|
|
{
|
|
|
|
return d.ny();
|
|
|
|
}
|
|
|
|
void eval(const ConstVector &in, Vector &out);
|
2009-09-08 15:55:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Local Variables:
|
|
|
|
// mode:C++
|
|
|
|
// End:
|