Automatic Differentiation
 
Loading...
Searching...
No Matches
beta_neg_binomial_lcdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_BETA_NEG_BINOMIAL_LCDF_HPP
2#define STAN_MATH_PRIM_PROB_BETA_NEG_BINOMIAL_LCDF_HPP
3
17#include <cmath>
18
19namespace stan {
20namespace math {
21
42template <typename T_n, typename T_r, typename T_alpha, typename T_beta>
44 const T_n& n, const T_r& r, const T_alpha& alpha, const T_beta& beta,
45 const double precision = 1e-8, const int max_steps = 1e8) {
46 static constexpr const char* function = "beta_neg_binomial_lcdf";
48 function, "Failures variable", n, "Number of successes parameter", r,
49 "Prior success parameter", alpha, "Prior failure parameter", beta);
50 if (size_zero(n, r, alpha, beta)) {
51 return 0;
52 }
53
54 using T_r_ref = ref_type_t<T_r>;
55 T_r_ref r_ref = r;
56 using T_alpha_ref = ref_type_t<T_alpha>;
57 T_alpha_ref alpha_ref = alpha;
58 using T_beta_ref = ref_type_t<T_beta>;
59 T_beta_ref beta_ref = beta;
60 check_positive_finite(function, "Number of successes parameter", r_ref);
61 check_positive_finite(function, "Prior success parameter", alpha_ref);
62 check_positive_finite(function, "Prior failure parameter", beta_ref);
63
64 scalar_seq_view<T_n> n_vec(n);
65 scalar_seq_view<T_r_ref> r_vec(r_ref);
66 scalar_seq_view<T_alpha_ref> alpha_vec(alpha_ref);
67 scalar_seq_view<T_beta_ref> beta_vec(beta_ref);
68 int size_n = stan::math::size(n);
69 size_t max_size_seq_view = max_size(n, r, alpha, beta);
70
71 // Explicit return for extreme values
72 // The gradients are technically ill-defined, but treated as zero
73 for (int i = 0; i < size_n; i++) {
74 if (n_vec.val(i) < 0) {
75 return negative_infinity();
76 }
77 }
78
79 using T_partials_return = partials_return_t<T_n, T_r, T_alpha, T_beta>;
80 T_partials_return log_cdf(0.0);
81 auto ops_partials = make_partials_propagator(r_ref, alpha_ref, beta_ref);
82 for (size_t i = 0; i < max_size_seq_view; i++) {
83 // Explicit return for extreme values
84 // The gradients are technically ill-defined, but treated as zero
85 if (n_vec.val(i) == std::numeric_limits<int>::max()) {
86 return 0.0;
87 }
88 auto n_dbl = n_vec.val(i);
89 auto r_dbl = r_vec.val(i);
90 auto alpha_dbl = alpha_vec.val(i);
91 auto beta_dbl = beta_vec.val(i);
92 auto b_plus_n = beta_dbl + n_dbl;
93 auto r_plus_n = r_dbl + n_dbl;
94 auto a_plus_r = alpha_dbl + r_dbl;
95 using a_t = return_type_t<decltype(b_plus_n), decltype(r_plus_n)>;
96 using b_t = return_type_t<decltype(n_dbl), decltype(a_plus_r),
97 decltype(b_plus_n)>;
98 auto F = hypergeometric_3F2(
99 std::initializer_list<a_t>{1.0, b_plus_n + 1.0, r_plus_n + 1.0},
100 std::initializer_list<b_t>{n_dbl + 2.0, a_plus_r + b_plus_n + 1.0},
101 1.0);
102 auto C = lgamma(r_plus_n + 1.0) + lbeta(a_plus_r, b_plus_n + 1.0)
103 - lgamma(r_dbl) - lbeta(alpha_dbl, beta_dbl) - lgamma(n_dbl + 2.0);
104 auto ccdf = stan::math::exp(C) * F;
105 log_cdf += log1m(ccdf);
106
108 auto chain_rule_term = -ccdf / (1.0 - ccdf);
109 auto digamma_n_r_alpha_beta = digamma(a_plus_r + b_plus_n + 1.0);
110 T_partials_return dF[6];
111 grad_F32<false, !is_constant<T_beta>::value, !is_constant_all<T_r>::value,
112 false, true, false>(dF, 1.0, b_plus_n + 1.0, r_plus_n + 1.0,
113 n_dbl + 2.0, a_plus_r + b_plus_n + 1.0, 1.0,
114 precision, max_steps);
115
117 auto digamma_r_alpha = digamma(a_plus_r);
118 if constexpr (!is_constant<T_r>::value) {
119 auto partial_lccdf = digamma(r_plus_n + 1.0)
120 + (digamma_r_alpha - digamma_n_r_alpha_beta)
121 + (dF[2] + dF[4]) / F - digamma(r_dbl);
122 partials<0>(ops_partials)[i] += partial_lccdf * chain_rule_term;
123 }
124 if constexpr (!is_constant<T_alpha>::value) {
125 auto partial_lccdf = digamma_r_alpha - digamma_n_r_alpha_beta
126 + dF[4] / F - digamma(alpha_dbl);
127 partials<1>(ops_partials)[i] += partial_lccdf * chain_rule_term;
128 }
129 }
130
131 if constexpr (!is_constant<T_alpha>::value
133 auto digamma_alpha_beta = digamma(alpha_dbl + beta_dbl);
134 if constexpr (!is_constant<T_alpha>::value) {
135 partials<1>(ops_partials)[i] += digamma_alpha_beta * chain_rule_term;
136 }
137 if constexpr (!is_constant<T_beta>::value) {
138 auto partial_lccdf = digamma(b_plus_n + 1.0) - digamma_n_r_alpha_beta
139 + (dF[1] + dF[4]) / F
140 - (digamma(beta_dbl) - digamma_alpha_beta);
141 partials<2>(ops_partials)[i] += partial_lccdf * chain_rule_term;
142 }
143 }
144 }
145 }
146
147 return ops_partials.build(log_cdf);
148}
149
150} // namespace math
151} // namespace stan
152#endif
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
return_type_t< T_r, T_alpha, T_beta > beta_neg_binomial_lcdf(const T_n &n, const T_r &r, const T_alpha &alpha, const T_beta &beta, const double precision=1e-8, const int max_steps=1e8)
Returns the log CDF of the Beta-Negative Binomial distribution with given number of successes,...
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
auto hypergeometric_3F2(const Ta &a, const Tb &b, const Tz &z)
Hypergeometric function (3F2).
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
static constexpr double e()
Return the base of the natural logarithm.
Definition constants.hpp:20
fvar< T > lbeta(const fvar< T > &x1, const fvar< T > &x2)
Definition lbeta.hpp:14
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
Definition lgamma.hpp:21
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
fvar< T > log1m(const fvar< T > &x)
Definition log1m.hpp:12
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
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 > exp(const fvar< T > &x)
Definition exp.hpp:13
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 ...
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...