1#ifndef STAN_MATH_PRIM_PROB_NEG_BINOMIAL_CDF_HPP
2#define STAN_MATH_PRIM_PROB_NEG_BINOMIAL_CDF_HPP
24template <
typename T_n,
typename T_shape,
typename T_inv_scale>
27 const T_inv_scale&
beta) {
32 static constexpr const char* function =
"neg_binomial_cdf";
34 alpha,
"Inverse scale parameter",
beta);
36 T_alpha_ref alpha_ref = alpha;
37 T_beta_ref beta_ref =
beta;
45 T_partials_return P(1.0);
52 size_t size_n_alpha =
max_size(n, alpha);
58 if (n_vec.val(i) < 0) {
59 return ops_partials.build(0.0);
64 digamma_alpha_vec(size_alpha);
67 digamma_sum_vec(size_n_alpha);
70 for (
size_t i = 0; i < size_alpha; i++) {
71 digamma_alpha_vec[i] =
digamma(alpha_vec.val(i));
73 for (
size_t i = 0; i < size_n_alpha; i++) {
74 const T_partials_return n_dbl = n_vec.val(i);
75 const T_partials_return alpha_dbl = alpha_vec.val(i);
76 digamma_sum_vec[i] =
digamma(n_dbl + alpha_dbl + 1);
80 for (
size_t i = 0; i < max_size_seq_view; i++) {
83 if (n_vec.val(i) == std::numeric_limits<int>::max()) {
84 return ops_partials.build(1.0);
87 const T_partials_return n_dbl = n_vec.val(i);
88 const T_partials_return alpha_dbl = alpha_vec.val(i);
89 const T_partials_return beta_dbl = beta_vec.val(i);
90 const T_partials_return inv_beta_p1 =
inv(beta_dbl + 1);
91 const T_partials_return p_dbl = beta_dbl * inv_beta_p1;
92 const T_partials_return d_dbl =
square(inv_beta_p1);
94 const T_partials_return P_i =
inc_beta(alpha_dbl, n_dbl + 1.0, p_dbl);
99 partials<0>(ops_partials)[i]
100 +=
inc_beta_dda(alpha_dbl, n_dbl + 1, p_dbl, digamma_alpha_vec[i],
106 partials<1>(ops_partials)[i]
107 +=
inc_beta_ddz(alpha_dbl, n_dbl + 1.0, p_dbl) * d_dbl / P_i;
112 for (
size_t i = 0; i < size_alpha; ++i) {
113 partials<0>(ops_partials)[i] *= P;
119 partials<1>(ops_partials)[i] *= P;
123 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>>.
T inc_beta_ddz(T a, T b, T z)
Returns the partial derivative of the regularized incomplete beta function, I_{z}(a,...
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
return_type_t< T_shape, T_inv_scale > neg_binomial_cdf(const T_n &n, const T_shape &alpha, const T_inv_scale &beta)
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
T inc_beta_dda(T a, T b, T z, T digamma_a, T digamma_ab)
Returns the partial derivative of the regularized incomplete beta function, I_{z}(a,...
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...