Stan Math Library  2.14.0
reverse mode automatic differentiation
categorical_logit_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_LOGIT_LOG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_LOGIT_LOG_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,
19  typename T_prob>
20  typename boost::math::tools::promote_args<T_prob>::type
22  const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>&
23  beta) {
24  static const char* function("categorical_logit_log");
25 
26  check_bounded(function, "categorical outcome out of support", n,
27  1, beta.size());
28  check_finite(function, "log odds parameter", beta);
29 
31  return 0.0;
32 
33  // FIXME: wasteful vs. creating term (n-1) if not vectorized
34  return beta(n-1) - log_sum_exp(beta); // == log_softmax(beta)(n-1);
35  }
36 
37  template <typename T_prob>
38  inline
39  typename boost::math::tools::promote_args<T_prob>::type
41  const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>&
42  beta) {
43  return categorical_logit_log<false>(n, beta);
44  }
45 
46  template <bool propto,
47  typename T_prob>
48  typename boost::math::tools::promote_args<T_prob>::type
49  categorical_logit_log(const std::vector<int>& ns,
50  const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>&
51  beta) {
52  static const char* function("categorical_logit_log");
53 
54  for (size_t k = 0; k < ns.size(); ++k)
55  check_bounded(function, "categorical outcome out of support",
56  ns[k], 1, beta.size());
57  check_finite(function, "log odds parameter", beta);
58 
60  return 0.0;
61 
62  if (ns.size() == 0)
63  return 0.0;
64 
65  Eigen::Matrix<T_prob, Eigen::Dynamic, 1> log_softmax_beta
66  = log_softmax(beta);
67 
68  // FIXME: replace with more efficient sum()
69  Eigen::Matrix<typename boost::math::tools::promote_args<T_prob>::type,
70  Eigen::Dynamic, 1> results(ns.size());
71  for (size_t i = 0; i < ns.size(); ++i)
72  results[i] = log_softmax_beta(ns[i] - 1);
73  return sum(results);
74  }
75 
76  template <typename T_prob>
77  inline
78  typename boost::math::tools::promote_args<T_prob>::type
79  categorical_logit_log(const std::vector<int>& ns,
80  const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>&
81  beta) {
82  return categorical_logit_log<false>(ns, beta);
83  }
84 
85  }
86 }
87 #endif
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
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.
boost::math::tools::promote_args< T_prob >::type categorical_logit_log(int n, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &beta)
Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > log_softmax(const Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > &alpha)
Definition: log_softmax.hpp:16
fvar< T > log_sum_exp(const std::vector< fvar< T > > &v)
Definition: log_sum_exp.hpp:13
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...

     [ Stan Home Page ] © 2011–2016, Stan Development Team.