ComputingTasks.cc 84.4 KB
Newer Older
sebastien's avatar
sebastien committed
1
/*
2
 * Copyright (C) 2003-2012 Dynare Team
sebastien's avatar
sebastien committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 *
 * 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/>.
18
 */
sebastien's avatar
sebastien committed
19

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

using namespace std;
sebastien's avatar
sebastien committed
26

27
#include "ComputingTasks.hh"
sebastien's avatar
sebastien committed
28
29
#include "Statement.hh"

30
31
32
33
34
#include <boost/algorithm/string/trim.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/tokenizer.hpp>

35
36
SteadyStatement::SteadyStatement(const OptionsList &options_list_arg) :
  options_list(options_list_arg)
sebastien's avatar
sebastien committed
37
38
39
{
}

40
void
41
SteadyStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings)
42
{
43
  mod_file_struct.steady_present = true;
44
45
}

sebastien's avatar
sebastien committed
46
void
sebastien's avatar
sebastien committed
47
SteadyStatement::writeOutput(ostream &output, const string &basename) const
sebastien's avatar
sebastien committed
48
49
50
51
52
53
54
55
56
57
58
{
  options_list.writeOutput(output);
  output << "steady;\n";
}

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

void
sebastien's avatar
sebastien committed
59
CheckStatement::writeOutput(ostream &output, const string &basename) const
60
{
sebastien's avatar
sebastien committed
61
  options_list.writeOutput(output);
62
  output << "check(M_,options_,oo_);\n";
63
64
}

65
void
66
CheckStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings)
67
68
69
70
{
  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)
{
}

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

82
83
void
ModelInfoStatement::writeOutput(ostream &output, const string &basename) const
84
85
86
87
88
{
  options_list.writeOutput(output);
  output << "model_info();\n";
}

89
90
SimulStatement::SimulStatement(const OptionsList &options_list_arg) :
  options_list(options_list_arg)
91
92
93
{
}

sebastien's avatar
sebastien committed
94
void
95
SimulStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings)
sebastien's avatar
sebastien committed
96
97
{
  mod_file_struct.simul_present = true;
98
99
100

  // The following is necessary to allow shocks+endval+simul in a loop
  mod_file_struct.shocks_present_but_simul_not_yet = false;
sebastien's avatar
sebastien committed
101
102
}

sebastien's avatar
sebastien committed
103
void
sebastien's avatar
sebastien committed
104
SimulStatement::writeOutput(ostream &output, const string &basename) const
105
{
sebastien's avatar
sebastien committed
106
  options_list.writeOutput(output);
107
  output << "simul();\n";
sebastien's avatar
sebastien committed
108
109
}

sebastien's avatar
sebastien committed
110
StochSimulStatement::StochSimulStatement(const SymbolList &symbol_list_arg,
111
                                         const OptionsList &options_list_arg) :
sebastien's avatar
sebastien committed
112
  symbol_list(symbol_list_arg),
113
  options_list(options_list_arg)
114
115
116
{
}

sebastien's avatar
sebastien committed
117
void
118
StochSimulStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings)
sebastien's avatar
sebastien committed
119
{
sebastien's avatar
sebastien committed
120
  mod_file_struct.stoch_simul_present = true;
sebastien's avatar
sebastien committed
121
122

  // Fill in option_order of mod_file_struct
123
  OptionsList::num_options_t::const_iterator it = options_list.num_options.find("order");
sebastien's avatar
sebastien committed
124
  if (it != options_list.num_options.end())
125
    mod_file_struct.order_option = max(mod_file_struct.order_option, atoi(it->second.c_str()));
sebastien's avatar
trunk:    
sebastien committed
126

127
128
129
130
  // Fill in mod_file_struct.partial_information
  it = options_list.num_options.find("partial_information");
  if (it != options_list.num_options.end() && it->second == "1")
    mod_file_struct.partial_information = true;
sebastien's avatar
sebastien committed
131
132
133
134
135
136

  // Option k_order_solver (implicit when order >= 3)
  it = options_list.num_options.find("k_order_solver");
  if ((it != options_list.num_options.end() && it->second == "1")
      || mod_file_struct.order_option >= 3)
    mod_file_struct.k_order_solver = true;
137
138
139
140
141
142
143
144

  // Check that option "pruning" is not used with k-order
  it = options_list.num_options.find("pruning");
  if ((it != options_list.num_options.end() && it->second == "1")
      && mod_file_struct.k_order_solver)
    {
      cerr << "ERROR: in 'stoch_simul', you cannot use option 'pruning' with 'k_order_solver' option or with 3rd order approximation" << endl;
      exit(EXIT_FAILURE);
145
    }
sebastien's avatar
sebastien committed
146
147
}

sebastien's avatar
sebastien committed
148
void
sebastien's avatar
sebastien committed
149
StochSimulStatement::writeOutput(ostream &output, const string &basename) const
150
{
sebastien's avatar
sebastien committed
151
  options_list.writeOutput(output);
sebastien's avatar
sebastien committed
152
  symbol_list.writeOutput("var_list_", output);
153
  output << "info = stoch_simul(var_list_);" << endl;
154
155
}

156
ForecastStatement::ForecastStatement(const SymbolList &symbol_list_arg,
157
                                     const OptionsList &options_list_arg) :
158
159
160
161
162
163
164
165
166
167
  symbol_list(symbol_list_arg),
  options_list(options_list_arg)
{
}

void
ForecastStatement::writeOutput(ostream &output, const string &basename) const
{
  options_list.writeOutput(output);
  symbol_list.writeOutput("var_list_", output);
168
  output << "info = dyn_forecast(var_list_,'simul');" << endl;
169
170
}

sebastien's avatar
sebastien committed
171
RamseyPolicyStatement::RamseyPolicyStatement(const SymbolList &symbol_list_arg,
sebastien's avatar
sebastien committed
172
                                             const OptionsList &options_list_arg) :
sebastien's avatar
sebastien committed
173
  symbol_list(symbol_list_arg),
michel's avatar
michel committed
174
175
176
177
178
  options_list(options_list_arg)
{
}

void
179
RamseyPolicyStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings)
michel's avatar
michel committed
180
{
sebastien's avatar
sebastien committed
181
  mod_file_struct.ramsey_policy_present = true;
sebastien's avatar
sebastien committed
182

sebastien's avatar
sebastien committed
183
184
185
  /* 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 */
186
  OptionsList::num_options_t::const_iterator it = options_list.num_options.find("order");
sebastien's avatar
sebastien committed
187
  if (it != options_list.num_options.end())
188
189
190
191
192
193
194
195
196
    {
      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
197

198
199
200
201
  // Fill in mod_file_struct.partial_information
  it = options_list.num_options.find("partial_information");
  if (it != options_list.num_options.end() && it->second == "1")
    mod_file_struct.partial_information = true;
sebastien's avatar
sebastien committed
202
203
204
205
206
207

  // Option k_order_solver (implicit when order >= 3)
  it = options_list.num_options.find("k_order_solver");
  if ((it != options_list.num_options.end() && it->second == "1")
      || mod_file_struct.order_option >= 3)
    mod_file_struct.k_order_solver = true;
michel's avatar
michel committed
208
209
210
211
212
213
}

void
RamseyPolicyStatement::writeOutput(ostream &output, const string &basename) const
{
  options_list.writeOutput(output);
sebastien's avatar
sebastien committed
214
  symbol_list.writeOutput("var_list_", output);
215
  output << "ramsey_policy(var_list_);\n";
michel's avatar
michel committed
216
217
}

218
219
220
221
222
223
224
225
DiscretionaryPolicyStatement::DiscretionaryPolicyStatement(const SymbolList &symbol_list_arg,
							   const OptionsList &options_list_arg) :
  symbol_list(symbol_list_arg),
  options_list(options_list_arg)
{
}

void
226
DiscretionaryPolicyStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings)
227
228
229
{
  mod_file_struct.discretionary_policy_present = true;

230
231
232
233
234
235
  if (options_list.symbol_list_options.find("instruments") == options_list.symbol_list_options.end())
    {
      cerr << "ERROR: discretionary_policy: the instruments option is required." << endl;
      exit(EXIT_FAILURE);
    }

236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
  /* Fill in option_order of mod_file_struct
     Since discretionary 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_t::const_iterator it = options_list.num_options.find("order");
  if (it != options_list.num_options.end())
    {
      int order = atoi(it->second.c_str());
      if (order > 1)
        {
          cerr << "ERROR: discretionary_policy: order > 1 is not yet implemented" << endl;
          exit(EXIT_FAILURE);
        }
      mod_file_struct.order_option = max(mod_file_struct.order_option, order + 1);
    }

  // Fill in mod_file_struct.partial_information
  it = options_list.num_options.find("partial_information");
  if (it != options_list.num_options.end() && it->second == "1")
    mod_file_struct.partial_information = true;

  // Option k_order_solver (implicit when order >= 3)
  it = options_list.num_options.find("k_order_solver");
  if ((it != options_list.num_options.end() && it->second == "1")
      || mod_file_struct.order_option >= 3)
    mod_file_struct.k_order_solver = true;
}

void
DiscretionaryPolicyStatement::writeOutput(ostream &output, const string &basename) const
{
  options_list.writeOutput(output);
  symbol_list.writeOutput("var_list_", output);
  output << "discretionary_policy(var_list_);\n";
}

sebastien's avatar
sebastien committed
271
EstimationStatement::EstimationStatement(const SymbolList &symbol_list_arg,
272
273
                                         const OptionsList &options_list_arg,
                                         const SymbolTable &symbol_table_arg) :
sebastien's avatar
sebastien committed
274
  symbol_list(symbol_list_arg),
275
276
  options_list(options_list_arg),
  symbol_table(symbol_table_arg)
277
278
279
{
}

sebastien's avatar
sebastien committed
280
void
281
EstimationStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings)
sebastien's avatar
sebastien committed
282
{
sebastien's avatar
sebastien committed
283
  mod_file_struct.estimation_present = true;
sebastien's avatar
sebastien committed
284
285

  // Fill in option_order of mod_file_struct
286
  OptionsList::num_options_t::const_iterator it = options_list.num_options.find("order");
sebastien's avatar
sebastien committed
287
  if (it != options_list.num_options.end())
288
289
290
291
292
293
294
295
296
297
298
299
    {
      int order = atoi(it->second.c_str());
          
      if (order > 2)
        {
          cerr << "ERROR: order > 2 is not supported in estimation" << endl;
          exit(EXIT_FAILURE);
        }
      
      mod_file_struct.order_option = max(mod_file_struct.order_option, order);
    }
  
300
301
302
303
  // Fill in mod_file_struct.partial_information
  it = options_list.num_options.find("partial_information");
  if (it != options_list.num_options.end() && it->second == "1")
    mod_file_struct.partial_information = true;
304

305
306
307
308
309
  // Fill in mod_file_struct.estimation_analytic_derivation
  it = options_list.num_options.find("analytic_derivation");
  if (it != options_list.num_options.end() && it->second == "1")
    mod_file_struct.estimation_analytic_derivation = true;

310
311
  it = options_list.num_options.find("dsge_var");
  if (it != options_list.num_options.end())
312
313
314
315
316
317
318
319
320
    // Ensure that irf_shocks & dsge_var have not both been passed
    if (options_list.symbol_list_options.find("irf_shocks") != options_list.symbol_list_options.end())
      {
        cerr << "The irf_shocks and dsge_var options may not both be passed to estimation." << endl;
        exit(EXIT_FAILURE);
      }
    else
      // Fill in mod_file_struct.dsge_var_calibrated
      mod_file_struct.dsge_var_calibrated = it->second;
321
322

  // Fill in mod_file_struct.dsge_var_estimated
323
  OptionsList::string_options_t::const_iterator it_str = options_list.string_options.find("dsge_var");
324
325
326
327
328
329
330
331
332
333
  if (it_str != options_list.string_options.end())
    mod_file_struct.dsge_var_estimated = true;

  // Fill in mod_file_struct.bayesian_irf_present
  it = options_list.num_options.find("bayesian_irf");
  if (it != options_list.num_options.end() && it->second == "1")
    mod_file_struct.bayesian_irf_present = true;

  it = options_list.num_options.find("dsge_varlag");
  if (it != options_list.num_options.end())
334
335
    if (mod_file_struct.dsge_var_calibrated.empty()
        && !mod_file_struct.dsge_var_estimated)
336
337
338
339
340
341
      {
        cerr << "ERROR: The estimation statement requires a dsge_var option to be passed "
             << "if the dsge_varlag option is passed." << endl;
        exit(EXIT_FAILURE);
      }

342
343
  if (!mod_file_struct.dsge_var_calibrated.empty()
      && mod_file_struct.dsge_var_estimated)
344
345
346
347
    {
      cerr << "ERROR: An estimation statement cannot take more than one dsge_var option." << endl;
      exit(EXIT_FAILURE);
    }
348
349
350
351

  if (options_list.string_options.find("datafile") == options_list.string_options.end() &&
      !mod_file_struct.estimation_data_statement_present)
    {
352
      cerr << "ERROR: The estimation statement requires a data file to be supplied via the datafile option." << endl;
353
354
      exit(EXIT_FAILURE);
    }
sebastien's avatar
sebastien committed
355
356
}

sebastien's avatar
sebastien committed
357
void
sebastien's avatar
sebastien committed
358
EstimationStatement::writeOutput(ostream &output, const string &basename) const
359
{
sebastien's avatar
sebastien committed
360
  options_list.writeOutput(output);
361
362
363
364
365
366
367
368

  // Special treatment for order option and particle filter
  OptionsList::num_options_t::const_iterator it = options_list.num_options.find("order");
  if (it == options_list.num_options.end())
    output << "options_.order = 1;" << endl;
  else if (atoi(it->second.c_str()) == 2)
    output << "options_.particle.status = 1;" << endl;

sebastien's avatar
sebastien committed
369
  symbol_list.writeOutput("var_list_", output);
sebastien's avatar
sebastien committed
370
  output << "dynare_estimation(var_list_);\n";
371
372
}

373
374
375
376
377
DynareSensitivityStatement::DynareSensitivityStatement(const OptionsList &options_list_arg) :
  options_list(options_list_arg)
{
}

sebastien's avatar
sebastien committed
378
void
379
DynareSensitivityStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings)
sebastien's avatar
sebastien committed
380
{
381
  OptionsList::num_options_t::const_iterator it = options_list.num_options.find("identification");
sebastien's avatar
sebastien committed
382
383
384
385
386
  if (it != options_list.num_options.end()
      && it->second == "1")
    mod_file_struct.identification_present = true;
}

387
388
389
void
DynareSensitivityStatement::writeOutput(ostream &output, const string &basename) const
{
390
  options_list.writeOutput(output, "options_gsa");
391
392
393
394
395
396
397

  /* Ensure that nograph, nodisplay and graph_format are also set in top-level
     options_.
     \todo factorize this code between identification and dynare_sensitivity,
     and provide a generic mechanism for this situation (maybe using regexps) */
  OptionsList::num_options_t::const_iterator it = options_list.num_options.find("nodisplay");
  if (it != options_list.num_options.end())
398
    output << "options_.nodisplay = " << it->second << ";" << endl;
399
400
  it = options_list.num_options.find("nograph");
  if (it != options_list.num_options.end())
401
    output << "options_.nograph = " << it->second << ";" << endl;
402
403
  OptionsList::string_options_t::const_iterator it2 = options_list.string_options.find("graph_format");
  if (it2 != options_list.string_options.end())
404
    output << "options_.graph_format = '" << it2->second << "';" << endl;
405
  
406
  output << "dynare_sensitivity(options_gsa);" << endl;
407
408
}

