bnmf-algs
Functions
bnmf_algs::details::online_EM Namespace Reference

Namespace containing online_EM algorithm related functions. More...

Functions

template<typename T >
size_t init_nan_values (matrix_t< T > &X)
 Initialize all NaN values in the given matrix with the mean of the remaining values that are different than NaN. More...
 
template<typename Scalar >
std::pair< matrix_t< Scalar >, matrix_t< Scalar > > init_alpha_beta (const std::vector< alloc_model::Params< Scalar >> &param_vec, size_t y)
 Initialize each entry of alpha and beta matrices with the given model parameters. More...
 
template<typename T >
std::tuple< matrix_t< T >, matrix_t< T >, vector_t< T > > init_S_xx (const matrix_t< T > &X_full, size_t z, const std::vector< size_t > &ii, const std::vector< size_t > &jj)
 Initialize S_pjk, S_ipk matrices and S_ppk vector from a Dirichlet distribution with all parameters equal to 1. More...
 
template<typename T , typename Scalar , typename PsiFunction >
void update_logW (const matrix_t< Scalar > &alpha, const matrix_t< T > &S_ipk, const vector_t< Scalar > &alpha_pk, const vector_t< T > &S_ppk, const PsiFunction &psi_fn, matrix_t< double > &logW)
 Perform an update on logW matrix. More...
 
template<typename T , typename Scalar , typename PsiFunction >
void update_logH (const matrix_t< Scalar > &beta, const matrix_t< T > &S_pjk, const Scalar b, const PsiFunction &psi_fn, matrix_t< double > &logH)
 Perform an update on logW matrix. More...
 
template<typename T >
std::tuple< std::vector< size_t >, std::vector< size_t >, std::vector< T > > find_nonzero (const matrix_t< T > &X)
 Find nonzero entries and their indices in the given matrix and return indices and values as vectors. More...
 
template<typename T >
double update_allocation (const std::vector< size_t > &ii, const std::vector< size_t > &jj, const std::vector< T > &xx, bld::EMResult< T > &res, vector_t< T > &S_ppk)
 Update the current allocation by performing a maximization step for each nonzero entry of the original input matrix. More...
 
template<typename T , typename Scalar >
double delta_log_PS (const matrix_t< Scalar > &alpha, const matrix_t< Scalar > &beta, const matrix_t< T > &S_ipk, const matrix_t< T > &S_pjk, const vector_t< Scalar > &alpha_pk, const vector_t< T > &S_ppk, Scalar b)
 Compute the difference in log_PS value computed using the given model variables. More...
 

Detailed Description

Namespace containing online_EM algorithm related functions.

Function Documentation

template<typename T , typename Scalar >
double bnmf_algs::details::online_EM::delta_log_PS ( const matrix_t< Scalar > &  alpha,
const matrix_t< Scalar > &  beta,
const matrix_t< T > &  S_ipk,
const matrix_t< T > &  S_pjk,
const vector_t< Scalar > &  alpha_pk,
const vector_t< T > &  S_ppk,
Scalar  b 
)

Compute the difference in log_PS value computed using the given model variables.

Template Parameters
TType of the matrix entries.
ScalarType of the model parameters.
Parameters
alphaalpha matrix containing alpha model parameters.
betabeta matrix containing beta model parameters.
S_ipkSum of S tensor along its 2nd axis.
S_pjkSum of S tensor along its 1st axis.
alpha_pkColumn sums of alpha matrix.
S_ppkSum of S tensor along its 1st and 2nd dimensions.
bb model parameter
Returns
Difference in log_PS value.
template<typename T >
std::tuple<std::vector<size_t>, std::vector<size_t>, std::vector<T> > bnmf_algs::details::online_EM::find_nonzero ( const matrix_t< T > &  X)

Find nonzero entries and their indices in the given matrix and return indices and values as vectors.

Template Parameters
TType of the input matrix.
Parameters
XInput matrix.
Returns
std::tuple of vectors ii, jj, xx where ii contains row indices, jj contains column indices of the nonzero entries and xx contains the corresponding nonzero entries.
Remarks
If an entry is NaN, it is assumed to be nonzero.
template<typename Scalar >
std::pair<matrix_t<Scalar>, matrix_t<Scalar> > bnmf_algs::details::online_EM::init_alpha_beta ( const std::vector< alloc_model::Params< Scalar >> &  param_vec,
size_t  y 
)

Initialize each entry of alpha and beta matrices with the given model parameters.

The returned alpha matrix is of shape \(x \times z\) and beta matrix is of shape \(z \times y\).

Template Parameters
ScalarType of the model parameters.
Parameters
param_vecVector containing an alloc_model::Params object for every hidden dimension.
yColumns of beta matrix.
Returns
alpha and beta matrices initialized with the given parameter values.
template<typename T >
size_t bnmf_algs::details::online_EM::init_nan_values ( matrix_t< T > &  X)

Initialize all NaN values in the given matrix with the mean of the remaining values that are different than NaN.

Template Parameters
TType of the matrix entries.
Parameters
XInput matrix containing NaN values.
Returns
Modified matrix containing the mean of non-NaN values in place of every previously NaN value.
template<typename T >
std::tuple<matrix_t<T>, matrix_t<T>, vector_t<T> > bnmf_algs::details::online_EM::init_S_xx ( const matrix_t< T > &  X_full,
size_t  z,
const std::vector< size_t > &  ii,
const std::vector< size_t > &  jj 
)

Initialize S_pjk, S_ipk matrices and S_ppk vector from a Dirichlet distribution with all parameters equal to 1.

For every nonzero value \(X_{ij}\) in the input matrix X, this function samples from Dirichlet distribution with \(z\) many 1's as the parameter vector and adds the resulting sample to \(i^{th}\) row of S_ipk matrix, \(j^{th}\) row of S_pjk matrix and to S_ppk vector.

Template Parameters
TType of the matrix entries.
Parameters
X_fullInput matrix X not containing any NaN values (NaNs must be initialized to some value).
zRank of the decomposition (hidden layer count).
iiRow indices of every nonzero value in the original X input matrix.
jjColumn indices of every nonzero value in the original X input matrix.
Returns
std::tuple of S_pjk, S_ipk and S_ppk.
template<typename T >
double bnmf_algs::details::online_EM::update_allocation ( const std::vector< size_t > &  ii,
const std::vector< size_t > &  jj,
const std::vector< T > &  xx,
bld::EMResult< T > &  res,
vector_t< T > &  S_ppk 
)

Update the current allocation by performing a maximization step for each nonzero entry of the original input matrix.

Template Parameters
TType of the input matrix.
Parameters
iiRow indices of the original nonzero entries.
jjColumn indices of the original nonzero entries.
xxCorresponding original nonzero entries.
resEMResult object containing the results computed up until now. Matrices and sums given in this EMResult object will be modified with the computed values.
S_ppkS_ppk vector that will be modified in-place.
Returns
Difference in log_PS value after the update is completed.
template<typename T , typename Scalar , typename PsiFunction >
void bnmf_algs::details::online_EM::update_logH ( const matrix_t< Scalar > &  beta,
const matrix_t< T > &  S_pjk,
const Scalar  b,
const PsiFunction &  psi_fn,
matrix_t< double > &  logH 
)

Perform an update on logW matrix.

Template Parameters
TType of the input matrix.
ScalarType of the model parameters.
PsiFunctionFunction/functor to compute psi (digamma) of a value.
Parameters
betabeta matrix returned by init_alpha_beta.
S_pjkSum of \(S\) along its first dim, i.e. \(S_{+jk}\).
bb model parameter passed to EM algorithm. (rate parameter of Gamma distribution).
psi_fnPsi function to use.
logHlogH matrix to update in-place.
template<typename T , typename Scalar , typename PsiFunction >
void bnmf_algs::details::online_EM::update_logW ( const matrix_t< Scalar > &  alpha,
const matrix_t< T > &  S_ipk,
const vector_t< Scalar > &  alpha_pk,
const vector_t< T > &  S_ppk,
const PsiFunction &  psi_fn,
matrix_t< double > &  logW 
)

Perform an update on logW matrix.

Template Parameters
TType of the input matrix.
ScalarType of the model parameters.
PsiFunctionFunction/functor to compute psi (digamma) of a value.
Parameters
alphaalpha matrix returned by init_alpha_beta.
S_ipkSum of \(S\) tensor along its second dim, i.e. \(S_{i+k}\).
alpha_pkVector containing column sums of alpha matrix.
S_ppkSum of \(S\) tensor along its first 2 dimensions, i.e. \(S_{++k}\).
psi_fnPsi function to use.
logWlogW matrix to update in-place.