| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

hypergeometric_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_HYPERGEOMETRIC_LOG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_HYPERGEOMETRIC_LOG_HPP
3
4 #include <stan/math/prim/scal/err/check_consistent_sizes.hpp>
5 #include <stan/math/prim/scal/err/check_bounded.hpp>
6 #include <stan/math/prim/scal/err/check_finite.hpp>
7 #include <stan/math/prim/scal/err/check_greater.hpp>
8 #include <stan/math/prim/scal/err/check_positive.hpp>
9 #include <stan/math/prim/scal/fun/binomial_coefficient_log.hpp>
10 #include <stan/math/prim/scal/meta/length.hpp>
11 #include <stan/math/prim/scal/meta/VectorView.hpp>
12 #include <stan/math/prim/scal/meta/VectorBuilder.hpp>
13 #include <stan/math/prim/scal/meta/return_type.hpp>
14 #include <stan/math/prim/scal/meta/include_summand.hpp>
15 #include <stan/math/prim/scal/fun/constants.hpp>
16 #include <boost/math/distributions.hpp>
17
18 namespace stan {
19 namespace math {
20
21 // Hypergeometric(n|N, a, b) [0 <= n <= a; 0 <= N-n <= b; 0 <= N <= a+b]
22 // n: #white balls drawn; N: #balls drawn;
23 // a: #white balls; b: #black balls
24 template <bool propto,
25 typename T_n, typename T_N,
26 typename T_a, typename T_b>
27 double
28 hypergeometric_log(const T_n& n, const T_N& N,
29 const T_a& a, const T_b& b) {
30 static const char* function("hypergeometric_log");
31
32 if (!(stan::length(n)
33 && stan::length(N)
34 && stan::length(a)
35 && stan::length(b)))
36 return 0.0;
37
38 VectorView<const T_n> n_vec(n);
39 VectorView<const T_N> N_vec(N);
40 VectorView<const T_a> a_vec(a);
41 VectorView<const T_b> b_vec(b);
42 size_t size = max_size(n, N, a, b);
43
44 double logp(0.0);
45 check_bounded(function, "Successes variable", n, 0, a);
46 check_greater(function, "Draws parameter", N, n);
47 for (size_t i = 0; i < size; i++) {
48 check_bounded(function, "Draws parameter minus successes variable",
49 N_vec[i]-n_vec[i], 0, b_vec[i]);
50 check_bounded(function, "Draws parameter", N_vec[i], 0,
51 a_vec[i]+b_vec[i]);
52 }
53 check_consistent_sizes(function,
54 "Successes variable", n,
55 "Draws parameter", N,
56 "Successes in population parameter", a,
57 "Failures in population parameter", b);
58
59 if (!include_summand<propto>::value)
60 return 0.0;
61
62 for (size_t i = 0; i < size; i++)
63 logp += math::binomial_coefficient_log(a_vec[i], n_vec[i])
64 + math::binomial_coefficient_log(b_vec[i], N_vec[i]-n_vec[i])
65 - math::binomial_coefficient_log(a_vec[i]+b_vec[i], N_vec[i]);
66 return logp;
67 }
68
69 template <typename T_n,
70 typename T_N,
71 typename T_a,
72 typename T_b>
73 inline
74 double
75 hypergeometric_log(const T_n& n,
76 const T_N& N,
77 const T_a& a,
78 const T_b& b) {
79 return hypergeometric_log<false>(n, N, a, b);
80 }
81
82 }
83 }
84 #endif
stan::math::binomial_coefficient_log
fvar< T > binomial_coefficient_log(const fvar< T > &x1, const fvar< T > &x2)
Definition: binomial_coefficient_log.hpp:15
stan::math::hypergeometric_log
double hypergeometric_log(const T_n &n, const T_N &N, const T_a &a, const T_b &b)
Definition: hypergeometric_log.hpp:28
check_positive.hpp
stan
Definition: log_sum_exp.hpp:8
stan::length
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
stan::math::check_bounded
bool check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Return true if the value is between the low and high values, inclusively.
Definition: check_bounded.hpp:95
binomial_coefficient_log.hpp
return_type.hpp
stan::math::include_summand
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: include_summand.hpp:35
include_summand.hpp
check_greater.hpp
VectorView.hpp
stan::max_size
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
VectorBuilder.hpp
check_bounded.hpp
length.hpp
constants.hpp
stan::math::size
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
stan::math::check_consistent_sizes
bool check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Return true if the dimension of x1 is consistent with x2.
Definition: check_consistent_sizes.hpp:31
check_finite.hpp
stan::VectorView
VectorView is a template expression that is constructed with a container or scalar, which it then allows to be used as an array using operator[].
Definition: VectorView.hpp:48
check_consistent_sizes.hpp
stan::math::check_greater
bool check_greater(const char *function, const char *name, const T_y &y, const T_low &low)
Return true if y is strictly greater than low.
Definition: check_greater.hpp:84

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