sebastien's avatar
sebastien committed
409
RplotStatement::RplotStatement(const SymbolList &symbol_list_arg,
sebastien's avatar
sebastien committed
410
                               const OptionsList &options_list_arg) :
sebastien's avatar
sebastien committed
411
  symbol_list(symbol_list_arg),
sebastien's avatar
sebastien committed
412
  options_list(options_list_arg)
413
414
415
{
}

sebastien's avatar
sebastien committed
416
void
sebastien's avatar
sebastien committed
417
RplotStatement::writeOutput(ostream &output, const string &basename) const
418
{
sebastien's avatar
sebastien committed
419
  options_list.writeOutput(output);
sebastien's avatar
sebastien committed
420
  symbol_list.writeOutput("var_list_", output);
sebastien's avatar
sebastien committed
421
  output << "rplot(var_list_);\n";
422
423
}

424
425
426
427
428
429
430
431
432
433
434
UnitRootVarsStatement::UnitRootVarsStatement(void)
{
}

void
UnitRootVarsStatement::writeOutput(ostream &output, const string &basename) const
{
  output << "options_.diffuse_filter = 1;" << endl
	 << "options_.steadystate.nocheck = 1;" << endl;
}

sebastien's avatar
sebastien committed
435
PeriodsStatement::PeriodsStatement(int periods_arg) : periods(periods_arg)
michel's avatar
michel committed
436
437
438
{
}

