Automatic Differentiation
 
Loading...
Searching...
No Matches
bernoulli_lccdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_BERNOULLI_LCCDF_HPP
2#define STAN_MATH_OPENCL_PRIM_BERNOULLI_LCCDF_HPP
3#ifdef STAN_OPENCL
4
11
12namespace stan {
13namespace math {
14
27template <
28 typename T_n_cl, typename T_prob_cl,
29 require_all_prim_or_rev_kernel_expression_t<T_n_cl, T_prob_cl>* = nullptr,
30 require_any_not_stan_scalar_t<T_n_cl, T_prob_cl>* = nullptr>
32 const T_prob_cl& theta) {
33 static constexpr const char* function = "bernoulli_lccdf(OpenCL)";
34 using T_partials_return = partials_return_t<T_prob_cl>;
35 using std::isnan;
36 constexpr bool is_n_vector = !is_stan_scalar<T_n_cl>::value;
37
38 check_consistent_sizes(function, "Random variable", n,
39 "Probability parameter", theta);
40 const size_t N = is_n_vector ? math::size(n) : math::size(theta);
41 if (N == 0) {
42 return 0.0;
43 }
44
45 const auto& theta_col = as_column_vector_or_scalar(theta);
46 const auto& theta_val = value_of(theta_col);
47
48 auto check_theta_bounded = check_cl(function, "Probability parameter",
49 theta_val, "in the interval [0, 1]");
50 auto theta_bounded_expr = 0.0 <= theta_val && theta_val <= 1.0;
51
52 auto any_n_negative = colwise_max(cast<char>(n < 0));
53 auto any_n_over_one = colwise_max(cast<char>(n >= 1));
54 auto P_expr = colwise_sum(log(theta_val));
55 auto deriv = elt_divide(1.0, theta_val);
56
57 matrix_cl<char> any_n_negative_cl;
58 matrix_cl<char> any_n_over_one_cl;
60 matrix_cl<double> deriv_cl;
61
62 results(check_theta_bounded, any_n_negative_cl, any_n_over_one_cl, P_cl,
63 deriv_cl)
64 = expressions(theta_bounded_expr, any_n_negative, any_n_over_one, P_expr,
66
67 if (from_matrix_cl(any_n_negative_cl).maxCoeff()) {
68 return 0.0;
69 }
70 if (from_matrix_cl(any_n_over_one_cl).maxCoeff()) {
71 return NEGATIVE_INFTY;
72 }
73
74 T_partials_return P = from_matrix_cl(P_cl).sum();
75 auto ops_partials = make_partials_propagator(theta_col);
76
78 partials<0>(ops_partials) = std::move(deriv_cl);
79 }
80
81 return ops_partials.build(P);
82}
83
84} // namespace math
85} // namespace stan
86#endif
87#endif
Represents an arithmetic matrix on the OpenCL device.
Definition matrix_cl.hpp:47
auto check_cl(const char *function, const char *var_name, T &&y, const char *must_be)
Constructs a check on opencl matrix or expression.
Definition check_cl.hpp:219
results_cl< T_results... > results(T_results &&... results)
Deduces types for constructing results_cl object.
auto as_column_vector_or_scalar(T &&a)
as_column_vector_or_scalar of a kernel generator expression.
elt_divide_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > elt_divide(T_a &&a, T_b &&b)
auto colwise_max(T &&a)
Column wise max - reduction of a kernel generator expression.
calc_if_< true, as_operation_cl_t< T > > calc_if(T &&a)
Definition calc_if.hpp:121
auto colwise_sum(T &&a)
Column wise sum - reduction of a kernel generator expression.
expressions_cl< T_expressions... > expressions(T_expressions &&... expressions)
Deduces types for constructing expressions_cl object.
auto from_matrix_cl(const T &src)
Copies the source matrix that is stored on the OpenCL device to the destination Eigen matrix.
Definition copy.hpp:61
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
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:18
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.
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
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 ...
bool isnan(const stan::math::var &a)
Checks if the given number is NaN.
Definition std_isnan.hpp:18
Checks if decayed type is a var, fvar, or arithmetic.
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...