initial_estimation_checks.m 11.2 KB
Newer Older
1
function DynareResults = initial_estimation_checks(objective_function,xparam1,DynareDataset,DatasetInfo,Model,EstimatedParameters,DynareOptions,BayesInfo,BoundsInfo,DynareResults)
2
% function DynareResults = initial_estimation_checks(objective_function,xparam1,DynareDataset,DatasetInfo,Model,EstimatedParameters,DynareOptions,BayesInfo,BoundsInfo,DynareResults)
assia's avatar
assia committed
3
% Checks data (complex values, ML evaluation, initial values, BK conditions,..)
4
%
assia's avatar
assia committed
5
% INPUTS
6
7
%   objective_function  [function handle] of the objective function
%   xparam1:            [vector] of parameters to be estimated
8
9
%   DynareDataset:      [dseries] object storing the dataset
%   DataSetInfo:        [structure] storing informations about the sample.
10
11
12
13
%   Model:              [structure] decribing the model
%   EstimatedParameters [structure] characterizing parameters to be estimated
%   DynareOptions       [structure] describing the options
%   BayesInfo           [structure] describing the priors
14
%   BoundsInfo          [structure] containing prior bounds
15
%   DynareResults       [structure] storing the results
16
%
assia's avatar
assia committed
17
% OUTPUTS
18
%    DynareResults     structure of temporary results
19
%
assia's avatar
assia committed
20
21
22
% SPECIAL REQUIREMENTS
%    none

23
% Copyright (C) 2003-2018 Dynare Team
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
assia's avatar
assia committed
39

40
41
42
43
%get maximum number of simultaneously observed variables for stochastic
%singularity check
maximum_number_non_missing_observations=max(sum(~isnan(DynareDataset.data),2));

Stéphane Adjemian's avatar
Stéphane Adjemian committed
44
if DynareOptions.order>1
45
46
47
48
49
50
51
52
53
    if any(any(isnan(DynareDataset.data)))
        error('initial_estimation_checks:: particle filtering does not support missing observations')
    end
    if DynareOptions.prefilter==1
        error('initial_estimation_checks:: particle filtering does not support the prefilter option')
    end
    if BayesInfo.with_trend
        error('initial_estimation_checks:: particle filtering does not support trends')
    end
54
55
56
    if Model.H==0
        error('initial_estimation_checks:: particle filtering requires measurement error on the observables')
    else
Stéphane Adjemian's avatar
Stéphane Adjemian committed
57
58
59
60
61
62
63
        if sum(diag(Model.H)>0)<length(DynareOptions.varobs)
            error('initial_estimation_checks:: particle filtering requires as many measurement errors as observed variables')
        else
            [~,flag]=chol(Model.H);
            if flag
                error('initial_estimation_checks:: the measurement error matrix must be positive definite')
            end
64
65
        end
    end
66
67
68
    if DynareOptions.order>2 && DynareOptions.particle.pruning==1
        error('initial_estimation_checks:: the particle filter with order>2 does not support pruning')
    end
69
70
end

71
72
73
non_zero_ME=length(EstimatedParameters.H_entries_to_check_for_positive_definiteness);

if maximum_number_non_missing_observations>Model.exo_nbr+non_zero_ME
74
75
    error(['initial_estimation_checks:: Estimation can''t take place because there are less declared shocks than observed variables!'])
end
76

77
if maximum_number_non_missing_observations>length(find(diag(Model.Sigma_e)))+non_zero_ME
78
    error(['initial_estimation_checks:: Estimation can''t take place because too many shocks have been calibrated with a zero variance!'])
79
80
end

81
if (any(BayesInfo.pshape  >0 ) && DynareOptions.mh_replic) && DynareOptions.mh_nblck<1
82
    error(['initial_estimation_checks:: Bayesian estimation cannot be conducted with mh_nblocks=0.'])
83
84
end

85
86
87
88
89
old_steady_params=Model.params; %save initial parameters for check if steady state changes param values

