Skip to content

Commit 7ada875

Browse files
authored
Merge pull request #3116 from lingium/feature/issue-3115-beta-neg-binomial-lcdf
add beta_neg_binomial_lcdf
2 parents 8ab9760 + 7514516 commit 7ada875

4 files changed

Lines changed: 248 additions & 3 deletions

File tree

stan/math/prim/prob.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <stan/math/prim/prob/beta_lcdf.hpp>
2727
#include <stan/math/prim/prob/beta_lpdf.hpp>
2828
#include <stan/math/prim/prob/beta_neg_binomial_lccdf.hpp>
29+
#include <stan/math/prim/prob/beta_neg_binomial_lcdf.hpp>
2930
#include <stan/math/prim/prob/beta_neg_binomial_lpmf.hpp>
3031
#include <stan/math/prim/prob/beta_proportion_ccdf_log.hpp>
3132
#include <stan/math/prim/prob/beta_proportion_cdf_log.hpp>

stan/math/prim/prob/beta_neg_binomial_lccdf.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ namespace math {
3434
* @param alpha prior success parameter
3535
* @param beta prior failure parameter
3636
* @param precision precision for `grad_F32`, default \f$10^{-8}\f$
37-
* @param max_steps max iteration allowed for `grad_F32`, default \f$10^{-8}\f$
37+
* @param max_steps max iteration allowed for `grad_F32`, default \f$10^{8}\f$
3838
* @return log probability or log sum of probabilities
3939
* @throw std::domain_error if r, alpha, or beta fails to be positive
4040
* @throw std::invalid_argument if container sizes mismatch
4141
*/
4242
template <typename T_n, typename T_r, typename T_alpha, typename T_beta>
4343
inline return_type_t<T_r, T_alpha, T_beta> beta_neg_binomial_lccdf(
4444
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 = 1e6) {
45+
const double precision = 1e-8, const int max_steps = 1e8) {
4646
static constexpr const char* function = "beta_neg_binomial_lccdf";
4747
check_consistent_sizes(
4848
function, "Failures variable", n, "Number of successes parameter", r,
@@ -100,7 +100,7 @@ inline return_type_t<T_r, T_alpha, T_beta> beta_neg_binomial_lccdf(
100100
std::initializer_list<b_t>{n_dbl + 2.0, a_plus_r + b_plus_n + 1.0},
101101
1.0);
102102
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);
103+
- lgamma(r_dbl) - lbeta(alpha_dbl, beta_dbl) - lgamma(n_dbl + 2.0);
104104
log_ccdf += C + stan::math::log(F);
105105

106106
if constexpr (!is_constant_all<T_r, T_alpha, T_beta>::value) {
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#ifndef STAN_MATH_PRIM_PROB_BETA_NEG_BINOMIAL_LCDF_HPP
2+
#define STAN_MATH_PRIM_PROB_BETA_NEG_BINOMIAL_LCDF_HPP
3+
4+
#include <stan/math/prim/meta.hpp>
5+
#include <stan/math/prim/err.hpp>
6+
#include <stan/math/prim/fun/constants.hpp>
7+
#include <stan/math/prim/fun/digamma.hpp>
8+
#include <stan/math/prim/fun/hypergeometric_3F2.hpp>
9+
#include <stan/math/prim/fun/grad_F32.hpp>
10+
#include <stan/math/prim/fun/lbeta.hpp>
11+
#include <stan/math/prim/fun/lgamma.hpp>
12+
#include <stan/math/prim/fun/max_size.hpp>
13+
#include <stan/math/prim/fun/scalar_seq_view.hpp>
14+
#include <stan/math/prim/fun/size.hpp>
15+
#include <stan/math/prim/fun/size_zero.hpp>
16+
#include <stan/math/prim/functor/partials_propagator.hpp>
17+
#include <cmath>
18+
19+
namespace stan {
20+
namespace math {
21+
22+
/** \ingroup prob_dists
23+
* Returns the log CDF of the Beta-Negative Binomial distribution with given
24+
* number of successes, prior success, and prior failure parameters.
25+
* Given containers of matching sizes, returns the log sum of probabilities.
26+
*
27+
* @tparam T_n type of failure parameter
28+
* @tparam T_r type of number of successes parameter
29+
* @tparam T_alpha type of prior success parameter
30+
* @tparam T_beta type of prior failure parameter
31+
*
32+
* @param n failure parameter
33+
* @param r Number of successes parameter
34+
* @param alpha prior success parameter
35+
* @param beta prior failure parameter
36+
* @param precision precision for `grad_F32`, default \f$10^{-8}\f$
37+
* @param max_steps max iteration allowed for `grad_F32`, default \f$10^{8}\f$
38+
* @return log probability or log sum of probabilities
39+
* @throw std::domain_error if r, alpha, or beta fails to be positive
40+
* @throw std::invalid_argument if container sizes mismatch
41+
*/
42+
template <typename T_n, typename T_r, typename T_alpha, typename T_beta>
43+
inline return_type_t<T_r, T_alpha, T_beta> beta_neg_binomial_lcdf(
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";
47+
check_consistent_sizes(
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+
107+
if constexpr (!is_constant_all<T_r, T_alpha, T_beta>::value) {
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+
116+
if constexpr (!is_constant<T_r>::value || !is_constant<T_alpha>::value) {
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
132+
|| !is_constant<T_beta>::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
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Arguments: Ints, Doubles, Doubles, Doubles
2+
#include <stan/math/prim/prob/beta_neg_binomial_lcdf.hpp>
3+
#include <stan/math/prim/fun/lbeta.hpp>
4+
#include <stan/math/prim/fun/lgamma.hpp>
5+
6+
using stan::math::var;
7+
using std::numeric_limits;
8+
using std::vector;
9+
10+
class AgradCdfLogBetaNegBinomial : public AgradCdfLogTest {
11+
public:
12+
void valid_values(vector<vector<double>>& parameters,
13+
vector<double>& cdf_log) {
14+
vector<double> param(4);
15+
16+
param[0] = 0; // n
17+
param[1] = 1.0; // r
18+
param[2] = 5.0; // alpha
19+
param[3] = 1.0; // beta
20+
parameters.push_back(param);
21+
cdf_log.push_back(std::log(0.833333333333333)); // expected cdf_log
22+
}
23+
24+
void invalid_values(vector<size_t>& index, vector<double>& value) {
25+
// n
26+
27+
// r
28+
index.push_back(1U);
29+
value.push_back(0.0);
30+
31+
index.push_back(1U);
32+
value.push_back(-1.0);
33+
34+
index.push_back(1U);
35+
value.push_back(std::numeric_limits<double>::infinity());
36+
37+
// alpha
38+
index.push_back(2U);
39+
value.push_back(0.0);
40+
41+
index.push_back(2U);
42+
value.push_back(-1.0);
43+
44+
index.push_back(2U);
45+
value.push_back(std::numeric_limits<double>::infinity());
46+
47+
// beta
48+
index.push_back(3U);
49+
value.push_back(0.0);
50+
51+
index.push_back(3U);
52+
value.push_back(-1.0);
53+
54+
index.push_back(3U);
55+
value.push_back(std::numeric_limits<double>::infinity());
56+
}
57+
58+
// BOUND INCLUDED IN ORDER FOR TEST TO PASS WITH CURRENT FRAMEWORK
59+
bool has_lower_bound() { return false; }
60+
61+
bool has_upper_bound() { return false; }
62+
63+
template <typename T_n, typename T_r, typename T_size1, typename T_size2,
64+
typename T4, typename T5>
65+
stan::return_type_t<T_r, T_size1, T_size2> cdf_log(const T_n& n, const T_r& r,
66+
const T_size1& alpha,
67+
const T_size2& beta,
68+
const T4&, const T5&) {
69+
return stan::math::beta_neg_binomial_lcdf(n, r, alpha, beta);
70+
}
71+
72+
template <typename T_n, typename T_r, typename T_size1, typename T_size2,
73+
typename T4, typename T5>
74+
stan::return_type_t<T_r, T_size1, T_size2> cdf_log_function(
75+
const T_n& n, const T_r& r, const T_size1& alpha, const T_size2& beta,
76+
const T4&, const T5&) {
77+
using stan::math::lbeta;
78+
using stan::math::lgamma;
79+
using stan::math::log_sum_exp;
80+
using std::vector;
81+
82+
vector<stan::return_type_t<T_r, T_size1, T_size2>> lpmf_values;
83+
84+
for (int i = 0; i <= n; i++) {
85+
auto lpmf = lbeta(i + r, alpha + beta) - lbeta(r, alpha)
86+
+ lgamma(i + beta) - lgamma(i + 1) - lgamma(beta);
87+
lpmf_values.push_back(lpmf);
88+
}
89+
90+
return log_sum_exp(lpmf_values);
91+
}
92+
};

0 commit comments

Comments
 (0)