Stan Math Library: stan/math/prim/mat/prob/multinomial_rng.hpp Source File

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

multinomial_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_MULTINOMIAL_RNG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_MULTINOMIAL_RNG_HPP
3
4 #include <stan/math/prim/mat/err/check_simplex.hpp>
5 #include <stan/math/prim/scal/err/check_size_match.hpp>
6 #include <stan/math/prim/scal/err/check_nonnegative.hpp>
7 #include <stan/math/prim/scal/err/check_positive.hpp>
8 #include <stan/math/prim/scal/fun/multiply_log.hpp>
9 #include <stan/math/prim/scal/fun/constants.hpp>
10 #include <stan/math/prim/scal/prob/binomial_rng.hpp>
11 #include <stan/math/prim/scal/meta/include_summand.hpp>
12 #include <boost/math/special_functions/gamma.hpp>
13 #include <boost/random/uniform_01.hpp>
14 #include <boost/random/variate_generator.hpp>
15 #include <vector>
16
17 namespace stan {
18
19 namespace math {
20
21 template <class RNG>
22 inline std::vector<int>
23 multinomial_rng(const Eigen::Matrix<double, Eigen::Dynamic, 1>& theta,
24 const int N,
25 RNG& rng) {
26 static const char* function("stan::math::multinomial_rng");
27 using stan::math::check_simplex;
28 using stan::math::check_positive;
29
30 check_simplex(function, "Probabilites parameter", theta);
31 check_positive(function, "number of trials variables", N);
32
33 std::vector<int> result(theta.size(), 0);
34 double mass_left = 1.0;
35 int n_left = N;
36 for (int k = 0; n_left > 0 && k < theta.size(); ++k) {
37 double p = theta[k] / mass_left;
38 if (p > 1.0) p = 1.0;
39 result[k] = binomial_rng(n_left, p, rng);
40 n_left -= result[k];
41 mass_left -= theta[k];
42 }
43 return result;
44 }
45
46
47 }
48 }
49 #endif
check_simplex.hpp
check_size_match.hpp
check_positive.hpp
stan
Definition: log_sum_exp.hpp:8
include_summand.hpp
multiply_log.hpp
check_nonnegative.hpp
stan::math::check_positive
bool check_positive(const char *function, const char *name, const T_y &y)
Return true if y is positive.
Definition: check_positive.hpp:68
stan::math::multinomial_rng
std::vector< int > multinomial_rng(const Eigen::Matrix< double, Eigen::Dynamic, 1 > &theta, const int N, RNG &rng)
Definition: multinomial_rng.hpp:23
constants.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
stan::math::binomial_rng
int binomial_rng(const int N, const double theta, RNG &rng)
Definition: binomial_rng.hpp:30
binomial_rng.hpp

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