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
23#include <vector>
24#include <cmath>
25
26namespace stan {
27namespace math {
28
64template <bool propto, typename T_y_cl, typename T_x_cl, typename T_alpha_cl,
65 typename T_beta_cl, typename T_phi_cl,
67 T_x_cl, T_y_cl, T_alpha_cl, T_beta_cl, T_phi_cl>* = nullptr>
68return_type_t<T_x_cl, T_alpha_cl, T_beta_cl, T_phi_cl>
69neg_binomial_2_log_glm_lpmf(const T_y_cl& y, const T_x_cl& x,
70 const T_alpha_cl& alpha, const T_beta_cl& beta,
71 const T_phi_cl& phi) {
72 static constexpr const char* function = "neg_binomial_2_log_glm_lpmf(OpenCL)";
73 using T_partials_return
75 constexpr bool is_y_vector = !is_stan_scalar<T_y_cl>::value;
76 constexpr bool is_phi_vector = !is_stan_scalar<T_phi_cl>::value;
77 constexpr bool is_alpha_vector = !is_stan_scalar<T_alpha_cl>::value;
78 using Eigen::Dynamic;
79 using std::isfinite;
80
81 const size_t N = x.rows();
82 const size_t M = x.cols();
83
84 if (is_y_vector) {
85 check_size_match(function, "Rows of ", "x", N, "rows of ", "y",
86 math::size(y));
87 }
88 check_size_match(function, "Columns of ", "x", M, "size of ", "beta",
90 if (is_phi_vector) {
91 check_size_match(function, "Rows of ", "x", N, "size of ", "phi",
92 math::size(phi));
93 }
94 if (is_alpha_vector) {
95 check_size_match(function, "Rows of ", "x", N, "size of ", "alpha",
96 math::size(alpha));
97 }
98 if (N == 0) {
99 return 0;
100 }
101 if (!include_summand<propto, T_x_cl, T_alpha_cl, T_beta_cl,
102 T_phi_cl>::value) {
103 return 0;
104 }
105
106 const auto& y_val = eval(value_of(y));
107 const auto& x_val = eval(value_of(x));
108 const auto& alpha_val = eval(value_of(alpha));
109 const auto& beta_val = eval(value_of(beta));
110 const auto& phi_val = eval(value_of(phi));
111
112 // copy any scalars to device, as this is expected by the kernel
113 const auto& y_val_cl = to_matrix_cl(y_val);
114 const auto& alpha_val_cl = to_matrix_cl(alpha_val);
115 const auto& phi_val_cl = to_matrix_cl(phi_val);
116
117 const int local_size
118 = opencl_kernels::neg_binomial_2_log_glm.get_option("LOCAL_SIZE_");
119 const int wgs = (N + local_size - 1) / local_size;
120
121 const bool need_theta_derivative
123 matrix_cl<double> theta_derivative_cl(need_theta_derivative ? N : 0, 1);
124 const bool need_theta_derivative_sum
125 = need_theta_derivative && !is_alpha_vector;
126 matrix_cl<double> theta_derivative_sum_cl(wgs, 1);
127 const bool need_phi_derivative_sum = !is_alpha_vector;
128 const bool need_phi_derivative
129 = !is_constant_all<T_phi_cl>::value || need_phi_derivative_sum;
130 matrix_cl<double> phi_derivative_cl(
131 need_phi_derivative ? (need_phi_derivative_sum ? wgs : N) : 0, 1);
132 const bool need_logp1 = include_summand<propto>::value;
133 const bool need_logp2
135 const bool need_logp3
137 const bool need_logp4 = include_summand<propto, T_phi_cl>::value
138 && (is_y_vector || is_phi_vector);
139 matrix_cl<double> logp_cl(wgs, 1);
140
141 try {
143 cl::NDRange(local_size * wgs), cl::NDRange(local_size), logp_cl,
144 theta_derivative_cl, theta_derivative_sum_cl, phi_derivative_cl,
145 y_val_cl, x_val, alpha_val_cl, beta_val, phi_val_cl, N, M, is_y_vector,
146 is_alpha_vector, is_phi_vector, need_theta_derivative,
147 need_theta_derivative_sum, need_phi_derivative, need_phi_derivative_sum,
148 need_logp1, need_logp2, need_logp3, need_logp4);
149 } catch (const cl::Error& e) {
150 check_opencl_error(function, e);
151 }
152
153 T_partials_return logp = sum(from_matrix_cl(logp_cl));
154 if (!std::isfinite(logp)) {
155 results(
156 check_cl(function, "Vector of dependent variables", y_val,
157 "nonnegative"),
158 check_cl(function, "Intercept", alpha_val, "finite"),
159 check_cl(function, "Precision parameter", phi_val, "positive finite"))
160 = expressions(y_val >= 0, isfinite(alpha_val),
161 isfinite(phi_val) && phi_val > 0);
162 check_cl(function, "Design matrix", x_val, "finite") = isfinite(x_val);
163 check_cl(function, "Weight vector", beta_val, "finite")
164 = isfinite(beta_val);
165 } else {
166 check_cl(function, "Precision parameter", phi_val, "positive finite")
167 = isfinite(phi_val) && phi_val > 0;
168 }
169
170 if (include_summand<propto, T_phi_cl>::value && !is_phi_vector) {
171 logp += N
172 * (multiply_log(forward_as<double>(phi_val),
173 forward_as<double>(phi_val))
174 - lgamma(forward_as<double>(phi_val)));
175 }
177 && !is_phi_vector) {
178 logp += forward_as<double>(lgamma(y_val + phi_val)) * N;
179 }
180
181 auto ops_partials = make_partials_propagator(x, alpha, beta, phi);
182 // Compute the necessary derivatives.
184 partials<0>(ops_partials)
185 = transpose(beta_val * transpose(theta_derivative_cl));
186 }
188 // transposition of a vector can be done without copying
189 const matrix_cl<double> theta_derivative_transpose_cl(
190 theta_derivative_cl.buffer(), 1, theta_derivative_cl.rows());
191 matrix_cl<double> edge3_partials_transpose_cl
192 = theta_derivative_transpose_cl * x_val;
193 partials<2>(ops_partials)
194 = matrix_cl<double>(edge3_partials_transpose_cl.buffer(),
195 edge3_partials_transpose_cl.cols(), 1);
196 if (beta_val.rows() != 0) {
197 edge<2>(ops_partials)
198 .partials_.add_write_event(
199 edge3_partials_transpose_cl.write_events().back());
200 }
201 }
203 if (is_alpha_vector) {
204 partials<1>(ops_partials) = std::move(theta_derivative_cl);
205 } else {
206 forward_as<internal::broadcast_array<double>>(
207 partials<1>(ops_partials))[0]
208 = sum(from_matrix_cl(theta_derivative_sum_cl));
209 }
210 }
212 if (is_phi_vector) {
213 partials<3>(ops_partials) = std::move(phi_derivative_cl);
214 } else {
215 forward_as<internal::broadcast_array<double>>(
216 partials<3>(ops_partials))[0]
217 = sum(from_matrix_cl(phi_derivative_cl));
218 }
219 }
220 return ops_partials.build(logp);
221}
222
223} // namespace math
224} // namespace stan
225
226#endif
227#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
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)
static constexpr double e()
Return the base of the natural logarithm.
Definition constants.hpp:20
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 > 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 ...
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...
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...