ComputingTasks.cc 28.8 KB
Newer Older
sebastien's avatar
sebastien committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * 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/>.
18
 */
sebastien's avatar
sebastien committed
19

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

using namespace std;
sebastien's avatar
sebastien committed
25

26
#include "ComputingTasks.hh"
sebastien's avatar
sebastien committed
27
28
29
30
31
32
33
34
#include "Statement.hh"

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

void
sebastien's avatar
sebastien committed
35
SteadyStatement::writeOutput(ostream &output, const string &basename) const
sebastien's avatar
sebastien committed
36
37
38
39
40
{
  options_list.writeOutput(output);
  output << "steady;\n";
}

ferhat's avatar
ferhat committed
41
42
43
44
45
46
47
48
49
50
51
52
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";
}

sebastien's avatar
sebastien committed
53
54
55
56
57
58
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
62
  options_list.writeOutput(output);
  output << "check;\n";
63
64
}

65
66
67
68
69
70
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

sebastien's avatar
sebastien committed
88
89
SimulStatement::SimulStatement(const OptionsList &options_list_arg) :
  options_list(options_list_arg)
90
91
92
{
}

sebastien's avatar
sebastien committed
93
94
95
96
97
98
void
SimulStatement::checkPass(ModFileStructure &mod_file_struct)
{
  mod_file_struct.simul_present = true;
}

sebastien's avatar
sebastien committed
99
void
sebastien's avatar
sebastien committed
100
SimulStatement::writeOutput(ostream &output, const string &basename) const
101
{
sebastien's avatar
sebastien committed
102
103
  options_list.writeOutput(output);
  output << "simul(oo_.dr);\n";
104
105
}

sebastien's avatar
sebastien committed
106
SimulSparseStatement::SimulSparseStatement(const OptionsList &options_list_arg,
ferhat's avatar
ferhat committed
107
                                           int compiler_arg, int mode_arg) :
sebastien's avatar
sebastien committed
108
  options_list(options_list_arg),
ferhat's avatar
ferhat committed
109
110
  compiler(compiler_arg),
  mode(mode_arg)
sebastien's avatar
sebastien committed
111
112
113
114
115
116
117
118
119
120
121
122
123
{
}

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);
ferhat's avatar
ferhat committed
124
  output << "if (~ options_.initval_file) & (size(oo_.endo_simul,2)<options_.periods)\n";
125
126
127
128
129
130
  output << "  if ~isfield(options_,'datafile')\n";
  output << "    make_y_;\n";
  output << "    make_ex_;\n";
  output << "  else\n";
  output << "    read_data_;\n";
  output << "  end\n";
sebastien's avatar
sebastien committed
131
  output << "end\n";
ferhat's avatar
ferhat committed
132
  if(mode==eSparseDLLMode)
133
    {
ferhat's avatar
ferhat committed
134
135
136
137
138
139
140
141
142
143
144
      if(compiler!=NO_COMPILE)
        {
          output << "disp('compiling...');\n";
          output << "t0=clock;\n";
          if (compiler == 0)
            output << "mex " << basename << "_dynamic.c;\n";
          else
            output << "mex " << basename << "_dynamic.cc;\n";
          output << "disp(['compiling time: ' num2str(etime(clock,t0))]);\n";
          output << "oo_.endo_simul=" << basename << "_dynamic;\n";
        }
145
      else
ferhat's avatar
ferhat committed
146
147
148
149
        {
          output << "oo_.endo_simul=simulate;\n";
          output << "clear simulate.dll;\n";
        }
150
    }
sebastien's avatar
sebastien committed
151
  else
152
    {
ferhat's avatar
ferhat committed
153
154
      //output << "oo_.endo_simul=" << basename << "_dynamic();\n";
      output << basename << "_dynamic;\n";
155
    }
sebastien's avatar
sebastien committed
156
157
}

