dynare_estimation_1.m 37.2 KB
Newer Older
1 2 3
function dynare_estimation_1(var_list_,dname)
% function dynare_estimation_1(var_list_,dname)
% runs the estimation of the model
4
%
5 6 7
% INPUTS
%   var_list_:  selected endogenous variables vector
%   dname:      alternative directory name
8
%
9 10 11 12 13 14
% OUTPUTS
%   none
%
% SPECIAL REQUIREMENTS
%   none

15
% Copyright (C) 2003-2017 Dynare Team
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
%
% 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/>.

32
global M_ options_ oo_ estim_params_ bayestopt_ dataset_ dataset_info
33

34 35 36 37 38 39 40 41 42 43 44
if isempty(estim_params_)
    mode_compute_o = options_.mode_compute;
    mh_replic_o = options_.mh_replic;
    options_.mode_compute = 0;
    options_.mh_replic = 0;
    reset_options_related_to_estimation = true;
else
    reset_options_related_to_estimation = false;
end


45 46
%store qz_criterium
qz_criterium_old=options_.qz_criterium;
47 48 49 50 51
if isnan(options_.first_obs)
    first_obs_nan_indicator=true;
else
    first_obs_nan_indicator=false;
end
52

53 54
% Set particle filter flag.
if options_.order > 1
55
    if options_.particle.status && options_.order==2
56
        skipline()
57
        disp('Estimation using a non linear filter!')
58
        skipline()
59
        if ~options_.nointeractive && ismember(options_.mode_compute,[1,3,4]) && ~strcmpi(options_.particle.filter_algorithm,'gf')% Known gradient-based optimizers
60
            disp('You are using a gradient-based mode-finder. Particle filtering introduces discontinuities in the')
Stéphane Adjemian's avatar
Stéphane Adjemian committed
61
            disp('objective function w.r.t the parameters. Thus, should use a non-gradient based optimizer.')
62
            fprintf('\nPlease choose a mode-finder:\n')
63
            fprintf('\t 0 - Continue using gradient-based method (it is most likely that you will no get any sensible result).\n')
64
            fprintf('\t 6 - Monte Carlo based algorithm\n')
65 66
            fprintf('\t 7 - Nelder-Mead simplex based optimization routine (Matlab optimization toolbox required)\n')
            fprintf('\t 8 - Nelder-Mead simplex based optimization routine (Dynare''s implementation)\n')
67
            fprintf('\t 9 - CMA-ES (Covariance Matrix Adaptation Evolution Strategy) algorithm\n')
68 69 70 71 72 73 74 75 76 77 78
            choice = [];
            while isempty(choice)
                choice = input('Please enter your choice: ');
                if isnumeric(choice) && isint(choice) && ismember(choice,[0 6 7 8 9])
                    if choice
                        options_.mode_compute = choice;
                    end
                else
                    fprintf('\nThis is an invalid choice (you have to choose between 0, 6, 7, 8 and 9).\n')
                    choice = [];
                end
79 80
            end
        end
81
    elseif options_.particle.status && options_.order>2
82 83
        error(['Non linear filter are not implemented with order ' int2str(options_.order) ' approximation of the model!'])
    elseif ~options_.particle.status && options_.order==2
84
        error('For estimating the model with a second order approximation using a non linear filter, one should have options_.particle.status=1;')
85 86 87
    else
        error(['Cannot estimate a model with an order ' int2str(options_.order) ' approximation!'])
    end
88 89
end

90
if ~options_.dsge_var
91
    if options_.particle.status
92
        objective_function = str2func('non_linear_dsge_likelihood');
93
        if strcmpi(options_.particle.filter_algorithm, 'sis')
94
            options_.particle.algorithm = 'sequential_importance_particle_filter';
95 96 97 98 99 100 101 102
        elseif strcmpi(options_.particle.filter_algorithm, 'apf')
            options_.particle.algorithm = 'auxiliary_particle_filter';
        elseif strcmpi(options_.particle.filter_algorithm, 'gf')
            options_.particle.algorithm = 'gaussian_filter';
        elseif strcmpi(options_.particle.filter_algorithm,  'gmf')
            options_.particle.algorithm = 'gaussian_mixture_filter';
        elseif strcmpi(options_.particle.filter_algorithm, 'cpf')
            options_.particle.algorithm = 'conditional_particle_filter';
103 104
        elseif strcmpi(options_.particle.filter_algorithm, 'nlkf')
            options_.particle.algorithm = 'nonlinear_kalman_filter';
105
        else
106
            error(['Estimation: Unknown filter ' options_.particle.filter_algorithm])
107
        end
108 109 110
    else
        objective_function = str2func('dsge_likelihood');
    end
111
else
112
    objective_function = str2func('dsge_var_likelihood');
113 114
end

115
[dataset_, dataset_info, xparam1, hh, M_, options_, oo_, estim_params_, bayestopt_, bounds] = ...
116
    dynare_estimation_init(var_list_, dname, [], M_, options_, oo_, estim_params_, bayestopt_);
117

118 119
if options_.dsge_var
    check_dsge_var_model(M_, estim_params_, bayestopt_);