sebastien's avatar
sebastien committed
439
void
sebastien's avatar
sebastien committed
440
PeriodsStatement::writeOutput(ostream &output, const string &basename) const
michel's avatar
michel committed
441
{
sebastien's avatar
sebastien committed
442
  output << "options_.periods = " << periods << ";" << endl;
michel's avatar
michel committed
443
444
}

sebastien's avatar
sebastien committed
445
DsampleStatement::DsampleStatement(int val1_arg) : val1(val1_arg), val2(-1)
446
447
448
{
}

sebastien's avatar
sebastien committed
449
DsampleStatement::DsampleStatement(int val1_arg, int val2_arg) : val1(val1_arg), val2(val2_arg)
450
451
452
{
}

sebastien's avatar
sebastien committed
453
void
sebastien's avatar
sebastien committed
454
DsampleStatement::writeOutput(ostream &output, const string &basename) const
455
{
sebastien's avatar
sebastien committed
456
  if (val2 < 0)
457
    output << "dsample(" << val1 << ");" << endl;
sebastien's avatar
sebastien committed
458
  else
459
    output << "dsample(" << val1 << ", " << val2 << ");" << endl;
sebastien's avatar
sebastien committed
460
461
462
463
464
465
466
467
468
}

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)
{
}

469
void
470
EstimatedParamsStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings)
471
472
473
{
  for (vector<EstimationParams>::const_iterator it = estim_params_list.begin();
       it != estim_params_list.end(); it++)
474
475
476
477
    {
      if (it->name == "dsge_prior_weight")
        mod_file_struct.dsge_prior_weight_in_estimated_params = true;

478
      // Handle case of degenerate beta prior
479
      if (it->prior == eBeta)
480
        try
481
          {
482
483
            if (it->mean->eval(eval_context_t()) == 0.5
                && it->std->eval(eval_context_t()) == 0.5)
484
485
486
487
488
489
490
491
              {
                cerr << "ERROR: The prior density is not defined for the beta distribution when the mean = standard deviation = 0.5." << endl;
                exit(EXIT_FAILURE);
              }
          }
        catch (ExprNode::EvalException &e)
          {
            // We don't have enough information to compute the numerical value, skip the test
492
493
          }
    }
494
495
}

