Automatic Differentiation
 
Loading...
Searching...
No Matches
bernoulli_logit_glm_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_BERNOULLI_LOGIT_GLM_LPMF_HPP
2#define STAN_MATH_OPENCL_PRIM_BERNOULLI_LOGIT_GLM_LPMF_HPP
3#ifdef STAN_OPENCL
4
20
21#include <cmath>
22
23namespace stan {
24namespace math {
25
52template <bool propto, typename T_x_cl, typename T_y_cl, typename T_alpha_cl,
53 typename T_beta_cl,
55 T_y_cl, T_x_cl, T_alpha_cl, T_beta_cl>* = nullptr>
57 const T_y_cl& y, const T_x_cl& x, const T_alpha_cl& alpha,
58 const T_beta_cl& beta) {
59 static constexpr const char* function = "bernoulli_logit_glm_lpmf(OpenCL)";
61 constexpr bool is_y_vector = !is_stan_scalar<T_y_cl>::value;
62 constexpr bool is_alpha_vector = !is_stan_scalar<T_alpha_cl>::value;
63 using std::isfinite;
64
65 const size_t N = x.rows();
66 const size_t M = x.cols();
67
68 if (is_y_vector) {
69 check_size_match(function, "Rows of ", "x", N, "rows of ", "y",
70 math::size(y));
71 }
72 check_size_match(function, "Columns of ", "x_cl", M, "size of ", "beta",
74 if (is_alpha_vector) {
75 check_size_match(function, "Rows of ", "x", N, "size of ", "alpha",
76 math::size(alpha));
77 }
78
79 if (N == 0) {
80 return 0;
81 }
83 return 0;
84 }
85
86 const auto& y_val = value_of(y);
87 const auto& x_val = value_of(x);
88 const auto& alpha_val = value_of(alpha);
89 const auto& beta_val = value_of(beta);
90
91 auto ytheta_expr = matrix_vector_multiply(x_val, beta_val) + alpha_val;
92 auto signs_expr = 2 * y_val - 1;
93 auto ytheta_signs_expr = elt_multiply(ytheta_expr, signs_expr);
94 auto exp_m_ytheta_expr = exp(-ytheta_signs_expr);
95 const double cutoff = 20.0;
96 auto high_bound_expr = ytheta_signs_expr > cutoff;
97 auto low_bound_expr = ytheta_signs_expr < -cutoff;
98 auto err_cond_expr = y_val < 0 || y_val > 1;
99 auto logp_expr
100 = colwise_sum(select(err_cond_expr, NOT_A_NUMBER,
101 select(high_bound_expr, -exp_m_ytheta_expr,
102 select(low_bound_expr, ytheta_signs_expr,
103 -log1p(exp_m_ytheta_expr)))));
104 auto theta_derivative_expr
105 = select(high_bound_expr, -exp_m_ytheta_expr,
106 select(low_bound_expr, signs_expr,
107 elt_divide(elt_multiply(signs_expr, exp_m_ytheta_expr),
108 (exp_m_ytheta_expr + 1))));
109
110 const int wgs = logp_expr.rows();
111 matrix_cl<double> logp_cl(wgs, 1);
112 constexpr bool need_theta_derivative
114 matrix_cl<double> theta_derivative_cl(need_theta_derivative ? N : 0, 1);
115 constexpr bool need_theta_derivative_sum
116 = need_theta_derivative && !is_alpha_vector;
117 matrix_cl<double> theta_derivative_sum_cl(need_theta_derivative_sum ? wgs : 0,
118 1);
119
120 results(logp_cl, theta_derivative_cl, theta_derivative_sum_cl) = expressions(
121 logp_expr, calc_if<need_theta_derivative>(theta_derivative_expr),
122 calc_if<need_theta_derivative_sum>(colwise_sum(theta_derivative_expr)));
123
124 T_partials_return logp = sum(from_matrix_cl(logp_cl));
125 if (!std::isfinite(logp)) {
126 results(check_cl(function, "Vector of dependent variables", y_val,
127 "in the interval [0, 1]"),
128 check_cl(function, "Intercept", alpha_val, "finite"))
129 = expressions(0 <= y_val && y_val <= 1, isfinite(alpha_val));
130 check_cl(function, "Weight vector", beta_val, "finite")
131 = isfinite(beta_val);
132 check_cl(function, "Matrix of independent variables", x_val, "finite")
133 = isfinite(x_val);
134 }
135
136 auto ops_partials = make_partials_propagator(x, alpha, beta);
137 // Compute the necessary derivatives.
139 partials<0>(ops_partials)
140 = transpose(beta_val * transpose(theta_derivative_cl));
141 }
143 if (is_alpha_vector) {
144 partials<1>(ops_partials) = theta_derivative_cl;
145 } else {
146 forward_as<internal::broadcast_array<double>>(
147 partials<1>(ops_partials))[0]
148 = sum(from_matrix_cl(theta_derivative_sum_cl));
149 }
150 }
152 // transposition of a vector can be done without copying
153 const matrix_cl<double> theta_derivative_transpose_cl(
154 theta_derivative_cl.buffer(), 1, theta_derivative_cl.rows());
155 matrix_cl<double> edge3_partials_transpose_cl
156 = theta_derivative_transpose_cl * x_val;
157 partials<2>(ops_partials)
158 = matrix_cl<double>(edge3_partials_transpose_cl.buffer(),
159 edge3_partials_transpose_cl.cols(), 1);
160 if (beta_val.rows() != 0) {
161 edge<2>(ops_partials)
162 .partials_.add_write_event(
163 edge3_partials_transpose_cl.write_events().back());
164 }
165 }
166 return ops_partials.build(logp);
167}
168
169} // namespace math
170} // namespace stan
171
172#endif
173#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)
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.
auto transpose(Arg &&a)
Transposes 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_x_cl, T_alpha_cl, T_beta_cl > bernoulli_logit_glm_lpmf(const T_y_cl &y, const T_x_cl &x, const T_alpha_cl &alpha, const T_beta_cl &beta)
Returns the log PMF of the Generalized Linear Model (GLM) with Bernoulli distribution and logit link ...
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.
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Definition size.hpp:19
static constexpr double NOT_A_NUMBER
(Quiet) not-a-number value.
Definition constants.hpp:56
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
fvar< T > log1p(const fvar< T > &x)
Definition log1p.hpp:12
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
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
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
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...