Stan  1.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs 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 
7 #include <stan/prob/traits.hpp>
10 #include <stan/prob/constants.hpp>
12 
13 namespace stan {
14 
15  namespace prob {
16 
17  // Categorical(n|theta) [0 < n <= N; 0 <= theta[n] <= 1; SUM theta = 1]
18  template <bool propto,
19  typename T_prob,
20  class Policy>
21  typename boost::math::tools::promote_args<T_prob>::type
23  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta,
24  const Policy&) {
25  static const char* function = "stan::prob::categorical_log(%1%)";
26 
29  using boost::math::tools::promote_args;
31 
33 
34  double lp = 0.0;
35  if (!check_bounded(function, n, lb, theta.size(),
36  "Number of categories",
37  &lp, Policy()))
38  return lp;
39 
41  Eigen::Matrix<double,Eigen::Dynamic,1> theta_dbl(theta.size());
42  for (int i = 0; i < theta_dbl.size(); ++i)
43  theta_dbl(i) = value_of(theta(i));
44  if (!check_simplex(function, theta,
45  "Probabilities parameter",
46  &lp, Policy()))
47  return lp;
48  } else {
49  if (!check_simplex(function, theta,
50  "Probabilities parameter",
51  &lp, Policy()))
52  return lp;
53  }
54 
56  return log(theta(n-1));
57  return 0.0;
58  }
59 
60  template <bool propto,
61  typename T_prob>
62  inline
63  typename boost::math::tools::promote_args<T_prob>::type
65  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta) {
66  return categorical_log<propto>(n,theta,stan::math::default_policy());
67  }
68 
69 
70  template <typename T_prob,
71  class Policy>
72  inline
73  typename boost::math::tools::promote_args<T_prob>::type
75  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta,
76  const Policy&) {
77  return categorical_log<false>(n,theta,Policy());
78  }
79 
80  template <typename T_prob>
81  inline
82  typename boost::math::tools::promote_args<T_prob>::type
84  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta) {
85  return categorical_log<false>(n,theta,stan::math::default_policy());
86  }
87 
88  template <class RNG>
89  inline int
90  categorical_rng(const Eigen::Matrix<double,Eigen::Dynamic,1>& theta,
91  RNG& rng) {
92  using boost::variate_generator;
93  using boost::uniform_01;
94  variate_generator<RNG&, uniform_01<> >
95  uniform01_rng(rng, uniform_01<>());
96 
97  Eigen::VectorXd index(theta.rows());
98  index.setZero();
99 
100  for(int i = 0; i < theta.rows(); i++) {
101  for(int j = i; j < theta.rows(); j++)
102  index(j) += theta(i,0);
103  }
104 
105  double c = uniform01_rng();
106  int b = 0;
107  while(c > index(b,0))
108  b++;
109  return b + 1;
110  }
111  }
112 }
113 #endif

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