Skip to content

Instantly share code, notes, and snippets.

@mmulvahill
Last active December 1, 2017 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmulvahill/aa82aca6edf985bb07305693e8cf82a7 to your computer and use it in GitHub Desktop.
Save mmulvahill/aa82aca6edf985bb07305693e8cf82a7 to your computer and use it in GitHub Desktop.
Abstract class approach to a proposal variance class for a modified Metropolis-Hastings sampler
//
// initial abstract class approach to the PV classes. The key problem lines
// are denoted by ****. The gist is 1. abstract classes don't allow you to leave
// the return type undefined/virtual, nor member object types to be left
// undefined/virtual. But, 2. the class does need to function differently (methods
// denoted ----).
//
// So far I've figured that a template class approach would solve the 1, but make 2
// more challenging -- how to define different methods for each type -- overloading?
// -- and how to limit to only the two types wanted (double and arma::mat<2,2>)
//
// I could use just a standard class and, for the marginal/1-parm PV, use a 1x1
// arma::mat instead of double PV then use if/then logic based on size() of the matrix
// to switch the functionality? Not a huge fan of this, but it would probably work
// just fine
//
#ifndef GUARD_proposalvariance_h
#define GUARD_proposalvariance_h
// [[Rcpp::depends(RcppArmadillo)]]
#include <Rcpp.h>
#include <RcppArmadillo.h>
//
// proposalvariance.h
//
// Definitions for the ProposalVariance class and its derivatives. This is
// an abstract class defining common elements for tracking acceptance rates
// and adjusting the proposal variance, needed for a Modified
// Metropolis-Hastings.
//
// NOTE: I've removed all the stuff related to acceptance rates...
//
// Author: Matt Mulvahill
// Created: 10/13/17
//
// NOTE: ProposalVariance has a constructor only to document the requirements
// for the constructor. It can't actually be used since its an abstract class.
//
// Abstract class definition
class ProposalVariance {
public:
// proposal variance accessors/methods
virtual void adjustpv();
virtual getpv() const { return pv; }; // ****
virtual getpsd() const { return psd; }; // ****
protected:
ProposalVariance(double initial_pv); // initial proposal variance (marginal)
ProposalVariance(arma::vec initial_pv); // initial proposal variances (joint/2-parm)
~ProposalVariance();
private:
virtual pv; // proposal variance ****
virtual psd; // proposal standard deviation or chol decomposed ****
virtual void initialize_proposals() = 0; // ----
virtual void set_proposals() = 0; // ----
};
// Marginal PV derivative class
class MarginalProposalVariance : public ProposalVariance
{
public:
MarginalProposalVariance(double initial_pv); // Constructor
~ProposalVariance(); // Destructor
void adjustpv();
double getpv() const { return pv; }; // ****
double getpsd() const { return psd; }; // ****
private:
void initialize_proposals(); // ----
void set_proposals(); // ----
double pv; // proposal var-covar matrix ****
double psd; // proposal var-covar matrix ****
};
// Joint PV derivative class
class JointProposalVariance : public ProposalVariance
{
public:
JointProposalVariance(arma::vec initial_pv, int adjust_iter, int max_iters,
double target_ratio);
~ProposalVariance(); // Destructor
void adjustpv(double corr = -0.90);
arma::mat getpv() const { return pv; }; // ****
arma::mat getpsd() const { return psd; }; // ****
private:
void initialize_proposal(arma::vec initial_pv); // ----
void set_proposals(double corr); // ----
double calc_covariance(double corr); // ----
arma::mat::fixed<2, 2> pv; // proposal var-covar matrix ****
arma::mat::fixed<2, 2> psd; // proposal correlation matrix ****
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment