bnmf-algs
seq_greedy_bld.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "defs.hpp"
4 #include "util/sampling.hpp"
5 #include "util_details.hpp"
6 
7 namespace bnmf_algs {
8 namespace bld {
54 template <typename T, typename Scalar>
56  const alloc_model::Params<Scalar>& model_params) {
57  details::check_bld_params(X, z, model_params);
58 
59  long x = X.rows();
60  long y = X.cols();
61 
62  // exit early if sum is 0
63  if (std::abs(X.sum()) <= std::numeric_limits<double>::epsilon()) {
64  tensor_t<T, 3> S(x, y, z);
65  S.setZero();
66  return S;
67  }
68 
69  // variables to update during the iterations
70  tensor_t<T, 3> S(x, y, z);
71  S.setZero();
72  matrix_t<T> S_ipk = matrix_t<T>::Zero(x, z); // S_{i+k}
73  vector_t<T> S_ppk = vector_t<T>::Zero(z); // S_{++k}
74  matrix_t<T> S_pjk = matrix_t<T>::Zero(y, z); // S_{+jk}
75  const Scalar sum_alpha = std::accumulate(model_params.alpha.begin(),
76  model_params.alpha.end(), 0.0);
77 
78  // sample X matrix one by one
79  auto matrix_sampler = util::sample_ones_noreplace(X);
80  int ii, jj;
81  for (const auto& sample : matrix_sampler) {
82  std::tie(ii, jj) = sample;
83 
84  // find the bin that increases log marginal the largest when it is
85  // incremented
86  size_t kmax = 0;
88  for (size_t k = 0; k < z; ++k) {
89  T log_marginal_change =
90  std::log(model_params.alpha[ii] + S_ipk(ii, k)) -
91  std::log(sum_alpha + S_ppk(k)) - std::log(1 + S(ii, jj, k)) +
92  std::log(model_params.beta[k] + S_pjk(jj, k));
93 
94  if (log_marginal_change > curr_max) {
95  curr_max = log_marginal_change;
96  kmax = k;
97  }
98  }
99 
100  // increase the corresponding bin accumulators
101  ++S(ii, jj, kmax);
102  ++S_ipk(ii, kmax);
103  ++S_ppk(kmax);
104  ++S_pjk(jj, kmax);
105  }
106 
107  return S;
108 }
109 } // namespace bld
110 } // namespace bnmf_algs
Structure to hold the parameters for the Allocation Model .
Definition: alloc_model_params.hpp:25
T tie(T...args)
util::Generator< std::pair< int, int >, details::SampleOnesNoReplaceComputer< T > > sample_ones_noreplace(const matrix_t< T > &X)
Return a bnmf_algs::util::Generator that will generate a sequence of samples from the given matrix wi...
Definition: sampling.hpp:526
Eigen::Tensor< Scalar, N, Eigen::RowMajor > tensor_t
Tensor type used in the computations.
Definition: defs.hpp:52
T log(T...args)
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > matrix_t
Matrix type used in the computations.
Definition: defs.hpp:41
T end(T...args)
T lowest(T...args)
std::vector< Scalar > alpha
Parameter vector of Dirichlet prior for matrix of size .
Definition: alloc_model_params.hpp:37
tensor_t< T, 3 > seq_greedy_bld(const matrix_t< T > &X, size_t z, const alloc_model::Params< Scalar > &model_params)
Compute tensor , the solution of BLD problem , from matrix using sequential greedy method...
Definition: seq_greedy_bld.hpp:55
void check_bld_params(const matrix_t< T > &X, size_t z, const alloc_model::Params< Scalar > &model_params)
Do parameter checks on BLD computing function parameters and throw an assertion error if the paramete...
Definition: util_details.hpp:20
T begin(T...args)
T accumulate(T...args)
std::vector< Scalar > beta
Parameter vector of Dirichlet prior for matrix of size .
Definition: alloc_model_params.hpp:42
Eigen::Matrix< Scalar, 1, Eigen::Dynamic, Eigen::RowMajor > vector_t
Vector type used in the computations.
Definition: defs.hpp:27
Main namespace for bnmf-algs library.
Definition: alloc_model_funcs.hpp:12