bayesmix/utils
Utils¶
Collection of miscellaneous auxiliary tools for the library.
Clustering utilities¶
The cluster_utils.h file includes some utilities for cluster estimation. These functions only use Eigen objects.
-
namespace bayesmix¶
Eigen matrix manipulation utilities¶
The eigen_utils.h file implements a few methods to manipulate groups of matrices, mainly by joining different objects, as well as additional utilities for SPD checking and grid creation.
-
namespace bayesmix
Functions
-
Eigen::MatrixXd vstack(const std::vector<Eigen::MatrixXd> &mats)¶
Concatenates a vector of Eigen matrices along the rows
- Parameters:
mats – The matrices to be concatenated
- Throws:
std::invalid – argument if sizes mismatch
- Returns:
The resulting matrix
-
void append_by_row(Eigen::MatrixXd *const a, const Eigen::MatrixXd &b)¶
Concatenates two matrices by row, modifying the first matrix in-place
- Throws:
std::invalid_argument – if sizes mismatch
-
Eigen::MatrixXd append_by_row(const Eigen::MatrixXd &a, const Eigen::MatrixXd &b)¶
Concatenates two matrices by row
- Parameters:
a, b – The matrices to be concatenated
- Throws:
std::invalid_argument – if sizes mismatch
- Returns:
The resulting matrix
-
template<template<typename...> class Container>
Eigen::MatrixXd stack_vectors(const Container<Eigen::VectorXd> &rows)¶ Creates an Eigen matrix from a collection of rows
- Template Parameters:
Container – An std-compatible container implementing
operator[]- Parameters:
rows – The rows of the matrix
- Returns:
The resulting matrix
-
void check_spd(const Eigen::MatrixXd &mat)¶
Checks whether the matrix is symmetric and semi-positive definite.
-
Eigen::MatrixXd get_2d_grid(const double x1, const double x2, const int nx, const double y1, const double y2, const int ny)¶
Creates a 2d grid over rectangle [x1, x2] x [y1, y2], with nx * ny points
- Parameters:
x1, x2, y1, y2 – Bounds for the rectangle
nx, ny – Number of points created along the x, y directions
- Returns:
The resulting grid
-
Eigen::MatrixXd vstack(const std::vector<Eigen::MatrixXd> &mats)¶
Eigen input-output utilities¶
The io_utils.h file implements basic input-output utilities for Eigen matrices from and to text files.
-
namespace bayesmix
Functions
-
bool check_file_is_writeable(const std::string &filename)¶
Checks whether the given file is available for writing.
-
Eigen::MatrixXd read_eigen_matrix(const std::string &filename, const char delim = ',')¶
Returns an Eigen matrix after reading it from a file.
-
void write_matrix_to_file(const Eigen::MatrixXd &mat, const std::string &filename, const char delim = ',')¶
Writes the given Eigen matrix to a text file.
-
bool check_file_is_writeable(const std::string &filename)¶
protobuf input-output utilities¶
The proto_utils.h file implements a few useful functions to manipulate Protobuf objects. For instance, this library implements its own version of vectors and matrices, and the functions implemented here convert from these types to the Eigen ones and viceversa. One can also read a Protobuf from a text file. This is mostly useful for algorithm configuration files.
-
namespace bayesmix
Functions
-
void to_proto(const Eigen::VectorXd &vec, bayesmix::Vector *const out)¶
Writes an Eigen vector to a bayesmix::Vector Protobuf object by pointer.
-
void to_proto(const Eigen::MatrixXd &mat, bayesmix::Matrix *const out)¶
Writes an Eigen matrix to a bayesmix::Matrix Protobuf object by pointer.
-
Eigen::VectorXd to_eigen(const bayesmix::Vector &vec)¶
Converts a bayesmix::Vector Protobuf object into an Eigen vector.
-
Eigen::MatrixXd to_eigen(const bayesmix::Matrix &mat)¶
Converts a bayesmix::Matrix Protobuf object into an Eigen matrix.
-
void read_proto_from_file(const std::string &filename, google::protobuf::Message *const out)¶
Writes from a given file to a Protobuf object via pointer.
-
void to_proto(const Eigen::VectorXd &vec, bayesmix::Vector *const out)¶
RNG wrapper¶
The rng.h file defines a simple Random Number Generation class wrapper. This class wraps the C++ standard RNG object and allows the use of any RNG seed. It is implemented as a singleton, so that every object used in the library has access to the same exact RNG engine. This is needed to ensure that the rng stream is well defined and that every random number generation causes an update in the rng state. The main drawback is that this design does not allow for efficient parallelization, as calls to the Rng::Instance() from different threads could cause data races. A preferred solution would be to define the Rng to be thread-local if omp-parallelism over several cores is desired, see: https://stackoverflow.com/q/64937761
and the ‘wood_factor’, i.e. L^{-1} * Lambda^T * Sigma^{-1}, where L is the (lower) Cholesky factor of I + Lambda^T * Sigma^{-1} * Lambda