Shocks.cc 6.71 KB
Newer Older
sebastien's avatar
sebastien committed
1
/*
sebastien's avatar
sebastien committed
2
 * Copyright (C) 2003-2009 Dynare Team
sebastien's avatar
sebastien committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This file is part of Dynare.
 *
 * Dynare is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Dynare is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
 */

20
using namespace std;
sebastien's avatar
sebastien committed
21
22
23

#include <iostream>

24
25
#include "Shocks.hh"

sebastien's avatar
sebastien committed
26
AbstractShocksStatement::AbstractShocksStatement(bool mshocks_arg,
27
28
29
30
31
32
                                                 const det_shocks_type &det_shocks_arg,
                                                 const var_and_std_shocks_type &var_shocks_arg,
                                                 const var_and_std_shocks_type &std_shocks_arg,
                                                 const covar_and_corr_shocks_type &covar_shocks_arg,
                                                 const covar_and_corr_shocks_type &corr_shocks_arg,
                                                 const SymbolTable &symbol_table_arg) :
sebastien's avatar
sebastien committed
33
34
35
36
37
38
39
  mshocks(mshocks_arg),
  det_shocks(det_shocks_arg),
  var_shocks(var_shocks_arg),
  std_shocks(std_shocks_arg),
  covar_shocks(covar_shocks_arg),
  corr_shocks(corr_shocks_arg),
  symbol_table(symbol_table_arg)
40
41
42
{
}