sebastien's avatar
sebastien committed
496
void
sebastien's avatar
sebastien committed
497
EstimatedParamsStatement::writeOutput(ostream &output, const string &basename) const
sebastien's avatar
sebastien committed
498
{
499
500
501
502
503
  output << "global estim_params_" << endl
         << "estim_params_.var_exo = [];" << endl
         << "estim_params_.var_endo = [];" << endl
         << "estim_params_.corrx = [];" << endl
         << "estim_params_.corrn = [];" << endl
Sébastien Villemot's avatar
Sébastien Villemot committed
504
         << "estim_params_.param_vals = [];" << endl;
sebastien's avatar
sebastien committed
505
506
507

  vector<EstimationParams>::const_iterator it;

508
  for (it = estim_params_list.begin(); it != estim_params_list.end(); it++)
509
    {
sebastien's avatar
sebastien committed
510
      int symb_id = symbol_table.getTypeSpecificID(it->name) + 1;
511
512
      SymbolType symb_type = symbol_table.getType(it->name);

513
      switch (it->type)
514
        {
sebastien's avatar
sebastien committed
515
        case 1:
516
          if (symb_type == eExogenous)
sebastien's avatar
sebastien committed
517
            output << "estim_params_.var_exo = [estim_params_.var_exo; ";
518
          else if (symb_type == eEndogenous)
sebastien's avatar
sebastien committed
519
            output << "estim_params_.var_endo = [estim_params_.var_endo; ";
520
          output << symb_id;
sebastien's avatar
sebastien committed
521
522
          break;
        case 2:
523
524
          output << "estim_params_.param_vals = [estim_params_.param_vals; "
                 << symb_id;
sebastien's avatar
sebastien committed
525
526
          break;
        case 3:
527
          if (symb_type == eExogenous)
sebastien's avatar
sebastien committed
528
            output << "estim_params_.corrx = [estim_params_.corrx; ";
529
          else if (symb_type == eEndogenous)
sebastien's avatar
sebastien committed
530
            output << "estim_params_.corrn = [estim_params_.corrn; ";
sebastien's avatar
sebastien committed
531
          output << symb_id << " " << symbol_table.getTypeSpecificID(it->name2)+1;
sebastien's avatar
sebastien committed
532
          break;
533
        }
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
      output << ", ";
      it->init_val->writeOutput(output);
      output << ", ";
      it->low_bound->writeOutput(output);
      output << ", ";
      it->up_bound->writeOutput(output);
      output << ", "
             << it->prior << ", ";
      it->mean->writeOutput(output);
      output << ", ";
      it->std->writeOutput(output);
      output << ", ";
      it->p3->writeOutput(output);
      output << ", ";
      it->p4->writeOutput(output);
      output << ", ";
      it->jscale->writeOutput(output);
      output << " ];" << endl;
552
553
554
    }
}

sebastien's avatar
sebastien committed
555
556
557
558
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)
559
{
sebastien's avatar
sebastien committed
560
561
562
}

void
sebastien's avatar
sebastien committed
563
EstimatedParamsInitStatement::writeOutput(ostream &output, const string &basename) const
sebastien's avatar
sebastien committed
564
565
566
{
  vector<EstimationParams>::const_iterator it;

567
  for (it = estim_params_list.begin(); it != estim_params_list.end(); it++)
568
    {
sebastien's avatar
sebastien committed
569
      int symb_id = symbol_table.getTypeSpecificID(it->name) + 1;
570
571
      SymbolType symb_type = symbol_table.getType(it->name);

sebastien's avatar
sebastien committed
572
      if (it->type < 3)
573
        {
574
          if (symb_type == eExogenous)
sebastien's avatar
sebastien committed
575
            {
576
577
578
579
              output << "tmp1 = find(estim_params_.var_exo(:,1)==" << symb_id << ");" << endl;
              output << "estim_params_.var_exo(tmp1,2) = ";
              it->init_val->writeOutput(output);
              output << ";" << endl;
sebastien's avatar
sebastien committed
580
            }
581
          else if (symb_type == eEndogenous)
sebastien's avatar
sebastien committed
582
            {
583
584
585
586
              output << "tmp1 = find(estim_params_.var_endo(:,1)==" << symb_id << ");" << endl;
              output << "estim_params_.var_endo(tmp1,2) = ";
              it->init_val->writeOutput(output);
              output << ";" << endl;
sebastien's avatar
sebastien committed
587
            }
588
          else if (symb_type == eParameter)
sebastien's avatar
sebastien committed
589
            {
590
591
592
593
              output << "tmp1 = find(estim_params_.param_vals(:,1)==" << symb_id << ");" << endl;
              output << "estim_params_.param_vals(tmp1,2) = ";
              it->init_val->writeOutput(output);
              output << ";" << endl;
sebastien's avatar
sebastien committed
594
            }
595
        }
sebastien's avatar
sebastien committed
596
      else
597
        {
598
          if (symb_type == eExogenous)
sebastien's avatar
sebastien committed
599
            {
sebastien's avatar
sebastien committed
600
              output << "tmp1 = find((estim_params_.corrx(:,1)==" << symb_id << ")) & (estim_params_.corrx(:,2)==" << symbol_table.getTypeSpecificID(it->name2)+1 << ");" << endl;
601
602
603
              output << "estim_params_.corrx(tmp1,3) = ";
              it->init_val->writeOutput(output);
              output << ";" << endl;
sebastien's avatar
sebastien committed
604
            }
605
          else if (symb_type == eEndogenous)
sebastien's avatar
sebastien committed
606
            {
sebastien's avatar
sebastien committed
607
              output << "tmp1 = find((estim_params_.corrn(:,1)==" << symb_id << ")) & (estim_params_.corrn(:,2)==" << symbol_table.getTypeSpecificID(it->name2)+1 << ";" << endl;
608
609
610
              output << "estim_params_.corrn(tmp1,3) = ";
              it->init_val->writeOutput(output);
              output << ";" << endl;
sebastien's avatar
sebastien committed
611
            }
612
613
614
615
        }
    }
}

