Automatic Differentiation
 
Loading...
Searching...
No Matches
bernoulli_lccdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_BERNOULLI_LCCDF_HPP
2#define STAN_MATH_PRIM_PROB_BERNOULLI_LCCDF_HPP
3
13
14namespace stan {
15namespace math {
16
29template <typename T_n, typename T_prob,
31 T_n, T_prob>* = nullptr>
32return_type_t<T_prob> bernoulli_lccdf(const T_n& n, const T_prob& theta) {
33 using T_theta_ref = ref_type_t<T_prob>;
34 static constexpr const char* function = "bernoulli_lccdf";
35 check_consistent_sizes(function, "Random variable", n,
36 "Probability parameter", theta);
37 T_theta_ref theta_ref = theta;
38 const auto& n_arr = as_value_column_array_or_scalar(n);
39 const auto& theta_arr = as_value_column_array_or_scalar(theta_ref);
40 check_bounded(function, "Probability parameter", theta_arr, 0.0, 1.0);
41
42 if (size_zero(n, theta)) {
43 return 0.0;
44 }
45
46 auto ops_partials = make_partials_propagator(theta_ref);
47
48 // Explicit return for extreme values
49 // The gradients are technically ill-defined, but treated as zero
50 if (any(n_arr < 0)) {
51 return ops_partials.build(0.0);
52 } else if (any(n_arr >= 1)) {
53 return ops_partials.build(NEGATIVE_INFTY);
54 }
55
56 size_t theta_size = math::size(theta_arr);
57 size_t n_size = math::size(n_arr);
58 double broadcast_n = theta_size == n_size ? 1 : n_size;
59
61 partials<0>(ops_partials) = inv(theta_arr) * broadcast_n;
62 }
63
64 return ops_partials.build(sum(log(theta_arr)) * broadcast_n);
65}
66
67} // namespace math
68} // namespace stan
69#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.
return_type_t< T_prob_cl > bernoulli_lccdf(const T_n_cl &n, const T_prob_cl &theta)
Returns the log CCDF of the Bernoulli distribution.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
int64_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:19
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
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
static constexpr double NEGATIVE_INFTY
Negative infinity.
Definition constants.hpp:51
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.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
fvar< T > inv(const fvar< T > &x)
Definition inv.hpp:13
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
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...