k_ord_dynare.cc 10.4 KB
Newer Older
george's avatar
george committed
1
/*
2
 * Copyright (C) 2008-2010 Dynare Team
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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/>.
 */
george's avatar
george committed
19 20 21 22 23

// GP, based on work by O.Kamenik

#include <vector>
#include "first_order.h"
24
#include "dynamic_abstract_class.hh"
george's avatar
george committed
25

26
#include <cmath>
sebastien's avatar
sebastien committed
27
#include <sstream>
george's avatar
george committed
28 29 30

#include "memory_file.h"

31 32 33 34

#include <iostream>
#include <fstream>

george's avatar
george committed
35 36 37 38
/**************************************************************************************/
/*       Dynare DynamicModel class                                                                 */
/**************************************************************************************/

39 40
KordpDynare::KordpDynare(const vector<string> &endo, int num_endo,
                         const vector<string> &exo, int nexog, int npar,
41 42
                         Vector &ysteady, TwoDMatrix &vcov, Vector &inParams, int nstat,
                         int npred, int nforw, int nboth, const int jcols, const Vector &nnzd,
43
                         const int nsteps, int norder,
44
                         Journal &jr, DynamicModelAC *dynamicModelFile_arg, double sstol,
45
                         const vector<int> &var_order, const TwoDMatrix &llincidence, double criterium) throw (TLException) :
sebastien's avatar
sebastien committed
46 47
  nStat(nstat), nBoth(nboth), nPred(npred), nForw(nforw), nExog(nexog), nPar(npar),
  nYs(npred + nboth), nYss(nboth + nforw), nY(num_endo), nJcols(jcols), NNZD(nnzd), nSteps(nsteps),
48
  nOrder(norder), journal(jr), ySteady(ysteady), params(inParams), vCov(vcov),
49
  md(1), dnl(*this, endo), denl(*this, exo), dsnl(*this, dnl, denl), ss_tol(sstol), varOrder(var_order),
50
  ll_Incidence(llincidence), qz_criterium(criterium), dynamicModelFile(dynamicModelFile_arg)
george's avatar
george committed
51
{
52
  ReorderDynareJacobianIndices();
53

sebastien's avatar
sebastien committed
54 55
  //	Initialise ModelDerivativeContainer(*this, this->md, nOrder);
  for (int iord = 1; iord <= nOrder; iord++)
56
    md.insert(new FSSparseTensor(iord, nY+nYs+nYss+nExog, nY));
george's avatar
george committed
57 58 59 60
}

KordpDynare::~KordpDynare()
{
61
  // No need to manually delete tensors in "md", they are deleted by the TensorContainer destructor
george's avatar
george committed
62 63
}

64
void
sebastien's avatar
sebastien committed
65
KordpDynare::solveDeterministicSteady()
george's avatar
george committed
66 67 68 69 70
{
  JournalRecordPair pa(journal);
  pa << "Non-linear solver for deterministic steady state By-passed " << endrec;
}

71
void
sebastien's avatar
sebastien committed
72
KordpDynare::evaluateSystem(Vector &out, const Vector &yy, const Vector &xx) throw (DynareException)
george's avatar
george committed
73
{
74 75
  // This method is only called when checking the residuals at steady state (Approximation::check), so return zero residuals
  out.zeros();
george's avatar
george committed
76 77
}

78 79
void
KordpDynare::evaluateSystem(Vector &out, const Vector &yym, const Vector &yy,
sebastien's avatar
sebastien committed
80
                            const Vector &yyp, const Vector &xx) throw (DynareException)
george's avatar
george committed
81
{
82 83
  // This method is only called when checking the residuals at steady state (Approximation::check), so return zero residuals
  out.zeros();
george's avatar
george committed
84
}
sebastien's avatar
sebastien committed
85

george's avatar
george committed
86
/************************************************
87 88 89 90
 * this is main derivative calculation functin that indirectly calls dynamic.dll
 * which performs actual calculation and reorders
 ***************************************************/
void
91
KordpDynare::calcDerivativesAtSteady() throw (DynareException)
george's avatar
george committed
92
{
93 94
  TwoDMatrix g1(nY, nJcols);
  g1.zeros();
95

96
  TwoDMatrix *g2p = NULL, *g3p = NULL;
97 98 99

  if (nOrder > 1)
    {
100 101 102
      // allocate space for sparse Hessian
      g2p = new TwoDMatrix((int) NNZD[1], 3);
      g2p->zeros();
103
    }
104

105 106
  if (nOrder > 2)
    {
107 108
      g3p = new TwoDMatrix((int) NNZD[2], 3);
      g3p->zeros();
109
    }
110 111 112 113

  Vector xx(nexog());
  xx.zeros();

sebastien's avatar
sebastien committed
114
  Vector out(nY);
george's avatar
george committed
115
  out.zeros();
116 117
  Vector llxSteady(nJcols-nExog);
  LLxSteady(ySteady, llxSteady);
sebastien's avatar
sebastien committed
118

119
  dynamicModelFile->eval(llxSteady, xx, params, ySteady, out, &g1, g2p, g3p);
120

121
  populateDerivativesContainer(g1, 1, JacobianIndices);
122

123
  if (nOrder > 1)
george's avatar
george committed
124
    {
125 126
      populateDerivativesContainer(*g2p, 2, JacobianIndices);
      delete g2p;
george's avatar
george committed
127
    }
128
  if (nOrder > 2)
george's avatar
george committed
129
    {
130 131
      populateDerivativesContainer(*g3p, 3, JacobianIndices);
      delete g3p;
george's avatar
george committed
132
    }
george's avatar
george committed
133 134 135
}

