Automatic Differentiation
 
Loading...
Searching...
No Matches
neg_binomial_cdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_NEG_BINOMIAL_CDF_HPP
2#define STAN_MATH_PRIM_PROB_NEG_BINOMIAL_CDF_HPP
3
18#include <cmath>
19#include <limits>
20
21namespace stan {
22namespace math {
23
24template <typename T_n, typename T_shape, typename T_inv_scale>
26 const T_n& n, const T_shape& alpha, const T_inv_scale& beta) {
27 using T_partials_return = partials_return_t<T_n, T_shape, T_inv_scale>;
28 using T_n_ref = ref_type_t<T_n>;
29 using T_alpha_ref = ref_type_t<T_shape>;
30 using T_beta_ref = ref_type_t<T_inv_scale>;
31 static constexpr const char* function = "neg_binomial_cdf";
32 check_consistent_sizes(function, "Failures variable", n, "Shape parameter",
33 alpha, "Inverse scale parameter", beta);
34 T_n_ref n_ref = n;
35 T_alpha_ref alpha_ref = alpha;
36 T_beta_ref beta_ref = beta;
37 check_positive_finite(function, "Shape parameter", alpha_ref);
38 check_positive_finite(function, "Inverse scale parameter", beta_ref);
39
40 if (size_zero(n, alpha, beta)) {
41 return 1.0;
42 }
43
44 T_partials_return P(1.0);
45 auto ops_partials = make_partials_propagator(alpha_ref, beta_ref);
46
47 scalar_seq_view<T_n_ref> n_vec(n_ref);
48 scalar_seq_view<T_alpha_ref> alpha_vec(alpha_ref);
49 scalar_seq_view<T_beta_ref> beta_vec(beta_ref);
50 size_t size_alpha = stan::math::size(alpha);
51 size_t size_n_alpha = max_size(n, alpha);
52 size_t max_size_seq_view = max_size(n, alpha, beta);
53
54 // Explicit return for extreme values
55 // The gradients are technically ill-defined, but treated as zero
56 for (size_t i = 0; i < stan::math::size(n); i++) {
57 if (n_vec.val(i) < 0) {
58 return ops_partials.build(0.0);
59 }
60 }
61
62 VectorBuilder<is_autodiff_v<T_shape>, T_partials_return, T_shape>
63 digamma_alpha_vec(size_alpha);
64 VectorBuilder<is_autodiff_v<T_shape>, T_partials_return, T_n, T_shape>
65 digamma_sum_vec(size_n_alpha);
66
67 if constexpr (is_autodiff_v<T_shape>) {
68 for (size_t i = 0; i < size_alpha; i++) {
69 digamma_alpha_vec[i] = digamma(alpha_vec.val(i));
70 }
71 for (size_t i = 0; i < size_n_alpha; i++) {
72 const T_partials_return n_dbl = n_vec.val(i);
73 const T_partials_return alpha_dbl = alpha_vec.val(i);
74 digamma_sum_vec[i] = digamma(n_dbl + alpha_dbl + 1);
75 }
76 }
77
78 for (size_t i = 0; i < max_size_seq_view; i++) {
79 // Explicit results for extreme values
80 // The gradients are technically ill-defined, but treated as zero
81 if (n_vec.val(i) == std::numeric_limits<int>::max()) {
82 return ops_partials.build(1.0);
83 }
84
85 const T_partials_return n_dbl = n_vec.val(i);
86 const T_partials_return alpha_dbl = alpha_vec.val(i);
87 const T_partials_return beta_dbl = beta_vec.val(i);
88 const T_partials_return inv_beta_p1 = inv(beta_dbl + 1);
89 const T_partials_return p_dbl = beta_dbl * inv_beta_p1;
90 const T_partials_return d_dbl = square(inv_beta_p1);
91
92 const T_partials_return P_i = inc_beta(alpha_dbl, n_dbl + 1.0, p_dbl);
93
94 P *= P_i;
95
96 if constexpr (is_autodiff_v<T_shape>) {
97 partials<0>(ops_partials)[i]
98 += inc_beta_dda(alpha_dbl, n_dbl + 1, p_dbl, digamma_alpha_vec[i],
99 digamma_sum_vec[i])
100 / P_i;
101 }
102
103 if constexpr (is_autodiff_v<T_inv_scale>) {
104 partials<1>(ops_partials)[i]
105 += inc_beta_ddz(alpha_dbl, n_dbl + 1.0, p_dbl) * d_dbl / P_i;
106 }
107 }
108
109 if constexpr (is_autodiff_v<T_shape>) {
110 for (size_t i = 0; i < size_alpha; ++i) {
111 partials<0>(ops_partials)[i] *= P;
112 }
113 }
114
115 if constexpr (is_autodiff_v<T_inv_scale>) {
116 for (size_t i = 0; i < stan::math::size(beta); ++i) {
117 partials<1>(ops_partials)[i] *= P;
118 }
119 }
120
121 return ops_partials.build(P);
122}
123
124} // namespace math
125} // namespace stan
126#endif
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>>.
Definition size.hpp:19
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.
Definition size_zero.hpp:19
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)
Definition inc_beta.hpp:19
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
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
fvar< T > inv(const fvar< T > &x)
Definition inv.hpp:13
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.
Definition digamma.hpp:23
fvar< T > square(const fvar< T > &x)
Definition square.hpp:12
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:56
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 ...