sebastien's avatar
sebastien committed
158
StochSimulStatement::StochSimulStatement(const SymbolList &symbol_list_arg,
sebastien's avatar
sebastien committed
159
                                         const OptionsList &options_list_arg) :
sebastien's avatar
sebastien committed
160
  symbol_list(symbol_list_arg),
sebastien's avatar
sebastien committed
161
  options_list(options_list_arg)
162
163
164
{
}

sebastien's avatar
sebastien committed
165
166
167
void
StochSimulStatement::checkPass(ModFileStructure &mod_file_struct)
{
sebastien's avatar
sebastien committed
168
  mod_file_struct.stoch_simul_present = true;
sebastien's avatar
sebastien committed
169
170
171
172

  // 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())
173
    mod_file_struct.order_option = max(mod_file_struct.order_option,atoi(it->second.c_str()));
sebastien's avatar
sebastien committed
174
175
}

sebastien's avatar
sebastien committed
176
void
sebastien's avatar
sebastien committed
177
StochSimulStatement::writeOutput(ostream &output, const string &basename) const
178
{
sebastien's avatar
sebastien committed
179
  options_list.writeOutput(output);
sebastien's avatar
sebastien committed
180
  symbol_list.writeOutput("var_list_", output);
181
  output << "info = stoch_simul(var_list_);\n";
182
183
}

184
185
186
187
188
189
190
191
192
193
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
194
  mod_file_struct.forecast_present = true;
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209

  // 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
210
RamseyPolicyStatement::RamseyPolicyStatement(const SymbolList &symbol_list_arg,
sebastien's avatar
sebastien committed
211
                                             const OptionsList &options_list_arg) :
sebastien's avatar
sebastien committed
212
  symbol_list(symbol_list_arg),
michel's avatar
michel committed
213
214
215
216
217
218
219
  options_list(options_list_arg)
{
}

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

sebastien's avatar
sebastien committed
222
223
224
  /* 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 */
sebastien's avatar
sebastien committed
225
226
  OptionsList::num_options_type::const_iterator it = options_list.num_options.find("order");
  if (it != options_list.num_options.end())
227
    mod_file_struct.order_option = max(mod_file_struct.order_option,atoi(it->second.c_str()) + 1);
michel's avatar
michel committed
228
229
230
231
232
233
}

void
RamseyPolicyStatement::writeOutput(ostream &output, const string &basename) const
{
  options_list.writeOutput(output);
sebastien's avatar
sebastien committed
234
  symbol_list.writeOutput("var_list_", output);
235
  output << "ramsey_policy(var_list_);\n";
michel's avatar
michel committed
236
237
}

sebastien's avatar
sebastien committed
238
EstimationStatement::EstimationStatement(const SymbolList &symbol_list_arg,
sebastien's avatar
sebastien committed
239
                                         const OptionsList &options_list_arg) :
sebastien's avatar
sebastien committed
240
  symbol_list(symbol_list_arg),
sebastien's avatar
sebastien committed
241
  options_list(options_list_arg)
242
243
244
{
}

sebastien's avatar
sebastien committed
245
246
247
void
EstimationStatement::checkPass(ModFileStructure &mod_file_struct)
{
sebastien's avatar
sebastien committed
248
  mod_file_struct.estimation_present = true;
sebastien's avatar
sebastien committed
249
250
251
252

  // 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())
253
    mod_file_struct.order_option = max(mod_file_struct.order_option,atoi(it->second.c_str()));
sebastien's avatar
sebastien committed
254
255
}

sebastien's avatar
sebastien committed
256
void
sebastien's avatar
sebastien committed
257
EstimationStatement::writeOutput(ostream &output, const string &basename) const
258
{
sebastien's avatar
sebastien committed
259
  options_list.writeOutput(output);
sebastien's avatar
sebastien committed
260
  symbol_list.writeOutput("var_list_", output);
sebastien's avatar
sebastien committed
261
  output << "dynare_estimation(var_list_);\n";
262
263
}