120 121 122 123 124 125 126 127 128
    if dataset_info.missing.state
        error('Estimation::DsgeVarLikelihood: I cannot estimate a DSGE-VAR model with missing observations!')
    end
    if options_.noconstant
        var_sample_moments(options_.dsge_varlag, -1, dataset_);
    else
        % The steady state is non zero ==> a constant in the VAR is needed!
        var_sample_moments(options_.dsge_varlag, 0, dataset_);
    end
129 130
end

131
% Set sigma_e_is_diagonal flag (needed if the shocks block is not declared in the mod file).
132
M_.sigma_e_is_diagonal = 1;
133
if estim_params_.ncx || any(nnz(tril(M_.Correlation_matrix,-1))) || isfield(estim_params_,'calibrated_covariances')
134 135 136
    M_.sigma_e_is_diagonal = 0;
end

137
data = dataset_.data;
138
rawdata = dataset_info.rawdata;
139 140
data_index = dataset_info.missing.aindex;
missing_value = dataset_info.missing.state;
141

142
% Set number of observations
143
gend = dataset_.nobs;
144
% Set the number of observed variables.
145
n_varobs = length(options_.varobs);
146
% Get the number of parameters to be estimated.
147 148 149 150 151 152
nvx = estim_params_.nvx;  % Variance of the structural innovations (number of parameters).
nvn = estim_params_.nvn;  % Variance of the measurement innovations (number of parameters).
ncx = estim_params_.ncx;  % Covariance of the structural innovations (number of parameters).
ncn = estim_params_.ncn;  % Covariance of the measurement innovations (number of parameters).
np  = estim_params_.np ;  % Number of deep parameters.
nx  = nvx+nvn+ncx+ncn+np; % Total number of parameters to be estimated.
153
%% Set the names of the priors.
Stéphane Adjemian's avatar
Stéphane Adjemian committed
154
pnames = ['     '; 'beta '; 'gamm '; 'norm '; 'invg '; 'unif '; 'invg2'; '     '; 'weibl'];
155

156
dr = oo_.dr;
157 158

if ~isempty(estim_params_)
159
    M_ = set_all_parameters(xparam1,estim_params_,M_);
160
end
161

162

163 164
%% perform initial estimation checks;
try
165
    oo_ = initial_estimation_checks(objective_function,xparam1,dataset_,dataset_info,M_,estim_params_,options_,bayestopt_,bounds,oo_);
166 167
catch % if check fails, provide info on using calibration if present
    e = lasterror();
168
    if estim_params_.full_calibration_detected %calibrated model present and no explicit starting values
169 170
        skipline(1);
        fprintf('ESTIMATION_CHECKS: There was an error in computing the likelihood for initial parameter values.\n')
171 172
        fprintf('ESTIMATION_CHECKS: If this is not a problem with the setting of options (check the error message below),\n')
        fprintf('ESTIMATION_CHECKS: you should try using the calibrated version of the model as starting values. To do\n')
173
        fprintf('ESTIMATION_CHECKS: this, add an empty estimated_params_init-block with use_calibration option immediately before the estimation\n')
174 175 176
        fprintf('ESTIMATION_CHECKS: command (and after the estimated_params-block so that it does not get overwritten):\n');
        skipline(2);
    end
177
    rethrow(e);
178
end
179

180
if isequal(options_.mode_compute,0) && isempty(options_.mode_file) && options_.mh_posterior_mode_estimation==0
181
    if options_.smoother == 1
182
        [atT,innov,measurement_error,updated_variables,ys,trend_coeff,aK,T,R,P,PK,decomp,Trend,state_uncertainty,M_,oo_,options_,bayestopt_] = DsgeSmoother(xparam1,gend,transpose(data),data_index,missing_value,M_,oo_,options_,bayestopt_,estim_params_);
183
        [oo_]=store_smoother_results(M_,oo_,options_,bayestopt_,dataset_,dataset_info,atT,innov,measurement_error,updated_variables,ys,trend_coeff,aK,P,PK,decomp,Trend,state_uncertainty);
184
    end
185 186
    %reset qz_criterium
    options_.qz_criterium=qz_criterium_old;
187
    return
188 189
end

190 191
%% Estimation of the posterior mode or likelihood mode

192
if ~isequal(options_.mode_compute,0) && ~options_.mh_posterior_mode_estimation
193 194
    %prepare settings for newrat
    if options_.mode_compute==5
195
        %get whether outer product Hessian is requested
196
        newratflag=[];
197
        if ~isempty(options_.optim_opt)
198 199
            options_list = read_key_value_string(options_.optim_opt);
            for i=1:rows(options_list)
200 201
                if strcmp(options_list{i,1},'Hessian')
                    newratflag=options_list{i,2};
202 203 204
                end
            end
        end
205
        if options_.analytic_derivation
206
            options_analytic_derivation_old = options_.analytic_derivation;
207
            options_.analytic_derivation = -1;
208
            if ~isempty(newratflag) && newratflag~=0 %numerical hessian explicitly specified
209 210
                error('newrat: analytic_derivation is incompatible with numerical Hessian.')
            else %use default
211
                newratflag=0; %exclude DYNARE numerical hessian
212
            end
213 214 215
        elseif ~options_.analytic_derivation
            if isempty(newratflag)
                newratflag=options_.newrat.hess; %use default numerical dynare hessian