% % check if steady state solves static model (except if diffuse_filter == 1)
[DynareResults.steady_state, new_steady_params] = evaluate_steady_state(DynareResults.steady_state,Model,DynareOptions,DynareResults,DynareOptions.diffuse_filter==0);

90
if isfield(EstimatedParameters,'param_vals') && ~isempty(EstimatedParameters.param_vals)
91
92
93
    %check whether steady state file changes estimated parameters
    Model_par_varied=Model; %store Model structure
    Model_par_varied.params(EstimatedParameters.param_vals(:,1))=Model_par_varied.params(EstimatedParameters.param_vals(:,1))*1.01; %vary parameters
94
    [~, new_steady_params_2] = evaluate_steady_state(DynareResults.steady_state,Model_par_varied,DynareOptions,DynareResults,DynareOptions.diffuse_filter==0);
95

96
    changed_par_indices=find((old_steady_params(EstimatedParameters.param_vals(:,1))-new_steady_params(EstimatedParameters.param_vals(:,1))) ...
97
                             | (Model_par_varied.params(EstimatedParameters.param_vals(:,1))-new_steady_params_2(EstimatedParameters.param_vals(:,1))));
98

99
100
    if ~isempty(changed_par_indices)
        fprintf('\nThe steady state file internally changed the values of the following estimated parameters:\n')
101
        disp(char(Model.param_names(EstimatedParameters.param_vals(changed_par_indices,1))))
102
        fprintf('This will override the parameter values drawn from the proposal density and may lead to wrong results.\n')
103
        fprintf('Check whether this is really intended.\n')
104
105
        warning('The steady state file internally changes the values of the estimated parameters.')
    end
106
end
107

108
109
if any(BayesInfo.pshape) % if Bayesian estimation
    nvx=EstimatedParameters.nvx;
110
    if nvx && any(BayesInfo.p3(1:nvx)<0)
111
112
113
114
        warning('Your prior allows for negative standard deviations for structural shocks. Due to working with variances, Dynare will be able to continue, but it is recommended to change your prior.')
    end
    offset=nvx;
    nvn=EstimatedParameters.nvn;
115
    if nvn && any(BayesInfo.p3(1+offset:offset+nvn)<0)
116
117
118
        warning('Your prior allows for negative standard deviations for measurement error. Due to working with variances, Dynare will be able to continue, but it is recommended to change your prior.')
    end
    offset = nvx+nvn;
119
120
    ncx=EstimatedParameters.ncx;
    if ncx && (any(BayesInfo.p3(1+offset:offset+ncx)<-1) || any(BayesInfo.p4(1+offset:offset+ncx)>1))
121
        warning('Your prior allows for correlations between structural shocks larger than +-1 and will not integrate to 1 due to truncation. Please change your prior')
122
123
    end
    offset = nvx+nvn+ncx;
124
125
    ncn=EstimatedParameters.ncn;
    if ncn && (any(BayesInfo.p3(1+offset:offset+ncn)<-1) || any(BayesInfo.p4(1+offset:offset+ncn)>1))
126
        warning('Your prior allows for correlations between measurement errors larger than +-1 and will not integrate to 1 due to truncation. Please change your prior')
127
128
129
    end
end

130
% display warning if some parameters are still NaN
131
test_for_deep_parameters_calibration(Model);
132

133
[lnprior,~,~,info]= priordens(xparam1,BayesInfo.pshape,BayesInfo.p6,BayesInfo.p7,BayesInfo.p3,BayesInfo.p4);
134
if any(info)
135
136
137
    fprintf('The prior density evaluated at the initial values is Inf for the following parameters: %s\n',BayesInfo.name{info,1})
    error('The initial value of the prior is -Inf')
end
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

if DynareOptions.ramsey_policy
    %test whether specification matches
    inst_nbr = size(DynareOptions.instruments,1);
    if inst_nbr~=0
        orig_endo_aux_nbr = Model.orig_endo_nbr + min(find([Model.aux_vars.type] == 6)) - 1;
        implied_inst_nbr = orig_endo_aux_nbr - Model.orig_eq_nbr;
        if inst_nbr>implied_inst_nbr
            error('You have specified more instruments than there are omitted equations')
        elseif inst_nbr<implied_inst_nbr
            error('You have specified fewer instruments than there are omitted equations')
        end
    end
