1#ifndef STAN_MATH_PRIM_PROB_BETA_BINOMIAL_CDF_HPP
2#define STAN_MATH_PRIM_PROB_BETA_BINOMIAL_CDF_HPP
42template <
typename T_n,
typename T_N,
typename T_size1,
typename T_size2>
45 const T_size2&
beta) {
51 static constexpr const char* function =
"beta_binomial_cdf";
53 "Population size parameter", N,
54 "First prior sample size parameter", alpha,
55 "Second prior sample size parameter",
beta);
61 T_alpha_ref alpha_ref = alpha;
62 T_beta_ref beta_ref =
beta;
69 T_partials_return P(1.0);
81 if (n_vec.val(i) < 0) {
82 return ops_partials.build(0.0);
86 for (
size_t i = 0; i < max_size_seq_view; i++) {
89 if (n_vec.val(i) >= N_vec.val(i)) {
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;
103 {n_dbl + 2, 1 - nu}, one);
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);
109 const T_partials_return Pi = 1 - C;
113 T_partials_return digammaDiff
118 T_partials_return dF[6];
120 grad_F32(dF, one, mu, 1 - N_minus_n, n_dbl + 2, 1 - nu, one);
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;
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;
136 partials<0>(ops_partials)[i] *= P;
141 partials<1>(ops_partials)[i] *= P;
145 return ops_partials.build(P);
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>>.
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.
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.
fvar< T > lbeta(const fvar< T > &x1, const fvar< T > &x2)
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.
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.
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.
fvar< T > exp(const fvar< T > &x)
typename ref_type_if< true, T >::type ref_type_t
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...