Stan Math Library  2.20.0
reverse mode automatic differentiation
dirichlet_lpmf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_DIRICHLET_LPMF_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_DIRICHLET_LPMF_HPP
3 
11 
12 namespace stan {
13 namespace math {
14 
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";
52 
54  T_partials_return;
55  typedef typename Eigen::Matrix<T_partials_return, -1, 1> T_partials_vec;
56 
57  check_consistent_sizes(function, "probabilities", theta, "prior sample sizes",
58  alpha);
59  check_positive(function, "prior sample sizes", alpha);
60  check_simplex(function, "probabilities", theta);
61 
62  vector_seq_view<T_prob> theta_vec(theta);
63  T_partials_vec theta_dbl = value_of(theta_vec[0]);
64 
65  vector_seq_view<T_prior_size> alpha_vec(alpha);
66  T_partials_vec alpha_dbl = value_of(alpha_vec[0]);
67 
68  T_partials_return lp(0.0);
69 
71  lp += lgamma(alpha_dbl.sum()) - lgamma(alpha_dbl).sum();
72 
74  lp += (theta_dbl.array().log() * (alpha_dbl.array() - 1.0)).sum();
75 
76  T_partials_vec theta_deriv = (alpha_dbl.array() - 1.0) / theta_dbl.array();
77 
78  T_partials_vec alpha_deriv = digamma(alpha_dbl.sum())
79  - digamma(alpha_dbl).array()
80  + theta_dbl.array().log();
81 
82  operands_and_partials<T_prob, T_prior_size> ops_partials(theta, alpha);
84  ops_partials.edge1_.partials_ = theta_deriv;
85 
87  ops_partials.edge2_.partials_ = alpha_deriv;
88 
89  return ops_partials.build(lp);
90 }
91 
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);
96 }
97 
98 } // namespace math
99 } // namespace stan
100 #endif
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
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.
Definition: lgamma.hpp:21
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:17
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Definition: conjunction.hpp:14
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
Definition: return_type.hpp:36
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.
Definition: digamma.hpp:23

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