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 }
54 if constexpr (!include_summand<propto, T_x_cl, T_alpha_cl,
55 T_beta_cl>::value) {
56 return 0;
57 }
58
59 auto&& x_val = value_of(x);
60 auto&& alpha_val = value_of(alpha);
61 auto&& beta_val = value_of(beta);
62
63 auto check_n_bounded
64 = check_cl(function, "Successes variable", n, "in the interval [0, N]");
65 auto n_bounded = 0 <= n && n <= N;
66 auto check_N_nonnegative
67 = check_cl(function, "Population size variable", n, "nonnegative");
68 auto N_nonnegative = N >= 0;
69
70 auto theta_expr = matrix_vector_multiply(x_val, beta_val) + alpha_val;
71 auto log_inv_logit_theta = log_inv_logit(theta_expr);
72 auto log1m_inv_logit_theta = log1m_inv_logit(theta_expr);
73 auto n_diff = N - n;
74 auto logp_expr1 = elt_multiply(n, log_inv_logit_theta)
75 + elt_multiply(n_diff, log1m_inv_logit_theta);
76 auto logp_expr
77 = static_select<include_summand<propto, T_n_cl, T_N_cl>::value>(
78 logp_expr1 + binomial_coefficient_log(N, n), logp_expr1);
79
80 constexpr bool need_theta_deriv
81 = is_any_autodiff_v<T_beta_cl, T_x_cl, T_alpha_cl>;
82 auto theta_deriv_expr = n - elt_multiply(N, exp(log_inv_logit_theta));
83
84 constexpr bool need_theta_deriv_sum = need_theta_deriv && !is_alpha_vector;
85 matrix_cl<double> logp_cl;
86 matrix_cl<double> theta_deriv_cl;
87 matrix_cl<double> theta_deriv_sum_cl;
88
89 results(check_n_bounded, check_N_nonnegative, logp_cl, theta_deriv_cl,
90 theta_deriv_sum_cl)
92 n_bounded, N_nonnegative, logp_expr,
93 calc_if<need_theta_deriv>(theta_deriv_expr),
94 calc_if<need_theta_deriv_sum>(colwise_sum(theta_deriv_expr)));
95
96 T_partials_return logp = sum(from_matrix_cl(logp_cl));
97 using std::isfinite;
98 if (!isfinite(logp)) {
99 check_cl(function, "Intercept", alpha_val, "finite") = isfinite(alpha_val);
100 check_cl(function, "Weight vector", beta_val, "finite")
101 = isfinite(beta_val);
102 check_cl(function, "Matrix of independent variables", x_val, "finite")
103 = isfinite(x_val);
104 }
105
106 auto ops_partials = make_partials_propagator(x, alpha, beta);
107 if constexpr (is_autodiff_v<T_x_cl>) {
108 partials<0>(ops_partials) = transpose(beta_val * transpose(theta_deriv_cl));
109 }
110 if constexpr (is_autodiff_v<T_alpha_cl>) {
111 if constexpr (is_alpha_vector) {
112 partials<1>(ops_partials) = theta_deriv_cl;
113 } else {
114 partials<1>(ops_partials)[0] = sum(from_matrix_cl(theta_deriv_sum_cl));
115 }
116 }
117 if constexpr (is_autodiff_v<T_beta_cl>) {
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.
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.
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...