Automatic Differentiation
 
Loading...
Searching...
No Matches
dirichlet_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_DIRICHLET_LPDF_HPP
2#define STAN_MATH_PRIM_PROB_DIRICHLET_LPDF_HPP
3
13
14namespace stan {
15namespace math {
16
56template <bool propto, typename T_prob, typename T_prior_size,
58 T_prob, T_prior_size>* = nullptr>
60 const T_prior_size& alpha) {
61 using T_partials_return = partials_return_t<T_prob, T_prior_size>;
62 using T_partials_array = typename Eigen::Array<T_partials_return, -1, -1>;
63 using T_theta_ref = ref_type_t<T_prob>;
64 using T_alpha_ref = ref_type_t<T_prior_size>;
65 static constexpr const char* function = "dirichlet_lpdf";
66
67 T_theta_ref theta_ref = theta;
68 T_alpha_ref alpha_ref = alpha;
69 vector_seq_view<T_theta_ref> theta_vec(theta_ref);
70 vector_seq_view<T_alpha_ref> alpha_vec(alpha_ref);
71 const size_t t_length = max_size_mvt(theta, alpha);
72
73 check_consistent_sizes(function, "probabilities", theta_vec[0],
74 "prior sample sizes", alpha_vec[0]);
75
76 for (size_t t = 0; t < t_length; t++) {
77 check_positive(function, "prior sample sizes", alpha_vec[t]);
78 check_simplex(function, "probabilities", theta_vec[t]);
79 }
80
81 const size_t t_size = theta_vec[0].size();
82
83 T_partials_array theta_dbl(t_size, t_length);
84 for (size_t t = 0; t < t_length; t++) {
85 theta_dbl.col(t) = theta_vec.val(t);
86 }
87 T_partials_array alpha_dbl(t_size, t_length);
88 for (size_t t = 0; t < t_length; t++) {
89 alpha_dbl.col(t) = alpha_vec.val(t);
90 }
91
92 T_partials_return lp(0.0);
93
95 lp += (lgamma(alpha_dbl.colwise().sum())
96 - lgamma(alpha_dbl).colwise().sum())
97 .sum();
98 }
99
100 const auto& alpha_m_1
101 = to_ref_if<!is_constant_all<T_prob>::value>(alpha_dbl - 1.0);
102 const auto& theta_log
103 = to_ref_if<!is_constant_all<T_prior_size>::value>(theta_dbl.log());
104
106 lp += (theta_log * alpha_m_1).sum();
107 }
108
109 auto ops_partials = make_partials_propagator(theta_ref, alpha_ref);
111 for (size_t t = 0; t < t_length; t++) {
112 partials_vec<0>(ops_partials)[t]
113 += (alpha_m_1.col(t) / theta_dbl.col(t)).matrix();
114 }
115 }
116
118 for (size_t t = 0; t < t_length; t++) {
119 partials_vec<1>(ops_partials)[t]
120 += (digamma(alpha_dbl.col(t).sum()) - digamma(alpha_dbl.col(t))
121 + theta_log.col(t))
122 .matrix();
123 }
124 }
125 return ops_partials.build(lp);
126}
127
128template <typename T_prob, typename T_prior_size>
130 const T_prior_size& alpha) {
131 return dirichlet_lpdf<false>(theta, alpha);
132}
133
134} // namespace math
135} // namespace stan
136#endif
This class provides a low-cost wrapper for situations where you either need an Eigen Vector or RowVec...
require_all_not_t< is_nonscalar_prim_or_rev_kernel_expression< std::decay_t< Types > >... > require_all_not_nonscalar_prim_or_rev_kernel_expression_t
Require none of the types satisfy is_nonscalar_prim_or_rev_kernel_expression.
return_type_t< T_prob_cl, T_prior_size_cl > dirichlet_lpdf(const T_prob_cl &theta, const T_prior_size_cl &alpha)
The log of the Dirichlet density for the given theta and a vector of prior sample sizes,...
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
int64_t max_size_mvt(const T1 &x1, const Ts &... xs)
Calculate the size of the largest multivariate input.
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
void check_simplex(const char *function, const char *name, const T &theta)
Throw an exception if the specified vector is not a simplex.
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
Definition lgamma.hpp:21
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
Definition digamma.hpp:23
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:55
typename partials_return_type< Args... >::type partials_return_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...