Automatic Differentiation
 
Loading...
Searching...
No Matches
beta_binomial_lccdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_BETA_BINOMIAL_LCCDF_HPP
2#define STAN_MATH_PRIM_PROB_BETA_BINOMIAL_LCCDF_HPP
3
20#include <cmath>
21
22namespace stan {
23namespace math {
24
43template <typename T_n, typename T_N, typename T_size1, typename T_size2>
45 const T_size1& alpha,
46 const T_size2& beta) {
47 using T_partials_return = partials_return_t<T_n, T_N, T_size1, T_size2>;
48 using std::exp;
49 using std::log;
50 using T_N_ref = ref_type_t<T_N>;
51 using T_alpha_ref = ref_type_t<T_size1>;
52 using T_beta_ref = ref_type_t<T_size2>;
53 static constexpr const char* function = "beta_binomial_lccdf";
54 check_consistent_sizes(function, "Successes variable", n,
55 "Population size parameter", N,
56 "First prior sample size parameter", alpha,
57 "Second prior sample size parameter", beta);
58 if (size_zero(n, N, alpha, beta)) {
59 return 0;
60 }
61
62 T_N_ref N_ref = N;
63 T_alpha_ref alpha_ref = alpha;
64 T_beta_ref beta_ref = beta;
65 check_nonnegative(function, "Population size parameter", N_ref);
66 check_positive_finite(function, "First prior sample size parameter",
67 alpha_ref);
68 check_positive_finite(function, "Second prior sample size parameter",
69 beta_ref);
70
71 T_partials_return P(0.0);
72 auto ops_partials = make_partials_propagator(alpha_ref, beta_ref);
73
74 scalar_seq_view<T_n> n_vec(n);
75 scalar_seq_view<T_N_ref> N_vec(N_ref);
76 scalar_seq_view<T_alpha_ref> alpha_vec(alpha_ref);
77 scalar_seq_view<T_beta_ref> beta_vec(beta_ref);
78 size_t max_size_seq_view = max_size(n, N, alpha, beta);
79
80 // Explicit return for extreme values
81 // The gradients are technically ill-defined, but treated as zero
82 for (size_t i = 0; i < stan::math::size(n); i++) {
83 if (n_vec.val(i) < 0) {
84 return ops_partials.build(0.0);
85 }
86 }
87
88 for (size_t i = 0; i < max_size_seq_view; i++) {
89 // Explicit results for extreme values
90 // The gradients are technically ill-defined, but treated as neg infinity
91 if (n_vec.val(i) >= N_vec.val(i)) {
92 return ops_partials.build(negative_infinity());
93 }
94
95 const T_partials_return n_dbl = n_vec.val(i);
96 const T_partials_return N_dbl = N_vec.val(i);
97 const T_partials_return alpha_dbl = alpha_vec.val(i);
98 const T_partials_return beta_dbl = beta_vec.val(i);
99 const T_partials_return mu = alpha_dbl + n_dbl + 1;
100 const T_partials_return nu = beta_dbl + N_dbl - n_dbl - 1;
101 const T_partials_return one = 1;
102
103 const T_partials_return F = hypergeometric_3F2(
104 {one, mu, -N_dbl + n_dbl + 1}, {n_dbl + 2, 1 - nu}, one);
105 T_partials_return C = lbeta(nu, mu) - lbeta(alpha_dbl, beta_dbl)
106 - lbeta(N_dbl - n_dbl, n_dbl + 2);
107 C = F * exp(C) / (N_dbl + 1);
108
109 const T_partials_return Pi = C;
110
111 P += log(Pi);
112
113 T_partials_return digammaDiff
115 ? 0
116 : digamma(alpha_dbl + beta_dbl) - digamma(mu + nu);
117
118 T_partials_return dF[6];
120 grad_F32(dF, one, mu, -N_dbl + n_dbl + 1, n_dbl + 2, 1 - nu, one);
121 }
123 partials<0>(ops_partials)[i]
124 += digamma(mu) - digamma(alpha_dbl) + digammaDiff + dF[1] / F;
125 }
127 partials<1>(ops_partials)[i]
128 += digamma(nu) - digamma(beta_dbl) + digammaDiff - dF[4] / F;
129 }
130 }
131
132 return ops_partials.build(P);
133}
134
135} // namespace math
136} // namespace stan
137#endif
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
return_type_t< T_size1, T_size2 > beta_binomial_lccdf(const T_n &n, const T_N &N, const T_size1 &alpha, const T_size2 &beta)
Returns the log CCDF of the Beta-Binomial distribution with given population size,...
size_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:18
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
auto hypergeometric_3F2(const Ta &a, const Tb &b, const Tz &z)
Hypergeometric function (3F2).
static constexpr double negative_infinity()
Return negative infinity.
size_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:19
void grad_F32(T *g, const T &a1, const T &a2, const T &a3, const T &b1, const T &b2, const T &z, const T &precision=1e-6, int max_steps=1e5)
Gradients of the hypergeometric function, 3F2.
Definition grad_F32.hpp:39
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
fvar< T > lbeta(const fvar< T > &x1, const fvar< T > &x2)
Definition lbeta.hpp:14
fvar< T > log(const fvar< T > &x)
Definition log.hpp:15
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
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 ...
Definition fvar.hpp:9
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...