mcmc_regression Derived Type

type, public, extends(metropolis_hastings) :: mcmc_regression

The mcmc_regression type extends the metropolis_hastings type to specifically target regression problems. The problem is formulated such that the target distribution takes the form , where is a normal distribution with as the mean and the model variance, is determined by computing the variance for the current estimate of the model.


Contents


Components

Type Visibility Attributes Name Initial
procedure(regression_function), public, pointer, nopass :: fcn

The function to fit.

real(kind=real64), public, allocatable, dimension(:) :: lower_limits

An optional array that, if used, provides a lower limit to each parameter in the model. If used, be sure this array is the same dimension as the parameter array. If not used, leave this alone and no lower limits will be placed on the parameters. If used and the array is not sized correctly, it will be ignored.

real(kind=real64), public, allocatable, dimension(:) :: upper_limits

An optional array that, if used, provides an upper limit to each parameter in the model. If used, be sure this array is the same dimension as the parameter array. If not used, leave this alone and no upper limits will be placed on the parameters. If used and the array is not sized correctly, it will be ignored.

real(kind=real64), public, allocatable, dimension(:) :: x

The independent-variable data to fit.

real(kind=real64), public, allocatable, dimension(:) :: y

The dependent-variable data to fit.


Type-Bound Procedures

procedure, public :: compute_fit_statistics => mr_calc_regression_stats

  • private function mr_calc_regression_stats(this, xc, alpha, err) result(rst)

    Calculates statistics for the quality of fit for the regression.

    Arguments

    Type IntentOptional Attributes Name
    class(mcmc_regression), intent(inout) :: this

    The mcmc_regression object.

    real(kind=real64), intent(in), dimension(:) :: xc

    The model parameters. Be sure to only include the model parameters.

    real(kind=real64), intent(in), optional :: alpha

    The significance level at which to evaluate the confidence intervals. The default value is 0.05 such that a 95% confidence interval is calculated.

    class(errors), intent(inout), optional, target :: err

    An error handling object.

    Return Value type(regression_statistics), allocatable, dimension(:)

    The resulting statistics for each parameter.

procedure, public :: compute_hastings_ratio => mh_hastings_ratio

  • private function mh_hastings_ratio(this, xc, xp) result(rst)

    Evaluates the Hasting's ratio. If the proposal distribution is symmetric, this ratio is unity; however, in the case of an asymmetric distribution this ratio is not ensured to be unity.

    Arguments

    Type IntentOptional Attributes Name
    class(metropolis_hastings), intent(inout) :: this

    The metropolis_hastings object.

    real(kind=real64), intent(in), dimension(:) :: xc

    The current state vector.

    real(kind=real64), intent(in), dimension(size(xc)) :: xp

    The proposed state vector.

    Return Value real(kind=real64)

    The ratio.

procedure, public :: covariance_matrix => mr_covariance

  • private function mr_covariance(this, xc, err) result(rst)

    Computes the covariance matrix for the model given the specified model parameters.

    Arguments

    Type IntentOptional Attributes Name
    class(mcmc_regression), intent(inout) :: this

    The mcmc_regression object.

    real(kind=real64), intent(in), dimension(:) :: xc

    The current set of model parameters.

    class(errors), intent(inout), optional, target :: err

    The error handling object.

    Return Value real(kind=real64), allocatable, dimension(:,:)

    The covariance matrix.

procedure, public :: evaluate_proposal_pdf => mh_eval_proposal

  • private pure function mh_eval_proposal(this, xc) result(rst)

    Evaluates the proposal distribution PDF at the specified set of variables.

    Arguments

    Type IntentOptional Attributes Name
    class(metropolis_hastings), intent(in) :: this

    The metropolis_hastings object.

    real(kind=real64), intent(in), dimension(:) :: xc

    The array of variables to evaluate.

    Return Value real(kind=real64)

    The value of the PDF at xc.

procedure, public :: generate_proposal => mr_proposal

  • private function mr_proposal(this, xc) result(rst)

    Arguments

    Arguments

    Type IntentOptional Attributes Name
    class(mcmc_regression), intent(inout) :: this

    The mcmc_regression object.

    real(kind=real64), intent(in), dimension(:) :: xc

    The current model parameters.

    Return Value real(kind=real64), allocatable, dimension(:)

    The proposed set of model parameters.

procedure, public :: get_accepted_count => mh_get_num_accepted

  • private pure function mh_get_num_accepted(this) result(rst)

    Gets the number of accepted steps.

    Arguments

    Type IntentOptional Attributes Name
    class(metropolis_hastings), intent(in) :: this

    The metropolis_hastings object.

    Return Value integer(kind=int32)

    The number of accepted steps.

procedure, public :: get_chain => mh_get_chain

  • private function mh_get_chain(this, bin, err) result(rst)

    Gets a copy of the stored Markov chain.

    Arguments

    Type IntentOptional Attributes Name
    class(metropolis_hastings), intent(in) :: this

    The metropolis_hastings object.

    real(kind=real64), intent(in), optional :: bin

    An optional input allowing for a burn-in region. The parameter represents the amount (percentage-based) of the overall chain to disregard as "burn-in" values. The value shoud exist on [0, 1). The default value is 0 such that no values are disregarded.

    class(errors), intent(inout), optional, target :: err

    The error handling object.

    Return Value real(kind=real64), allocatable, dimension(:,:)

    The resulting chain with each parameter represented by a column.

procedure, public :: get_chain_length => mh_get_chain_length

  • private pure function mh_get_chain_length(this) result(rst)

    Gets the length of the chain (number of stored state variables).

    Arguments

    Type IntentOptional Attributes Name
    class(metropolis_hastings), intent(in) :: this

    The metropolis_hastings object.

    Return Value integer(kind=int32)

    The chain length.

procedure, public :: get_data_variance => mr_get_data_variance

  • private pure function mr_get_data_variance(this) result(rst)

    Gets the variance of the observed data.

    Arguments

    Type IntentOptional Attributes Name
    class(mcmc_regression), intent(in) :: this

    The mcmc_regression object.

    Return Value real(kind=real64)

    The variance.

procedure, public :: get_proposal_cholesky => mh_get_prop_chol_cov

  • private pure function mh_get_prop_chol_cov(this) result(rst)

    Gets the Cholesky-factored (lower-triangular) form of the proposal covariance matrix.

    Arguments

    Type IntentOptional Attributes Name
    class(metropolis_hastings), intent(in) :: this

    The metropolis_hastings object.

    Return Value real(kind=real64), allocatable, dimension(:,:)

    The Cholesky-factored form of the proposal covariance matrix store in lower-triangular form.

procedure, public :: get_proposal_covariance => mh_get_prop_cov

  • private pure function mh_get_prop_cov(this) result(rst)

    Gets the covariance matrix of the proposal distribution.

    Arguments

    Type IntentOptional Attributes Name
    class(metropolis_hastings), intent(in) :: this

    The metropolis_hastings object.

    Return Value real(kind=real64), allocatable, dimension(:,:)

    The covariance matrix.

procedure, public :: get_proposal_initialized => mh_get_is_prop_init

  • private pure function mh_get_is_prop_init(this) result(rst)

    Gets a value determining if the proposal distribution object has been initialized.

    Arguments

    Type IntentOptional Attributes Name
    class(metropolis_hastings), intent(in) :: this

    The metropolis_hastings object.

    Return Value logical

    Returns true if the object has been initialized; else, false.

procedure, public :: get_proposal_means => mh_get_prop_mean

  • private pure function mh_get_prop_mean(this) result(rst)

    Gets the mean values of the proposal distribution.

    Arguments

    Type IntentOptional Attributes Name
    class(metropolis_hastings), intent(in) :: this

    The metropolis_hastings object.

    Return Value real(kind=real64), allocatable, dimension(:)

    An array containing the mean values.

procedure, public :: get_state_variable_count => mh_get_nvars

  • private pure function mh_get_nvars(this) result(rst)

    Gets the number of state variables.

    Arguments

    Type IntentOptional Attributes Name
    class(metropolis_hastings), intent(in) :: this

    The metropolis_hastings object.

    Return Value integer(kind=int32)

    The number of state variables.

generic, public :: initialize_proposal => mh_init_proposal_1, mh_init_proposal_2

  • private subroutine mh_init_proposal_1(this, mu, sigma, err)

    Initializes the multivariate normal distribution used to generate proposals.

    Arguments

    Type IntentOptional Attributes Name
    class(metropolis_hastings), intent(inout) :: this

    The metropolis_hastings object.

    real(kind=real64), intent(in), dimension(:) :: mu

    An N-element array containing the mean values for the distribution.

    real(kind=real64), intent(in), dimension(:,:) :: sigma

    An N-by-N covariance matrix for the distribution. This matrix must be positive-definite.

    class(errors), intent(inout), optional, target :: err

    An error handling object.

  • private subroutine mh_init_proposal_2(this, n, err)

    Initializes the multivariate normal distribution to a mean of zero and a variance of one.

    Arguments

    Type IntentOptional Attributes Name
    class(metropolis_hastings), intent(inout) :: this

    The metropolis_hastings object.

    integer(kind=int32), intent(in) :: n

    The number of state variables.

    class(errors), intent(inout), optional, target :: err

    An error handling object.

procedure, public :: likelihood => mr_likelihood

  • private function mr_likelihood(this, x) result(rst)

    Estimates the likelihood of the model.

    The likelihood is computed as follows assuming is known a priori.

    Arguments

    Type IntentOptional Attributes Name
    class(mcmc_regression), intent(inout) :: this

    The mcmc_regression object.

    real(kind=real64), intent(in), dimension(:) :: x

    The current set of model parameters.

    Return Value real(kind=real64)

    The likelihood value.

