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

20
#include <cstdlib>
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <sstream>

using namespace std;

#include "ComputingTasks.hh"
#include "Statement.hh"

SteadyStatement::SteadyStatement(const OptionsList &options_list_arg) :
  options_list(options_list_arg)
{
}

void
SteadyStatement::writeOutput(ostream &output, const string &basename) const
{
  options_list.writeOutput(output);
  output << "steady;\n";
}

SteadySparseStatement::SteadySparseStatement(const OptionsList &options_list_arg) :
  options_list(options_list_arg)
{
}

void
SteadySparseStatement::writeOutput(ostream &output, const string &basename) const
{
  options_list.writeOutput(output);
  output << basename << "_static;\n";
}

CheckStatement::CheckStatement(const OptionsList &options_list_arg) :
  options_list(options_list_arg)
{
}

void
CheckStatement::writeOutput(ostream &output, const string &basename) const
{
  options_list.writeOutput(output);
  output << "check;\n";
}

void
CheckStatement::checkPass(ModFileStructure &mod_file_struct)
{
  mod_file_struct.check_present = true;
}

ferhat's avatar
ferhat committed
71
ModelInfoStatement::ModelInfoStatement(const OptionsList &options_list_arg) :
72
73
74
75
  options_list(options_list_arg)
{
}

ferhat's avatar
ferhat committed
76
void ModelInfoStatement::checkPass(ModFileStructure &mod_file_struct)
77
{
ferhat's avatar
ferhat committed
78
  //mod_file_struct.model_info_present = true;
79
80
}

ferhat's avatar
ferhat committed
81
void ModelInfoStatement::writeOutput(ostream &output, const string &basename) const
82
83
84
85
86
{
  options_list.writeOutput(output);
  output << "model_info();\n";
}

ferhat's avatar
ferhat committed
87

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
SimulStatement::SimulStatement(const OptionsList &options_list_arg) :
  options_list(options_list_arg)
{
}

void
SimulStatement::checkPass(ModFileStructure &mod_file_struct)
{
  mod_file_struct.simul_present = true;
}

void
SimulStatement::writeOutput(ostream &output, const string &basename) const
{
  options_list.writeOutput(output);
  output << "simul(oo_.dr);\n";
}

SimulSparseStatement::SimulSparseStatement(const OptionsList &options_list_arg,
107
                                           int mode_arg) :
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  options_list(options_list_arg),
  mode(mode_arg)
{
}

void
SimulSparseStatement::checkPass(ModFileStructure &mod_file_struct)
{
  mod_file_struct.simul_present = true;
}

void
SimulSparseStatement::writeOutput(ostream &output, const string &basename) const
{
  options_list.writeOutput(output);
  output << "if (~ options_.initval_file) & (size(oo_.endo_simul,2)<options_.periods)\n";
  output << "  if ~isfield(options_,'datafile')\n";
  output << "    make_y_;\n";
  output << "    make_ex_;\n";
  output << "  else\n";
  output << "    read_data_;\n";
  output << "  end\n";
  output << "end\n";
  if(mode==eSparseDLLMode)
132
    output << "oo_.endo_simul=simulate;\n";
133
134
135
136
137
  else
    {
      //output << "oo_.endo_simul=" << basename << "_dynamic();\n";
      output << basename << "_dynamic;\n";
    }
138
  output << "dyn2vec;\n";
139
140
}

sebastien's avatar
sebastien committed
141
StochSimulStatement::StochSimulStatement(const SymbolList &symbol_list_arg,
142
                                         const OptionsList &options_list_arg) :
sebastien's avatar
sebastien committed
143
  symbol_list(symbol_list_arg),
144
145
146
147
148
149
150
  options_list(options_list_arg)
{
}

void
StochSimulStatement::checkPass(ModFileStructure &mod_file_struct)
{
sebastien's avatar
sebastien committed
151
  mod_file_struct.stoch_simul_present = true;
152
153
154
155

  // Fill in option_order of mod_file_struct
  OptionsList::num_options_type::const_iterator it = options_list.num_options.find("order");
  if (it != options_list.num_options.end())
156
    mod_file_struct.order_option = max(mod_file_struct.order_option,atoi(it->second.c_str()));
sebastien's avatar
trunk:    
sebastien committed
157
158
159
160
161
162
163
164

  // This (temporary) check is present in stoch_simul, osr and ramsey_policy
  if (options_list.num_options.find("simul") != options_list.num_options.end()
      && options_list.num_options.find("hp_filter") != options_list.num_options.end())
    {
      cerr << "ERROR: stoch_simul: HP filter is not yet implemented when computing empirical simulations" << endl;
      exit(EXIT_FAILURE);
    }
165
166
167
168
169
170
}

