Automatic Differentiation
 
Loading...
Searching...
No Matches
multinomial_logit_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_MULTINOMIAL_LOGIT_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_MULTINOMIAL_LOGIT_LPMF_HPP
3
10#include <vector>
11
12namespace stan {
13namespace math {
14
23template <bool propto, typename T_beta, typename T_prob = scalar_type_t<T_beta>,
24 require_eigen_col_vector_t<T_beta>* = nullptr>
26 const T_beta& beta) {
27 static constexpr const char* function = "multinomial_logit_lpmf";
28 check_size_match(function, "Size of number of trials variable", ns.size(),
29 "rows of log-probabilities parameter", beta.rows());
30 check_nonnegative(function, "Number of trials variable", ns);
31 const auto& beta_ref = to_ref(beta);
32 check_finite(function, "log-probabilities parameter", beta_ref);
33
35
36 decltype(auto) ns_map = as_array_or_scalar(ns);
37
39 lp += lgamma(1 + ns_map.sum()) - lgamma(1 + ns_map).sum();
40 }
41
43 T_prob alpha = log_sum_exp(beta_ref);
44 for (unsigned int i = 0; i < ns.size(); ++i) {
45 if (ns[i] != 0) {
46 lp += ns[i] * (beta_ref.coeff(i) - alpha);
47 }
48 }
49 }
50
51 return lp;
52}
53
54template <typename T_beta, require_eigen_col_vector_t<T_beta>* = nullptr>
56 const T_beta& beta) {
57 return multinomial_logit_lpmf<false>(ns, beta);
58}
59
60} // namespace math
61} // namespace stan
62#endif
return_type_t< T_prob > multinomial_logit_lpmf(const std::vector< int > &ns, const T_beta &beta)
Multinomial log PMF in log parametrization.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
T as_array_or_scalar(T &&v)
Returns specified input value.
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
Definition lgamma.hpp:21
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
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.
Definition beta.hpp:51
fvar< T > log_sum_exp(const fvar< T > &x1, const fvar< T > &x2)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...