Stan  1.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs 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>
6 #include <stan/agrad.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,
23  typename T_N,
24  typename T_a,
25  typename T_b,
26  class Policy>
27  double
28  hypergeometric_log(const T_n& n,
29  const T_N& N,
30  const T_a& a,
31  const T_b& b,
32  const Policy&) {
33  static const char* function = "stan::prob::hypergeometric_log(%1%)";
34 
40 
41  // check if any vectors are zero length
42  if (!(stan::length(n)
43  && stan::length(N)
44  && stan::length(a)
45  && stan::length(b)))
46  return 0.0;
47 
48 
49  VectorView<const T_n> n_vec(n);
50  VectorView<const T_N> N_vec(N);
51  VectorView<const T_a> a_vec(a);
52  VectorView<const T_b> b_vec(b);
53  size_t size = max_size(n, N, a, b);
54 
55  double logp(0.0);
56  if (!check_bounded(function, n, 0, a, "Successes variable", &logp, Policy()))
57  return logp;
58  if (!check_greater(function, N, n, "Draws parameter", &logp, Policy()))
59  return logp;
60  for (size_t i = 0; i < size; i++) {
61  if (!check_bounded(function, N_vec[i]-n_vec[i], 0, b_vec[i], "Draws parameter minus successes variable", &logp, Policy()))
62  return logp;
63  if (!check_bounded(function, N_vec[i], 0, a_vec[i]+b_vec[i], "Draws parameter", &logp, Policy()))
64  return logp;
65  }
66  if (!(check_consistent_sizes(function,
67  n,N,a,b,
68  "Successes variable","Draws parameter","Successes in population parameter","Failures in population parameter",
69  &logp, Policy())))
70  return logp;
71 
72  // check if no variables are involved and prop-to
74  return 0.0;
75 
76 
77  for (size_t i = 0; i < size; i++)
78  logp += math::binomial_coefficient_log(a_vec[i],n_vec[i])
79  + math::binomial_coefficient_log(b_vec[i],N_vec[i]-n_vec[i])
80  - math::binomial_coefficient_log(a_vec[i]+b_vec[i],N_vec[i]);
81  return logp;
82  }
83 
84 
85  template <bool propto,
86  typename T_n,
87  typename T_N,
88  typename T_a,
89  typename T_b>
90  inline
91  double
92  hypergeometric_log(const T_n& n,
93  const T_N& N,
94  const T_a& a,
95  const T_b& b) {
96  return hypergeometric_log<propto>(n,N,a,b,stan::math::default_policy());
97  }
98 
99  template <typename T_n,
100  typename T_N,
101  typename T_a,
102  typename T_b,
103  class Policy>
104  inline
105  double
106  hypergeometric_log(const T_n& n,
107  const T_N& N,
108  const T_a& a,
109  const T_b& b,
110  const Policy&) {
111  return hypergeometric_log<false>(n,N,a,b,Policy());
112  }
113 
114  template <typename T_n,
115  typename T_N,
116  typename T_a,
117  typename T_b>
118  inline
119  double
120  hypergeometric_log(const T_n& n,
121  const T_N& N,
122  const T_a& a,
123  const T_b& b) {
124  return hypergeometric_log<false>(n,N,a,b,stan::math::default_policy());
125  }
126 
127  template <class RNG>
128  inline int
129  hypergeometric_rng(const int N,
130  const int a,
131  const int b,
132  RNG& rng) {
133  using boost::variate_generator;
134 
135  boost::math::hypergeometric_distribution<>dist (b, N, a + b);
136  std::vector<double> index(a);
137  for(int i = 0; i < a; i++)
138  index[i] = cdf(dist, i + 1);
139 
140  double c = uniform_rng(0.0, 1.0, rng);
141  int min = 0;
142  int max = a - 1;
143  int mid = 0;
144  while(min < max) {
145  mid = (min + max) / 2;
146  if(index[mid] > c)
147  max = mid;
148  else
149  min = mid + 1;
150  }
151  return min + 1;
152  }
153  }
154 }
155 #endif

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