sebastien's avatar
sebastien committed
264
PriorAnalysisStatement::PriorAnalysisStatement(const SymbolList &symbol_list_arg,
sebastien's avatar
sebastien committed
265
                                               const OptionsList &options_list_arg) :
sebastien's avatar
sebastien committed
266
  symbol_list(symbol_list_arg),
sebastien's avatar
sebastien committed
267
268
269
270
271
  options_list(options_list_arg)
{
}

void
sebastien's avatar
sebastien committed
272
PriorAnalysisStatement::writeOutput(ostream &output, const string &basename) const
sebastien's avatar
sebastien committed
273
274
{
  options_list.writeOutput(output);
sebastien's avatar
sebastien committed
275
  symbol_list.writeOutput("var_list_", output);
sebastien's avatar
sebastien committed
276
277
278
  output << "prior_analysis(var_list_);\n";
}

sebastien's avatar
sebastien committed
279
PosteriorAnalysisStatement::PosteriorAnalysisStatement(const SymbolList &symbol_list_arg,
sebastien's avatar
sebastien committed
280
                                                       const OptionsList &options_list_arg) :
sebastien's avatar
sebastien committed
281
  symbol_list(symbol_list_arg),
sebastien's avatar
sebastien committed
282
283
284
285
286
  options_list(options_list_arg)
{
}

void
sebastien's avatar
sebastien committed
287
PosteriorAnalysisStatement::writeOutput(ostream &output, const string &basename) const
sebastien's avatar
sebastien committed
288
289
{
  options_list.writeOutput(output);
sebastien's avatar
sebastien committed
290
  symbol_list.writeOutput("var_list_", output);
sebastien's avatar
sebastien committed
291
292
  output << "posterior_analysis(var_list_);\n";
}
293

294
295
296
297
298
299
300
301
DynareSensitivityStatement::DynareSensitivityStatement(const OptionsList &options_list_arg) :
  options_list(options_list_arg)
{
}

void
DynareSensitivityStatement::writeOutput(ostream &output, const string &basename) const
{
302
303
  options_list.writeOutput(output,"options_gsa");
  output << "dynare_sensitivity(options_gsa);" << endl;
304
305
}

sebastien's avatar
sebastien committed
306
RplotStatement::RplotStatement(const SymbolList &symbol_list_arg,
sebastien's avatar
sebastien committed
307
                               const OptionsList &options_list_arg) :
sebastien's avatar
sebastien committed
308
  symbol_list(symbol_list_arg),
sebastien's avatar
sebastien committed
309
  options_list(options_list_arg)
310
311
312
{
}

sebastien's avatar
sebastien committed
313
void
sebastien's avatar
sebastien committed
314
RplotStatement::writeOutput(ostream &output, const string &basename) const
315
{
sebastien's avatar
sebastien committed
316
  options_list.writeOutput(output);
sebastien's avatar
sebastien committed
317
  symbol_list.writeOutput("var_list_", output);
sebastien's avatar
sebastien committed
318
  output << "rplot(var_list_);\n";
319
320
}

sebastien's avatar
sebastien committed
321
322
UnitRootVarsStatement::UnitRootVarsStatement(const SymbolList &symbol_list_arg) :
  symbol_list(symbol_list_arg)
323
324
325
{
}

sebastien's avatar
sebastien committed
326
void
sebastien's avatar
sebastien committed
327
UnitRootVarsStatement::writeOutput(ostream &output, const string &basename) const
328
{
sebastien's avatar
sebastien committed
329
  symbol_list.writeOutput("options_.unit_root_vars", output);
330
331
}

sebastien's avatar
sebastien committed
332
PeriodsStatement::PeriodsStatement(int periods_arg) : periods(periods_arg)
michel's avatar
michel committed
333
334
335
{
}

