Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 <boost/math/distributions.hpp>
7 #include <stan/math.hpp>
9 #include <stan/meta/traits.hpp>
10 #include <stan/prob/traits.hpp>
11 #include <stan/prob/constants.hpp>
12 #include <vector>
13 
14 namespace stan {
15 
16  namespace prob {
17 
18  // Hypergeometric(n|N,a,b) [0 <= n <= a; 0 <= N-n <= b; 0 <= N <= a+b]
19  // n: #white balls drawn; N: #balls drawn;
20  // a: #white balls; b: #black balls
21  template <bool propto,
22  typename T_n, typename T_N,
23  typename T_a, typename T_b>
24  double
25  hypergeometric_log(const T_n& n, const T_N& N,
26  const T_a& a, const T_b& b) {
27  static const char* function = "stan::prob::hypergeometric_log(%1%)";
28 
34 
35  // check if any vectors are zero length
36  if (!(stan::length(n)
37  && stan::length(N)
38  && stan::length(a)
39  && stan::length(b)))
40  return 0.0;
41 
42 
43  VectorView<const T_n> n_vec(n);
44  VectorView<const T_N> N_vec(N);
45  VectorView<const T_a> a_vec(a);
46  VectorView<const T_b> b_vec(b);
47  size_t size = max_size(n, N, a, b);
48 
49  double logp(0.0);
50  check_bounded(function, n, 0, a, "Successes variable", &logp);
51  check_greater(function, N, n, "Draws parameter", &logp);
52  for (size_t i = 0; i < size; i++) {
53  check_bounded(function, N_vec[i]-n_vec[i], 0, b_vec[i],
54  "Draws parameter minus successes variable", &logp);
55  check_bounded(function, N_vec[i], 0, a_vec[i]+b_vec[i],
56  "Draws parameter", &logp);
57  }
58  check_consistent_sizes(function,
59  n,N,a,b,
60  "Successes variable","Draws parameter",
61  "Successes in population parameter",
62  "Failures in population parameter",
63  &logp);
64 
65  // check if no variables are involved and prop-to
67  return 0.0;
68 
69 
70  for (size_t i = 0; i < size; i++)
71  logp += math::binomial_coefficient_log(a_vec[i],n_vec[i])
72  + math::binomial_coefficient_log(b_vec[i],N_vec[i]-n_vec[i])
73  - math::binomial_coefficient_log(a_vec[i]+b_vec[i],N_vec[i]);
74  return logp;
75  }
76 
77  template <typename T_n,
78  typename T_N,
79  typename T_a,
80  typename T_b>
81  inline
82  double
83  hypergeometric_log(const T_n& n,
84  const T_N& N,
85  const T_a& a,
86  const T_b& b) {
87  return hypergeometric_log<false>(n,N,a,b);
88  }
89 
90  template <class RNG>
91  inline int
92  hypergeometric_rng(const int N,
93  const int a,
94  const int b,
95  RNG& rng) {
96  using boost::variate_generator;
97 
98  static const char* function = "stan::prob::hypergeometric_rng(%1%)";
99 
102 
103  check_bounded(function, N, 0, a+b, "Draws parameter", (int*)0);
104  check_positive(function,N,"Draws parameter", (int*)0);
105  check_positive(function,a,"Successes in population parameter", (int*)0);
106  check_positive(function,b,"Failures in population parameter", (int*)0);
107 
108  boost::math::hypergeometric_distribution<>dist (b, N, a + b);
109  std::vector<double> index(a);
110  for(int i = 0; i < a; i++)
111  index[i] = cdf(dist, i + 1);
112 
113  double c = uniform_rng(0.0, 1.0, rng);
114  int min = 0;
115  int max = a - 1;
116  int mid = 0;
117  while(min < max) {
118  mid = (min + max) / 2;
119  if(index[mid] > c)
120  max = mid;
121  else
122  min = mid + 1;
123  }
124  return min + 1;
125  }
126  }
127 }
128 #endif
bool check_bounded(const char *function, const T_y &y, const T_low &low, const T_high &high, const char *name, T_result *result)
bool check_greater(const char *function, const T_y &y, const T_low &low, const char *name, T_result *result)
double max(const double a, const double b)
Definition: max.hpp:7
size_t length(const T &)
Definition: traits.hpp:159
boost::math::tools::promote_args< T_N, T_n >::type binomial_coefficient_log(const T_N N, const T_n n)
Return the log of the binomial coefficient for the specified arguments.
bool check_finite(const char *function, const T_y &y, const char *name, T_result *result)
Checks if the variable y is finite.
double hypergeometric_log(const T_n &n, const T_N &N, const T_a &a, const T_b &b)
int hypergeometric_rng(const int N, const int a, const int b, RNG &rng)
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: traits.hpp:35
double uniform_rng(const double alpha, const double beta, RNG &rng)
Definition: uniform.hpp:339
bool check_consistent_sizes(const char *function, const T1 &x1, const T2 &x2, const char *name1, const char *name2, T_result *result)
size_t max_size(const T1 &x1, const T2 &x2)
Definition: traits.hpp:191
bool check_positive(const char *function, const T_y &y, const char *name, T_result *result)
int size(const std::vector< T > &x)
Definition: size.hpp:11
double min(const double a, const double b)
Definition: min.hpp:7
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
Definition: traits.hpp:275
double dist(const std::vector< double > &x, const std::vector< double > &y)
Definition: dist.hpp:11

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