Automatic Differentiation
 
Loading...
Searching...
No Matches
binomial_logit_glm_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_BINOMIAL_LOGIT_GLM_LPMF_HPP
2#define STAN_MATH_OPENCL_PRIM_BINOMIAL_LOGIT_GLM_LPMF_HPP
3#ifdef STAN_OPENCL
4
21
22#include <cmath>
23
24namespace stan {
25namespace math {
26
27template <bool propto, typename T_n_cl, typename T_N_cl, typename T_x_cl,
28 typename T_alpha_cl, typename T_beta_cl,
30 T_n_cl, T_N_cl, T_x_cl, T_alpha_cl, T_beta_cl>* = nullptr>
32 const T_n_cl& n, const T_N_cl& N, const T_x_cl& x, const T_alpha_cl& alpha,
33 const T_beta_cl& beta) {
34 static const char* function = "binomial_logit_glm_lpmf(OpenCL)";
36 constexpr bool is_y_vector = !is_stan_scalar<T_n_cl>::value;
37 constexpr bool is_alpha_vector = !is_stan_scalar<T_alpha_cl>::value;
38
39 const size_t N_instances
40 = max(max_size(n, N, alpha), static_cast<size_t>(x.rows()));
41 const size_t N_attributes = x.cols();
42
43 check_consistent_sizes(function, "Successes variable", n,
44 "Population size parameter", N);
45 check_consistent_size(function, "Successes variable", n, N_instances);
46 check_consistent_size(function, "Population size parameter", N, N_instances);
47 check_consistent_size(function, "Weight vector", beta, N_attributes);
48 check_consistent_size(function, "Vector of intercepts", alpha, N_instances);
49
50 if (N_instances == 0 || N_attributes == 0) {
51 return 0;
52 }
54 return 0;
55 }
56
57 auto&& x_val = value_of(x);
58 auto&& alpha_val = value_of(alpha);
59 auto&& beta_val = value_of(beta);
60
61 auto check_n_bounded
62 = check_cl(function, "Successes variable", n, "in the interval [0, N]");
63 auto n_bounded = 0 <= n && n <= N;
64 auto check_N_nonnegative
65 = check_cl(function, "Population size variable", n, "nonnegative");
66 auto N_nonnegative = N >= 0;
67
68 auto theta_expr = matrix_vector_multiply(x_val, beta_val) + alpha_val;
69 auto log_inv_logit_theta = log_inv_logit(theta_expr);
70 auto log1m_inv_logit_theta = log1m_inv_logit(theta_expr);
71 auto n_diff = N - n;
72 auto logp_expr1 = elt_multiply(n, log_inv_logit_theta)
73 + elt_multiply(n_diff, log1m_inv_logit_theta);
74 auto logp_expr
75 = static_select<include_summand<propto, T_n_cl, T_N_cl>::value>(
76 logp_expr1 + binomial_coefficient_log(N, n), logp_expr1);
77
78 constexpr bool need_theta_deriv
80 auto theta_deriv_expr = n - elt_multiply(N, exp(log_inv_logit_theta));
81
82 constexpr bool need_theta_deriv_sum = need_theta_deriv && !is_alpha_vector;
83 matrix_cl<double> logp_cl;
84 matrix_cl<double> theta_deriv_cl;
85 matrix_cl<double> theta_deriv_sum_cl;
86
87 results(check_n_bounded, check_N_nonnegative, logp_cl, theta_deriv_cl,
88 theta_deriv_sum_cl)
90 n_bounded, N_nonnegative, logp_expr,
91 calc_if<need_theta_deriv>(theta_deriv_expr),
92 calc_if<need_theta_deriv_sum>(colwise_sum(theta_deriv_expr)));
93
94 T_partials_return logp = sum(from_matrix_cl(logp_cl));
95 using std::isfinite;
96 if (!isfinite(logp)) {
97 check_cl(function, "Intercept", alpha_val, "finite") = isfinite(alpha_val);
98 check_cl(function, "Weight vector", beta_val, "finite")
99 = isfinite(beta_val);
100 check_cl(function, "Matrix of independent variables", x_val, "finite")
101 = isfinite(x_val);
102 }
103
104 auto ops_partials = make_partials_propagator(x, alpha, beta);
106 partials<0>(ops_partials) = transpose(beta_val * transpose(theta_deriv_cl));
107 }
109 if (is_alpha_vector) {
110 partials<1>(ops_partials) = theta_deriv_cl;
111 } else {
112 forward_as<internal::broadcast_array<double>>(
113 partials<1>(ops_partials))[0]
114 = sum(from_matrix_cl(theta_deriv_sum_cl));
115 }
116 }
118 // transposition of a vector can be done without copying
119 const matrix_cl<double> theta_derivative_transpose_cl(
120 theta_deriv_cl.buffer(), 1, theta_deriv_cl.rows());
121 matrix_cl<double> edge3_partials_transpose_cl
122 = theta_derivative_transpose_cl * x_val;
123 partials<2>(ops_partials)
124 = matrix_cl<double>(edge3_partials_transpose_cl.buffer(),
125 edge3_partials_transpose_cl.cols(), 1);
126 if (beta_val.rows() != 0) {
127 edge<2>(ops_partials)
128 .partials_.add_write_event(
129 edge3_partials_transpose_cl.write_events().back());
130 }
131 }
132 return ops_partials.build(logp);
133}
134
135} // namespace math
136} // namespace stan
137
138#endif
139#endif
const cl::Buffer & buffer() const
const tbb::concurrent_vector< cl::Event > & write_events() const
Get the events from the event stacks.
Represents an arithmetic matrix on the OpenCL device.
Definition matrix_cl.hpp:47
elt_multiply_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > elt_multiply(T_a &&a, T_b &&b)
isfinite_< as_operation_cl_t< T > > isfinite(T &&a)
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 transpose(Arg &&a)
Transposes a kernel generator expression.
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
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
void check_consistent_size(const char *function, const char *name, const T &x, size_t expected_size)
Check if x is consistent with size expected_size.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
fvar< T > log_inv_logit(const fvar< T > &x)
auto max(T1 x, T2 y)
Returns the maximum value of the two specified scalar arguments.
Definition max.hpp:25
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
return_type_t< T_x_cl, T_alpha_cl, T_beta_cl > binomial_logit_glm_lpmf(const T_n_cl &n, const T_N_cl &N, const T_x_cl &x, const T_alpha_cl &alpha, const T_beta_cl &beta)
auto matrix_vector_multiply(T_matrix &&matrix, T_vector &&vector)
Multiplies a matrix and a vector on an OpenCL device.
fvar< T > beta(const fvar< T > &x1, const fvar< T > &x2)
Return fvar with the beta function applied to the specified arguments and its gradient.
Definition beta.hpp:51
fvar< T > log1m_inv_logit(const fvar< T > &x)
Return the natural logarithm of one minus the inverse logit of the specified argument.
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:13
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...