|
| 1 | +// Arguments: Ints, Doubles, Doubles, Doubles |
| 2 | +#include <stan/math/prim/prob/beta_neg_binomial_lccdf.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 AgradCcdfLogBetaNegBinomial : public AgradCcdfLogTest { |
| 11 | + public: |
| 12 | + void valid_values(vector<vector<double>>& parameters, |
| 13 | + vector<double>& ccdf_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 | + ccdf_log.push_back(std::log(1.0 - 0.833333333333333)); // expected ccdf_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> ccdf_log(const T_n& n, |
| 66 | + const T_r& r, |
| 67 | + const T_size1& alpha, |
| 68 | + const T_size2& beta, |
| 69 | + const T4&, const T5&) { |
| 70 | + return stan::math::beta_neg_binomial_lccdf(n, r, alpha, beta); |
| 71 | + } |
| 72 | + |
| 73 | + template <typename T_n, typename T_r, typename T_size1, typename T_size2, |
| 74 | + typename T4, typename T5> |
| 75 | + stan::return_type_t<T_r, T_size1, T_size2> ccdf_log_function( |
| 76 | + const T_n& n, const T_r& r, const T_size1& alpha, const T_size2& beta, |
| 77 | + const T4&, const T5&) { |
| 78 | + using stan::math::lbeta; |
| 79 | + using stan::math::lgamma; |
| 80 | + using stan::math::log1m; |
| 81 | + using stan::math::log_sum_exp; |
| 82 | + using std::vector; |
| 83 | + |
| 84 | + vector<stan::return_type_t<T_r, T_size1, T_size2>> lpmf_values; |
| 85 | + |
| 86 | + for (int i = 0; i <= n; i++) { |
| 87 | + auto lpmf = lbeta(i + r, alpha + beta) - lbeta(r, alpha) |
| 88 | + + lgamma(i + beta) - lgamma(i + 1) - lgamma(beta); |
| 89 | + lpmf_values.push_back(lpmf); |
| 90 | + } |
| 91 | + |
| 92 | + auto log_cdf = log_sum_exp(lpmf_values); |
| 93 | + |
| 94 | + return log1m(exp(log_cdf)); |
| 95 | + } |
| 96 | +}; |
0 commit comments