Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
dirichlet.hpp
Go to the documentation of this file.
1 #ifndef STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__CONTINUOUS__DIRICHLET_HPP
2 #define STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__CONTINUOUS__DIRICHLET_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 
11 #include <stan/prob/traits.hpp>
13 
14 namespace stan {
15 
16  namespace prob {
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<T_prior_sample_size,Eigen::Dynamic,1>& alpha) {
48  static const char* function = "stan::prob::dirichlet_log(%1%)";
49  using boost::math::lgamma;
50  using boost::math::tools::promote_args;
55 
56  typename promote_args<T_prob,T_prior_sample_size>::type lp(0.0);
57  check_consistent_sizes(function, theta, alpha,
58  "probabilities", "prior sample sizes",
59  &lp);
60  check_positive(function, alpha, "prior sample sizes", &lp);
61  check_simplex(function, theta, "probabilities", &lp);
62 
64  lp += lgamma(alpha.sum());
65  for (int k = 0; k < alpha.rows(); ++k)
66  lp -= lgamma(alpha[k]);
67  }
69  for (int k = 0; k < theta.rows(); ++k)
70  lp += multiply_log(alpha[k]-1, theta[k]);
71  return lp;
72  }
73 
74  template <typename T_prob, typename T_prior_sample_size>
75  inline
76  typename boost::math::tools::promote_args<T_prob,T_prior_sample_size>::type
77  dirichlet_log(const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta,
78  const Eigen::Matrix<T_prior_sample_size,Eigen::Dynamic,1>& alpha) {
79  return dirichlet_log<false>(theta,alpha);
80  }
81 
82  template <class RNG>
83  inline Eigen::VectorXd
84  dirichlet_rng(const Eigen::Matrix<double,Eigen::Dynamic,1>& alpha,
85  RNG& rng) {
86  using boost::variate_generator;
87  using boost::gamma_distribution;
88 
89  double sum = 0;
90  Eigen::VectorXd y(alpha.rows());
91  for(int i = 0; i < alpha.rows(); i++) {
92  variate_generator<RNG&, gamma_distribution<> >
93  gamma_rng(rng, gamma_distribution<>(alpha(i,0),1));
94  y(i) = gamma_rng();
95  sum += y(i);
96  }
97 
98  for(int i = 0; i < alpha.rows(); i++)
99  y(i) /= sum;
100  return y;
101  }
102  }
103 }
104 #endif
boost::math::tools::promote_args< T_a, T_b >::type multiply_log(const T_a a, const T_b b)
Calculated the value of the first argument times log of the second argument while behaving properly w...
bool check_simplex(const char *function, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta, const char *name, T_result *result)
Return true if the specified vector is simplex.
fvar< T > sum(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: sum.hpp:14
fvar< T > lgamma(const fvar< T > &x)
Definition: lgamma.hpp:15
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: traits.hpp:35
Eigen::VectorXd dirichlet_rng(const Eigen::Matrix< double, Eigen::Dynamic, 1 > &alpha, RNG &rng)
Definition: dirichlet.hpp:84
bool check_consistent_sizes(const char *function, const T1 &x1, const T2 &x2, const char *name1, const char *name2, T_result *result)
bool check_positive(const char *function, const T_y &y, const char *name, T_result *result)
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)
double gamma_rng(const double alpha, const double beta, RNG &rng)
Definition: gamma.hpp:491
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.hpp:46

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