Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
categorical.hpp
Go to the documentation of this file.
1 #ifndef STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__DISCRETE__CATEGORICAL_HPP
2 #define STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__DISCRETE__CATEGORICAL_HPP
3 
4 #include <boost/random/uniform_01.hpp>
5 #include <boost/random/variate_generator.hpp>
6 
10 #include <stan/math/matrix/sum.hpp>
12 #include <stan/prob/constants.hpp>
13 #include <stan/prob/traits.hpp>
14 
15 namespace stan {
16 
17  namespace prob {
18 
19  // Categorical(n|theta) [0 < n <= N; 0 <= theta[n] <= 1; SUM theta = 1]
20  template <bool propto,
21  typename T_prob>
22  typename boost::math::tools::promote_args<T_prob>::type
24  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta) {
25  static const char* function = "stan::prob::categorical_log(%1%)";
26 
29  using boost::math::tools::promote_args;
31 
32  int lb = 1;
33 
34  T_prob lp = 0.0;
35  check_bounded(function, n, lb, theta.size(),
36  "Number of categories",
37  &lp);
38 
40  if (!check_simplex(function, theta,
41  "Probabilities parameter",
42  &lp))
43  return lp;
44  } else {
45  if (!check_simplex(function, theta,
46  "Probabilities parameter",
47  &lp))
48  return lp;
49  }
50 
52  return log(theta(n-1));
53  return 0.0;
54  }
55 
56  template <typename T_prob>
57  inline
58  typename boost::math::tools::promote_args<T_prob>::type
59  categorical_log(const typename
60  math::index_type<Eigen::Matrix<T_prob,
61  Eigen::Dynamic,1> >::type n,
62  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta) {
63 
64  return categorical_log<false>(n,theta);
65  }
66 
67 
68  // Categorical(n|theta) [0 < n <= N; 0 <= theta[n] <= 1; SUM theta = 1]
69  template <bool propto,
70  typename T_prob>
71  typename boost::math::tools::promote_args<T_prob>::type
72  categorical_log(const std::vector<int>& ns,
73  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta) {
74  static const char* function = "stan::prob::categorical_log(%1%)";
75 
76  using boost::math::tools::promote_args;
79  using stan::math::sum;
81 
82  int lb = 1;
83 
84  T_prob lp = 0.0;
85  for (size_t i = 0; i < ns.size(); ++i)
86  check_bounded(function, ns[i], lb, theta.size(),
87  "element of outcome array",
88  &lp);
89 
91  if (!check_simplex(function, theta,
92  "Probabilities parameter",
93  &lp))
94  return lp;
95  } else {
96  if (!check_simplex(function, theta,
97  "Probabilities parameter",
98  &lp))
99  return lp;
100  }
101 
103  return 0.0;
104 
105  if (ns.size() == 0)
106  return 0.0;
107 
108  Eigen::Matrix<T_prob,Eigen::Dynamic,1> log_theta(theta.size());
109  for (int i = 0; i < theta.size(); ++i)
110  log_theta(i) = log(theta(i));
111 
112  Eigen::Matrix<typename boost::math::tools::promote_args<T_prob>::type,
113  Eigen::Dynamic,1> log_theta_ns(ns.size());
114  for (size_t i = 0; i < ns.size(); ++i)
115  log_theta_ns(i) = log_theta(ns[i] - 1);
116 
117  return sum(log_theta_ns);
118  }
119 
120 
121  template <typename T_prob>
122  inline
123  typename boost::math::tools::promote_args<T_prob>::type
124  categorical_log(const std::vector<int>& ns,
125  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta) {
126  return categorical_log<false>(ns,theta);
127  }
128 
129  template <class RNG>
130  inline int
131  categorical_rng(const Eigen::Matrix<double,Eigen::Dynamic,1>& theta,
132  RNG& rng) {
133  using boost::variate_generator;
134  using boost::uniform_01;
136 
137  static const char* function = "stan::prob::categorical_rng(%1%)";
138 
139  check_simplex(function, theta,
140  "Probabilities parameter", (double*)0);
141 
142  variate_generator<RNG&, uniform_01<> >
143  uniform01_rng(rng, uniform_01<>());
144 
145  Eigen::VectorXd index(theta.rows());
146  index.setZero();
147 
148  for(int i = 0; i < theta.rows(); i++) {
149  for(int j = i; j < theta.rows(); j++)
150  index(j) += theta(i,0);
151  }
152 
153  double c = uniform01_rng();
154  int b = 0;
155  while(c > index(b,0))
156  b++;
157  return b + 1;
158  }
159  }
160 }
161 #endif
bool check_bounded(const char *function, const T_y &y, const T_low &low, const T_high &high, const char *name, T_result *result)
boost::math::tools::promote_args< T_prob >::type categorical_log(int n, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta)
Definition: categorical.hpp:23
bool check_simplex(const char *function, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta, const char *name, T_result *result)
Return true if the specified vector is simplex.
int categorical_rng(const Eigen::Matrix< double, Eigen::Dynamic, 1 > &theta, RNG &rng)
fvar< T > sum(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: sum.hpp:14
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
Definition: traits.hpp:57
double value_of(const T x)
Return the value of the specified scalar argument converted to a double value.
Definition: value_of.hpp:24
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:21
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: traits.hpp:35
double sum(std::vector< double > &x)
Definition: sum.hpp:10
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15

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