Automatic Differentiation
 
Loading...
Searching...
No Matches
neg_binomial_2_log_glm_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_NEG_BINOMIAL_2_LOG_GLM_LPMF_HPP
2#define STAN_MATH_OPENCL_PRIM_NEG_BINOMIAL_2_LOG_GLM_LPMF_HPP
3#ifdef STAN_OPENCL
4
18#include <cmath>
19
20namespace stan {
21namespace math {
22
58template <bool propto, typename T_y_cl, typename T_x_cl, typename T_alpha_cl,
59 typename T_beta_cl, typename T_phi_cl,
61 T_x_cl, T_y_cl, T_alpha_cl, T_beta_cl, T_phi_cl>* = nullptr>
62inline return_type_t<T_x_cl, T_alpha_cl, T_beta_cl, T_phi_cl>
63neg_binomial_2_log_glm_lpmf(const T_y_cl& y, const T_x_cl& x,
64 const T_alpha_cl& alpha, const T_beta_cl& beta,
65 const T_phi_cl& phi) {
66 static constexpr const char* function = "neg_binomial_2_log_glm_lpmf(OpenCL)";
67 using T_partials_return
69 constexpr bool is_y_vector = !is_stan_scalar<T_y_cl>::value;
70 constexpr bool is_phi_vector = !is_stan_scalar<T_phi_cl>::value;
71 constexpr bool is_alpha_vector = !is_stan_scalar<T_alpha_cl>::value;
72 using Eigen::Dynamic;
73 using std::isfinite;
74
75 const size_t N = x.rows();
76 const size_t M = x.cols();
77
78 if constexpr (is_y_vector) {
79 check_size_match(function, "Rows of ", "x", N, "rows of ", "y",
80 math::size(y));
81 }
82 check_size_match(function, "Columns of ", "x", M, "size of ", "beta",
84 if constexpr (is_phi_vector) {
85 check_size_match(function, "Rows of ", "x", N, "size of ", "phi",
86 math::size(phi));
87 }
88 if constexpr (is_alpha_vector) {
89 check_size_match(function, "Rows of ", "x", N, "size of ", "alpha",
90 math::size(alpha));
91 }
92 if (N == 0) {
93 return 0;
94 }
95 if constexpr (!include_summand<propto, T_x_cl, T_alpha_cl, T_beta_cl,
96 T_phi_cl>::value) {
97 return 0;
98 }
99
100 const auto& y_val = eval(value_of(y));
101 const auto& x_val = eval(value_of(x));
102 const auto& alpha_val = eval(value_of(alpha));
103 const auto& beta_val = eval(value_of(beta));
104 const auto& phi_val = eval(value_of(phi));
105
106 // copy any scalars to device, as this is expected by the kernel
107 const auto& y_val_cl = to_matrix_cl(y_val);
108 const auto& alpha_val_cl = to_matrix_cl(alpha_val);
109 const auto& phi_val_cl = to_matrix_cl(phi_val);
110
111 const int local_size
112 = opencl_kernels::neg_binomial_2_log_glm.get_option("LOCAL_SIZE_");
113 const int wgs = (N + local_size - 1) / local_size;
114
115 const bool need_theta_derivative
116 = is_any_autodiff_v<T_x_cl, T_beta_cl, T_alpha_cl>;
117 matrix_cl<double> theta_derivative_cl(need_theta_derivative ? N : 0, 1);
118 const bool need_theta_derivative_sum
119 = need_theta_derivative && !is_alpha_vector;
120 matrix_cl<double> theta_derivative_sum_cl(wgs, 1);
121 const bool need_phi_derivative = is_autodiff_v<T_phi_cl>;
122 const bool need_phi_derivative_sum = need_phi_derivative && !is_phi_vector;
123 matrix_cl<double> phi_derivative_cl(
124 need_phi_derivative ? (need_phi_derivative_sum ? wgs : N) : 0, 1);
125 const bool need_logp1 = include_summand<propto>::value;
126 const bool need_logp2
128 const bool need_logp3
130 const bool need_logp4 = include_summand<propto, T_phi_cl>::value
131 && (is_y_vector || is_phi_vector);
132 matrix_cl<double> logp_cl(wgs, 1);
133
134 try {
136 cl::NDRange(local_size * wgs), cl::NDRange(local_size), logp_cl,
137 theta_derivative_cl, theta_derivative_sum_cl, phi_derivative_cl,
138 y_val_cl, x_val, alpha_val_cl, beta_val, phi_val_cl, N, M, is_y_vector,
139 is_alpha_vector, is_phi_vector, need_theta_derivative,
140 need_theta_derivative_sum, need_phi_derivative, need_phi_derivative_sum,
141 need_logp1, need_logp2, need_logp3, need_logp4);
142 } catch (const cl::Error& e) {
143 check_opencl_error(function, e);
144 }
145
146 T_partials_return logp = sum(from_matrix_cl(logp_cl));
147 if (!std::isfinite(logp)) {
148 results(
149 check_cl(function, "Vector of dependent variables", y_val,
150 "nonnegative"),
151 check_cl(function, "Intercept", alpha_val, "finite"),
152 check_cl(function, "Precision parameter", phi_val, "positive finite"))
153 = expressions(y_val >= 0, isfinite(alpha_val),
154 isfinite(phi_val) && phi_val > 0);
155 check_cl(function, "Design matrix", x_val, "finite") = isfinite(x_val);
156 check_cl(function, "Weight vector", beta_val, "finite")
157 = isfinite(beta_val);
158 } else {
159 check_cl(function, "Precision parameter", phi_val, "positive finite")
160 = isfinite(phi_val) && phi_val > 0;
161 }
162
163 if constexpr (include_summand<propto, T_phi_cl>::value && !is_phi_vector) {
164 logp += N * (lmultiply(phi_val, phi_val) - lgamma(phi_val));
165 }
166 if constexpr (include_summand<propto, T_phi_cl>::value && !is_y_vector
167 && !is_phi_vector) {
168 logp += lgamma(y_val + phi_val) * N;
169 }
170
171 auto ops_partials = make_partials_propagator(x, alpha, beta, phi);
172 // Compute the necessary derivatives.
173 if constexpr (is_autodiff_v<T_x_cl>) {
174 partials<0>(ops_partials)
175 = transpose(beta_val * transpose(theta_derivative_cl));
176 }
177 if constexpr (is_autodiff_v<T_beta_cl>) {
178 // transposition of a vector can be done without copying
179 const matrix_cl<double> theta_derivative_transpose_cl(
180 theta_derivative_cl.buffer(), 1, theta_derivative_cl.rows());
181 matrix_cl<double> edge3_partials_transpose_cl
182 = theta_derivative_transpose_cl * x_val;
183 partials<2>(ops_partials)
184 = matrix_cl<double>(edge3_partials_transpose_cl.buffer(),
185 edge3_partials_transpose_cl.cols(), 1);
186 if (beta_val.rows() != 0) {
187 edge<2>(ops_partials)
188 .partials_.add_write_event(
189 edge3_partials_transpose_cl.write_events().back());
190 }
191 }
192 if constexpr (is_autodiff_v<T_alpha_cl>) {
193 if constexpr (is_alpha_vector) {
194 partials<1>(ops_partials) = std::move(theta_derivative_cl);
195 } else {
196 partials<1>(ops_partials)[0]
197 = sum(from_matrix_cl(theta_derivative_sum_cl));
198 }
199 }
200 if constexpr (is_autodiff_v<T_phi_cl>) {
201 if constexpr (is_phi_vector) {
202 partials<3>(ops_partials) = std::move(phi_derivative_cl);
203 } else {
204 partials<3>(ops_partials)[0] = sum(from_matrix_cl(phi_derivative_cl));
205 }
206 }
207 return ops_partials.build(logp);
208}
209
210} // namespace math
211} // namespace stan
212
213#endif
214#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
void check_opencl_error(const char *function, const cl::Error &e)
Throws the domain error with specifying the OpenCL error that occurred.
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.
auto transpose(Arg &&a)
Transposes a kernel generator expression.
expressions_cl< T_expressions... > expressions(T_expressions &&... expressions)
Deduces types for constructing expressions_cl object.
const kernel_cl< out_buffer, out_buffer, out_buffer, out_buffer, in_buffer, in_buffer, in_buffer, in_buffer, in_buffer, int, int, int, int, int, int, int, int, int, int, int, int, int > neg_binomial_2_log_glm("neg_binomial_2_log_glm", {digamma_device_function, log1p_exp_device_function, neg_binomial_2_log_glm_kernel_code}, {{"REDUCTION_STEP_SIZE", 4}, {"LOCAL_SIZE_", 64}})
See the docs for neg_binomial_2_log_glm_lpmf() .
matrix_cl< scalar_type_t< T > > to_matrix_cl(T &&src)
Copies the source Eigen matrix, std::vector or scalar to the destination matrix that is stored on the...
Definition copy.hpp:45
return_type_t< T_x_cl, T_alpha_cl, T_beta_cl, T_phi_cl > neg_binomial_2_log_glm_lpmf(const T_y_cl &y, const T_x_cl &x, const T_alpha_cl &alpha, const T_beta_cl &beta, const T_phi_cl &phi)
Returns the log PMF of the Generalized Linear Model (GLM) with Negative-Binomial-2 distribution and l...
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.
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 e()
Return the base of the natural logarithm.
Definition constants.hpp:18
T eval(T &&arg)
Inputs which have a plain_type equal to the own time are forwarded unmodified (for Eigen expressions ...
Definition eval.hpp:20
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
fvar< T > lmultiply(const fvar< T > &x1, const fvar< T > &x2)
Definition lmultiply.hpp:14
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
Definition lgamma.hpp:21
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.
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...