categorical_logit_lpmf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_LOGIT_LPMF_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_LOGIT_LPMF_HPP
3
11 #include <boost/math/tools/promotion.hpp>
12 #include <vector>
13
14 namespace stan {
15 namespace math {
16
17 // CategoricalLog(n|theta) [0 < n <= N, theta unconstrained], no checking
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";
22
23  check_bounded(function, "categorical outcome out of support", n, 1,
24  beta.size());
25  check_finite(function, "log odds parameter", beta);
26
28  return 0.0;
29
30  // FIXME: wasteful vs. creating term (n-1) if not vectorized
31  return beta(n - 1) - log_sum_exp(beta); // == log_softmax(beta)(n-1);
32 }
33
34 template <typename T_prob>
37  const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& beta) {
38  return categorical_logit_lpmf<false>(n, beta);
39 }
40
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";
46
47  for (const auto& x : ns)
48  check_bounded(function, "categorical outcome out of support", x, 1,
49  beta.size());
50  check_finite(function, "log odds parameter", beta);
51
53  return 0.0;
54
55  if (ns.empty())
56  return 0.0;
57
58  Eigen::Matrix<T_prob, Eigen::Dynamic, 1> log_softmax_beta = log_softmax(beta);
59
60  // FIXME: replace with more efficient sum()
62  Eigen::Dynamic, 1>
63  results(ns.size());
64  for (size_t i = 0; i < ns.size(); ++i)
65  results[i] = log_softmax_beta(ns[i] - 1);
66  return sum(results);
67 }
68
69 template <typename T_prob>
71 categorical_logit_lpmf(const std::vector<int>& ns,
72  const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& beta) {
73  return categorical_logit_lpmf<false>(ns, beta);
74 }
75
76 } // namespace math
77 } // namespace stan
78 #endif
fvar< T > sum(const std::vector< fvar< T > > &m)
Definition: sum.hpp:20
void check_finite(const char *function, const char *name, const T_y &y)
void check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Double_t beta
Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > log_softmax(const Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > &alpha)
Definition: log_softmax.hpp:14
fvar< T > log_sum_exp(const std::vector< fvar< T > > &v)
Definition: log_sum_exp.hpp:12
boost::math::tools::promote_args< T_prob >::type categorical_logit_lpmf(int n, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &beta)
static const double ns
Module that plots metrics from reconstructed cosmic ray data.