void
StochSimulStatement::writeOutput(ostream &output, const string &basename) const
{
  options_list.writeOutput(output);
sebastien's avatar
sebastien committed
171
  symbol_list.writeOutput("var_list_", output);
ferhat's avatar
ferhat committed
172
173
174
175
176
  output << "if(options_.model_mode)\n";
  output << "  info = stoch_simul_sparse(var_list_);\n";
  output << "else\n";
  output << "  info = stoch_simul(var_list_);\n";
  output << "end\n";
177
178
}

179
180
181
182
183
184
185
186
187
188
ForecastStatement::ForecastStatement(const SymbolList &symbol_list_arg,
                                         const OptionsList &options_list_arg) :
  symbol_list(symbol_list_arg),
  options_list(options_list_arg)
{
}

void
ForecastStatement::checkPass(ModFileStructure &mod_file_struct)
{
sebastien's avatar
sebastien committed
189
  mod_file_struct.forecast_present = true;
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

  // Fill in option_order of mod_file_struct
  OptionsList::num_options_type::const_iterator it = options_list.num_options.find("order");
  if (it != options_list.num_options.end())
    mod_file_struct.order_option = max(mod_file_struct.order_option,atoi(it->second.c_str()));
}

void
ForecastStatement::writeOutput(ostream &output, const string &basename) const
{
  options_list.writeOutput(output);
  symbol_list.writeOutput("var_list_", output);
  output << "info = forecast(var_list_,'simul');\n";
}

sebastien's avatar
sebastien committed
205
RamseyPolicyStatement::RamseyPolicyStatement(const SymbolList &symbol_list_arg,
206
                                             const OptionsList &options_list_arg) :
sebastien's avatar
sebastien committed
207
  symbol_list(symbol_list_arg),
208
209
210
211
212
213
214
  options_list(options_list_arg)
{
}

void
RamseyPolicyStatement::checkPass(ModFileStructure &mod_file_struct)
{
sebastien's avatar
sebastien committed
215
  mod_file_struct.ramsey_policy_present = true;
216
217
218
219
220
221

  /* Fill in option_order of mod_file_struct
     Since ramsey policy needs one further order of derivation (for example, for 1st order
     approximation, it needs 2nd derivatives), we add 1 to the order declared by user */
  OptionsList::num_options_type::const_iterator it = options_list.num_options.find("order");
  if (it != options_list.num_options.end())
222
223
224
225
226
227
228
229
230
    {
      int order = atoi(it->second.c_str());
      if (order > 1)
        {
          cerr << "ERROR: ramsey_policy: order > 1 is not yet implemented" << endl;
          exit(EXIT_FAILURE);
        }
      mod_file_struct.order_option = max(mod_file_struct.order_option, order + 1);
    }
sebastien's avatar
trunk:    
sebastien committed
231
232
233
234
235
236
237
238

  // This (temporary) check is present in stoch_simul, osr and ramsey_policy
  if (options_list.num_options.find("simul") != options_list.num_options.end()
      && options_list.num_options.find("hp_filter") != options_list.num_options.end())
    {
      cerr << "ERROR: ramsey_policy: HP filter is not yet implemented when computing empirical simulations" << endl;
      exit(EXIT_FAILURE);
    }
239
240
241
242
243
244
}

void
RamseyPolicyStatement::writeOutput(ostream &output, const string &basename) const
{
  options_list.writeOutput(output);
sebastien's avatar
sebastien committed
245
  symbol_list.writeOutput("var_list_", output);
246
247
248
  output << "ramsey_policy(var_list_);\n";
}

sebastien's avatar
sebastien committed
249
EstimationStatement::EstimationStatement(const SymbolList &symbol_list_arg,
250
                                         const OptionsList &options_list_arg) :
sebastien's avatar
sebastien committed
251
  symbol_list(symbol_list_arg),
252
253
254
255
256
257
258
  options_list(options_list_arg)
{
}

void
EstimationStatement::checkPass(ModFileStructure &mod_file_struct)
{
sebastien's avatar
sebastien committed
259
  mod_file_struct.estimation_present = true;
260
261
262
263

  // Fill in option_order of mod_file_struct
  OptionsList::num_options_type::const_iterator it = options_list.num_options.find("order");
  if (it != options_list.num_options.end())
264
    mod_file_struct.order_option = max(mod_file_struct.order_option,atoi(it->second.c_str()));
265
266
267
268
269
270
}

