Automatic Differentiation
 
Loading...
Searching...
No Matches
bernoulli_lcdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_BERNOULLI_LCDF_HPP
2#define STAN_MATH_OPENCL_PRIM_BERNOULLI_LCDF_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_lcdf(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 Pi = 1.0 - theta_val;
54 auto cond = n >= 1;
55 auto P_expr = colwise_sum(select(cond, 0.0, log(Pi)));
56 auto deriv = select(cond, 0.0, elt_divide(-1.0, Pi));
57
58 matrix_cl<char> any_n_negative_cl;
60 matrix_cl<double> deriv_cl;
61
62 results(check_theta_bounded, any_n_negative_cl, P_cl, deriv_cl)
63 = expressions(theta_bounded_expr, any_n_negative, P_expr,
65
66 if (from_matrix_cl(any_n_negative_cl).maxCoeff()) {
67 return NEGATIVE_INFTY;
68 }
69
70 T_partials_return P = from_matrix_cl(P_cl).sum();
71 auto ops_partials = make_partials_propagator(theta_col);
72
74 partials<0>(ops_partials) = std::move(deriv_cl);
75 }
76
77 return ops_partials.build(P);
78}
79
80} // namespace math
81} // namespace stan
82#endif
83#endif
Represents an arithmetic matrix on the OpenCL device.
Definition matrix_cl.hpp:47
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
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_lcdf(const T_n_cl &n, const T_prob_cl &theta)
Returns the log CDF 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...