Stan Math Library  2.10.0
reverse mode automatic differentiation
dirichlet_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_DIRICHLET_LOG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_DIRICHLET_LOG_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 
16  namespace math {
17 
43  template <bool propto,
44  typename T_prob, typename T_prior_sample_size>
45  typename boost::math::tools::promote_args<T_prob, T_prior_sample_size>::type
46  dirichlet_log(const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta,
47  const Eigen::Matrix
48  <T_prior_sample_size, Eigen::Dynamic, 1>& alpha) {
49  static const char* function("stan::math::dirichlet_log");
50  using boost::math::lgamma;
51  using boost::math::tools::promote_args;
56 
57  typename promote_args<T_prob, T_prior_sample_size>::type lp(0.0);
58  check_consistent_sizes(function,
59  "probabilities", theta,
60  "prior sample sizes", alpha);
61  check_positive(function, "prior sample sizes", alpha);
62  check_simplex(function, "probabilities", theta);
63 
65  lp += lgamma(alpha.sum());
66  for (int k = 0; k < alpha.rows(); ++k)
67  lp -= lgamma(alpha[k]);
68  }
70  for (int k = 0; k < theta.rows(); ++k)
71  lp += multiply_log(alpha[k]-1, theta[k]);
72  }
73  return lp;
74  }
75 
76  template <typename T_prob, typename T_prior_sample_size>
77  inline
78  typename boost::math::tools::promote_args<T_prob, T_prior_sample_size>::type
79  dirichlet_log(const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta,
80  const Eigen::Matrix
81  <T_prior_sample_size, Eigen::Dynamic, 1>& alpha) {
82  return dirichlet_log<false>(theta, alpha);
83  }
84  }
85 }
86 #endif
fvar< T > lgamma(const fvar< T > &x)
Definition: lgamma.hpp:15
boost::math::tools::promote_args< T_prob, T_prior_sample_size >::type dirichlet_log(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...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
bool check_positive(const char *function, const char *name, const T_y &y)
Return true if y is positive.
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)
bool check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Return true if the dimension of x1 is consistent with x2.
bool check_simplex(const char *function, const char *name, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta)
Return true if the specified vector is simplex.

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