Stan Math Library  2.20.0
reverse mode automatic differentiation
hypergeometric_lpmf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_HYPERGEOMETRIC_LPMF_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_HYPERGEOMETRIC_LPMF_HPP
3 
10 
11 namespace stan {
12 namespace math {
13 
14 // Hypergeometric(n|N, a, b) [0 <= n <= a; 0 <= N-n <= b; 0 <= N <= a+b]
15 // n: #white balls drawn; N: #balls drawn;
16 // a: #white balls; b: #black balls
17 template <bool propto, typename T_n, typename T_N, typename T_a, typename T_b>
18 double hypergeometric_lpmf(const T_n& n, const T_N& N, const T_a& a,
19  const T_b& b) {
20  static const char* function = "hypergeometric_lpmf";
21 
22  if (size_zero(n, N, a, b))
23  return 0.0;
24 
25  scalar_seq_view<T_n> n_vec(n);
26  scalar_seq_view<T_N> N_vec(N);
27  scalar_seq_view<T_a> a_vec(a);
28  scalar_seq_view<T_b> b_vec(b);
29  size_t size = max_size(n, N, a, b);
30 
31  double logp(0.0);
32  check_bounded(function, "Successes variable", n, 0, a);
33  check_greater(function, "Draws parameter", N, n);
34  for (size_t i = 0; i < size; i++) {
35  check_bounded(function, "Draws parameter minus successes variable",
36  N_vec[i] - n_vec[i], 0, b_vec[i]);
37  check_bounded(function, "Draws parameter", N_vec[i], 0,
38  a_vec[i] + b_vec[i]);
39  }
40  check_consistent_sizes(function, "Successes variable", n, "Draws parameter",
41  N, "Successes in population parameter", a,
42  "Failures in population parameter", b);
43 
45  return 0.0;
46 
47  for (size_t i = 0; i < size; i++)
48  logp += math::binomial_coefficient_log(a_vec[i], n_vec[i])
49  + math::binomial_coefficient_log(b_vec[i], N_vec[i] - n_vec[i])
50  - math::binomial_coefficient_log(a_vec[i] + b_vec[i], N_vec[i]);
51  return logp;
52 }
53 
54 template <typename T_n, typename T_N, typename T_a, typename T_b>
55 inline double hypergeometric_lpmf(const T_n& n, const T_N& N, const T_a& a,
56  const T_b& b) {
57  return hypergeometric_lpmf<false>(n, N, a, b);
58 }
59 
60 } // namespace math
61 } // namespace stan
62 #endif
fvar< T > binomial_coefficient_log(const fvar< T > &x1, const fvar< T > &x2)
void check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Check if the value is between the low and high values, inclusively.
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
bool size_zero(T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition: size_zero.hpp:18
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
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
void check_greater(const char *function, const char *name, const T_y &y, const T_low &low)
Check if y is strictly greater than low.
double hypergeometric_lpmf(const T_n &n, const T_N &N, const T_a &a, const T_b &b)
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Check if the dimension of x1 is consistent with x2.

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