1 #ifndef STAN_MATH_OPENCL_PRIM_NORMAL_LPDF_HPP
2 #define STAN_MATH_OPENCL_PRIM_NORMAL_LPDF_HPP
3 #ifdef STAN_OPENCL
4
5 #include <stan/math/prim/meta.hpp>
6 #include <stan/math/prim/err.hpp>
7 #include <stan/math/prim/fun/constants.hpp>
8 #include <stan/math/prim/fun/elt_divide.hpp>
9 #include <stan/math/prim/fun/elt_multiply.hpp>
10 #include <stan/math/opencl/kernel_generator.hpp>
11 #include <stan/math/prim/functor/operands_and_partials.hpp>
12
13 namespace stan {
14 namespace math {
15
16 /** \ingroup opencl
17 * The log of the normal density for the specified scalar(s) given
18 * the specified mean(s) and deviation(s). y, mu, or sigma can
19 * each be either a scalar or a vector matrix_cl. Any vector inputs
20 * must be the same length.
21 *
22 * <p>The result log probability is defined to be the sum of the
23 * log probabilities for each observation/mean/deviation triple.
24 *
25 * @tparam T_y_cl type of scalar
26 * @tparam T_loc_cl type of location parameter
27 * @tparam T_scale_cl type of scale parameter
28 * @param y (Sequence of) scalar(s).
29 * @param mu (Sequence of) location parameter(s)
30 * for the normal distribution.
31 * @param sigma (Sequence of) scale parameters for the normal distribution.
32 * @return The log of the product of the densities.
33 * @throw std::domain_error if the scale is not positive.
34 */
35 template <
36 bool propto, typename T_y_cl, typename T_loc_cl, typename T_scale_cl,
37 require_all_prim_or_rev_kernel_expression_t<T_y_cl, T_loc_cl,
38 T_scale_cl>* = nullptr,
39 require_any_not_stan_scalar_t<T_y_cl, T_loc_cl, T_scale_cl>* = nullptr>
normal_lpdf(const T_y_cl & y,const T_loc_cl & mu,const T_scale_cl & sigma)40 inline return_type_t<T_y_cl, T_loc_cl, T_scale_cl> normal_lpdf(
41 const T_y_cl& y, const T_loc_cl& mu, const T_scale_cl& sigma) {
42 static const char* function = "normal_lpdf(OpenCL)";
43 using T_partials_return = partials_return_t<T_y_cl, T_loc_cl, T_scale_cl>;
44 using std::isfinite;
45 using std::isnan;
46
47 check_consistent_sizes(function, "Random variable", y, "Location parameter",
48 mu, "Scale parameter", sigma);
49 const size_t N = max_size(y, mu, sigma);
50 if (N == 0) {
51 return 0.0;
52 }
53 if (!include_summand<propto, T_y_cl, T_loc_cl, T_scale_cl>::value) {
54 return 0.0;
55 }
56
57 const auto& y_col = as_column_vector_or_scalar(y);
58 const auto& mu_col = as_column_vector_or_scalar(mu);
59 const auto& sigma_col = as_column_vector_or_scalar(sigma);
60
61 const auto& y_val = value_of(y_col);
62 const auto& mu_val = value_of(mu_col);
63 const auto& sigma_val = value_of(sigma_col);
64
65 auto check_y_not_nan
66 = check_cl(function, "Random variable", y_val, "not NaN");
67 auto y_not_nan = !isnan(y_val);
68 auto check_mu_finite
69 = check_cl(function, "Location parameter", mu_val, "finite");
70 auto mu_finite = isfinite(mu_val);
71 auto check_sigma_positive
72 = check_cl(function, "Scale parameter", sigma_val, "positive");
73 auto sigma_positive = 0 < sigma_val;
74
75 auto inv_sigma = elt_divide(1., sigma_val);
76 auto y_scaled = elt_multiply((y_val - mu_val), inv_sigma);
77 auto y_scaled_sq = elt_multiply(y_scaled, y_scaled);
78
79 auto logp1 = -0.5 * y_scaled_sq;
80 auto logp_expr
81 = colwise_sum(static_select<include_summand<propto, T_scale_cl>::value>(
82 logp1 - log(sigma_val), logp1));
83
84 auto scaled_diff = elt_multiply(inv_sigma, y_scaled);
85 auto sigma_deriv = elt_multiply(inv_sigma, y_scaled_sq) - inv_sigma;
86
87 matrix_cl<double> logp_cl;
88 matrix_cl<double> mu_deriv_cl;
89 matrix_cl<double> y_deriv_cl;
90 matrix_cl<double> sigma_deriv_cl;
91
92 results(check_y_not_nan, check_mu_finite, check_sigma_positive, logp_cl,
93 y_deriv_cl, mu_deriv_cl, sigma_deriv_cl)
94 = expressions(y_not_nan, mu_finite, sigma_positive, logp_expr,
95 calc_if<!is_constant<T_y_cl>::value>(-scaled_diff),
96 calc_if<!is_constant<T_loc_cl>::value>(scaled_diff),
97 calc_if<!is_constant<T_scale_cl>::value>(sigma_deriv));
98
99 T_partials_return logp = sum(from_matrix_cl(logp_cl));
100
101 if (include_summand<propto>::value) {
102 logp += NEG_LOG_SQRT_TWO_PI * N;
103 }
104
105 operands_and_partials<decltype(y_col), decltype(mu_col), decltype(sigma_col)>
106 ops_partials(y_col, mu_col, sigma_col);
107
108 if (!is_constant<T_y_cl>::value) {
109 ops_partials.edge1_.partials_ = std::move(y_deriv_cl);
110 }
111 if (!is_constant<T_loc_cl>::value) {
112 ops_partials.edge2_.partials_ = std::move(mu_deriv_cl);
113 }
114 if (!is_constant<T_scale_cl>::value) {
115 ops_partials.edge3_.partials_ = std::move(sigma_deriv_cl);
116 }
117 return ops_partials.build(logp);
118 }
119
120 } // namespace math
121 } // namespace stan
122 #endif
123 #endif
124