bayesmix/hierarchies/updaters
Updaters¶
An Updater implements the machinery to provide a sampling from the full conditional distribution of a given hierarchy.
The only operation performed is draw that samples from the full conditional, either exactly or via Markov chain Monte Carlo.
-
class AbstractUpdater¶
Base class for an Updater.
An updater is a class able to sample from the full conditional distribution of an
Hierarchy, coming from the product of aLikelihoodand aPrior, possibly using a Metropolis-Hastings algorithm.Subclassed by MetropolisUpdater< RandomWalkUpdater >, MetropolisUpdater< MalaUpdater >, SemiConjugateUpdater< UniNormLikelihood, NxIGPriorModel >, SemiConjugateUpdater< MultiNormLikelihood, NWPriorModel >, SemiConjugateUpdater< UniNormLikelihood, NIGPriorModel >, SemiConjugateUpdater< UniLinRegLikelihood, MNIGPriorModel >, FAUpdater, MetropolisUpdater< DerivedUpdater >, SemiConjugateUpdater< Likelihood, PriorModel >
Public Functions
-
virtual ~AbstractUpdater() = default¶
Default destructor.
-
inline virtual bool is_conjugate() const¶
Returns whether the current updater is for a (semi)conjugate model or not.
-
virtual void draw(AbstractLikelihood &like, AbstractPriorModel &prior, bool update_params) = 0¶
Sampling from the full conditional, given the likelihood and the prior model that constitutes the hierarchy
- Parameters:
like – The likelihood of the hierarchy
prior – The prior model of the hierarchy
update_params – Save posterior hyperparameters after draw?
-
inline virtual ProtoHypersPtr compute_posterior_hypers(AbstractLikelihood &like, AbstractPriorModel &prior)¶
Computes the posterior hyperparameters required for the sampling in case of conjugate hierarchies
-
inline void save_posterior_hypers(ProtoHypersPtr post_hypers_)¶
Stores the posterior hyperparameters in an appropriate container.
-
virtual ~AbstractUpdater() = default¶
Code Structure¶
We distinguish between semi-conjugate updaters and the metropolis-like updaters.
Semi Conjugate Updaters¶
A semi-conjugate updater can be used when the full conditional distribution has the same form of the prior. Therefore, to sample from the full conditional, it is sufficient to call the draw method of the prior, but with an updated set of hyperparameters.
The class SemiConjugateUpdater defines the API
-
template<class Likelihood, class PriorModel>
class SemiConjugateUpdater : public AbstractUpdater¶ Updater for semi-conjugate hierarchies.
We say that a hierarchy is semi-conjugate if the full conditionals of each parameter is in the same parametric family of the prior distribution of that parameter.
As a consequence, sampling from the full conditional can be done by calling the
samplemethod from thePriorModelclass, with updater hyperparametersClasses inheriting from this one should only implement the
compute_posterior_hypers(...)member functionThis class is templated with respect to
- Template Parameters:
Likelihood – the likelihood of the hierarchy, instance of
AbstractLikelihoodPriorModel – the prior of the hierarchy, instance of
AbstractPriorModel
Public Functions
-
virtual void draw(AbstractLikelihood &like, AbstractPriorModel &prior, bool update_params) override¶
Sampling from the full conditional, given the likelihood and the prior model that constitutes the hierarchy
- Parameters:
like – The likelihood of the hierarchy
prior – The prior model of the hierarchy
update_params – Save posterior hyperparameters after draw?
Classes inheriting from this one should only implement the compute_posterior_hypers(...) member function.
Metropolis-like Updaters¶
A Metropolis updater uses the Metropolis-Hastings algorithm (or its variations) to sample from the full conditional density.
-
template<class DerivedUpdater>
class MetropolisUpdater : public AbstractUpdater¶ Base class for updaters using a Metropolis-Hastings algorithm
This class serves as the base for a CRTP. Children of this class should implement the methods template <typename F> Eigen::VectorXd sample_proposal(Eigen::VectorXd curr_state, AbstractLikelihood &like, AbstractPriorModel &prior, F &target_lpdf) and template <typename F> double proposal_lpdf(Eigen::VectorXd prop_state, Eigen::VectorXd curr_state, AbstractLikelihood &like, AbstractPriorModel &prior, F &target_lpdf) where the template parameter is needed to allow the use of stan’s automatic differentiation if the gradient of the full conditional is required.
Public Functions
-
inline virtual void draw(AbstractLikelihood &like, AbstractPriorModel &prior, bool update_params) override¶
Samples from the full conditional distribution using a Metropolis-Hastings step
-
inline virtual void draw(AbstractLikelihood &like, AbstractPriorModel &prior, bool update_params) override¶
Classes inheriting from this one should only implement the sample_proposal(...) method, which samples from the porposal distribution, and the proposal_lpdf one, which evaluates the proposal density log-probability density function.
Updater Classes¶
-
class RandomWalkUpdater : public MetropolisUpdater<RandomWalkUpdater>¶
Metropolis-Hastings updater using an isotropic proposal function centered in the current value of the parameters (unconstrained). This class requires that the Hierarchy’s state implements the
get_unconstrained(),set_from_unconstrained()andlog_det_jac()functions.Given the current value of the unconstrained parameters
, a new value is proposed from
and then either accepted (in which case the hierarchy’s state is set to
) or rejected. Public Functions
-
template<typename F>
inline Eigen::VectorXd sample_proposal(Eigen::VectorXd curr_state, AbstractLikelihood &like, AbstractPriorModel &prior, F &target_lpdf)¶ Samples from the proposal distribution
- Parameters:
curr_state – the current state (unconstrained parametrization)
like – instance of likelihood
prior – instance of prior
target_lpdf – either double or stan::math::var. Needed for stan’s automatic differentiation. It is not used here.
-
template<typename F>
inline double proposal_lpdf(Eigen::VectorXd prop_state, Eigen::VectorXd curr_state, AbstractLikelihood &like, AbstractPriorModel &prior, F &target_lpdf)¶ Evaluates the log probability density function of the proposal
- Parameters:
prop_state – the proposed state (at which to evaluate the lpdf)
curr_state – the current state (unconstrained parametrization)
like – instance of likelihood
prior – instance of prior
target_lpdf – either double or stan::math::var. Needed for stan’s automatic differentiation. It is not used here.
-
inline virtual std::shared_ptr<AbstractUpdater> clone() const override¶
Returns a shared_ptr to a new instance of
this
-
template<typename F>
-
class MalaUpdater : public MetropolisUpdater<MalaUpdater>¶
Metropolis Adjusted Langevin Algorithm.
This class requires that the Hierarchy’s state implements the
get_unconstrained(),set_from_unconstrained()andlog_det_jac()functions.Given the current value of the unconstrained parameters
, a new value is proposed from
and then either accepted (in which case the hierarchy’s state is set to
) or rejected. Public Functions
-
inline Eigen::VectorXd sample_proposal(Eigen::VectorXd curr_state, AbstractLikelihood &like, AbstractPriorModel &prior, target_lpdf_unconstrained &target_lpdf)¶
Samples from the proposal distribution
- Parameters:
curr_state – the current state (unconstrained parametrization)
like – instance of likelihood
prior – instance of prior
target_lpdf – either double or stan::math::var. Needed for stan’s automatic differentiation. It will be filled with the lpdf at the ‘curr_state’
-
inline double proposal_lpdf(Eigen::VectorXd prop_state, Eigen::VectorXd curr_state, AbstractLikelihood &like, AbstractPriorModel &prior, target_lpdf_unconstrained &target_lpdf)¶
Evaluates the log probability density function of the proposal
- Parameters:
prop_state – the proposed state (at which to evaluate the lpdf)
curr_state – the current state (unconstrained parametrization)
like – instance of likelihood
prior – instance of prior
target_lpdf – either double or stan::math::var. Needed for stan’s automatic differentiation. It will be filled with the lpdf at ‘curr_state’
-
inline Eigen::VectorXd sample_proposal(Eigen::VectorXd curr_state, AbstractLikelihood &like, AbstractPriorModel &prior, target_lpdf_unconstrained &target_lpdf)¶
-
class NNIGUpdater : public SemiConjugateUpdater<UniNormLikelihood, NIGPriorModel>¶
Updater specific for the
UniNormLikelihoodused in combination withNIGPriorModel, that is the model
It exploits the conjugacy of the model to sample the full conditional of
by calling NIGPriorModel::samplewith updated parametersPublic Functions
-
inline virtual bool is_conjugate() const override¶
Returns whether the current updater is for a (semi)conjugate model or not.
-
virtual ProtoHypersPtr compute_posterior_hypers(AbstractLikelihood &like, AbstractPriorModel &prior) override¶
Computes the posterior hyperparameters required for the sampling in case of conjugate hierarchies
-
inline virtual bool is_conjugate() const override¶
-
class NNxIGUpdater : public SemiConjugateUpdater<UniNormLikelihood, NxIGPriorModel>¶
Updater specific for the
UniNormLikelihoodused in combination withNxIGPriorModel, that is the model
It exploits the semi-conjugacy of the model to sample the full conditional of
by calling NxIGPriorModel::samplewith updated parametersPublic Functions
-
virtual ProtoHypersPtr compute_posterior_hypers(AbstractLikelihood &like, AbstractPriorModel &prior) override¶
Computes the posterior hyperparameters required for the sampling in case of conjugate hierarchies
-
virtual ProtoHypersPtr compute_posterior_hypers(AbstractLikelihood &like, AbstractPriorModel &prior) override¶
-
class NNWUpdater : public SemiConjugateUpdater<MultiNormLikelihood, NWPriorModel>¶
Updater specific for the
MultiNormLikelihoodused in combination withNWPriorModel, that is the model
It exploits the conjugacy of the model to sample the full conditional of
by calling NWPriorModel::samplewith updated parameters.Public Functions
-
inline virtual bool is_conjugate() const override¶
Returns whether the current updater is for a (semi)conjugate model or not.
-
virtual ProtoHypersPtr compute_posterior_hypers(AbstractLikelihood &like, AbstractPriorModel &prior) override¶
Computes the posterior hyperparameters required for the sampling in case of conjugate hierarchies
-
inline virtual bool is_conjugate() const override¶
-
class MNIGUpdater : public SemiConjugateUpdater<UniLinRegLikelihood, MNIGPriorModel>¶
Updater specific for the
UniLinRegLikelihoodused in combination withMNIGPriorModel, that is the model
It exploits the conjugacy of the model to sample the full conditional of
by calling MNIGPriorModel::samplewith updated parametersPublic Functions
-
inline virtual bool is_conjugate() const override¶
Returns whether the current updater is for a (semi)conjugate model or not.
-
virtual ProtoHypersPtr compute_posterior_hypers(AbstractLikelihood &like, AbstractPriorModel &prior) override¶
Computes the posterior hyperparameters required for the sampling in case of conjugate hierarchies
-
inline virtual bool is_conjugate() const override¶
-
class FAUpdater : public AbstractUpdater¶
Updater specific for the
FAHierachy. See Bhattacharya, Anirban, and David B. Dunson. “Sparse Bayesian infinite factor models.” Biometrika (2011): 291-306. for further detailsPublic Functions
-
virtual void draw(AbstractLikelihood &like, AbstractPriorModel &prior, bool update_params) override¶
Sampling from the full conditional, given the likelihood and the prior model that constitutes the hierarchy
- Parameters:
like – The likelihood of the hierarchy
prior – The prior model of the hierarchy
update_params – Save posterior hyperparameters after draw?
-
virtual void draw(AbstractLikelihood &like, AbstractPriorModel &prior, bool update_params) override¶