/*******************************************************************************
sebastien's avatar
sebastien committed
136 137
 * populateDerivatives to sparse Tensor and fit it in the Derivatives Container
 *******************************************************************************/
138
void
139
KordpDynare::populateDerivativesContainer(const TwoDMatrix &g, int ord, const vector<int> &vOrder)
george's avatar
george committed
140
{
141
  // model derivatives FSSparseTensor instance
142
  FSSparseTensor *mdTi = (new FSSparseTensor(ord, nJcols, nY));
143 144 145

  IntSequence s(ord, 0);

146 147
  if (ord == 1)
    {
148
      for (int i = 0; i < g.ncols(); i++)
sebastien's avatar
sebastien committed
149
        {
150
          for (int j = 0; j < g.nrows(); j++)
sebastien's avatar
sebastien committed
151 152 153
            {
              double x;
              if (s[0] < nJcols-nExog)
154
                x = g.get(j, vOrder[s[0]]);
sebastien's avatar
sebastien committed
155
              else
156
                x = g.get(j, s[0]);
sebastien's avatar
sebastien committed
157 158 159 160 161
              if (x != 0.0)
                mdTi->insert(s, j, x);
            }
          s[0]++;
        }
162
    }
sebastien's avatar
sebastien committed
163
  else if (ord == 2)
164
    {
sebastien's avatar
sebastien committed
165 166 167
      int nJcols1 = nJcols-nExog;
      vector<int> revOrder(nJcols1);
      for (int i = 0; i < nJcols1; i++)
168 169
        revOrder[vOrder[i]] = i;
      for (int i = 0; i < g.nrows(); i++)
sebastien's avatar
sebastien committed
170
        {
171 172
          int j = (int) g.get(i, 0)-1; // hessian indices start with 1
          int i1 = (int) g.get(i, 1) -1;
173 174
          int s0 = i1 / nJcols;
          int s1 = i1 % nJcols;
sebastien's avatar
sebastien committed
175 176 177 178 179 180 181 182 183 184
          if (s0 < nJcols1)
            s[0] = revOrder[s0];
          else
            s[0] = s0;
          if (s1 < nJcols1)
            s[1] = revOrder[s1];
          else
            s[1] = s1;
          if (s[1] >= s[0])
            {
185
              double x = g.get(i, 2);
sebastien's avatar
sebastien committed
186 187 188
              mdTi->insert(s, j, x);
            }
        }
189
    }
sebastien's avatar
sebastien committed
190
  else if (ord == 3)
191 192 193 194 195
    {
      int nJcols1 = nJcols-nExog;
      int nJcols2 = nJcols*nJcols;
      vector<int> revOrder(nJcols1);
      for (int i = 0; i < nJcols1; i++)
196 197
        revOrder[vOrder[i]] = i;
      for (int i = 0; i < g.nrows(); i++)
sebastien's avatar
sebastien committed
198
        {
199 200
          int j = (int) g.get(i, 0)-1;
          int i1 = (int) g.get(i, 1) -1;
201 202 203 204
          int s0 = i1 / nJcols2;
          int i2 = i1 % nJcols2;
          int s1 = i2 / nJcols;
          int s2 = i2 % nJcols;
sebastien's avatar
sebastien committed
205 206 207 208 209 210 211 212 213 214 215 216
          if (s0 < nJcols1)
            s[0] = revOrder[s0];
          else
            s[0] = s0;
          if (s1 < nJcols1)
            s[1] = revOrder[s1];
          else
            s[1] = s1;
          if (s2 < nJcols1)
            s[2] = revOrder[s2];
          else
            s[2] = s2;
217 218 219
          double x = g.get(i, 2);
          if (s.isSorted())
            mdTi->insert(s, j, x);
sebastien's avatar
sebastien committed
220
        }
221
    }
sebastien's avatar
sebastien committed
222

george's avatar
george committed
223 224
  // md container
  md.remove(Symmetry(ord));
225
  md.insert(mdTi);
226
  // No need to delete mdTi, it will be deleted by TensorContainer destructor
george's avatar
george committed
227 228 229
}

