bayesmix/hierarchies/likelihoods/states

States

States are classes used to store parameters \theta_h of every mixture component. Their main purpose is to handle serialization and de-serialization of the state. Moreover, they allow to go from the constrained to the unconstrained representation of the parameters (and viceversa) and compute the associated determinant of the Jacobian appearing in the change of density formula.

Code Structure

All classes must inherit from the BaseState class

class BaseState

Abstract base class for a generic state

Given a statistical model with likelihood L(y \mid \tau) and prior p(\tau) a State class represents the value of tau at a certain MCMC iteration. In addition, each instance stores the cardinality of the number of observations in the model.

State classes inheriting from this one should implement the methods set_from_proto() and to_proto(), that are used to deserialize from (and serialize to) a bayesmix::AlgorithmState::ClusterState protocol buffer message.

Optionally, each state can have an “unconstrained” representation, where a bijective transformation B is applied to \tau, so that the image of B is in \mathbb{R}^d for some d. This is essential for the default updaters such as RandomWalkUpdater and MalaUpdater to work, but is not necessary for other model-specific updaters. If such a representation is needed, child classes should also implement get_unconstrained(), set_from_unconstrained(), and log_det_jac().

Subclassed by State::FA, State::MultiLS, State::UniLS, State::UniLinRegLS

Public Functions

inline virtual Eigen::VectorXd get_unconstrained() const

Returns the unconstrained representation x = B(\tau).

inline virtual void set_from_unconstrained(const Eigen::VectorXd &in)

Sets the current state as \tau = B^{-1}(in)

Parameters:

in – the unconstrained representation of the state

inline virtual double log_det_jac() const

Returns the log determinant of the jacobian of B^{-1}.

virtual void set_from_proto(const ProtoState &state_, bool update_card) = 0

Sets the current state from a protobuf object

Parameters:
  • state_ – a bayesmix::AlgorithmState::ClusterState instance

  • update_card – if true, the current cardinality is updated

virtual ProtoState get_as_proto() const = 0

Returns a bayesmix::AlgorithmState::ClusterState representing the current value of the state

inline std::shared_ptr<ProtoState> to_proto() const

Returns a shared pointer to bayesmix::AlgorithmState::ClusterState representing the current value of the state

Depending on the chosen Updater, the unconstrained representation might not be needed, and the methods get_unconstrained(), set_from_unconstrained() and log_det_jac() might never be called. Therefore, we do not force users to implement them. Instead, the set_from_proto() and get_as_proto() are fundamental as they allow the interaction with Google’s Protocol Buffers library.

State Classes

class UniLS : public State::BaseState

A univariate location-scale state with parametrization (mean, var) The unconstrained representation corresponds to (mean, log(var))

Public Functions

inline virtual Eigen::VectorXd get_unconstrained() const override

Returns the unconstrained representation x = B(\tau).

inline virtual void set_from_unconstrained(const Eigen::VectorXd &in) override

Sets the current state as \tau = B^{-1}(in)

Parameters:

in – the unconstrained representation of the state

inline virtual void set_from_proto(const ProtoState &state_, bool update_card) override

Sets the current state from a protobuf object

Parameters:
  • state_ – a bayesmix::AlgorithmState::ClusterState instance

  • update_card – if true, the current cardinality is updated

inline virtual ProtoState get_as_proto() const override

Returns a bayesmix::AlgorithmState::ClusterState representing the current value of the state

inline virtual double log_det_jac() const override

Returns the log determinant of the jacobian of B^{-1}.

class MultiLS : public State::BaseState

A univariate location-scale state with parametrization (mean, Cov) where Cov is the covariance matrix. The unconstrained representation corresponds to (mean, B(cov)), where B is the stan::math::cov_matrix_free() transformation.

Public Functions

inline virtual Eigen::VectorXd get_unconstrained() const override

Returns the unconstrained representation x = B(\tau).

inline virtual void set_from_unconstrained(const Eigen::VectorXd &in) override

Sets the current state as \tau = B^{-1}(in)

Parameters:

in – the unconstrained representation of the state

inline virtual void set_from_proto(const ProtoState &state_, bool update_card) override

Sets the current state from a protobuf object

Parameters:
  • state_ – a bayesmix::AlgorithmState::ClusterState instance

  • update_card – if true, the current cardinality is updated

inline virtual ProtoState get_as_proto() const override

Returns a bayesmix::AlgorithmState::ClusterState representing the current value of the state

inline virtual double log_det_jac() const override

Returns the log determinant of the jacobian of B^{-1}.

class FA : public State::BaseState

State of a Factor Analytic model

Y_i = \Lambda\bm{\eta}_i + \bm{\varepsilon}

where Y_i is a p-dimensional vetor, \bm{\eta}_i is a d-dimensional one, \Lambda is a p \times d matrix and \bm{\varepsilon} is an error term with mean zero and diagonal covariance matrix \psi.

For faster likelihood evaluation, we store also the cov_wood factor and the log determinant of the matrix \Lambda \Lambda^T + \psi, see the compute_wood_chol_and_logdet(...) function for more details.

The unconstrained representation for this state is not implemented.

Public Functions

inline virtual void set_from_proto(const ProtoState &state_, bool update_card) override

Sets the current state from a protobuf object

Parameters:
  • state_ – a bayesmix::AlgorithmState::ClusterState instance

  • update_card – if true, the current cardinality is updated

inline void compute_wood_factors()

Sets cov_logdet and cov_wood by calling bayesmix::compute_wood_chol_and_logdet()

inline virtual ProtoState get_as_proto() const override

Returns a bayesmix::AlgorithmState::ClusterState representing the current value of the state