1#ifndef STAN_MATH_PRIM_PROB_NEG_BINOMIAL_LCCDF_HPP
2#define STAN_MATH_PRIM_PROB_NEG_BINOMIAL_LCCDF_HPP
26template <
typename T_n,
typename T_shape,
typename T_inv_scale>
28 const T_n& n,
const T_shape& alpha,
const T_inv_scale& beta_param) {
35 static constexpr const char* function =
"neg_binomial_lccdf";
37 alpha,
"Inverse scale parameter", beta_param);
39 T_alpha_ref alpha_ref = alpha;
40 T_beta_ref beta_ref = beta_param;
49 T_partials_return P(0.0);
57 size_t size_n_alpha =
max_size(n, alpha);
58 size_t max_size_seq_view =
max_size(n, alpha, beta_param);
62 for (
size_t i = 0; i < size_n; i++) {
63 if (n_vec.val(i) < 0) {
64 return ops_partials.build(0.0);
71 digammaAlpha_vec(size_alpha);
74 digammaSum_vec(size_n_alpha);
77 for (
size_t i = 0; i < size_n; i++) {
78 digammaN_vec[i] =
digamma(n_vec.val(i) + 1);
80 for (
size_t i = 0; i < size_alpha; i++) {
81 digammaAlpha_vec[i] =
digamma(alpha_vec.val(i));
83 for (
size_t i = 0; i < size_n_alpha; i++) {
84 const T_partials_return n_dbl = n_vec.val(i);
85 const T_partials_return alpha_dbl = alpha_vec.val(i);
86 digammaSum_vec[i] =
digamma(n_dbl + alpha_dbl + 1);
90 for (
size_t i = 0; i < max_size_seq_view; i++) {
93 if (n_vec.val(i) == std::numeric_limits<int>::max()) {
97 const T_partials_return n_dbl = n_vec.val(i);
98 const T_partials_return alpha_dbl = alpha_vec.val(i);
99 const T_partials_return beta_dbl = beta_vec.val(i);
100 const T_partials_return inv_beta_p1 =
inv(beta_dbl + 1);
101 const T_partials_return p_dbl = beta_dbl * inv_beta_p1;
102 const T_partials_return d_dbl =
square(inv_beta_p1);
103 const T_partials_return Pi = 1.0 -
inc_beta(alpha_dbl, n_dbl + 1.0, p_dbl);
104 const T_partials_return beta_func =
beta(n_dbl + 1, alpha_dbl);
109 T_partials_return g1 = 0;
110 T_partials_return g2 = 0;
113 digammaAlpha_vec[i], digammaN_vec[i], digammaSum_vec[i],
115 partials<0>(ops_partials)[i] -= g1 / Pi;
118 partials<1>(ops_partials)[i] -= d_dbl *
pow(1 - p_dbl, n_dbl)
119 *
pow(p_dbl, alpha_dbl - 1)
124 return ops_partials.build(P);
VectorBuilder allocates type T1 values to be used as intermediate values.
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
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>>.
static constexpr double negative_infinity()
Return negative infinity.
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
auto pow(const T1 &x1, const T2 &x2)
fvar< T > log(const fvar< T > &x)
void grad_reg_inc_beta(T &g1, T &g2, const T &a, const T &b, const T &z, const T &digammaA, const T &digammaB, const T &digammaSum, const T &betaAB)
Computes the gradients of the regularized incomplete beta function.
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
return_type_t< T_shape, T_inv_scale > neg_binomial_lccdf(const T_n &n, const T_shape &alpha, const T_inv_scale &beta_param)
fvar< T > inc_beta(const fvar< T > &a, const fvar< T > &b, const fvar< T > &x)
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.
fvar< T > inv(const fvar< T > &x)
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 > square(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...