1 #ifndef __STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__DISCRETE__CATEGORICAL_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__DISCRETE__CATEGORICAL_HPP__
4 #include <boost/random/uniform_01.hpp>
5 #include <boost/random/variate_generator.hpp>
18 template <
bool propto,
21 typename boost::math::tools::promote_args<T_prob>::type
23 const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta,
25 static const char*
function =
"stan::prob::categorical_log(%1%)";
29 using boost::math::tools::promote_args;
36 "Number of categories",
41 Eigen::Matrix<double,Eigen::Dynamic,1> theta_dbl(theta.size());
42 for (
int i = 0; i < theta_dbl.size(); ++i)
45 "Probabilities parameter",
50 "Probabilities parameter",
56 return log(theta(n-1));
60 template <
bool propto,
63 typename boost::math::tools::promote_args<T_prob>::type
65 const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta) {
70 template <
typename T_prob,
73 typename boost::math::tools::promote_args<T_prob>::type
75 const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta,
77 return categorical_log<false>(n,theta,Policy());
80 template <
typename T_prob>
82 typename boost::math::tools::promote_args<T_prob>::type
84 const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta) {
92 using boost::variate_generator;
93 using boost::uniform_01;
94 variate_generator<RNG&, uniform_01<> >
95 uniform01_rng(rng, uniform_01<>());
97 Eigen::VectorXd index(theta.rows());
100 for(
int i = 0; i < theta.rows(); i++) {
101 for(
int j = i; j < theta.rows(); j++)
102 index(j) += theta(i,0);
105 double c = uniform01_rng();
107 while(c > index(b,0))