bayesmix/hierarchies/prior_models
Prior Models¶
A PriorModel represents the prior for the parameters in the likelihood, i.e.

with
being a suitable prior on the parameters space. We also allow for more flexible priors adding further level of randomness (i.e. the hyperprior) on the parameter characterizing 
Main operations performed¶
A likelihood object must be able to perform the following operations:
a. First of all, lpdf() and lpdf_from_unconstrained() methods evaluate the log-prior density function at the current state
or its unconstrained representation.
In particular, lpdf_from_unconstrained() is needed by Metropolis-like updaters.
b. The sample() method generates a draw from the prior distribution. If hier_hypers is nullptr, the prior hyperparameter values are used.
To allow sampling from the full conditional distribution in case of semi-congugate hierarchies, we introduce the ``hier_hypers parameter, which is a pointer to a Protobuf message storing the hierarchy hyperaprameters to use for the sampling.
The
update_hypers()method updates the prior hyperparameters, given the vector of all cluster states.
Code structure¶
As for the Likelihood classes we employ the Curiously Recurring Template Pattern to manage the polymorphic nature of PriorModel classes.
The class AbstractPriorModel defines the API, i.e. all the methods that need to be called from outside of a PrioModel class.
A template class BasePriorModel inherits from AbstractPriorModel and implements some of the necessary virtual methods, which need not be implemented by the child classes.
Instead, child classes must implement:
lpdf: evaluates
sample: samples from
given a hyperparameters (passed as a pointer). If hier_hypersisnullptr, the prior hyperparameter values are used.set_hypers_from_proto: sets the hyperparameters from aProbufmessageget_hypers_proto: returns the hyperparameters as aProbufmessageinitialize_hypers: provides a default initialization of hyperparameters
In case you want to use a Metropolis-like updater, child classes should also implement:
lpdf_from_unconstrained: evaluates
, where
is the vector of unconstrained parameters.
Abstract Classes¶
-
class AbstractPriorModel¶
Abstract class for a generic prior model
This class is the basis for a curiously recurring template pattern (CRTP) for
PriorModelobjects, ad it is solely composed of interface functions for derived classes to use.A prior model represents the prior for the parameters in the likelihood. Hence, it can evaluate the log probability density function (lpdf) for a given parameter state.
We also store a pointer to the protobuf object that represents the type of prior used for the parameters in the likelihood.
Subclassed by BasePriorModel< NxIGPriorModel, State::UniLS, Hyperparams::NxIG, bayesmix::NNxIGPrior >, BasePriorModel< NIGPriorModel, State::UniLS, Hyperparams::NIG, bayesmix::NNIGPrior >, BasePriorModel< FAPriorModel, State::FA, Hyperparams::FA, bayesmix::FAPrior >, BasePriorModel< MNIGPriorModel, State::UniLinRegLS, Hyperparams::MNIG, bayesmix::LinRegUniPrior >, BasePriorModel< NWPriorModel, State::MultiLS, Hyperparams::NW, bayesmix::NNWPrior >, BasePriorModel< Derived, State, HyperParams, Prior >
Public Functions
-
virtual ~AbstractPriorModel() = default¶
Default destructor.
-
virtual std::shared_ptr<AbstractPriorModel> clone() const = 0¶
Returns an independent, data-less copy of this object.
-
virtual std::shared_ptr<AbstractPriorModel> deep_clone() const = 0¶
Returns an independent, data-less deep copy of this object.
-
virtual double lpdf(const google::protobuf::Message &state_) = 0¶
Evaluates the log likelihood for the prior model, given the state of the cluster
- Parameters:
state_ – A Protobuf message storing the current state of the cluster
- Returns:
The evaluation of the log likelihood
-
inline virtual double lpdf_from_unconstrained(Eigen::VectorXd unconstrained_params) const¶
Evaluates the log likelihood for unconstrained parameter values. By unconstrained parameters we mean that each entry of the parameter vector can range over (-inf, inf). Usually, some kind of transformation is required from the unconstrained parameterization to the actual parameterization.
- Parameters:
unconstrained_params – vector collecting the unconstrained parameters
- Returns:
The evaluation of the log likelihood of the prior model
-
inline virtual stan::math::var lpdf_from_unconstrained(Eigen::Matrix<stan::math::var, Eigen::Dynamic, 1> unconstrained_params) const¶
Evaluates the log likelihood for unconstrained parameter values. By unconstrained parameters we mean that each entry of the parameter vector can range over (-inf, inf). Usually, some kind of transformation is required from the unconstrained parameterization to the actual parameterization. This version using
stan::math::vartype is required for Stan automatic aifferentiation.- Parameters:
unconstrained_params – vector collecting the unconstrained parameters
- Returns:
The evaluation of the log likelihood of the prior model
-
virtual std::shared_ptr<google::protobuf::Message> sample_proto(ProtoHypersPtr hier_hypers = nullptr) = 0¶
Sampling from the prior model
- Parameters:
hier_hypers – A pointer to a
bayesmix::AlgorithmState::hierarchyHypersobject, which defines the parameters to use for the sampling. The default behaviour (i.e.hier_hypers = nullptr) uses prior hyperparameters- Returns:
A Protobuf message storing the state sampled from the prior model
-
virtual void update_hypers(const std::vector<bayesmix::AlgorithmState::ClusterState> &states) = 0¶
Updates hyperparameter values given a vector of cluster states.
-
virtual google::protobuf::Message *get_mutable_prior() = 0¶
Returns a pointer to the Protobuf message of the prior of this cluster.
-
virtual void set_hypers_from_proto(const google::protobuf::Message &state_) = 0¶
Read and set hyperparameter values from a given Protobuf message.
-
virtual void write_hypers_to_proto(google::protobuf::Message *out) const = 0¶
Writes current values of the hyperparameters to a Protobuf message by pointer
-
virtual std::shared_ptr<bayesmix::AlgorithmState::HierarchyHypers> get_hypers_proto() const = 0¶
Writes current value of hyperparameters to a Protobuf message and return a shared_ptr. New hierarchies have to first modify the field ‘oneof val’ in the AlgoritmState::HierarchyHypers message by adding the appropriate type
-
virtual ~AbstractPriorModel() = default¶
-
template<class Derived, class State, typename HyperParams, typename Prior>
class BasePriorModel : public AbstractPriorModel¶ Base template class of a
PriorModelobjectThis class derives from
AbstractPriorModeland is templated overDerived(needed for the curiously recurring template pattern),State(an instance ofBasestate),HyperParams(a struct representing the hyperparameters, seehyperparams.h) andPrior: a protobuf message representing the type of prior imposed on the hyperparameters.- Template Parameters:
Derived – Name of the implemented derived class
State – Class name of the container for state values
HyperParams – Class name of the container for hyperparameters
Prior – Class name of the protobuf message for the prior on the hyperparameters.
Public Functions
-
BasePriorModel() = default¶
Default constructor.
-
~BasePriorModel() = default¶
Default destructor.
-
inline virtual double lpdf_from_unconstrained(Eigen::VectorXd unconstrained_params) const override¶
Evaluates the log likelihood for unconstrained parameter values. By unconstrained parameters we mean that each entry of the parameter vector can range over (-inf, inf). Usually, some kind of transformation is required from the unconstrained parameterization to the actual parameterization.
- Parameters:
unconstrained_params – vector collecting the unconstrained parameters
- Returns:
The evaluation of the log likelihood of the prior model
-
inline virtual stan::math::var lpdf_from_unconstrained(Eigen::Matrix<stan::math::var, Eigen::Dynamic, 1> unconstrained_params) const override¶
This version using
stan::math::vartype is required for Stan automatic aifferentiation. Evaluates the log likelihood for unconstrained parameter values. By unconstrained parameters we mean that each entry of the parameter vector can range over (-inf, inf). Usually, some kind of transformation is required from the unconstrained parameterization to the actual parameterization.- Parameters:
unconstrained_params – vector collecting the unconstrained parameters
- Returns:
The evaluation of the log likelihood of the prior model
-
inline virtual std::shared_ptr<google::protobuf::Message> sample_proto(ProtoHypersPtr hier_hypers = nullptr) override¶
Sampling from the prior model
- Parameters:
hier_hypers – A pointer to a
bayesmix::AlgorithmState::hierarchyHypersobject, which defines the parameters to use for the sampling. The default behaviour (i.e.hier_hypers = nullptr) uses prior hyperparameters- Returns:
A Protobuf message storing the state sampled from the prior model
-
virtual std::shared_ptr<AbstractPriorModel> clone() const override¶
Returns an independent, data-less copy of this object.
-
virtual std::shared_ptr<AbstractPriorModel> deep_clone() const override¶
Returns an independent, data-less deep copy of this object.
-
virtual google::protobuf::Message *get_mutable_prior() override¶
Returns a pointer to the Protobuf message of the prior of this cluster.
-
inline HyperParams get_hypers() const¶
Returns the struct of the current prior hyperparameters.
-
virtual void write_hypers_to_proto(google::protobuf::Message *out) const override¶
Writes current values of the hyperparameters to a Protobuf message by pointer
-
void initialize()¶
Initializes the prior model (both prior and hyperparameters)
Non-abstract Classes¶
-
class NIGPriorModel : public BasePriorModel<NIGPriorModel, State::UniLS, Hyperparams::NIG, bayesmix::NNIGPrior>¶
A conjugate prior model for the univariate normal likelihood, that is

