Automatic Differentiation
 
Loading...
Searching...
No Matches
hypergeometric_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_HYPERGEOMETRIC_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_HYPERGEOMETRIC_LPMF_HPP
3
10
11namespace stan {
12namespace 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
17template <bool propto, typename T_n, typename T_N, typename T_a, typename T_b>
18double hypergeometric_lpmf(const T_n& n, const T_N& N, const T_a& a,
19 const T_b& b) {
20 static constexpr const char* function = "hypergeometric_lpmf";
21 check_bounded(function, "Successes variable", value_of(n), 0, a);
22 check_consistent_sizes(function, "Successes variable", n, "Draws parameter",
23 N, "Successes in population parameter", a,
24 "Failures in population parameter", b);
25 check_greater_or_equal(function, "Draws parameter", N, n);
26
27 if (size_zero(n, N, a, b)) {
28 return 0.0;
29 }
30
31 double logp(0.0);
32
33 scalar_seq_view<T_n> n_vec(n);
34 scalar_seq_view<T_N> N_vec(N);
35 scalar_seq_view<T_a> a_vec(a);
36 scalar_seq_view<T_b> b_vec(b);
37 size_t max_size_seq_view = max_size(n, N, a, b);
38
39 for (size_t i = 0; i < max_size_seq_view; i++) {
40 check_bounded(function, "Draws parameter minus successes variable",
41 N_vec[i] - n_vec[i], 0, b_vec[i]);
42 check_bounded(function, "Draws parameter", N_vec[i], 0,
43 a_vec[i] + b_vec[i]);
44 }
45
47 return 0.0;
48 }
49
50 for (size_t i = 0; i < max_size_seq_view; i++) {
51 logp += math::binomial_coefficient_log(a_vec[i], n_vec[i])
52 + math::binomial_coefficient_log(b_vec[i], N_vec[i] - n_vec[i])
53 - math::binomial_coefficient_log(a_vec[i] + b_vec[i], N_vec[i]);
54 }
55 return logp;
56}
57
58template <typename T_n, typename T_N, typename T_a, typename T_b>
59inline double hypergeometric_lpmf(const T_n& n, const T_N& N, const T_a& a,
60 const T_b& b) {
61 return hypergeometric_lpmf<false>(n, N, a, b);
62}
63
64} // namespace math
65} // namespace stan
66#endif
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
binomial_coefficient_log_< as_operation_cl_t< T1 >, as_operation_cl_t< T2 > > binomial_coefficient_log(T1 &&a, T2 &&b)
double hypergeometric_lpmf(const T_n &n, const T_N &N, const T_a &a, const T_b &b)
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
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.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
void check_greater_or_equal(const char *function, const char *name, const T_y &y, const T_low &low, Idxs... idxs)
Throw an exception if y is not greater or equal than low.
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...