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#include <cstdint>
24
25namespace stan {
26namespace math {
27
28template <bool propto, typename T_n_cl, typename T_N_cl, typename T_x_cl,
29 typename T_alpha_cl, typename T_beta_cl,
31 T_n_cl, T_N_cl, T_x_cl, T_alpha_cl, T_beta_cl>* = nullptr>
33 const T_n_cl& n, const T_N_cl& N, const T_x_cl& x, const T_alpha_cl& alpha,
34 const T_beta_cl& beta) {
35 static const char* function = "binomial_logit_glm_lpmf(OpenCL)";
37 constexpr bool is_y_vector = !is_stan_scalar<T_n_cl>::value;
38 constexpr bool is_alpha_vector = !is_stan_scalar<T_alpha_cl>::value;
39
40 const size_t N_instances
41 = max(max_size(n, N, alpha), static_cast<int64_t>(x.rows()));
42 const size_t N_attributes = x.cols();
43
44 check_consistent_sizes(function, "Successes variable", n,
45 "Population size parameter", N);
46 check_consistent_size(function, "Successes variable", n, N_instances);
47 check_consistent_size(function, "Population size parameter", N, N_instances);
48 check_consistent_size(function, "Weight vector", beta, N_attributes);
49 check_consistent_size(function, "Vector of intercepts", alpha, N_instances);
50
51 if (N_instances == 0 || N_attributes == 0) {
52 return 0;
53 }
55 return 0;
56 }
57
58 auto&& x_val = value_of(x);
59 auto&& alpha_val = value_of(alpha);
60 auto&& beta_val = value_of(beta);
61
62 auto check_n_bounded
63 = check_cl(function, "Successes variable", n, "in the interval [0, N]");
64 auto n_bounded = 0 <= n && n <= N;
65 auto check_N_nonnegative
66 = check_cl(function, "Population size variable", n, "nonnegative");
67 auto N_nonnegative = N >= 0;
68
69 auto theta_expr = matrix_vector_multiply(x_val, beta_val) + alpha_val;
70 auto log_inv_logit_theta = log_inv_logit(theta_expr);
71 auto log1m_inv_logit_theta = log1m_inv_logit(theta_expr);
72 auto n_diff = N - n;
73 auto logp_expr1 = elt_multiply(n, log_inv_logit_theta)
74 + elt_multiply(n_diff, log1m_inv_logit_theta);
75 auto logp_expr
76 = static_select<include_summand<propto, T_n_cl, T_N_cl>::value>(
77 logp_expr1 + binomial_coefficient_log(N, n), logp_expr1);
78
79 constexpr bool need_theta_deriv
81 auto theta_deriv_expr = n - elt_multiply(N, exp(log_inv_logit_theta));
82
83 constexpr bool need_theta_deriv_sum = need_theta_deriv && !is_alpha_vector;
84 matrix_cl<double> logp_cl;
85 matrix_cl<double> theta_deriv_cl;
86 matrix_cl<double> theta_deriv_sum_cl;
87
88 results(check_n_bounded, check_N_nonnegative, logp_cl, theta_deriv_cl,
89 theta_deriv_sum_cl)
91 n_bounded, N_nonnegative, logp_expr,
92 calc_if<need_theta_deriv>(theta_deriv_expr),
93 calc_if<need_theta_deriv_sum>(colwise_sum(theta_deriv_expr)));
94
95 T_partials_return logp = sum(from_matrix_cl(logp_cl));
96 using std::isfinite;
97 if (!isfinite(logp)) {
98 check_cl(function, "Intercept", alpha_val, "finite") = isfinite(alpha_val);
99 check_cl(function, "Weight vector", beta_val, "finite")
100 = isfinite(beta_val);
101 check_cl(function, "Matrix of independent variables", x_val, "finite")
102 = isfinite(x_val);
103 }
104
105 auto ops_partials = make_partials_propagator(x, alpha, beta);
107 partials<0>(ops_partials) = transpose(beta_val * transpose(theta_deriv_cl));
108 }
110 if (is_alpha_vector) {
111 partials<1>(ops_partials) = theta_deriv_cl;
112 } else {
113 forward_as<internal::broadcast_array<double>>(
114 partials<1>(ops_partials))[0]
115 = sum(from_matrix_cl(theta_deriv_sum_cl));
116 }
117 }
119 // transposition of a vector can be done without copying
120 const matrix_cl<double> theta_derivative_transpose_cl(
121 theta_deriv_cl.buffer(), 1, theta_deriv_cl.rows());
122 matrix_cl<double> edge3_partials_transpose_cl
123 = theta_derivative_transpose_cl * x_val;
124 partials<2>(ops_partials)
125 = matrix_cl<double>(edge3_partials_transpose_cl.buffer(),
126 edge3_partials_transpose_cl.cols(), 1);
127 if (beta_val.rows() != 0) {
128 edge<2>(ops_partials)
129 .partials_.add_write_event(
130 edge3_partials_transpose_cl.write_events().back());
131 }
132 }
133 return ops_partials.build(logp);
134}
135
136} // namespace math
137} // namespace stan
138
139#endif
140#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.
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.
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.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
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:15
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 ...
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...