| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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>
7 #include <stan/math/prim/mat/err/check_simplex.hpp>
8 #include <stan/math/prim/scal/err/check_consistent_sizes.hpp>
9 #include <stan/math/prim/scal/err/check_positive.hpp>
10 #include <stan/math/prim/scal/fun/multiply_log.hpp>
11 #include <stan/math/prim/scal/fun/constants.hpp>
12 #include <stan/math/prim/scal/meta/include_summand.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_log(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_log");
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
59 if (include_summand<propto, T_prior_sample_size>::value) {
60 lp += lgamma(alpha.sum());
61 for (int k = 0; k < alpha.rows(); ++k)
62 lp -= lgamma(alpha[k]);
63 }
64 if (include_summand<propto, T_prob, T_prior_sample_size>::value) {
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_log(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_log<false>(theta, alpha);
78 }
79
80 }
81 }
82 #endif
check_simplex.hpp
stan::math::lgamma
fvar< T > lgamma(const fvar< T > &x)
Definition: lgamma.hpp:14
check_positive.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::dirichlet_log
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...
Definition: dirichlet_log.hpp:45
stan::math::include_summand
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: include_summand.hpp:35
include_summand.hpp
multiply_log.hpp
stan::math::check_positive
bool check_positive(const char *function, const char *name, const T_y &y)
Return true if y is positive.
Definition: check_positive.hpp:66
stan::math::multiply_log
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)
Definition: multiply_log.hpp:14
constants.hpp
stan::math::check_consistent_sizes
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.
Definition: check_consistent_sizes.hpp:31
stan::math::check_simplex
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.
Definition: check_simplex.hpp:40
check_consistent_sizes.hpp

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