Automatic Differentiation
 
Loading...
Searching...
No Matches
bernoulli_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_BERNOULLI_LPMF_HPP
2#define STAN_MATH_OPENCL_PRIM_BERNOULLI_LPMF_HPP
3#ifdef STAN_OPENCL
4
12
13namespace stan {
14namespace math {
15
28template <
29 bool propto, typename T_n_cl, typename T_prob_cl,
30 require_all_prim_or_rev_kernel_expression_t<T_n_cl, T_prob_cl>* = nullptr,
31 require_any_not_stan_scalar_t<T_n_cl, T_prob_cl>* = nullptr>
33 const T_prob_cl& theta) {
34 static constexpr const char* function = "bernoulli_lpmf(OpenCL)";
35 using T_partials_return = partials_return_t<T_prob_cl>;
36 constexpr bool is_n_vector = !is_stan_scalar<T_n_cl>::value;
37 constexpr bool is_theta_vector = !is_stan_scalar<T_prob_cl>::value;
38
39 check_consistent_sizes(function, "Random variable", n,
40 "Probability parameter", theta);
41 const size_t N = is_n_vector ? math::size(n) : math::size(theta);
42 if (N == 0) {
43 return 0.0;
44 }
46 return 0.0;
47 }
48
49 const auto& theta_col = as_column_vector_or_scalar(theta);
50 const auto& theta_val = value_of(theta_col);
51
52 T_partials_return logp(0.0);
53 auto ops_partials = make_partials_propagator(theta_col);
54
55 auto check_n_bounded = check_cl(function, "n", n, "in the interval [0, 1]");
56 auto n_bounded_expr = 0 <= n && n <= 1;
57
58 if (is_theta_vector) {
59 auto logp_expr
60 = colwise_sum(select(n == 1, log(theta_val), log1p(-theta_val)));
61 auto deriv_expr = inv(theta_val + select(n == 1, 0, -1));
62
63 auto check_theta_bounded = check_cl(function, "Probability parameter",
64 theta_val, "in the interval [0, 1]");
65 auto theta_bounded_expr = 0 <= theta_val && theta_val <= 1;
66
67 matrix_cl<double> logp_cl;
68 matrix_cl<double> deriv_cl;
69
70 results(logp_cl, deriv_cl, check_n_bounded, check_theta_bounded)
71 = expressions(logp_expr,
73 n_bounded_expr, theta_bounded_expr);
74
75 logp = sum(from_matrix_cl(logp_cl));
76
78 partials<0>(ops_partials) = deriv_cl;
79 }
80 } else {
81 auto n_sum_expr = rowwise_sum(forward_as<matrix_cl<int>>(n));
82
83 matrix_cl<int> n_sum_cl;
84
85 results(n_sum_cl, check_n_bounded)
86 = expressions(n_sum_expr, n_bounded_expr);
87
88 size_t n_sum = sum(from_matrix_cl(n_sum_cl));
89 double theta_val_scal = forward_as<double>(theta_val);
90 if (n_sum == N) {
91 logp = N * log(theta_val_scal);
92 } else if (n_sum == 0) {
93 logp = N * log1m(theta_val_scal);
94 } else {
95 logp = n_sum * log(theta_val_scal) + (N - n_sum) * log1m(theta_val_scal);
96 }
98 double& edge1_partial = forward_as<internal::broadcast_array<double>>(
99 partials<0>(ops_partials))[0];
100 if (n_sum == N) {
101 edge1_partial += N / theta_val_scal;
102 } else if (n_sum == 0) {
103 edge1_partial += N / (theta_val_scal - 1);
104 } else {
105 edge1_partial
106 += n_sum / theta_val_scal + (N - n_sum) / (theta_val_scal - 1);
107 }
108 }
109 }
110 return ops_partials.build(logp);
111}
112
113} // namespace math
114} // namespace stan
115#endif
116#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 rowwise_sum(T &&a)
Rowwise sum 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.
return_type_t< T_prob_cl > bernoulli_lpmf(const T_n_cl &n, const T_prob_cl &theta)
Returns the log PMF of the Bernoulli distribution.
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
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
T_actual && forward_as(T_actual &&a)
Assume which type we get.
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
fvar< T > log(const fvar< T > &x)
Definition log.hpp:15
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 > log1p(const fvar< T > &x)
Definition log1p.hpp:12
fvar< T > log1m(const fvar< T > &x)
Definition log1m.hpp:12
fvar< T > inv(const fvar< T > &x)
Definition inv.hpp:12
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 ...
Definition fvar.hpp:9
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...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...