Stan Math Library  2.20.0
reverse mode automatic differentiation
neg_binomial_lccdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_NEG_BINOMIAL_LCCDF_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_NEG_BINOMIAL_LCCDF_HPP
3 
15 #include <cmath>
16 #include <limits>
17 
18 namespace stan {
19 namespace math {
20 
21 template <typename T_n, typename T_shape, typename T_inv_scale>
23  const T_n& n, const T_shape& alpha, const T_inv_scale& beta) {
24  static const char* function = "neg_binomial_lccdf";
26  T_partials_return;
27 
28  if (size_zero(n, alpha, beta))
29  return 0.0;
30 
31  T_partials_return P(0.0);
32 
33  check_positive_finite(function, "Shape parameter", alpha);
34  check_positive_finite(function, "Inverse scale parameter", beta);
35  check_consistent_sizes(function, "Failures variable", n, "Shape parameter",
36  alpha, "Inverse scale parameter", beta);
37 
38  scalar_seq_view<T_n> n_vec(n);
39  scalar_seq_view<T_shape> alpha_vec(alpha);
40  scalar_seq_view<T_inv_scale> beta_vec(beta);
41  size_t size = max_size(n, alpha, beta);
42 
43  using std::exp;
44  using std::log;
45  using std::pow;
46 
47  operands_and_partials<T_shape, T_inv_scale> ops_partials(alpha, beta);
48 
49  // Explicit return for extreme values
50  // The gradients are technically ill-defined, but treated as zero
51  for (size_t i = 0; i < stan::length(n); i++) {
52  if (value_of(n_vec[i]) < 0)
53  return ops_partials.build(0.0);
54  }
55 
56  VectorBuilder<!is_constant_all<T_shape>::value, T_partials_return, T_shape>
57  digammaN_vec(stan::length(alpha));
58  VectorBuilder<!is_constant_all<T_shape>::value, T_partials_return, T_shape>
59  digammaAlpha_vec(stan::length(alpha));
60  VectorBuilder<!is_constant_all<T_shape>::value, T_partials_return, T_shape>
61  digammaSum_vec(stan::length(alpha));
62 
64  for (size_t i = 0; i < stan::length(alpha); i++) {
65  const T_partials_return n_dbl = value_of(n_vec[i]);
66  const T_partials_return alpha_dbl = value_of(alpha_vec[i]);
67 
68  digammaN_vec[i] = digamma(n_dbl + 1);
69  digammaAlpha_vec[i] = digamma(alpha_dbl);
70  digammaSum_vec[i] = digamma(n_dbl + alpha_dbl + 1);
71  }
72  }
73 
74  for (size_t i = 0; i < size; i++) {
75  // Explicit results for extreme values
76  // The gradients are technically ill-defined, but treated as zero
77  if (value_of(n_vec[i]) == std::numeric_limits<int>::max())
78  return ops_partials.build(negative_infinity());
79 
80  const T_partials_return n_dbl = value_of(n_vec[i]);
81  const T_partials_return alpha_dbl = value_of(alpha_vec[i]);
82  const T_partials_return beta_dbl = value_of(beta_vec[i]);
83  const T_partials_return p_dbl = beta_dbl / (1.0 + beta_dbl);
84  const T_partials_return d_dbl = 1.0 / ((1.0 + beta_dbl) * (1.0 + beta_dbl));
85  const T_partials_return Pi = 1.0 - inc_beta(alpha_dbl, n_dbl + 1.0, p_dbl);
86  const T_partials_return beta_func = stan::math::beta(n_dbl + 1, alpha_dbl);
87 
88  P += log(Pi);
89 
91  T_partials_return g1 = 0;
92  T_partials_return g2 = 0;
93 
94  grad_reg_inc_beta(g1, g2, alpha_dbl, n_dbl + 1, p_dbl,
95  digammaAlpha_vec[i], digammaN_vec[i], digammaSum_vec[i],
96  beta_func);
97  ops_partials.edge1_.partials_[i] -= g1 / Pi;
98  }
100  ops_partials.edge2_.partials_[i] -= d_dbl * pow(1 - p_dbl, n_dbl)
101  * pow(p_dbl, alpha_dbl - 1)
102  / beta_func / Pi;
103  }
104 
105  return ops_partials.build(P);
106 }
107 
108 } // namespace math
109 } // namespace stan
110 #endif
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.
Definition: value_of.hpp:17
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Definition: conjunction.hpp:14
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:12
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.
Definition: length.hpp:16
bool size_zero(T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition: size_zero.hpp:18
fvar< T > inc_beta(const fvar< T > &a, const fvar< T > &b, const fvar< T > &x)
Definition: inc_beta.hpp:18
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.
Definition: beta.hpp:51
return_type< T_shape, T_inv_scale >::type neg_binomial_lccdf(const T_n &n, const T_shape &alpha, const T_inv_scale &beta)
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
Definition: return_type.hpp:36
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:11
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
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.
Definition: max.hpp:21
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.
Definition: size.hpp:17
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition: pow.hpp:16
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.
Definition: constants.hpp:115
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
Definition: digamma.hpp:23

     [ Stan Home Page ] © 2011–2018, Stan Development Team.