void
EstimationStatement::writeOutput(ostream &output, const string &basename) const
{
  options_list.writeOutput(output);
sebastien's avatar
sebastien committed
271
  symbol_list.writeOutput("var_list_", output);
272
273
274
  output << "dynare_estimation(var_list_);\n";
}

sebastien's avatar
sebastien committed
275
PriorAnalysisStatement::PriorAnalysisStatement(const SymbolList &symbol_list_arg,
276
                                               const OptionsList &options_list_arg) :
sebastien's avatar
sebastien committed
277
  symbol_list(symbol_list_arg),
278
279
280
281
282
283
284
285
  options_list(options_list_arg)
{
}

void
PriorAnalysisStatement::writeOutput(ostream &output, const string &basename) const
{
  options_list.writeOutput(output);
sebastien's avatar
sebastien committed
286
  symbol_list.writeOutput("var_list_", output);
287
288
289
  output << "prior_analysis(var_list_);\n";
}

sebastien's avatar
sebastien committed
290
PosteriorAnalysisStatement::PosteriorAnalysisStatement(const SymbolList &symbol_list_arg,
291
                                                       const OptionsList &options_list_arg) :
sebastien's avatar
sebastien committed
292
  symbol_list(symbol_list_arg),
293
294
295
296
297
298
299
300
  options_list(options_list_arg)
{
}

void
PosteriorAnalysisStatement::writeOutput(ostream &output, const string &basename) const
{
  options_list.writeOutput(output);
sebastien's avatar
sebastien committed
301
  symbol_list.writeOutput("var_list_", output);
302
303
304
305
306
307
308
309
310
311
312
  output << "posterior_analysis(var_list_);\n";
}

DynareSensitivityStatement::DynareSensitivityStatement(const OptionsList &options_list_arg) :
  options_list(options_list_arg)
{
}

void
DynareSensitivityStatement::writeOutput(ostream &output, const string &basename) const
{
313
314
  options_list.writeOutput(output,"options_gsa");
  output << "dynare_sensitivity(options_gsa);" << endl;
315
316
}

sebastien's avatar
sebastien committed
317
RplotStatement::RplotStatement(const SymbolList &symbol_list_arg,
318
                               const OptionsList &options_list_arg) :
sebastien's avatar
sebastien committed
319
  symbol_list(symbol_list_arg),
320
321
322
323
324
325
326
327
  options_list(options_list_arg)
{
}

void
RplotStatement::writeOutput(ostream &output, const string &basename) const
{
  options_list.writeOutput(output);
sebastien's avatar
sebastien committed
328
  symbol_list.writeOutput("var_list_", output);
329
330
331
  output << "rplot(var_list_);\n";
}

sebastien's avatar
sebastien committed
332
333
UnitRootVarsStatement::UnitRootVarsStatement(const SymbolList &symbol_list_arg) :
  symbol_list(symbol_list_arg)
334
335
336
337
338
339
{
}

void
UnitRootVarsStatement::writeOutput(ostream &output, const string &basename) const
{
sebastien's avatar
sebastien committed
340
  symbol_list.writeOutput("options_.unit_root_vars", output);
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
}

PeriodsStatement::PeriodsStatement(int periods_arg) : periods(periods_arg)
{
}

void
PeriodsStatement::writeOutput(ostream &output, const string &basename) const
{
  output << "options_.periods = " << periods << ";" << endl;
  output << "options_.simul = 1;" << endl;
}

CutoffStatement::CutoffStatement(double cutoff_arg) : cutoff(cutoff_arg)
{
}

void
CutoffStatement::writeOutput(ostream &output, const string &basename) const
{
  output << "options_.cutoff = " << cutoff << ";" << endl;
}

MarkowitzStatement::MarkowitzStatement(double markowitz_arg) : markowitz(markowitz_arg)
{
}

void
MarkowitzStatement::writeOutput(ostream &output, const string &basename) const
{
  output << "options_.markowitz = " << markowitz << ";" << endl;
}

DsampleStatement::DsampleStatement(int val1_arg) : val1(val1_arg), val2(-1)
{
}

DsampleStatement::DsampleStatement(int val1_arg, int val2_arg) : val1(val1_arg), val2(val2_arg)
{
}