With several possibilies for hyper-priors on
and
. We have considered a normal prior for
and a Normal-Gamma-Gamma for
in addition to fixing prior hyperparameters Public Functions
-
virtual double lpdf(const google::protobuf::Message &state_) override¶
Evaluates the log likelihood for the prior model, given the state of the cluster
- Parameters:
state_ – A Protobuf message storing the current state of the cluster
- Returns:
The evaluation of the log likelihood
-
virtual void update_hypers(const std::vector<bayesmix::AlgorithmState::ClusterState> &states) override¶
Updates hyperparameter values given a vector of cluster states.
-
virtual void set_hypers_from_proto(const google::protobuf::Message &hypers_) override¶
Read and set hyperparameter values from a given Protobuf message.
-
virtual std::shared_ptr<bayesmix::AlgorithmState::HierarchyHypers> get_hypers_proto() const override¶
Writes current value of hyperparameters to a Protobuf message and return a shared_ptr. New hierarchies have to first modify the field ‘oneof val’ in the AlgoritmState::HierarchyHypers message by adding the appropriate type
Protected Functions
-
virtual void initialize_hypers() override¶
Initializes hierarchy hyperparameters to appropriate values.
-
virtual double lpdf(const google::protobuf::Message &state_) override¶
-
class NxIGPriorModel : public BasePriorModel<NxIGPriorModel, State::UniLS, Hyperparams::NxIG, bayesmix::NNxIGPrior>¶
A semi-conjugate prior model for the univariate normal likelihood, that is

