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_shape& alpha,
27 const T_inv_scale& beta) {
28 using T_partials_return = partials_return_t<T_n, T_shape, T_inv_scale>;
29 using T_n_ref = ref_type_t<T_n>;
30 using T_alpha_ref = ref_type_t<T_shape>;
31 using T_beta_ref = ref_type_t<T_inv_scale>;
32 static constexpr const char* function = "neg_binomial_cdf";
33 check_consistent_sizes(function, "Failures variable", n, "Shape parameter",
34 alpha, "Inverse scale parameter", beta);
35 T_n_ref n_ref = n;
36 T_alpha_ref alpha_ref = alpha;
37 T_beta_ref beta_ref = beta;
38 check_positive_finite(function, "Shape parameter", alpha_ref);
39 check_positive_finite(function, "Inverse scale parameter", beta_ref);
40
41 if (size_zero(n, alpha, beta)) {
42 return 1.0;
43 }
44
45 T_partials_return P(1.0);
46 auto ops_partials = make_partials_propagator(alpha_ref, beta_ref);
47
48 scalar_seq_view<T_n_ref> n_vec(n_ref);
49 scalar_seq_view<T_alpha_ref> alpha_vec(alpha_ref);
50 scalar_seq_view<T_beta_ref> beta_vec(beta_ref);
51 size_t size_alpha = stan::math::size(alpha);
52 size_t size_n_alpha = max_size(n, alpha);
53 size_t max_size_seq_view = max_size(n, alpha, beta);
54
55 // Explicit return for extreme values
56 // The gradients are technically ill-defined, but treated as zero
57 for (size_t i = 0; i < stan::math::size(n); i++) {
58 if (n_vec.val(i) < 0) {
59 return ops_partials.build(0.0);
60 }
61 }
62
63 VectorBuilder<!is_constant_all<T_shape>::value, T_partials_return, T_shape>
64 digamma_alpha_vec(size_alpha);
66 T_shape>
67 digamma_sum_vec(size_n_alpha);
68
70 for (size_t i = 0; i < size_alpha; i++) {
71 digamma_alpha_vec[i] = digamma(alpha_vec.val(i));
72 }
73 for (size_t i = 0; i < size_n_alpha; i++) {
74 const T_partials_return n_dbl = n_vec.val(i);
75 const T_partials_return alpha_dbl = alpha_vec.val(i);
76 digamma_sum_vec[i] = digamma(n_dbl + alpha_dbl + 1);
77 }
78 }
79
80 for (size_t i = 0; i < max_size_seq_view; i++) {
81 // Explicit results for extreme values
82 // The gradients are technically ill-defined, but treated as zero
83 if (n_vec.val(i) == std::numeric_limits<int>::max()) {
84 return ops_partials.build(1.0);
85 }
86
87 const T_partials_return n_dbl = n_vec.val(i);
88 const T_partials_return alpha_dbl = alpha_vec.val(i);
89 const T_partials_return beta_dbl = beta_vec.val(i);
90 const T_partials_return inv_beta_p1 = inv(beta_dbl + 1);
91 const T_partials_return p_dbl = beta_dbl * inv_beta_p1;
92 const T_partials_return d_dbl = square(inv_beta_p1);
93
94 const T_partials_return P_i = inc_beta(alpha_dbl, n_dbl + 1.0, p_dbl);
95
96 P *= P_i;
97
99 partials<0>(ops_partials)[i]
100 += inc_beta_dda(alpha_dbl, n_dbl + 1, p_dbl, digamma_alpha_vec[i],
101 digamma_sum_vec[i])
102 / P_i;
103 }
104
106 partials<1>(ops_partials)[i]
107 += inc_beta_ddz(alpha_dbl, n_dbl + 1.0, p_dbl) * d_dbl / P_i;
108 }
109 }
110
112 for (size_t i = 0; i < size_alpha; ++i) {
113 partials<0>(ops_partials)[i] *= P;
114 }
115 }
116
118 for (size_t i = 0; i < stan::math::size(beta); ++i) {
119 partials<1>(ops_partials)[i] *= P;
120 }
121 }
122
123 return ops_partials.build(P);
124}
125
126} // namespace math
127} // namespace stan
128#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:12
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:55
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...