Stan Math Library  2.14.0
reverse mode automatic differentiation
categorical_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_LOG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_LOG_HPP
3 
12 #include <boost/random/uniform_01.hpp>
13 #include <boost/random/variate_generator.hpp>
14 #include <cmath>
15 #include <vector>
16 
17 namespace stan {
18  namespace math {
19 
20  // Categorical(n|theta) [0 < n <= N; 0 <= theta[n] <= 1; SUM theta = 1]
21  template <bool propto,
22  typename T_prob>
23  typename boost::math::tools::promote_args<T_prob>::type
25  const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
26  static const char* function("categorical_log");
27 
28  using boost::math::tools::promote_args;
29  using std::log;
30 
31  int lb = 1;
32 
33  check_bounded(function, "Number of categories", n, lb, theta.size());
34  check_simplex(function, "Probabilities parameter", theta);
35 
37  return log(theta(n-1));
38  return 0.0;
39  }
40 
41  template <typename T_prob>
42  inline
43  typename boost::math::tools::promote_args<T_prob>::type
44  categorical_log(const typename
45  math::index_type<Eigen::Matrix<T_prob,
46  Eigen::Dynamic, 1> >::type n,
47  const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
48  return categorical_log<false>(n, theta);
49  }
50 
51  template <bool propto,
52  typename T_prob>
53  typename boost::math::tools::promote_args<T_prob>::type
54  categorical_log(const std::vector<int>& ns,
55  const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
56  static const char* function("categorical_log");
57 
58  using boost::math::tools::promote_args;
59  using std::log;
60 
61  int lb = 1;
62 
63  for (size_t i = 0; i < ns.size(); ++i)
64  check_bounded(function, "element of outcome array", ns[i],
65  lb, theta.size());
66 
67  check_simplex(function, "Probabilities parameter", theta);
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_theta(theta.size());
76  for (int i = 0; i < theta.size(); ++i)
77  log_theta(i) = log(theta(i));
78 
79  Eigen::Matrix<typename boost::math::tools::promote_args<T_prob>::type,
80  Eigen::Dynamic, 1> log_theta_ns(ns.size());
81  for (size_t i = 0; i < ns.size(); ++i)
82  log_theta_ns(i) = log_theta(ns[i] - 1);
83 
84  return sum(log_theta_ns);
85  }
86 
87  template <typename T_prob>
88  inline
89  typename boost::math::tools::promote_args<T_prob>::type
90  categorical_log(const std::vector<int>& ns,
91  const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
92  return categorical_log<false>(ns, theta);
93  }
94 
95  }
96 }
97 #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:14
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_log(int n, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta)

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