void
DsampleStatement::writeOutput(ostream &output, const string &basename) const
{
  if (val2 < 0)
    output << "options_.dsample = " << val1 << ";" << endl;
  else
    output << "options_.dsample = [" << val1 << "; " << val2 << "];" << endl;
}

sebastien's avatar
sebastien committed
391
392
VarobsStatement::VarobsStatement(const SymbolList &symbol_list_arg) :
  symbol_list(symbol_list_arg)
393
394
395
396
397
398
{
}

void
VarobsStatement::writeOutput(ostream &output, const string &basename) const
{
sebastien's avatar
sebastien committed
399
  symbol_list.writeOutput("options_.varobs", output);
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
}

EstimatedParamsStatement::EstimatedParamsStatement(const vector<EstimationParams> &estim_params_list_arg,
                                                   const SymbolTable &symbol_table_arg) :
  estim_params_list(estim_params_list_arg),
  symbol_table(symbol_table_arg)
{
}

void
EstimatedParamsStatement::writeOutput(ostream &output, const string &basename) const
{
  output << "global estim_params_\n";
  output << "var_list_ = [];\n";
  output << "estim_params_.var_exo = [];\n";
  output << "estim_params_.var_endo = [];\n";
  output << "estim_params_.corrx = [];\n";
  output << "estim_params_.corrn = [];\n";
  output << "estim_params_.param_names = [];\n";
  output << "estim_params_.user_param_names = [];\n";
  output << "estim_params_.param_vals = [];\n";
  output << "M_.H = 0;\n";

  vector<EstimationParams>::const_iterator it;

  for(it = estim_params_list.begin(); it != estim_params_list.end(); it++)
    {
      switch(it->type)
        {
        case 1:
          if (symbol_table.getType(it->name) == eExogenous)
            output << "estim_params_.var_exo = [estim_params_.var_exo; ";
          else if (symbol_table.getType(it->name) == eEndogenous)
            output << "estim_params_.var_endo = [estim_params_.var_endo; ";
          output << symbol_table.getID(it->name)+1;
          break;
        case 2:
          output << "estim_params_.param_vals = [estim_params_.param_vals; ";
          output << symbol_table.getID(it->name)+1;
          break;
        case 3:
          if (symbol_table.getType(it->name) == eExogenous)
            output << "estim_params_.corrx = [estim_params_.corrx; ";
          else if (symbol_table.getType(it->name) == eEndogenous)
            output << "estim_params_.corrn = [estim_params_.corrn; ";
          output << symbol_table.getID(it->name)+1;
          output << " " << symbol_table.getID(it->name2)+1;
          break;
        }
      output << " " << it->init_val << " " <<  it->low_bound
             << " " << it->up_bound << " " <<  it->prior
             << " " << it->mean << " " <<  it->std
             << " " << it->p3 << " " <<  it->p4  << " " <<  it->jscale << "];\n";
    }
}

EstimatedParamsInitStatement::EstimatedParamsInitStatement(const vector<EstimationParams> &estim_params_list_arg,
                                                           const SymbolTable &symbol_table_arg) :
  estim_params_list(estim_params_list_arg),
  symbol_table(symbol_table_arg)
{
}

void
EstimatedParamsInitStatement::writeOutput(ostream &output, const string &basename) const
{
  vector<EstimationParams>::const_iterator it;

  for(it = estim_params_list.begin(); it != estim_params_list.end(); it++)
    {
      if (it->type < 3)
        {
          if (symbol_table.getType(it->name) == eExogenous)
            {
              output << "tmp1 = find(estim_params_.var_exo(:,1)==" << symbol_table.getID(it->name)+1 << ");\n";
              output << "estim_params_.var_exo(tmp1,2) = " << it->init_val << ";\n";
            }
          else if (symbol_table.getType(it->name) == eEndogenous)
            {
              output << "tmp1 = find(estim_params_.var_endo(:,1)==" << symbol_table.getID(it->name)+1 << ");\n";
              output << "estim_params_.var_endo(tmp1,2) = " << it->init_val << ";\n";
            }
          else if (symbol_table.getType(it->name) == eParameter)
            {
              output << "tmp1 = find(estim_params_.param_vals(:,1)==" << symbol_table.getID(it->name)+1 << ");\n";
              output << "estim_params_.param_vals(tmp1,2) = " << it->init_val << ";\n";
            }
        }
      else
        {
          if (symbol_table.getType(it->name) == eExogenous)
            {
              output << "tmp1 = find((estim_params_.corrx(:,1)==" << symbol_table.getID(it->name)+1 << ")) & (estim_params_.corrx(:,2)==" << symbol_table.getID(it->name2)+1 << ");\n";
              output << "estim_params_.corrx(tmp1,3) = " << it->init_val << ";\n";
            }
          else if (symbol_table.getType(it->name) == eEndogenous)
            {
              output << "tmp1 = find((estim_params_.corrn(:,1)==" << symbol_table.getID(it->name)+1 << ")) & (estim_params_.corrn(:,2)==" << symbol_table.getID(it->name2)+1 << ";\n";
              output << "estim_params_.corrx(tmp1,3) = " << it->init_val << ";\n";
            }
        }
    }
}

