Automatic Differentiation
 
Loading...
Searching...
No Matches
beta_binomial_cdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_BETA_BINOMIAL_CDF_HPP
2#define STAN_MATH_PRIM_PROB_BETA_BINOMIAL_CDF_HPP
3
19#include <cmath>
20
21namespace stan {
22namespace math {
23
42template <typename T_n, typename T_N, typename T_size1, typename T_size2>
44 const T_size1& alpha,
45 const T_size2& beta) {
46 using T_partials_return = partials_return_t<T_n, T_N, T_size1, T_size2>;
47 using std::exp;
48 using T_N_ref = ref_type_t<T_N>;
49 using T_alpha_ref = ref_type_t<T_size1>;
50 using T_beta_ref = ref_type_t<T_size2>;
51 static constexpr const char* function = "beta_binomial_cdf";
52 check_consistent_sizes(function, "Successes variable", n,
53 "Population size parameter", N,
54 "First prior sample size parameter", alpha,
55 "Second prior sample size parameter", beta);
56 if (size_zero(n, N, alpha, beta)) {
57 return 1.0;
58 }
59
60 T_N_ref N_ref = N;
61 T_alpha_ref alpha_ref = alpha;
62 T_beta_ref beta_ref = beta;
63 check_nonnegative(function, "Population size parameter", N_ref);
64 check_positive_finite(function, "First prior sample size parameter",
65 alpha_ref);
66 check_positive_finite(function, "Second prior sample size parameter",
67 beta_ref);
68
69 T_partials_return P(1.0);
70 auto ops_partials = make_partials_propagator(alpha_ref, beta_ref);
71
72 scalar_seq_view<T_n> n_vec(n);
73 scalar_seq_view<T_N_ref> N_vec(N_ref);
74 scalar_seq_view<T_alpha_ref> alpha_vec(alpha_ref);
75 scalar_seq_view<T_beta_ref> beta_vec(beta_ref);
76 size_t max_size_seq_view = max_size(n, N, alpha, beta);
77
78 // Explicit return for extreme values
79 // The gradients are technically ill-defined, but treated as zero
80 for (size_t i = 0; i < stan::math::size(n); i++) {
81 if (n_vec.val(i) < 0) {
82 return ops_partials.build(0.0);
83 }
84 }
85
86 for (size_t i = 0; i < max_size_seq_view; i++) {
87 // Explicit results for extreme values
88 // The gradients are technically ill-defined, but treated as zero
89 if (n_vec.val(i) >= N_vec.val(i)) {
90 continue;
91 }
92
93 const T_partials_return n_dbl = n_vec.val(i);
94 const T_partials_return N_dbl = N_vec.val(i);
95 const T_partials_return alpha_dbl = alpha_vec.val(i);
96 const T_partials_return beta_dbl = beta_vec.val(i);
97 const T_partials_return N_minus_n = N_dbl - n_dbl;
98 const T_partials_return mu = alpha_dbl + n_dbl + 1;
99 const T_partials_return nu = beta_dbl + N_minus_n - 1;
100 const T_partials_return one = 1;
101
102 const T_partials_return F = hypergeometric_3F2({one, mu, 1 - N_minus_n},
103 {n_dbl + 2, 1 - nu}, one);
104
105 T_partials_return C = lbeta(nu, mu) - lbeta(alpha_dbl, beta_dbl)
106 - lbeta(N_minus_n, n_dbl + 2);
107 C = F * exp(C) / (N_dbl + 1);
108
109 const T_partials_return Pi = 1 - C;
110
111 P *= Pi;
112
113 T_partials_return digammaDiff
115 ? 0
116 : digamma(alpha_dbl + beta_dbl) - digamma(mu + nu);
117
118 T_partials_return dF[6];
120 grad_F32(dF, one, mu, 1 - N_minus_n, n_dbl + 2, 1 - nu, one);
121 }
123 const T_partials_return g
124 = -C * (digamma(mu) - digamma(alpha_dbl) + digammaDiff + dF[1] / F);
125 partials<0>(ops_partials)[i] += g / Pi;
126 }
128 const T_partials_return g
129 = -C * (digamma(nu) - digamma(beta_dbl) + digammaDiff - dF[4] / F);
130 partials<1>(ops_partials)[i] += g / Pi;
131 }
132 }
133
135 for (size_t i = 0; i < stan::math::size(alpha); ++i) {
136 partials<0>(ops_partials)[i] *= P;
137 }
138 }
140 for (size_t i = 0; i < stan::math::size(beta); ++i) {
141 partials<1>(ops_partials)[i] *= P;
142 }
143 }
144
145 return ops_partials.build(P);
146}
147
148} // namespace math
149} // namespace stan
150#endif
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
return_type_t< T_size1, T_size2 > beta_binomial_cdf(const T_n &n, const T_N &N, const T_size1 &alpha, const T_size2 &beta)
Returns the CDF of the Beta-Binomial distribution with given population size, prior success,...
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Definition size.hpp:19
auto hypergeometric_3F2(const Ta &a, const Tb &b, const Tz &z)
Hypergeometric function (3F2).
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
void grad_F32(T1 *g, const T2 &a1, const T3 &a2, const T4 &a3, const T5 &b1, const T6 &b2, const T7 &z, const T8 &precision=1e-6, int max_steps=1e5)
Gradients of the hypergeometric function, 3F2.
Definition grad_F32.hpp:55
fvar< T > lbeta(const fvar< T > &x1, const fvar< T > &x2)
Definition lbeta.hpp:14
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
fvar< T > beta(const fvar< T > &x1, const fvar< T > &x2)
Return fvar with the beta function applied to the specified arguments and its gradient.
Definition beta.hpp:51
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
Definition digamma.hpp:23
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:15
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:55
typename partials_return_type< Args... >::type partials_return_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...