Stan  1.1
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs 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 <stan/prob/traits.hpp>
10 
11 
12 namespace stan {
13 
14  namespace prob {
15  // Multinomial(ns|N,theta) [0 <= n <= N; SUM ns = N;
16  // 0 <= theta[n] <= 1; SUM theta = 1]
17  template <bool propto,
18  typename T_prob,
19  class Policy>
20  typename boost::math::tools::promote_args<T_prob>::type
21  multinomial_log(const std::vector<int>& ns,
22  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta,
23  const Policy&) {
24  static const char* function = "stan::prob::multinomial_log(%1%)";
25 
29  using boost::math::tools::promote_args;
30 
31  typename promote_args<T_prob>::type lp(0.0);
32  if (!check_nonnegative(function, ns, "Number of trials variable", &lp, Policy()))
33  return lp;
34  if (!check_simplex(function, theta, "Probabilites parameter",
35  &lp, Policy()))
36  return lp;
37  if (!check_size_match(function,
38  ns.size(), "Size of number of trials variable",
39  theta.rows(), "rows of probabilities parameter",
40  &lp, Policy()))
41  return lp;
43 
45  double sum = 1.0;
46  for (unsigned int i = 0; i < ns.size(); ++i)
47  sum += ns[i];
48  lp += lgamma(sum);
49  for (unsigned int i = 0; i < ns.size(); ++i)
50  lp -= lgamma(ns[i] + 1.0);
51  }
53  for (unsigned int i = 0; i < ns.size(); ++i)
54  lp += multiply_log(ns[i], theta[i]);
55  return lp;
56  }
57 
58 
59  template <bool propto,
60  typename T_prob>
61  typename boost::math::tools::promote_args<T_prob>::type
62  multinomial_log(const std::vector<int>& ns,
63  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta) {
64  return multinomial_log<propto>(ns,theta,stan::math::default_policy());
65  }
66 
67  template <typename T_prob,
68  class Policy>
69  typename boost::math::tools::promote_args<T_prob>::type
70  multinomial_log(const std::vector<int>& ns,
71  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta,
72  const Policy&) {
73  return multinomial_log<false>(ns,theta,Policy());
74  }
75 
76 
77  template <typename T_prob>
78  typename boost::math::tools::promote_args<T_prob>::type
79  multinomial_log(const std::vector<int>& ns,
80  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta) {
81  return multinomial_log<false>(ns,theta,stan::math::default_policy());
82  }
83 
84  }
85 }
86 #endif

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