EstimatedParamsBoundsStatement::EstimatedParamsBoundsStatement(const vector<EstimationParams> &estim_params_list_arg,
                                                               const SymbolTable &symbol_table_arg) :
  estim_params_list(estim_params_list_arg),
  symbol_table(symbol_table_arg)
{
}

void
EstimatedParamsBoundsStatement::writeOutput(ostream &output, const string &basename) const
{
  vector<EstimationParams>::const_iterator it;

  for(it = estim_params_list.begin(); it != estim_params_list.end(); it++)
    {
      if (it->type < 3)
        {
          if (symbol_table.getType(it->name) == eExogenous)
            {
              output << "tmp1 = find(estim_params_.var_exo(:,1)==" << symbol_table.getID(it->name)+1 << ");\n";
              output << "estim_params_.var_exo(tmp1,3) = " << it->low_bound << ";\n";
              output << "estim_params_.var_exo(tmp1,4) = " << it->up_bound << ";\n";
            }
          else if (symbol_table.getType(it->name) == eEndogenous)
            {
              output << "tmp1 = find(estim_params_.var_endo(:,1)==" << symbol_table.getID(it->name)+1 << ");\n";
              output << "estim_params_.var_endo(tmp1,3) = " << it->low_bound << ";\n";
              output << "estim_params_.var_endo(tmp1,4) = " << it->up_bound << ";\n";
            }
          else if (symbol_table.getType(it->name) == eParameter)
            {
              output << "tmp1 = find(estim_params_.param_vals(:,1)==" << symbol_table.getID(it->name)+1 << ");\n";
              output << "estim_params_.param_vals(tmp1,3) = " << it->low_bound << ";\n";
              output << "estim_params_.param_vals(tmp1,4) = " << it->up_bound << ";\n";
            }
        }
      else
        {
          if (symbol_table.getType(it->name) == eExogenous)
            {
              output << "tmp1 = find((estim_params_.corrx(:,1)==" << symbol_table.getID(it->name)+1 << ")) & (estim_params_.corrx(:,2)==" << symbol_table.getID(it->name2)+1 << ");\n";
              output << "estim_params_.corrx(tmp1,4) = " << it->low_bound << ";\n";
              output << "estim_params_.corrx(tmp1,5) = " << it->up_bound << ";\n";
            }
          else if (symbol_table.getType(it->name) == eEndogenous)
            {
              output << "tmp1 = find((estim_params_.corrn(:,1)==" << symbol_table.getID(it->name)+1 << ")) & (estim_params_.corrn(:,2)==" << symbol_table.getID(it->name2)+1 << ";\n";
              output << "estim_params_.corrx(tmp1,4) = " << it->low_bound << ";\n";
              output << "estim_params_.corrx(tmp1,5) = " << it->up_bound << ";\n";
            }
        }
    }
}

ObservationTrendsStatement::ObservationTrendsStatement(const trend_elements_type &trend_elements_arg,
                                                       const SymbolTable &symbol_table_arg) :
  trend_elements(trend_elements_arg),
  symbol_table(symbol_table_arg)
{
}

void
ObservationTrendsStatement::writeOutput(ostream &output, const string &basename) const
{
  output << "options_.trend_coeff_ = {};" << endl;

  trend_elements_type::const_iterator it;

  for(it = trend_elements.begin(); it != trend_elements.end(); it++)
    {
573
      SymbolType type = symbol_table.getType(it->first);
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
      if (type == eEndogenous)
        {
          output << "tmp1 = strmatch('" << it->first << "',options_.varobs,'exact');\n";
          output << "options_.trend_coeffs{tmp1} = '";
          it->second->writeOutput(output);
          output << "';" << endl;
        }
      else
        cout << "Error : Non-variable symbol used in TREND_COEFF: " << it->first << endl;
    }
}

