Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
multinomial.hpp
Go to the documentation of this file.
1 #ifndef STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__DISCRETE__MULTINOMIAL_HPP
2 #define STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__DISCRETE__MULTINOMIAL_HPP
3 
4 #include <boost/math/special_functions/gamma.hpp>
5 #include <boost/random/uniform_01.hpp>
6 #include <boost/random/variate_generator.hpp>
7 
12 #include <stan/prob/constants.hpp>
15 #include <stan/prob/traits.hpp>
16 
17 namespace stan {
18 
19  namespace prob {
20  // Multinomial(ns|N,theta) [0 <= n <= N; SUM ns = N;
21  // 0 <= theta[n] <= 1; SUM theta = 1]
22  template <bool propto,
23  typename T_prob>
24  typename boost::math::tools::promote_args<T_prob>::type
25  multinomial_log(const std::vector<int>& ns,
26  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta) {
27  static const char* function = "stan::prob::multinomial_log(%1%)";
28 
32  using boost::math::tools::promote_args;
33  using boost::math::lgamma;
34 
35  typename promote_args<T_prob>::type lp(0.0);
36  check_nonnegative(function, ns, "Number of trials variable", &lp);
37  check_simplex(function, theta, "Probabilites parameter",
38  &lp);
39  check_size_match(function,
40  ns.size(), "Size of number of trials variable",
41  theta.rows(), "rows of probabilities parameter",
42  &lp);
44 
46  double sum = 1.0;
47  for (unsigned int i = 0; i < ns.size(); ++i)
48  sum += ns[i];
49  lp += lgamma(sum);
50  for (unsigned int i = 0; i < ns.size(); ++i)
51  lp -= lgamma(ns[i] + 1.0);
52  }
54  for (unsigned int i = 0; i < ns.size(); ++i)
55  lp += multiply_log(ns[i], theta[i]);
56  return lp;
57  }
58 
59  template <typename T_prob>
60  typename boost::math::tools::promote_args<T_prob>::type
61  multinomial_log(const std::vector<int>& ns,
62  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta) {
63  return multinomial_log<false>(ns,theta);
64  }
65 
66  template <class RNG>
67  inline std::vector<int>
68  multinomial_rng(const Eigen::Matrix<double,Eigen::Dynamic,1>& theta,
69  const int N,
70  RNG& rng) {
71  static const char* function = "stan::prob::multinomial_rng(%1%)";
74 
75  check_simplex(function, theta, "Probabilites parameter", (double*)0);
76  check_positive(function,N,"number of trials variables", (double*)0);
77 
78  std::vector<int> result(theta.size(),0);
79  double mass_left = 1.0;
80  int n_left = N;
81  for (int k = 0; n_left > 0 && k < theta.size(); ++k) {
82  double p = theta[k] / mass_left;
83  if (p > 1.0) p = 1.0;
84  result[k] = binomial_rng(n_left,p,rng);
85  n_left -= result[k];
86  mass_left -= theta[k];
87  }
88  return result;
89  }
90 
91 
92  }
93 }
94 #endif
bool check_size_match(const char *function, T_size1 i, const char *name_i, T_size2 j, const char *name_j, T_result *result)
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.
std::vector< int > multinomial_rng(const Eigen::Matrix< double, Eigen::Dynamic, 1 > &theta, const int N, RNG &rng)
Definition: multinomial.hpp:68
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
int binomial_rng(const int N, const double theta, RNG &rng)
Definition: binomial.hpp:470
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: traits.hpp:35
bool check_nonnegative(const char *function, const T_y &y, const char *name, 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)
boost::math::tools::promote_args< T_prob >::type multinomial_log(const std::vector< int > &ns, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta)
Definition: multinomial.hpp:25

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