Unverified Commit 3c7865a9 authored by Sébastien Villemot's avatar Sébastien Villemot
Browse files

Merge branch 'rattoma/dynare-initial_estimation_checks_errors'

Ref. !1839
parents f4f40475 74121c2a
......@@ -5116,6 +5116,16 @@ block decomposition of the model (see :opt:`block`).
See :opt:`graph_format <graph_format = ( FORMAT, FORMAT... )>`.
.. option:: no_init_estimation_check_first_obs
Do not check for stochastic singularity in first period. If used, `ESTIMATION CHECKS`
does not return an error if the check fails only in first observation.
This should only be used when observing stock variables (e.g. capital) in first period, on top of their associated flow (e.g. investment).
Using this option may lead to a crash or provide undesired/wrong results for badly specified problems
(e.g. the additional variable observed in first period is not predetermined).
For advanced use only.
.. option:: lik_init = INTEGER
Type of initialization of Kalman filter:
......
......@@ -346,6 +346,7 @@ options_.ramsey.maxit = 500;
% estimation
options_.initial_period = NaN; %dates(1,1);
options_.no_init_estimation_check_first_obs=false;
options_.dataset.file = [];
options_.dataset.series = [];
options_.dataset.firstobs = dates();
......
......@@ -39,7 +39,8 @@ function DynareResults = initial_estimation_checks(objective_function,xparam1,Dy
%get maximum number of simultaneously observed variables for stochastic
%singularity check
maximum_number_non_missing_observations=max(sum(~isnan(DynareDataset.data),2));
maximum_number_non_missing_observations=max(sum(~isnan(DynareDataset.data(2:end,:)),2));
init_number_non_missing_observations=sum(~isnan(DynareDataset.data(1,:)),2);
if DynareOptions.order>1
if any(any(isnan(DynareDataset.data)))
......@@ -82,13 +83,33 @@ end
non_zero_ME=length(EstimatedParameters.H_entries_to_check_for_positive_definiteness);
print_init_check_warning=false;
if maximum_number_non_missing_observations>Model.exo_nbr+non_zero_ME
error(['initial_estimation_checks:: Estimation can''t take place because there are less declared shocks than observed variables!'])
end
if init_number_non_missing_observations>Model.exo_nbr+non_zero_ME
if DynareOptions.no_init_estimation_check_first_obs
print_init_check_warning=true;
else
error(['initial_estimation_checks:: Estimation can''t take place because there are less declared shocks than observed variables in first period!'])
end
end
if maximum_number_non_missing_observations>length(find(diag(Model.Sigma_e)))+non_zero_ME
error(['initial_estimation_checks:: Estimation can''t take place because too many shocks have been calibrated with a zero variance!'])
end
if init_number_non_missing_observations>length(find(diag(Model.Sigma_e)))+non_zero_ME
if DynareOptions.no_init_estimation_check_first_obs
print_init_check_warning=true;
else
error(['initial_estimation_checks:: Estimation can''t take place because too many shocks have been calibrated with a zero variance in first period!'])
end
end
if print_init_check_warning
fprintf('ESTIMATION_CHECKS: You decided to ignore test of stochastic singularity in first_obs.\n');
fprintf('ESTIMATION_CHECKS: If this was not done on purpose (typically when observing a stock variable [capital] in first period, on top of its flow [investment]),\n');
fprintf('ESTIMATION_CHECKS: it may lead to a crash or provide undesired/wrong results later on!\n');
end
if (any(BayesInfo.pshape >0 ) && DynareOptions.mh_replic) && DynareOptions.mh_nblck<1
error(['initial_estimation_checks:: Bayesian estimation cannot be conducted with mh_nblocks=0.'])
......
......@@ -54,6 +54,9 @@ wsOct
!/estimation/method_of_moments/RBC/RBC_Andreasen_Data_2.mat
!/estimation/method_of_moments/AFVRR/AFVRR_data.mat
!/estimation/method_of_moments/AFVRR/AFVRR_steady_helper.m
!estimation/no_init_estimation_check_first_obs/fsdat_mat.m
!estimation/no_init_estimation_check_first_obs/fsdat_mat_XFAIL.m
!estimation/no_init_estimation_check_first_obs/fsdat_simul.m
!/expectations/expectation_ss_old_steadystate.m
!/external_function/extFunDeriv.m
!/external_function/extFunNoDerivs.m
......
......@@ -60,6 +60,7 @@ MODFILES = \
estimation/method_of_moments/AFVRR/AFVRR_M0.mod \
estimation/method_of_moments/AFVRR/AFVRR_MFB.mod \
estimation/method_of_moments/AFVRR/AFVRR_MFB_RRA.mod \
estimation/no_init_estimation_check_first_obs/fs2000_init_check.mod \
moments/example1_var_decomp.mod \
moments/example1_bp_test.mod \
moments/test_AR1_spectral_density.mod \
......@@ -573,7 +574,9 @@ XFAIL_MODFILES = ramst_xfail.mod \
kalman_initial_state/fs2000_kalman_initial_xfail.mod \
example1_extra_exo_xfail.mod \
estimation/tune_mh_jscale/fs2000_1_xfail.mod \
estimation/tune_mh_jscale/fs2000_2_xfail.mod
estimation/tune_mh_jscale/fs2000_2_xfail.mod \
estimation/no_init_estimation_check_first_obs/fs2000_init_check_XFAIL.mod \
estimation/no_init_estimation_check_first_obs/fs2000_init_check_XFAIL2.mod
MFILES = histval_initval_file/ramst_initval_file_data.m
......
// Test for the no_init_estimation_check_first_obs option
var m P c e W R k d n l gy_obs gp_obs y dA;
varexo e_a e_m;
parameters alp bet gam mst rho psi del;
alp = 0.33;
bet = 0.99;
gam = 0.003;
mst = 1.011;
rho = 0.7;
psi = 0.787;
del = 0.02;
model;
dA = exp(gam+e_a);
log(m) = (1-rho)*log(mst) + rho*log(m(-1))+e_m;
-P/(c(+1)*P(+1)*m)+bet*P(+1)*(alp*exp(-alp*(gam+log(e(+1))))*k^(alp-1)*n(+1)^(1-alp)+(1-del)*exp(-(gam+log(e(+1)))))/(c(+2)*P(+2)*m(+1))=0;
W = l/n;
-(psi/(1-psi))*(c*P/(1-n))+l/n = 0;
R = P*(1-alp)*exp(-alp*(gam+e_a))*k(-1)^alp*n^(-alp)/W;
1/(c*P)-bet*P*(1-alp)*exp(-alp*(gam+e_a))*k(-1)^alp*n^(1-alp)/(m*l*c(+1)*P(+1)) = 0;
c+k = exp(-alp*(gam+e_a))*k(-1)^alp*n^(1-alp)+(1-del)*exp(-(gam+e_a))*k(-1);
P*c = m;
m-1+d = l;
e = exp(e_a);
y = k(-1)^alp*n^(1-alp)*exp(-alp*(gam+e_a));
gy_obs = dA*y/y(-1);
gp_obs = (P/P(-1))*m(-1)/dA;
end;
initval;
k = 6;
m = mst;
P = 2.25;
c = 0.45;
e = 1;
W = 4;
R = 1.02;
d = 0.85;
n = 0.19;
l = 0.86;
y = 0.6;
gy_obs = exp(gam);
gp_obs = exp(-gam);
dA = exp(gam);
end;
shocks;
var e_a; stderr 0.014;
var e_m; stderr 0.005;
end;
steady;
check;
estimated_params;
alp, beta_pdf, 0.356, 0.02;
bet, beta_pdf, 0.993, 0.002;
gam, normal_pdf, 0.0085, 0.003;
mst, normal_pdf, 1.0002, 0.007;
rho, beta_pdf, 0.129, 0.223;
psi, beta_pdf, 0.65, 0.05;
del, beta_pdf, 0.01, 0.005;
stderr e_a, inv_gamma_pdf, 0.035449, inf;
stderr e_m, inv_gamma_pdf, 0.008862, inf;
end;
varobs gp_obs gy_obs k;
options_.solve_tolf = 1e-12;
estimation(order=1,datafile=fsdat_mat,nobs=192,loglinear,mh_replic=0,use_univariate_filters_if_singularity_is_detected=0, smoother, consider_all_endogenous, no_init_estimation_check_first_obs);
// Test for the (absence of) the no_init_estimation_check_first_obs option
var m P c e W R k d n l gy_obs gp_obs y dA;
varexo e_a e_m;
parameters alp bet gam mst rho psi del;
alp = 0.33;
bet = 0.99;
gam = 0.003;
mst = 1.011;
rho = 0.7;
psi = 0.787;
del = 0.02;
model;
dA = exp(gam+e_a);
log(m) = (1-rho)*log(mst) + rho*log(m(-1))+e_m;
-P/(c(+1)*P(+1)*m)+bet*P(+1)*(alp*exp(-alp*(gam+log(e(+1))))*k^(alp-1)*n(+1)^(1-alp)+(1-del)*exp(-(gam+log(e(+1)))))/(c(+2)*P(+2)*m(+1))=0;
W = l/n;
-(psi/(1-psi))*(c*P/(1-n))+l/n = 0;
R = P*(1-alp)*exp(-alp*(gam+e_a))*k(-1)^alp*n^(-alp)/W;
1/(c*P)-bet*P*(1-alp)*exp(-alp*(gam+e_a))*k(-1)^alp*n^(1-alp)/(m*l*c(+1)*P(+1)) = 0;
c+k = exp(-alp*(gam+e_a))*k(-1)^alp*n^(1-alp)+(1-del)*exp(-(gam+e_a))*k(-1);
P*c = m;
m-1+d = l;
e = exp(e_a);
y = k(-1)^alp*n^(1-alp)*exp(-alp*(gam+e_a));
gy_obs = dA*y/y(-1);
gp_obs = (P/P(-1))*m(-1)/dA;
end;
initval;
k = 6;
m = mst;
P = 2.25;
c = 0.45;
e = 1;
W = 4;
R = 1.02;
d = 0.85;
n = 0.19;
l = 0.86;
y = 0.6;
gy_obs = exp(gam);
gp_obs = exp(-gam);
dA = exp(gam);
end;
shocks;
var e_a; stderr 0.014;
var e_m; stderr 0.005;
end;
steady;
check;
estimated_params;
alp, beta_pdf, 0.356, 0.02;
bet, beta_pdf, 0.993, 0.002;
gam, normal_pdf, 0.0085, 0.003;
mst, normal_pdf, 1.0002, 0.007;
rho, beta_pdf, 0.129, 0.223;
psi, beta_pdf, 0.65, 0.05;
del, beta_pdf, 0.01, 0.005;
stderr e_a, inv_gamma_pdf, 0.035449, inf;
stderr e_m, inv_gamma_pdf, 0.008862, inf;
end;
varobs gp_obs gy_obs k;
options_.solve_tolf = 1e-12;
estimation(order=1,datafile=fsdat_mat,nobs=192,loglinear,mh_replic=0,use_univariate_filters_if_singularity_is_detected=0, consider_all_endogenous, smoother);
// Test for the no_init_estimation_check_first_obs option
var m P c e W R k d n l gy_obs gp_obs y dA;
varexo e_a e_m;
parameters alp bet gam mst rho psi del;
alp = 0.33;
bet = 0.99;
gam = 0.003;
mst = 1.011;
rho = 0.7;
psi = 0.787;
del = 0.02;
model;
dA = exp(gam+e_a);
log(m) = (1-rho)*log(mst) + rho*log(m(-1))+e_m;
-P/(c(+1)*P(+1)*m)+bet*P(+1)*(alp*exp(-alp*(gam+log(e(+1))))*k^(alp-1)*n(+1)^(1-alp)+(1-del)*exp(-(gam+log(e(+1)))))/(c(+2)*P(+2)*m(+1))=0;
W = l/n;
-(psi/(1-psi))*(c*P/(1-n))+l/n = 0;
R = P*(1-alp)*exp(-alp*(gam+e_a))*k(-1)^alp*n^(-alp)/W;
1/(c*P)-bet*P*(1-alp)*exp(-alp*(gam+e_a))*k(-1)^alp*n^(1-alp)/(m*l*c(+1)*P(+1)) = 0;
c+k = exp(-alp*(gam+e_a))*k(-1)^alp*n^(1-alp)+(1-del)*exp(-(gam+e_a))*k(-1);
P*c = m;
m-1+d = l;
e = exp(e_a);
y = k(-1)^alp*n^(1-alp)*exp(-alp*(gam+e_a));
gy_obs = dA*y/y(-1);
gp_obs = (P/P(-1))*m(-1)/dA;
end;
initval;
k = 6;
m = mst;
P = 2.25;
c = 0.45;
e = 1;
W = 4;
R = 1.02;
d = 0.85;
n = 0.19;
l = 0.86;
y = 0.6;
gy_obs = exp(gam);
gp_obs = exp(-gam);
dA = exp(gam);
end;
shocks;
var e_a; stderr 0.014;
var e_m; stderr 0.005;
end;
steady;
check;
estimated_params;
alp, beta_pdf, 0.356, 0.02;
bet, beta_pdf, 0.993, 0.002;
gam, normal_pdf, 0.0085, 0.003;
mst, normal_pdf, 1.0002, 0.007;
rho, beta_pdf, 0.129, 0.223;
psi, beta_pdf, 0.65, 0.05;
del, beta_pdf, 0.01, 0.005;
stderr e_a, inv_gamma_pdf, 0.035449, inf;
stderr e_m, inv_gamma_pdf, 0.008862, inf;
end;
varobs gp_obs gy_obs k;
options_.solve_tolf = 1e-12;
estimation(order=1,datafile=fsdat_mat_XFAIL,nobs=192,loglinear,mh_replic=0,use_univariate_filters_if_singularity_is_detected=0, smoother, consider_all_endogenous, no_init_estimation_check_first_obs);
fsdat_simul;
k=nan(size(gp_obs));
k(1) = 20;
gy_obs =[
1.0030045
0.99990934
1.0172778
0.99464043
1.0253423
1.0150215
0.97772557
0.97832186
1.0159561
1.0085937
1.0102649
1.0007604
1.0112596
1.0163279
1.0173204
1.0103896
1.0006493
0.99447124
1.0196405
1.0089304
0.99650737
1.0139707
0.97865842
1.0192225
0.99139628
1.0141362
1.0196612
0.97483476
0.99686151
0.99594464
1.0000642
1.0172243
1.0025773
0.97199728
1.0217815
1.0219949
0.99490252
1.0190728
1.0111337
1.0003792
0.98969164
1.010438
1.0216309
1.0016671
1.0357588
0.98803787
1.0093457
1.0177035
0.98548204
1.0274294
1.0141377
1.0091174
0.96427632
1.0083272
1.0007882
0.99038262
1.0031336
0.99500213
0.98203716
0.9889452
1.011632
0.99451949
0.97291047
0.98750871
0.99992418
0.97657318
0.99930448
1.0008515
1.0044064
0.98133792
1.0091702
1.0087023
1.0119876
1.0143019
1.0311061
0.99340471
1.0057428
0.99197259
1.0071019
0.99448853
1.0061819
1.0070088
0.9950913
1.0302318
0.9817693
1.0072885
0.97355282
0.98782586
1.0136674
0.99863956
1.0205668
0.99611384
1.0073805
0.99691529
1.0089194
1.0030467
1.0112006
1.0260523
0.97803331
0.99423374
1.0043727
1.0140173
1.0111473
0.99524348
0.99775943
0.9958619
0.9982344
1.0210212
1.0022288
1.0014801
1.011456
1.0124871
0.99843599
0.99324886
0.99912838
1.003327
1.0072071
1.0115223
1.009266
1.0070554
1.0129916
1.0053413
1.0051638
0.99212952
1.0214422
0.98716707
0.99905788
0.98877357
0.98568476
0.99767393
1.0061791
0.98423439
0.99492949
0.98786999
0.99754239
1.0168619
0.99472384
1.0041658
0.98123181
1.0112882
0.99245422
1.0010255
1.0017799
1.0089968
1.0072824
0.99768475
1.0044726
1.0118678
1.0056385
1.0276965
1.0025122
1.0065161
1.0234338
0.99760167
0.98922272
1.0101918
1.011615
1.0085286
1.0074455
0.98866757
0.99959012
1.0129881
0.99127881
0.97971901
1.0185314
1.020054
1.0132605
0.98063643
0.99490253
1.0101531
1.0004526
1.0059109
0.98974491
1.0062391
1.0216488
0.99398446
0.97786609
1.0019274
0.99587153
1.0095881
1.0111887
0.99457649
0.97896734
1.000172
1.0142951
1.0034224
1.0037242
1.0016059
1.016556
0.99687023
1.0117844
1.0059212
0.98083159
0.98638851
1.0128713
1.0096232
1.0115891
1.0011213
1.0147105
1.0066344
1.0164429
0.99825038
0.99403411
];
gp_obs =[
1.0079715
1.0074573
1.0153107
1.0152677
1.0011653
0.99950061
1.0328311
1.0192317
1.009827
0.99588916
1.007474
1.0113061
0.98696624
0.99978663
0.98240542
0.98861723
0.99008763
1.0185076
1.0052452
0.99447194
1.0092685
1.01208
1.0105237
0.98513875
1.0165628
0.99485934
1.0050255
1.0140756
1.0093128
1.0155868
1.0107023
0.99212762
1.0095465
1.0028435
1.0069437
1.0070473
1.0145902
1.0186922
1.0059917
1.0113072
1.0107386
0.99769196
0.99793444
1.0050791
0.98307821
1.0107594
0.99689982
0.98667064
0.9991662
0.98274722
0.98422032
0.99393016
1.0118567
0.99912781
1.0023744
1.0086662
1.0164773
1.0169327
1.0372478
1.0314242
1.0004256
1.0110541
1.0076575
1.0119851
1.0055188
1.0213959
1.0234416
1.0264917
1.0292725
1.0385184
1.0200999