CalibVarStatement::CalibVarStatement(const calib_var_type &calib_var_arg,
                                     const calib_covar_type &calib_covar_arg,
                                     const calib_ac_type &calib_ac_arg,
                                     const SymbolTable &symbol_table_arg) :
  calib_var(calib_var_arg),
  calib_covar(calib_covar_arg),
  calib_ac(calib_ac_arg),
  symbol_table(symbol_table_arg)
{
}

void
CalibVarStatement::writeOutput(ostream &output, const string &basename) const
{

sebastien's avatar
sebastien committed
601
602
603
  output << "%" << endl
         << "% CALIB_VAR" << endl
         << "%" << endl;
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710

  for(int i = 1; i < 4 ; i++)
    {
      output << "calib_var_index{" << i << "} = [];\n";
      output << "calib_targets{" << i << "} = [];\n";
      output << "calib_weights{" << i << "}=[];\n";
    }

  // Print calibration variances
  for(calib_var_type::const_iterator it = calib_var.begin();
      it != calib_var.end(); it++)
    {
      const string &name = it->first;
      const string &weight = it->second.first;
      const NodeID expression = it->second.second;

      int id = symbol_table.getID(name) + 1;
      if (symbol_table.getType(name) == eEndogenous)
        {
          output << "calib_var_index{1} = [calib_var_index{1};" <<  id << "," << id << "];\n";
          output << "calib_weights{1} = [calib_weights{1}; " << weight << "];\n";
          output << "calib_targets{1} =[calib_targets{1}; ";
          expression->writeOutput(output);
          output << "];\n";
        }
      else if (symbol_table.getType(name) == eExogenous)
        {
          output << "calib_var_index{3} = [calib_var_index{3};" <<  id << "," << id << "];\n";
          output << "calib_weights{3} = [calib_weights{3}; " << weight << "];\n";
          output << "calib_targets{3} =[calib_targets{3}; ";
          expression->writeOutput(output);
          output << "];\n";
        }
    }

  // Print calibration covariances
  for(calib_covar_type::const_iterator it = calib_covar.begin();
      it != calib_covar.end(); it++)
    {
      const string &name1 = it->first.first;
      const string &name2 = it->first.second;
      const string &weight = it->second.first;
      const NodeID expression = it->second.second;

      int id1 = symbol_table.getID(name1) + 1;
      int id2 = symbol_table.getID(name2) + 1;
      if (symbol_table.getType(name1) == eEndogenous)
        {
          output << "calib_var_index{1} = [calib_var_index{1};" <<  id1 << "," << id2 << "];\n";
          output << "calib_weights{1} = [calib_weights{1}; " << weight << "];\n";
          output << "calib_targets{1} =[calib_targets{1}; ";
          expression->writeOutput(output);
          output << "];\n";
        }
      else if (symbol_table.getType(name1) == eExogenous)
        {
          output << "calib_var_index{3} = [calib_var_index{3};" <<  id1 << "," << id2 << "];\n";
          output << "calib_weights{3} = [calib_weights{3}; " << weight << "];\n";
          output << "calib_targets{3} =[calib_targets{3}; ";
          expression->writeOutput(output);
          output << "];\n";
        }
    }

  // Print calibration autocorrelations
  int max_iar = 3;

  for(calib_ac_type::const_iterator it = calib_ac.begin();
      it != calib_ac.end(); it++)
    {
      const string &name = it->first.first;
      int iar = it->first.second + 3;
      const string &weight = it->second.first;
      const NodeID expression = it->second.second;

      int id = symbol_table.getID(name) + 1;

      if (iar > max_iar)
        {
          // Create new variables
          for(int i = max_iar + 1; i <= iar; i++)
            {
              output << "calib_var_index{" << i << "} = [];\n";
              output << "calib_targets{" << i << "} = [];\n";
              output << "calib_weights{" << i << "}=[];\n";
            }
          max_iar = iar;
        }

      output << "calib_var_index{" << iar << "} = [calib_var_index{" << iar << "};" <<  id << "];\n";
      output << "calib_weights{" << iar << "} = [calib_weights{" << iar << "}; " << weight << "];\n";
      output << "calib_targets{" << iar << "} =[calib_targets{" << iar << "}; ";
      expression->writeOutput(output);
      output << "];\n";
    }
}

CalibStatement::CalibStatement(int covar_arg) : covar(covar_arg)
{
}

