1#ifndef STAN_MATH_PRIM_PROB_DIRICHLET_LPDF_HPP
2#define STAN_MATH_PRIM_PROB_DIRICHLET_LPDF_HPP
56template <
bool propto,
typename T_prob,
typename T_prior_size,
58 T_prob, T_prior_size>* =
nullptr>
60 const T_prior_size& alpha) {
62 using T_partials_array =
typename Eigen::Array<T_partials_return, -1, -1>;
65 static constexpr const char* function =
"dirichlet_lpdf";
67 T_theta_ref theta_ref = theta;
68 T_alpha_ref alpha_ref = alpha;
74 "prior sample sizes", alpha_vec[0]);
76 for (
size_t t = 0; t < t_length; t++) {
81 const size_t t_size = theta_vec[0].size();
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);
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);
92 T_partials_return lp(0.0);
95 lp += (
lgamma(alpha_dbl.colwise().sum())
96 -
lgamma(alpha_dbl).colwise().sum())
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());
106 lp += (theta_log * alpha_m_1).
sum();
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();
118 for (
size_t t = 0; t < t_length; t++) {
119 partials_vec<1>(ops_partials)[t]
125 return ops_partials.build(lp);
128template <
typename T_prob,
typename T_prior_size>
130 const T_prior_size& alpha) {
131 return dirichlet_lpdf<false>(theta, alpha);
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.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
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.
typename ref_type_if< true, T >::type ref_type_t
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...