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>
62inline return_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 constexpr (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 constexpr (is_sigma_vector) {
83 check_size_match(function, "Rows of ", "x", N, "size of ", "sigma",
84 math::size(sigma));
85 }
86 if constexpr (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 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);
122 matrix_cl<double> mu_derivative_cl(need_mu_derivative ? N : 0, 1);
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);
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_autodiff_v<T_sigma_cl>;
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 }
148 if constexpr (is_autodiff_v<T_y_cl>) {
149 if constexpr (is_y_vector) {
150 partials<0>(ops_partials) = -mu_derivative_cl;
151 } else {
152 partials<0>(ops_partials)[0] = -mu_derivative_sum;
153 }
154 }
155 if constexpr (is_autodiff_v<T_x_cl>) {
156 partials<1>(ops_partials)
157 = transpose(beta_val * transpose(mu_derivative_cl));
158 }
159 if constexpr (is_autodiff_v<T_alpha_cl>) {
160 if constexpr (is_alpha_vector) {
161 partials<2>(ops_partials) = mu_derivative_cl;
162 } else {
163 partials<2>(ops_partials)[0] = mu_derivative_sum;
164 }
165 }
166 if constexpr (is_autodiff_v<T_beta_cl>) {
167 // transposition of a vector can be done without copying
168 const matrix_cl<double> mu_derivative_transpose_cl(
169 mu_derivative_cl.buffer(), 1, mu_derivative_cl.rows());
170 matrix_cl<double> edge4_partials_transpose_cl
171 = mu_derivative_transpose_cl * x_val;
172 partials<3>(ops_partials)
173 = matrix_cl<double>(edge4_partials_transpose_cl.buffer(),
174 edge4_partials_transpose_cl.cols(), 1);
175 if (beta_val.rows() != 0) {
176 edge<3>(ops_partials)
177 .partials_.add_write_event(
178 edge4_partials_transpose_cl.write_events().back());
179 }
180 }
181 if constexpr (is_autodiff_v<T_sigma_cl>) {
182 partials<4>(ops_partials) = sigma_derivative_cl;
183 }
184
185 if (!std::isfinite(y_scaled_sq_sum)) {
186 results(
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"))
190 = expressions(isfinite(y_val), isfinite(alpha_val),
191 isfinite(sigma_val) && sigma_val > 0);
192 check_cl(function, "Weight vector", x_val, "finite") = isfinite(x_val);
193 check_cl(function, "Weight vector", beta_val, "finite")
194 = isfinite(beta_val);
195 }
196
197 // Compute log probability.
198 T_partials_return logp(0.0);
199 if constexpr (include_summand<propto>::value) {
200 logp += NEG_LOG_SQRT_TWO_PI * N;
201 }
203 if constexpr (is_sigma_vector) {
204 logp -= sum(from_matrix_cl(log_sigma_sum_cl));
205 } else {
206 logp -= N * log(sigma_val);
207 }
208 }
209 logp -= 0.5 * y_scaled_sq_sum;
210
211 return ops_partials.build(logp);
212}
213
214} // namespace math
215} // namespace stan
216
217#endif
218#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.
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
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:18
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.
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.
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...