sebastien's avatar
sebastien committed
336
void
sebastien's avatar
sebastien committed
337
PeriodsStatement::writeOutput(ostream &output, const string &basename) const
michel's avatar
michel committed
338
{
sebastien's avatar
sebastien committed
339
340
  output << "options_.periods = " << periods << ";" << endl;
  output << "options_.simul = 1;" << endl;
michel's avatar
michel committed
341
342
}

343
CutoffStatement::CutoffStatement(double cutoff_arg) : cutoff(cutoff_arg)
sebastien's avatar
sebastien committed
344
345
346
347
348
349
350
351
352
{
}

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

353
354
355
356
357
358
359
360
361
362
MarkowitzStatement::MarkowitzStatement(double markowitz_arg) : markowitz(markowitz_arg)
{
}

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

sebastien's avatar
sebastien committed
363
DsampleStatement::DsampleStatement(int val1_arg) : val1(val1_arg), val2(-1)
364
365
366
{
}

sebastien's avatar
sebastien committed
367
DsampleStatement::DsampleStatement(int val1_arg, int val2_arg) : val1(val1_arg), val2(val2_arg)
368
369
370
{
}

sebastien's avatar
sebastien committed
371
void
sebastien's avatar
sebastien committed
372
DsampleStatement::writeOutput(ostream &output, const string &basename) const
373
{
sebastien's avatar
sebastien committed
374
375
376
377
378
379
  if (val2 < 0)
    output << "options_.dsample = " << val1 << ";" << endl;
  else
    output << "options_.dsample = [" << val1 << "; " << val2 << "];" << endl;
}

sebastien's avatar
sebastien committed
380
381
VarobsStatement::VarobsStatement(const SymbolList &symbol_list_arg) :
  symbol_list(symbol_list_arg)
sebastien's avatar
sebastien committed
382
383
384
385
{
}

void
sebastien's avatar
sebastien committed
386
VarobsStatement::writeOutput(ostream &output, const string &basename) const
sebastien's avatar
sebastien committed
387
{
sebastien's avatar
sebastien committed
388
  symbol_list.writeOutput("options_.varobs", output);
sebastien's avatar
sebastien committed
389
390
391
392
393
394
395
396
397
398
}

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
sebastien's avatar
sebastien committed
399
EstimatedParamsStatement::writeOutput(ostream &output, const string &basename) const
sebastien's avatar
sebastien committed
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
{
  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++)
415
    {
sebastien's avatar
sebastien committed
416
      switch(it->type)
417
        {
sebastien's avatar
sebastien committed
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
        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;
437
        }
sebastien's avatar
sebastien committed
438
439
440
441
      output << " " << it->init_val << " " <<  it->low_bound
             << " " << it->up_bound << " " <<  it->prior
             << " " << it->mean << " " <<  it->std
             << " " << it->p3 << " " <<  it->p4  << " " <<  it->jscale << "];\n";
442
443
444
    }
}

sebastien's avatar
sebastien committed
445
446
447
448
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)
449
{
sebastien's avatar
sebastien committed
450
451
452
}

void
sebastien's avatar
sebastien committed
453
EstimatedParamsInitStatement::writeOutput(ostream &output, const string &basename) const
sebastien's avatar
sebastien committed
454
455
456
457
{
  vector<EstimationParams>::const_iterator it;

  for(it = estim_params_list.begin(); it != estim_params_list.end(); it++)
458
    {
sebastien's avatar
sebastien committed
459
      if (it->type < 3)
460
        {
sebastien's avatar
sebastien committed
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
          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";
            }
476
        }
sebastien's avatar
sebastien committed
477
      else
478
        {
sebastien's avatar
sebastien committed
479
480
481
482
483
484
485
486
487
488
          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";
            }
489
490
491
492
        }
    }
}

sebastien's avatar
sebastien committed
493
494
495
496
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)
497
{
sebastien's avatar
sebastien committed
498
499
500
}