216 217
            end
        end
218
    end
219

220
    [xparam1, fval, exitflag, hh, options_, Scale, new_rat_hess_info] = dynare_minimize_objective(objective_function,xparam1,options_.mode_compute,options_,[bounds.lb bounds.ub],bayestopt_.name,bayestopt_,hh,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,bounds,oo_);
221 222
    fprintf('\nFinal value of minus the log posterior (or likelihood):%f \n', fval);

223
    if isnumeric(options_.mode_compute) && options_.mode_compute==5 && options_.analytic_derivation==-1 %reset options changed by newrat
224
        options_.analytic_derivation = options_analytic_derivation_old; %reset
225
    elseif isnumeric(options_.mode_compute) && options_.mode_compute==6 %save scaling factor
226 227 228
        save([M_.fname '_optimal_mh_scale_parameter.mat'],'Scale');
        options_.mh_jscale = Scale;
        bayestopt_.jscale = ones(length(xparam1),1)*Scale;
229
    end
230
    if ~isnumeric(options_.mode_compute) || ~isequal(options_.mode_compute,6) %always already computes covariance matrix
231
        if options_.cova_compute == 1 %user did not request covariance not to be computed
232
            if options_.analytic_derivation && strcmp(func2str(objective_function),'dsge_likelihood')
233
                ana_deriv_old = options_.analytic_derivation;
234
                options_.analytic_derivation = 2;
235
                [junk1, junk2,junk3, junk4, hh] = feval(objective_function,xparam1, ...
236
                                                        dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,bounds,oo_);
237
                options_.analytic_derivation = ana_deriv_old;
238
            elseif ~isnumeric(options_.mode_compute) || ~(isequal(options_.mode_compute,5) && newratflag~=1 && strcmp(func2str(objective_function),'dsge_likelihood')) 
239 240 241 242 243 244 245 246
                % with flag==0, we force to use the hessian from outer product gradient of optimizer 5
                if options_.hessian.use_penalized_objective
                    penalized_objective_function = str2func('penalty_objective_function');
                    hh = hessian(penalized_objective_function, xparam1, options_.gstep, objective_function, fval, dataset_, dataset_info, options_, M_, estim_params_, bayestopt_, bounds,oo_);
                else
                    hh = hessian(objective_function, xparam1, options_.gstep, dataset_, dataset_info, options_, M_, estim_params_, bayestopt_, bounds,oo_);
                end
                hh = reshape(hh, nx, nx);
247
            elseif isnumeric(options_.mode_compute) && isequal(options_.mode_compute,5)
248 249 250 251 252 253 254 255 256 257 258 259 260
                % other numerical hessian options available with optimizer 5
                %
                % if newratflag == 0
                % compute outer product gradient of optimizer 5
                %
                % if newratflag == 2
                % compute 'mixed' outer product gradient of optimizer 5
                % with diagonal elements computed with numerical second order derivatives
                %
                % uses univariate filters, so to get max # of available
                % densitities for outer product gradient
                kalman_algo0 = options_.kalman_algo;
                compute_hessian = 1;
261
                if ~((options_.kalman_algo == 2) || (options_.kalman_algo == 4))
262
                    options_.kalman_algo=2;
263
                    if options_.lik_init == 3
264 265
                        options_.kalman_algo=4;
                    end
266
                elseif newratflag==0 % hh already contains outer product gradient with univariate filter
267
                    compute_hessian = 0;
268
                end
269
                if compute_hessian
270 271
                    crit = options_.newrat.tolerance.f;
                    newratflag = newratflag>0;
272
                    hh = reshape(mr_hessian(xparam1,objective_function,fval,newratflag,crit,new_rat_hess_info,dataset_, dataset_info, options_,M_,estim_params_,bayestopt_,bounds,oo_), nx, nx);
273 274
                end
                options_.kalman_algo = kalman_algo0;
275
            end
276
        end
277 278
    end
    parameter_names = bayestopt_.name;
279
    if options_.cova_compute || options_.mode_compute==5 || options_.mode_compute==6
280
        save([M_.fname '_mode.mat'],'xparam1','hh','parameter_names','fval');
281
    else
282
        save([M_.fname '_mode.mat'],'xparam1','parameter_names','fval');
283
    end
284 285
end

286
if ~options_.mh_posterior_mode_estimation && options_.cova_compute
287 288 289
    try
        chol(hh);
    catch
290
        skipline()
291 292 293
        disp('POSTERIOR KERNEL OPTIMIZATION PROBLEM!')
        disp(' (minus) the hessian matrix at the "mode" is not positive definite!')
        disp('=> posterior variance of the estimated parameters are not positive.')
294
        disp('You should try to change the initial values of the parameters using')
295
        disp('the estimated_params_init block, or use another optimization routine.')
296
        params_at_bound=find(abs(xparam1-bounds.ub)<1.e-10 | abs(xparam1-bounds.lb)<1.e-10);
297 298
        if ~isempty(params_at_bound)
            for ii=1:length(params_at_bound)
299
                params_at_bound_name{ii,1}=get_the_name(params_at_bound(ii),0,M_,estim_params_,options_);
