Stan Math Library  2.9.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 
16  namespace math {
17 
18  // CategoricalLog(n|theta) [0 < n <= N, theta unconstrained], no checking
19  template <bool propto,
20  typename T_prob>
21  typename boost::math::tools::promote_args<T_prob>::type
23  const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>&
24  beta) {
25  static const char* function("stan::math::categorical_logit_log");
26 
30 
31  check_bounded(function, "categorical outcome out of support", n,
32  1, beta.size());
33  check_finite(function, "log odds parameter", beta);
34 
36  return 0.0;
37 
38  // FIXME: wasteful vs. creating term (n-1) if not vectorized
39  return beta(n-1) - log_sum_exp(beta); // == log_softmax(beta)(n-1);
40  }
41 
42  template <typename T_prob>
43  inline
44  typename boost::math::tools::promote_args<T_prob>::type
46  const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>&
47  beta) {
48  return categorical_logit_log<false>(n, beta);
49  }
50 
51  template <bool propto,
52  typename T_prob>
53  typename boost::math::tools::promote_args<T_prob>::type
54  categorical_logit_log(const std::vector<int>& ns,
55  const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>&
56  beta) {
57  static const char* function("stan::math::categorical_logit_log");
58 
62  using stan::math::sum;
63 
64  for (size_t k = 0; k < ns.size(); ++k)
65  check_bounded(function, "categorical outcome out of support",
66  ns[k], 1, beta.size());
67  check_finite(function, "log odds parameter", beta);
68 
70  return 0.0;
71 
72  if (ns.size() == 0)
73  return 0.0;
74 
75  Eigen::Matrix<T_prob, Eigen::Dynamic, 1> log_softmax_beta
76  = log_softmax(beta);
77 
78  // FIXME: replace with more efficient sum()
79  Eigen::Matrix<typename boost::math::tools::promote_args<T_prob>::type,
80  Eigen::Dynamic, 1> results(ns.size());
81  for (size_t i = 0; i < ns.size(); ++i)
82  results[i] = log_softmax_beta(ns[i] - 1);
83  return sum(results);
84  }
85 
86  template <typename T_prob>
87  inline
88  typename boost::math::tools::promote_args<T_prob>::type
89  categorical_logit_log(const std::vector<int>& ns,
90  const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>&
91  beta) {
92  return categorical_logit_log<false>(ns, beta);
93  }
94 
95 
96  }
97 }
98 #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
boost::math::tools::promote_args< T_prob >::type categorical_logit_log(int n, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &beta)
bool check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Return true 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)
Definition: log_softmax.hpp:16
fvar< T > log_sum_exp(const std::vector< fvar< T > > &v)
Definition: log_sum_exp.hpp:14
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
bool check_finite(const char *function, const char *name, const T_y &y)
Return true if y is finite.

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