Automatic Differentiation
 
Loading...
Searching...
No Matches
bernoulli_cdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_BERNOULLI_CDF_HPP
2#define STAN_MATH_PRIM_PROB_BERNOULLI_CDF_HPP
3
11
12namespace stan {
13namespace math {
14
27template <typename T_n, typename T_prob,
29 T_n, T_prob>* = nullptr>
30return_type_t<T_prob> bernoulli_cdf(const T_n& n, const T_prob& theta) {
31 using T_partials_return = partials_return_t<T_n, T_prob>;
32 using T_theta_ref = ref_type_t<T_prob>;
33 static constexpr const char* function = "bernoulli_cdf";
34 check_consistent_sizes(function, "Random variable", n,
35 "Probability parameter", theta);
36 T_theta_ref theta_ref = theta;
37 const auto& n_arr = as_value_column_array_or_scalar(n);
38 const auto& theta_arr = as_value_column_array_or_scalar(theta_ref);
39 check_bounded(function, "Probability parameter", theta_arr, 0.0, 1.0);
40
41 if (size_zero(n, theta)) {
42 return 1.0;
43 }
44
45 auto ops_partials = make_partials_propagator(theta_ref);
46
47 // Explicit return for extreme values
48 // The gradients are technically ill-defined, but treated as zero
49 if (any(n_arr < 0)) {
50 return ops_partials.build(0.0);
51 }
52 const auto& log1m_theta = select(theta_arr == 1, 0.0, log1m(theta_arr));
53 const auto& P1 = select(n_arr == 0, log1m_theta, 0.0);
54
55 T_partials_return P = sum(P1);
56
58 partials<0>(ops_partials) = select(n_arr == 0, -exp(P - P1), 0.0);
59 }
60 return ops_partials.build(exp(P));
61}
62
63} // namespace math
64} // namespace stan
65#endif
require_all_not_t< is_nonscalar_prim_or_rev_kernel_expression< std::decay_t< Types > >... > require_all_not_nonscalar_prim_or_rev_kernel_expression_t
Require none of the types satisfy is_nonscalar_prim_or_rev_kernel_expression.
select_< as_operation_cl_t< T_condition >, as_operation_cl_t< T_then >, as_operation_cl_t< T_else > > select(T_condition &&condition, T_then &&then, T_else &&els)
Selection operation on kernel generator expressions.
Definition select.hpp:148
return_type_t< T_prob_cl > bernoulli_cdf(const T_n_cl &n, const T_prob_cl &theta)
Returns the CDF of the Bernoulli distribution.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
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.
constexpr bool any(T x)
Return true if any values in the input are true.
Definition any.hpp:21
auto as_value_column_array_or_scalar(T &&a)
Extract the value from an object and for eigen vectors and std::vectors convert to an eigen column ar...
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:22
fvar< T > log1m(const fvar< T > &x)
Definition log1m.hpp:12
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:13
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...