sebastien's avatar
sebastien committed
616
617
618
619
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)
620
{
sebastien's avatar
sebastien committed
621
622
623
}

void
sebastien's avatar
sebastien committed
624
EstimatedParamsBoundsStatement::writeOutput(ostream &output, const string &basename) const
sebastien's avatar
sebastien committed
625
626
627
{
  vector<EstimationParams>::const_iterator it;

628
  for (it = estim_params_list.begin(); it != estim_params_list.end(); it++)
629
    {
sebastien's avatar
sebastien committed
630
      int symb_id = symbol_table.getTypeSpecificID(it->name) + 1;
631
632
      SymbolType symb_type = symbol_table.getType(it->name);

sebastien's avatar
sebastien committed
633
      if (it->type < 3)
634
        {
635
          if (symb_type == eExogenous)
sebastien's avatar
sebastien committed
636
            {
637
638
639
640
641
642
643
644
645
              output << "tmp1 = find(estim_params_.var_exo(:,1)==" << symb_id << ");" << endl;

              output << "estim_params_.var_exo(tmp1,3) = ";
              it->low_bound->writeOutput(output);
              output << ";" << endl;

              output << "estim_params_.var_exo(tmp1,4) = ";
              it->up_bound->writeOutput(output);
              output << ";" << endl;
sebastien's avatar
sebastien committed
646
            }
647
          else if (symb_type == eEndogenous)
sebastien's avatar
sebastien committed
648
            {
649
650
651
652
653
654
655
656
657
              output << "tmp1 = find(estim_params_.var_endo(:,1)==" << symb_id << ");" << endl;

              output << "estim_params_.var_endo(tmp1,3) = ";
              it->low_bound->writeOutput(output);
              output << ";" << endl;

              output << "estim_params_.var_endo(tmp1,4) = ";
              it->up_bound->writeOutput(output);
              output << ";" << endl;
sebastien's avatar
sebastien committed
658
            }
659
          else if (symb_type == eParameter)
sebastien's avatar
sebastien committed
660
            {
661
662
663
664
665
666
667
668
669
              output << "tmp1 = find(estim_params_.param_vals(:,1)==" << symb_id << ");" << endl;

              output << "estim_params_.param_vals(tmp1,3) = ";
              it->low_bound->writeOutput(output);
              output << ";" << endl;

              output << "estim_params_.param_vals(tmp1,4) = ";
              it->up_bound->writeOutput(output);
              output << ";" << endl;
sebastien's avatar
sebastien committed
670
            }
671
        }
sebastien's avatar
sebastien committed
672
      else
673
        {
674
          if (symb_type == eExogenous)
sebastien's avatar
sebastien committed
675
            {
sebastien's avatar
sebastien committed
676
              output << "tmp1 = find((estim_params_.corrx(:,1)==" << symb_id << ")) & (estim_params_.corrx(:,2)==" << symbol_table.getTypeSpecificID(it->name2)+1 << ");" << endl;
677
678
679
680
681
682
683
684

              output << "estim_params_.corrx(tmp1,4) = ";
              it->low_bound->writeOutput(output);
              output << ";" << endl;

              output << "estim_params_.corrx(tmp1,5) = ";
              it->up_bound->writeOutput(output);
              output << ";" << endl;
sebastien's avatar
sebastien committed
685
            }
686
          else if (symb_type == eEndogenous)
sebastien's avatar
sebastien committed
687
            {
sebastien's avatar
sebastien committed
688
              output << "tmp1 = find((estim_params_.corrn(:,1)==" << symb_id << ")) & (estim_params_.corrn(:,2)==" << symbol_table.getTypeSpecificID(it->name2)+1 << ";" << endl;
689
690
691
692
693
694
695
696

              output << "estim_params_.corrn(tmp1,4) = ";
              it->low_bound->writeOutput(output);
              output << ";" << endl;

              output << "estim_params_.corrn(tmp1,5) = ";
              it->up_bound->writeOutput(output);
              output << ";" << endl;
sebastien's avatar
sebastien committed
697
            }
698
699
        }
    }
sebastien's avatar
sebastien committed
700
701
}

702
ObservationTrendsStatement::ObservationTrendsStatement(const trend_elements_t &trend_elements_arg,
sebastien's avatar
sebastien committed
703
704
705
706
707
708
709
                                                       const SymbolTable &symbol_table_arg) :
  trend_elements(trend_elements_arg),
  symbol_table(symbol_table_arg)
{
}

