Skip to content
Snippets Groups Projects
Select Git revision
  • 38e300b94e04f8bc2f16a79a32739bfd169c9c1b
  • master default protected
  • 6.x protected
  • madysson
  • 5.x protected
  • asm
  • time-varying-information-set
  • 4.6 protected
  • dynare_minreal
  • dragonfly
  • various_fixes
  • 4.5 protected
  • clang+openmp
  • exo_steady_state
  • declare_vars_in_model_block
  • julia
  • error_msg_undeclared_model_vars
  • static_aux_vars
  • slice
  • aux_func
  • penalty
  • 6.4 protected
  • 6.3 protected
  • 6.2 protected
  • 6.1 protected
  • 6.0 protected
  • 6-beta2 protected
  • 6-beta1 protected
  • 5.5 protected
  • 5.4 protected
  • 5.3 protected
  • 5.2 protected
  • 5.1 protected
  • 5.0 protected
  • 5.0-rc1 protected
  • 4.7-beta3 protected
  • 4.7-beta2 protected
  • 4.7-beta1 protected
  • 4.6.4 protected
  • 4.6.3 protected
  • 4.6.2 protected
41 results

calibrate_mh_scale_parameter.m

Blame
  • calibrate_mh_scale_parameter.m 3.76 KiB
    function Scale = calibrate_mh_scale_parameter(ObjectiveFunction, CovarianceMatrix, Parameters, MhBounds, options, varargin)
    
    % Tune the MH scale parameter so that the overall acceptance ratio is close to AcceptanceTarget.
    %
    % INPUTS
    % - ObjectiveFunction             [fhandle]      Function (posterior kernel).
    % - CovarianceMatrix              [double]       n*n matrix, covariance matrix of the jumping distribution.
    % - Parameters                    [double]       n*1 vector, parameter values.
    % - MhBounds                      [double]       n*2 matrix, bounds on the possible values for the parameters.
    % - options                       [structure]    content of options_.tune_mh_jscale.
    % - varargin                      [cell]         Additional arguments to be passed to ObjectiveFunction.
    %
    % OUTPUTS
    % - Scale                         [double]       scalar, optimal scale parameter for teh jumping distribution.
    
    % Fire up the wait bar
    hh = dyn_waitbar(0,'Tuning of the scale parameter...');
    set(hh,'Name','Tuning of the scale parameter.');
    
    % Intilialize various counters.
    j = 1; jj  = 1; isux = 0; jsux = 0; i = 0;
    
    % Evaluate the objective function.
    logpo0 = - feval(ObjectiveFunction, Parameters, varargin{:});
    logpo1 = logpo0;
    
    % Get the dimension of the problem.
    n = length(Parameters);
    
    % Initialize the correction on the scale factor.
    correction = 1.0;
    
    % Set the initial value of the scale parameter
    Scale = options.guess;
    
    % Transposition of some arrays.
    MhBounds = MhBounds';
    Parameters = Parameters';
    
    % Compute the Cholesky of the covariance matrix, return an error if the
    % matrix is not positive definite.
    try
        dd = chol(CovarianceMatrix);
    catch
        error('The covariance matrix has to be a symetric positive definite matrix!')
    end
    
    % Set parameters related to the proposal distribution
    if options.rwmh.proposal_distribution=='rand_multivariate_normal'
        nu = n;
    elseif options.rwmh.proposal_distribution=='rand_multivariate_student'
        nu = options.rwmh.student_degrees_of_freedom;
    end
    
    % Random Walk Metropolis Hastings iterations...
    while j<=options.maxiter
        % Obtain a proposal (jump)
        proposal = feval(options.rwmh.proposal_distribution, Parameters, Scale*dd, nu);
        % If out of boundaries set the posterior kernel equal to minus infinity
        % so that the proposal will be rejected with probability one.
        if all(proposal > MhBounds(1,:)) && all(proposal < MhBounds(2,:))
            logpo0 = -feval(ObjectiveFunction, proposal(:), varargin{:});
        else
            logpo0 = -inf;
        end
        % Move if the proposal is enough likely...
        if logpo0>-inf && log(rand)<logpo0-logpo1
            Parameters = proposal;
            logpo1 = logpo0;
            isux = isux + 1;