Automatic Differentiation
 
Loading...
Searching...
No Matches
categorical_logit_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_CATEGORICAL_LOGIT_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_CATEGORICAL_LOGIT_LPMF_HPP
3
10#include <vector>
11
12namespace stan {
13namespace math {
14
15// CategoricalLog(n|theta) [0 < n <= N, theta unconstrained], no checking
16template <bool propto, typename T_prob, require_col_vector_t<T_prob>* = nullptr>
18 static constexpr const char* function = "categorical_logit_lpmf";
19 check_bounded(function, "categorical outcome out of support", n, 1,
20 beta.size());
21 ref_type_t<T_prob> beta_ref = beta;
22 check_finite(function, "log odds parameter", beta_ref);
23
25 return 0.0;
26 }
27
28 // FIXME: wasteful vs. creating term (n-1) if not vectorized
29 return beta_ref.coeff(n - 1)
30 - log_sum_exp(beta_ref); // == log_softmax(beta)(n-1);
31}
32
33template <bool propto, typename T_prob, require_col_vector_t<T_prob>* = nullptr>
35 const T_prob& beta) {
36 static constexpr const char* function = "categorical_logit_lpmf";
37
38 check_bounded(function, "categorical outcome out of support", ns, 1,
39 beta.size());
40 ref_type_t<T_prob> beta_ref = beta;
41 check_finite(function, "log odds parameter", beta_ref);
42
44 return 0.0;
45 }
46
47 if (ns.empty()) {
48 return 0.0;
49 }
50
51 auto log_softmax_beta = to_ref(log_softmax(beta_ref));
52
53 // FIXME: replace with more efficient sum()
54 Eigen::Matrix<return_type_t<T_prob>, Eigen::Dynamic, 1> results(ns.size());
55 for (size_t i = 0; i < ns.size(); ++i) {
56 results[i] = log_softmax_beta(ns[i] - 1);
57 }
58 return sum(results);
59}
60
61template <typename T_n, typename T_prob, require_st_integral<T_n>* = nullptr,
62 require_col_vector_t<T_prob>* = nullptr>
64 const T_prob& beta) {
65 return categorical_logit_lpmf<false>(ns, beta);
66}
67
68} // namespace math
69} // namespace stan
70#endif
results_cl< T_results... > results(T_results &&... results)
Deduces types for constructing results_cl object.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
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.
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.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
auto log_softmax(const T &x)
Return the log softmax of the specified vector or container of vectors.
return_type_t< T_prob > categorical_logit_lpmf(int n, const T_prob &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.
Definition beta.hpp:51
fvar< T > log_sum_exp(const fvar< T > &x1, const fvar< T > &x2)
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:55
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...