1 #ifndef STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_LPMF_HPP 2 #define STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_LPMF_HPP 8 #include <boost/math/tools/promotion.hpp> 16 template <
bool propto,
typename T_prob>
18 int n,
const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
19 static const char*
function =
"categorical_lpmf";
25 check_bounded(
function,
"Number of categories", n, lb, theta.size());
29 return log(theta(n - 1));
33 template <
typename T_prob>
36 Eigen::Matrix<T_prob, Eigen::Dynamic, 1> >::type n,
37 const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
38 return categorical_lpmf<false>(n, theta);
41 template <
bool propto,
typename T_prob>
43 const std::vector<int>& ns,
44 const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
45 static const char*
function =
"categorical_lpmf";
51 for (
size_t i = 0; i < ns.size(); ++i)
52 check_bounded(
function,
"element of outcome array", ns[i], lb,
63 Eigen::Matrix<T_prob, Eigen::Dynamic, 1> log_theta(theta.size());
64 for (
int i = 0; i < theta.size(); ++i)
65 log_theta(i) =
log(theta(i));
67 Eigen::Matrix<typename boost::math::tools::promote_args<T_prob>::type,
69 log_theta_ns(ns.size());
70 for (
size_t i = 0; i < ns.size(); ++i)
71 log_theta_ns(i) = log_theta(ns[i] - 1);
73 return sum(log_theta_ns);
76 template <
typename T_prob>
78 const std::vector<int>& ns,
79 const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
80 return categorical_lpmf<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_lpmf(int n, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta)