Commit d05d1f87 authored by Sébastien Villemot's avatar Sébastien Villemot
Browse files

Merge remote-tracking branch 'houtanb/master'

parents 1d67940e 0da1aab3
...@@ -1244,6 +1244,14 @@ Base 10 logarithm. ...@@ -1244,6 +1244,14 @@ Base 10 logarithm.
Square root. Square root.
@end defun @end defun
@defun abs (@var{x})
Absolute value.
@end defun
@defun sign (@var{x})
Signum function.
@end defun
@defun sin (@var{x}) @defun sin (@var{x})
@defunx cos (@var{x}) @defunx cos (@var{x})
@defunx tan (@var{x}) @defunx tan (@var{x})
......
...@@ -130,15 +130,15 @@ tdf = 3; % degrees of freedom for t-dist for initial draw of the MC loo ...@@ -130,15 +130,15 @@ tdf = 3; % degrees of freedom for t-dist for initial draw of the MC loo
nbuffer = 1000; % a block or buffer of draws (buffer) that is saved to the disk (not memory) nbuffer = 1000; % a block or buffer of draws (buffer) that is saved to the disk (not memory)
ndraws1=1*nbuffer; % 1st part of Monte Carlo draws ndraws1=1*nbuffer; % 1st part of Monte Carlo draws
ndraws2=10*ndraws1; % 2nd part of Monte Carlo draws ndraws2=10*ndraws1; % 2nd part of Monte Carlo draws
seednumber = options_.DynareRandomStreams.seed; %7910; %472534; % if 0, random state at each clock time % seednumber = options_.DynareRandomStreams.seed; %7910; %472534; % if 0, random state at each clock time
% good one 420 for [29 45], [29 54] % % good one 420 for [29 45], [29 54]
if seednumber % if seednumber
randn('state',seednumber); % randn('state',seednumber);
rand('state',seednumber); % rand('state',seednumber);
else % else
randn('state',fix(100*sum(clock))); % randn('state',fix(100*sum(clock)));
rand('state',fix(100*sum(clock))); % rand('state',fix(100*sum(clock)));
end % end
% nstarts=1 % number of starting points % nstarts=1 % number of starting points
% imndraws = nstarts*ndraws2; % total draws for impulse responses or forecasts % imndraws = nstarts*ndraws2; % total draws for impulse responses or forecasts
%<<<<<<<<<<<<<<<<<<< %<<<<<<<<<<<<<<<<<<<
......
...@@ -188,6 +188,8 @@ enum UnaryOpcode ...@@ -188,6 +188,8 @@ enum UnaryOpcode
oAsinh, oAsinh,
oAtanh, oAtanh,
oSqrt, oSqrt,
oAbs,
oSign,
oSteadyState, oSteadyState,
oSteadyStateParamDeriv, // for the derivative of the STEADY_STATE operator w.r.t. to a parameter oSteadyStateParamDeriv, // for the derivative of the STEADY_STATE operator w.r.t. to a parameter
oSteadyStateParam2ndDeriv, // for the 2nd derivative of the STEADY_STATE operator w.r.t. to a parameter oSteadyStateParam2ndDeriv, // for the 2nd derivative of the STEADY_STATE operator w.r.t. to a parameter
......
...@@ -401,6 +401,28 @@ DataTree::AddSqrt(expr_t iArg1) ...@@ -401,6 +401,28 @@ DataTree::AddSqrt(expr_t iArg1)
return Zero; return Zero;
} }
expr_t
DataTree::AddAbs(expr_t iArg1)
{
if (iArg1 == Zero)
return Zero;
if (iArg1 == One)
return One;
else
return AddUnaryOp(oAbs, iArg1);
}
expr_t
DataTree::AddSign(expr_t iArg1)
{
if (iArg1 == Zero)
return Zero;
if (iArg1 == One)
return One;
else
return AddUnaryOp(oSign, iArg1);
}
expr_t expr_t
DataTree::AddErf(expr_t iArg1) DataTree::AddErf(expr_t iArg1)
{ {
......
...@@ -176,6 +176,10 @@ public: ...@@ -176,6 +176,10 @@ public:
expr_t AddAtanh(expr_t iArg1); expr_t AddAtanh(expr_t iArg1);
//! Adds "sqrt(arg)" to model tree //! Adds "sqrt(arg)" to model tree
expr_t AddSqrt(expr_t iArg1); expr_t AddSqrt(expr_t iArg1);
//! Adds "abs(arg)" to model tree
expr_t AddAbs(expr_t iArg1);
//! Adds "sign(arg)" to model tree
expr_t AddSign(expr_t iArg1);
//! Adds "erf(arg)" to model tree //! Adds "erf(arg)" to model tree
expr_t AddErf(expr_t iArg1); expr_t AddErf(expr_t iArg1);
//! Adds "max(arg1,arg2)" to model tree //! Adds "max(arg1,arg2)" to model tree
......
...@@ -108,7 +108,7 @@ class ParsingDriver; ...@@ -108,7 +108,7 @@ class ParsingDriver;
%token LABELS LAPLACE LIK_ALGO LIK_INIT LINEAR LOAD_IDENT_FILES LOAD_MH_FILE LOAD_PARAMS_AND_STEADY_STATE LOGLINEAR %token LABELS LAPLACE LIK_ALGO LIK_INIT LINEAR LOAD_IDENT_FILES LOAD_MH_FILE LOAD_PARAMS_AND_STEADY_STATE LOGLINEAR
%token MARKOWITZ MARGINAL_DENSITY MAX %token MARKOWITZ MARGINAL_DENSITY MAX
%token MFS MH_DROP MH_INIT_SCALE MH_JSCALE MH_MODE MH_NBLOCKS MH_REPLIC MH_RECOVER MIN MINIMAL_SOLVING_PERIODS %token MFS MH_DROP MH_INIT_SCALE MH_JSCALE MH_MODE MH_NBLOCKS MH_REPLIC MH_RECOVER MIN MINIMAL_SOLVING_PERIODS
%token MODE_CHECK MODE_COMPUTE MODE_FILE MODEL MODEL_COMPARISON MODEL_INFO MSHOCKS %token MODE_CHECK MODE_COMPUTE MODE_FILE MODEL MODEL_COMPARISON MODEL_INFO MSHOCKS ABS SIGN
%token MODIFIEDHARMONICMEAN MOMENTS_VARENDO DIFFUSE_FILTER %token MODIFIEDHARMONICMEAN MOMENTS_VARENDO DIFFUSE_FILTER
%token <string_val> NAME %token <string_val> NAME
%token NAN_CONSTANT NO_STATIC NOBS NOCONSTANT NOCORR NODIAGNOSTIC NOFUNCTIONS %token NAN_CONSTANT NO_STATIC NOBS NOCONSTANT NOCORR NODIAGNOSTIC NOFUNCTIONS
...@@ -459,6 +459,10 @@ expression : '(' expression ')' ...@@ -459,6 +459,10 @@ expression : '(' expression ')'
{ $$ = driver.add_atan($3); } { $$ = driver.add_atan($3); }
| SQRT '(' expression ')' | SQRT '(' expression ')'
{ $$ = driver.add_sqrt($3); } { $$ = driver.add_sqrt($3); }
| ABS '(' expression ')'
{ $$ = driver.add_abs($3); }
| SIGN '(' expression ')'
{ $$ = driver.add_sign($3); }
| MAX '(' expression COMMA expression ')' | MAX '(' expression COMMA expression ')'
{ $$ = driver.add_max($3, $5); } { $$ = driver.add_max($3, $5); }
| MIN '(' expression COMMA expression ')' | MIN '(' expression COMMA expression ')'
...@@ -613,6 +617,10 @@ hand_side : '(' hand_side ')' ...@@ -613,6 +617,10 @@ hand_side : '(' hand_side ')'
{ $$ = driver.add_atan($3); } { $$ = driver.add_atan($3); }
| SQRT '(' hand_side ')' | SQRT '(' hand_side ')'
{ $$ = driver.add_sqrt($3); } { $$ = driver.add_sqrt($3); }
| ABS '(' hand_side ')'
{ $$ = driver.add_abs($3); }
| SIGN '(' hand_side ')'
{ $$ = driver.add_sign($3); }
| MAX '(' hand_side COMMA hand_side ')' | MAX '(' hand_side COMMA hand_side ')'
{ $$ = driver.add_max($3, $5); } { $$ = driver.add_max($3, $5); }
| MIN '(' hand_side COMMA hand_side ')' | MIN '(' hand_side COMMA hand_side ')'
......
...@@ -511,6 +511,8 @@ string eofbuff; ...@@ -511,6 +511,8 @@ string eofbuff;
<DYNARE_STATEMENT,DYNARE_BLOCK>sqrt {return token::SQRT;} <DYNARE_STATEMENT,DYNARE_BLOCK>sqrt {return token::SQRT;}
<DYNARE_STATEMENT,DYNARE_BLOCK>max {return token::MAX;} <DYNARE_STATEMENT,DYNARE_BLOCK>max {return token::MAX;}
<DYNARE_STATEMENT,DYNARE_BLOCK>min {return token::MIN;} <DYNARE_STATEMENT,DYNARE_BLOCK>min {return token::MIN;}
<DYNARE_STATEMENT,DYNARE_BLOCK>abs {return token::ABS;}
<DYNARE_STATEMENT,DYNARE_BLOCK>sign {return token::SIGN;}
<DYNARE_STATEMENT,DYNARE_BLOCK>normcdf {return token::NORMCDF;} <DYNARE_STATEMENT,DYNARE_BLOCK>normcdf {return token::NORMCDF;}
<DYNARE_STATEMENT,DYNARE_BLOCK>normpdf {return token::NORMPDF;} <DYNARE_STATEMENT,DYNARE_BLOCK>normpdf {return token::NORMPDF;}
<DYNARE_STATEMENT,DYNARE_BLOCK>erf {return token::ERF;} <DYNARE_STATEMENT,DYNARE_BLOCK>erf {return token::ERF;}
......
...@@ -1323,6 +1323,11 @@ UnaryOpNode::composeDerivatives(expr_t darg, int deriv_id) ...@@ -1323,6 +1323,11 @@ UnaryOpNode::composeDerivatives(expr_t darg, int deriv_id)
case oSqrt: case oSqrt:
t11 = datatree.AddPlus(this, this); t11 = datatree.AddPlus(this, this);
return datatree.AddDivide(darg, t11); return datatree.AddDivide(darg, t11);
case oAbs:
t11 = datatree.AddSign(arg);
return datatree.AddTimes(t11, darg);
case oSign:
return datatree.Zero;
case oSteadyState: case oSteadyState:
if (datatree.isDynamic()) if (datatree.isDynamic())
{ {
...@@ -1406,6 +1411,7 @@ UnaryOpNode::cost(const temporary_terms_t &temporary_terms, bool is_matlab) cons ...@@ -1406,6 +1411,7 @@ UnaryOpNode::cost(const temporary_terms_t &temporary_terms, bool is_matlab) cons
switch (op_code) switch (op_code)
{ {
case oUminus: case oUminus:
case oSign:
return cost + 70; return cost + 70;
case oExp: case oExp:
return cost + 160; return cost + 160;
...@@ -1437,6 +1443,7 @@ UnaryOpNode::cost(const temporary_terms_t &temporary_terms, bool is_matlab) cons ...@@ -1437,6 +1443,7 @@ UnaryOpNode::cost(const temporary_terms_t &temporary_terms, bool is_matlab) cons
case oAtanh: case oAtanh:
return cost + 350; return cost + 350;
case oSqrt: case oSqrt:
case oAbs:
return cost + 570; return cost + 570;
case oSteadyState: case oSteadyState:
case oSteadyStateParamDeriv: case oSteadyStateParamDeriv:
...@@ -1449,6 +1456,7 @@ UnaryOpNode::cost(const temporary_terms_t &temporary_terms, bool is_matlab) cons ...@@ -1449,6 +1456,7 @@ UnaryOpNode::cost(const temporary_terms_t &temporary_terms, bool is_matlab) cons
switch (op_code) switch (op_code)
{ {
case oUminus: case oUminus:
case oSign:
return cost + 3; return cost + 3;
case oExp: case oExp:
case oAcosh: case oAcosh:
...@@ -1477,6 +1485,7 @@ UnaryOpNode::cost(const temporary_terms_t &temporary_terms, bool is_matlab) cons ...@@ -1477,6 +1485,7 @@ UnaryOpNode::cost(const temporary_terms_t &temporary_terms, bool is_matlab) cons
case oAtanh: case oAtanh:
return cost + 150; return cost + 150;
case oSqrt: case oSqrt:
case oAbs:
return cost + 90; return cost + 90;
case oSteadyState: case oSteadyState:
case oSteadyStateParamDeriv: case oSteadyStateParamDeriv:
...@@ -1622,6 +1631,15 @@ UnaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type, ...@@ -1622,6 +1631,15 @@ UnaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
case oSqrt: case oSqrt:
output << "sqrt"; output << "sqrt";
break; break;
case oAbs:
output << "abs";
break;
case oSign:
if (output_type == oCDynamicModel)
output << "copysign";
else
output << "sign";
break;
case oSteadyState: case oSteadyState:
ExprNodeOutputType new_output_type; ExprNodeOutputType new_output_type;
switch (output_type) switch (output_type)
...@@ -1692,6 +1710,8 @@ UnaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type, ...@@ -1692,6 +1710,8 @@ UnaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
&& arg->precedence(output_type, temporary_terms) < precedence(output_type, temporary_terms))) && arg->precedence(output_type, temporary_terms) < precedence(output_type, temporary_terms)))
{ {
output << LEFT_PAR(output_type); output << LEFT_PAR(output_type);
if (op_code == oSign && output_type == oCDynamicModel)
output << "1.0,";
close_parenthesis = true; close_parenthesis = true;
} }
...@@ -1763,6 +1783,10 @@ UnaryOpNode::eval_opcode(UnaryOpcode op_code, double v) throw (EvalException, Ev ...@@ -1763,6 +1783,10 @@ UnaryOpNode::eval_opcode(UnaryOpcode op_code, double v) throw (EvalException, Ev
return (atanh(v)); return (atanh(v));
case oSqrt: case oSqrt:
return (sqrt(v)); return (sqrt(v));
case oAbs:
return (abs(v));
case oSign:
return (v > 0) ? 1 : ((v < 0) ? -1 : 0);
case oSteadyState: case oSteadyState:
return (v); return (v);
case oSteadyStateParamDeriv: case oSteadyStateParamDeriv:
...@@ -1901,6 +1925,10 @@ UnaryOpNode::normalizeEquation(int var_endo, vector<pair<int, pair<expr_t, expr_ ...@@ -1901,6 +1925,10 @@ UnaryOpNode::normalizeEquation(int var_endo, vector<pair<int, pair<expr_t, expr_
return (make_pair(0, datatree.AddAtanh(New_expr_t))); return (make_pair(0, datatree.AddAtanh(New_expr_t)));
case oSqrt: case oSqrt:
return (make_pair(0, datatree.AddSqrt(New_expr_t))); return (make_pair(0, datatree.AddSqrt(New_expr_t)));
case oAbs:
return (make_pair(0, datatree.AddAbs(New_expr_t)));
case oSign:
return (make_pair(0, datatree.AddSign(New_expr_t)));
case oSteadyState: case oSteadyState:
return (make_pair(0, datatree.AddSteadyState(New_expr_t))); return (make_pair(0, datatree.AddSteadyState(New_expr_t)));
case oErf: case oErf:
...@@ -1960,6 +1988,10 @@ UnaryOpNode::buildSimilarUnaryOpNode(expr_t alt_arg, DataTree &alt_datatree) con ...@@ -1960,6 +1988,10 @@ UnaryOpNode::buildSimilarUnaryOpNode(expr_t alt_arg, DataTree &alt_datatree) con
return alt_datatree.AddAtanh(alt_arg); return alt_datatree.AddAtanh(alt_arg);
case oSqrt: case oSqrt:
return alt_datatree.AddSqrt(alt_arg); return alt_datatree.AddSqrt(alt_arg);
case oAbs:
return alt_datatree.AddAbs(alt_arg);
case oSign:
return alt_datatree.AddSign(alt_arg);
case oSteadyState: case oSteadyState:
return alt_datatree.AddSteadyState(alt_arg); return alt_datatree.AddSteadyState(alt_arg);
case oSteadyStateParamDeriv: case oSteadyStateParamDeriv:
......
...@@ -1746,6 +1746,18 @@ ParsingDriver::add_sqrt(expr_t arg1) ...@@ -1746,6 +1746,18 @@ ParsingDriver::add_sqrt(expr_t arg1)
return data_tree->AddSqrt(arg1); return data_tree->AddSqrt(arg1);
} }
expr_t
ParsingDriver::add_abs(expr_t arg1)
{
return data_tree->AddAbs(arg1);
}
expr_t
ParsingDriver::add_sign(expr_t arg1)
{
return data_tree->AddSign(arg1);
}
expr_t expr_t
ParsingDriver::add_max(expr_t arg1, expr_t arg2) ParsingDriver::add_max(expr_t arg1, expr_t arg2)
{ {
......
...@@ -485,6 +485,10 @@ public: ...@@ -485,6 +485,10 @@ public:
expr_t add_atanh(expr_t arg1); expr_t add_atanh(expr_t arg1);
//! Writes token "sqrt(arg1)" to model tree //! Writes token "sqrt(arg1)" to model tree
expr_t add_sqrt(expr_t arg1); expr_t add_sqrt(expr_t arg1);
//! Writes token "abs(arg1)" to model tree
expr_t add_abs(expr_t arg1);
//! Writes token "sign(arg1)" to model tree
expr_t add_sign(expr_t arg1);
//! Writes token "max(arg1,arg2)" to model tree //! Writes token "max(arg1,arg2)" to model tree
expr_t add_max(expr_t arg1, expr_t arg2); expr_t add_max(expr_t arg1, expr_t arg2);
//! Writes token "min(arg1,arg2)" to model tree //! Writes token "min(arg1,arg2)" to model tree
......
...@@ -8,6 +8,7 @@ MODFILES = \ ...@@ -8,6 +8,7 @@ MODFILES = \
example1_use_dll.mod \ example1_use_dll.mod \
example1_with_tags.mod \ example1_with_tags.mod \
example1_irf_shocks.mod \ example1_irf_shocks.mod \
example1_abs_sign_dynamic.mod \
t_sgu_ex1.mod \ t_sgu_ex1.mod \
osr_example.mod \ osr_example.mod \
optimal_policy/ramsey.mod \ optimal_policy/ramsey.mod \
......
// Example 1 from Collard's guide to Dynare
var y, c, k, a, h, b;
varexo e, u;
parameters beta, rho, alpha, delta, theta, psi, tau;
alpha = 0.36;
rho = 0.95;
tau = 0.025;
beta = 0.99;
delta = 0.025;
psi = 0;
theta = 2.95;
phi = 0.1;
model(use_dll);
c*theta*h^(1+psi)=abs((1-alpha)*y);
k = beta*(((exp(b)*c)/(exp(b(+1))*c(+1)))
*(exp(b(+1))*alpha*y(+1)+(1-delta)*k));
y = exp(a)*(k(-1)^alpha)*(h^(1-alpha));
k = exp(b)*(y-c)+(1-delta)*k(-1)*sign(k*k);
a = rho*a(-1)+tau*b(-1) + e;
b = tau*a(-1)+rho*b(-1) + u;
end;
initval;
y = 1.08068253095672;
c = 0.80359242014163;
h = 0.29175631001732;
k = 11.08360443260358;
a = 0;
b = 0;
e = 0;
u = 0;
end;
shocks;
var e; stderr 0.009;
var u; stderr 0.009;
var e, u = phi*0.009*0.009;
end;
stoch_simul;
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment