Shocks.cc 14.6 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2014 Dynare Team
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * 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/>.
 */

20
21
#include <cassert>
#include <cstdlib>
22
23
24
25
26
#include <iostream>

#include "Shocks.hh"

AbstractShocksStatement::AbstractShocksStatement(bool mshocks_arg,
27
                                                 const det_shocks_t &det_shocks_arg,
28
                                                 const SymbolTable &symbol_table_arg) :
29
30
31
32
33
34
35
36
37
38
  mshocks(mshocks_arg),
  det_shocks(det_shocks_arg),
  symbol_table(symbol_table_arg)
{
}

void
AbstractShocksStatement::writeDetShocks(ostream &output) const
{
  int exo_det_length = 0;
39
  int counter = 1;
40

41
  for (det_shocks_t::const_iterator it = det_shocks.begin();
42
       it != det_shocks.end(); it++)
43
    {
sebastien's avatar
sebastien committed
44
      int id = symbol_table.getTypeSpecificID(it->first) + 1;
45
46
      bool exo_det = (symbol_table.getType(it->first) == eExogenousDet);

47
      for (size_t i = 0; i < it->second.size(); i++)
48
        {
49
50
51
          const int &period1 = it->second[i].period1;
          const int &period2 = it->second[i].period2;
          const expr_t value = it->second[i].value;
52

53
54
55
56
57
58
59
60
61
62
63
64
65
          output << "M_.det_shocks(" << counter << ").exo_det=" << (int) exo_det
                 << ";" << endl
                 << "M_.det_shocks(" << counter << ").exo_id=" << id
                 << ";" << endl
                 << "M_.det_shocks(" << counter << ").multiplicative=" << (int) mshocks
                 << ";" << endl
                 << "M_.det_shocks(" << counter << ").periods=" << period1
                 << ":" << period2 << ";" << endl
                 << "M_.det_shocks(" << counter << ").value=(";
          value->writeOutput(output);
          output << ");" << endl;

          counter++;
66
67
68
69
70
71
72
73

          if (exo_det && (period2 > exo_det_length))
            exo_det_length = period2;
        }
    }
  output << "M_.exo_det_length = " << exo_det_length << ";\n";
}

74
75
76
77
78
ShocksStatement::ShocksStatement(const det_shocks_t &det_shocks_arg,
                                 const var_and_std_shocks_t &var_shocks_arg,
                                 const var_and_std_shocks_t &std_shocks_arg,
                                 const covar_and_corr_shocks_t &covar_shocks_arg,
                                 const covar_and_corr_shocks_t &corr_shocks_arg,
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
                                 const SymbolTable &symbol_table_arg) :
  AbstractShocksStatement(false, det_shocks_arg, symbol_table_arg),
  var_shocks(var_shocks_arg),
  std_shocks(std_shocks_arg),
  covar_shocks(covar_shocks_arg),
  corr_shocks(corr_shocks_arg)
{
}

void
ShocksStatement::writeOutput(ostream &output, const string &basename) const
{
  output << "%" << endl
         << "% SHOCKS instructions" << endl
         << "%" << endl;

  writeDetShocks(output);
  writeVarAndStdShocks(output);
  writeCovarAndCorrShocks(output);
  if (covar_shocks.size()+corr_shocks.size() > 0)
    output << "M_.sigma_e_is_diagonal = 0;" << endl;
  else
    output << "M_.sigma_e_is_diagonal = 1;" << endl;
}

Sébastien Villemot's avatar
Sébastien Villemot committed
104
void
105
ShocksStatement::writeVarOrStdShock(ostream &output, var_and_std_shocks_t::const_iterator &it,
106
                                    bool stddev) const
Sébastien Villemot's avatar
Sébastien Villemot committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
{
  SymbolType type = symbol_table.getType(it->first);
  assert(type == eExogenous || symbol_table.isObservedVariable(it->first));

  int id;
  if (type == eExogenous)
    {
      output << "M_.Sigma_e(";
      id = symbol_table.getTypeSpecificID(it->first) + 1;
    }
  else
    {
      output << "M_.H(";
      id = symbol_table.getObservedVariableIndex(it->first) + 1;
    }

  output << id << ", " << id << ") = ";
124
  if (stddev)
Sébastien Villemot's avatar
Sébastien Villemot committed
125
126
    output << "(";
  it->second->writeOutput(output);
127
  if (stddev)
Sébastien Villemot's avatar
Sébastien Villemot committed
128
129
130
131
    output << ")^2";
  output << ";" << endl;
}

