| 1 |
% ========================================================================= |
| 2 |
% Copyright © 2019-2020 Dynare Team |
| 3 |
% |
| 4 |
% This file is part of Dynare. |
| 5 |
% |
| 6 |
% Dynare is free software: you can redistribute it and/or modify |
| 7 |
% it under the terms of the GNU General Public License as published by |
| 8 |
% the Free Software Foundation, either version 3 of the License, or |
| 9 |
% (at your option) any later version. |
| 10 |
% |
| 11 |
% Dynare is distributed in the hope that it will be useful, |
| 12 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
% GNU General Public License for more details. |
| 15 |
% |
| 16 |
% You should have received a copy of the GNU General Public License |
| 17 |
% along with Dynare. If not, see <https://www.gnu.org/licenses/>. |
| 18 |
% ========================================================================= |
| 19 |
|
| 20 |
|
| 21 |
/* |
| 22 |
Check the policy functions obtained by perturbation at a high approximation |
| 23 |
order, using the Burnside (1998, JEDC) model (for which the analytical form of |
| 24 |
the policy function is known). |
| 25 |
|
| 26 |
As shown by Burnside, the policy function for yₜ is: |
| 27 |
|
| 28 |
yₜ = βⁱ exp[aᵢ+bᵢ(xₜ−xₛₛ)] |
| 29 |
|
| 30 |
|
| 31 |
where: |
| 32 |
θ² ⎛ 2ρ 1−ρ²ⁱ⎞ |
| 33 |
— aᵢ = iθxₛₛ + σ² ─────── ⎢i − ────(1−ρⁱ) + ρ² ─────⎥ |
| 34 |
2(1−ρ)² ⎝ 1−ρ 1−ρ² ⎠ |
| 35 |
|
| 36 |
θρ |
| 37 |
— bᵢ = ───(1−ρⁱ) |
| 38 |
1−ρ |
| 39 |
|
| 40 |
— xₛₛ is the steady state of x |
| 41 |
— σ is the standard deviation of e. |
| 42 |
|
| 43 |
With some algebra, it can be shown that the derivative of yₜ at the deterministic |
| 44 |
steady state is equal to: |
| 45 |
|
| 46 |
∂ᵐ⁺ⁿ⁺²ᵖ yₜ ∞ (2p)! |
| 47 |
──────────────── = ∑ βⁱ bᵢᵐ⁺ⁿ ρᵐ ───── cᵢᵖ exp(iθxₛₛ) |
| 48 |
∂ᵐxₜ₋₁ ∂ⁿeₜ ∂²ᵖs ⁱ⁼¹ p! |
| 49 |
|
| 50 |
where: |
| 51 |
— s is the stochastic scale factor |
| 52 |
|
| 53 |
θ² ⎛ 2ρ 1−ρ²ⁱ⎞ |
| 54 |
— cᵢ = ─────── ⎢i − ────(1−ρⁱ) + ρ² ─────⎥ |
| 55 |
2(1−ρ)² ⎝ 1−ρ 1−ρ² ⎠ |
| 56 |
|
| 57 |
Note that derivatives with respect to an odd order for s (i.e. ∂²ᵖ⁺¹s) are always |
| 58 |
equal to zero. |
| 59 |
|
| 60 |
The policy function as returned in the oo_.dr.g_* matrices has the following properties: |
| 61 |
— its elements are pre-multiplied by the Taylor coefficients; |
| 62 |
— derivatives w.r.t. the stochastic scale factor have already been summed up; |
| 63 |
— symmetric elements are folded (and they are not pre-multiplied by the number of repetitions). |
| 64 |
|
| 65 |
As a consequence, the element gₘₙ corresponding to the m-th derivative w.r.t. |
| 66 |
to xₜ₋₁ and the n-th derivative w.r.t. to eₜ is given by: |
| 67 |
|
| 68 |
1 ∞ cᵢᵖ |
| 69 |
gₘₙ = ────── ∑ ∑ βⁱ bᵢᵐ⁺ⁿ ρᵐ ──── exp(iθxₛₛ) |
| 70 |
(m+n)! 0≤2p≤k-m-n ⁱ⁼¹ p! |
| 71 |
|
| 72 |
where k is the order of approximation. |
| 73 |
|
| 74 |
*/ |
| 75 |
|
| 76 |
@#define ORDER = 3 |
| 77 |
|
| 78 |
var y x; |
| 79 |
varobs y; |
| 80 |
varexo e; |
| 81 |
|
| 82 |
parameters beta theta rho xbar; |
| 83 |
xbar = 0.0179; |
| 84 |
rho = -0.139; |
| 85 |
theta = -1.5; |
| 86 |
theta = -10; |
| 87 |
beta = 0.95; |
| 88 |
|
| 89 |
model; |
| 90 |
y = beta*exp(theta*x(+1))*(1+y(+1)); |
| 91 |
x = (1-rho)*xbar + rho*x(-1)+e; |
| 92 |
end; |
| 93 |
|
| 94 |
shocks; |
| 95 |
var e; stderr 0.0348; |
| 96 |
end; |
| 97 |
|
| 98 |
steady_state_model; |
| 99 |
x = xbar; |
| 100 |
y = beta*exp(theta*xbar)/(1-beta*exp(theta*xbar)); |
| 101 |
end; |
| 102 |
|
| 103 |
estimated_params; |
| 104 |
stderr e, normal_pdf, 0.0348,0.01; |
| 105 |
beta, normal_pdf, 0.95, 0.01; |
| 106 |
theta, normal_pdf, -10, 0.01; |
| 107 |
rho, normal_pdf, -0.139, 0.01; |
| 108 |
xbar, normal_pdf, 0.0179, 0.01; |
| 109 |
end; |
| 110 |
|
| 111 |
steady;check;model_diagnostics; |
| 112 |
stoch_simul(order=@{ORDER},k_order_solver,irf=0,drop=0,periods=0,nograph); |
| 113 |
identification(order=@{ORDER},nograph,no_identification_strength); |
| 114 |
|
| 115 |
%make sure everything is computed at prior mean |
| 116 |
[xparam_prior, estim_params_]= set_prior(estim_params_,M_,options_); |
| 117 |
M_ = set_all_parameters(xparam_prior,estim_params_,M_); |
| 118 |
[oo_.dr,info,M_.params] = resol(0,M_, options_, oo_.dr, oo_.steady_state, oo_.exo_steady_state, oo_.exo_det_steady_state); |
| 119 |
|
| 120 |
indpmodel = estim_params_.param_vals(:,1); |
| 121 |
indpstderr = estim_params_.var_exo(:,1); |
| 122 |
indpcorr = estim_params_.corrx(:,1:2); |
| 123 |
totparam_nbr = length(indpmodel) + length(indpstderr) + size(indpcorr,1); |
| 124 |
|
| 125 |
%% Verify that the policy function coefficients are correct |
| 126 |
i = 1:800; |
| 127 |
SE_e=sqrt(M_.Sigma_e); |
| 128 |
aux1 = rho*(1-rho.^i)/(1-rho); |
| 129 |
aux2 = (1-rho.^(2*i))/(1-rho^2); |
| 130 |
aux3 = 1/((1-rho)^2); |
| 131 |
aux4 = (i-2*aux1+rho^2*aux2); |
| 132 |
aux5=aux3*aux4; |
| 133 |
b = theta*aux1; |
| 134 |
c = 1/2*theta^2*SE_e^2*aux5; |
| 135 |
|
| 136 |
%derivatives wrt to rho only |
| 137 |
daux1_drho = zeros(1,length(i)); |
| 138 |
daux2_drho = zeros(1,length(i)); |
| 139 |
daux3_drho = 2/((1-rho)^3); |
| 140 |
daux4_drho = zeros(1,length(i)); |
| 141 |
daux5_drho = zeros(1,length(i)); |
| 142 |
for ii = 1:length(i) |
| 143 |
if ii == 1 |
| 144 |
daux1_drho(ii) = 1; |
| 145 |
daux2_drho(ii) = 0; |
| 146 |
else |
| 147 |
daux1_drho(ii) = rho/(rho^2 - 2*rho + 1) - 1/(rho - 1) + ((ii+1)*rho^ii)/(rho - 1) - rho^(ii+1)/(rho^2 - 2*rho + 1); |
| 148 |
daux2_drho(ii) = (2*rho)/(rho^4 - 2*rho^2 + 1) + (2*ii*rho^(2*ii-1))/(rho^2 - 1) - (2*rho^(2*ii+1))/(rho^4 - 2*rho^2 + 1); |
| 149 |
end |
| 150 |
daux4_drho(ii) = -2*daux1_drho(ii) + 2*rho*aux2(ii) + rho^2*daux2_drho(ii); |
| 151 |
daux5_drho(ii) = daux3_drho*aux4(ii) + aux3*daux4_drho(ii); |
| 152 |
end |
| 153 |
%derivatives of b and c wrt to all parameters |
| 154 |
db = zeros(size(b,1),size(b,2),M_.exo_nbr+M_.param_nbr); |
| 155 |
db(:,:,3) = aux1;%wrt theta |
| 156 |
db(:,:,4) = theta*daux1_drho;%wrt rho |
| 157 |
dc = zeros(size(c,1),size(c,2),M_.exo_nbr+M_.param_nbr); |
| 158 |
dc(:,:,1) = theta^2*SE_e*aux3*aux4;%wrt SE_e |
| 159 |
dc(:,:,3) = theta*SE_e^2*aux3*aux4;%wrt theta |
| 160 |
dc(:,:,4) = 1/2*theta^2*SE_e^2*daux5_drho; %wrt rho |
| 161 |
|
| 162 |
d2flag=0; |
| 163 |
g_0 = 1/2*oo_.dr.ghs2; if ~isequal(g_0,oo_.dr.g_0); error('something wrong'); end |
| 164 |
g_1 = [oo_.dr.ghx oo_.dr.ghu] +3/6*[oo_.dr.ghxss oo_.dr.ghuss]; if ~isequal(g_1,oo_.dr.g_1); error('something wrong'); end |
| 165 |
g_2 = 1/2*[oo_.dr.ghxx oo_.dr.ghxu oo_.dr.ghuu]; if ~isequal(g_2,oo_.dr.g_2); error('something wrong'); end |
| 166 |
g_3 = 1/6*[oo_.dr.ghxxx oo_.dr.ghxxu oo_.dr.ghxuu oo_.dr.ghuuu]; if ~isequal(g_3,oo_.dr.g_3); error('something wrong'); end |
| 167 |
|
| 168 |
tols = [1e-4 1e-4 1e-12 1e-12]; |
| 169 |
KRONFLAGS = [-1 -2 0 1]; |
| 170 |
for k = 1:length(KRONFLAGS) |
| 171 |
fprintf('KRONFLAG=%d\n',KRONFLAGS(k)); |
| 172 |
options_.analytic_derivation_mode = KRONFLAGS(k); |
| 173 |
DERIVS = identification.get_perturbation_params_derivs(M_, options_, estim_params_, oo_.dr, oo_.steady_state, oo_.exo_steady_state, oo_.exo_det_steady_state, indpmodel, indpstderr, indpcorr, d2flag); |
| 174 |
oo_.dr.dg_0 = permute(1/2*DERIVS.dghs2,[1 3 2]); |
| 175 |
oo_.dr.dg_1 = cat(2,DERIVS.dghx,DERIVS.dghu) + 3/6*cat(2,DERIVS.dghxss,DERIVS.dghuss); |
| 176 |
oo_.dr.dg_2 = 1/2*cat(2,DERIVS.dghxx,DERIVS.dghxu,DERIVS.dghuu); |
| 177 |
oo_.dr.dg_3 = 1/6*[DERIVS.dghxxx DERIVS.dghxxu DERIVS.dghxuu DERIVS.dghuuu]; |
| 178 |
|
| 179 |
for ord = 0:@{ORDER} |
| 180 |
g = oo_.dr.(['g_' num2str(ord)])(2,:); % Retrieve computed policy function for variable y |
| 181 |
dg = oo_.dr.(['dg_' num2str(ord)])(2,:,:); |
| 182 |
for m = 0:ord % m is the derivation order with respect to x(-1) |
| 183 |
v = 0; |
| 184 |
dv = zeros(1,M_.exo_nbr + M_.param_nbr); |
| 185 |
for p = 0:floor((@{ORDER}-ord)/2) % 2p is the derivation order with respect to s |
| 186 |
if ord+2*p > 0 % Skip the deterministic steady state constant |
| 187 |
v = v + sum(beta.^i.*exp(theta*xbar*i).*b.^ord.*rho^m.*c.^p)/factorial(ord)/factorial(p); |
| 188 |
%derivatives |
| 189 |
dv(:,1) = dv(:,1) + sum( beta.^i.*exp(theta*xbar*i).*ord.*b.^(ord-1).*db(:,:,1).*rho^m.*c.^p... |
| 190 |
+beta.^i.*exp(theta*xbar*i).*b.^ord.*rho^m.*p.*c.^(p-1).*dc(:,:,1)... |
| 191 |
)/factorial(ord)/factorial(p);%wrt SE_E |
| 192 |
dv(:,2) = dv(:,2) + sum( i.*beta.^(i-1).*exp(theta*xbar*i).*b.^ord.*rho^m.*c.^p... |
| 193 |
+beta.^i.*exp(theta*xbar*i).*ord.*b.^(ord-1).*db(:,:,2).*rho^m.*c.^p... |
| 194 |
+beta.^i.*exp(theta*xbar*i).*b.^ord.*rho^m.*p.*c.^(p-1).*dc(:,:,2)... |
| 195 |
)/factorial(ord)/factorial(p);%wrt beta |
| 196 |
dv(:,3) = dv(:,3) + sum( beta.^i.*exp(theta*xbar*i).*xbar.*i.*b.^ord.*rho^m.*c.^p... |
| 197 |
+beta.^i.*exp(theta*xbar*i).*ord.*b.^(ord-1).*db(:,:,3).*rho^m.*c.^p... |
| 198 |
+beta.^i.*exp(theta*xbar*i).*b.^ord.*rho^m.*p.*c.^(p-1).*dc(:,:,3)... |
| 199 |
)/factorial(ord)/factorial(p);%wrt theta |
| 200 |
dv(:,4) = dv(:,4) + sum( beta.^i.*exp(theta*xbar*i).*b.^ord.*m.*rho^(m-1).*c.^p... |
| 201 |
+beta.^i.*exp(theta*xbar*i).*ord.*b.^(ord-1).*db(:,:,4).*rho^m.*c.^p... |
| 202 |
+beta.^i.*exp(theta*xbar*i).*b.^ord.*rho^m.*p.*c.^(p-1).*dc(:,:,4)... |
| 203 |
)/factorial(ord)/factorial(p);%wrt rho |
| 204 |
dv(:,5) = dv(:,5) + sum( beta.^i.*exp(theta*xbar*i).*theta.*i.*b.^ord.*rho^m.*c.^p... |
| 205 |
+beta.^i.*exp(theta*xbar*i).*ord.*b.^(ord-1).*db(:,:,5).*rho^m.*c.^p... |
| 206 |
+beta.^i.*exp(theta*xbar*i).*b.^ord.*rho^m.*p.*c.^(p-1).*dc(:,:,5)... |
| 207 |
)/factorial(ord)/factorial(p);%wrt xbar |
| 208 |
end |
| 209 |
end |
| 210 |
if abs(v-g(ord+1-m)) > 1e-14 |
| 211 |
error(['Error in matrix oo_.dr.g_' num2str(ord)]) |
| 212 |
end |
| 213 |
chk_dg = squeeze(dg(:,ord+1-m,:))'; |
| 214 |
if isempty(indpstderr) |
| 215 |
chk_dv = dv(:,M_.exo_nbr+indpmodel); |
| 216 |
elseif isempty(indpmodel) |
| 217 |
chk_dv = dv(:,1:M_.exo_nbr); |
| 218 |
else |
| 219 |
chk_dv = dv; |
| 220 |
end |
| 221 |
fprintf('Max absolute deviation for dg_%d(2,%d,:): %e\n',ord,ord+1-m,norm( chk_dv - chk_dg, Inf)); |
| 222 |
if norm( chk_dv - chk_dg, Inf) > tols(k) |
| 223 |
error(['Error in matrix dg_' num2str(ord)]) |
| 224 |
chk_dv |
| 225 |
chk_dg |
| 226 |
end |
| 227 |
end |
| 228 |
end |
| 229 |
fprintf('\n'); |
| 230 |
end |