Stan  1.0
probability, sampling & optimization
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, ns.size(), theta.rows(), &lp, Policy()))
38  return lp;
40 
42  double sum = 1.0;
43  for (unsigned int i = 0; i < ns.size(); ++i)
44  sum += ns[i];
45  lp += lgamma(sum);
46  for (unsigned int i = 0; i < ns.size(); ++i)
47  lp -= lgamma(ns[i] + 1.0);
48  }
50  for (unsigned int i = 0; i < ns.size(); ++i)
51  lp += multiply_log(ns[i], theta[i]);
52  return lp;
53  }
54 
55 
56  template <bool propto,
57  typename T_prob>
58  typename boost::math::tools::promote_args<T_prob>::type
59  multinomial_log(const std::vector<int>& ns,
60  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta) {
61  return multinomial_log<propto>(ns,theta,stan::math::default_policy());
62  }
63 
64  template <typename T_prob,
65  class Policy>
66  typename boost::math::tools::promote_args<T_prob>::type
67  multinomial_log(const std::vector<int>& ns,
68  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta,
69  const Policy&) {
70  return multinomial_log<false>(ns,theta,Policy());
71  }
72 
73 
74  template <typename T_prob>
75  typename boost::math::tools::promote_args<T_prob>::type
76  multinomial_log(const std::vector<int>& ns,
77  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta) {
78  return multinomial_log<false>(ns,theta,stan::math::default_policy());
79  }
80 
81  }
82 }
83 #endif
var multiply_log(const var &a, const var &b)
Return the value of a*log(b).
var lgamma(const stan::agrad::var &a)
The log gamma function for variables (C99).
var sum(const Eigen::Matrix< var, R, C > &m)
Returns the sum of the coefficients of the specified matrix, column vector or row vector.
Definition: matrix.hpp:837
bool check_simplex(const char *function, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta, const char *name, T_result *result, const Policy &)
Return true if the specified vector is simplex.
boost::math::tools::promote_args< T_a, T_b >::type multiply_log(T_a a, T_b b)
bool check_nonnegative(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
boost::math::policies::policy default_policy
Default error-handling policy from Boost.
bool check_size_match(const char *function, T_size1 i, T_size2 j, T_result *result, const Policy &)
boost::math::tools::promote_args< T_prob >::type multinomial_log(const std::vector< int > &ns, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta, const Policy &)
Definition: multinomial.hpp:21
Probability, optimization and sampling library.
Definition: agrad.cpp:6
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: traits.hpp:33

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