Stan Math Library  2.20.0
reverse mode automatic differentiation
neg_binomial_2_cdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_NEG_BINOMIAL_2_CDF_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_NEG_BINOMIAL_2_CDF_HPP
3 
14 #include <limits>
15 
16 namespace stan {
17 namespace math {
18 
19 template <typename T_n, typename T_location, typename T_precision>
21  const T_n& n, const T_location& mu, const T_precision& phi) {
22  static const char* function = "neg_binomial_2_cdf";
23  typedef
25  T_partials_return;
26 
27  T_partials_return P(1.0);
28  if (size_zero(n, mu, phi))
29  return P;
30 
31  check_positive_finite(function, "Location parameter", mu);
32  check_positive_finite(function, "Precision parameter", phi);
33  check_not_nan(function, "Random variable", n);
34  check_consistent_sizes(function, "Random variable", n, "Location parameter",
35  mu, "Precision Parameter", phi);
36 
37  scalar_seq_view<T_n> n_vec(n);
38  scalar_seq_view<T_location> mu_vec(mu);
39  scalar_seq_view<T_precision> phi_vec(phi);
40  size_t size = max_size(n, mu, phi);
41 
43 
44  // Explicit return for extreme values
45  // The gradients are technically ill-defined, but treated as zero
46  for (size_t i = 0; i < stan::length(n); i++) {
47  if (value_of(n_vec[i]) < 0)
48  return ops_partials.build(0.0);
49  }
50 
52  T_precision>
53  digamma_phi_vec(stan::length(phi));
54 
56  T_precision>
57  digamma_sum_vec(stan::length(phi));
58 
60  for (size_t i = 0; i < stan::length(phi); i++) {
61  const T_partials_return n_dbl = value_of(n_vec[i]);
62  const T_partials_return phi_dbl = value_of(phi_vec[i]);
63 
64  digamma_phi_vec[i] = digamma(phi_dbl);
65  digamma_sum_vec[i] = digamma(n_dbl + phi_dbl + 1);
66  }
67  }
68 
69  for (size_t i = 0; i < size; i++) {
70  // Explicit results for extreme values
71  // The gradients are technically ill-defined, but treated as zero
72  if (value_of(n_vec[i]) == std::numeric_limits<int>::max())
73  return ops_partials.build(1.0);
74 
75  const T_partials_return n_dbl = value_of(n_vec[i]);
76  const T_partials_return mu_dbl = value_of(mu_vec[i]);
77  const T_partials_return phi_dbl = value_of(phi_vec[i]);
78 
79  const T_partials_return p_dbl = phi_dbl / (mu_dbl + phi_dbl);
80  const T_partials_return d_dbl
81  = 1.0 / ((mu_dbl + phi_dbl) * (mu_dbl + phi_dbl));
82 
83  const T_partials_return P_i = inc_beta(phi_dbl, n_dbl + 1.0, p_dbl);
84 
85  P *= P_i;
86 
88  ops_partials.edge1_.partials_[i]
89  += -inc_beta_ddz(phi_dbl, n_dbl + 1.0, p_dbl) * phi_dbl * d_dbl / P_i;
90 
92  ops_partials.edge2_.partials_[i]
93  += inc_beta_dda(phi_dbl, n_dbl + 1, p_dbl, digamma_phi_vec[i],
94  digamma_sum_vec[i])
95  / P_i
96  + inc_beta_ddz(phi_dbl, n_dbl + 1.0, p_dbl) * mu_dbl * d_dbl / P_i;
97  }
98  }
99 
101  for (size_t i = 0; i < stan::length(mu); ++i)
102  ops_partials.edge1_.partials_[i] *= P;
103  }
104 
106  for (size_t i = 0; i < stan::length(phi); ++i)
107  ops_partials.edge2_.partials_[i] *= P;
108  }
109 
110  return ops_partials.build(P);
111 }
112 
113 } // namespace math
114 } // namespace stan
115 #endif
return_type< T_location, T_precision >::type neg_binomial_2_cdf(const T_n &n, const T_location &mu, const T_precision &phi)
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
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, b) with respect to a.
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Definition: conjunction.hpp:14
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
T inc_beta_ddz(T a, T b, T z)
Returns the partial derivative of the regularized incomplete beta function, I_{z}(a, b) with respect to z.
fvar< T > inc_beta(const fvar< T > &a, const fvar< T > &b, const fvar< T > &x)
Definition: inc_beta.hpp:18
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
Definition: return_type.hpp:36
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
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
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_
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.