| Stan Math Library 2.6.3 probability, sampling & optimization

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
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>
6 #include <stan/math/prim/mat/err/check_simplex.hpp>
7 #include <stan/math/prim/scal/err/check_bounded.hpp>
8 #include <stan/math/prim/scal/fun/value_of.hpp>
9 #include <stan/math/prim/mat/fun/sum.hpp>
10 #include <stan/math/prim/mat/meta/index_type.hpp>
11 #include <stan/math/prim/scal/fun/constants.hpp>
12 #include <stan/math/prim/scal/meta/include_summand.hpp>
13
14 namespace stan {
15
16 namespace math {
17
18 template <class RNG>
19 inline int
20 categorical_rng(const Eigen::Matrix<double, Eigen::Dynamic, 1>& theta,
21 RNG& rng) {
22 using boost::variate_generator;
23 using boost::uniform_01;
24 using stan::math::check_simplex;
25
26 static const char* function("stan::math::categorical_rng");
27
28 check_simplex(function, "Probabilities parameter", theta);
29
30 variate_generator<RNG&, uniform_01<> >
31 uniform01_rng(rng, uniform_01<>());
32
33 Eigen::VectorXd index(theta.rows());
34 index.setZero();
35
36 for (int i = 0; i < theta.rows(); i++) {
37 for (int j = i; j < theta.rows(); j++)
38 index(j) += theta(i, 0);
39 }
40
41 double c = uniform01_rng();
42 int b = 0;
43 while (c > index(b, 0))
44 b++;
45 return b + 1;
46 }
47 }
48 }
49 #endif
check_simplex.hpp
include_summand.hpp
stan::math::categorical_rng
int categorical_rng(const Eigen::Matrix< double, Eigen::Dynamic, 1 > &theta, RNG &rng)
Definition: categorical_rng.hpp:20
index_type.hpp
check_bounded.hpp
constants.hpp
sum.hpp
stan::math::check_simplex
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.
Definition: check_simplex.hpp:41
value_of.hpp

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