Automatic Differentiation
 
Loading...
Searching...
No Matches
bernoulli_cdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_BERNOULLI_CDF_HPP
2#define STAN_MATH_OPENCL_PRIM_BERNOULLI_CDF_HPP
3#ifdef STAN_OPENCL
4
10
11namespace stan {
12namespace math {
13
26template <
27 typename T_n_cl, typename T_prob_cl,
28 require_all_prim_or_rev_kernel_expression_t<T_n_cl, T_prob_cl>* = nullptr,
29 require_any_not_stan_scalar_t<T_n_cl, T_prob_cl>* = nullptr>
31 const T_prob_cl& theta) {
32 static constexpr const char* function = "bernoulli_cdf(OpenCL)";
33 using T_partials_return = partials_return_t<T_prob_cl>;
34 using std::isnan;
35 constexpr bool is_n_vector = !is_stan_scalar<T_n_cl>::value;
36
37 check_consistent_sizes(function, "Random variable", n,
38 "Probability parameter", theta);
39 const size_t N = is_n_vector ? math::size(n) : math::size(theta);
40 if (N == 0) {
41 return 1.0;
42 }
43
44 const auto& theta_col = as_column_vector_or_scalar(theta);
45 const auto& theta_val = value_of(theta_col);
46
47 auto check_theta_bounded = check_cl(function, "Probability parameter",
48 theta_val, "in the interval [0, 1]");
49 auto theta_bounded_expr = 0.0 <= theta_val && theta_val <= 1.0;
50
51 auto any_n_negative = colwise_max(cast<char>(n < 0));
52 auto cond = n >= 1;
53 auto Pi_uncond = 1.0 - theta_val;
54 auto Pi = select(cond, INFTY, Pi_uncond);
55 auto P_expr = colwise_prod(select(cond, 1.0, Pi_uncond));
56
57 matrix_cl<char> any_n_negative_cl;
60
61 results(check_theta_bounded, any_n_negative_cl, Pi_cl, P_cl)
62 = expressions(theta_bounded_expr, any_n_negative,
64
65 if (from_matrix_cl(any_n_negative_cl).maxCoeff()) {
66 return 0.0;
67 }
68
69 T_partials_return P = from_matrix_cl(P_cl).prod();
70 auto ops_partials = make_partials_propagator(theta_col);
71
73 partials<0>(ops_partials) = elt_divide(-P, Pi_cl);
74 }
75
76 return ops_partials.build(P);
77}
78
79} // namespace math
80} // namespace stan
81#endif
82#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.
auto colwise_prod(T &&a)
Column wise product - reduction 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
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_cdf(const T_n_cl &n, const T_prob_cl &theta)
Returns the CDF of the Bernoulli distribution.
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.
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.
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
static constexpr double INFTY
Positive infinity.
Definition constants.hpp:46
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
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...