procedure, public :: on_acceptance => mr_on_success

  • private subroutine mr_on_success(this, iter, alpha, xc, xp, err)

    Updates the covariance matrix of the proposal distribution upon a successful step. If overloaded, be sure to call the base method to retain the functionallity required to keep the covariance matrix up-to-date.

    Arguments

    Type IntentOptional Attributes Name
    class(mcmc_regression), intent(inout) :: this

    The mcmc_regression object.

    integer(kind=int32), intent(in) :: iter

    The current iteration number.

    real(kind=real64), intent(in) :: alpha

    The proposal probability term used for acceptance criteria.

    real(kind=real64), intent(in), dimension(:) :: xc

    The current model parameter estimates.

    real(kind=real64), intent(in), dimension(size(xc)) :: xp

    The recently accepted model parameter estimates.

    class(errors), intent(inout), optional, target :: err

    An error handling object.

procedure, public :: on_rejection => mh_on_rejection

  • private subroutine mh_on_rejection(this, iter, alpha, xc, xp, err)

    Currently, this routine does nothing and is a placeholder for the user that inherits this class to provide functionallity upon rejection of a proposed value.

    Arguments

    Type IntentOptional Attributes Name
    class(metropolis_hastings), intent(inout) :: this

    The metropolis_hastings object.

    integer(kind=int32), intent(in) :: iter

    The current iteration number.

    real(kind=real64), intent(in) :: alpha

    The proposal probabilty term used for acceptance criteria.

    real(kind=real64), intent(in), dimension(:) :: xc

    An N-element array containing the current state variables.

    real(kind=real64), intent(in), dimension(size(xc)) :: xp

    An N-element array containing the proposed state variables that were just rejected.

    class(errors), intent(inout), optional, target :: err

    An error handling object.

procedure, public :: push_new_state => mr_push

  • private subroutine mr_push(this, x, err)

    Pushes a new set of parameters onto the end of the chain buffer.

    Arguments

    Type IntentOptional Attributes Name
    class(mcmc_regression), intent(inout) :: this

    The mcmc_regression object.

    real(kind=real64), intent(in), dimension(:) :: x

    The new N-element state array.

    class(errors), intent(inout), optional, target :: err

    An error handling object.

procedure, public :: reset => mh_clear_chain

  • private subroutine mh_clear_chain(this)

    Resets the object and clears out the buffer storing the chain values.

    Arguments

    Type IntentOptional Attributes Name
    class(metropolis_hastings), intent(inout) :: this

    The metropolis_hastings object.

procedure, public :: sample => mh_sample

  • private subroutine mh_sample(this, xi, niter, err)

    Samples the distribution using the Metropolis-Hastings algorithm.

    Arguments

    Type IntentOptional Attributes Name
    class(metropolis_hastings), intent(inout) :: this

    The metropolis_hastings object.

    real(kind=real64), intent(in), dimension(:) :: xi

    An N-element array containing initial starting values of the state variables.

    integer(kind=int32), intent(in), optional :: niter

    An optional input defining the number of iterations to take. The default is 10,000.

    class(errors), intent(inout), optional, target :: err

    The error handling object.

procedure, public :: set_data_variance => mr_set_data_variance

  • private subroutine mr_set_data_variance(this, x)

    Sets the variance of the observed data.

    Arguments

    Type IntentOptional Attributes Name
    class(mcmc_regression), intent(inout) :: this

    The mcmc_regression object.

    real(kind=real64), intent(in) :: x

    The variance.

procedure, public :: set_proposal_covariance => mh_set_prop_cov

  • private subroutine mh_set_prop_cov(this, x, err)

    Sets the covariance matrix of the proposal distribution.

    Arguments

    Type IntentOptional Attributes Name
    class(metropolis_hastings), intent(inout) :: this

    The metropolis_hastings object.

    real(kind=real64), intent(in), dimension(:,:) :: x

    The covariance matrix. This matrix must be positive-definite.

    class(errors), intent(inout), optional, target :: err

    An error handling object.

procedure, public :: set_proposal_means => mh_set_prop_mean

  • private subroutine mh_set_prop_mean(this, x, err)

    Sets the mean values of the proposal distribution.

    Arguments

    Type IntentOptional Attributes Name
    class(metropolis_hastings), intent(inout) :: this

    The metropolis_hastings object.

    real(kind=real64), intent(in), dimension(:) :: x

    The updated mean values.

    class(errors), intent(inout), optional, target :: err

    An error handling object.

procedure, public :: target_distribution => mr_target

  • private function mr_target(this, x) result(rst)

    Returns the probability value from the target distribution given the current set of model parameters.

    The probability value is determined as follows, assuming is the function value. .

    Arguments

    Type IntentOptional Attributes Name
    class(mcmc_regression), intent(inout) :: this

    The mcmc_regression object.

    real(kind=real64), intent(in), dimension(:) :: x

    The current set of model parameters.

    Return Value real(kind=real64)

    The value of the probability density function being sampled.