1 #ifndef STAN_MATH_PRIM_SCAL_PROB_NEG_BINOMIAL_LCDF_HPP 2 #define STAN_MATH_PRIM_SCAL_PROB_NEG_BINOMIAL_LCDF_HPP 20 template <
typename T_n,
typename T_shape,
typename T_inv_scale>
22 const T_n& n,
const T_shape& alpha,
const T_inv_scale&
beta) {
23 static const char*
function =
"neg_binomial_lcdf";
30 T_partials_return P(0.0);
35 alpha,
"Inverse scale parameter", beta);
56 digammaN_vec(stan::length(alpha));
58 digammaAlpha_vec(stan::length(alpha));
60 digammaSum_vec(stan::length(alpha));
64 const T_partials_return n_dbl =
value_of(n_vec[i]);
65 const T_partials_return alpha_dbl =
value_of(alpha_vec[i]);
67 digammaN_vec[i] =
digamma(n_dbl + 1);
68 digammaAlpha_vec[i] =
digamma(alpha_dbl);
69 digammaSum_vec[i] =
digamma(n_dbl + alpha_dbl + 1);
73 for (
size_t i = 0; i <
size; i++) {
77 return ops_partials.
build(0.0);
79 const T_partials_return n_dbl =
value_of(n_vec[i]);
80 const T_partials_return alpha_dbl =
value_of(alpha_vec[i]);
81 const T_partials_return beta_dbl =
value_of(beta_vec[i]);
82 const T_partials_return p_dbl = beta_dbl / (1.0 + beta_dbl);
83 const T_partials_return d_dbl = 1.0 / ((1.0 + beta_dbl) * (1.0 + beta_dbl));
84 const T_partials_return Pi =
inc_beta(alpha_dbl, n_dbl + 1.0, p_dbl);
90 T_partials_return g1 = 0;
91 T_partials_return g2 = 0;
94 digammaAlpha_vec[i], digammaN_vec[i], digammaSum_vec[i],
96 ops_partials.
edge1_.partials_[i] += g1 / Pi;
99 ops_partials.
edge2_.partials_[i] += d_dbl *
pow(1 - p_dbl, n_dbl)
100 *
pow(p_dbl, alpha_dbl - 1)
104 return ops_partials.
build(P);
return_type< T_shape, T_inv_scale >::type neg_binomial_lcdf(const T_n &n, const T_shape &alpha, const T_inv_scale &beta)
boost::math::tools::promote_args< double, typename partials_type< typename scalar_type< T >::type >::type, typename partials_return_type< T_pack... >::type >::type type
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.
fvar< T > inc_beta(const fvar< T > &a, const fvar< T > &b, const fvar< T > &x)
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.
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.
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)
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.
int max(const std::vector< int > &x)
Returns the maximum coefficient in the specified column vector.
VectorBuilder allocates type T1 values to be used as intermediate values.
internal::ops_partials_edge< double, Op2 > edge2_
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
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.