1#ifndef STAN_MATH_OPENCL_PRIM_BINOMIAL_LPMF_HPP
2#define STAN_MATH_OPENCL_PRIM_BINOMIAL_LPMF_HPP
31template <
bool propto,
typename T_n_cl,
typename T_N_cl,
typename T_prob_cl,
33 T_prob_cl>* =
nullptr,
35 T_n_cl, T_N_cl, T_prob_cl>* =
nullptr>
37 const T_prob_cl& theta) {
38 static constexpr const char* function =
"binomial_lpmf(OpenCL)";
43 "Population size parameter", N,
44 "Probability parameter", theta);
45 const size_t siz =
max_size(n, N, theta);
54 const auto& theta_val =
value_of(theta_col);
57 =
check_cl(function,
"Successes variable", n,
"in the interval [0, N]");
58 auto n_bounded = 0 <= n && n <= N;
59 auto check_N_nonnegative
60 =
check_cl(function,
"Population size variable", n,
"nonnegative");
61 auto N_nonnegative = N >= 0;
62 auto check_theta_bounded =
check_cl(function,
"Probability parameter",
63 theta_val,
"in the interval [0, 1]");
64 auto theta_bounded = 0.0 <= theta_val && theta_val <= 1.0;
66 auto log1m_theta =
log1p(-theta_val);
69 auto n_is_zero = n == 0;
70 auto N_is_zero = N == 0;
72 auto N_minus_n = N - n;
76 select(n_is_N, n_times_log_theta,
77 n_times_log_theta +
elt_multiply(N_minus_n, log1m_theta))));
85 auto one_m_theta = 1.0 - theta_val;
89 select(n_is_N, n_div_theta,
90 n_div_theta -
elt_divide(N_minus_n, one_m_theta))));
97 constexpr bool need_sums
99 constexpr bool need_deriv
102 results(check_n_bounded, check_N_nonnegative, check_theta_bounded, logp_cl,
103 sum_n_cl, sum_N_cl, deriv_cl)
104 =
expressions(n_bounded, N_nonnegative, theta_bounded, logp_expr,
105 calc_if<need_sums>(sum_n_expr),
106 calc_if<need_sums>(sum_N_expr),
107 calc_if<need_deriv>(deriv_theta));
116 double theta_dbl = forward_as<double>(theta_val);
117 double& partial = forward_as<internal::broadcast_array<double>>(
118 partials<0>(ops_partials))[0];
121 partial = -sum_N / (1.0 - theta_dbl);
122 }
else if (sum_n == sum_N) {
123 partial = sum_n / theta_dbl;
125 partial = sum_n / theta_dbl - (sum_N - sum_n) / (1.0 - theta_dbl);
129 partials<0>(ops_partials) = std::move(deriv_cl);
133 return ops_partials.build(logp);
Represents an arithmetic matrix on the OpenCL device.
require_any_t< is_nonscalar_prim_or_rev_kernel_expression< std::decay_t< Types > >... > require_any_nonscalar_prim_or_rev_kernel_expression_t
Require any of the types satisfy is_nonscalar_prim_or_rev_kernel_expression.
elt_multiply_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > elt_multiply(T_a &&a, T_b &&b)
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.
auto check_cl(const char *function, const char *var_name, T &&y, const char *must_be)
Constructs a check on opencl matrix or expression.
results_cl< T_results... > results(T_results &&... results)
Deduces types for constructing results_cl object.
binomial_coefficient_log_< as_operation_cl_t< T1 >, as_operation_cl_t< T2 > > binomial_coefficient_log(T1 &&a, T2 &&b)
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_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 > binomial_lpmf(const T_n_cl &n, const T_N_cl N, const T_prob_cl &theta)
Returns the log PMF for the binomial distribution evaluated at the specified success,...
auto from_matrix_cl(const T &src)
Copies the source matrix that is stored on the OpenCL device to the destination Eigen matrix.
require_all_t< is_prim_or_rev_kernel_expression< std::decay_t< Types > >... > require_all_prim_or_rev_kernel_expression_t
Require type satisfies is_prim_or_rev_kernel_expression.
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.
fvar< T > log(const fvar< T > &x)
T1 static_select(T1 &&a, T2 &&b)
Returns one of the arguments that can be of different type, depending on the compile time condition.
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > log1p(const fvar< T > &x)
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
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.
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...