void
sebastien's avatar
sebastien committed
501
EstimatedParamsBoundsStatement::writeOutput(ostream &output, const string &basename) const
sebastien's avatar
sebastien committed
502
503
504
505
{
  vector<EstimationParams>::const_iterator it;

  for(it = estim_params_list.begin(); it != estim_params_list.end(); it++)
506
    {
sebastien's avatar
sebastien committed
507
      if (it->type < 3)
508
        {
sebastien's avatar
sebastien committed
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
          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";
            }
527
        }
sebastien's avatar
sebastien committed
528
      else
529
        {
sebastien's avatar
sebastien committed
530
531
532
533
534
535
536
537
538
539
540
541
          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";
            }
542
543
        }
    }
sebastien's avatar
sebastien committed
544
545
546
547
548
549
550
551
552
553
}

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
sebastien's avatar
sebastien committed
554
ObservationTrendsStatement::writeOutput(ostream &output, const string &basename) const
sebastien's avatar
sebastien committed
555
556
557
558
559
560
{
  output << "options_.trend_coeff_ = {};" << endl;

  trend_elements_type::const_iterator it;

  for(it = trend_elements.begin(); it != trend_elements.end(); it++)
561
    {
562
      SymbolType type = symbol_table.getType(it->first);
sebastien's avatar
sebastien committed
563
      if (type == eEndogenous)
564
        {
sebastien's avatar
sebastien committed
565
          output << "tmp1 = strmatch('" << it->first << "',options_.varobs,'exact');\n";
sebastien's avatar
sebastien committed
566
567
568
          output << "options_.trend_coeffs{tmp1} = '";
          it->second->writeOutput(output);
          output << "';" << endl;
569
        }
sebastien's avatar
sebastien committed
570
571
      else
        cout << "Error : Non-variable symbol used in TREND_COEFF: " << it->first << endl;
572
573
574
    }
}

sebastien's avatar
sebastien committed
575
576
577
578
579
580
581
582
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)
583
{
sebastien's avatar
sebastien committed
584
585
586
}