/*********************************************************
230 231 232 233
 * LLxSteady()
 * returns ySteady extended with leads and lags suitable for
 * passing to <model>_dynamic DLL
 *************************************************************/
234 235
void
KordpDynare::LLxSteady(const Vector &yS, Vector &llxSteady) throw (DynareException, TLException)
236 237
{
  if ((nJcols-nExog) == yS.length())
sebastien's avatar
sebastien committed
238 239
    throw DynareException(__FILE__, __LINE__, "ySteady already of right size");

240
  // create temporary square 2D matrix size nEndo x nEndo (sparse)
george's avatar
george committed
241
  // for the lag, current and lead blocks of the jacobian
242 243 244 245
  if (llxSteady.length() != nJcols-nExog)
    throw DynareException(__FILE__, __LINE__, "llxSteady has wrong size");

  for (int ll_row = 0; ll_row < ll_Incidence.nrows(); ll_row++)
sebastien's avatar
sebastien committed
246 247 248
    {
      // populate (non-sparse) vector with ysteady values
      for (int i = 0; i < nY; i++)
249
        {
250 251
          if (ll_Incidence.get(ll_row, i))
            llxSteady[((int) ll_Incidence.get(ll_row, i))-1] = yS[i];
252
        }
sebastien's avatar
sebastien committed
253
    }
george's avatar
george committed
254 255 256
}

/************************************
sebastien's avatar
sebastien committed
257 258 259 260
 * Reorder DynareJacobianIndices of variables in a vector according to
 * given int * varOrder together with lead & lag incidence matrix and
 * any the extra columns for exogenous vars, and then,
 * reorders its blocks given by the varOrder and the Dynare++ expectations:
george's avatar
george committed
261

sebastien's avatar
sebastien committed
262 263
 * extra	nboth+ npred (t-1) lags
 * varOrder
264
                static:
george's avatar
george committed
265
    pred
266 267
    both
    forward
sebastien's avatar
sebastien committed
268 269
    * extra both + nforw (t+1) leads, and
    * extra exogen
270

sebastien's avatar
sebastien committed
271
    * so to match the jacobian organisation expected by the Appoximation class
george's avatar
george committed
272 273 274 275 276 277 278 279 280
      both + nforw (t+1) leads
      static
      pred
      both
      forward
      nboth+ npred  (t-1) lags
      exogen
************************************/

281 282
void
KordpDynare::ReorderDynareJacobianIndices() throw (TLException)
283 284
{
  // create temporary square 2D matrix size nEndo x nEndo (sparse)
george's avatar
george committed
285
  // for the lag, current and lead blocks of the jacobian
286
  JacobianIndices.resize(nJcols);
287
  vector <int> tmp(nY);
sebastien's avatar
sebastien committed
288 289
  int i, j, rjoff = nJcols-nExog-1;

290
  for (int ll_row = 0; ll_row < ll_Incidence.nrows(); ll_row++)
sebastien's avatar
sebastien committed
291 292 293 294
    {
      // reorder in orde-var order & populate temporary nEndo (sparse) vector with
      // the lag, current and lead blocks of the jacobian respectively
      for (i = 0; i < nY; i++)
295
        tmp[i] = ((int) ll_Incidence.get(ll_row, varOrder[i]-1));
sebastien's avatar
sebastien committed
296 297 298 299 300
      // write the reordered blocks back to the jacobian
      // in reverse order
      for (j = nY-1; j >= 0; j--)
        if (tmp[j])
          {
301
            JacobianIndices[rjoff] = tmp[j] -1;
sebastien's avatar
sebastien committed
302 303 304 305 306
            rjoff--;
            if (rjoff < 0)
              break;
          }
    }
sebastien's avatar
sebastien committed
307

george's avatar
george committed
308
  //add the indices for the nExog exogenous jacobians
309
  for (j = nJcols-nExog; j < nJcols; j++)
310
    JacobianIndices[j] = j;
george's avatar
george committed
311 312 313 314 315 316
}

/**************************************************************************************/
/*       DynareNameList class                                                         */
/**************************************************************************************/

317
DynareNameList::DynareNameList(const KordpDynare &dynare, const vector<string> &names_arg) : names(names_arg)
george's avatar
george committed
318 319 320
{
}

321
DynareStateNameList::DynareStateNameList(const KordpDynare &dynare, const DynareNameList &dnl,
322
                                         const DynareNameList &denl)
george's avatar
george committed
323
{
324
  for (int i = 0; i < dynare.nys(); i++)
325
    names.push_back(string(dnl.getName(i+dynare.nstat())));
326
  for (int i = 0; i < dynare.nexog(); i++)
327
    names.push_back(string(denl.getName(i)));
george's avatar
george committed
328
}