1 #ifndef __STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__DISCRETE__MULTINOMIAL_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__DISCRETE__MULTINOMIAL_HPP__
4 #include <boost/math/special_functions/gamma.hpp>
5 #include <boost/random/uniform_01.hpp>
6 #include <boost/random/variate_generator.hpp>
22 template <
bool propto,
25 typename boost::math::tools::promote_args<T_prob>::type
27 const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta,
29 static const char*
function =
"stan::prob::multinomial_log(%1%)";
34 using boost::math::tools::promote_args;
37 typename promote_args<T_prob>::type lp(0.0);
44 ns.size(),
"Size of number of trials variable",
45 theta.rows(),
"rows of probabilities parameter",
52 for (
unsigned int i = 0; i < ns.size(); ++i)
55 for (
unsigned int i = 0; i < ns.size(); ++i)
59 for (
unsigned int i = 0; i < ns.size(); ++i)
65 template <
bool propto,
67 typename boost::math::tools::promote_args<T_prob>::type
69 const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta) {
73 template <
typename T_prob,
75 typename boost::math::tools::promote_args<T_prob>::type
77 const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta,
79 return multinomial_log<false>(ns,theta,Policy());
82 template <
typename T_prob>
83 typename boost::math::tools::promote_args<T_prob>::type
85 const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta) {
90 inline std::vector<int>
94 std::vector<int> result(N,0);
95 double mass_left = 1.0;
97 for (
int k = 0; n_left > 0 && k < theta.size(); ++k) {
98 result[k] =
binomial_rng(n_left,theta[k] / mass_left,rng);
100 mass_left -= theta[k];