void
sebastien's avatar
sebastien committed
587
CalibVarStatement::writeOutput(ostream &output, const string &basename) const
sebastien's avatar
sebastien committed
588
589
{

sebastien's avatar
sebastien committed
590
591
592
  output << "%" << endl
         << "% CALIB_VAR" << endl
         << "%" << endl;
sebastien's avatar
sebastien committed
593
594

  for(int i = 1; i < 4 ; i++)
595
    {
sebastien's avatar
sebastien committed
596
597
598
      output << "calib_var_index{" << i << "} = [];\n";
      output << "calib_targets{" << i << "} = [];\n";
      output << "calib_weights{" << i << "}=[];\n";
599
    }
sebastien's avatar
sebastien committed
600
601
602
603

  // Print calibration variances
  for(calib_var_type::const_iterator it = calib_var.begin();
      it != calib_var.end(); it++)
604
    {
sebastien's avatar
sebastien committed
605
606
      const string &name = it->first;
      const string &weight = it->second.first;
sebastien's avatar
sebastien committed
607
      const NodeID expression = it->second.second;
sebastien's avatar
sebastien committed
608
609
610

      int id = symbol_table.getID(name) + 1;
      if (symbol_table.getType(name) == eEndogenous)
611
        {
sebastien's avatar
sebastien committed
612
613
          output << "calib_var_index{1} = [calib_var_index{1};" <<  id << "," << id << "];\n";
          output << "calib_weights{1} = [calib_weights{1}; " << weight << "];\n";
sebastien's avatar
sebastien committed
614
615
616
          output << "calib_targets{1} =[calib_targets{1}; ";
          expression->writeOutput(output);
          output << "];\n";
617
        }
sebastien's avatar
sebastien committed
618
      else if (symbol_table.getType(name) == eExogenous)
619
        {
sebastien's avatar
sebastien committed
620
621
          output << "calib_var_index{3} = [calib_var_index{3};" <<  id << "," << id << "];\n";
          output << "calib_weights{3} = [calib_weights{3}; " << weight << "];\n";
sebastien's avatar
sebastien committed
622
623
624
          output << "calib_targets{3} =[calib_targets{3}; ";
          expression->writeOutput(output);
          output << "];\n";
625
626
        }
    }
sebastien's avatar
sebastien committed
627
628
629
630

  // Print calibration covariances
  for(calib_covar_type::const_iterator it = calib_covar.begin();
      it != calib_covar.end(); it++)
631
    {
sebastien's avatar
sebastien committed
632
633
634
      const string &name1 = it->first.first;
      const string &name2 = it->first.second;
      const string &weight = it->second.first;
sebastien's avatar
sebastien committed
635
      const NodeID expression = it->second.second;
sebastien's avatar
sebastien committed
636
637
638
639

      int id1 = symbol_table.getID(name1) + 1;
      int id2 = symbol_table.getID(name2) + 1;
      if (symbol_table.getType(name1) == eEndogenous)
640
        {
sebastien's avatar
sebastien committed
641
642
          output << "calib_var_index{1} = [calib_var_index{1};" <<  id1 << "," << id2 << "];\n";
          output << "calib_weights{1} = [calib_weights{1}; " << weight << "];\n";
sebastien's avatar
sebastien committed
643
644
645
          output << "calib_targets{1} =[calib_targets{1}; ";
          expression->writeOutput(output);
          output << "];\n";
646
        }
sebastien's avatar
sebastien committed
647
      else if (symbol_table.getType(name1) == eExogenous)
648
        {
sebastien's avatar
sebastien committed
649
650
          output << "calib_var_index{3} = [calib_var_index{3};" <<  id1 << "," << id2 << "];\n";
          output << "calib_weights{3} = [calib_weights{3}; " << weight << "];\n";
sebastien's avatar
sebastien committed
651
652
653
          output << "calib_targets{3} =[calib_targets{3}; ";
          expression->writeOutput(output);
          output << "];\n";
654
655
656
        }
    }

sebastien's avatar
sebastien committed
657
658
659
660
661
  // Print calibration autocorrelations
  int max_iar = 3;

  for(calib_ac_type::const_iterator it = calib_ac.begin();
      it != calib_ac.end(); it++)
662
    {
sebastien's avatar
sebastien committed
663
664
665
      const string &name = it->first.first;
      int iar = it->first.second + 3;
      const string &weight = it->second.first;
sebastien's avatar
sebastien committed
666
      const NodeID expression = it->second.second;
sebastien's avatar
sebastien committed
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683

      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";
sebastien's avatar
sebastien committed
684
685
686
      output << "calib_targets{" << iar << "} =[calib_targets{" << iar << "}; ";
      expression->writeOutput(output);
      output << "];\n";
687
688
689
    }
}

sebastien's avatar
sebastien committed
690
CalibStatement::CalibStatement(int covar_arg) : covar(covar_arg)
691
692
693
{
}

sebastien's avatar
sebastien committed
694
void
sebastien's avatar
sebastien committed
695
CalibStatement::writeOutput(ostream &output, const string &basename) const
696
{
sebastien's avatar
sebastien committed
697
  output << "M_.Sigma_e=calib(calib_var_index,calib_targets,calib_weights," << covar << ",Sigma_e_);\n";
698
699
}

sebastien's avatar
sebastien committed
700
701
OsrParamsStatement::OsrParamsStatement(const SymbolList &symbol_list_arg) :
  symbol_list(symbol_list_arg)
702
703
704
{
}

sebastien's avatar
sebastien committed
705
void
sebastien's avatar
sebastien committed
706
OsrParamsStatement::writeOutput(ostream &output, const string &basename) const
707
{
sebastien's avatar
sebastien committed
708
  symbol_list.writeOutput("osr_params_", output);
709
710
}

sebastien's avatar
sebastien committed
711
OsrStatement::OsrStatement(const SymbolList &symbol_list_arg,
sebastien's avatar
sebastien committed
712
                           const OptionsList &options_list_arg) :
