Stan  1.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs 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  class Policy>
46  typename boost::math::tools::promote_args<T_prob,T_prior_sample_size>::type
47  dirichlet_log(const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta,
48  const Eigen::Matrix<T_prior_sample_size,Eigen::Dynamic,1>& alpha,
49  const Policy&) {
50  // FIXME: parameter check
51  using boost::math::lgamma;
52  using boost::math::tools::promote_args;
53  typename promote_args<T_prob,T_prior_sample_size>::type lp(0.0);
54 
57  lp += lgamma(alpha.sum());
58  for (int k = 0; k < alpha.rows(); ++k)
59  lp -= lgamma(alpha[k]);
60  }
62  for (int k = 0; k < theta.rows(); ++k)
63  lp += multiply_log(alpha[k]-1, theta[k]);
64  return lp;
65  }
66 
67  template <bool propto,
68  typename T_prob, typename T_prior_sample_size>
69  inline
70  typename boost::math::tools::promote_args<T_prob,T_prior_sample_size>::type
71  dirichlet_log(const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta,
72  const Eigen::Matrix<T_prior_sample_size,Eigen::Dynamic,1>& alpha) {
73  return dirichlet_log<propto>(theta,alpha,stan::math::default_policy());
74  }
75 
76 
77  template <typename T_prob, typename T_prior_sample_size,
78  class Policy>
79  inline
80  typename boost::math::tools::promote_args<T_prob,T_prior_sample_size>::type
81  dirichlet_log(const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta,
82  const Eigen::Matrix<T_prior_sample_size,Eigen::Dynamic,1>& alpha,
83  const Policy&) {
84  return dirichlet_log<false>(theta,alpha,Policy());
85  }
86 
87  template <typename T_prob, typename T_prior_sample_size>
88  inline
89  typename boost::math::tools::promote_args<T_prob,T_prior_sample_size>::type
90  dirichlet_log(const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta,
91  const Eigen::Matrix<T_prior_sample_size,Eigen::Dynamic,1>& alpha) {
92  return dirichlet_log<false>(theta,alpha,stan::math::default_policy());
93  }
94 
95  template <class RNG>
96  inline Eigen::VectorXd
97  dirichlet_rng(const Eigen::Matrix<double,Eigen::Dynamic,1>& alpha,
98  RNG& rng) {
99  using boost::variate_generator;
100  using boost::gamma_distribution;
101 
102  double sum = 0;
103  Eigen::VectorXd y(alpha.rows());
104  for(int i = 0; i < alpha.rows(); i++) {
105  variate_generator<RNG&, gamma_distribution<> >
106  gamma_rng(rng, gamma_distribution<>(alpha(i,0),1));
107  y(i) = gamma_rng();
108  sum += y(i);
109  }
110 
111  for(int i = 0; i < alpha.rows(); i++)
112  y(i) /= sum;
113  return y;
114  }
115  }
116 }
117 #endif

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