300 301 302 303 304 305 306 307 308 309
            end
            disp_string=[params_at_bound_name{1,:}];
            for ii=2:size(params_at_bound_name,1)
                disp_string=[disp_string,', ',params_at_bound_name{ii,:}];
            end
            fprintf('\nThe following parameters are at the prior bound: %s\n', disp_string)
            fprintf('Some potential solutions are:\n')
            fprintf('   - Check your model for mistakes.\n')
            fprintf('   - Check whether model and data are consistent (correct observation equation).\n')
            fprintf('   - Shut off prior_trunc.\n')
310
            fprintf('   - Change the optimization bounds.\n')
311 312
            fprintf('   - Use a different mode_compute like 6 or 9.\n')
            fprintf('   - Check whether the parameters estimated are identified.\n')
313
            fprintf('   - Check prior shape (e.g. Inf density at bound(s)).\n')
314 315
            fprintf('   - Increase the informativeness of the prior.\n')
        end
316 317
        warning('The results below are most likely wrong!');
    end
318 319
end

320
if options_.mode_check.status == 1 && ~options_.mh_posterior_mode_estimation
321
    ana_deriv_old = options_.analytic_derivation;
322
    options_.analytic_derivation = 0;
323
    mode_check(objective_function,xparam1,hh,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,bounds,oo_);
324
    options_.analytic_derivation = ana_deriv_old;
325 326
end

327
oo_.posterior.optimization.mode = [];
328
oo_.posterior.optimization.Variance = [];
329 330
oo_.posterior.optimization.log_density=[];

331
invhess=[];
332
if ~options_.mh_posterior_mode_estimation
333
    oo_.posterior.optimization.mode = xparam1;
334 335 336
    if exist('fval','var')
        oo_.posterior.optimization.log_density=-fval;
    end
337
    if options_.cova_compute
