| Stan Math Library 2.6.3 probability, sampling & optimization

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
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
4 #include <boost/random/uniform_01.hpp>
5 #include <boost/random/variate_generator.hpp>
6 #include <stan/math/prim/mat/err/check_simplex.hpp>
7 #include <stan/math/prim/scal/err/check_bounded.hpp>
8 #include <stan/math/prim/scal/fun/value_of.hpp>
9 #include <stan/math/prim/mat/fun/sum.hpp>
10 #include <stan/math/prim/mat/meta/index_type.hpp>
11 #include <stan/math/prim/scal/fun/constants.hpp>
12 #include <stan/math/prim/scal/meta/include_summand.hpp>
13 #include <stan/math/prim/scal/meta/is_constant_struct.hpp>
14 #include <cmath>
15 #include <vector>
16
17 namespace stan {
18
19 namespace math {
20
21 // Categorical(n|theta) [0 < n <= N; 0 <= theta[n] <= 1; SUM theta = 1]
22 template <bool propto,
23 typename T_prob>
24 typename boost::math::tools::promote_args<T_prob>::type
25 categorical_log(int n,
26 const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
27 static const char* function("stan::math::categorical_log");
28
29 using stan::math::check_bounded;
30 using stan::math::check_simplex;
31 using boost::math::tools::promote_args;
32 using stan::math::value_of;
33 using std::log;
34
35 int lb = 1;
36
37 T_prob lp = 0.0;
38 check_bounded(function, "Number of categories", n, lb, theta.size());
39
40 if (!stan::is_constant_struct<T_prob>::value) {
41 if (!check_simplex(function, "Probabilities parameter", theta))
42 return lp;
43 } else {
44 if (!check_simplex(function, "Probabilities parameter", theta))
45 return lp;
46 }
47
48 if (include_summand<propto, T_prob>::value)
49 return log(theta(n-1));
50 return 0.0;
51 }
52
53 template <typename T_prob>
54 inline
55 typename boost::math::tools::promote_args<T_prob>::type
56 categorical_log(const typename
57 math::index_type<Eigen::Matrix<T_prob,
58 Eigen::Dynamic, 1> >::type n,
59 const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
60 return categorical_log<false>(n, theta);
61 }
62
63
64 // Categorical(n|theta) [0 < n <= N; 0 <= theta[n] <= 1; SUM theta = 1]
65 template <bool propto,
66 typename T_prob>
67 typename boost::math::tools::promote_args<T_prob>::type
68 categorical_log(const std::vector<int>& ns,
69 const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
70 static const char* function("stan::math::categorical_log");
71
72 using boost::math::tools::promote_args;
73 using stan::math::check_bounded;
74 using stan::math::check_simplex;
75 using stan::math::sum;
76 using stan::math::value_of;
77 using std::log;
78
79 int lb = 1;
80
81 T_prob lp = 0.0;
82 for (size_t i = 0; i < ns.size(); ++i)
83 check_bounded(function, "element of outcome array", ns[i],
84 lb, theta.size());
85
86 if (!stan::is_constant_struct<T_prob>::value) {
87 if (!check_simplex(function, "Probabilities parameter", theta))
88 return lp;
89 } else {
90 if (!check_simplex(function, "Probabilities parameter", theta))
91 return lp;
92 }
93
94 if (!include_summand<propto, T_prob>::value)
95 return 0.0;
96
97 if (ns.size() == 0)
98 return 0.0;
99
100 Eigen::Matrix<T_prob, Eigen::Dynamic, 1> log_theta(theta.size());
101 for (int i = 0; i < theta.size(); ++i)
102 log_theta(i) = log(theta(i));
103
104 Eigen::Matrix<typename boost::math::tools::promote_args<T_prob>::type,
105 Eigen::Dynamic, 1> log_theta_ns(ns.size());
106 for (size_t i = 0; i < ns.size(); ++i)
107 log_theta_ns(i) = log_theta(ns[i] - 1);
108
109 return sum(log_theta_ns);
110 }
111
112
113 template <typename T_prob>
114 inline
115 typename boost::math::tools::promote_args<T_prob>::type
116 categorical_log(const std::vector<int>& ns,
117 const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
118 return categorical_log<false>(ns, theta);
119 }
120
121 }
122 }
123 #endif
stan::math::sum
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
check_simplex.hpp
stan::math::value_of
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:16
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
stan::math::check_bounded
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.
Definition: check_bounded.hpp:95
stan::math::include_summand
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: include_summand.hpp:36
include_summand.hpp
stan::is_constant_struct
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
Definition: is_constant_struct.hpp:16
stan::math::index_type
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:19
index_type.hpp
stan::math::categorical_log
boost::math::tools::promote_args< T_prob >::type categorical_log(int n, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta)
Definition: categorical_log.hpp:25
check_bounded.hpp
constants.hpp
sum.hpp
stan::math::check_simplex
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.
Definition: check_simplex.hpp:41
is_constant_struct.hpp
value_of.hpp

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