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>
62inline return_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();
76 if constexpr (is_y_vector) {
82 if constexpr (is_sigma_vector) {
86 if constexpr (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
120 T_x_cl, T_beta_cl> || (is_autodiff_v<T_alpha_cl> && is_alpha_vector)
121 || (is_autodiff_v<T_y_cl> && is_y_vector);
123 constexpr bool need_mu_derivative_sum
124 = (is_autodiff_v<T_alpha_cl> && !is_alpha_vector)
125 || (is_autodiff_v<T_y_cl> && !is_y_vector);
128 constexpr bool need_sigma_derivative = is_autodiff_v<T_sigma_cl>;
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) {
148 if constexpr (is_autodiff_v<T_y_cl>) {
149 if constexpr (is_y_vector) {
150 partials<0>(ops_partials) = -mu_derivative_cl;
152 partials<0>(ops_partials)[0] = -mu_derivative_sum;
155 if constexpr (is_autodiff_v<T_x_cl>) {
156 partials<1>(ops_partials)
159 if constexpr (is_autodiff_v<T_alpha_cl>) {
160 if constexpr (is_alpha_vector) {
161 partials<2>(ops_partials) = mu_derivative_cl;
163 partials<2>(ops_partials)[0] = mu_derivative_sum;
166 if constexpr (is_autodiff_v<T_beta_cl>) {
169 mu_derivative_cl.
buffer(), 1, mu_derivative_cl.
rows());
171 = mu_derivative_transpose_cl * x_val;
172 partials<3>(ops_partials)
174 edge4_partials_transpose_cl.
cols(), 1);
175 if (beta_val.rows() != 0) {
176 edge<3>(ops_partials)
177 .partials_.add_write_event(
181 if constexpr (is_autodiff_v<T_sigma_cl>) {
182 partials<4>(ops_partials) = sigma_derivative_cl;
185 if (!std::isfinite(y_scaled_sq_sum)) {
187 check_cl(function,
"Vector of dependent variables", y_val,
"finite"),
188 check_cl(function,
"Intercept", alpha_val,
"finite"),
189 check_cl(function,
"Scale vector", sigma_val,
"positive finite"))
191 isfinite(sigma_val) && sigma_val > 0);
193 check_cl(function,
"Weight vector", beta_val,
"finite")
198 T_partials_return logp(0.0);
203 if constexpr (is_sigma_vector) {
206 logp -= N *
log(sigma_val);
209 logp -= 0.5 * y_scaled_sq_sum;
211 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.
constexpr bool is_any_autodiff_v
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...