Automatic Differentiation
 
Loading...
Searching...
No Matches
normal_id_glm_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_NORMAL_ID_GLM_LPDF_HPP
2#define STAN_MATH_OPENCL_PRIM_NORMAL_ID_GLM_LPDF_HPP
3#ifdef STAN_OPENCL
4
21#include <cmath>
22
23namespace stan {
24namespace math {
25
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>
63normal_id_glm_lpdf(const T_y_cl& y, const T_x_cl& x, const T_alpha_cl& alpha,
64 const T_beta_cl& beta, const T_sigma_cl& sigma) {
65 using T_partials_return
67 constexpr bool is_y_vector = !is_stan_scalar<T_y_cl>::value;
68 constexpr bool is_sigma_vector = !is_stan_scalar<T_sigma_cl>::value;
69 constexpr bool is_alpha_vector = !is_stan_scalar<T_alpha_cl>::value;
70 using std::isfinite;
71 static constexpr const char* function = "normal_id_glm_lpdf(OpenCL)";
72
73 const size_t N = x.rows();
74 const size_t M = x.cols();
75
76 if (is_y_vector) {
77 check_size_match(function, "Rows of ", "x", N, "rows of ", "y",
78 math::size(y));
79 }
80 check_size_match(function, "Columns of ", "x_cl", M, "size of ", "beta",
82 if (is_sigma_vector) {
83 check_size_match(function, "Rows of ", "x", N, "size of ", "sigma",
84 math::size(sigma));
85 }
86 if (is_alpha_vector) {
87 check_size_match(function, "Rows of ", "x", N, "size of ", "alpha",
88 math::size(alpha));
89 }
90 if (!include_summand<propto, T_y_cl, T_x_cl, T_alpha_cl, T_beta_cl,
91 T_sigma_cl>::value) {
92 return 0;
93 }
94 if (N == 0) {
95 return 0;
96 }
97
98 const auto& y_val = value_of(y);
99 const auto& x_val = value_of(x);
100 const auto& alpha_val = value_of(alpha);
101 const auto& beta_val = value_of(beta);
102 const auto& sigma_val = value_of(sigma);
103
104 auto inv_sigma_expr = elt_divide(1., sigma_val);
105 auto y_scaled_expr = elt_multiply(
106 (y_val - matrix_vector_multiply(x_val, beta_val) - alpha_val),
107 inv_sigma_expr);
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
113 = elt_multiply((y_scaled_sq_expr - 1), inv_sigma_expr);
114 auto log_sigma_sum_expr = colwise_sum(log(sigma_val));
115
116 const int wgs = y_scaled_sq_sum_expr.rows();
117
118 constexpr bool need_mu_derivative
120 || (!is_constant<T_alpha_cl>::value && is_alpha_vector)
121 || (!is_constant<T_y_cl>::value && is_y_vector);
122 matrix_cl<double> mu_derivative_cl(need_mu_derivative ? N : 0, 1);
123 constexpr bool need_mu_derivative_sum
124 = (!is_constant<T_alpha_cl>::value && !is_alpha_vector)
125 || (!is_constant<T_y_cl>::value && !is_y_vector);
126 matrix_cl<double> mu_derivative_sum_cl(need_mu_derivative_sum ? wgs : 0, 1);
127 matrix_cl<double> y_scaled_sq_sum_cl(wgs, 1);
128 constexpr bool need_sigma_derivative = !is_constant_all<T_sigma_cl>::value;
129 matrix_cl<double> sigma_derivative_cl(need_sigma_derivative ? N : 0, 1);
130 constexpr bool need_log_sigma_sum
132 matrix_cl<double> log_sigma_sum_cl(need_log_sigma_sum ? wgs : 0, 1);
133
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));
141
142 double y_scaled_sq_sum = sum(from_matrix_cl(y_scaled_sq_sum_cl));
143 auto ops_partials = make_partials_propagator(y, x, alpha, beta, sigma);
144 double mu_derivative_sum;
145 if (need_mu_derivative_sum) {
146 mu_derivative_sum = sum(from_matrix_cl(mu_derivative_sum_cl));
147 }
149 if (is_y_vector) {
150 partials<0>(ops_partials) = -mu_derivative_cl;
151 } else {
152 forward_as<internal::broadcast_array<double>>(
153 partials<0>(ops_partials))[0]
154 = -mu_derivative_sum;
155 }
156 }
158 partials<1>(ops_partials)
159 = transpose(beta_val * transpose(mu_derivative_cl));
160 }
162 if (is_alpha_vector) {
163 partials<2>(ops_partials) = mu_derivative_cl;
164 } else {
165 forward_as<internal::broadcast_array<double>>(
166 partials<2>(ops_partials))[0]
167 = mu_derivative_sum;
168 }
169 }
171 // transposition of a vector can be done without copying
172 const matrix_cl<double> mu_derivative_transpose_cl(
173 mu_derivative_cl.buffer(), 1, mu_derivative_cl.rows());
174 matrix_cl<double> edge4_partials_transpose_cl
175 = mu_derivative_transpose_cl * x_val;
176 partials<3>(ops_partials)
177 = matrix_cl<double>(edge4_partials_transpose_cl.buffer(),
178 edge4_partials_transpose_cl.cols(), 1);
179 if (beta_val.rows() != 0) {
180 edge<3>(ops_partials)
181 .partials_.add_write_event(
182 edge4_partials_transpose_cl.write_events().back());
183 }
184 }
186 partials<4>(ops_partials) = sigma_derivative_cl;
187 }
188
189 if (!std::isfinite(y_scaled_sq_sum)) {
190 results(
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"))
194 = expressions(isfinite(y_val), isfinite(alpha_val),
195 isfinite(sigma_val) && sigma_val > 0);
196 check_cl(function, "Weight vector", x_val, "finite") = isfinite(x_val);
197 check_cl(function, "Weight vector", beta_val, "finite")
198 = isfinite(beta_val);
199 }
200
201 // Compute log probability.
202 T_partials_return logp(0.0);
204 logp += NEG_LOG_SQRT_TWO_PI * N;
205 }
207 if (is_sigma_vector) {
208 logp -= sum(from_matrix_cl(log_sigma_sum_cl));
209 } else {
210 logp -= N * log(forward_as<double>(sigma_val));
211 }
212 }
213 logp -= 0.5 * y_scaled_sq_sum;
214
215 return ops_partials.build(logp);
216}
217
218} // namespace math
219} // namespace stan
220
221#endif
222#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)
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_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.
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.
size_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:18
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
fvar< T > log(const fvar< T > &x)
Definition log.hpp:15
const double NEG_LOG_SQRT_TWO_PI
The value of minus the natural logarithm of the square root of , .
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:22
auto matrix_vector_multiply(T_matrix &&matrix, T_vector &&vector)
Multiplies a matrix and a vector on an OpenCL device.
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 ...
Definition fvar.hpp:9
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...