|
| 1 | +#ifndef STAN_MATH_PRIM_PROB_POISSON_BINOMIAL_LCCDF_HPP |
| 2 | +#define STAN_MATH_PRIM_PROB_POISSON_BINOMIAL_LCCDF_HPP |
| 3 | + |
| 4 | +#include <stan/math/prim/meta.hpp> |
| 5 | +#include <stan/math/prim/err.hpp> |
| 6 | +#include <stan/math/prim/fun/binomial_coefficient_log.hpp> |
| 7 | +#include <stan/math/prim/fun/log.hpp> |
| 8 | +#include <stan/math/prim/fun/log1m.hpp> |
| 9 | +#include <stan/math/prim/fun/log_sum_exp.hpp> |
| 10 | +#include <stan/math/prim/fun/log1m_exp.hpp> |
| 11 | +#include <stan/math/prim/fun/max_size.hpp> |
| 12 | +#include <stan/math/prim/fun/multiply_log.hpp> |
| 13 | +#include <stan/math/prim/fun/size.hpp> |
| 14 | +#include <stan/math/prim/fun/size_zero.hpp> |
| 15 | +#include <stan/math/prim/fun/value_of.hpp> |
| 16 | +#include <stan/math/prim/fun/poisson_binomial_log_probs.hpp> |
| 17 | + |
| 18 | +namespace stan { |
| 19 | +namespace math { |
| 20 | + |
| 21 | +/** \ingroup prob_dists |
| 22 | + * Returns the log CCDF for the Poisson-binomial distribution evaluated at the |
| 23 | + * specified number of successes and probabilities of successes. |
| 24 | + * |
| 25 | + * @tparam T_y type of number of successes parameter |
| 26 | + * @tparam T_theta type of chance of success parameters |
| 27 | + * @param y input scalar, vector, or nested vector of numbers of successes |
| 28 | + * @param theta array of chances of success parameters |
| 29 | + * @return sum of log probabilities |
| 30 | + * @throw std::domain_error if y is out of bounds |
| 31 | + * @throw std::domain_error if theta is not a valid vector of probabilities |
| 32 | + * @throw std::invalid_argument If y and theta are different lengths |
| 33 | + */ |
| 34 | +template <bool propto, typename T_y, typename T_theta> |
| 35 | +return_type_t<T_theta> poisson_binomial_lccdf(const T_y& y, |
| 36 | + const T_theta& theta) { |
| 37 | + static const char* function = "poisson_binomial_lccdf"; |
| 38 | + |
| 39 | + size_t size_theta = size_mvt(theta); |
| 40 | + if (size_theta > 1) { |
| 41 | + check_consistent_sizes(function, "Successes variables", y, |
| 42 | + "Probability parameters", theta); |
| 43 | + } |
| 44 | + |
| 45 | + size_t max_sz = std::max(stan::math::size(y), size_mvt(theta)); |
| 46 | + scalar_seq_view<T_y> y_vec(y); |
| 47 | + vector_seq_view<T_theta> theta_vec(theta); |
| 48 | + |
| 49 | + for (size_t i = 0; i < max_sz; ++i) { |
| 50 | + check_bounded(function, "Successes variable", y_vec[i], 0, |
| 51 | + theta_vec[i].size()); |
| 52 | + check_finite(function, "Probability parameters", theta_vec[i]); |
| 53 | + check_bounded(function, "Probability parameters", theta_vec[i], 0.0, 1.0); |
| 54 | + } |
| 55 | + |
| 56 | + return sum(log1m_exp(log_sum_exp(poisson_binomial_log_probs(y, theta)))); |
| 57 | +} |
| 58 | + |
| 59 | +template <typename T_y, typename T_theta> |
| 60 | +return_type_t<T_theta> poisson_binomial_lccdf(const T_y& y, |
| 61 | + const T_theta& theta) { |
| 62 | + return poisson_binomial_lccdf<false>(y, theta); |
| 63 | +} |
| 64 | + |
| 65 | +} // namespace math |
| 66 | +} // namespace stan |
| 67 | +#endif |
0 commit comments