Stan Math Library  2.12.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  T_prob lp = 0.0;
34  check_bounded(function, "Number of categories", n, lb, theta.size());
35 
37  if (!check_simplex(function, "Probabilities parameter", theta))
38  return lp;
39  } else {
40  if (!check_simplex(function, "Probabilities parameter", theta))
41  return lp;
42  }
43 
45  return log(theta(n-1));
46  return 0.0;
47  }
48 
49  template <typename T_prob>
50  inline
51  typename boost::math::tools::promote_args<T_prob>::type
52  categorical_log(const typename
53  math::index_type<Eigen::Matrix<T_prob,
54  Eigen::Dynamic, 1> >::type n,
55  const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
56  return categorical_log<false>(n, theta);
57  }
58 
59  template <bool propto,
60  typename T_prob>
61  typename boost::math::tools::promote_args<T_prob>::type
62  categorical_log(const std::vector<int>& ns,
63  const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
64  static const char* function("categorical_log");
65 
66  using boost::math::tools::promote_args;
67  using std::log;
68 
69  int lb = 1;
70 
71  T_prob lp = 0.0;
72  for (size_t i = 0; i < ns.size(); ++i)
73  check_bounded(function, "element of outcome array", ns[i],
74  lb, theta.size());
75 
77  if (!check_simplex(function, "Probabilities parameter", theta))
78  return lp;
79  } else {
80  if (!check_simplex(function, "Probabilities parameter", theta))
81  return lp;
82  }
83 
85  return 0.0;
86 
87  if (ns.size() == 0)
88  return 0.0;
89 
90  Eigen::Matrix<T_prob, Eigen::Dynamic, 1> log_theta(theta.size());
91  for (int i = 0; i < theta.size(); ++i)
92  log_theta(i) = log(theta(i));
93 
94  Eigen::Matrix<typename boost::math::tools::promote_args<T_prob>::type,
95  Eigen::Dynamic, 1> log_theta_ns(ns.size());
96  for (size_t i = 0; i < ns.size(); ++i)
97  log_theta_ns(i) = log_theta(ns[i] - 1);
98 
99  return sum(log_theta_ns);
100  }
101 
102  template <typename T_prob>
103  inline
104  typename boost::math::tools::promote_args<T_prob>::type
105  categorical_log(const std::vector<int>& ns,
106  const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
107  return categorical_log<false>(ns, theta);
108  }
109 
110  }
111 }
112 #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
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
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.
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
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)
bool check_simplex(const char *function, const char *name, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta)
Return true if the specified vector is simplex.

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