void
sebastien's avatar
sebastien committed
710
ObservationTrendsStatement::writeOutput(ostream &output, const string &basename) const
sebastien's avatar
sebastien committed
711
712
713
{
  output << "options_.trend_coeff_ = {};" << endl;

714
  trend_elements_t::const_iterator it;
sebastien's avatar
sebastien committed
715

716
  for (it = trend_elements.begin(); it != trend_elements.end(); it++)
717
    {
718
      SymbolType type = symbol_table.getType(it->first);
sebastien's avatar
sebastien committed
719
      if (type == eEndogenous)
720
        {
sebastien's avatar
sebastien committed
721
          output << "tmp1 = strmatch('" << it->first << "',options_.varobs,'exact');\n";
sebastien's avatar
sebastien committed
722
723
724
          output << "options_.trend_coeffs{tmp1} = '";
          it->second->writeOutput(output);
          output << "';" << endl;
725
        }
sebastien's avatar
sebastien committed
726
727
      else
        cout << "Error : Non-variable symbol used in TREND_COEFF: " << it->first << endl;
728
729
730
    }
}

sebastien's avatar
sebastien committed
731
732
OsrParamsStatement::OsrParamsStatement(const SymbolList &symbol_list_arg) :
  symbol_list(symbol_list_arg)
733
734
735
{
}

736
void
737
OsrParamsStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings)
738
739
740
741
{
  mod_file_struct.osr_params_present = true;
}

sebastien's avatar
sebastien committed
742
void
sebastien's avatar
sebastien committed
743
OsrParamsStatement::writeOutput(ostream &output, const string &basename) const
744
{
sebastien's avatar
sebastien committed
745
  symbol_list.writeOutput("osr_params_", output);
746
747
}

sebastien's avatar
sebastien committed
748
OsrStatement::OsrStatement(const SymbolList &symbol_list_arg,
sebastien's avatar
sebastien committed
749
                           const OptionsList &options_list_arg) :
sebastien's avatar
sebastien committed
750
  symbol_list(symbol_list_arg),
sebastien's avatar
sebastien committed
751
  options_list(options_list_arg)
752
753
754
{
}

sebastien's avatar
sebastien committed
755
void
756
OsrStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings)
sebastien's avatar
sebastien committed
757
{
sebastien's avatar
sebastien committed
758
  mod_file_struct.osr_present = true;
sebastien's avatar
sebastien committed
759
760

  // Fill in option_order of mod_file_struct
761
  OptionsList::num_options_t::const_iterator it = options_list.num_options.find("order");
sebastien's avatar
sebastien committed
762
  if (it != options_list.num_options.end())
763
    mod_file_struct.order_option = max(mod_file_struct.order_option, atoi(it->second.c_str()));
sebastien's avatar
trunk:    
sebastien committed
764

765
766
767
768
  // Fill in mod_file_struct.partial_information
  it = options_list.num_options.find("partial_information");
  if (it != options_list.num_options.end() && it->second == "1")
    mod_file_struct.partial_information = true;
sebastien's avatar
sebastien committed
769
770
771
772
773
774

  // Option k_order_solver (implicit when order >= 3)
  it = options_list.num_options.find("k_order_solver");
  if ((it != options_list.num_options.end() && it->second == "1")
      || mod_file_struct.order_option >= 3)
    mod_file_struct.k_order_solver = true;
sebastien's avatar
sebastien committed
775
776
}

sebastien's avatar
sebastien committed
777
void
sebastien's avatar
sebastien committed
778
OsrStatement::writeOutput(ostream &output, const string &basename) const
779
{
sebastien's avatar
sebastien committed
780
  options_list.writeOutput(output);
sebastien's avatar
sebastien committed
781
  symbol_list.writeOutput("var_list_", output);
sebastien's avatar
sebastien committed
782
  output << "osr(var_list_,osr_params_,obj_var_,optim_weights_);\n";
783
784
}

785
786
OptimWeightsStatement::OptimWeightsStatement(const var_weights_t &var_weights_arg,
                                             const covar_weights_t &covar_weights_arg,
sebastien's avatar
sebastien committed
787
788
789
790
                                             const SymbolTable &symbol_table_arg) :
  var_weights(var_weights_arg),
  covar_weights(covar_weights_arg),
  symbol_table(symbol_table_arg)
791
792
793
{
}

794
void
795
OptimWeightsStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings)
796
797
798
799
{
  mod_file_struct.optim_weights_present = true;
}

