1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BETA_BINOMIAL_LCCDF_HPP 2 #define STAN_MATH_PRIM_SCAL_PROB_BETA_BINOMIAL_LCCDF_HPP 38 template <
typename T_n,
typename T_N,
typename T_size1,
typename T_size2>
40 const T_n& n,
const T_N& N,
const T_size1& alpha,
const T_size2&
beta) {
41 static const char*
function =
"beta_binomial_lccdf";
48 T_partials_return P(0.0);
54 "Population size parameter", N,
55 "First prior sample size parameter", alpha,
56 "Second prior sample size parameter", beta);
74 return ops_partials.
build(0.0);
77 for (
size_t i = 0; i <
size; i++) {
84 const T_partials_return n_dbl =
value_of(n_vec[i]);
85 const T_partials_return N_dbl =
value_of(N_vec[i]);
86 const T_partials_return alpha_dbl =
value_of(alpha_vec[i]);
87 const T_partials_return beta_dbl =
value_of(beta_vec[i]);
89 const T_partials_return mu = alpha_dbl + n_dbl + 1;
90 const T_partials_return nu = beta_dbl + N_dbl - n_dbl - 1;
92 const T_partials_return F
93 =
F32((T_partials_return)1, mu, -N_dbl + n_dbl + 1, n_dbl + 2, 1 - nu,
94 (T_partials_return)1);
96 T_partials_return C =
lgamma(nu) -
lgamma(N_dbl - n_dbl);
98 C +=
lgamma(N_dbl + 2) -
lgamma(N_dbl + alpha_dbl + beta_dbl);
104 const T_partials_return Pi = C;
108 T_partials_return dF[6];
109 T_partials_return digammaOne = 0;
110 T_partials_return digammaTwo = 0;
114 digammaTwo =
digamma(alpha_dbl + beta_dbl);
115 grad_F32(dF, (T_partials_return)1, mu, -N_dbl + n_dbl + 1, n_dbl + 2,
116 1 - nu, (T_partials_return)1);
119 const T_partials_return g = -C
120 * (
digamma(mu) - digammaOne + dF[1] / F
121 -
digamma(alpha_dbl) + digammaTwo);
122 ops_partials.
edge1_.partials_[i] -= g / Pi;
125 const T_partials_return g = -C
126 * (
digamma(nu) - digammaOne - dF[4] / F
127 -
digamma(beta_dbl) + digammaTwo);
128 ops_partials.
edge2_.partials_[i] -= g / Pi;
132 return ops_partials.
build(P);
boost::math::tools::promote_args< double, typename partials_type< typename scalar_type< T >::type >::type, typename partials_return_type< T_pack... >::type >::type type
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
T F32(const T &a1, const T &a2, const T &a3, const T &b1, const T &b2, const T &z, double precision=1e-6, int max_steps=1e5)
Hypergeometric function (3F2).
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
fvar< T > log(const fvar< T > &x)
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
This template builds partial derivatives with respect to a set of operands.
size_t length(const std::vector< T > &x)
Returns the length of the provided std::vector.
bool size_zero(T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
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.
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
fvar< T > exp(const fvar< T > &x)
return_type< T_size1, T_size2 >::type 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, prior success, and prior failure parameters.
size_t max_size(const T1 &x1, const T2 &x2)
T_return_type build(double value)
Build the node to be stored on the autodiff graph.
internal::ops_partials_edge< double, Op2 > edge2_
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
void grad_F32(T *g, const T &a1, const T &a2, const T &a3, const T &b1, const T &b2, const T &z, const T &precision=1e-6, int max_steps=1e5)
Gradients of the hypergeometric function, 3F2.
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Check if the dimension of x1 is consistent with x2.
internal::ops_partials_edge< double, Op1 > edge1_
double negative_infinity()
Return negative infinity.
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.