338 339
        hsd = sqrt(diag(hh));
        invhess = inv(hh./(hsd*hsd'))./(hsd*hsd');
340
        stdh = sqrt(diag(invhess));
341
        oo_.posterior.optimization.Variance = invhess;
342
    end
343
else
344 345 346 347 348 349 350
    variances = bayestopt_.p2.*bayestopt_.p2;
    idInf = isinf(variances);
    variances(idInf) = 1;
    invhess = options_.mh_posterior_mode_estimation*diag(variances);
    xparam1 = bayestopt_.p5;
    idNaN = isnan(xparam1);
    xparam1(idNaN) = bayestopt_.p1(idNaN);
351 352
    outside_bound_pars=find(xparam1 < bounds.lb | xparam1 > bounds.ub);
    xparam1(outside_bound_pars) = bayestopt_.p1(outside_bound_pars);
353 354
end

355 356 357
if ~options_.cova_compute
    stdh = NaN(length(xparam1),1);
end
358

359
if any(bayestopt_.pshape > 0) && ~options_.mh_posterior_mode_estimation
360
    % display results table and store parameter estimates and standard errors in results
361
    oo_=display_estimation_results_table(xparam1,stdh,M_,options_,estim_params_,bayestopt_,oo_,pnames,'Posterior','posterior');
362
    % Laplace approximation to the marginal log density:
363 364
    if options_.cova_compute
        estim_params_nbr = size(xparam1,1);
365
        log_det_invhess = log(det(invhess./(stdh*stdh')))+2*sum(log(stdh));
366
        likelihood = feval(objective_function,xparam1,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,bounds,oo_);
367
        oo_.MarginalDensity.LaplaceApproximation = .5*estim_params_nbr*log(2*pi) + .5*log_det_invhess - likelihood;
368
        skipline()
369
        disp(sprintf('Log data density [Laplace approximation] is %f.',oo_.MarginalDensity.LaplaceApproximation))
370
        skipline()
371
    end
372 373 374 375 376 377
    if options_.dsge_var
        [junk1,junk2,junk3,junk4,junk5,junk6,junk7,oo_.dsge_var.posterior_mode.PHI_tilde,oo_.dsge_var.posterior_mode.SIGMA_u_tilde,oo_.dsge_var.posterior_mode.iXX,oo_.dsge_var.posterior_mode.prior] =...
            feval(objective_function,xparam1,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,bounds,oo_);
        clear('junk1','junk2','junk3','junk4','junk5','junk6','junk7');
    end

378
elseif ~any(bayestopt_.pshape > 0) && ~options_.mh_posterior_mode_estimation
379
    oo_=display_estimation_results_table(xparam1,stdh,M_,options_,estim_params_,bayestopt_,oo_,pnames,'Maximum Likelihood','mle');
380 381 382 383 384 385 386
end

if np > 0
    pindx = estim_params_.param_vals(:,1);
    save([M_.fname '_params.mat'],'pindx');
end

387
switch options_.MCMC_jumping_covariance
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
  case 'hessian' %Baseline
                 %do nothing and use hessian from mode_compute
  case 'prior_variance' %Use prior variance
    if any(isinf(bayestopt_.p2))
        error('Infinite prior variances detected. You cannot use the prior variances as the proposal density, if some variances are Inf.')
    else
        hh = diag(1./(bayestopt_.p2.^2));
    end
    hsd = sqrt(diag(hh));
    invhess = inv(hh./(hsd*hsd'))./(hsd*hsd');
  case 'identity_matrix' %Use identity
    invhess = eye(nx);
  otherwise %user specified matrix in file
    try
        load(options_.MCMC_jumping_covariance,'jumping_covariance')
        hh=jumping_covariance;
    catch
        error(['No matrix named ''jumping_covariance'' could be found in ',options_.MCMC_jumping_covariance,'.mat'])
    end
    [nrow, ncol]=size(hh);
    if ~isequal(nrow,ncol) && ~isequal(nrow,nx) %check if square and right size
        error(['jumping_covariance matrix must be square and have ',num2str(nx),' rows and columns'])
    end
    try %check for positive definiteness
        chol(hh);
413 414
        hsd = sqrt(diag(hh));
        invhess = inv(hh./(hsd*hsd'))./(hsd*hsd');
415 416 417
    catch
        error(['Specified jumping_covariance is not positive definite'])
    end
418 419
end

420 421
if (any(bayestopt_.pshape  >0 ) && options_.mh_replic) || ...
        (any(bayestopt_.pshape >0 ) && options_.load_mh_file)  %% not ML estimation
422
    outside_bound_pars=find(xparam1 < bounds.lb | xparam1 > bounds.ub);
423 424 425 426 427 428 429
    if ~isempty(outside_bound_pars)
        for ii=1:length(outside_bound_pars)
            outside_bound_par_names{ii,1}=get_the_name(ii,0,M_,estim_params_,options_);
        end
        disp_string=[outside_bound_par_names{1,:}];
        for ii=2:size(outside_bound_par_names,1)
            disp_string=[disp_string,', ',outside_bound_par_names{ii,:}];
430
        end
431 432 433 434 435
        if options_.prior_trunc>0
            error(['Estimation:: Mode value(s) of ', disp_string ,' are outside parameter bounds. Potentially, you should set prior_trunc=0.'])
        else
            error(['Estimation:: Mode value(s) of ', disp_string ,' are outside parameter bounds.'])
        end
436 437
    end
    % runs MCMC
438
    if options_.mh_replic || options_.load_mh_file
439
        posterior_sampler_options = options_.posterior_sampler_options.current_options;
440 441
        posterior_sampler_options.invhess = invhess;
        [posterior_sampler_options, options_] = check_posterior_sampler_options(posterior_sampler_options, options_);
442 443
        % store current options in global
        options_.posterior_sampler_options.current_options = posterior_sampler_options;
444 445 446 447 448 449
        if options_.mh_replic
            ana_deriv_old = options_.analytic_derivation;
            options_.analytic_derivation = 0;
            posterior_sampler(objective_function,posterior_sampler_options.proposal_distribution,xparam1,posterior_sampler_options,bounds,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,oo_);
            options_.analytic_derivation = ana_deriv_old;
        end
450
    end
451 452
    %% Here I discard first mh_drop percent of the draws:
    CutSample(M_, options_, estim_params_);
453
    if options_.mh_posterior_mode_estimation
454 455
        %reset qz_criterium
        options_.qz_criterium=qz_criterium_old;
456
        return
457
    else
458 459
        %get stored results if required
        if options_.load_mh_file && options_.load_results_after_load_mh
460
            oo_load_mh=load([M_.fname '_results'],'oo_');
461
        end
462
        if ~options_.nodiagnostic
463 464 465 466 467 468 469
            if (options_.mh_replic>0 || (options_.load_mh_file && ~options_.load_results_after_load_mh))
                oo_= McMCDiagnostics(options_, estim_params_, M_,oo_);
            elseif options_.load_mh_file && options_.load_results_after_load_mh
                if isfield(oo_load_mh.oo_,'convergence')
                    oo_.convergence=oo_load_mh.oo_.convergence;
                end
            end
470
        end
471
        %% Estimation of the marginal density from the Mh draws:
472
        if options_.mh_replic || (options_.load_mh_file && ~options_.load_results_after_load_mh)
473
            [marginal,oo_] = marginal_density(M_, options_, estim_params_, oo_, bayestopt_);
MichelJuillard's avatar
MichelJuillard committed
474
            % Store posterior statistics by parameter name
Stéphane Adjemian's avatar
Stéphane Adjemian committed
475
            oo_ = GetPosteriorParametersStatistics(estim_params_, M_, options_, bayestopt_, oo_, pnames);
476 477 478
            if ~options_.nograph
                oo_ = PlotPosteriorDistributions(estim_params_, M_, options_, bayestopt_, oo_);
            end
MichelJuillard's avatar
MichelJuillard committed
479 480
            % Store posterior mean in a vector and posterior variance in
            % a matrix
481
            [oo_.posterior.metropolis.mean,oo_.posterior.metropolis.Variance] ...
482
                = GetPosteriorMeanVariance(M_,options_.mh_drop);
483
        elseif options_.load_mh_file && options_.load_results_after_load_mh
484
            %% load fields from previous MCMC run stored in results-file
485
            field_names={'posterior_mode','posterior_std_at_mode',...% fields set by marginal_density
486 487 488
                         'posterior_mean','posterior_hpdinf','posterior_hpdsup','posterior_median','posterior_variance','posterior_std','posterior_deciles','posterior_density',...% fields set by GetPosteriorParametersStatistics
                         'prior_density',...%fields set by PlotPosteriorDistributions
                        };
489 490 491 492
            for field_iter=1:size(field_names,2)
                if isfield(oo_load_mh.oo_,field_names{1,field_iter})
                    oo_.(field_names{1,field_iter})=oo_load_mh.oo_.(field_names{1,field_iter});
                end
493
            end
494 495 496
            % field set by marginal_density
            if isfield(oo_load_mh.oo_,'MarginalDensity') && isfield(oo_load_mh.oo_.MarginalDensity,'ModifiedHarmonicMean')
                oo_.MarginalDensity.ModifiedHarmonicMean=oo_load_mh.oo_.MarginalDensity.ModifiedHarmonicMean;
497
            end
498 499 500 501
            % field set by GetPosteriorMeanVariance
            if isfield(oo_load_mh.oo_,'posterior') && isfield(oo_load_mh.oo_.posterior,'metropolis')
                oo_.posterior.metropolis=oo_load_mh.oo_.posterior.metropolis;
            end
502
        end
503
        [error_flag,junk,options_]= metropolis_draw(1,options_,estim_params_,M_);
504
        if options_.bayesian_irf
Stéphane Adjemian's avatar
Stéphane Adjemian committed
505 506 507
            if error_flag
                error('Estimation::mcmc: I cannot compute the posterior IRFs!')
            end
508
            PosteriorIRF('posterior');
509
        end
510
        if options_.moments_varendo
Stéphane Adjemian's avatar
Stéphane Adjemian committed
511 512 513
            if error_flag
                error('Estimation::mcmc: I cannot compute the posterior moments for the endogenous variables!')
            end
514
            oo_ = compute_moments_varendo('posterior',options_,M_,oo_,var_list_);
515
        end
516
        if options_.smoother || ~isempty(options_.filter_step_ahead) || options_.forecast
Stéphane Adjemian's avatar
Stéphane Adjemian committed
517 518 519
            if error_flag
                error('Estimation::mcmc: I cannot compute the posterior statistics!')
            end
520
            prior_posterior_statistics('posterior',dataset_,dataset_info);
521
        end
522
        xparam1 = get_posterior_parameters('mean',M_,estim_params_,oo_,options_);
523
        M_ = set_all_parameters(xparam1,estim_params_,M_);
524 525
    end
end
526

527
if options_.particle.status
528 529
    %reset qz_criterium
    options_.qz_criterium=qz_criterium_old;
530 531 532
    return
end

533
if (~((any(bayestopt_.pshape > 0) && options_.mh_replic) || (any(bayestopt_.pshape> 0) && options_.load_mh_file)) ...
MichelJuillard's avatar
MichelJuillard committed
534
    || ~options_.smoother ) && options_.partial_information == 0  % to be fixed
535
    %% ML estimation, or posterior mode without Metropolis-Hastings or Metropolis without Bayesian smoothes variables
536
    [atT,innov,measurement_error,updated_variables,ys,trend_coeff,aK,T,R,P,PK,decomp,Trend,state_uncertainty,M_,oo_,options_,bayestopt_] = DsgeSmoother(xparam1,dataset_.nobs,transpose(dataset_.data),dataset_info.missing.aindex,dataset_info.missing.state,M_,oo_,options_,bayestopt_,estim_params_);
537
    [oo_,yf]=store_smoother_results(M_,oo_,options_,bayestopt_,dataset_,dataset_info,atT,innov,measurement_error,updated_variables,ys,trend_coeff,aK,P,PK,decomp,Trend,state_uncertainty);
538

539
    if ~options_.nograph
540
        [nbplt,nr,nc,lr,lc,nstar] = pltorg(M_.exo_nbr);
541
        if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format)))
542
            fidTeX = fopen([M_.fname '_SmoothedShocks.tex'],'w');
543
            fprintf(fidTeX,'%% TeX eps-loader file generated by dynare_estimation_1.m (Dynare).\n');
544 545
            fprintf(fidTeX,['%% ' datestr(now,0) '\n']);
            fprintf(fidTeX,' \n');
546
        end
547
        for plt = 1:nbplt
548
            fh = dyn_figure(options_.nodisplay,'Name','Smoothed shocks');
549 550
            NAMES = [];
            if options_.TeX, TeXNAMES = []; end
551
            nstar0=min(nstar,M_.exo_nbr-(plt-1)*nstar);
552 553 554 555 556 557 558
            if gend==1
                marker_string{1,1}='-ro';
                marker_string{2,1}='-ko';
            else
                marker_string{1,1}='-r';
                marker_string{2,1}='-k';
            end
559
            for i=1:nstar0
560 561
                k = (plt-1)*nstar+i;
                subplot(nr,nc,i);
562
                plot([1 gend],[0 0],marker_string{1,1},'linewidth',.5)
563
                hold on
564
                plot(1:gend,innov(k,:),marker_string{2,1},'linewidth',1)
565 566
                hold off
                name = deblank(M_.exo_names(k,:));
567 568 569 570 571
                if isempty(NAMES)
                    NAMES = name;
                else
                    NAMES = char(NAMES,name);
                end
572 573 574
                if ~isempty(options_.XTick)
                    set(gca,'XTick',options_.XTick)
                    set(gca,'XTickLabel',options_.XTickLabel)
575
                end
576 577 578
                if gend>1
                    xlim([1 gend])
                end
579 580
                if options_.TeX
                    texname = M_.exo_names_tex(k,:);
581 582 583 584 585
                    if isempty(TeXNAMES)
                        TeXNAMES = ['$ ' deblank(texname) ' $'];
                    else
                        TeXNAMES = char(TeXNAMES,['$ ' deblank(texname) ' $']);
                    end
586
                end
587
                title(name,'Interpreter','none')
588
            end
589
            dyn_saveas(fh,[M_.fname '_SmoothedShocks' int2str(plt)],options_.nodisplay,options_.graph_format);
590
            if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format)))
