1#ifndef STAN_MATH_PRIM_PROB_NEG_BINOMIAL_2_CDF_HPP
2#define STAN_MATH_PRIM_PROB_NEG_BINOMIAL_2_CDF_HPP
23template <
typename T_n,
typename T_location,
typename T_precision>
25 const T_n& n,
const T_location& mu,
const T_precision& phi) {
30 static constexpr const char* function =
"neg_binomial_2_cdf";
32 mu,
"Precision Parameter", phi);
36 T_phi_ref phi_ref = phi;
46 T_partials_return P(1.0);
53 size_t size_n_phi =
max_size(n, phi);
54 size_t max_size_seq_view =
max_size(n, mu, phi);
59 if (n_vec.val(i) < 0) {
60 return ops_partials.build(0.0);
66 digamma_phi_vec(size_phi);
69 digamma_sum_vec(size_n_phi);
72 for (
size_t i = 0; i < size_phi; i++) {
73 digamma_phi_vec[i] =
digamma(phi_vec.val(i));
75 for (
size_t i = 0; i < size_n_phi; i++) {
76 const T_partials_return n_dbl = n_vec.val(i);
77 const T_partials_return phi_dbl = phi_vec.val(i);
78 digamma_sum_vec[i] =
digamma(n_dbl + phi_dbl + 1);
82 for (
size_t i = 0; i < max_size_seq_view; i++) {
85 if (n_vec.val(i) == std::numeric_limits<int>::max()) {
86 return ops_partials.build(1.0);
89 const T_partials_return n_dbl_p1 = n_vec.val(i) + 1;
90 const T_partials_return mu_dbl = mu_vec.val(i);
91 const T_partials_return phi_dbl = phi_vec.val(i);
92 const T_partials_return inv_mu_plus_phi =
inv(mu_dbl + phi_dbl);
93 const T_partials_return p_dbl = phi_dbl * inv_mu_plus_phi;
94 const T_partials_return d_dbl =
square(inv_mu_plus_phi);
95 const T_partials_return P_i =
inc_beta(phi_dbl, n_dbl_p1, p_dbl);
96 const T_partials_return inc_beta_ddz_i
104 partials<0>(ops_partials)[i] -= inc_beta_ddz_i * phi_dbl;
108 partials<1>(ops_partials)[i]
109 +=
inc_beta_dda(phi_dbl, n_dbl_p1, p_dbl, digamma_phi_vec[i],
112 + inc_beta_ddz_i * mu_dbl;
118 partials<0>(ops_partials)[i] *= P;
123 for (
size_t i = 0; i < size_phi; ++i) {
124 partials<1>(ops_partials)[i] *= P;
128 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.
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)
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
return_type_t< T_location, T_precision > neg_binomial_2_cdf(const T_n &n, const T_location &mu, const T_precision &phi)
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
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...