sebastien's avatar
sebastien committed
43
44
void
AbstractShocksStatement::writeDetShocks(ostream &output) const
45
{
sebastien's avatar
sebastien committed
46
  int exo_det_length = 0;
47

sebastien's avatar
sebastien committed
48
49
50
  for(det_shocks_type::const_iterator it = det_shocks.begin();
      it != det_shocks.end(); it++)
    {
sebastien's avatar
sebastien committed
51
      int id = symbol_table.getTypeSpecificID(it->first) + 1;
sebastien's avatar
sebastien committed
52
53
      bool exo_det = (symbol_table.getType(it->first) == eExogenousDet);
      int set_shocks_index = ((int) mshocks) + 2 * ((int) exo_det);
54

sebastien's avatar
sebastien committed
55
56
57
58
      for (unsigned int i = 0; i < it->second.size(); i++)
        {
          const int &period1 = it->second[i].period1;
          const int &period2 = it->second[i].period2;
sebastien's avatar
sebastien committed
59
          const NodeID value = it->second[i].value;
sebastien's avatar
sebastien committed
60
61

          if (period1 == period2)
sebastien's avatar
sebastien committed
62
63
64
65
66
67
            {
              output << "set_shocks(" << set_shocks_index << "," << period1
                     << ", " << id << ", ";
              value->writeOutput(output);
              output << ");" << endl;
            }
sebastien's avatar
sebastien committed
68
          else
sebastien's avatar
sebastien committed
69
70
71
72
73
74
            {
              output << "set_shocks(" << set_shocks_index << "," << period1
                     << ":" << period2 << ", " << id << ", ";
              value->writeOutput(output);
              output << ");" << endl;
            }
sebastien's avatar
sebastien committed
75
76
77
78
79
80

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

sebastien's avatar
sebastien committed
83
84
void
AbstractShocksStatement::writeVarAndStdShocks(ostream &output) const
85
{
sebastien's avatar
sebastien committed
86
  var_and_std_shocks_type::const_iterator it;
87

sebastien's avatar
sebastien committed
88
  for(it = var_shocks.begin(); it != var_shocks.end(); it++)
89
    {
sebastien's avatar
sebastien committed
90
      int id = symbol_table.getTypeSpecificID(it->first) + 1;
sebastien's avatar
sebastien committed
91
92
93
94
      const NodeID value = it->second;
      output << "M_.Sigma_e(" << id << ", " << id << ") = ";
      value->writeOutput(output);
      output << ";" << endl;
95
    }
sebastien's avatar
sebastien committed
96
97

  for(it = std_shocks.begin(); it != std_shocks.end(); it++)
98
    {
sebastien's avatar
sebastien committed
99
      int id = symbol_table.getTypeSpecificID(it->first) + 1;
sebastien's avatar
sebastien committed
100
101
102
103
      const NodeID value = it->second;
      output << "M_.Sigma_e(" << id << ", " << id << ") = (";
      value->writeOutput(output);
      output << ")^2;" << endl;
104
105
106
    }
}

sebastien's avatar
sebastien committed
107
108
void
AbstractShocksStatement::writeCovarAndCorrShocks(ostream &output) const
109
{
sebastien's avatar
sebastien committed
110
111
112
  covar_and_corr_shocks_type::const_iterator it;

  for(it = covar_shocks.begin(); it != covar_shocks.end(); it++)
113
    {
sebastien's avatar
sebastien committed
114
115
      int id1 = symbol_table.getTypeSpecificID(it->first.first) + 1;
      int id2 = symbol_table.getTypeSpecificID(it->first.second) + 1;
sebastien's avatar
sebastien committed
116
117
118
119
      const NodeID value = it->second;
      output << "M_.Sigma_e(" << id1 << ", " << id2 << ") = ";
      value->writeOutput(output);
      output << "; M_.Sigma_e(" << id2 << ", " << id1 << ") = M_.Sigma_e("
sebastien's avatar
sebastien committed
120
             << id1 << ", " << id2 << ");\n";
121
    }
sebastien's avatar
sebastien committed
122
123

  for(it = corr_shocks.begin(); it != corr_shocks.end(); it++)
124
    {
sebastien's avatar
sebastien committed
125
126
      int id1 = symbol_table.getTypeSpecificID(it->first.first) + 1;
      int id2 = symbol_table.getTypeSpecificID(it->first.second) + 1;
sebastien's avatar
sebastien committed
127
128
129
130
      const NodeID value = it->second;
      output << "M_.Sigma_e(" << id1 << ", " << id2 << ") = ";
      value->writeOutput(output);
      output << "*sqrt(M_.Sigma_e(" << id1 << ", " << id1 << ")*M_.Sigma_e("
131
             << id2 << ", " << id2 << ")); M_.Sigma_e(" << id2 << ", "
sebastien's avatar
sebastien committed
132
             << id1 << ") = M_.Sigma_e(" << id1 << ", " << id2 << ");\n";
133
134
135
136
    }
}


sebastien's avatar
sebastien committed
137
138
139
140
141
142
143
144
ShocksStatement::ShocksStatement(const det_shocks_type &det_shocks_arg,
                                 const var_and_std_shocks_type &var_shocks_arg,
                                 const var_and_std_shocks_type &std_shocks_arg,
                                 const covar_and_corr_shocks_type &covar_shocks_arg,
                                 const covar_and_corr_shocks_type &corr_shocks_arg,
                                 const SymbolTable &symbol_table_arg) :
  AbstractShocksStatement(false, det_shocks_arg, var_shocks_arg, std_shocks_arg,
                          covar_shocks_arg, corr_shocks_arg, symbol_table_arg)
145
146
147
{
}

sebastien's avatar
sebastien committed
148
void
sebastien's avatar
sebastien committed
149
ShocksStatement::writeOutput(ostream &output, const string &basename) const
150
{
sebastien's avatar
sebastien committed
151
152
153
  output << "%" << endl
         << "% SHOCKS instructions" << endl
         << "%" << endl;
154

sebastien's avatar
sebastien committed
155
  // Write instruction that initializes a shock
sebastien's avatar
sebastien committed
156
  output << "make_ex_;" << endl;
sebastien's avatar
sebastien committed
157
158
159
160

  writeDetShocks(output);
  writeVarAndStdShocks(output);
  writeCovarAndCorrShocks(output);
161
162
}

sebastien's avatar
sebastien committed
163
164
165
166
167
168
169
170
MShocksStatement::MShocksStatement(const det_shocks_type &det_shocks_arg,
                                   const var_and_std_shocks_type &var_shocks_arg,
                                   const var_and_std_shocks_type &std_shocks_arg,
                                   const covar_and_corr_shocks_type &covar_shocks_arg,
                                   const covar_and_corr_shocks_type &corr_shocks_arg,
                                   const SymbolTable &symbol_table_arg) :
  AbstractShocksStatement(true, det_shocks_arg, var_shocks_arg, std_shocks_arg,
                          covar_shocks_arg, corr_shocks_arg, symbol_table_arg)
171
172
173
{
}

sebastien's avatar
sebastien committed
174
void
sebastien's avatar
sebastien committed
175
MShocksStatement::writeOutput(ostream &output, const string &basename) const
176
{
sebastien's avatar
sebastien committed
177
178
179
  output << "%" << endl
         << "% SHOCKS instructions" << endl
         << "%" << endl;
180

sebastien's avatar
sebastien committed
181
  // Write instruction that initializes a shock
sebastien's avatar
sebastien committed
182
  output << "make_ex_;" << endl;
183

sebastien's avatar
sebastien committed
184
185
186
  writeDetShocks(output);
  writeVarAndStdShocks(output);
  writeCovarAndCorrShocks(output);
187
}