591
                fprintf(fidTeX,'\\begin{figure}[H]\n');
592
                for jj = 1:nstar0
593
                    fprintf(fidTeX,'\\psfrag{%s}[1][][0.5][0]{%s}\n',deblank(NAMES(jj,:)),deblank(TeXNAMES(jj,:)));
594
                end
595
                fprintf(fidTeX,'\\centering \n');
596
                fprintf(fidTeX,'\\includegraphics[width=%2.2f\\textwidth]{%s_SmoothedShocks%s}\n',options_.figures.textwidth*min(i/nc,1),M_.fname,int2str(plt));
597 598 599 600
                fprintf(fidTeX,'\\caption{Smoothed shocks.}');
                fprintf(fidTeX,'\\label{Fig:SmoothedShocks:%s}\n',int2str(plt));
                fprintf(fidTeX,'\\end{figure}\n');
                fprintf(fidTeX,'\n');
601
            end
602
        end
603
        if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format)))
604 605 606
            fprintf(fidTeX,'\n');
            fprintf(fidTeX,'%% End of TeX file.\n');
            fclose(fidTeX);
607
        end
608 609 610 611
    end
    if nvn
        number_of_plots_to_draw = 0;
        index = [];
612
        for obs_iter=1:n_varobs
613
            if max(abs(measurement_error(obs_iter,:))) > options_.ME_plot_tol;
