Automatic Differentiation
 
Loading...
Searching...
No Matches
categorical_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_CATEGORICAL_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_CATEGORICAL_LPMF_HPP
3
8#include <cmath>
9#include <vector>
10
11namespace stan {
12namespace math {
13
14// Categorical(n|theta) [0 < n <= N; 0 <= theta[n] <= 1; SUM theta = 1]
15template <bool propto, typename T_prob,
16 require_eigen_col_vector_t<T_prob>* = nullptr>
17return_type_t<T_prob> categorical_lpmf(int n, const T_prob& theta) {
18 static constexpr const char* function = "categorical_lpmf";
19 using std::log;
20
21 check_bounded(function, "Number of categories", n, 1, theta.size());
22 ref_type_t<T_prob> theta_ref = theta;
23 check_simplex(function, "Probabilities parameter", value_of(theta_ref));
24
26 return log(theta_ref.coeff(n - 1));
27 }
28 return 0.0;
29}
30
31template <bool propto, typename T_prob,
33return_type_t<T_prob> categorical_lpmf(const std::vector<int>& ns,
34 const T_prob& theta) {
35 static constexpr const char* function = "categorical_lpmf";
36
37 check_bounded(function, "element of outcome array", ns, 1, theta.size());
38 ref_type_t<T_prob> theta_ref = theta;
39 check_simplex(function, "Probabilities parameter", value_of(theta_ref));
40
42 return 0.0;
43 }
44
45 if (ns.size() == 0) {
46 return 0.0;
47 }
48
49 Eigen::Matrix<value_type_t<T_prob>, Eigen::Dynamic, 1> log_theta
50 = log(theta_ref);
51 Eigen::Matrix<return_type_t<T_prob>, Eigen::Dynamic, 1> log_theta_ns(
52 ns.size());
53 for (size_t i = 0; i < ns.size(); ++i) {
54 log_theta_ns(i) = log_theta(ns[i] - 1);
55 }
56
57 return sum(log_theta_ns);
58}
59
60template <typename T_n, typename T_prob, require_st_integral<T_n>* = nullptr,
61 require_eigen_col_vector_t<T_prob>* = nullptr>
63 const T_prob& theta) {
64 return categorical_lpmf<false>(ns, theta);
65}
66
67} // namespace math
68} // namespace stan
69#endif
require_t< is_eigen_col_vector< std::decay_t< T > > > require_eigen_col_vector_t
Require type satisfies is_eigen_col_vector.
Definition is_vector.hpp:98
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
void check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Check if the value is between the low and high values, inclusively.
return_type_t< T_prob > categorical_lpmf(int n, const T_prob &theta)
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
fvar< T > log(const fvar< T > &x)
Definition log.hpp:15
void check_simplex(const char *function, const char *name, const T &theta)
Throw an exception if the specified vector is not a simplex.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:55
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...