Stan Math Library  2.15.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 
4 #include <boost/math/special_functions/gamma.hpp>
5 #include <boost/random/gamma_distribution.hpp>
6 #include <boost/random/variate_generator.hpp>
13 
14 namespace stan {
15  namespace math {
16 
42  template <bool propto,
43  typename T_prob, typename T_prior_sample_size>
44  typename boost::math::tools::promote_args<T_prob, T_prior_sample_size>::type
45  dirichlet_lpmf(const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta,
46  const Eigen::Matrix
47  <T_prior_sample_size, Eigen::Dynamic, 1>& alpha) {
48  static const char* function("dirichlet_lpmf");
49  using boost::math::lgamma;
50  using boost::math::tools::promote_args;
51 
52  typename promote_args<T_prob, T_prior_sample_size>::type lp(0.0);
53  check_consistent_sizes(function,
54  "probabilities", theta,
55  "prior sample sizes", alpha);
56  check_positive(function, "prior sample sizes", alpha);
57  check_simplex(function, "probabilities", theta);
58 
60  lp += lgamma(alpha.sum());
61  for (int k = 0; k < alpha.rows(); ++k)
62  lp -= lgamma(alpha[k]);
63  }
65  for (int k = 0; k < theta.rows(); ++k)
66  lp += multiply_log(alpha[k]-1, theta[k]);
67  }
68  return lp;
69  }
70 
71  template <typename T_prob, typename T_prior_sample_size>
72  inline
73  typename boost::math::tools::promote_args<T_prob, T_prior_sample_size>::type
74  dirichlet_lpmf(const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta,
75  const Eigen::Matrix
76  <T_prior_sample_size, Eigen::Dynamic, 1>& alpha) {
77  return dirichlet_lpmf<false>(theta, alpha);
78  }
79 
80  }
81 }
82 #endif
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.
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
Definition: lgamma.hpp:20
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
boost::math::tools::promote_args< T_prob, T_prior_sample_size >::type dirichlet_lpmf(const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta, const Eigen::Matrix< T_prior_sample_size, Eigen::Dynamic, 1 > &alpha)
The log of the Dirichlet density for the given theta and a vector of prior sample sizes...
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)
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.

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