2008-02-03 11:28:36 +01:00
/*
2022-04-26 11:32:07 +02:00
* Copyright © 2003 - 2022 Dynare Team
2008-02-03 11:28:36 +01:00
*
* 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
2021-06-09 16:52:20 +02:00
* along with Dynare . If not , see < https : //www.gnu.org/licenses/>.
2008-02-03 11:28:36 +01:00
*/
2009-10-16 19:23:57 +02:00
# include <cassert>
# include <cstdlib>
2008-02-03 11:28:36 +01:00
# include <iostream>
2018-06-04 12:31:07 +02:00
# include <utility>
2008-02-03 11:28:36 +01:00
# include "Shocks.hh"
AbstractShocksStatement : : AbstractShocksStatement ( bool mshocks_arg ,
2014-04-10 11:54:49 +02:00
bool overwrite_arg ,
2018-06-04 12:31:07 +02:00
det_shocks_t det_shocks_arg ,
2009-01-23 11:59:37 +01:00
const SymbolTable & symbol_table_arg ) :
2018-10-04 17:18:27 +02:00
mshocks { mshocks_arg } ,
overwrite { overwrite_arg } ,
det_shocks { move ( det_shocks_arg ) } ,
symbol_table { symbol_table_arg }
2008-02-03 11:28:36 +01:00
{
}
void
AbstractShocksStatement : : writeDetShocks ( ostream & output ) const
{
int exo_det_length = 0 ;
2019-12-16 19:42:59 +01:00
for ( const auto & [ id , shock_vec ] : det_shocks )
2022-06-03 16:24:26 +02:00
for ( bool exo_det = ( symbol_table . getType ( id ) = = SymbolType : : exogenousDet ) ;
const auto & [ period1 , period2 , value ] : shock_vec )
{
output < < " M_.det_shocks = [ M_.det_shocks; " < < endl
< < boolalpha
< < " struct('exo_det', " < < exo_det
< < " ,'exo_id', " < < symbol_table . getTypeSpecificID ( id ) + 1
< < " ,'multiplicative', " < < mshocks
< < " ,'periods', " < < period1 < < " : " < < period2
< < " ,'value', " ;
value - > writeOutput ( output ) ;
output < < " ) ]; " < < endl ;
2008-02-03 11:28:36 +01:00
2022-06-03 16:24:26 +02:00
if ( exo_det & & period2 > exo_det_length )
exo_det_length = period2 ;
}
2008-02-03 11:28:36 +01:00
output < < " M_.exo_det_length = " < < exo_det_length < < " ; \n " ;
}
2017-02-08 18:29:57 +01:00
void
AbstractShocksStatement : : writeJsonDetShocks ( ostream & output ) const
{
2019-04-03 16:32:52 +02:00
output < < R " ( " deterministic_shocks " : [) " ;
2022-06-03 16:24:26 +02:00
for ( bool printed_something { false } ;
const auto & [ id , shock_vec ] : det_shocks )
2017-02-08 18:29:57 +01:00
{
2022-06-03 16:24:26 +02:00
if ( exchange ( printed_something , true ) )
2017-02-08 18:29:57 +01:00
output < < " , " ;
2022-06-03 16:24:26 +02:00
output < < R " ({ " var " : " ) " << symbol_table.getName(id) << R " ( " , ) "
2019-04-03 16:32:52 +02:00
< < R " ( " values " : [) " ;
2022-06-03 16:24:26 +02:00
for ( bool printed_something2 { false } ;
const auto & [ period1 , period2 , value ] : shock_vec )
2017-06-14 07:01:31 +02:00
{
2022-06-03 16:24:26 +02:00
if ( exchange ( printed_something2 , true ) )
2017-06-14 07:01:31 +02:00
output < < " , " ;
2021-07-13 16:24:37 +02:00
output < < R " ({ " period1 " : ) " < < period1 < < " , "
< < R " ( " period2 " : ) " < < period2 < < " , "
2019-04-03 16:32:52 +02:00
< < R " ( " value " : " ) " ;
2021-07-13 16:24:37 +02:00
value - > writeJsonOutput ( output , { } , { } ) ;
2019-04-03 16:32:52 +02:00
output < < R " ( " } ) " ;
2017-06-14 07:01:31 +02:00
}
2017-02-08 18:29:57 +01:00
output < < " ]} " ;
}
output < < " ] " ;
}
2014-04-10 11:54:49 +02:00
ShocksStatement : : ShocksStatement ( bool overwrite_arg ,
2021-07-13 16:24:37 +02:00
det_shocks_t det_shocks_arg ,
2018-06-04 12:31:07 +02:00
var_and_std_shocks_t var_shocks_arg ,
var_and_std_shocks_t std_shocks_arg ,
covar_and_corr_shocks_t covar_shocks_arg ,
covar_and_corr_shocks_t corr_shocks_arg ,
2009-10-29 18:16:10 +01:00
const SymbolTable & symbol_table_arg ) :
2021-07-13 16:24:37 +02:00
AbstractShocksStatement { false , overwrite_arg , move ( det_shocks_arg ) , symbol_table_arg } ,
2018-10-04 17:18:27 +02:00
var_shocks { move ( var_shocks_arg ) } ,
std_shocks { move ( std_shocks_arg ) } ,
covar_shocks { move ( covar_shocks_arg ) } ,
corr_shocks { move ( corr_shocks_arg ) }
2009-10-29 18:16:10 +01:00
{
}
void
2022-06-24 17:10:12 +02:00
ShocksStatement : : writeOutput ( ostream & output , [[maybe_unused]] const string & basename ,
[[maybe_unused]] bool minimal_workspace ) const
2009-10-29 18:16:10 +01:00
{
output < < " % " < < endl
< < " % SHOCKS instructions " < < endl
< < " % " < < endl ;
2014-04-10 11:54:49 +02:00
if ( overwrite )
{
output < < " M_.det_shocks = []; " < < endl ;
output < < " M_.Sigma_e = zeros( " < < symbol_table . exo_nbr ( ) < < " , "
2017-06-14 07:01:31 +02:00
< < symbol_table . exo_nbr ( ) < < " ); " < < endl
< < " M_.Correlation_matrix = eye( " < < symbol_table . exo_nbr ( ) < < " , "
< < symbol_table . exo_nbr ( ) < < " ); " < < endl ;
2014-04-10 11:54:49 +02:00
if ( has_calibrated_measurement_errors ( ) )
output < < " M_.H = zeros( " < < symbol_table . observedVariablesNbr ( ) < < " , "
< < symbol_table . observedVariablesNbr ( ) < < " ); " < < endl
< < " M_.Correlation_matrix_ME = eye( " < < symbol_table . observedVariablesNbr ( ) < < " , "
< < symbol_table . observedVariablesNbr ( ) < < " ); " < < endl ;
else
output < < " M_.H = 0; " < < endl
< < " M_.Correlation_matrix_ME = 1; " < < endl ;
}
2009-10-29 18:16:10 +01:00
writeDetShocks ( output ) ;
writeVarAndStdShocks ( output ) ;
writeCovarAndCorrShocks ( output ) ;
2014-04-10 11:43:26 +02:00
/* M_.sigma_e_is_diagonal is initialized to 1 by ModFile.cc.
If there are no off - diagonal elements , and we are not in overwrite mode ,
then we don ' t reset it to 1 , since there might be previous shocks blocks
with off - diagonal elements . */
2009-10-29 18:16:10 +01:00
if ( covar_shocks . size ( ) + corr_shocks . size ( ) > 0 )
output < < " M_.sigma_e_is_diagonal = 0; " < < endl ;
2014-04-10 11:43:26 +02:00
else if ( overwrite )
2009-10-29 18:16:10 +01:00
output < < " M_.sigma_e_is_diagonal = 1; " < < endl ;
}
2017-02-08 18:29:57 +01:00
void
ShocksStatement : : writeJsonOutput ( ostream & output ) const
{
2019-04-03 16:32:52 +02:00
output < < R " ({ " statementName " : " shocks " ) "
2022-05-16 12:26:21 +02:00
< < R " (, " overwrite " : ) " < < boolalpha < < overwrite ;
2017-02-08 18:29:57 +01:00
if ( ! det_shocks . empty ( ) )
{
output < < " , " ;
writeJsonDetShocks ( output ) ;
}
2019-04-03 16:32:52 +02:00
output < < R " (, " variance " : [) " ;
2022-06-03 16:24:26 +02:00
for ( bool printed_something { false } ;
auto & [ id , value ] : var_shocks )
2017-02-08 18:29:57 +01:00
{
2022-06-03 16:24:26 +02:00
if ( exchange ( printed_something , true ) )
2017-02-08 18:29:57 +01:00
output < < " , " ;
2022-06-03 16:24:26 +02:00
output < < R " ({ " name " : " ) " << symbol_table.getName(id) << R " ( " , ) "
2019-04-03 16:32:52 +02:00
< < R " ( " variance " : " ) " ;
2022-06-03 16:24:26 +02:00
value - > writeJsonOutput ( output , { } , { } ) ;
2019-04-03 16:32:52 +02:00
output < < R " ( " } ) " ;
2017-02-08 18:29:57 +01:00
}
output < < " ] "
2019-04-03 16:32:52 +02:00
< < R " (, " stderr " : [) " ;
2022-06-03 16:24:26 +02:00
for ( bool printed_something { false } ;
auto & [ id , value ] : std_shocks )
2017-02-08 18:29:57 +01:00
{
2022-06-03 16:24:26 +02:00
if ( exchange ( printed_something , true ) )
2017-02-08 18:29:57 +01:00
output < < " , " ;
2022-06-03 16:24:26 +02:00
output < < R " ({ " name " : " ) " << symbol_table.getName(id) << R " ( " , ) "
2019-04-03 16:32:52 +02:00
< < R " ( " stderr " : " ) " ;
2022-06-03 16:24:26 +02:00
value - > writeJsonOutput ( output , { } , { } ) ;
2019-04-03 16:32:52 +02:00
output < < R " ( " } ) " ;
2017-02-08 18:29:57 +01:00
}
output < < " ] "
2019-04-03 16:32:52 +02:00
< < R " (, " covariance " : [) " ;
2022-06-03 16:24:26 +02:00
for ( bool printed_something { false } ;
auto & [ ids , value ] : covar_shocks )
2017-02-08 18:29:57 +01:00
{
2022-06-03 16:24:26 +02:00
if ( exchange ( printed_something , true ) )
2017-02-08 18:29:57 +01:00
output < < " , " ;
output < < " { "
2022-06-03 16:24:26 +02:00
< < R " ( " name " : " ) " << symbol_table.getName(ids.first) << R " ( " , ) "
< < R " ( " name2 " : " ) " << symbol_table.getName(ids.second) << R " ( " , ) "
2019-04-03 16:32:52 +02:00
< < R " ( " covariance " : " ) " ;
2022-06-03 16:24:26 +02:00
value - > writeJsonOutput ( output , { } , { } ) ;
2019-04-03 16:32:52 +02:00
output < < R " ( " } ) " ;
2017-02-08 18:29:57 +01:00
}
output < < " ] "
2019-04-03 16:32:52 +02:00
< < R " (, " correlation " : [) " ;
2022-06-03 16:24:26 +02:00
for ( bool printed_something { false } ;
auto & [ ids , value ] : corr_shocks )
2017-02-08 18:29:57 +01:00
{
2022-06-03 16:24:26 +02:00
if ( exchange ( printed_something , true ) )
2017-02-08 18:29:57 +01:00
output < < " , " ;
output < < " { "
2022-06-03 16:24:26 +02:00
< < R " ( " name " : " ) " << symbol_table.getName(ids.first) << R " ( " , ) "
< < R " ( " name2 " : " ) " << symbol_table.getName(ids.second) << R " ( " , ) "
2019-04-03 16:32:52 +02:00
< < R " ( " correlation " : " ) " ;
2022-06-03 16:24:26 +02:00
value - > writeJsonOutput ( output , { } , { } ) ;
2019-04-03 16:32:52 +02:00
output < < R " ( " } ) " ;
2017-02-08 18:29:57 +01:00
}
output < < " ] "
< < " } " ;
}
2010-04-14 15:03:41 +02:00
void
2022-06-02 10:50:21 +02:00
ShocksStatement : : writeVarOrStdShock ( ostream & output , const pair < int , expr_t > & it ,
2010-04-15 17:46:05 +02:00
bool stddev ) const
2010-04-14 15:03:41 +02:00
{
2022-06-02 10:50:21 +02:00
SymbolType type = symbol_table . getType ( it . first ) ;
assert ( type = = SymbolType : : exogenous | | symbol_table . isObservedVariable ( it . first ) ) ;
2010-04-14 15:03:41 +02:00
int id ;
2018-07-17 18:34:07 +02:00
if ( type = = SymbolType : : exogenous )
2010-04-14 15:03:41 +02:00
{
output < < " M_.Sigma_e( " ;
2022-06-02 10:50:21 +02:00
id = symbol_table . getTypeSpecificID ( it . first ) + 1 ;
2010-04-14 15:03:41 +02:00
}
else
{
output < < " M_.H( " ;
2022-06-02 10:50:21 +02:00
id = symbol_table . getObservedVariableIndex ( it . first ) + 1 ;
2010-04-14 15:03:41 +02:00
}
output < < id < < " , " < < id < < " ) = " ;
2010-04-15 17:46:05 +02:00
if ( stddev )
2010-04-14 15:03:41 +02:00
output < < " ( " ;
2022-06-02 10:50:21 +02:00
it . second - > writeOutput ( output ) ;
2010-04-15 17:46:05 +02:00
if ( stddev )
2010-04-14 15:03:41 +02:00
output < < " )^2 " ;
output < < " ; " < < endl ;
}
2009-10-29 18:16:10 +01:00
void
ShocksStatement : : writeVarAndStdShocks ( ostream & output ) const
2008-02-03 11:28:36 +01:00
{
2022-06-02 10:50:21 +02:00
for ( const auto & it : var_shocks )
2010-04-14 15:03:41 +02:00
writeVarOrStdShock ( output , it , false ) ;
2008-02-03 11:28:36 +01:00
2022-06-02 10:50:21 +02:00
for ( const auto & it : std_shocks )
2010-04-14 15:03:41 +02:00
writeVarOrStdShock ( output , it , true ) ;
}
void
2022-06-02 10:50:21 +02:00
ShocksStatement : : writeCovarOrCorrShock ( ostream & output , const pair < pair < int , int > , expr_t > & it ,
2010-04-14 15:03:41 +02:00
bool corr ) const
{
2022-06-02 10:50:21 +02:00
SymbolType type1 = symbol_table . getType ( it . first . first ) ;
SymbolType type2 = symbol_table . getType ( it . first . second ) ;
2018-07-17 18:34:07 +02:00
assert ( ( type1 = = SymbolType : : exogenous & & type2 = = SymbolType : : exogenous )
2022-06-02 10:50:21 +02:00
| | ( symbol_table . isObservedVariable ( it . first . first ) & & symbol_table . isObservedVariable ( it . first . second ) ) ) ;
2013-10-28 15:29:41 +01:00
string matrix , corr_matrix ;
2010-04-14 15:03:41 +02:00
int id1 , id2 ;
2018-07-17 18:34:07 +02:00
if ( type1 = = SymbolType : : exogenous )
2008-02-03 11:28:36 +01:00
{
2010-04-14 15:03:41 +02:00
matrix = " M_.Sigma_e " ;
2013-10-28 15:29:41 +01:00
corr_matrix = " M_.Correlation_matrix " ;
2022-06-02 10:50:21 +02:00
id1 = symbol_table . getTypeSpecificID ( it . first . first ) + 1 ;
id2 = symbol_table . getTypeSpecificID ( it . first . second ) + 1 ;
2010-04-14 15:03:41 +02:00
}
else
{
matrix = " M_.H " ;
2013-10-28 15:29:41 +01:00
corr_matrix = " M_.Correlation_matrix_ME " ;
2022-06-02 10:50:21 +02:00
id1 = symbol_table . getObservedVariableIndex ( it . first . first ) + 1 ;
id2 = symbol_table . getObservedVariableIndex ( it . first . second ) + 1 ;
2008-02-03 11:28:36 +01:00
}
2010-04-14 15:03:41 +02:00
output < < matrix < < " ( " < < id1 < < " , " < < id2 < < " ) = " ;
2022-06-02 10:50:21 +02:00
it . second - > writeOutput ( output ) ;
2010-04-14 15:03:41 +02:00
if ( corr )
output < < " *sqrt( " < < matrix < < " ( " < < id1 < < " , " < < id1 < < " )* "
< < matrix < < " ( " < < id2 < < " , " < < id2 < < " )) " ;
output < < " ; " < < endl
< < matrix < < " ( " < < id2 < < " , " < < id1 < < " ) = "
< < matrix < < " ( " < < id1 < < " , " < < id2 < < " ); " < < endl ;
2013-10-28 15:29:41 +01:00
if ( corr )
{
output < < corr_matrix < < " ( " < < id1 < < " , " < < id2 < < " ) = " ;
2022-06-02 10:50:21 +02:00
it . second - > writeOutput ( output ) ;
2013-10-28 15:29:41 +01:00
output < < " ; " < < endl
< < corr_matrix < < " ( " < < id2 < < " , " < < id1 < < " ) = "
< < corr_matrix < < " ( " < < id1 < < " , " < < id2 < < " ); " < < endl ;
}
2008-02-03 11:28:36 +01:00
}
void
2009-10-29 18:16:10 +01:00
ShocksStatement : : writeCovarAndCorrShocks ( ostream & output ) const
2008-02-03 11:28:36 +01:00
{
2022-06-02 10:50:21 +02:00
for ( const auto & it : covar_shocks )
2010-04-14 15:03:41 +02:00
writeCovarOrCorrShock ( output , it , false ) ;
2008-02-03 11:28:36 +01:00
2022-06-02 10:50:21 +02:00
for ( const auto & it : corr_shocks )
2010-04-14 15:03:41 +02:00
writeCovarOrCorrShock ( output , it , true ) ;
}
void
2022-06-24 17:10:12 +02:00
ShocksStatement : : checkPass ( ModFileStructure & mod_file_struct ,
[[maybe_unused]] WarningConsolidation & warnings )
2010-04-14 15:03:41 +02:00
{
2013-08-27 10:38:54 +02:00
/* Error out if variables are not of the right type. This must be done here
and not at parsing time ( see # 448 ) .
Also Determine if there is a calibrated measurement error */
2019-12-16 19:42:59 +01:00
for ( auto [ id , val ] : var_shocks )
2013-08-27 10:38:54 +02:00
{
2019-12-16 19:42:59 +01:00
if ( symbol_table . getType ( id ) ! = SymbolType : : exogenous
& & ! symbol_table . isObservedVariable ( id ) )
2013-08-27 10:38:54 +02:00
{
cerr < < " shocks: setting a variance on ' "
2019-12-16 19:42:59 +01:00
< < symbol_table . getName ( id ) < < " ' is not allowed, because it is neither an exogenous variable nor an observed endogenous variable " < < endl ;
2013-08-27 10:38:54 +02:00
exit ( EXIT_FAILURE ) ;
}
}
2010-04-14 15:03:41 +02:00
2019-12-16 19:42:59 +01:00
for ( auto [ id , val ] : std_shocks )
2013-08-27 10:38:54 +02:00
{
2019-12-16 19:42:59 +01:00
if ( symbol_table . getType ( id ) ! = SymbolType : : exogenous
& & ! symbol_table . isObservedVariable ( id ) )
2013-08-27 10:38:54 +02:00
{
cerr < < " shocks: setting a standard error on ' "
2019-12-16 19:42:59 +01:00
< < symbol_table . getName ( id ) < < " ' is not allowed, because it is neither an exogenous variable nor an observed endogenous variable " < < endl ;
2013-08-27 10:38:54 +02:00
exit ( EXIT_FAILURE ) ;
}
}
2010-04-14 15:03:41 +02:00
2019-12-16 19:42:59 +01:00
for ( const auto & [ ids , val ] : covar_shocks )
2013-08-27 10:38:54 +02:00
{
2022-06-02 10:50:21 +02:00
auto & [ symb_id1 , symb_id2 ] = ids ;
2017-06-14 07:01:31 +02:00
2018-07-17 18:34:07 +02:00
if ( ! ( ( symbol_table . getType ( symb_id1 ) = = SymbolType : : exogenous
& & symbol_table . getType ( symb_id2 ) = = SymbolType : : exogenous )
2013-08-27 10:38:54 +02:00
| | ( symbol_table . isObservedVariable ( symb_id1 )
& & symbol_table . isObservedVariable ( symb_id2 ) ) ) )
{
cerr < < " shocks: setting a covariance between ' "
< < symbol_table . getName ( symb_id1 ) < < " ' and ' "
< < symbol_table . getName ( symb_id2 ) < < " 'is not allowed; covariances can only be specified for exogenous or observed endogenous variables of same type " < < endl ;
exit ( EXIT_FAILURE ) ;
}
}
2010-04-14 15:03:41 +02:00
2019-12-16 19:42:59 +01:00
for ( const auto & [ ids , val ] : corr_shocks )
2013-08-27 10:38:54 +02:00
{
2022-06-02 10:50:21 +02:00
auto & [ symb_id1 , symb_id2 ] = ids ;
2017-06-14 07:01:31 +02:00
2018-07-17 18:34:07 +02:00
if ( ! ( ( symbol_table . getType ( symb_id1 ) = = SymbolType : : exogenous
& & symbol_table . getType ( symb_id2 ) = = SymbolType : : exogenous )
2013-08-27 10:38:54 +02:00
| | ( symbol_table . isObservedVariable ( symb_id1 )
& & symbol_table . isObservedVariable ( symb_id2 ) ) ) )
{
cerr < < " shocks: setting a correlation between ' "
< < symbol_table . getName ( symb_id1 ) < < " ' and ' "
< < symbol_table . getName ( symb_id2 ) < < " 'is not allowed; correlations can only be specified for exogenous or observed endogenous variables of same type " < < endl ;
exit ( EXIT_FAILURE ) ;
}
}
2013-11-29 14:50:58 +01:00
2014-04-10 11:54:49 +02:00
// Determine if there is a calibrated measurement error
mod_file_struct . calibrated_measurement_errors | = has_calibrated_measurement_errors ( ) ;
2013-11-29 14:50:58 +01:00
// Fill in mod_file_struct.parameters_with_shocks_values (related to #469)
2019-12-16 19:42:59 +01:00
for ( auto [ id , val ] : var_shocks )
val - > collectVariables ( SymbolType : : parameter , mod_file_struct . parameters_within_shocks_values ) ;
for ( auto [ id , val ] : std_shocks )
val - > collectVariables ( SymbolType : : parameter , mod_file_struct . parameters_within_shocks_values ) ;
for ( const auto & [ ids , val ] : covar_shocks )
val - > collectVariables ( SymbolType : : parameter , mod_file_struct . parameters_within_shocks_values ) ;
for ( const auto & [ ids , val ] : corr_shocks )
val - > collectVariables ( SymbolType : : parameter , mod_file_struct . parameters_within_shocks_values ) ;
2008-02-03 11:28:36 +01:00
}
2014-04-10 11:54:49 +02:00
bool
ShocksStatement : : has_calibrated_measurement_errors ( ) const
{
2019-12-16 19:42:59 +01:00
for ( auto [ id , val ] : var_shocks )
if ( symbol_table . isObservedVariable ( id ) )
2014-04-10 11:54:49 +02:00
return true ;
2019-12-16 19:42:59 +01:00
for ( auto [ id , val ] : std_shocks )
if ( symbol_table . isObservedVariable ( id ) )
2014-04-10 11:54:49 +02:00
return true ;
2019-12-16 19:42:59 +01:00
for ( const auto & [ ids , val ] : covar_shocks )
if ( symbol_table . isObservedVariable ( ids . first )
| | symbol_table . isObservedVariable ( ids . second ) )
2014-04-10 11:54:49 +02:00
return true ;
2019-12-16 19:42:59 +01:00
for ( const auto & [ ids , val ] : corr_shocks )
if ( symbol_table . isObservedVariable ( ids . first )
| | symbol_table . isObservedVariable ( ids . second ) )
2014-04-10 11:54:49 +02:00
return true ;
return false ;
}
MShocksStatement : : MShocksStatement ( bool overwrite_arg ,
2021-07-13 16:24:37 +02:00
det_shocks_t det_shocks_arg ,
2008-02-03 11:28:36 +01:00
const SymbolTable & symbol_table_arg ) :
2021-07-13 16:24:37 +02:00
AbstractShocksStatement { true , overwrite_arg , move ( det_shocks_arg ) , symbol_table_arg }
2008-02-03 11:28:36 +01:00
{
}
void
2022-06-24 17:10:12 +02:00
MShocksStatement : : writeOutput ( ostream & output , [[maybe_unused]] const string & basename ,
[[maybe_unused]] bool minimal_workspace ) const
2008-02-03 11:28:36 +01:00
{
2008-06-16 18:33:28 +02:00
output < < " % " < < endl
2009-10-29 18:16:10 +01:00
< < " % MSHOCKS instructions " < < endl
2008-06-16 18:33:28 +02:00
< < " % " < < endl ;
2008-02-03 11:28:36 +01:00
2014-04-10 11:54:49 +02:00
if ( overwrite )
output < < " M_.det_shocks = []; " < < endl ;
2008-02-03 11:28:36 +01:00
writeDetShocks ( output ) ;
}
2009-10-16 19:23:57 +02:00
2020-11-26 16:23:26 +01:00
void
MShocksStatement : : writeJsonOutput ( ostream & output ) const
{
output < < R " ({ " statementName " : " mshocks " ) "
2022-05-16 12:26:21 +02:00
< < R " (, " overwrite " : ) " < < boolalpha < < overwrite ;
2020-11-26 16:23:26 +01:00
if ( ! det_shocks . empty ( ) )
{
output < < " , " ;
writeJsonDetShocks ( output ) ;
}
output < < " } " ;
}
2021-07-13 17:07:53 +02:00
ShocksSurpriseStatement : : ShocksSurpriseStatement ( bool overwrite_arg ,
AbstractShocksStatement : : det_shocks_t surprise_shocks_arg ,
const SymbolTable & symbol_table_arg ) :
overwrite { overwrite_arg } , surprise_shocks { move ( surprise_shocks_arg ) } ,
symbol_table { symbol_table_arg }
{
}
void
2022-06-24 17:10:12 +02:00
ShocksSurpriseStatement : : checkPass ( ModFileStructure & mod_file_struct ,
[[maybe_unused]] WarningConsolidation & warnings )
2021-07-13 17:07:53 +02:00
{
mod_file_struct . shocks_surprise_present = true ;
}
void
2022-06-24 17:10:12 +02:00
ShocksSurpriseStatement : : writeOutput ( ostream & output , [[maybe_unused]] const string & basename ,
[[maybe_unused]] bool minimal_workspace ) const
2021-07-13 17:07:53 +02:00
{
if ( overwrite )
output < < " M_.surprise_shocks = [ " < < endl ;
else
output < < " M_.surprise_shocks = [ M_.surprise_shocks; " < < endl ;
for ( const auto & [ id , shock_vec ] : surprise_shocks )
2022-06-02 10:50:21 +02:00
for ( const auto & [ period1 , period2 , value ] : shock_vec )
{
output < < " struct('exo_id', " < < symbol_table . getTypeSpecificID ( id ) + 1
< < " ,'periods', " < < period1 < < " : " < < period2
< < " ,'value', " ;
value - > writeOutput ( output ) ;
output < < " ); " < < endl ;
}
2021-07-13 17:07:53 +02:00
output < < " ]; " < < endl ;
}
void
ShocksSurpriseStatement : : writeJsonOutput ( ostream & output ) const
{
output < < R " ({ " statementName " : " shocks " ) "
< < R " (, " surprise " : true) "
< < R " (, " surprise_shocks " : [) " ;
2022-06-03 16:24:26 +02:00
for ( bool printed_something { false } ;
const auto & [ id , shock_vec ] : surprise_shocks )
2021-07-13 17:07:53 +02:00
{
2022-06-03 16:24:26 +02:00
if ( exchange ( printed_something , true ) )
2021-07-13 17:07:53 +02:00
output < < " , " ;
2022-06-03 16:24:26 +02:00
output < < R " ({ " var " : " ) " << symbol_table.getName(id) << R " ( " , ) "
2021-07-13 17:07:53 +02:00
< < R " ( " values " : [) " ;
2022-06-03 16:24:26 +02:00
for ( bool printed_something2 { false } ;
const auto & [ period1 , period2 , value ] : shock_vec )
2021-07-13 17:07:53 +02:00
{
2022-06-03 16:24:26 +02:00
if ( exchange ( printed_something2 , true ) )
2021-07-13 17:07:53 +02:00
output < < " , " ;
output < < R " ({ " period1 " : ) " < < period1 < < " , "
< < R " ( " period2 " : ) " < < period2 < < " , "
< < R " ( " value " : " ) " ;
value - > writeJsonOutput ( output , { } , { } ) ;
output < < R " ( " } ) " ;
}
output < < " ]} " ;
}
output < < " ]} " ;
}
2022-04-20 16:57:39 +02:00
ShocksLearntInStatement : : ShocksLearntInStatement ( int learnt_in_period_arg , bool overwrite_arg ,
2022-04-26 15:21:45 +02:00
learnt_shocks_t learnt_shocks_arg ,
2022-04-20 16:57:39 +02:00
const SymbolTable & symbol_table_arg ) :
learnt_in_period { learnt_in_period_arg } , overwrite { overwrite_arg } ,
learnt_shocks { move ( learnt_shocks_arg ) } , symbol_table { symbol_table_arg }
{
}
void
2022-06-24 17:10:12 +02:00
ShocksLearntInStatement : : checkPass ( ModFileStructure & mod_file_struct ,
[[maybe_unused]] WarningConsolidation & warnings )
2022-04-20 16:57:39 +02:00
{
mod_file_struct . shocks_learnt_in_present = true ;
}
2022-04-26 15:21:45 +02:00
string
ShocksLearntInStatement : : typeToString ( LearntShockType type )
{
switch ( type )
{
case LearntShockType : : level :
return " level " ;
case LearntShockType : : add :
return " add " ;
case LearntShockType : : multiply :
return " multiply " ;
}
exit ( EXIT_FAILURE ) ; // Silence GCC warning
}
2022-04-20 16:57:39 +02:00
void
2022-06-24 17:10:12 +02:00
ShocksLearntInStatement : : writeOutput ( ostream & output , [[maybe_unused]] const string & basename ,
[[maybe_unused]] bool minimal_workspace ) const
2022-04-20 16:57:39 +02:00
{
if ( overwrite )
output < < " if ~isempty(M_.learnt_shocks) " < < endl
< < " M_.learnt_shocks = M_.learnt_shocks([M_.learnt_shocks.learnt_in] ~= " < < learnt_in_period < < " ); " < < endl
< < " end " < < endl ;
output < < " M_.learnt_shocks = [ M_.learnt_shocks; " < < endl ;
for ( const auto & [ id , shock_vec ] : learnt_shocks )
2022-06-02 10:50:21 +02:00
for ( const auto & [ type , period1 , period2 , value ] : shock_vec )
{
output < < " struct('learnt_in', " < < learnt_in_period
< < " ,'exo_id', " < < symbol_table . getTypeSpecificID ( id ) + 1
< < " ,'periods', " < < period1 < < " : " < < period2
< < " ,'type',' " < < typeToString ( type ) < < " ' "
< < " ,'value', " ;
value - > writeOutput ( output ) ;
output < < " ); " < < endl ;
}
2022-04-20 16:57:39 +02:00
output < < " ]; " < < endl ;
}
void
ShocksLearntInStatement : : writeJsonOutput ( ostream & output ) const
{
output < < R " ({ " statementName " : " shocks " ) "
< < R " (, " learnt_in " : ) " < < learnt_in_period
2022-05-16 12:26:21 +02:00
< < R " (, " overwrite " : ) " < < boolalpha < < overwrite
2022-04-20 16:57:39 +02:00
< < R " (, " learnt_shocks " : [) " ;
2022-06-03 16:24:26 +02:00
for ( bool printed_something { false } ;
const auto & [ id , shock_vec ] : learnt_shocks )
2022-04-20 16:57:39 +02:00
{
2022-06-03 16:24:26 +02:00
if ( exchange ( printed_something , true ) )
2022-04-20 16:57:39 +02:00
output < < " , " ;
2022-06-03 16:24:26 +02:00
output < < R " ({ " var " : " ) " << symbol_table.getName(id) << R " ( " , ) "
2022-04-20 16:57:39 +02:00
< < R " ( " values " : [) " ;
2022-06-03 16:24:26 +02:00
for ( bool printed_something2 { false } ;
const auto & [ type , period1 , period2 , value ] : shock_vec )
2022-04-20 16:57:39 +02:00
{
2022-06-03 16:24:26 +02:00
if ( exchange ( printed_something2 , true ) )
2022-04-20 16:57:39 +02:00
output < < " , " ;
output < < R " ({ " period1 " : ) " < < period1 < < " , "
< < R " ( " period2 " : ) " < < period2 < < " , "
2022-04-26 15:21:45 +02:00
< < R " ( " type " : " ) " << typeToString(type) << R " ( " , ) "
2022-04-20 16:57:39 +02:00
< < R " ( " value " : " ) " ;
value - > writeJsonOutput ( output , { } , { } ) ;
output < < R " ( " } ) " ;
}
output < < " ]} " ;
}
output < < " ]} " ;
}
2018-06-04 12:31:07 +02:00
ConditionalForecastPathsStatement : : ConditionalForecastPathsStatement ( AbstractShocksStatement : : det_shocks_t paths_arg ,
2016-09-06 14:43:06 +02:00
const SymbolTable & symbol_table_arg ) :
2018-10-04 17:18:27 +02:00
paths { move ( paths_arg ) } ,
2022-06-02 10:50:21 +02:00
symbol_table { symbol_table_arg } ,
path_length { computePathLength ( paths ) }
2009-10-16 19:23:57 +02:00
{
}
2022-06-02 10:50:21 +02:00
int
ConditionalForecastPathsStatement : : computePathLength ( const AbstractShocksStatement : : det_shocks_t & paths )
2009-10-16 19:23:57 +02:00
{
2022-06-02 10:50:21 +02:00
int length { 0 } ;
2021-07-13 16:24:37 +02:00
for ( const auto & [ ignore , elems ] : paths )
2022-06-02 10:50:21 +02:00
for ( auto & [ period1 , period2 , value ] : elems )
// Period1 < Period2, as enforced in ParsingDriver::add_period()
length = max ( length , period2 ) ;
return length ;
2009-10-16 19:23:57 +02:00
}
void
2022-06-24 17:10:12 +02:00
ConditionalForecastPathsStatement : : writeOutput ( ostream & output ,
[[maybe_unused]] const string & basename ,
[[maybe_unused]] bool minimal_workspace ) const
2009-10-16 19:23:57 +02:00
{
assert ( path_length > 0 ) ;
output < < " constrained_vars_ = []; " < < endl
2018-09-27 15:19:30 +02:00
< < " constrained_paths_ = NaN( " < < paths . size ( ) < < " , " < < path_length < < " ); " < < endl ;
2009-10-16 19:23:57 +02:00
2022-06-03 16:24:26 +02:00
for ( int k { 1 } ;
const auto & [ id , elems ] : paths )
2009-10-16 19:23:57 +02:00
{
2022-06-03 16:24:26 +02:00
if ( k = = 1 )
output < < " constrained_vars_ = " < < symbol_table . getTypeSpecificID ( id ) + 1 < < " ; " < < endl ;
2010-09-26 13:42:41 +02:00
else
2022-06-03 16:24:26 +02:00
output < < " constrained_vars_ = [constrained_vars_; " < < symbol_table . getTypeSpecificID ( id ) + 1 < < " ]; " < < endl ;
for ( const auto & [ period1 , period2 , value ] : elems )
2021-07-13 16:24:37 +02:00
for ( int j = period1 ; j < = period2 ; j + + )
2009-10-16 19:23:57 +02:00
{
output < < " constrained_paths_( " < < k < < " , " < < j < < " )= " ;
2021-07-13 16:24:37 +02:00
value - > writeOutput ( output ) ;
2009-10-16 19:23:57 +02:00
output < < " ; " < < endl ;
}
2022-06-03 16:24:26 +02:00
k + + ;
2009-10-16 19:23:57 +02:00
}
}
2014-02-10 19:22:54 +01:00
2019-03-29 17:02:29 +01:00
void
ConditionalForecastPathsStatement : : writeJsonOutput ( ostream & output ) const
{
2019-04-03 16:32:52 +02:00
output < < R " ({ " statementName " : " conditional_forecast_paths " ) "
< < R " (, " paths " : [) " ;
2022-06-03 16:24:26 +02:00
for ( bool printed_something { false } ;
const auto & [ id , elems ] : paths )
2019-03-29 17:02:29 +01:00
{
2022-06-03 16:24:26 +02:00
if ( exchange ( printed_something , true ) )
2019-03-29 17:02:29 +01:00
output < < " , " ;
2022-06-03 16:24:26 +02:00
output < < R " ({ " var " : " ) " << symbol_table.getName(id) << R " ( " , ) "
2019-04-03 16:32:52 +02:00
< < R " ( " values " : [) " ;
2022-06-03 16:24:26 +02:00
for ( bool printed_something2 { false } ;
const auto & [ period1 , period2 , value ] : elems )
2019-03-29 17:02:29 +01:00
{
2022-06-03 16:24:26 +02:00
if ( exchange ( printed_something2 , true ) )
2019-03-29 17:02:29 +01:00
output < < " , " ;
2021-07-13 16:24:37 +02:00
output < < R " ({ " period1 " : ) " < < period1 < < " , "
< < R " ( " period2 " : ) " < < period2 < < " , "
2019-04-03 16:32:52 +02:00
< < R " ( " value " : " ) " ;
2021-07-13 16:24:37 +02:00
value - > writeJsonOutput ( output , { } , { } ) ;
2019-04-03 16:32:52 +02:00
output < < R " ( " } ) " ;
2019-03-29 17:02:29 +01:00
}
output < < " ]} " ;
}
output < < " ]} " ;
}
2018-06-04 12:31:07 +02:00
MomentCalibration : : MomentCalibration ( constraints_t constraints_arg ,
2014-02-10 19:22:54 +01:00
const SymbolTable & symbol_table_arg )
2018-10-04 17:18:27 +02:00
: constraints { move ( constraints_arg ) } , symbol_table { symbol_table_arg }
2014-02-10 19:22:54 +01:00
{
}
void
2022-06-24 17:10:12 +02:00
MomentCalibration : : writeOutput ( ostream & output , [[maybe_unused]] const string & basename ,
[[maybe_unused]] bool minimal_workspace ) const
2014-02-10 19:22:54 +01:00
{
2014-03-17 16:45:43 +01:00
output < < " options_.endogenous_prior_restrictions.moment = { " < < endl ;
2019-12-20 16:59:30 +01:00
for ( const auto & c : constraints )
2014-02-10 19:22:54 +01:00
{
output < < " ' " < < symbol_table . getName ( c . endo1 ) < < " ', "
< < " ' " < < symbol_table . getName ( c . endo2 ) < < " ', "
2014-03-17 18:10:44 +01:00
< < c . lags < < " , "
2019-09-25 12:02:44 +02:00
< < " [ " ;
c . lower_bound - > writeOutput ( output ) ;
output < < " , " ;
c . upper_bound - > writeOutput ( output ) ;
output < < " ]; "
2014-02-10 19:22:54 +01:00
< < endl ;
}
output < < " }; " < < endl ;
}
2017-02-08 18:29:57 +01:00
void
MomentCalibration : : writeJsonOutput ( ostream & output ) const
{
2019-04-03 16:32:52 +02:00
output < < R " ({ " statementName " : " moment_calibration " ) "
< < R " (, " moment_calibration_criteria " : [) " ;
2022-06-03 16:24:26 +02:00
for ( bool printed_something { false } ;
const auto & c : constraints )
2017-02-08 18:29:57 +01:00
{
2022-06-03 16:24:26 +02:00
if ( exchange ( printed_something , true ) )
2017-02-08 18:29:57 +01:00
output < < " , " ;
2022-06-03 16:24:26 +02:00
output < < R " ({ " endogenous1 " : " ) " << symbol_table.getName(c.endo1) << R " ( " ) "
< < R " (, " endogenous2 " : " ) " << symbol_table.getName(c.endo2) << R " ( " ) "
< < R " (, " lags " : " ) " << c.lags << R " ( " ) "
2019-09-25 12:02:44 +02:00
< < R " (, " lower_bound " : " ) " ;
2022-06-03 16:24:26 +02:00
c . lower_bound - > writeJsonOutput ( output , { } , { } ) ;
2019-09-25 12:02:44 +02:00
output < < R " ( " ) "
< < R " (, " upper_bound " : " ) " ;
2022-06-03 16:24:26 +02:00
c . upper_bound - > writeJsonOutput ( output , { } , { } ) ;
2019-09-25 12:02:44 +02:00
output < < R " ( " ) "
2017-02-08 18:29:57 +01:00
< < " } " ;
}
output < < " ] "
< < " } " ;
}
2018-06-04 12:31:07 +02:00
IrfCalibration : : IrfCalibration ( constraints_t constraints_arg ,
2015-09-07 11:39:33 +02:00
const SymbolTable & symbol_table_arg ,
2018-06-04 12:31:07 +02:00
OptionsList options_list_arg )
2018-10-04 17:18:27 +02:00
: constraints { move ( constraints_arg ) } , symbol_table { symbol_table_arg } , options_list { move ( options_list_arg ) }
2014-02-10 19:22:54 +01:00
{
}
void
2022-06-24 17:10:12 +02:00
IrfCalibration : : writeOutput ( ostream & output , [[maybe_unused]] const string & basename ,
[[maybe_unused]] bool minimal_workspace ) const
2014-02-10 19:22:54 +01:00
{
2015-09-07 11:39:33 +02:00
options_list . writeOutput ( output ) ;
2014-03-17 16:45:43 +01:00
output < < " options_.endogenous_prior_restrictions.irf = { " < < endl ;
2019-12-20 16:59:30 +01:00
for ( const auto & c : constraints )
2014-02-10 19:22:54 +01:00
{
output < < " ' " < < symbol_table . getName ( c . endo ) < < " ', "
< < " ' " < < symbol_table . getName ( c . exo ) < < " ', "
2014-03-17 18:10:44 +01:00
< < c . periods < < " , "
2019-09-25 12:02:44 +02:00
< < " [ " ;
c . lower_bound - > writeOutput ( output ) ;
output < < " , " ;
c . upper_bound - > writeOutput ( output ) ;
output < < " ]; "
2014-02-10 19:22:54 +01:00
< < endl ;
}
output < < " }; " < < endl ;
}
2016-04-10 18:55:17 +02:00
2017-02-08 18:29:57 +01:00
void
IrfCalibration : : writeJsonOutput ( ostream & output ) const
{
2019-04-03 16:32:52 +02:00
output < < R " ({ " statementName " : " irf_calibration " ) " ;
2017-02-08 18:29:57 +01:00
if ( options_list . getNumberOfOptions ( ) )
{
output < < " , " ;
options_list . writeJsonOutput ( output ) ;
}
2019-04-03 16:32:52 +02:00
output < < R " (, " irf_restrictions " : [) " ;
2022-06-03 16:24:26 +02:00
for ( bool printed_something { false } ;
const auto & c : constraints )
2017-02-08 18:29:57 +01:00
{
2022-06-03 16:24:26 +02:00
if ( exchange ( printed_something , true ) )
2017-02-08 18:29:57 +01:00
output < < " , " ;
2022-06-03 16:24:26 +02:00
output < < R " ({ " endogenous " : " ) " << symbol_table.getName(c.endo) << R " ( " ) "
< < R " (, " exogenous " : " ) " << symbol_table.getName(c.exo) << R " ( " ) "
< < R " (, " periods " : " ) " << c.periods << R " ( " ) "
2019-09-25 12:02:44 +02:00
< < R " (, " lower_bound " : " ) " ;
2022-06-03 16:24:26 +02:00
c . lower_bound - > writeJsonOutput ( output , { } , { } ) ;
2019-09-25 12:02:44 +02:00
output < < R " ( " ) " ;
output < < R " (, " upper_bound " : " ) " ;
2022-06-03 16:24:26 +02:00
c . upper_bound - > writeJsonOutput ( output , { } , { } ) ;
2019-09-25 12:02:44 +02:00
output < < R " ( " ) "
2017-02-08 18:29:57 +01:00
< < " } " ;
}
output < < " ] "
< < " } " ;
}
2018-06-04 12:31:07 +02:00
ShockGroupsStatement : : ShockGroupsStatement ( group_t shock_groups_arg , string name_arg )
2018-10-04 17:18:27 +02:00
: shock_groups { move ( shock_groups_arg ) } , name { move ( name_arg ) }
2016-04-10 18:55:17 +02:00
{
}
void
2022-06-24 17:10:12 +02:00
ShockGroupsStatement : : writeOutput ( ostream & output , [[maybe_unused]] const string & basename ,
[[maybe_unused]] bool minimal_workspace ) const
2016-04-10 18:55:17 +02:00
{
2016-09-14 14:03:03 +02:00
int i = 1 ;
2022-06-02 10:50:21 +02:00
for ( auto it = shock_groups . begin ( ) ; it ! = shock_groups . end ( ) ; + + it )
2016-04-10 18:55:17 +02:00
{
2022-06-02 10:50:21 +02:00
bool unique_label { true } ;
2019-12-16 19:42:59 +01:00
for ( auto it1 = it + 1 ; it1 ! = shock_groups . end ( ) ; + + it1 )
2016-09-14 14:03:03 +02:00
if ( it - > name = = it1 - > name )
{
unique_label = false ;
cerr < < " Warning: shock group label ' " < < it - > name < < " ' has been reused. "
< < " Only using the last definition. " < < endl ;
break ;
}
if ( unique_label )
{
output < < " M_.shock_groups. " < < name
< < " .group " < < i < < " .label = ' " < < it - > name < < " '; " < < endl
< < " M_.shock_groups. " < < name
< < " .group " < < i < < " .shocks = { " ;
2019-12-20 16:59:30 +01:00
for ( const auto & it1 : it - > list )
2018-06-04 12:26:16 +02:00
output < < " ' " < < it1 < < " ' " ;
2016-09-14 14:03:03 +02:00
output < < " }; " < < endl ;
i + + ;
}
2016-04-10 18:55:17 +02:00
}
}
2019-04-12 14:16:42 +02:00
void
ShockGroupsStatement : : writeJsonOutput ( ostream & output ) const
{
2019-04-12 14:34:25 +02:00
output < < R " ({ " statementName " : " shock_groups " , " name " : " ) " << name << R " ( " , " groups " : [) " ;
2022-06-02 10:50:21 +02:00
bool printed_something { false } ;
for ( auto it = shock_groups . begin ( ) ; it ! = shock_groups . end ( ) ; + + it )
2019-04-12 14:16:42 +02:00
{
2022-06-02 10:50:21 +02:00
bool unique_label { true } ;
2019-12-16 19:42:59 +01:00
for ( auto it1 = it + 1 ; it1 ! = shock_groups . end ( ) ; + + it1 )
2019-04-12 14:16:42 +02:00
if ( it - > name = = it1 - > name )
{
unique_label = false ;
break ;
}
if ( unique_label )
{
2022-06-02 10:50:21 +02:00
if ( exchange ( printed_something , true ) )
2019-04-12 14:16:42 +02:00
output < < " , " ;
2019-04-12 14:34:25 +02:00
output < < R " ({ " group_name " : " ) " << it->name << R " ( " ,) "
2019-04-12 14:16:42 +02:00
< < R " ( " shocks " : [) " ;
2022-06-03 16:24:26 +02:00
for ( bool printed_something2 { false } ;
const auto & it1 : it - > list )
2019-04-12 14:16:42 +02:00
{
2022-06-03 16:24:26 +02:00
if ( exchange ( printed_something2 , true ) )
2019-04-12 14:16:42 +02:00
output < < " , " ;
2022-06-03 16:24:26 +02:00
output < < R " ( " ) " << it1 << R " ( " ) " ;
2019-04-12 14:16:42 +02:00
}
2019-04-12 14:34:25 +02:00
output < < " ]} " ;
2019-04-12 14:16:42 +02:00
}
}
2019-04-12 14:34:25 +02:00
output < < " ]} " ;
2019-04-12 14:16:42 +02:00
}
2019-12-02 11:18:21 +01:00
Init2shocksStatement : : Init2shocksStatement ( vector < pair < int , int > > init2shocks_arg , string name_arg ,
const SymbolTable & symbol_table_arg )
: init2shocks { move ( init2shocks_arg ) } , name { move ( name_arg ) } , symbol_table { symbol_table_arg }
{
}
void
2022-06-24 17:10:12 +02:00
Init2shocksStatement : : checkPass ( [[maybe_unused]] ModFileStructure & mod_file_struct ,
[[maybe_unused]] WarningConsolidation & warnings )
2019-12-02 11:18:21 +01:00
{
for ( size_t i = 0 ; i < init2shocks . size ( ) ; i + + )
for ( size_t j = i + 1 ; j < init2shocks . size ( ) ; j + + )
if ( init2shocks . at ( i ) . first = = init2shocks . at ( j ) . first )
{
cerr < < " Init2shocks( " < < name < < " ): enogenous variable ' "
< < symbol_table . getName ( init2shocks . at ( i ) . first )
< < " ' appears more than once in the init2shocks statement " < < endl ;
exit ( EXIT_FAILURE ) ;
}
}
void
2022-06-24 17:10:12 +02:00
Init2shocksStatement : : writeOutput ( ostream & output , [[maybe_unused]] const string & basename ,
[[maybe_unused]] bool minimal_workspace ) const
2019-12-02 11:18:21 +01:00
{
output < < " M_.init2shocks. " < < name < < " = { " < < endl ;
2022-06-02 10:50:21 +02:00
for ( const auto & [ id1 , id2 ] : init2shocks )
output < < " {' " < < symbol_table . getName ( id1 ) < < " ', ' " < < symbol_table . getName ( id2 ) < < " '}; " < < endl ;
2019-12-02 11:18:21 +01:00
output < < " }; " < < endl ;
}
void
Init2shocksStatement : : writeJsonOutput ( ostream & output ) const
{
output < < R " ({ " statementName " : " init2shocks " , " name " : " ) " << name << R " ( " , " groups " : [) " ;
2022-06-03 16:24:26 +02:00
for ( bool printed_something { false } ;
const auto & [ id1 , id2 ] : init2shocks )
2019-12-02 11:18:21 +01:00
{
2022-06-03 16:24:26 +02:00
if ( exchange ( printed_something , true ) )
2019-12-02 11:18:21 +01:00
output < < " , " ;
2022-06-03 16:24:26 +02:00
output < < R " ({ " endogenous " : " ) " << symbol_table.getName(id1) << R " ( " , ) "
< < R " ( " exogenous " : " ) " << symbol_table.getName(id2) << R " ( " }) " ;
2019-12-02 11:18:21 +01:00
}
output < < " ]} " ;
}
2021-05-26 18:00:06 +02:00
HeteroskedasticShocksStatement : : HeteroskedasticShocksStatement ( bool overwrite_arg ,
const heteroskedastic_shocks_t & values_arg ,
const heteroskedastic_shocks_t & scales_arg ,
const SymbolTable & symbol_table_arg )
: overwrite { overwrite_arg } , values { values_arg } , scales { scales_arg } , symbol_table { symbol_table_arg }
{
}
void
2022-06-24 17:10:12 +02:00
HeteroskedasticShocksStatement : : writeOutput ( ostream & output , [[maybe_unused]] const string & basename ,
[[maybe_unused]] bool minimal_workspace ) const
2021-05-26 18:00:06 +02:00
{
2021-06-15 12:53:40 +02:00
// NB: The first initialization of the fields is done in ModFile::writeMOutput()
2021-05-26 18:00:06 +02:00
if ( overwrite )
2021-06-15 12:53:40 +02:00
output < < " M_.heteroskedastic_shocks.Qvalue_orig = []; " < < endl
< < " M_.heteroskedastic_shocks.Qscale_orig = []; " < < endl ;
2021-05-26 18:00:06 +02:00
2021-06-15 12:53:40 +02:00
for ( const auto & [ symb_id , vec ] : values )
2022-06-03 16:24:26 +02:00
for ( int tsid = symbol_table . getTypeSpecificID ( symb_id ) ;
const auto & [ period1 , period2 , value ] : vec )
{
output < < " M_.heteroskedastic_shocks.Qvalue_orig = [M_.heteroskedastic_shocks.Qvalue_orig; struct('exo_id', "
< < tsid + 1 < < " ,'periods', "
< < period1 < < " : " < < period2 < < " ,'value', " ;
value - > writeOutput ( output ) ;
output < < " )]; " < < endl ;
}
2021-06-15 12:53:40 +02:00
for ( const auto & [ symb_id , vec ] : scales )
2022-06-03 16:24:26 +02:00
for ( int tsid = symbol_table . getTypeSpecificID ( symb_id ) ;
const auto & [ period1 , period2 , scale ] : vec )
{
output < < " M_.heteroskedastic_shocks.Qscale_orig = [M_.heteroskedastic_shocks.Qscale_orig; struct('exo_id', "
< < tsid + 1 < < " ,'periods', "
< < period1 < < " : " < < period2 < < " ,'scale', " ;
scale - > writeOutput ( output ) ;
output < < " )]; " < < endl ;
}
2021-05-26 18:00:06 +02:00
}
void
HeteroskedasticShocksStatement : : writeJsonOutput ( ostream & output ) const
{
output < < R " ({ " statementName " : " heteroskedastic_shocks " ) "
2022-05-16 12:26:21 +02:00
< < R " (, " overwrite " : ) " < < boolalpha < < overwrite
2021-05-26 18:00:06 +02:00
< < R " (, " shocks_values " : [) " ;
2022-06-03 16:24:26 +02:00
for ( bool printed_something { false } ;
const auto & [ symb_id , vec ] : values )
2021-05-26 18:00:06 +02:00
{
2022-06-03 16:24:26 +02:00
if ( exchange ( printed_something , true ) )
2021-05-26 18:00:06 +02:00
output < < " , " ;
2022-06-03 16:24:26 +02:00
output < < R " ({ " var " : " ) " << symbol_table.getName(symb_id) << R " ( " , ) "
2021-05-26 18:00:06 +02:00
< < R " ( " values " : [) " ;
2022-06-03 16:24:26 +02:00
for ( bool printed_something2 { false } ;
const auto & [ period1 , period2 , value ] : vec )
2021-05-26 18:00:06 +02:00
{
2022-06-03 16:24:26 +02:00
if ( exchange ( printed_something2 , true ) )
2021-05-26 18:00:06 +02:00
output < < " , " ;
output < < R " ({ " period1 " : ) " < < period1 < < " , "
< < R " ( " period2 " : ) " < < period2 < < " , "
< < R " ( " value " : " ) " ;
value - > writeJsonOutput ( output , { } , { } ) ;
output < < R " ( " } ) " ;
}
output < < " ]} " ;
}
output < < R " (], " shocks_scales " : [) " ;
2022-06-03 16:24:26 +02:00
for ( bool printed_something { false } ;
const auto & [ symb_id , vec ] : scales )
2021-05-26 18:00:06 +02:00
{
2022-06-03 16:24:26 +02:00
if ( exchange ( printed_something , true ) )
2021-05-26 18:00:06 +02:00
output < < " , " ;
2022-06-03 16:24:26 +02:00
output < < R " ({ " var " : " ) " << symbol_table.getName(symb_id) << R " ( " , ) "
2021-05-26 18:00:06 +02:00
< < R " ( " scales " : [) " ;
2022-06-03 16:24:26 +02:00
for ( bool printed_something2 { false } ;
const auto & [ period1 , period2 , value ] : vec )
2021-05-26 18:00:06 +02:00
{
2022-06-03 16:24:26 +02:00
if ( exchange ( printed_something2 , true ) )
2021-05-26 18:00:06 +02:00
output < < " , " ;
output < < R " ({ " period1 " : ) " < < period1 < < " , "
< < R " ( " period2 " : ) " < < period2 < < " , "
< < R " ( " value " : " ) " ;
value - > writeJsonOutput ( output , { } , { } ) ;
output < < R " ( " } ) " ;
}
output < < " ]} " ;
}
output < < " ]} " ;
}