132
133
void
ShocksStatement::writeVarAndStdShocks(ostream &output) const
134
{
135
  var_and_std_shocks_t::const_iterator it;
136

137
  for (it = var_shocks.begin(); it != var_shocks.end(); it++)
Sébastien Villemot's avatar
Sébastien Villemot committed
138
    writeVarOrStdShock(output, it, false);
139

140
  for (it = std_shocks.begin(); it != std_shocks.end(); it++)
Sébastien Villemot's avatar
Sébastien Villemot committed
141
142
143
144
    writeVarOrStdShock(output, it, true);
}

void
145
ShocksStatement::writeCovarOrCorrShock(ostream &output, covar_and_corr_shocks_t::const_iterator &it,
Sébastien Villemot's avatar
Sébastien Villemot committed
146
147
148
149
150
151
                                       bool corr) const
{
  SymbolType type1 = symbol_table.getType(it->first.first);
  SymbolType type2 = symbol_table.getType(it->first.second);
  assert((type1 == eExogenous && type2 == eExogenous)
         || (symbol_table.isObservedVariable(it->first.first) && symbol_table.isObservedVariable(it->first.second)));
152
  string matrix, corr_matrix;
Sébastien Villemot's avatar
Sébastien Villemot committed
153
154
  int id1, id2;
  if (type1 == eExogenous)
155
    {
Sébastien Villemot's avatar
Sébastien Villemot committed
156
      matrix = "M_.Sigma_e";
157
      corr_matrix = "M_.Correlation_matrix";
Sébastien Villemot's avatar
Sébastien Villemot committed
158
159
160
161
162
163
      id1 = symbol_table.getTypeSpecificID(it->first.first) + 1;
      id2 = symbol_table.getTypeSpecificID(it->first.second) + 1;
    }
  else
    {
      matrix = "M_.H";
164
      corr_matrix = "M_.Correlation_matrix_ME";
Sébastien Villemot's avatar
Sébastien Villemot committed
165
166
      id1 = symbol_table.getObservedVariableIndex(it->first.first) + 1;
      id2 = symbol_table.getObservedVariableIndex(it->first.second) + 1;
167
    }
Sébastien Villemot's avatar
Sébastien Villemot committed
168
169
170
171
172
173
174
175
176

  output << matrix << "(" << id1 << ", " << id2 << ") = ";
  it->second->writeOutput(output);
  if (corr)
    output << "*sqrt(" << matrix << "(" << id1 << ", " << id1 << ")*"
           << matrix << "(" << id2 << ", " << id2 << "))";
  output << ";" << endl
         << matrix << "(" << id2 << ", " << id1 << ") = "
         << matrix << "(" << id1 << ", " << id2 << ");" << endl;
177
178
179
180
181
182
183
184
185

  if (corr)
    {
      output << corr_matrix << "(" << id1 << ", " << id2 << ") = ";
      it->second->writeOutput(output);
      output << ";" << endl
             << corr_matrix << "(" << id2 << ", " << id1 << ") = "
             << corr_matrix << "(" << id1 << ", " << id2 << ");" << endl;
    }
186
187
188
}

void
189
ShocksStatement::writeCovarAndCorrShocks(ostream &output) const
190
{
191
  covar_and_corr_shocks_t::const_iterator it;
192

193
  for (it = covar_shocks.begin(); it != covar_shocks.end(); it++)
Sébastien Villemot's avatar
Sébastien Villemot committed
194
    writeCovarOrCorrShock(output, it, false);
195

196
  for (it = corr_shocks.begin(); it != corr_shocks.end(); it++)
Sébastien Villemot's avatar
Sébastien Villemot committed
197
198
199
200
    writeCovarOrCorrShock(output, it, true);
}

