ms-sbvar: correcting bugs in specification of linear restrictions

issue#70
Michel Juillard 2011-11-07 12:26:20 +01:00
parent b8dcb71b78
commit 7854b06743
3 changed files with 52 additions and 54 deletions

View File

@ -707,9 +707,12 @@ svar_var_list : svar_var_list COMMA symbol
; ;
restriction_expression : expression {driver.check_restriction_expression_constant($1);} restriction_expression : expression {driver.check_restriction_expression_constant($1);}
| restriction_elem_expression | restriction_expression_1
| restriction_expression restriction_elem_expression ;
;
restriction_expression_1 : restriction_elem_expression
| restriction_expression_1 restriction_elem_expression
;
restriction_elem_expression : COEFF '(' symbol COMMA INT_NUMBER ')' restriction_elem_expression : COEFF '(' symbol COMMA INT_NUMBER ')'
{ driver.add_positive_restriction_element($3,$5);} { driver.add_positive_restriction_element($3,$5);}
@ -717,8 +720,6 @@ restriction_elem_expression : COEFF '(' symbol COMMA INT_NUMBER ')'
{ driver.add_negative_restriction_element($4,$6);} { driver.add_negative_restriction_element($4,$6);}
| expression TIMES COEFF '(' symbol COMMA INT_NUMBER ')' | expression TIMES COEFF '(' symbol COMMA INT_NUMBER ')'
{ driver.add_positive_restriction_element($1,$5,$7);} { driver.add_positive_restriction_element($1,$5,$7);}
| MINUS expression COEFF TIMES '(' symbol COMMA INT_NUMBER ')'
{ driver.add_negative_restriction_element($2,$6,$8);}
; ;
markov_switching : MARKOV_SWITCHING '(' ms_options_list ')' ';' markov_switching : MARKOV_SWITCHING '(' ms_options_list ')' ';'

View File