sebastien's avatar
sebastien committed
713
  symbol_list(symbol_list_arg),
sebastien's avatar
sebastien committed
714
  options_list(options_list_arg)
715
716
717
{
}

sebastien's avatar
sebastien committed
718
719
720
void
OsrStatement::checkPass(ModFileStructure &mod_file_struct)
{
sebastien's avatar
sebastien committed
721
  mod_file_struct.osr_present = true;
sebastien's avatar
sebastien committed
722
723
724
725

  // 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())
726
    mod_file_struct.order_option = max(mod_file_struct.order_option,atoi(it->second.c_str()));
sebastien's avatar
sebastien committed
727
728
}

sebastien's avatar
sebastien committed
729
void
sebastien's avatar
sebastien committed
730
OsrStatement::writeOutput(ostream &output, const string &basename) const
731
{
sebastien's avatar
sebastien committed
732
  options_list.writeOutput(output);
sebastien's avatar
sebastien committed
733
  symbol_list.writeOutput("var_list_", output);
sebastien's avatar
sebastien committed
734
  output << "osr(var_list_,osr_params_,obj_var_,optim_weights_);\n";
735
736
}

sebastien's avatar
sebastien committed
737
738
739
740
741
742
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)
743
744
745
{
}

sebastien's avatar
sebastien committed
746
void
sebastien's avatar
sebastien committed
747
OptimWeightsStatement::writeOutput(ostream &output, const string &basename) const
748
{
sebastien's avatar
sebastien committed
749
750
751
752
753
  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
754
755
756

  for(var_weights_type::const_iterator it = var_weights.begin();
      it != var_weights.end(); it++)
757
    {
sebastien's avatar
sebastien committed
758
      const string &name = it->first;
sebastien's avatar
sebastien committed
759
      const NodeID value = it->second;
sebastien's avatar
sebastien committed
760
      int id = symbol_table.getID(name) + 1;
sebastien's avatar
sebastien committed
761
762
763
      output <<  "optim_weights_(" << id << "," << id << ") = ";
      value->writeOutput(output);
      output << ";" << endl;
sebastien's avatar
sebastien committed
764
      output << "obj_var_ = [obj_var_; " << id << "];\n";
765
    }
sebastien's avatar
sebastien committed
766
767
768

  for(covar_weights_type::const_iterator it = covar_weights.begin();
      it != covar_weights.end(); it++)
769
    {
sebastien's avatar
sebastien committed
770
771
      const string &name1 = it->first.first;
      const string &name2 = it->first.second;
sebastien's avatar
sebastien committed
772
      const NodeID value = it->second;
sebastien's avatar
sebastien committed
773
774
      int id1 = symbol_table.getID(name1) + 1;
      int id2 = symbol_table.getID(name2) + 1;
sebastien's avatar
sebastien committed
775
776
777
      output <<  "optim_weights_(" << id1 << "," << id2 << ") = ";
      value->writeOutput(output);
      output << ";" << endl;
sebastien's avatar
sebastien committed
778
      output << "obj_var_ = [obj_var_; " << id1 << " " << id2 << "];\n";
779
780
781
    }
}

sebastien's avatar
sebastien committed
782
DynaSaveStatement::DynaSaveStatement(const SymbolList &symbol_list_arg,
sebastien's avatar
trunk:    
sebastien committed
783
                                     const string &filename_arg) :
sebastien's avatar
sebastien committed
784
  symbol_list(symbol_list_arg),
sebastien's avatar
trunk:    
sebastien committed
785
  filename(filename_arg)
786
787
788
{
}

sebastien's avatar
sebastien committed
789
void
sebastien's avatar
sebastien committed
790
DynaSaveStatement::writeOutput(ostream &output, const string &basename) const
791
{
sebastien's avatar
sebastien committed
792
  symbol_list.writeOutput("var_list_", output);
sebastien's avatar
trunk:    
sebastien committed
793
794
  output << "dynasave('" << filename
         << "',var_list_);" << endl;
795
796
}