Public Functions
-
virtual double lpdf(const google::protobuf::Message &state_) override¶
Evaluates the log likelihood for the prior model, given the state of the cluster
- Parameters:
state_ – A Protobuf message storing the current state of the cluster
- Returns:
The evaluation of the log likelihood
-
virtual void update_hypers(const std::vector<bayesmix::AlgorithmState::ClusterState> &states) override¶
Updates hyperparameter values given a vector of cluster states.
-
virtual void set_hypers_from_proto(const google::protobuf::Message &hypers_) override¶
Read and set hyperparameter values from a given Protobuf message.
-
virtual std::shared_ptr<bayesmix::AlgorithmState::HierarchyHypers> get_hypers_proto() const override¶
Writes current value of hyperparameters to a Protobuf message and return a shared_ptr. New hierarchies have to first modify the field ‘oneof val’ in the AlgoritmState::HierarchyHypers message by adding the appropriate type
Protected Functions
-
virtual void initialize_hypers() override¶
Initializes hierarchy hyperparameters to appropriate values.
-
virtual double lpdf(const google::protobuf::Message &state_) override¶
-
class NWPriorModel : public BasePriorModel<NWPriorModel, State::MultiLS, Hyperparams::NW, bayesmix::NNWPrior>¶
A conjugate prior model for the multivariate normal likelihood, that is

