1#ifndef STAN_MATH_PRIM_PROB_BETA_BINOMIAL_LCCDF_HPP
2#define STAN_MATH_PRIM_PROB_BETA_BINOMIAL_LCCDF_HPP
43template <
typename T_n,
typename T_N,
typename T_size1,
typename T_size2>
46 const T_size2&
beta) {
53 static constexpr const char* function =
"beta_binomial_lccdf";
55 "Population size parameter", N,
56 "First prior sample size parameter", alpha,
57 "Second prior sample size parameter",
beta);
63 T_alpha_ref alpha_ref = alpha;
64 T_beta_ref beta_ref =
beta;
71 T_partials_return P(0.0);
83 if (n_vec.val(i) < 0) {
84 return ops_partials.build(0.0);
88 for (
size_t i = 0; i < max_size_seq_view; i++) {
91 if (n_vec.val(i) >= N_vec.val(i)) {
95 const T_partials_return n_dbl = n_vec.val(i);
96 const T_partials_return N_dbl = N_vec.val(i);
97 const T_partials_return alpha_dbl = alpha_vec.val(i);
98 const T_partials_return beta_dbl = beta_vec.val(i);
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 const T_partials_return one = 1;
104 {one, mu, -N_dbl + n_dbl + 1}, {n_dbl + 2, 1 - nu}, one);
105 T_partials_return C =
lbeta(nu, mu) -
lbeta(alpha_dbl, beta_dbl)
106 -
lbeta(N_dbl - n_dbl, n_dbl + 2);
107 C = F *
exp(C) / (N_dbl + 1);
109 const T_partials_return Pi = C;
113 T_partials_return digammaDiff
118 T_partials_return dF[6];
120 grad_F32(dF, one, mu, -N_dbl + n_dbl + 1, n_dbl + 2, 1 - nu, one);
123 partials<0>(ops_partials)[i]
127 partials<1>(ops_partials)[i]
132 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_lccdf(const T_n &n, const T_N &N, const T_size1 &alpha, const T_size2 &beta)
Returns the log CCDF of the Beta-Binomial distribution with given population size,...
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).
static constexpr double negative_infinity()
Return negative infinity.
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)
fvar< T > log(const fvar< T > &x)
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...