void
201
ShocksStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings)
Sébastien Villemot's avatar
Sébastien Villemot committed
202
{
203
204
205
  /* Error out if variables are not of the right type. This must be done here
     and not at parsing time (see #448).
     Also Determine if there is a calibrated measurement error */
206
  for (var_and_std_shocks_t::const_iterator it = var_shocks.begin();
Sébastien Villemot's avatar
Sébastien Villemot committed
207
       it != var_shocks.end(); it++)
208
209
210
211
212
213
214
215
216
217
218
219
220
    {
      if (symbol_table.getType(it->first) != eExogenous
          && !symbol_table.isObservedVariable(it->first))
        {
          cerr << "shocks: setting a variance on '"
               << symbol_table.getName(it->first) << "' is not allowed, because it is neither an exogenous variable nor an observed endogenous variable" << endl;
          exit(EXIT_FAILURE);
        }
    
      if (symbol_table.isObservedVariable(it->first))
        mod_file_struct.calibrated_measurement_errors = true;
    }
  
Sébastien Villemot's avatar
Sébastien Villemot committed
221

222
  for (var_and_std_shocks_t::const_iterator it = std_shocks.begin();
Sébastien Villemot's avatar
Sébastien Villemot committed
223
       it != std_shocks.end(); it++)
224
225
226
227
228
229
230
231
232
233
234
235
    {
      if (symbol_table.getType(it->first) != eExogenous
          && !symbol_table.isObservedVariable(it->first))
        {
          cerr << "shocks: setting a standard error on '"
               << symbol_table.getName(it->first) << "' is not allowed, because it is neither an exogenous variable nor an observed endogenous variable" << endl;
          exit(EXIT_FAILURE);
        }

      if (symbol_table.isObservedVariable(it->first))
        mod_file_struct.calibrated_measurement_errors = true;
    }
Sébastien Villemot's avatar
Sébastien Villemot committed
236

237
  for (covar_and_corr_shocks_t::const_iterator it = covar_shocks.begin();
Sébastien Villemot's avatar
Sébastien Villemot committed
238
       it != covar_shocks.end(); it++)
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
    {
      int symb_id1 = it->first.first;
      int symb_id2 = it->first.second;
      
      if (!((symbol_table.getType(symb_id1) == eExogenous
             && symbol_table.getType(symb_id2) == eExogenous)
            || (symbol_table.isObservedVariable(symb_id1)
                && symbol_table.isObservedVariable(symb_id2))))
        {
          cerr << "shocks: setting a covariance between '"
               << symbol_table.getName(symb_id1) << "' and '"
               << symbol_table.getName(symb_id2) << "'is not allowed; covariances can only be specified for exogenous or observed endogenous variables of same type" << endl;
          exit(EXIT_FAILURE);
        }
      
      if (symbol_table.isObservedVariable(symb_id1)
          || symbol_table.isObservedVariable(symb_id2))
        mod_file_struct.calibrated_measurement_errors = true;
    }
Sébastien Villemot's avatar
Sébastien Villemot committed
258

259
  for (covar_and_corr_shocks_t::const_iterator it = corr_shocks.begin();
Sébastien Villemot's avatar
Sébastien Villemot committed
260
       it != corr_shocks.end(); it++)
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
    {
      int symb_id1 = it->first.first;
      int symb_id2 = it->first.second;
      
      if (!((symbol_table.getType(symb_id1) == eExogenous
             && symbol_table.getType(symb_id2) == eExogenous)
            || (symbol_table.isObservedVariable(symb_id1)
                && symbol_table.isObservedVariable(symb_id2))))
        {
          cerr << "shocks: setting a correlation between '"
               << symbol_table.getName(symb_id1) << "' and '"
               << symbol_table.getName(symb_id2) << "'is not allowed; correlations can only be specified for exogenous or observed endogenous variables of same type" << endl;
          exit(EXIT_FAILURE);
        }
      
      if (symbol_table.isObservedVariable(it->first.first)
          || symbol_table.isObservedVariable(it->first.second))
        mod_file_struct.calibrated_measurement_errors = true;
    }
280
281
282
283

  // Fill in mod_file_struct.parameters_with_shocks_values (related to #469)
  for (var_and_std_shocks_t::const_iterator it = var_shocks.begin();
       it != var_shocks.end(); ++it)
284
    it->second->collectVariables(eParameter, mod_file_struct.parameters_within_shocks_values);
285
286
  for (var_and_std_shocks_t::const_iterator it = std_shocks.begin();
       it != std_shocks.end(); ++it)
287
    it->second->collectVariables(eParameter, mod_file_struct.parameters_within_shocks_values);
288
289
  for (covar_and_corr_shocks_t::const_iterator it = covar_shocks.begin();
       it != covar_shocks.end(); ++it)
290
    it->second->collectVariables(eParameter, mod_file_struct.parameters_within_shocks_values);
291
292
  for (covar_and_corr_shocks_t::const_iterator it = corr_shocks.begin();
       it != corr_shocks.end(); ++it)
293
    it->second->collectVariables(eParameter, mod_file_struct.parameters_within_shocks_values);
294
295
}

296
MShocksStatement::MShocksStatement(const det_shocks_t &det_shocks_arg,
297
                                   const SymbolTable &symbol_table_arg) :
298
  AbstractShocksStatement(true, det_shocks_arg, symbol_table_arg)
299
300
301
302
303
304
{
}

void
MShocksStatement::writeOutput(ostream &output, const string &basename) const
{
sebastien's avatar
sebastien committed
305
  output << "%" << endl
306
         << "% MSHOCKS instructions" << endl
sebastien's avatar
sebastien committed
307
         << "%" << endl;
308
309
310

  writeDetShocks(output);
}
311

312
ConditionalForecastPathsStatement::ConditionalForecastPathsStatement(const AbstractShocksStatement::det_shocks_t &paths_arg) :
313
314
315
316
317
318
  paths(paths_arg),
  path_length(-1)
{
}

void
319
ConditionalForecastPathsStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings)
320
{
321
  for (AbstractShocksStatement::det_shocks_t::const_iterator it = paths.begin();
322
       it != paths.end(); it++)
323
324
    {
      int this_path_length = 0;
325
      const vector<AbstractShocksStatement::DetShockElement> &elems = it->second;
326
      for (int i = 0; i < (int) elems.size(); i++)
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
        // Period1 < Period2, as enforced in ParsingDriver::add_period()
        this_path_length = max(this_path_length, elems[i].period2);
      if (path_length == -1)
        path_length = this_path_length;
      else if (path_length != this_path_length)
        {
          cerr << "conditional_forecast_paths: all constrained paths must have the same length!" << endl;
          exit(EXIT_FAILURE);
        }
    }
}

void
ConditionalForecastPathsStatement::writeOutput(ostream &output, const string &basename) const
{
  assert(path_length > 0);
  output << "constrained_vars_ = [];" << endl
         << "constrained_paths_ = zeros(" << paths.size() << ", " << path_length << ");" << endl;

  int k = 1;

348
  for (AbstractShocksStatement::det_shocks_t::const_iterator it = paths.begin();
349
       it != paths.end(); it++)
350
    {
351
      if (it == paths.begin())
352
353
354
        {
          output << "constrained_vars_ = " << it->first +1 << ";" << endl;
        }
355
      else
356
357
358
359
        {
          output << "constrained_vars_ = [constrained_vars_; " << it->first +1 << "];" << endl;
        }

360

361
      const vector<AbstractShocksStatement::DetShockElement> &elems = it->second;
362
363
      for (int i = 0; i < (int) elems.size(); i++)
        for (int j = elems[i].period1; j <= elems[i].period2; j++)
364
365
366
367
368
369
370
371
          {
            output << "constrained_paths_(" << k << "," << j << ")=";
            elems[i].value->writeOutput(output);
            output << ";" << endl;
          }
      k++;
    }
}
372
373
374
375
376
377
378
379
380
381

MomentCalibration::MomentCalibration(const constraints_t &constraints_arg,
                                     const SymbolTable &symbol_table_arg)
  : constraints(constraints_arg), symbol_table(symbol_table_arg)
{
}

void
MomentCalibration::writeOutput(ostream &output, const string &basename) const
{
382
  output << "options_.endogenous_prior_restrictions.moment = {" << endl;
383
384
385
386
387
  for (size_t i = 0; i < constraints.size(); i++)
    {
      const Constraint &c = constraints[i];
      output << "'" << symbol_table.getName(c.endo1) << "', "
             << "'" << symbol_table.getName(c.endo2) << "', "
388
             << c.lags << ", "
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
             << "[ " << c.lower_bound << ", " << c.upper_bound << " ];"
             << endl;
    }
  output << "};" << endl;
}

IrfCalibration::IrfCalibration(const constraints_t &constraints_arg,
                               const SymbolTable &symbol_table_arg)
  : constraints(constraints_arg), symbol_table(symbol_table_arg)
{
}

void
IrfCalibration::writeOutput(ostream &output, const string &basename) const
{
404
  output << "options_.endogenous_prior_restrictions.irf = {" << endl;
405
406
407
408
409
  for (size_t i = 0; i < constraints.size(); i++)
    {
      const Constraint &c = constraints[i];
      output << "'" << symbol_table.getName(c.endo) << "', "
             << "'" << symbol_table.getName(c.exo) << "', "
410
             << c.periods << ", "
411
412
413
414
415
             << "[ " << c.lower_bound << ", " << c.upper_bound << " ];"
             << endl;
    }
  output << "};" << endl;
}