Stan  1.0
probability, sampling & optimization
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 <stan/prob/traits.hpp>
9 
10 namespace stan {
11 
12  namespace prob {
13 
14  // Categorical(n|theta) [0 < n <= N; 0 <= theta[n] <= 1; SUM theta = 1]
15  template <bool propto,
16  typename T_prob,
17  class Policy>
18  typename boost::math::tools::promote_args<T_prob>::type
20  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta,
21  const Policy&) {
22  static const char* function = "stan::prob::categorical_log(%1%)";
23 
26  using boost::math::tools::promote_args;
27 
29 
30  typename promote_args<T_prob>::type lp(0.0);
31  if (!check_bounded(function, n, lb, theta.size(),
32  "Number of categories",
33  &lp, Policy()))
34  return lp;
35  if (!check_simplex(function, theta,
36  "Probabilities parameter",
37  &lp, Policy()))
38  return lp;
39 
41  return log(theta(n-1));
42  return 0.0;
43  }
44 
45  template <bool propto,
46  typename T_prob>
47  inline
48  typename boost::math::tools::promote_args<T_prob>::type
50  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta) {
51  return categorical_log<propto>(n,theta,stan::math::default_policy());
52  }
53 
54 
55  template <typename T_prob,
56  class Policy>
57  inline
58  typename boost::math::tools::promote_args<T_prob>::type
60  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta,
61  const Policy&) {
62  return categorical_log<false>(n,theta,Policy());
63  }
64 
65  template <typename T_prob>
66  inline
67  typename boost::math::tools::promote_args<T_prob>::type
69  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta) {
70  return categorical_log<false>(n,theta,stan::math::default_policy());
71  }
72 
73 
74  }
75 }
76 #endif
internal::traits< Derived >::Index size_type
var log(const var &a)
Return the natural log of the specified variable (cmath).
Definition: agrad.hpp:1730
bool check_simplex(const char *function, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta, const char *name, T_result *result, const Policy &)
Return true if the specified vector is simplex.
bool check_bounded(const char *function, const T_y &y, const T_low &low, const T_high &high, const char *name, T_result *result, const Policy &)
boost::math::policies::policy default_policy
Default error-handling policy from Boost.
boost::math::tools::promote_args< T_prob >::type categorical_log(const typename Eigen::Matrix< T_prob, Eigen::Dynamic, 1 >::size_type n, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta, const Policy &)
Definition: categorical.hpp:19
Probability, optimization and sampling library.
Definition: agrad.cpp:6
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: traits.hpp:33

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