1 #ifndef STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_LOGIT_LPMF_HPP 2 #define STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_LOGIT_LPMF_HPP 11 #include <boost/math/tools/promotion.hpp> 18 template <
bool propto,
typename T_prob>
20 int n,
const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>&
beta) {
21 static const char*
function =
"categorical_logit_lpmf";
23 check_bounded(
function,
"categorical outcome out of support", n, 1,
34 template <
typename T_prob>
35 inline typename boost::math::tools::promote_args<T_prob>::type
37 const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>&
beta) {
38 return categorical_logit_lpmf<false>(n,
beta);
41 template <
bool propto,
typename T_prob>
43 const std::vector<int>& ns,
44 const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>&
beta) {
45 static const char*
function =
"categorical_logit_lpmf";
47 for (
const auto& x : ns)
48 check_bounded(
function,
"categorical outcome out of support", x, 1,
58 Eigen::Matrix<T_prob, Eigen::Dynamic, 1> log_softmax_beta =
log_softmax(beta);
61 Eigen::Matrix<typename boost::math::tools::promote_args<T_prob>::type,
64 for (
size_t i = 0; i < ns.size(); ++i)
65 results[i] = log_softmax_beta(ns[i] - 1);
69 template <
typename T_prob>
70 inline typename boost::math::tools::promote_args<T_prob>::type
72 const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>&
beta) {
73 return categorical_logit_lpmf<false>(ns,
beta);
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
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.
Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > log_softmax(const Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > &alpha)
fvar< T > log_sum_exp(const std::vector< fvar< T > > &v)
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
boost::math::tools::promote_args< T_prob >::type categorical_logit_lpmf(int n, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &beta)
fvar< T > beta(const fvar< T > &x1, const fvar< T > &x2)
Return fvar with the beta function applied to the specified arguments and its gradient.