1#ifndef STAN_MATH_OPENCL_PRIM_NORMAL_ID_GLM_LPDF_HPP
2#define STAN_MATH_OPENCL_PRIM_NORMAL_ID_GLM_LPDF_HPP
58template <
bool propto,
typename T_y_cl,
typename T_x_cl,
typename T_alpha_cl,
59 typename T_beta_cl,
typename T_sigma_cl,
61 T_x_cl, T_y_cl, T_alpha_cl, T_beta_cl, T_sigma_cl>* =
nullptr>
62return_type_t<T_y_cl, T_x_cl, T_alpha_cl, T_beta_cl, T_sigma_cl>
64 const T_beta_cl&
beta,
const T_sigma_cl& sigma) {
65 using T_partials_return
71 static constexpr const char* function =
"normal_id_glm_lpdf(OpenCL)";
73 const size_t N = x.rows();
74 const size_t M = x.cols();
82 if (is_sigma_vector) {
86 if (is_alpha_vector) {
100 const auto& alpha_val =
value_of(alpha);
102 const auto& sigma_val =
value_of(sigma);
104 auto inv_sigma_expr =
elt_divide(1., sigma_val);
108 auto mu_derivative_expr =
elt_multiply(y_scaled_expr, inv_sigma_expr);
109 auto mu_derivative_sum_expr =
colwise_sum(mu_derivative_expr);
110 auto y_scaled_sq_expr =
elt_multiply(y_scaled_expr, y_scaled_expr);
111 auto y_scaled_sq_sum_expr =
colwise_sum(y_scaled_sq_expr);
112 auto sigma_derivative_expr
116 const int wgs = y_scaled_sq_sum_expr.rows();
118 constexpr bool need_mu_derivative
123 constexpr bool need_mu_derivative_sum
130 constexpr bool need_log_sigma_sum
134 results(mu_derivative_cl, mu_derivative_sum_cl, y_scaled_sq_sum_cl,
135 sigma_derivative_cl, log_sigma_sum_cl)
136 =
expressions(calc_if<need_mu_derivative>(mu_derivative_expr),
137 calc_if<need_mu_derivative_sum>(mu_derivative_sum_expr),
138 y_scaled_sq_sum_expr,
139 calc_if<need_sigma_derivative>(sigma_derivative_expr),
140 calc_if<need_log_sigma_sum>(log_sigma_sum_expr));
144 double mu_derivative_sum;
145 if (need_mu_derivative_sum) {
150 partials<0>(ops_partials) = -mu_derivative_cl;
152 forward_as<internal::broadcast_array<double>>(
153 partials<0>(ops_partials))[0]
154 = -mu_derivative_sum;
158 partials<1>(ops_partials)
162 if (is_alpha_vector) {
163 partials<2>(ops_partials) = mu_derivative_cl;
165 forward_as<internal::broadcast_array<double>>(
166 partials<2>(ops_partials))[0]
173 mu_derivative_cl.
buffer(), 1, mu_derivative_cl.
rows());
175 = mu_derivative_transpose_cl * x_val;
176 partials<3>(ops_partials)
178 edge4_partials_transpose_cl.
cols(), 1);
179 if (beta_val.rows() != 0) {
180 edge<3>(ops_partials)
181 .partials_.add_write_event(
186 partials<4>(ops_partials) = sigma_derivative_cl;
189 if (!std::isfinite(y_scaled_sq_sum)) {
191 check_cl(function,
"Vector of dependent variables", y_val,
"finite"),
192 check_cl(function,
"Intercept", alpha_val,
"finite"),
193 check_cl(function,
"Scale vector", sigma_val,
"positive finite"))
195 isfinite(sigma_val) && sigma_val > 0);
197 check_cl(function,
"Weight vector", beta_val,
"finite")
202 T_partials_return logp(0.0);
207 if (is_sigma_vector) {
210 logp -= N *
log(forward_as<double>(sigma_val));
213 logp -= 0.5 * y_scaled_sq_sum;
215 return ops_partials.build(logp);
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.
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.
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_y_cl, T_x_cl, T_alpha_cl, T_beta_cl, T_sigma_cl > normal_id_glm_lpdf(const T_y_cl &y, const T_x_cl &x, const T_alpha_cl &alpha, const T_beta_cl &beta, const T_sigma_cl &sigma)
Returns the log PDF of the Generalized Linear Model (GLM) with Normal distribution and id link functi...
auto from_matrix_cl(const T &src)
Copies the source matrix that is stored on the OpenCL device to the destination Eigen matrix.
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>>.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
fvar< T > log(const fvar< T > &x)
const double NEG_LOG_SQRT_TWO_PI
The value of minus the natural logarithm of the square root of , .
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.
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.
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...