From 0b1888bc84537a9ccfe17ecaf30dce4e7ae03207 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 23 Feb 2010 18:16:52 +0100 Subject: [PATCH] Simple tests showing how to use external functions. --- ...1_1st_and_2nd_deriv_functions_provided.mod | 46 +++++++++++++++++++ .../example1_1st_deriv_function_provided.mod | 46 +++++++++++++++++++ .../example1_no_deriv_functions_provided.mod | 44 ++++++++++++++++++ tests/external_function/extFunDeriv.m | 5 ++ tests/external_function/extFunNoDerivs.m | 3 ++ .../extFunWithFirstAndSecondDerivs.m | 9 ++++ 6 files changed, 153 insertions(+) create mode 100644 tests/external_function/example1_1st_and_2nd_deriv_functions_provided.mod create mode 100644 tests/external_function/example1_1st_deriv_function_provided.mod create mode 100644 tests/external_function/example1_no_deriv_functions_provided.mod create mode 100644 tests/external_function/extFunDeriv.m create mode 100644 tests/external_function/extFunNoDerivs.m create mode 100644 tests/external_function/extFunWithFirstAndSecondDerivs.m diff --git a/tests/external_function/example1_1st_and_2nd_deriv_functions_provided.mod b/tests/external_function/example1_1st_and_2nd_deriv_functions_provided.mod new file mode 100644 index 000000000..8726ed43c --- /dev/null +++ b/tests/external_function/example1_1st_and_2nd_deriv_functions_provided.mod @@ -0,0 +1,46 @@ +// 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; + +external_function(nargs=2, name=extFunWithFirstAndSecondDerivs, first_deriv_provided, second_deriv_provided); + +model; +c*theta*h^(1+psi)=(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)+extFunWithFirstAndSecondDerivs((1-delta),k(-1)); +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; diff --git a/tests/external_function/example1_1st_deriv_function_provided.mod b/tests/external_function/example1_1st_deriv_function_provided.mod new file mode 100644 index 000000000..ee2f3d99f --- /dev/null +++ b/tests/external_function/example1_1st_deriv_function_provided.mod @@ -0,0 +1,46 @@ +// 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; + +external_function(nargs=2, name=extFunNoDerivs, first_deriv_provided=extFunDeriv); + +model; +c*theta*h^(1+psi)=(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)+extFunNoDerivs((1-delta),k(-1)); +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; diff --git a/tests/external_function/example1_no_deriv_functions_provided.mod b/tests/external_function/example1_no_deriv_functions_provided.mod new file mode 100644 index 000000000..e78cb7953 --- /dev/null +++ b/tests/external_function/example1_no_deriv_functions_provided.mod @@ -0,0 +1,44 @@ +// 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; +c*theta*h^(1+psi)=(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)+extFunNoDerivs((1-delta),k(-1)); +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; diff --git a/tests/external_function/extFunDeriv.m b/tests/external_function/extFunDeriv.m new file mode 100644 index 000000000..93d756590 --- /dev/null +++ b/tests/external_function/extFunDeriv.m @@ -0,0 +1,5 @@ +function df=extFunDeriv(a,b) +da=b^2; +db=2*a*b; +df=[da db]; +end \ No newline at end of file diff --git a/tests/external_function/extFunNoDerivs.m b/tests/external_function/extFunNoDerivs.m new file mode 100644 index 000000000..032f021d6 --- /dev/null +++ b/tests/external_function/extFunNoDerivs.m @@ -0,0 +1,3 @@ +function y=extFunNoDerivs(a,b) +y=a*(b^2); +end \ No newline at end of file diff --git a/tests/external_function/extFunWithFirstAndSecondDerivs.m b/tests/external_function/extFunWithFirstAndSecondDerivs.m new file mode 100644 index 000000000..d22c9b3f7 --- /dev/null +++ b/tests/external_function/extFunWithFirstAndSecondDerivs.m @@ -0,0 +1,9 @@ +function [y df d2f]=extFunWithFirstAndSecondDerivs(a,b) +y=a*(b^2); + +da=b^2; +db=2*a*b; +df=[da db]; + +d2f=[0 2*b; 2*b 2*a]; +end \ No newline at end of file