void
CalibStatement::writeOutput(ostream &output, const string &basename) const
{
  output << "M_.Sigma_e=calib(calib_var_index,calib_targets,calib_weights," << covar << ",Sigma_e_);\n";
}

sebastien's avatar
sebastien committed
711
712
OsrParamsStatement::OsrParamsStatement(const SymbolList &symbol_list_arg) :
  symbol_list(symbol_list_arg)
713
714
715
716
717
718
{
}

void
OsrParamsStatement::writeOutput(ostream &output, const string &basename) const
{
sebastien's avatar
sebastien committed
719
  symbol_list.writeOutput("osr_params_", output);
720
721
}

sebastien's avatar
sebastien committed
722
OsrStatement::OsrStatement(const SymbolList &symbol_list_arg,
723
                           const OptionsList &options_list_arg) :
sebastien's avatar
sebastien committed
724
  symbol_list(symbol_list_arg),
725
726
727
728
729
730
731
  options_list(options_list_arg)
{
}

void
OsrStatement::checkPass(ModFileStructure &mod_file_struct)
{
sebastien's avatar
sebastien committed
732
  mod_file_struct.osr_present = true;
733
734
735
736

  // Fill in option_order of mod_file_struct
  OptionsList::num_options_type::const_iterator it = options_list.num_options.find("order");
  if (it != options_list.num_options.end())
737
    mod_file_struct.order_option = max(mod_file_struct.order_option,atoi(it->second.c_str()));
sebastien's avatar
trunk:    
sebastien committed
738
739
740
741
742
743
744
745

  // This (temporary) check is present in stoch_simul, osr and ramsey_policy
  if (options_list.num_options.find("simul") != options_list.num_options.end()
      && options_list.num_options.find("hp_filter") != options_list.num_options.end())
    {
      cerr << "ERROR: osr: HP filter is not yet implemented when computing empirical simulations" << endl;
      exit(EXIT_FAILURE);
    }
746
747
748
749
750
751
}

void
OsrStatement::writeOutput(ostream &output, const string &basename) const
{
  options_list.writeOutput(output);
sebastien's avatar
sebastien committed
752
  symbol_list.writeOutput("var_list_", output);
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
  output << "osr(var_list_,osr_params_,obj_var_,optim_weights_);\n";
}

OptimWeightsStatement::OptimWeightsStatement(const var_weights_type &var_weights_arg,
                                             const covar_weights_type &covar_weights_arg,
                                             const SymbolTable &symbol_table_arg) :
  var_weights(var_weights_arg),
  covar_weights(covar_weights_arg),
  symbol_table(symbol_table_arg)
{
}

void
OptimWeightsStatement::writeOutput(ostream &output, const string &basename) const
{
sebastien's avatar
sebastien committed
768
769
770
771
772
  output << "%" << endl
         << "% OPTIM_WEIGHTS" << endl
         << "%" << endl
         << "optim_weights_ = sparse(M_.endo_nbr,M_.endo_nbr);" << endl
         << "obj_var_ = [];" << endl << endl;
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800

  for(var_weights_type::const_iterator it = var_weights.begin();
      it != var_weights.end(); it++)
    {
      const string &name = it->first;
      const NodeID value = it->second;
      int id = symbol_table.getID(name) + 1;
      output <<  "optim_weights_(" << id << "," << id << ") = ";
      value->writeOutput(output);
      output << ";" << endl;
      output << "obj_var_ = [obj_var_; " << id << "];\n";
    }

  for(covar_weights_type::const_iterator it = covar_weights.begin();
      it != covar_weights.end(); it++)
    {
      const string &name1 = it->first.first;
      const string &name2 = it->first.second;
      const NodeID value = it->second;
      int id1 = symbol_table.getID(name1) + 1;
      int id2 = symbol_table.getID(name2) + 1;
      output <<  "optim_weights_(" << id1 << "," << id2 << ") = ";
      value->writeOutput(output);
      output << ";" << endl;
      output << "obj_var_ = [obj_var_; " << id1 << " " << id2 << "];\n";
    }
}

sebastien's avatar
sebastien committed
801
DynaSaveStatement::DynaSaveStatement(const SymbolList &symbol_list_arg,
sebastien's avatar
trunk:    
sebastien committed
802
                                     const string &filename_arg) :
sebastien's avatar
sebastien committed
803
  symbol_list(symbol_list_arg),
sebastien's avatar
trunk:    
sebastien committed
804
  filename(filename_arg)
805
806
807
808
809
810
{
}

