Skip to content

Commit c6e8a2b

Browse files
committed
fix issues
1 parent 3cef4b6 commit c6e8a2b

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

stan/math/prim/prob/beta_neg_binomial_lccdf.hpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,16 @@ namespace math {
3333
* @param r Number of successes parameter
3434
* @param alpha prior success parameter
3535
* @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$
3638
* @return log probability or log sum of probabilities
3739
* @throw std::domain_error if r, alpha, or beta fails to be positive
3840
* @throw std::invalid_argument if container sizes mismatch
3941
*/
4042
template <typename T_n, typename T_r, typename T_alpha, typename T_beta>
4143
inline return_type_t<T_r, T_alpha, T_beta> beta_neg_binomial_lccdf(
42-
const T_n& n, const T_r& r, const T_alpha& alpha, const T_beta& 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 = 1e6) {
4346
using std::exp;
4447
using std::log;
4548
using T_partials_return = partials_return_t<T_n, T_r, T_alpha, T_beta>;
@@ -61,7 +64,7 @@ inline return_type_t<T_r, T_alpha, T_beta> beta_neg_binomial_lccdf(
6164
check_positive_finite(function, "Prior success parameter", alpha_ref);
6265
check_positive_finite(function, "Prior failure parameter", beta_ref);
6366

64-
T_partials_return P(0.0);
67+
T_partials_return log_ccdf(0.0);
6568
auto ops_partials = make_partials_propagator(r_ref, alpha_ref, beta_ref);
6669

6770
scalar_seq_view<T_n> n_vec(n);
@@ -93,7 +96,8 @@ inline return_type_t<T_r, T_alpha, T_beta> beta_neg_binomial_lccdf(
9396
T_partials_return r_plus_n = r_dbl + n_dbl;
9497
T_partials_return a_plus_r = alpha_dbl + r_dbl;
9598
T_partials_return one = 1;
96-
T_partials_return precision = 1e-8; // default -6, set -8 to pass all tests
99+
T_partials_return precision_t
100+
= precision; // default -6, set -8 to pass all tests
97101

98102
T_partials_return F
99103
= hypergeometric_3F2({one, b_plus_n + 1, r_plus_n + 1},
@@ -102,15 +106,15 @@ inline return_type_t<T_r, T_alpha, T_beta> beta_neg_binomial_lccdf(
102106
- lgamma(r_dbl) - lbeta(alpha_dbl, beta_dbl)
103107
- lgamma(n_dbl + 2);
104108
T_partials_return ccdf = exp(C) * F;
105-
T_partials_return P_i = log(ccdf);
106-
P += P_i;
109+
T_partials_return log_ccdf_i = log(ccdf);
110+
log_ccdf += log_ccdf_i;
107111

108-
if (!is_constant_all<T_r, T_alpha, T_beta>::value) {
112+
if constexpr (!is_constant_all<T_r, T_alpha, T_beta>::value) {
109113
T_partials_return digamma_n_r_alpha_beta
110114
= digamma(a_plus_r + b_plus_n + 1);
111115
T_partials_return dF[6];
112116
grad_F32(dF, one, b_plus_n + 1, r_plus_n + 1, n_dbl + 2,
113-
a_plus_r + b_plus_n + 1, one, precision, 1e5);
117+
a_plus_r + b_plus_n + 1, one, precision_t, max_steps);
114118

115119
if constexpr (!is_constant<T_r>::value || !is_constant<T_alpha>::value) {
116120
T_partials_return digamma_r_alpha = digamma(a_plus_r);
@@ -143,7 +147,7 @@ inline return_type_t<T_r, T_alpha, T_beta> beta_neg_binomial_lccdf(
143147
}
144148
}
145149

146-
return ops_partials.build(P);
150+
return ops_partials.build(log_ccdf);
147151
}
148152

149153
} // namespace math

test/prob/beta_neg_binomial/beta_neg_binomial_ccdf_log_test.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ class AgradCcdfLogBetaNegBinomial : public AgradCcdfLogTest {
1919
param[3] = 0.5; // beta
2020
parameters.push_back(param);
2121
ccdf_log.push_back(std::log(1.0 - 0.967906252841089)); // expected ccdf_log
22+
23+
param[0] = 0; // n
24+
param[1] = 0.01; // r
25+
param[2] = 100; // alpha
26+
param[3] = 0.01; // beta
27+
parameters.push_back(param);
28+
ccdf_log.push_back(std::log(1.0 - 0.999998995084973)); // expected ccdf_log
2229
}
2330

2431
void invalid_values(vector<size_t>& index, vector<double>& value) {

0 commit comments

Comments
 (0)