sebastien's avatar
sebastien committed
800
void
sebastien's avatar
sebastien committed
801
OptimWeightsStatement::writeOutput(ostream &output, const string &basename) const
802
{
sebastien's avatar
sebastien committed
803
804
805
806
807
  output << "%" << endl
         << "% OPTIM_WEIGHTS" << endl
         << "%" << endl
         << "optim_weights_ = sparse(M_.endo_nbr,M_.endo_nbr);" << endl
         << "obj_var_ = [];" << endl << endl;
sebastien's avatar
sebastien committed
808

809
  for (var_weights_t::const_iterator it = var_weights.begin();
810
       it != var_weights.end(); it++)
811
    {
sebastien's avatar
sebastien committed
812
      const string &name = it->first;
813
      const expr_t value = it->second;
sebastien's avatar
sebastien committed
814
      int id = symbol_table.getTypeSpecificID(name) + 1;
Houtan Bastani's avatar
Houtan Bastani committed
815
      output << "optim_weights_(" << id << "," << id << ") = ";
sebastien's avatar
sebastien committed
816
817
      value->writeOutput(output);
      output << ";" << endl;
sebastien's avatar
sebastien committed
818
      output << "obj_var_ = [obj_var_; " << id << "];\n";
819
    }
sebastien's avatar
sebastien committed
820

821
  for (covar_weights_t::const_iterator it = covar_weights.begin();
822
       it != covar_weights.end(); it++)
823
    {
sebastien's avatar
sebastien committed
824
825
      const string &name1 = it->first.first;
      const string &name2 = it->first.second;
826
      const expr_t value = it->second;
sebastien's avatar
sebastien committed
827
828
      int id1 = symbol_table.getTypeSpecificID(name1) + 1;
      int id2 = symbol_table.getTypeSpecificID(name2) + 1;
Houtan Bastani's avatar
Houtan Bastani committed
829
      output << "optim_weights_(" << id1 << "," << id2 << ") = ";
sebastien's avatar
sebastien committed
830
831
      value->writeOutput(output);
      output << ";" << endl;
832
      output << "obj_var_ = [obj_var_; " << id1 << "; " << id2 << "];\n";
833
834
835
    }
}

sebastien's avatar
sebastien committed
836
DynaSaveStatement::DynaSaveStatement(const SymbolList &symbol_list_arg,
sebastien's avatar
trunk:    
sebastien committed
837
                                     const string &filename_arg) :
sebastien's avatar
sebastien committed
838
  symbol_list(symbol_list_arg),
sebastien's avatar
trunk:    
sebastien committed
839
  filename(filename_arg)
840
841
842
{
}

sebastien's avatar
sebastien committed
843
void
sebastien's avatar
sebastien committed
844
DynaSaveStatement::writeOutput(ostream &output, const string &basename) const
845
{
sebastien's avatar
sebastien committed
846
  symbol_list.writeOutput("var_list_", output);
sebastien's avatar
trunk:    
sebastien committed
847
848
  output << "dynasave('" << filename
         << "',var_list_);" << endl;
849
850
}

sebastien's avatar
sebastien committed
851
DynaTypeStatement::DynaTypeStatement(const SymbolList &symbol_list_arg,
sebastien's avatar
trunk:    
sebastien committed
852
                                     const string &filename_arg) :
sebastien's avatar
sebastien committed
853
  symbol_list(symbol_list_arg),
sebastien's avatar
trunk:    
sebastien committed
854
  filename(filename_arg)
855
856
857
{
}

sebastien's avatar
sebastien committed
858
void
sebastien's avatar
sebastien committed
859
DynaTypeStatement::writeOutput(ostream &output, const string &basename) const
860
{
sebastien's avatar
sebastien committed
861
  symbol_list.writeOutput("var_list_", output);
sebastien's avatar
trunk:    
sebastien committed
862
863
  output << "dynatype('" << filename
         << "',var_list_);" << endl;
864
865
}

866
ModelComparisonStatement::ModelComparisonStatement(const filename_list_t &filename_list_arg,
sebastien's avatar
sebastien committed
867
868
869
                                                   const OptionsList &options_list_arg) :
  filename_list(filename_list_arg),
  options_list(options_list_arg)
870
871
872
{
}

sebastien's avatar
sebastien committed
873
void
sebastien's avatar
sebastien committed
874
ModelComparisonStatement::writeOutput(ostream &output, const string &basename) const
sebastien's avatar
sebastien committed
875
876
877
{
  options_list.writeOutput(output);

sebastien's avatar
trunk:    
sebastien committed
878
879
  output << "ModelNames_ = {};" << endl;
  output << "ModelPriors_ = [];" << endl;
sebastien's avatar
sebastien committed
880

881
  for (filename_list_t::const_iterator it = filename_list.begin();
882
       it != filename_list.end(); it++)
sebastien's avatar
sebastien committed
883
    {
sebastien's avatar
trunk:    
sebastien committed
884
885
      output << "ModelNames_ = { ModelNames_{:} '" << (*it).first << "'};" << endl;
      output << "ModelPriors_ = [ ModelPriors_ ; " << (*it).second << "];" << endl;
sebastien's avatar
sebastien committed
886
    }
sebastien's avatar
trunk:    
sebastien committed
887
  output << "model_comparison(ModelNames_,ModelPriors_,oo_,options_,M_.fname);" << endl;
sebastien's avatar
sebastien committed
888
}
sebastien's avatar
sebastien committed
889

sebastien's avatar
sebastien committed
890
PlannerObjectiveStatement::PlannerObjectiveStatement(StaticModel *model_tree_arg) :
sebastien's avatar
sebastien committed
891
892
893
894
895
896
897
898
899
900
  model_tree(model_tree_arg)
{
}

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

void
Houtan Bastani's avatar