Stan Math Library  2.20.0
reverse mode automatic differentiation
categorical_lpmf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_LPMF_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_LPMF_HPP
3 
8 #include <boost/math/tools/promotion.hpp>
9 #include <cmath>
10 #include <vector>
11 
12 namespace stan {
13 namespace math {
14 
15 // Categorical(n|theta) [0 < n <= N; 0 <= theta[n] <= 1; SUM theta = 1]
16 template <bool propto, typename T_prob>
17 typename boost::math::tools::promote_args<T_prob>::type categorical_lpmf(
18  int n, const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
19  static const char* function = "categorical_lpmf";
20 
21  using std::log;
22 
23  int lb = 1;
24 
25  check_bounded(function, "Number of categories", n, lb, theta.size());
26  check_simplex(function, "Probabilities parameter", theta);
27 
29  return log(theta(n - 1));
30  return 0.0;
31 }
32 
33 template <typename T_prob>
34 inline typename boost::math::tools::promote_args<T_prob>::type categorical_lpmf(
35  const typename math::index_type<
36  Eigen::Matrix<T_prob, Eigen::Dynamic, 1> >::type n,
37  const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
38  return categorical_lpmf<false>(n, theta);
39 }
40 
41 template <bool propto, typename T_prob>
42 typename boost::math::tools::promote_args<T_prob>::type categorical_lpmf(
43  const std::vector<int>& ns,
44  const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
45  static const char* function = "categorical_lpmf";
46 
47  using std::log;
48 
49  int lb = 1;
50 
51  for (size_t i = 0; i < ns.size(); ++i)
52  check_bounded(function, "element of outcome array", ns[i], lb,
53  theta.size());
54 
55  check_simplex(function, "Probabilities parameter", theta);
56 
58  return 0.0;
59 
60  if (ns.size() == 0)
61  return 0.0;
62 
63  Eigen::Matrix<T_prob, Eigen::Dynamic, 1> log_theta(theta.size());
64  for (int i = 0; i < theta.size(); ++i)
65  log_theta(i) = log(theta(i));
66 
67  Eigen::Matrix<typename boost::math::tools::promote_args<T_prob>::type,
68  Eigen::Dynamic, 1>
69  log_theta_ns(ns.size());
70  for (size_t i = 0; i < ns.size(); ++i)
71  log_theta_ns(i) = log_theta(ns[i] - 1);
72 
73  return sum(log_theta_ns);
74 }
75 
76 template <typename T_prob>
77 inline typename boost::math::tools::promote_args<T_prob>::type categorical_lpmf(
78  const std::vector<int>& ns,
79  const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
80  return categorical_lpmf<false>(ns, theta);
81 }
82 
83 } // namespace math
84 } // namespace stan
85 #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_simplex(const char *function, const char *name, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta)
Check if the specified vector is simplex.
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.
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:12
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:18
boost::math::tools::promote_args< T_prob >::type categorical_lpmf(int n, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta)

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