Automatic Differentiation
 
Loading...
Searching...
No Matches
binomial_cdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_BINOMIAL_CDF_HPP
2#define STAN_MATH_PRIM_PROB_BINOMIAL_CDF_HPP
3
14#include <cmath>
15
16namespace stan {
17namespace math {
18
35template <typename T_n, typename T_N, typename T_prob>
36return_type_t<T_prob> binomial_cdf(const T_n& n, const T_N& N,
37 const T_prob& theta) {
38 using T_partials_return = partials_return_t<T_n, T_N, T_prob>;
39 using T_n_ref = ref_type_t<T_n>;
40 using T_N_ref = ref_type_t<T_N>;
41 using T_theta_ref = ref_type_t<T_prob>;
42 using std::exp;
43 using std::pow;
44 static constexpr const char* function = "binomial_cdf";
45 check_consistent_sizes(function, "Successes variable", n,
46 "Population size parameter", N,
47 "Probability parameter", theta);
48
49 T_n_ref n_ref = n;
50 T_N_ref N_ref = N;
51 T_theta_ref theta_ref = theta;
52
53 check_nonnegative(function, "Population size parameter", N_ref);
54 check_bounded(function, "Probability parameter", value_of(theta_ref), 0.0,
55 1.0);
56
57 if (size_zero(n, N, theta)) {
58 return 1.0;
59 }
60
61 T_partials_return P(1.0);
62 auto ops_partials = make_partials_propagator(theta_ref);
63
64 scalar_seq_view<T_n_ref> n_vec(n_ref);
65 scalar_seq_view<T_N_ref> N_vec(N_ref);
66 scalar_seq_view<T_theta_ref> theta_vec(theta_ref);
67 size_t max_size_seq_view = max_size(n, N, theta);
68
69 // Explicit return for extreme values
70 // The gradients are technically ill-defined, but treated as zero
71 for (size_t i = 0; i < stan::math::size(n); i++) {
72 if (n_vec.val(i) < 0) {
73 return ops_partials.build(0.0);
74 }
75 }
76
77 for (size_t i = 0; i < max_size_seq_view; i++) {
78 const T_partials_return n_dbl = n_vec.val(i);
79 const T_partials_return N_dbl = N_vec.val(i);
80
81 // Explicit results for extreme values
82 // The gradients are technically ill-defined, but treated as zero
83 if (n_dbl >= N_dbl) {
84 continue;
85 }
86
87 const T_partials_return theta_dbl = theta_vec.val(i);
88 const T_partials_return Pi
89 = inc_beta(N_dbl - n_dbl, n_dbl + 1, 1 - theta_dbl);
90
91 P *= Pi;
92
94 const T_partials_return denom = beta(N_dbl - n_dbl, n_dbl + 1) * Pi;
95 partials<0>(ops_partials)[i] -= pow(theta_dbl, n_dbl)
96 * pow(1 - theta_dbl, N_dbl - n_dbl - 1)
97 / denom;
98 }
99 }
100
102 for (size_t i = 0; i < stan::math::size(theta); ++i) {
103 partials<0>(ops_partials)[i] *= P;
104 }
105 }
106
107 return ops_partials.build(P);
108}
109
110} // namespace math
111} // namespace stan
112#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_cdf(const T_n &n, const T_N &N, const T_prob &theta)
Returns the CDF for the binomial distribution evaluated at the specified success, population size,...
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
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...