Automatic Differentiation
 
Loading...
Searching...
No Matches
poisson_binomial_lccdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_POISSON_BINOMIAL_LCCDF_HPP
2#define STAN_MATH_PRIM_PROB_POISSON_BINOMIAL_LCCDF_HPP
3
19
20namespace stan {
21namespace math {
22
36template <bool propto, typename T_y, typename T_theta>
38 const T_theta& theta) {
39 static constexpr const char* function = "poisson_binomial_lccdf";
40
41 size_t size_theta = size_mvt(theta);
42 if (size_theta > 1) {
43 check_consistent_sizes(function, "Successes variables", y,
44 "Probability parameters", theta);
45 }
46
47 size_t max_sz = std::max(stan::math::size(y), size_mvt(theta));
48 scalar_seq_view<T_y> y_vec(y);
49 vector_seq_view<T_theta> theta_vec(theta);
50
51 for (size_t i = 0; i < max_sz; ++i) {
52 check_bounded(function, "Successes variable", y_vec[i], 0,
53 theta_vec[i].size());
54 check_finite(function, "Probability parameters", theta_vec.val(i));
55 check_bounded(function, "Probability parameters", theta_vec.val(i), 0.0,
56 1.0);
57 }
58
59 return_type_t<T_theta> lccdf = 0.0;
60 for (size_t i = 0; i < max_sz; ++i) {
61 if (stan::math::size(theta_vec[i]) == 1) {
62 if (y_vec[i] == 0) {
63 lccdf += log(theta_vec[i][0]);
64 } else {
65 lccdf -= stan::math::INFTY;
66 }
67 } else {
68 auto x = log1m_exp(
69 log_sum_exp(poisson_binomial_log_probs(y_vec[i], theta_vec[i])));
70 lccdf += x;
71 }
72 }
73 return lccdf;
74}
75
76template <typename T_y, typename T_theta>
78 const T_theta& theta) {
79 return poisson_binomial_lccdf<false>(y, theta);
80}
81
82} // namespace math
83} // namespace stan
84#endif
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
This class provides a low-cost wrapper for situations where you either need an Eigen Vector or RowVec...
return_type_t< T_theta > poisson_binomial_lccdf(const T_y &y, const T_theta &theta)
Returns the log CCDF for the Poisson-binomial distribution evaluated at the specified number of succe...
size_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Definition size.hpp:18
size_t size_mvt(const ScalarT &)
Provides the size of a multivariate argument.
Definition size_mvt.hpp:24
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
plain_type_t< T_theta > poisson_binomial_log_probs(int y, const T_theta &theta)
Returns the last row of the log probability matrix of the Poisson-Binomial distribution given the num...
void check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Check if the value is between the low and high values, inclusively.
fvar< T > log1m_exp(const fvar< T > &x)
Return the natural logarithm of one minus the exponentiation of the specified argument.
Definition log1m_exp.hpp:23
fvar< T > log(const fvar< T > &x)
Definition log.hpp:15
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
static constexpr double INFTY
Positive infinity.
Definition constants.hpp:46
fvar< T > log_sum_exp(const fvar< T > &x1, const fvar< T > &x2)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9