@ -834,6 +834,8 @@ ParsingDriver::add_restriction_equation_nbr(string *eq_nbr)
{ {
svar_equation_nbr = atoi(eq_nbr->c_str()); svar_equation_nbr = atoi(eq_nbr->c_str());
svar_left_handside = true; svar_left_handside = true;
// reinitialize restriction type that must be set from the first restriction element
svar_restriction_type = ParsingDriver::NOT_SET;
} }
void void
@ -848,94 +850,79 @@ ParsingDriver::add_restriction_equal()
void void
ParsingDriver::add_positive_restriction_element(expr_t value, string *variable, string *lag) ParsingDriver::add_positive_restriction_element(expr_t value, string *variable, string *lag)
{ {
check_symbol_existence(*variable);
int symb_id = mod_file->symbol_table.getID(*variable);
// if the expression is not on the left handside, change its sign // if the expression is not on the left handside, change its sign
if (!svar_left_handside) if (!svar_left_handside)
value = add_uminus(value); value = add_uminus(value);
int current_lag = atoi(lag->c_str()); add_restriction_element(value, variable, lag);
SvarIdentificationStatement::svar_identification_restriction new_restriction;
new_restriction.equation = svar_equation_nbr;
if (current_lag > 0)
new_restriction.restriction_nbr = ++svar_Ri_restriction_nbr[svar_equation_nbr];
else
new_restriction.restriction_nbr = ++svar_Qi_restriction_nbr[svar_equation_nbr];
new_restriction.lag = current_lag;
new_restriction.variable = symb_id;
new_restriction.value = value;
svar_ident_restrictions.push_back(new_restriction);
} }
void void
ParsingDriver::add_positive_restriction_element(string *variable, string *lag) ParsingDriver::add_positive_restriction_element(string *variable, string *lag)
{ {
check_symbol_existence(*variable);
int symb_id = mod_file->symbol_table.getID(*variable);
expr_t value(data_tree->One); expr_t value(data_tree->One);
// if the expression is not on the left handside, change its sign // if the expression is not on the left handside, change its sign
if (!svar_left_handside) if (!svar_left_handside)
value = add_uminus(value); value = add_uminus(value);
int current_lag = atoi(lag->c_str()); add_restriction_element(value, variable, lag);
SvarIdentificationStatement::svar_identification_restriction new_restriction;
new_restriction.equation = svar_equation_nbr;
if (current_lag > 0)
new_restriction.restriction_nbr = ++svar_Ri_restriction_nbr[svar_equation_nbr];
else
new_restriction.restriction_nbr = ++svar_Qi_restriction_nbr[svar_equation_nbr];
new_restriction.lag = current_lag;
new_restriction.variable = symb_id;
new_restriction.value = value;
svar_ident_restrictions.push_back(new_restriction);
} }
void void
ParsingDriver::add_negative_restriction_element(expr_t value, string *variable, string *lag) ParsingDriver::add_negative_restriction_element(expr_t value, string *variable, string *lag)
{ {
check_symbol_existence(*variable);
int symb_id = mod_file->symbol_table.getID(*variable);
// if the expression is on the left handside, change its sign // if the expression is on the left handside, change its sign
if (svar_left_handside) if (svar_left_handside)
value = add_uminus(value); value = add_uminus(value);
int current_lag = atoi(lag->c_str()); add_restriction_element(value, variable, lag);
SvarIdentificationStatement::svar_identification_restriction new_restriction;
new_restriction.equation = svar_equation_nbr;
if (current_lag > 0)
new_restriction.restriction_nbr = ++svar_Ri_restriction_nbr[svar_equation_nbr];
else
new_restriction.restriction_nbr = ++svar_Qi_restriction_nbr[svar_equation_nbr];
new_restriction.lag = current_lag;
new_restriction.variable = symb_id;
new_restriction.value = value;
svar_ident_restrictions.push_back(new_restriction);
} }
void void
ParsingDriver::add_negative_restriction_element(string *variable, string *lag) ParsingDriver::add_negative_restriction_element(string *variable, string *lag)
{ {
check_symbol_existence(*variable);
int symb_id = mod_file->symbol_table.getID(*variable);
expr_t value(data_tree->One); expr_t value(data_tree->One);
// if the expression is on the left handside, change its sign // if the expression is on the left handside, change its sign
if (svar_left_handside) if (svar_left_handside)
value = add_uminus(value); value = add_uminus(value);
add_restriction_element(value, variable, lag);
}
void
ParsingDriver::add_restriction_element(expr_t value, string *variable, string *lag)
{
check_symbol_existence(*variable);
int symb_id = mod_file->symbol_table.getID(*variable);
int current_lag = atoi(lag->c_str()); int current_lag = atoi(lag->c_str());
if (svar_restriction_type == ParsingDriver::NOT_SET)
{
if (current_lag == 0)
{
svar_restriction_type = ParsingDriver::Qi_TYPE;
++svar_Qi_restriction_nbr[svar_equation_nbr];
}
else
{
svar_restriction_type = ParsingDriver::Ri_TYPE;
++svar_Ri_restriction_nbr[svar_equation_nbr];
}
}
else
{
if ((svar_restriction_type == Qi_TYPE && current_lag > 0)
|| (svar_restriction_type == Ri_TYPE && current_lag == 0))
error("SVAR_IDENTIFICATION: a single restrictions must affect either Qi or Ri, but not both");
}
SvarIdentificationStatement::svar_identification_restriction new_restriction; SvarIdentificationStatement::svar_identification_restriction new_restriction;
new_restriction.equation = svar_equation_nbr; new_restriction.equation = svar_equation_nbr;
if (current_lag > 0) if (current_lag > 0)
new_restriction.restriction_nbr = ++svar_Ri_restriction_nbr[svar_equation_nbr]; new_restriction.restriction_nbr = svar_Ri_restriction_nbr[svar_equation_nbr];
else else
new_restriction.restriction_nbr = ++svar_Qi_restriction_nbr[svar_equation_nbr]; new_restriction.restriction_nbr = svar_Qi_restriction_nbr[svar_equation_nbr];
new_restriction.lag = current_lag; new_restriction.lag = current_lag;
new_restriction.variable = symb_id; new_restriction.variable = symb_id;
new_restriction.value = value; new_restriction.value = value;

View File

@ -159,6 +159,14 @@ private:
//! Temporary storage for current restriction number in svar_identification block //! Temporary storage for current restriction number in svar_identification block
map<int,int> svar_Qi_restriction_nbr; map<int,int> svar_Qi_restriction_nbr;
map<int,int> svar_Ri_restriction_nbr; map<int,int> svar_Ri_restriction_nbr;
//! Temporary storage for restriction type
enum SvarRestrictionType
{
NOT_SET,
Qi_TYPE,
Ri_TYPE
};
SvarRestrictionType svar_restriction_type;
//! Temporary storage for argument list of external function //! Temporary storage for argument list of external function
stack<vector<expr_t> > stack_external_function_args; stack<vector<expr_t> > stack_external_function_args;
@ -378,6 +386,8 @@ public:
void add_negative_restriction_element(expr_t value, string *variable, string *lag); void add_negative_restriction_element(expr_t value, string *variable, string *lag);
//! Svar_Idenditification Statement: add negative unit coefficient of a linear restriction //! Svar_Idenditification Statement: add negative unit coefficient of a linear restriction
void add_negative_restriction_element(string *variable, string *lag); void add_negative_restriction_element(string *variable, string *lag);
//! Svar_Idenditification Statement: add restriction element
void add_restriction_element(expr_t value, string *variable, string *lag);
//! Svar_Identification Statement: check that restriction is homogenous //! Svar_Identification Statement: check that restriction is homogenous
void check_restriction_expression_constant(expr_t value); void check_restriction_expression_constant(expr_t value);
//! Svar_Identification Statement: restriction of form upper cholesky //! Svar_Identification Statement: restriction of form upper cholesky