1 #ifndef STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_LOG_HPP 2 #define STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_LOG_HPP 12 #include <boost/random/uniform_01.hpp> 13 #include <boost/random/variate_generator.hpp> 21 template <
bool propto,
23 typename boost::math::tools::promote_args<T_prob>::type
25 const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
26 static const char*
function(
"categorical_log");
28 using boost::math::tools::promote_args;
33 check_bounded(
function,
"Number of categories", n, lb, theta.size());
37 return log(theta(n-1));
41 template <
typename T_prob>
43 typename boost::math::tools::promote_args<T_prob>::type
46 Eigen::Dynamic, 1> >::type n,
47 const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
48 return categorical_log<false>(n, theta);
51 template <
bool propto,
53 typename boost::math::tools::promote_args<T_prob>::type
55 const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
56 static const char*
function(
"categorical_log");
58 using boost::math::tools::promote_args;
63 for (
size_t i = 0; i < ns.size(); ++i)
75 Eigen::Matrix<T_prob, Eigen::Dynamic, 1> log_theta(theta.size());
76 for (
int i = 0; i < theta.size(); ++i)
77 log_theta(i) =
log(theta(i));
79 Eigen::Matrix<typename boost::math::tools::promote_args<T_prob>::type,
80 Eigen::Dynamic, 1> log_theta_ns(ns.size());
81 for (
size_t i = 0; i < ns.size(); ++i)
82 log_theta_ns(i) = log_theta(ns[i] - 1);
84 return sum(log_theta_ns);
87 template <
typename T_prob>
89 typename boost::math::tools::promote_args<T_prob>::type
91 const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
92 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.
void check_simplex(const char *function, const char *name, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta)
Check if the specified vector is simplex.
void check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Check if the value is between the low and high values, inclusively.
fvar< T > log(const fvar< T > &x)
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
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)