sebastien's avatar
sebastien committed
797
DynaTypeStatement::DynaTypeStatement(const SymbolList &symbol_list_arg,
sebastien's avatar
trunk:    
sebastien committed
798
                                     const string &filename_arg) :
sebastien's avatar
sebastien committed
799
  symbol_list(symbol_list_arg),
sebastien's avatar
trunk:    
sebastien committed
800
  filename(filename_arg)
801
802
803
{
}

sebastien's avatar
sebastien committed
804
void
sebastien's avatar
sebastien committed
805
DynaTypeStatement::writeOutput(ostream &output, const string &basename) const
806
{
sebastien's avatar
sebastien committed
807
  symbol_list.writeOutput("var_list_", output);
sebastien's avatar
trunk:    
sebastien committed
808
809
  output << "dynatype('" << filename
         << "',var_list_);" << endl;
810
811
}

sebastien's avatar
sebastien committed
812
813
814
815
ModelComparisonStatement::ModelComparisonStatement(const filename_list_type &filename_list_arg,
                                                   const OptionsList &options_list_arg) :
  filename_list(filename_list_arg),
  options_list(options_list_arg)
816
817
818
{
}

sebastien's avatar
sebastien committed
819
void
sebastien's avatar
sebastien committed
820
ModelComparisonStatement::writeOutput(ostream &output, const string &basename) const
sebastien's avatar
sebastien committed
821
822
823
{
  options_list.writeOutput(output);

sebastien's avatar
trunk:    
sebastien committed
824
825
  output << "ModelNames_ = {};" << endl;
  output << "ModelPriors_ = [];" << endl;
sebastien's avatar
sebastien committed
826
827
828
829

  for(filename_list_type::const_iterator it = filename_list.begin();
      it != filename_list.end(); it++)
    {
sebastien's avatar
trunk:    
sebastien committed
830
831
      output << "ModelNames_ = { ModelNames_{:} '" << (*it).first << "'};" << endl;
      output << "ModelPriors_ = [ ModelPriors_ ; " << (*it).second << "];" << endl;
sebastien's avatar
sebastien committed
832
    }
sebastien's avatar
trunk:    
sebastien committed
833
  output << "model_comparison(ModelNames_,ModelPriors_,oo_,options_,M_.fname);" << endl;
sebastien's avatar
sebastien committed
834
}
sebastien's avatar
sebastien committed
835
836
837
838
839
840
841
842
843
844
845
846
847
848

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

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

void
PlannerObjectiveStatement::checkPass(ModFileStructure &mod_file_struct)
{
849
850
  if (model_tree->equation_number() != 1)
    {
851
      cerr << "ERROR: planer_objective: should have only one equation!" << endl;
852
853
      exit(-1);
    }
sebastien's avatar
sebastien committed
854
855
856
857
858
859
}

void
PlannerObjectiveStatement::computingPass()
{
  model_tree->computeStaticHessian = true;
sebastien's avatar
sebastien committed
860
  model_tree->computingPass(eval_context_type());
sebastien's avatar
sebastien committed
861
862
863
864
865
866
867
}

void
PlannerObjectiveStatement::writeOutput(ostream &output, const string &basename) const
{
  model_tree->writeStaticFile(basename + "_objective");
}
868
869
870
871
872
873
874

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

875
876
877
878
879
880
void
BVARDensityStatement::checkPass(ModFileStructure &mod_file_struct)
{
  mod_file_struct.bvar_density_present = true;
}

881
882
883
884
885
886
887
888
889
890
891
892
893
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)
{
}

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

900
901
902
903
904
905
void
BVARForecastStatement::writeOutput(ostream &output, const string &basename) const
{
  options_list.writeOutput(output);
  output << "bvar_forecast(" << nlags << ");" << endl;
}