bayesmix/algorithms
The Algorithm class handles other class objects and performs the MCMC simulation.
There are two types of Algorithm: marginal and conditional, each of which can only be used with the matching type of Mixing.
Algorithms¶
-
class BaseAlgorithm¶
Abstract template class representing a Gibbs sampler for mixture models. Gibbs samplers are a particular class of Markov chain Monte Carlo (MCMC) algorithms that are used to perform posterior inference in Bayesian models. For the specific class of models that can be fit using these algorithm, see
MarginalAlgorithmandConditionalAlgorithm.This class receives a number of initialization parameters related to the MCMC algorithm itself (such as the number of iterations) in a Protobuf message. It also utilizes several class objects related to the underlying model: a
Mixingobject, and a vector ofHierarchyobjects. It includes methods for running the MCMC simulation and for estimating the posterior density on a given grid of points. In particular, density estimation is performed by averaging the estimate associated to each single MCMC iteration. The specific method depends on whether the algorithm is marginal or conditional (see derived classesMarginalAlgorithmandConditionalAlgorithm). Results for this class’ methods are saved either toCollectorobjects, or to text files.Subclassed by ConditionalAlgorithm, MarginalAlgorithm
Public Functions
-
virtual bool is_conditional() const = 0¶
Returns whether the algorithm is conditional or marginal.
-
inline virtual bool requires_conjugate_hierarchy() const¶
Returns whether it is restricted to conjugate
Hierarchyobjects.
-
inline void run(BaseCollector *collector)¶
Runs the algorithm and saves the MCMC chain to the given
Collector
-
virtual Eigen::MatrixXd eval_lpdf(BaseCollector *const collector, const Eigen::MatrixXd &grid, const Eigen::RowVectorXd &hier_covariate = Eigen::RowVectorXd(0), const Eigen::RowVectorXd &mix_covariate = Eigen::RowVectorXd(0))¶
Evaluates the estimated log-pdf on all iterations over a grid of points
- Parameters:
collector –
Collectorobject from which the MCMC is readgrid – Grid of row points on which the density is to be evaluated
hier_covariate – Optional covariates related to the
Hierarchymix_covariate – Optional covariates related to the
Mixing
- Returns:
The estimation over all iterations (rows) and points (cols)
-
virtual void read_params_from_proto(const bayesmix::AlgorithmParams ¶ms)¶
Reads and sets algorithm parameters from an appropriate Protobuf message.
-
virtual Eigen::VectorXd lpdf_from_state(const Eigen::MatrixXd &grid, const Eigen::RowVectorXd &hier_covariate, const Eigen::RowVectorXd &mix_covariate) = 0¶
Evaluates the estimated log-lpdf on the state contained in
curr_state- Parameters:
grid – Grid of row points on which the density is to be evaluated
hier_covariate – Optional covariates related to the
Hierarchymix_covariate – Optional covariates related to the
Mixing
- Returns:
The estimation on the iteration of
curr_stateover all points
-
virtual bool is_conditional() const = 0¶
-
class MarginalAlgorithm : public BaseAlgorithm¶
Template class for a marginal sampler deriving from
BaseAlgorithm.This template class implements a generic Gibbs sampling marginal algorithm as the child of the
BaseAlgorithmclass. A mixture model sampled from a Marginal Algorithm can be expressed as
where
is a density for each value of
and
take values in
. Depending on the actual implementation, the algorithm might require the kernel/likelihood
and
to be conjugagte or not. In the former case, a conjugate hierarchy must be specified. In this library, each
is represented as an Hierarchyobject (which inherits fromAbstractHierarchy), that also holds the information related to the base measure
is (see AbstractHierarchy). The
is instead represented as a Mixingobject, which inherits fromAbstractMixing.The state of a marginal algorithm only consists of allocations and unique values. In this class of algorithms, the local lpdf estimate for a single iteration is a weighted average of likelihood values corresponding to each component (i.e. cluster), with the weights being based on its cardinality, and of the marginal component, which depends on the specific algorithm.
Subclassed by Neal2Algorithm, SplitAndMergeAlgorithm
Public Functions
-
inline virtual bool is_conditional() const override¶
Returns whether the algorithm is conditional or marginal.
-
virtual Eigen::VectorXd lpdf_from_state(const Eigen::MatrixXd &grid, const Eigen::RowVectorXd &hier_covariate, const Eigen::RowVectorXd &mix_covariate) override¶
Evaluates the estimated log-lpdf on the state contained in
curr_state- Parameters:
grid – Grid of row points on which the density is to be evaluated
hier_covariate – Optional covariates related to the
Hierarchymix_covariate – Optional covariates related to the
Mixing
- Returns:
The estimation on the iteration of
curr_stateover all points
-
inline virtual bool is_conditional() const override¶
-
class Neal2Algorithm : public MarginalAlgorithm¶
Template class for Neal’s algorithm 2 for conjugate hierarchies.
This class implements Neal’s Gibbs sampling algorithm 2 from Neal (2000) that generates a Markov chain on the clustering of the provided data.
This algorithm requires the use of a
ConjugateHierarchyobject.Subclassed by Neal3Algorithm, Neal8Algorithm
-
class Neal3Algorithm : public Neal2Algorithm¶
Template class for Neal’s algorithm 3 for conjugate hierarchies.
This class implements Neal’s Gibbs sampling algorithm 3 from Neal (2000) that generates a Markov chain on the clustering of the provided data.
This algorithm requires the use of a
ConjugateHierarchyobject. Algorithm 3 from Neal (2000) is almost identical to Algorithm 2, except that conjugacy is further exploied by marginalizing the unique values from the state when updating the cluster allocations, which leads to improved efficiency in terms of effective sample size, but might require longer runtimes. For more information, please refer to theNeal2Algorithmclass, as well asBaseAlgorithmandMarginalAlgorithmon which it is based.
-
class Neal8Algorithm : public Neal2Algorithm¶
Template class for Neal’s algorithm 8 for conjugate hierarchies.
This class implements Neal’s Gibbs sampling algorithm 8 from Neal (2000) that generates a Markov chain on the clustering of the provided data.
It extends Neal’s algorithm 2 to also deal with cases when the kernel/likelihood f(x | phi) is not conjugate to G, thanks to the introduction of additional, auxiliary unique values.
Public Functions
-
inline virtual bool requires_conjugate_hierarchy() const override¶
Returns whether it is restricted to conjugate
Hierarchyobjects.
-
inline unsigned int get_n_aux() const¶
Returns number of auxiliary blocks.
-
inline void set_n_aux(const unsigned int n_aux_)¶
Sets number of auxiliary blocks.
-
inline virtual bool requires_conjugate_hierarchy() const override¶
-
class SplitAndMergeAlgorithm : public MarginalAlgorithm¶
Template class for Split and Merge for conjugate hierarchies.
This class implements Split and Merge algorithm from Jain and Neal (2004) that generates a Markov chain on the clustering of the provided data.
This algorithm requires the use of a
ConjugateHierarchyobject. After picking at random two points, the Split and Merge algorithm takes all the points that are in the same clusters as the first two and assigns to each of them, at random, one of two temporary clusters. Then, it updates these assignments by doing a restricted Gibbs sampling (GS), restricted in the sense that it considers only the points cited above and only the two temporary clusters. The result of this operation is used to compute the acceptance probability for the split or merge proposal. These Metropolis-Hastings (MH) steps are alternated with full Gibbs sampling steps, similar to the ones that are computed in Neal3 algorithm.
-
class ConditionalAlgorithm : public BaseAlgorithm¶
Template class for a conditional sampler deriving from
BaseAlgorithm.This template class implements a generic Gibbs sampling conditional algorithm as the child of the
BaseAlgorithmclass. A mixture model sampled from a conditional algorithm can be expressed as
where
is a density for each value of
,
take values in
and
are nonnegative weights whose sum is a.s. 1, i.e.
is a probability distribution on the k-1 dimensional unit simplex). In this library, each
is represented as an Hierarchyobject (which inherits fromAbstractHierarchy), that also holds the information related to the base measure
is (see AbstractHierarchy). The weights
are represented as a Mixingobject, which inherits fromAbstractMixing.The state of a conditional algorithm consists of the unique values, the cluster allocations and the mixture weights. The former two are stored in this class, while the weights are stored in the
Mixingobject.Subclassed by BlockedGibbsAlgorithm, SliceSampler
Public Functions
-
inline virtual bool is_conditional() const override¶
Returns whether the algorithm is conditional or marginal.
-
virtual Eigen::VectorXd lpdf_from_state(const Eigen::MatrixXd &grid, const Eigen::RowVectorXd &hier_covariate, const Eigen::RowVectorXd &mix_covariate) override¶
Evaluates the estimated log-lpdf on the state contained in
curr_state- Parameters:
grid – Grid of row points on which the density is to be evaluated
hier_covariate – Optional covariates related to the
Hierarchymix_covariate – Optional covariates related to the
Mixing
- Returns:
The estimation on the iteration of
curr_stateover all points
-
inline virtual bool is_conditional() const override¶
-
class BlockedGibbsAlgorithm : public ConditionalAlgorithm¶
Template class for the blocked Gibbs sampling algorithm.
This class implement the blocked Gibbs sampling procedure from [1].
[1] Ishwaran, H., & James, L. F. (2001). Gibbs sampling methods for stick-breaking priors. Journal of the American Statistical Association, 96(453), 161-173.