Stan Math Library: stan/math/prim/scal/prob/beta_binomial_cdf.hpp Source File

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

beta_binomial_cdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BETA_BINOMIAL_CDF_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_BETA_BINOMIAL_CDF_HPP
3
4 #include <stan/math/prim/scal/meta/is_constant_struct.hpp>
5 #include <stan/math/prim/scal/meta/partials_return_type.hpp>
6 #include <stan/math/prim/scal/meta/OperandsAndPartials.hpp>
7 #include <stan/math/prim/scal/err/check_consistent_sizes.hpp>
8 #include <stan/math/prim/scal/err/check_nonnegative.hpp>
9 #include <stan/math/prim/scal/err/check_positive_finite.hpp>
10 #include <stan/math/prim/scal/fun/constants.hpp>
11 #include <stan/math/prim/scal/fun/lbeta.hpp>
12 #include <stan/math/prim/scal/fun/digamma.hpp>
13 #include <stan/math/prim/scal/fun/lgamma.hpp>
14 #include <stan/math/prim/scal/fun/binomial_coefficient_log.hpp>
15 #include <stan/math/prim/scal/fun/value_of.hpp>
16 #include <stan/math/prim/scal/meta/contains_nonconstant_struct.hpp>
17 #include <stan/math/prim/scal/meta/include_summand.hpp>
18 #include <stan/math/prim/scal/prob/beta_rng.hpp>
19 #include <stan/math/prim/scal/fun/F32.hpp>
20 #include <stan/math/prim/scal/fun/grad_F32.hpp>
21 #include <cmath>
22
23 namespace stan {
24
25 namespace math {
26
27 // Beta-Binomial CDF
28 template <typename T_n, typename T_N,
29 typename T_size1, typename T_size2>
30 typename return_type<T_size1, T_size2>::type
31 beta_binomial_cdf(const T_n& n, const T_N& N, const T_size1& alpha,
32 const T_size2& beta) {
33 static const char* function("stan::math::beta_binomial_cdf");
34 typedef typename stan::partials_return_type<T_n, T_N, T_size1,
35 T_size2>::type
36 T_partials_return;
37
38 using stan::math::check_positive_finite;
39 using stan::math::check_nonnegative;
40 using stan::math::value_of;
41 using stan::math::check_consistent_sizes;
42 using stan::math::include_summand;
43
44 // Ensure non-zero argument lengths
45 if (!(stan::length(n) && stan::length(N) && stan::length(alpha)
46 && stan::length(beta)))
47 return 1.0;
48
49 T_partials_return P(1.0);
50
51 // Validate arguments
52 check_nonnegative(function, "Population size parameter", N);
53 check_positive_finite(function,
54 "First prior sample size parameter", alpha);
55 check_positive_finite(function,
56 "Second prior sample size parameter", beta);
57 check_consistent_sizes(function,
58 "Successes variable", n,
59 "Population size parameter", N,
60 "First prior sample size parameter", alpha,
61 "Second prior sample size parameter", beta);
62
63 // Wrap arguments in vector views
64 VectorView<const T_n> n_vec(n);
65 VectorView<const T_N> N_vec(N);
66 VectorView<const T_size1> alpha_vec(alpha);
67 VectorView<const T_size2> beta_vec(beta);
68 size_t size = max_size(n, N, alpha, beta);
69
70 // Compute vectorized CDF and gradient
71 using stan::math::lgamma;
72 using stan::math::lbeta;
73 using stan::math::digamma;
74 using std::exp;
75 using std::exp;
76
77 OperandsAndPartials<T_size1, T_size2>
78 operands_and_partials(alpha, beta);
79
80 // Explicit return for extreme values
81 // The gradients are technically ill-defined, but treated as zero
82 for (size_t i = 0; i < stan::length(n); i++) {
83 if (value_of(n_vec[i]) <= 0)
84 return operands_and_partials.value(0.0);
85 }
86
87 for (size_t i = 0; i < size; i++) {
88 // Explicit results for extreme values
89 // The gradients are technically ill-defined, but treated as zero
90 if (value_of(n_vec[i]) >= value_of(N_vec[i])) {
91 continue;
92 }
93
94 const T_partials_return n_dbl = value_of(n_vec[i]);
95 const T_partials_return N_dbl = value_of(N_vec[i]);
96 const T_partials_return alpha_dbl = value_of(alpha_vec[i]);
97 const T_partials_return beta_dbl = value_of(beta_vec[i]);
98
99 const T_partials_return mu = alpha_dbl + n_dbl + 1;
100 const T_partials_return nu = beta_dbl + N_dbl - n_dbl - 1;
101
102 const T_partials_return F = stan::math::F32((T_partials_return)1, mu,
103 -N_dbl + n_dbl + 1,
104 n_dbl + 2, 1 - nu,
105 (T_partials_return)1);
106
107 T_partials_return C = lgamma(nu) - lgamma(N_dbl - n_dbl);
108 C += lgamma(mu) - lgamma(n_dbl + 2);
109 C += lgamma(N_dbl + 2) - lgamma(N_dbl + alpha_dbl + beta_dbl);
110 C = exp(C);
111
112 C *= F / exp(lbeta(alpha_dbl, beta_dbl));
113 C /= N_dbl + 1;
114
115 const T_partials_return Pi = 1 - C;
116
117 P *= Pi;
118
119 T_partials_return dF[6];
120 T_partials_return digammaOne = 0;
121 T_partials_return digammaTwo = 0;
122
123 if (contains_nonconstant_struct<T_size1, T_size2>::value) {
124 digammaOne = digamma(mu + nu);
125 digammaTwo = digamma(alpha_dbl + beta_dbl);
126 stan::math::grad_F32(dF, (T_partials_return)1, mu, -N_dbl + n_dbl + 1,
127 n_dbl + 2,
128 1 - nu, (T_partials_return)1);
129 }
130 if (!is_constant_struct<T_size1>::value) {
131 const T_partials_return g
132 = - C * (digamma(mu) - digammaOne + dF[1] / F
133 - digamma(alpha_dbl) + digammaTwo);
134 operands_and_partials.d_x1[i]
135 += g / Pi;
136 }
137 if (!is_constant_struct<T_size2>::value) {
138 const T_partials_return g
139 = - C * (digamma(nu) - digammaOne - dF[4] / F - digamma(beta_dbl)
140 + digammaTwo);
141 operands_and_partials.d_x2[i]
142 += g / Pi;
143 }
144 }
145
146 if (!is_constant_struct<T_size1>::value) {
147 for (size_t i = 0; i < stan::length(alpha); ++i)
148 operands_and_partials.d_x1[i] *= P;
149 }
150 if (!is_constant_struct<T_size2>::value) {
151 for (size_t i = 0; i < stan::length(beta); ++i)
152 operands_and_partials.d_x2[i] *= P;
153 }
154
155 return operands_and_partials.value(P);
156 }
157
158 }
159 }
160 #endif
stan::math::OperandsAndPartials::d_x2
VectorView< T_return_type, false, true > d_x2
Definition: OperandsAndPartials.hpp:40
lgamma.hpp
stan::math::lgamma
fvar< T > lgamma(const fvar< T > &x)
Definition: lgamma.hpp:15
check_positive_finite.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::value_of
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:16
stan::math::lbeta
fvar< T > lbeta(const fvar< T > &x1, const fvar< T > &x2)
Definition: lbeta.hpp:16
digamma.hpp
stan::math::OperandsAndPartials::value
T_return_type value(double value)
Returns a T_return_type with the value specified with the partial derivatves.
Definition: OperandsAndPartials.hpp:68
contains_nonconstant_struct.hpp
stan::length
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
binomial_coefficient_log.hpp
is_constant_struct.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
F32.hpp
include_summand.hpp
check_nonnegative.hpp
stan::is_constant_struct
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
Definition: is_constant_struct.hpp:13
stan::math::beta_binomial_cdf
return_type< T_size1, T_size2 >::type beta_binomial_cdf(const T_n &n, const T_N &N, const T_size1 &alpha, const T_size2 &beta)
Definition: beta_binomial_cdf.hpp:31
stan::partials_return_type
Definition: partials_return_type.hpp:16
stan::math::exp
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
beta_rng.hpp
stan::math::OperandsAndPartials
This class builds partial derivatives with respect to a set of operands.
Definition: OperandsAndPartials.hpp:38
stan::max_size
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
partials_return_type.hpp
OperandsAndPartials.hpp
stan::contains_nonconstant_struct
Definition: contains_nonconstant_struct.hpp:14
grad_F32.hpp
stan::math::grad_F32
void grad_F32(T *g, T a, T b, T c, T d, T e, T z, T precision=1e-6)
Definition: grad_F32.hpp:11
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
lbeta.hpp
stan::math::check_nonnegative
bool check_nonnegative(const char *function, const char *name, const T_y &y)
Return true if y is non-negative.
Definition: check_nonnegative.hpp:66
stan::math::F32
T F32(T a, T b, T c, T d, T e, T z, T precision=1e-6)
Definition: F32.hpp:11
value_of.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_positive_finite
bool check_positive_finite(const char *function, const char *name, const T_y &y)
Return true if y is positive and finite.
Definition: check_positive_finite.hpp:28
stan::math::OperandsAndPartials::d_x1
VectorView< T_return_type, false, true > d_x1
Definition: OperandsAndPartials.hpp:39
stan::math::digamma
fvar< T > digamma(const fvar< T > &x)
Definition: digamma.hpp:16

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