Automatic Differentiation
 
Loading...
Searching...
No Matches
binomial_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_BINOMIAL_LPMF_HPP
2#define STAN_MATH_OPENCL_PRIM_BINOMIAL_LPMF_HPP
3#ifdef STAN_OPENCL
4
10
11namespace stan {
12namespace math {
13
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>
36return_type_t<T_prob_cl> binomial_lpmf(const T_n_cl& n, const T_N_cl N,
37 const T_prob_cl& theta) {
38 static constexpr const char* function = "binomial_lpmf(OpenCL)";
39 using T_partials_return = partials_return_t<T_prob_cl>;
40 using std::isnan;
41
42 check_consistent_sizes(function, "Successes variable", n,
43 "Population size parameter", N,
44 "Probability parameter", theta);
45 const size_t siz = max_size(n, N, theta);
46 if (siz == 0) {
47 return 0.0;
48 }
50 return 0.0;
51 }
52
53 const auto& theta_col = as_column_vector_or_scalar(theta);
54 const auto& theta_val = value_of(theta_col);
55
56 auto check_n_bounded
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;
65
66 auto log1m_theta = log1p(-theta_val);
67
68 auto n_times_log_theta = elt_multiply(n, log(theta_val));
69 auto n_is_zero = n == 0;
70 auto N_is_zero = N == 0;
71 auto n_is_N = n == N;
72 auto N_minus_n = N - n;
73 auto logp1 = select(
74 N_is_zero, 0.0,
75 select(n_is_zero, elt_multiply(N, log1m_theta),
76 select(n_is_N, n_times_log_theta,
77 n_times_log_theta + elt_multiply(N_minus_n, log1m_theta))));
79 logp1 + binomial_coefficient_log(N, n), logp1));
80
81 auto sum_n_expr = colwise_sum(n);
82 auto sum_N_expr = colwise_sum(N);
83
84 auto n_div_theta = elt_divide(n, theta_val);
85 auto one_m_theta = 1.0 - theta_val;
86 auto deriv_theta = select(
87 N_is_zero, 0.0,
88 select(n_is_zero, -elt_divide(N, one_m_theta),
89 select(n_is_N, n_div_theta,
90 n_div_theta - elt_divide(N_minus_n, one_m_theta))));
91
92 matrix_cl<int> sum_n_cl;
93 matrix_cl<int> sum_N_cl;
94 matrix_cl<double> logp_cl;
95 matrix_cl<double> deriv_cl;
96
97 constexpr bool need_sums
99 constexpr bool need_deriv
101
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));
108
109 T_partials_return logp = sum(from_matrix_cl(logp_cl));
110 auto ops_partials = make_partials_propagator(theta_col);
111
113 if (need_sums) {
114 int sum_n = sum(from_matrix_cl(sum_n_cl));
115 int sum_N = sum(from_matrix_cl(sum_N_cl));
116 double theta_dbl = forward_as<double>(theta_val);
117 double& partial = forward_as<internal::broadcast_array<double>>(
118 partials<0>(ops_partials))[0];
119 if (sum_N != 0) {
120 if (sum_n == 0) {
121 partial = -sum_N / (1.0 - theta_dbl);
122 } else if (sum_n == sum_N) {
123 partial = sum_n / theta_dbl;
124 } else {
125 partial = sum_n / theta_dbl - (sum_N - sum_n) / (1.0 - theta_dbl);
126 }
127 }
128 } else {
129 partials<0>(ops_partials) = std::move(deriv_cl);
130 }
131 }
132
133 return ops_partials.build(logp);
134}
135
136} // namespace math
137} // namespace stan
138#endif
139#endif
Represents an arithmetic matrix on the OpenCL device.
Definition matrix_cl.hpp:47
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.
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.
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.
Definition copy.hpp:61
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.
size_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_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:15
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 > 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
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
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...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...