Stan Math Library  2.10.0
reverse mode automatic differentiation
hypergeometric_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_HYPERGEOMETRIC_LOG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_HYPERGEOMETRIC_LOG_HPP
3 
16 #include <boost/math/distributions.hpp>
17 
18 namespace stan {
19 
20  namespace math {
21 
22  // Hypergeometric(n|N, a, b) [0 <= n <= a; 0 <= N-n <= b; 0 <= N <= a+b]
23  // n: #white balls drawn; N: #balls drawn;
24  // a: #white balls; b: #black balls
25  template <bool propto,
26  typename T_n, typename T_N,
27  typename T_a, typename T_b>
28  double
29  hypergeometric_log(const T_n& n, const T_N& N,
30  const T_a& a, const T_b& b) {
31  static const char* function("stan::math::hypergeometric_log");
32 
38 
39  // check if any vectors are zero length
40  if (!(stan::length(n)
41  && stan::length(N)
42  && stan::length(a)
43  && stan::length(b)))
44  return 0.0;
45 
46 
47  VectorView<const T_n> n_vec(n);
48  VectorView<const T_N> N_vec(N);
49  VectorView<const T_a> a_vec(a);
50  VectorView<const T_b> b_vec(b);
51  size_t size = max_size(n, N, a, b);
52 
53  double logp(0.0);
54  check_bounded(function, "Successes variable", n, 0, a);
55  check_greater(function, "Draws parameter", N, n);
56  for (size_t i = 0; i < size; i++) {
57  check_bounded(function, "Draws parameter minus successes variable",
58  N_vec[i]-n_vec[i], 0, b_vec[i]);
59  check_bounded(function, "Draws parameter", N_vec[i], 0,
60  a_vec[i]+b_vec[i]);
61  }
62  check_consistent_sizes(function,
63  "Successes variable", n,
64  "Draws parameter", N,
65  "Successes in population parameter", a,
66  "Failures in population parameter", b);
67 
68  // check if no variables are involved and prop-to
70  return 0.0;
71 
72 
73  for (size_t i = 0; i < size; i++)
74  logp += math::binomial_coefficient_log(a_vec[i], n_vec[i])
75  + math::binomial_coefficient_log(b_vec[i], N_vec[i]-n_vec[i])
76  - math::binomial_coefficient_log(a_vec[i]+b_vec[i], N_vec[i]);
77  return logp;
78  }
79 
80  template <typename T_n,
81  typename T_N,
82  typename T_a,
83  typename T_b>
84  inline
85  double
86  hypergeometric_log(const T_n& n,
87  const T_N& N,
88  const T_a& a,
89  const T_b& b) {
90  return hypergeometric_log<false>(n, N, a, b);
91  }
92  }
93 }
94 #endif
fvar< T > binomial_coefficient_log(const fvar< T > &x1, const fvar< T > &x2)
double hypergeometric_log(const T_n &n, const T_N &N, const T_a &a, const T_b &b)
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
bool check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Return true if the value is between the low and high values, inclusively.
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
bool check_finite(const char *function, const char *name, const T_y &y)
Return true if y is finite.
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
bool check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Return true if the dimension of x1 is consistent with x2.
VectorView is a template expression that is constructed with a container or scalar, which it then allows to be used as an array using operator[].
Definition: VectorView.hpp:48
bool check_greater(const char *function, const char *name, const T_y &y, const T_low &low)
Return true if y is strictly greater than low.

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