Stan  1.0
probability, sampling & optimization
hypergeometric.hpp
Go to the documentation of this file.
1 #ifndef __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__DISCRETE__HYPERGEOMETRIC_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__DISCRETE__HYPERGEOMETRIC_HPP__
3 
4 #include <stan/agrad.hpp>
7 #include <stan/meta/traits.hpp>
8 #include <stan/prob/traits.hpp>
10 
11 namespace stan {
12 
13  namespace prob {
14 
15  // Hypergeometric(n|N,a,b) [0 <= n <= a; 0 <= N-n <= b; 0 <= N <= a+b]
16  // n: #white balls drawn; N: #balls drawn;
17  // a: #white balls; b: #black balls
18  template <bool propto,
19  typename T_n,
20  typename T_N,
21  typename T_a,
22  typename T_b,
23  class Policy>
24  double
25  hypergeometric_log(const T_n& n,
26  const T_N& N,
27  const T_a& a,
28  const T_b& b,
29  const Policy&) {
30  static const char* function = "stan::prob::hypergeometric_log(%1%)";
31 
37 
38  // check if any vectors are zero length
39  if (!(stan::length(n)
40  && stan::length(N)
41  && stan::length(a)
42  && stan::length(b)))
43  return 0.0;
44 
45 
46  VectorView<const T_n> n_vec(n);
47  VectorView<const T_N> N_vec(N);
48  VectorView<const T_a> a_vec(a);
49  VectorView<const T_b> b_vec(b);
50  size_t size = max_size(n, N, a, b);
51 
52  double logp(0.0);
53  if (!check_bounded(function, n, 0, a, "Successes variable", &logp, Policy()))
54  return logp;
55  if (!check_greater(function, N, n, "Draws parameter", &logp, Policy()))
56  return logp;
57  for (size_t i = 0; i < size; i++) {
58  if (!check_bounded(function, N_vec[i]-n_vec[i], 0, b_vec[i], "Draws parameter minus successes variable", &logp, Policy()))
59  return logp;
60  if (!check_bounded(function, N_vec[i], 0, a_vec[i]+b_vec[i], "Draws parameter", &logp, Policy()))
61  return logp;
62  }
63  if (!(check_consistent_sizes(function,
64  n,N,a,b,
65  "Successes variable","Draws parameter","Successes in population parameter","Failures in population parameter",
66  &logp, Policy())))
67  return logp;
68 
69  // check if no variables are involved and prop-to
71  return 0.0;
72 
73 
74  for (size_t i = 0; i < size; i++)
75  logp += math::binomial_coefficient_log(a_vec[i],n_vec[i])
76  + math::binomial_coefficient_log(b_vec[i],N_vec[i]-n_vec[i])
77  - math::binomial_coefficient_log(a_vec[i]+b_vec[i],N_vec[i]);
78  return logp;
79  }
80 
81 
82  template <bool propto,
83  typename T_n,
84  typename T_N,
85  typename T_a,
86  typename T_b>
87  inline
88  double
89  hypergeometric_log(const T_n& n,
90  const T_N& N,
91  const T_a& a,
92  const T_b& b) {
93  return hypergeometric_log<propto>(n,N,a,b,stan::math::default_policy());
94  }
95 
96  template <typename T_n,
97  typename T_N,
98  typename T_a,
99  typename T_b,
100  class Policy>
101  inline
102  double
103  hypergeometric_log(const T_n& n,
104  const T_N& N,
105  const T_a& a,
106  const T_b& b,
107  const Policy&) {
108  return hypergeometric_log<false>(n,N,a,b,Policy());
109  }
110 
111  template <typename T_n,
112  typename T_N,
113  typename T_a,
114  typename T_b>
115  inline
116  double
117  hypergeometric_log(const T_n& n,
118  const T_N& N,
119  const T_a& a,
120  const T_b& b) {
121  return hypergeometric_log<false>(n,N,a,b,stan::math::default_policy());
122  }
123 
124 
125  }
126 }
127 #endif
boost::math::tools::promote_args< T_N, T_n >::type binomial_coefficient_log(T_N N, T_n n)
Return the log of the binomial coefficient for the specified arguments.
bool check_greater(const char *function, const T_y &y, const T_low &low, const char *name, T_result *result, const Policy &)
bool check_consistent_sizes(const char *function, const T1 &x1, const T2 &x2, const char *name1, const char *name2, T_result *result, const Policy &)
bool check_bounded(const char *function, const T_y &y, const T_low &low, const T_high &high, const char *name, T_result *result, const Policy &)
bool check_finite(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
Checks if the variable y is finite.
boost::math::policies::policy default_policy
Default error-handling policy from Boost.
double hypergeometric_log(const T_n &n, const T_N &N, const T_a &a, const T_b &b, const Policy &)
Probability, optimization and sampling library.
Definition: agrad.cpp:6
size_t length(const T &x)
Definition: traits.hpp:111
size_t max_size(const T1 &x1, const T2 &x2)
Definition: traits.hpp:148
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.