Automatic Differentiation
 
Loading...
Searching...
No Matches
neg_binomial_lcdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_NEG_BINOMIAL_LCDF_HPP
2#define STAN_MATH_PRIM_PROB_NEG_BINOMIAL_LCDF_HPP
3
20#include <cmath>
21#include <limits>
22
23namespace stan {
24namespace math {
25
26template <typename T_n, typename T_shape, typename T_inv_scale>
28 const T_n& n, const T_shape& alpha, const T_inv_scale& beta_param) {
29 using T_partials_return = partials_return_t<T_n, T_shape, T_inv_scale>;
30 using std::exp;
31 using std::log;
32 using std::pow;
33 using T_n_ref = ref_type_t<T_n>;
34 using T_alpha_ref = ref_type_t<T_shape>;
35 using T_beta_ref = ref_type_t<T_inv_scale>;
36 static constexpr const char* function = "neg_binomial_lcdf";
37 check_consistent_sizes(function, "Failures variable", n, "Shape parameter",
38 alpha, "Inverse scale parameter", beta_param);
39 T_n_ref n_ref = n;
40 T_alpha_ref alpha_ref = alpha;
41 T_beta_ref beta_ref = beta_param;
42 check_positive_finite(function, "Shape parameter", alpha_ref);
43 check_positive_finite(function, "Inverse scale parameter", beta_ref);
44
45 if (size_zero(n, alpha, beta_param)) {
46 return 0;
47 }
48
49 T_partials_return P(0.0);
50 auto ops_partials = make_partials_propagator(alpha_ref, beta_ref);
51
52 scalar_seq_view<T_n_ref> n_vec(n_ref);
53 scalar_seq_view<T_alpha_ref> alpha_vec(alpha_ref);
54 scalar_seq_view<T_beta_ref> beta_vec(beta_ref);
55 size_t size_n = stan::math::size(n);
56 size_t size_alpha = stan::math::size(alpha);
57 size_t size_n_alpha = max_size(n, alpha);
58 size_t max_size_seq_view = max_size(n, alpha, beta_param);
59
60 // Explicit return for extreme values
61 // The gradients are technically ill-defined, but treated as zero
62 for (size_t i = 0; i < size_n; i++) {
63 if (n_vec.val(i) < 0) {
64 return ops_partials.build(negative_infinity());
65 }
66 }
67
69 digammaN_vec(size_n);
70 VectorBuilder<!is_constant_all<T_shape>::value, T_partials_return, T_shape>
71 digammaAlpha_vec(size_alpha);
73 T_shape>
74 digammaSum_vec(size_n_alpha);
75
77 for (size_t i = 0; i < size_n; i++) {
78 digammaN_vec[i] = digamma(n_vec.val(i) + 1);
79 }
80 for (size_t i = 0; i < size_alpha; i++) {
81 digammaAlpha_vec[i] = digamma(alpha_vec.val(i));
82 }
83 for (size_t i = 0; i < size_n_alpha; i++) {
84 const T_partials_return n_dbl = n_vec.val(i);
85 const T_partials_return alpha_dbl = alpha_vec.val(i);
86 digammaSum_vec[i] = digamma(n_dbl + alpha_dbl + 1);
87 }
88 }
89
90 for (size_t i = 0; i < max_size_seq_view; i++) {
91 // Explicit results for extreme values
92 // The gradients are technically ill-defined, but treated as zero
93 if (n_vec.val(i) == std::numeric_limits<int>::max()) {
94 return ops_partials.build(0.0);
95 }
96
97 const T_partials_return n_dbl = n_vec.val(i);
98 const T_partials_return alpha_dbl = alpha_vec.val(i);
99 const T_partials_return beta_dbl = beta_vec.val(i);
100 const T_partials_return inv_beta_p1 = inv(beta_dbl + 1);
101 const T_partials_return p_dbl = beta_dbl * inv_beta_p1;
102 const T_partials_return d_dbl = square(inv_beta_p1);
103 const T_partials_return Pi = inc_beta(alpha_dbl, n_dbl + 1.0, p_dbl);
104 const T_partials_return beta_func = beta(n_dbl + 1, alpha_dbl);
105
106 P += log(Pi);
107
109 T_partials_return g1 = 0;
110 T_partials_return g2 = 0;
111
112 grad_reg_inc_beta(g1, g2, alpha_dbl, n_dbl + 1, p_dbl,
113 digammaAlpha_vec[i], digammaN_vec[i], digammaSum_vec[i],
114 beta_func);
115 partials<0>(ops_partials)[i] += g1 / Pi;
116 }
118 partials<1>(ops_partials)[i] += d_dbl * pow(1 - p_dbl, n_dbl)
119 * pow(p_dbl, alpha_dbl - 1)
120 / (beta_func * Pi);
121 }
122 }
123
124 return ops_partials.build(P);
125}
126
127} // namespace math
128} // namespace stan
129#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
static constexpr double negative_infinity()
Return negative infinity.
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
auto pow(const T1 &x1, const T2 &x2)
Definition pow.hpp:32
fvar< T > log(const fvar< T > &x)
Definition log.hpp:15
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.
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > inc_beta(const fvar< T > &a, const fvar< T > &b, const fvar< T > &x)
Definition inc_beta.hpp:19
return_type_t< T_shape, T_inv_scale > neg_binomial_lcdf(const T_n &n, const T_shape &alpha, const T_inv_scale &beta_param)
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...