From b6fab81eaf34e4d769d0fa10d06319f89ad5ebb4 Mon Sep 17 00:00:00 2001 From: Zhi Ling <1336265834@qq.com> Date: Wed, 30 Oct 2024 00:19:38 +0800 Subject: [PATCH 1/2] delete beta_neg_binomial_lccdf test --- .../beta_neg_binomial_ccdf_log_test.hpp | 96 ------------------- 1 file changed, 96 deletions(-) delete mode 100644 test/prob/beta_neg_binomial/beta_neg_binomial_ccdf_log_test.hpp diff --git a/test/prob/beta_neg_binomial/beta_neg_binomial_ccdf_log_test.hpp b/test/prob/beta_neg_binomial/beta_neg_binomial_ccdf_log_test.hpp deleted file mode 100644 index 82a3da2eb45..00000000000 --- a/test/prob/beta_neg_binomial/beta_neg_binomial_ccdf_log_test.hpp +++ /dev/null @@ -1,96 +0,0 @@ -// Arguments: Ints, Doubles, Doubles, Doubles -#include -#include -#include - -using stan::math::var; -using std::numeric_limits; -using std::vector; - -class AgradCcdfLogBetaNegBinomial : public AgradCcdfLogTest { - public: - void valid_values(vector>& parameters, - vector& ccdf_log) { - vector param(4); - - param[0] = 10; // n - param[1] = 5.5; // r - param[2] = 2.5; // alpha - param[3] = 0.5; // beta - parameters.push_back(param); - ccdf_log.push_back(std::log(1.0 - 0.967906252841089)); // expected ccdf_log - } - - void invalid_values(vector& index, vector& value) { - // n - - // r - index.push_back(1U); - value.push_back(0.0); - - index.push_back(1U); - value.push_back(-1.0); - - index.push_back(1U); - value.push_back(std::numeric_limits::infinity()); - - // alpha - index.push_back(2U); - value.push_back(0.0); - - index.push_back(2U); - value.push_back(-1.0); - - index.push_back(2U); - value.push_back(std::numeric_limits::infinity()); - - // beta - index.push_back(3U); - value.push_back(0.0); - - index.push_back(3U); - value.push_back(-1.0); - - index.push_back(3U); - value.push_back(std::numeric_limits::infinity()); - } - - // BOUND INCLUDED IN ORDER FOR TEST TO PASS WITH CURRENT FRAMEWORK - bool has_lower_bound() { return false; } - - bool has_upper_bound() { return false; } - - template - stan::return_type_t ccdf_log(const T_n& n, - const T_r& r, - const T_size1& alpha, - const T_size2& beta, - const T4&, const T5&) { - return stan::math::beta_neg_binomial_lccdf(n, r, alpha, beta); - } - - template - stan::return_type_t ccdf_log_function( - const T_n& n, const T_r& r, const T_size1& alpha, const T_size2& beta, - const T4&, const T5&) { - using stan::math::lbeta; - using stan::math::lgamma; - using stan::math::log1m; - using stan::math::log_sum_exp; - using std::vector; - - vector> lpmf_values; - - for (int i = 0; i <= n; i++) { - auto lpmf = lbeta(i + r, alpha + beta) - lbeta(r, alpha) - + lgamma(i + beta) - lgamma(i + 1) - lgamma(beta); - lpmf_values.push_back(lpmf); - } - - auto log_cdf = log_sum_exp(lpmf_values); - - return log1m(exp(log_cdf)); - } -}; From 177994de54bf840fdd7286c7f9346ee4278d6b10 Mon Sep 17 00:00:00 2001 From: Zhi Ling <1336265834@qq.com> Date: Wed, 30 Oct 2024 13:03:30 +0800 Subject: [PATCH 2/2] add beta_neg_binomial_lccdf test back --- .../beta_neg_binomial_ccdf_log_test.hpp | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 test/prob/beta_neg_binomial/beta_neg_binomial_ccdf_log_test.hpp diff --git a/test/prob/beta_neg_binomial/beta_neg_binomial_ccdf_log_test.hpp b/test/prob/beta_neg_binomial/beta_neg_binomial_ccdf_log_test.hpp new file mode 100644 index 00000000000..4871ca77eeb --- /dev/null +++ b/test/prob/beta_neg_binomial/beta_neg_binomial_ccdf_log_test.hpp @@ -0,0 +1,96 @@ +// Arguments: Ints, Doubles, Doubles, Doubles +#include +#include +#include + +using stan::math::var; +using std::numeric_limits; +using std::vector; + +class AgradCcdfLogBetaNegBinomial : public AgradCcdfLogTest { + public: + void valid_values(vector>& parameters, + vector& ccdf_log) { + vector param(4); + + param[0] = 0; // n + param[1] = 1.0; // r + param[2] = 5.0; // alpha + param[3] = 1.0; // beta + parameters.push_back(param); + ccdf_log.push_back(std::log(1.0 - 0.833333333333333)); // expected ccdf_log + } + + void invalid_values(vector& index, vector& value) { + // n + + // r + index.push_back(1U); + value.push_back(0.0); + + index.push_back(1U); + value.push_back(-1.0); + + index.push_back(1U); + value.push_back(std::numeric_limits::infinity()); + + // alpha + index.push_back(2U); + value.push_back(0.0); + + index.push_back(2U); + value.push_back(-1.0); + + index.push_back(2U); + value.push_back(std::numeric_limits::infinity()); + + // beta + index.push_back(3U); + value.push_back(0.0); + + index.push_back(3U); + value.push_back(-1.0); + + index.push_back(3U); + value.push_back(std::numeric_limits::infinity()); + } + + // BOUND INCLUDED IN ORDER FOR TEST TO PASS WITH CURRENT FRAMEWORK + bool has_lower_bound() { return false; } + + bool has_upper_bound() { return false; } + + template + stan::return_type_t ccdf_log(const T_n& n, + const T_r& r, + const T_size1& alpha, + const T_size2& beta, + const T4&, const T5&) { + return stan::math::beta_neg_binomial_lccdf(n, r, alpha, beta); + } + + template + stan::return_type_t ccdf_log_function( + const T_n& n, const T_r& r, const T_size1& alpha, const T_size2& beta, + const T4&, const T5&) { + using stan::math::lbeta; + using stan::math::lgamma; + using stan::math::log1m; + using stan::math::log_sum_exp; + using std::vector; + + vector> lpmf_values; + + for (int i = 0; i <= n; i++) { + auto lpmf = lbeta(i + r, alpha + beta) - lbeta(r, alpha) + + lgamma(i + beta) - lgamma(i + 1) - lgamma(beta); + lpmf_values.push_back(lpmf); + } + + auto log_cdf = log_sum_exp(lpmf_values); + + return log1m(exp(log_cdf)); + } +};