end

153
% Evaluate the likelihood.
154
155
ana_deriv = DynareOptions.analytic_derivation;
DynareOptions.analytic_derivation=0;
156
157
if ~isequal(DynareOptions.mode_compute,11) || ...
        (isequal(DynareOptions.mode_compute,11) && isequal(DynareOptions.order,1))
158
159
160
161
162
163
    %shut off potentially automatic switch to diffuse filter for the
    %purpose of checking stochastic singularity
    use_univariate_filters_if_singularity_is_detected_old=DynareOptions.use_univariate_filters_if_singularity_is_detected;
    DynareOptions.use_univariate_filters_if_singularity_is_detected=0;
    [fval,info] = feval(objective_function,xparam1,DynareDataset,DatasetInfo,DynareOptions,Model,EstimatedParameters,BayesInfo,BoundsInfo,DynareResults);
    if info(1)==50
164
165
166
167
168
        fprintf('\ninitial_estimation_checks:: The forecast error variance in the multivariate Kalman filter became singular.\n')
        fprintf('initial_estimation_checks:: This is often a sign of stochastic singularity, but can also sometimes happen by chance\n')
        fprintf('initial_estimation_checks:: for a particular combination of parameters and data realizations.\n')
        fprintf('initial_estimation_checks:: If you think the latter is the case, you should try with different initial values for the estimated parameters.\n')
        error('initial_estimation_checks:: The forecast error variance in the multivariate Kalman filter became singular.')
169
    end
170
171
172
    if info(1)==201
        fprintf('initial_estimation_checks:: Initial covariance of the states is not positive definite. Try a different nonlinear_filter_initialization.\n')
        error('initial_estimation_checks:: Initial covariance of the states is not positive definite. Try a different nonlinear_filter_initialization.')
Stéphane Adjemian's avatar
Stéphane Adjemian committed
173
    end
174
    %reset options
175
    DynareOptions.use_univariate_filters_if_singularity_is_detected=use_univariate_filters_if_singularity_is_detected_old;
176
else
177
    info=0;
178
179
    fval = 0;
end
180
181
182
if DynareOptions.debug
    DynareResults.likelihood_at_initial_parameters=fval;
end
183
DynareOptions.analytic_derivation=ana_deriv;
184

185
186
187
188
189
% if DynareOptions.mode_compute==5
%     if ~strcmp(func2str(objective_function),'dsge_likelihood')
%         error('Options mode_compute=5 is not compatible with non linear filters or Dsge-VAR models!')
%     end
% end
190
191
192
193
194
if isnan(fval)
    error('The initial value of the likelihood is NaN')
elseif imag(fval)
    error('The initial value of the likelihood is complex')
end
michel's avatar
michel committed
195

196
if info(1) > 0
197
198
    if DynareOptions.order>1
        [eigenvalues_] = check(Model,DynareOptions, DynareResults);
199
        if any(abs(1-abs(eigenvalues_))<abs(DynareOptions.qz_criterium-1))
200
            error('Your model has at least one unit root and you are using a nonlinear filter. Please set nonlinear_filter_initialization=3.')
201
202
        end
    else
203
204
        disp('Error in computing likelihood for initial parameter values')
        print_info(info, DynareOptions.noprint, DynareOptions)
205
    end
206
207
end

208
209
210
211
212
213
if DynareOptions.prefilter==1
    if (~DynareOptions.loglinear && any(abs(DynareResults.steady_state(BayesInfo.mfys))>1e-9)) || (DynareOptions.loglinear && any(abs(log(DynareResults.steady_state(BayesInfo.mfys)))>1e-9))
        disp(['You are trying to estimate a model with a non zero steady state for the observed endogenous'])
        disp(['variables using demeaned data!'])
        error('You should change something in your mod file...')
    end
214
215
end

216
217
218
if ~isequal(DynareOptions.mode_compute,11)
    disp(['Initial value of the log posterior (or likelihood): ' num2str(-fval)]);
end