| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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 <stan/math/prim/mat/err/check_simplex.hpp>
5 #include <stan/math/prim/scal/err/check_bounded.hpp>
6 #include <stan/math/prim/scal/fun/value_of.hpp>
7 #include <stan/math/prim/mat/fun/sum.hpp>
8 #include <stan/math/prim/mat/meta/index_type.hpp>
9 #include <stan/math/prim/scal/fun/constants.hpp>
10 #include <stan/math/prim/scal/meta/include_summand.hpp>
11 #include <stan/math/prim/scal/meta/is_constant_struct.hpp>
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
24 categorical_log(int n,
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
36 if (!stan::is_constant_struct<T_prob>::value) {
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
44 if (include_summand<propto, T_prob>::value)
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
76 if (!stan::is_constant_struct<T_prob>::value) {
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
84 if (!include_summand<propto, T_prob>::value)
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
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
Definition: log_sum_exp.hpp:8
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
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
is_constant_struct.hpp
stan::math::include_summand
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: include_summand.hpp:35
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:13
stan::math::index_type
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:18
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:24
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:40
value_of.hpp

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