bayesmix/hierarchies/likelihoods/states
States¶
States are classes used to store parameters
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
and prior
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()andto_proto(), that are used to deserialize from (and serialize to) abayesmix::AlgorithmState::ClusterStateprotocol buffer message.Optionally, each state can have an “unconstrained” representation, where a bijective transformation
is applied to
, so that the image of
is in
for some
. This is essential for the default updaters such as RandomWalkUpdaterandMalaUpdaterto work, but is not necessary for other model-specific updaters. If such a representation is needed, child classes should also implementget_unconstrained(),set_from_unconstrained(), andlog_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
.
-
inline virtual void set_from_unconstrained(const Eigen::VectorXd &in)¶
Sets the current state as
- Parameters:
in – the unconstrained representation of the state
-
inline virtual double log_det_jac() const¶
Returns the log determinant of the jacobian of
.
-
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::ClusterStaterepresenting the current value of the state
-
inline std::shared_ptr<ProtoState> to_proto() const¶
Returns a shared pointer to
bayesmix::AlgorithmState::ClusterStaterepresenting the current value of the state
-
inline virtual Eigen::VectorXd get_unconstrained() const¶
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
.
-
inline virtual void set_from_unconstrained(const Eigen::VectorXd &in) override¶
Sets the current state as
- 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::ClusterStaterepresenting the current value of the state
-
inline virtual double log_det_jac() const override¶
Returns the log determinant of the jacobian of
.
-
inline virtual Eigen::VectorXd get_unconstrained() const override¶
-
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
.
-
inline virtual void set_from_unconstrained(const Eigen::VectorXd &in) override¶
Sets the current state as
- 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::ClusterStaterepresenting the current value of the state
-
inline virtual double log_det_jac() const override¶
Returns the log determinant of the jacobian of
.
-
inline virtual Eigen::VectorXd get_unconstrained() const override¶
-
class FA : public State::BaseState¶
State of a Factor Analytic model

where
is a
-dimensional vetor,
is a
-dimensional one,
is a
matrix and
is an error term with mean zero and diagonal covariance matrix
.For faster likelihood evaluation, we store also the
cov_woodfactor and the log determinant of the matrix
, 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::ClusterStaterepresenting the current value of the state
-
inline virtual void set_from_proto(const ProtoState &state_, bool update_card) override¶