| Stan Math Library 2.6.3 probability, sampling & optimization

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
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 <boost/math/distributions.hpp>
5 #include <stan/math/prim/scal/err/check_consistent_sizes.hpp>
6 #include <stan/math/prim/scal/err/check_bounded.hpp>
7 #include <stan/math/prim/scal/err/check_finite.hpp>
8 #include <stan/math/prim/scal/err/check_greater.hpp>
9 #include <stan/math/prim/scal/err/check_positive.hpp>
10 #include <stan/math/prim/scal/fun/binomial_coefficient_log.hpp>
11 #include <stan/math/prim/scal/meta/length.hpp>
12 #include <stan/math/prim/scal/meta/is_constant_struct.hpp>
13 #include <stan/math/prim/scal/meta/VectorView.hpp>
14 #include <stan/math/prim/scal/meta/VectorBuilder.hpp>
15 #include <stan/math/prim/scal/meta/partials_return_type.hpp>
16 #include <stan/math/prim/scal/meta/return_type.hpp>
17 #include <stan/math/prim/scal/meta/include_summand.hpp>
18 #include <stan/math/prim/scal/fun/constants.hpp>
19
20 namespace stan {
21
22 namespace math {
23
24 // Hypergeometric(n|N, a, b) [0 <= n <= a; 0 <= N-n <= b; 0 <= N <= a+b]
25 // n: #white balls drawn; N: #balls drawn;
26 // a: #white balls; b: #black balls
27 template <bool propto,
28 typename T_n, typename T_N,
29 typename T_a, typename T_b>
30 double
31 hypergeometric_log(const T_n& n, const T_N& N,
32 const T_a& a, const T_b& b) {
33 static const char* function("stan::math::hypergeometric_log");
34
35 using stan::math::check_finite;
36 using stan::math::check_bounded;
37 using stan::math::check_greater;
38 using stan::math::check_consistent_sizes;
39 using stan::math::include_summand;
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 check_bounded(function, "Successes variable", n, 0, a);
57 check_greater(function, "Draws parameter", N, n);
58 for (size_t i = 0; i < size; i++) {
59 check_bounded(function, "Draws parameter minus successes variable",
60 N_vec[i]-n_vec[i], 0, b_vec[i]);
61 check_bounded(function, "Draws parameter", N_vec[i], 0,
62 a_vec[i]+b_vec[i]);
63 }
64 check_consistent_sizes(function,
65 "Successes variable", n,
66 "Draws parameter", N,
67 "Successes in population parameter", a,
68 "Failures in population parameter", b);
69
70 // check if no variables are involved and prop-to
71 if (!include_summand<propto>::value)
72 return 0.0;
73
74
75 for (size_t i = 0; i < size; i++)
76 logp += math::binomial_coefficient_log(a_vec[i], n_vec[i])
77 + math::binomial_coefficient_log(b_vec[i], N_vec[i]-n_vec[i])
78 - math::binomial_coefficient_log(a_vec[i]+b_vec[i], N_vec[i]);
79 return logp;
80 }
81
82 template <typename T_n,
83 typename T_N,
84 typename T_a,
85 typename T_b>
86 inline
87 double
88 hypergeometric_log(const T_n& n,
89 const T_N& N,
90 const T_a& a,
91 const T_b& b) {
92 return hypergeometric_log<false>(n, N, a, b);
93 }
94 }
95 }
96 #endif
stan::math::binomial_coefficient_log
fvar< T > binomial_coefficient_log(const fvar< T > &x1, const fvar< T > &x2)
Definition: binomial_coefficient_log.hpp:16
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:31
check_positive.hpp
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:36
VectorView.hpp
include_summand.hpp
check_greater.hpp
stan::max_size
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
VectorBuilder.hpp
partials_return_type.hpp
check_bounded.hpp
length.hpp
stan::math::check_finite
bool check_finite(const char *function, const char *name, const T_y &y)
Return true if y is finite.
Definition: check_finite.hpp:62
constants.hpp
stan::math::size
int size(const std::vector< T > &x)
Definition: size.hpp:11
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
is_constant_struct.hpp
stan::VectorView
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
Definition: VectorView.hpp:41
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–2015, Stan Development Team.