614
                number_of_plots_to_draw = number_of_plots_to_draw + 1;
615
                index = cat(1,index,obs_iter);
616 617
            end
        end
618 619
        if ~options_.nograph
            [nbplt,nr,nc,lr,lc,nstar] = pltorg(number_of_plots_to_draw);
620
            if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format)))
621
                fidTeX = fopen([M_.fname '_SmoothedObservationErrors.tex'],'w');
622
                fprintf(fidTeX,'%% TeX eps-loader file generated by dynare_estimation_1.m (Dynare).\n');
623 624
                fprintf(fidTeX,['%% ' datestr(now,0) '\n']);
                fprintf(fidTeX,' \n');
625
            end
626
            for plt = 1:nbplt
627
                fh = dyn_figure(options_.nodisplay,'Name','Smoothed observation errors');
628 629
                NAMES = [];
                if options_.TeX, TeXNAMES = []; end
630
                nstar0=min(nstar,number_of_plots_to_draw-(plt-1)*nstar);
631 632 633 634 635 636 637
                if gend==1
                    marker_string{1,1}='-ro';
                    marker_string{2,1}='-ko';
                else
                    marker_string{1,1}='-r';
                    marker_string{2,1}='-k';
                end
638
                for i=1:nstar0
639 640
                    k = (plt-1)*nstar+i;
                    subplot(nr,nc,i);
641
                    plot([1 gend],[0 0],marker_string{1,1},'linewidth',.5)
642
                    hold on
643
                    plot(1:gend,measurement_error(index(k),:),marker_string{2,1},'linewidth',1)
644
                    hold off
645
                    name = options_.varobs{index(k)};
646 647 648
                    if gend>1
                        xlim([1 gend])
                    end
649 650 651 652 653
                    if isempty(NAMES)
                        NAMES = name;
                    else
                        NAMES = char(NAMES,name);
                    end
654 655 656 657 658
                    if ~isempty(options_.XTick)
                        set(gca,'XTick',options_.XTick)
                        set(gca,'XTickLabel',options_.XTickLabel)
                    end
                    if options_.TeX
659
                        idx = strmatch(options_.varobs{index(k)},M_.endo_names,'exact');
660
                        texname = M_.endo_names_tex(idx,:);
661 662 663 664 665 666
                        if isempty(TeXNAMES)
                            TeXNAMES = ['$ ' deblank(texname) ' $'];
                        else
                            TeXNAMES = char(TeXNAMES,['$ ' deblank(texname) ' $']);
                        end
                    end
667 668
                    title(name,'Interpreter','none')
                end
669
                dyn_saveas(fh,[M_.fname '_SmoothedObservationErrors' int2str(plt)],options_.nodisplay,options_.graph_format);
670
                if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format)))
671
                    fprintf(fidTeX,'\\begin{figure}[H]\n');
672
                    for jj = 1:nstar0
673
                        fprintf(fidTeX,'\\psfrag{%s}[1][][0.5][0]{%s}\n',deblank(NAMES(jj,:)),deblank(TeXNAMES(jj,:)));
674
                    end
675
                    fprintf(fidTeX,'\\centering \n');
