Stan Math Library  2.12.0
reverse mode automatic differentiation
categorical_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_RNG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_RNG_HPP
3 
4 #include <boost/random/uniform_01.hpp>
5 #include <boost/random/variate_generator.hpp>
13 
14 namespace stan {
15  namespace math {
16 
17  template <class RNG>
18  inline int
19  categorical_rng(const Eigen::Matrix<double, Eigen::Dynamic, 1>& theta,
20  RNG& rng) {
21  using boost::variate_generator;
22  using boost::uniform_01;
23 
24  static const char* function("categorical_rng");
25 
26  check_simplex(function, "Probabilities parameter", theta);
27 
28  variate_generator<RNG&, uniform_01<> >
29  uniform01_rng(rng, uniform_01<>());
30 
31  Eigen::VectorXd index(theta.rows());
32  index.setZero();
33 
34  for (int i = 0; i < theta.rows(); i++) {
35  for (int j = i; j < theta.rows(); j++)
36  index(j) += theta(i, 0);
37  }
38 
39  double c = uniform01_rng();
40  int b = 0;
41  while (c > index(b, 0))
42  b++;
43  return b + 1;
44  }
45  }
46 }
47 #endif
int categorical_rng(const Eigen::Matrix< double, Eigen::Dynamic, 1 > &theta, RNG &rng)
bool check_simplex(const char *function, const char *name, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta)
Return true if the specified vector is simplex.

     [ Stan Home Page ] © 2011–2016, Stan Development Team.