1 #ifndef STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_LOG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_LOG_HPP
4 #include <boost/random/uniform_01.hpp>
5 #include <boost/random/variate_generator.hpp>
22 template <
bool propto,
24 typename boost::math::tools::promote_args<T_prob>::type
26 const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
27 static const char*
function(
"stan::math::categorical_log");
31 using boost::math::tools::promote_args;
38 check_bounded(
function,
"Number of categories", n, lb, theta.size());
41 if (!
check_simplex(
function,
"Probabilities parameter", theta))
44 if (!
check_simplex(
function,
"Probabilities parameter", theta))
49 return log(theta(n-1));
53 template <
typename T_prob>
55 typename boost::math::tools::promote_args<T_prob>::type
58 Eigen::Dynamic, 1> >::type n,
59 const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
60 return categorical_log<false>(n, theta);
65 template <
bool propto,
67 typename boost::math::tools::promote_args<T_prob>::type
69 const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
70 static const char*
function(
"stan::math::categorical_log");
72 using boost::math::tools::promote_args;
82 for (
size_t i = 0; i < ns.size(); ++i)
87 if (!
check_simplex(
function,
"Probabilities parameter", theta))
90 if (!
check_simplex(
function,
"Probabilities parameter", theta))
100 Eigen::Matrix<T_prob, Eigen::Dynamic, 1> log_theta(theta.size());
101 for (
int i = 0; i < theta.size(); ++i)
102 log_theta(i) =
log(theta(i));
104 Eigen::Matrix<typename boost::math::tools::promote_args<T_prob>::type,
105 Eigen::Dynamic, 1> log_theta_ns(ns.size());
106 for (
size_t i = 0; i < ns.size(); ++i)
107 log_theta_ns(i) = log_theta(ns[i] - 1);
109 return sum(log_theta_ns);
113 template <
typename T_prob>
115 typename boost::math::tools::promote_args<T_prob>::type
117 const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
118 return categorical_log<false>(ns, theta);
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
fvar< T > log(const fvar< T > &x)
bool check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Return true if the value is between the low and high values, inclusively.
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
Primary template class for the metaprogram to compute the index type of a container.
boost::math::tools::promote_args< T_prob >::type categorical_log(int n, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta)
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.