676
                    fprintf(fidTeX,'\\includegraphics[width=%2.2f\\textwidth]{%s_SmoothedObservationErrors%s}\n',options_.figures.textwidth*min(i/nc,1),M_.fname,int2str(plt));
677 678
                    fprintf(fidTeX,'\\caption{Smoothed observation errors.}');
                    fprintf(fidTeX,'\\label{Fig:SmoothedObservationErrors:%s}\n',int2str(plt));
679 680
                    fprintf(fidTeX,'\\end{figure}\n');
                    fprintf(fidTeX,'\n');
681
                end
682
            end
683
            if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format)))
684 685 686 687
                fprintf(fidTeX,'\n');
                fprintf(fidTeX,'%% End of TeX file.\n');
                fclose(fidTeX);
            end
688
        end
689
    end
690 691 692
    %%
    %%  Historical and smoothed variabes
    %%
693
    if ~options_.nograph
694 695 696 697 698 699
        [nbplt,nr,nc,lr,lc,nstar] = pltorg(n_varobs);
        if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format)))
            fidTeX = fopen([M_.fname '_HistoricalAndSmoothedVariables.tex'],'w');
            fprintf(fidTeX,'%% TeX eps-loader file generated by dynare_estimation_1.m (Dynare).\n');
            fprintf(fidTeX,['%% ' datestr(now,0) '\n']);
            fprintf(fidTeX,' \n');
700
        end
701 702 703 704 705 706 707 708
        for plt = 1:nbplt
            fh = dyn_figure(options_.nodisplay,'Name','Historical and smoothed variables');
            NAMES = [];
            if options_.TeX, TeXNAMES = []; end
            nstar0=min(nstar,n_varobs-(plt-1)*nstar);
            if gend==1
                marker_string{1,1}='-ro';
                marker_string{2,1}='--ko';
709
            else
710 711
                marker_string{1,1}='-r';
                marker_string{2,1}='--k';
712
            end
713 714 715 716 717 718 719 720 721 722
            for i=1:nstar0
                k = (plt-1)*nstar+i;
                subplot(nr,nc,i);
                plot(1:gend,yf(k,:),marker_string{1,1},'linewidth',1)
                hold on
                plot(1:gend,rawdata(:,k),marker_string{2,1},'linewidth',1)
                hold off
                name = options_.varobs{k};
                if isempty(NAMES)
                    NAMES = name;
723
                else
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
                    NAMES = char(NAMES,name);
                end
                if ~isempty(options_.XTick)
                    set(gca,'XTick',options_.XTick)
                    set(gca,'XTickLabel',options_.XTickLabel)
                end
                if gend>1
                    xlim([1 gend])
                end
                if options_.TeX
                    idx = strmatch(options_.varobs{k},M_.endo_names,'exact');
                    texname = M_.endo_names_tex(idx,:);
                    if isempty(TeXNAMES)
                        TeXNAMES = ['$ ' deblank(texname) ' $'];
                    else
                        TeXNAMES = char(TeXNAMES,['$ ' deblank(texname) ' $']);
                    end
741
                end
742 743 744 745 746 747 748 749 750 751 752 753 754 755
                title(name,'Interpreter','none')
            end
            dyn_saveas(fh,[M_.fname '_HistoricalAndSmoothedVariables' int2str(plt)],options_.nodisplay,options_.graph_format);
            if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format)))
                fprintf(fidTeX,'\\begin{figure}[H]\n');
                for jj = 1:nstar0
                    fprintf(fidTeX,'\\psfrag{%s}[1][][0.5][0]{%s}\n',deblank(NAMES(jj,:)),deblank(TeXNAMES(jj,:)));
                end
                fprintf(fidTeX,'\\centering \n');
                fprintf(fidTeX,'\\includegraphics[width=%2.2f\\textwidth]{%s_HistoricalAndSmoothedVariables%s}\n',options_.figures.textwidth*min(i/nc,1),M_.fname,int2str(plt));
                fprintf(fidTeX,'\\caption{Historical and smoothed variables.}');
                fprintf(fidTeX,'\\label{Fig:HistoricalAndSmoothedVariables:%s}\n',int2str(plt));
                fprintf(fidTeX,'\\end{figure}\n');
                fprintf(fidTeX,'\n');
756 757
            end
        end
758
        if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format)))
759
            fprintf(fidTeX,'\n');
760 761
            fprintf(fidTeX,'%% End of TeX file.\n');
            fclose(fidTeX);
762 763
        end
    end
764
end
765

766
if options_.forecast > 0 && options_.mh_replic == 0 && ~options_.load_mh_file
767
    oo_.forecast = dyn_forecast(var_list_,M_,options_,oo_,'smoother',dataset_info);
768 769 770 771 772 773 774
end

if np > 0
    pindx = estim_params_.param_vals(:,1);
    save([M_.fname '_pindx.mat'] ,'pindx');
end

775 776
%reset qz_criterium
options_.qz_criterium=qz_criterium_old;
777 778 779 780 781

if reset_options_related_to_estimation
    options_.mode_compute = mode_compute_o;
    options_.mh_replic = mh_replic_o;
end
782 783 784
if first_obs_nan_indicator
    options_.first_obs=NaN;
end