void
DynaSaveStatement::writeOutput(ostream &output, const string &basename) const
{
sebastien's avatar
sebastien committed
811
  symbol_list.writeOutput("var_list_", output);
sebastien's avatar
trunk:    
sebastien committed
812
813
  output << "dynasave('" << filename
         << "',var_list_);" << endl;
814
815
}

sebastien's avatar
sebastien committed
816
DynaTypeStatement::DynaTypeStatement(const SymbolList &symbol_list_arg,
sebastien's avatar
trunk:    
sebastien committed
817
                                     const string &filename_arg) :
sebastien's avatar
sebastien committed
818
  symbol_list(symbol_list_arg),
sebastien's avatar
trunk:    
sebastien committed
819
  filename(filename_arg)
820
821
822
823
824
825
{
}

void
DynaTypeStatement::writeOutput(ostream &output, const string &basename) const
{
sebastien's avatar
sebastien committed
826
  symbol_list.writeOutput("var_list_", output);
sebastien's avatar
trunk:    
sebastien committed
827
828
  output << "dynatype('" << filename
         << "',var_list_);" << endl;
829
830
831
832
833
834
835
836
837
838
839
840
841
842
}

ModelComparisonStatement::ModelComparisonStatement(const filename_list_type &filename_list_arg,
                                                   const OptionsList &options_list_arg) :
  filename_list(filename_list_arg),
  options_list(options_list_arg)
{
}

void
ModelComparisonStatement::writeOutput(ostream &output, const string &basename) const
{
  options_list.writeOutput(output);

sebastien's avatar
trunk:    
sebastien committed
843
844
  output << "ModelNames_ = {};" << endl;
  output << "ModelPriors_ = [];" << endl;
845
846
847
848

  for(filename_list_type::const_iterator it = filename_list.begin();
      it != filename_list.end(); it++)
    {
sebastien's avatar
trunk:    
sebastien committed
849
850
      output << "ModelNames_ = { ModelNames_{:} '" << (*it).first << "'};" << endl;
      output << "ModelPriors_ = [ ModelPriors_ ; " << (*it).second << "];" << endl;
851
    }
sebastien's avatar
trunk:    
sebastien committed
852
  output << "model_comparison(ModelNames_,ModelPriors_,oo_,options_,M_.fname);" << endl;
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
}

PlannerObjectiveStatement::PlannerObjectiveStatement(ModelTree *model_tree_arg) :
  model_tree(model_tree_arg)
{
}

PlannerObjectiveStatement::~PlannerObjectiveStatement()
{
  delete model_tree;
}

void
PlannerObjectiveStatement::checkPass(ModFileStructure &mod_file_struct)
{
  if (model_tree->equation_number() != 1)
    {
870
      cerr << "ERROR: planer_objective: should have only one equation!" << endl;
871
      exit(EXIT_FAILURE);
872
873
874
875
876
877
878
    }
}

void
PlannerObjectiveStatement::computingPass()
{
  model_tree->computeStaticHessian = true;
879
  model_tree->computingPass(eval_context_type(), false);
880
881
882
883
884
885
886
887
888
889
890
891
892
893
}

void
PlannerObjectiveStatement::writeOutput(ostream &output, const string &basename) const
{
  model_tree->writeStaticFile(basename + "_objective");
}

BVARDensityStatement::BVARDensityStatement(int maxnlags_arg, const OptionsList &options_list_arg) :
  maxnlags(maxnlags_arg),
  options_list(options_list_arg)
{
}

894
895
896
897
898
899
void
BVARDensityStatement::checkPass(ModFileStructure &mod_file_struct)
{
  mod_file_struct.bvar_density_present = true;
}

900
901
902
903
904
905
906
907
908
909
910
911
912
void
BVARDensityStatement::writeOutput(ostream &output, const string &basename) const
{
  options_list.writeOutput(output);
  output << "bvar_density(" << maxnlags << ");" << endl;
}

BVARForecastStatement::BVARForecastStatement(int nlags_arg, const OptionsList &options_list_arg) :
  nlags(nlags_arg),
  options_list(options_list_arg)
{
}

913
914
915
916
917
918
void
BVARForecastStatement::checkPass(ModFileStructure &mod_file_struct)
{
  mod_file_struct.bvar_forecast_present = true;
}

919
920
921
922
923
924
void
BVARForecastStatement::writeOutput(ostream &output, const string &basename) const
{
  options_list.writeOutput(output);
  output << "bvar_forecast(" << nlags << ");" << endl;
}