Automatic Differentiation
 
Loading...
Searching...
No Matches
binomial_lcdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_BINOMIAL_LCDF_HPP
2#define STAN_MATH_PRIM_PROB_BINOMIAL_LCDF_HPP
3
16#include <cmath>
17
18namespace stan {
19namespace math {
20
37template <typename T_n, typename T_N, typename T_prob>
38return_type_t<T_prob> binomial_lcdf(const T_n& n, const T_N& N,
39 const T_prob& theta) {
40 using T_partials_return = partials_return_t<T_n, T_N, T_prob>;
41 using T_n_ref = ref_type_t<T_n>;
42 using T_N_ref = ref_type_t<T_N>;
43 using T_theta_ref = ref_type_t<T_prob>;
44 using std::exp;
45 using std::log;
46 using std::pow;
47 static constexpr const char* function = "binomial_lcdf";
48 check_consistent_sizes(function, "Successes variable", n,
49 "Population size parameter", N,
50 "Probability parameter", theta);
51
52 T_n_ref n_ref = n;
53 T_N_ref N_ref = N;
54 T_theta_ref theta_ref = theta;
55
56 check_nonnegative(function, "Population size parameter", N_ref);
57 check_bounded(function, "Probability parameter", value_of(theta_ref), 0.0,
58 1.0);
59
60 if (size_zero(n, N, theta)) {
61 return 0;
62 }
63
64 T_partials_return P(0.0);
65 auto ops_partials = make_partials_propagator(theta_ref);
66
67 scalar_seq_view<T_n_ref> n_vec(n_ref);
68 scalar_seq_view<T_N_ref> N_vec(N_ref);
69 scalar_seq_view<T_theta_ref> theta_vec(theta_ref);
70 size_t max_size_seq_view = max_size(n, N, theta);
71
72 // Explicit return for extreme values
73 // The gradients are technically ill-defined,
74 // but treated as negative infinity
75 for (size_t i = 0; i < stan::math::size(n); i++) {
76 if (n_vec.val(i) < 0) {
77 return ops_partials.build(NEGATIVE_INFTY);
78 }
79 }
80
81 for (size_t i = 0; i < max_size_seq_view; i++) {
82 const T_partials_return n_dbl = n_vec.val(i);
83 const T_partials_return N_dbl = N_vec.val(i);
84
85 // Explicit results for extreme values
86 // The gradients are technically ill-defined, but treated as zero
87 if (n_dbl >= N_dbl) {
88 continue;
89 }
90
91 const T_partials_return theta_dbl = theta_vec.val(i);
92 const T_partials_return Pi
93 = inc_beta(N_dbl - n_dbl, n_dbl + 1, 1 - theta_dbl);
94
95 P += log(Pi);
96
98 const T_partials_return denom = beta(N_dbl - n_dbl, n_dbl + 1) * Pi;
99 partials<0>(ops_partials)[i] -= pow(theta_dbl, n_dbl)
100 * pow(1 - theta_dbl, N_dbl - n_dbl - 1)
101 / denom;
102 }
103 }
104 return ops_partials.build(P);
105}
106
107} // namespace math
108} // namespace stan
109#endif
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
return_type_t< T_prob > binomial_lcdf(const T_n &n, const T_N &N, const T_prob &theta)
Returns the log CDF for the binomial distribution evaluated at the specified success,...
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
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
size_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:19
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
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.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
fvar< T > log(const fvar< T > &x)
Definition log.hpp:15
static constexpr double NEGATIVE_INFTY
Negative infinity.
Definition constants.hpp:51
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > inc_beta(const fvar< T > &a, const fvar< T > &b, const fvar< T > &x)
Definition inc_beta.hpp:19
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition pow.hpp:19
fvar< T > beta(const fvar< T > &x1, const fvar< T > &x2)
Return fvar with the beta function applied to the specified arguments and its gradient.
Definition beta.hpp:51
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:55
typename partials_return_type< Args... >::type partials_return_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...