1 #ifndef STAN_MATH_PRIM_MAT_PROB_DIRICHLET_LPMF_HPP 2 #define STAN_MATH_PRIM_MAT_PROB_DIRICHLET_LPMF_HPP 48 template <
bool propto,
typename T_prob,
typename T_prior_size>
50 const T_prob& theta,
const T_prior_size& alpha) {
51 static const char*
function =
"dirichlet_lpmf";
55 typedef typename Eigen::Matrix<T_partials_return, -1, 1> T_partials_vec;
63 T_partials_vec theta_dbl =
value_of(theta_vec[0]);
66 T_partials_vec alpha_dbl =
value_of(alpha_vec[0]);
68 T_partials_return lp(0.0);
74 lp += (theta_dbl.array().log() * (alpha_dbl.array() - 1.0)).
sum();
76 T_partials_vec theta_deriv = (alpha_dbl.array() - 1.0) / theta_dbl.array();
78 T_partials_vec alpha_deriv =
digamma(alpha_dbl.sum())
80 + theta_dbl.array().log();
84 ops_partials.
edge1_.partials_ = theta_deriv;
87 ops_partials.
edge2_.partials_ = alpha_deriv;
89 return ops_partials.
build(lp);
92 template <
typename T_prob,
typename T_prior_size>
94 const T_prob& theta,
const T_prior_size& alpha) {
95 return dirichlet_lpmf<false>(theta, alpha);
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
void check_simplex(const char *function, const char *name, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta)
Check if the specified vector is simplex.
boost::math::tools::promote_args< double, typename partials_type< typename scalar_type< T >::type >::type, typename partials_return_type< T_pack... >::type >::type type
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
This template builds partial derivatives with respect to a set of operands.
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
return_type< T_prob, T_prior_size >::type dirichlet_lpmf(const T_prob &theta, const T_prior_size &alpha)
The log of the Dirichlet density for the given theta and a vector of prior sample sizes...
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
T_return_type build(double value)
Build the node to be stored on the autodiff graph.
internal::ops_partials_edge< double, Op2 > edge2_
This class provides a low-cost wrapper for situations where you either need an Eigen Vector or RowVec...
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Check if the dimension of x1 is consistent with x2.
internal::ops_partials_edge< double, Op1 > edge1_
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.