With some options for hyper-priors on
and
. We have considered a normal prior for
in addition to fixing prior hyperparameters Public Functions
-
virtual double lpdf(const google::protobuf::Message &state_) override¶
Evaluates the log likelihood for the prior model, given the state of the cluster
- Parameters:
state_ – A Protobuf message storing the current state of the cluster
- Returns:
The evaluation of the log likelihood
-
virtual void update_hypers(const std::vector<bayesmix::AlgorithmState::ClusterState> &states) override¶
Updates hyperparameter values given a vector of cluster states.
-
virtual void set_hypers_from_proto(const google::protobuf::Message &hypers_) override¶
Read and set hyperparameter values from a given Protobuf message.
-
virtual std::shared_ptr<bayesmix::AlgorithmState::HierarchyHypers> get_hypers_proto() const override¶
Writes current value of hyperparameters to a Protobuf message and return a shared_ptr. New hierarchies have to first modify the field ‘oneof val’ in the AlgoritmState::HierarchyHypers message by adding the appropriate type
Protected Functions
-
virtual void initialize_hypers() override¶
Initializes hierarchy hyperparameters to appropriate values.
-
virtual double lpdf(const google::protobuf::Message &state_) override¶
-
class MNIGPriorModel : public BasePriorModel<MNIGPriorModel, State::UniLinRegLS, Hyperparams::MNIG, bayesmix::LinRegUniPrior>¶
A conjugate prior model for the scalar linear regression likelihood, i.e.

Public Functions
-
virtual double lpdf(const google::protobuf::Message &state_) override¶
Evaluates the log likelihood for the prior model, given the state of the cluster
- Parameters:
state_ – A Protobuf message storing the current state of the cluster
- Returns:
The evaluation of the log likelihood
-
virtual void update_hypers(const std::vector<bayesmix::AlgorithmState::ClusterState> &states) override¶
Updates hyperparameter values given a vector of cluster states.
-
virtual void set_hypers_from_proto(const google::protobuf::Message &hypers_) override¶
Read and set hyperparameter values from a given Protobuf message.
-
virtual std::shared_ptr<bayesmix::AlgorithmState::HierarchyHypers> get_hypers_proto() const override¶
Writes current value of hyperparameters to a Protobuf message and return a shared_ptr. New hierarchies have to first modify the field ‘oneof val’ in the AlgoritmState::HierarchyHypers message by adding the appropriate type
Protected Functions
-
virtual void initialize_hypers() override¶
Initializes hierarchy hyperparameters to appropriate values.
-
virtual double lpdf(const google::protobuf::Message &state_) override¶
-
class FAPriorModel : public BasePriorModel<FAPriorModel, State::FA, Hyperparams::FA, bayesmix::FAPrior>¶
A priormodel for the factor analyzers likelihood, that is

Where
is the Dirichlet-Laplace distribution. See Bhattacharya A., Pati D., Pillai N.S., Dunson D.B. (2015). JASA 110(512), 1479–1490 for details. Public Functions
-
virtual double lpdf(const google::protobuf::Message &state_) override¶
Evaluates the log likelihood for the prior model, given the state of the cluster
- Parameters:
state_ – A Protobuf message storing the current state of the cluster
- Returns:
The evaluation of the log likelihood
-
virtual void update_hypers(const std::vector<bayesmix::AlgorithmState::ClusterState> &states) override¶
Updates hyperparameter values given a vector of cluster states.
-
virtual void set_hypers_from_proto(const google::protobuf::Message &hypers_) override¶
Read and set hyperparameter values from a given Protobuf message.
-
virtual std::shared_ptr<bayesmix::AlgorithmState::HierarchyHypers> get_hypers_proto() const override¶
Writes current value of hyperparameters to a Protobuf message and return a shared_ptr. New hierarchies have to first modify the field ‘oneof val’ in the AlgoritmState::HierarchyHypers message by adding the appropriate type
Protected Functions
-
virtual void initialize_hypers() override¶
Initializes hierarchy hyperparameters to appropriate values.
-
virtual double lpdf(const google::protobuf::Message &state_) override¶