Automatic Differentiation
 
Loading...
Searching...
No Matches
skew_normal_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_SKEW_NORMAL_LPDF_HPP
2#define STAN_MATH_OPENCL_PRIM_SKEW_NORMAL_LPDF_HPP
3#ifdef STAN_OPENCL
4
12
13namespace stan {
14namespace math {
15
36template <bool propto, typename T_y_cl, typename T_loc_cl, typename T_scale_cl,
37 typename T_shape_cl,
39 T_y_cl, T_loc_cl, T_scale_cl, T_shape_cl>* = nullptr,
40 require_any_not_stan_scalar_t<T_y_cl, T_loc_cl, T_scale_cl,
41 T_shape_cl>* = nullptr>
43 const T_y_cl& y, const T_loc_cl& mu, const T_scale_cl& sigma,
44 const T_shape_cl& alpha) {
45 static constexpr const char* function = "skew_normal_lpdf(OpenCL)";
46 using T_partials_return
48 using std::isfinite;
49 using std::isnan;
50
51 check_consistent_sizes(function, "Random variable", y, "Location parameter",
52 mu, "Scale parameter", sigma, "Shape paramter", alpha);
53 const size_t N = max_size(y, mu, sigma, alpha);
54 if (N == 0) {
55 return 0.0;
56 }
57 if (!include_summand<propto, T_y_cl, T_loc_cl, T_scale_cl,
58 T_shape_cl>::value) {
59 return 0.0;
60 }
61
62 const auto& y_col = as_column_vector_or_scalar(y);
63 const auto& mu_col = as_column_vector_or_scalar(mu);
64 const auto& sigma_col = as_column_vector_or_scalar(sigma);
65 const auto& alpha_col = as_column_vector_or_scalar(alpha);
66
67 const auto& y_val = value_of(y_col);
68 const auto& mu_val = value_of(mu_col);
69 const auto& sigma_val = value_of(sigma_col);
70 const auto& alpha_val = value_of(alpha_col);
71
72 auto check_y_not_nan
73 = check_cl(function, "Random variable", y_val, "not NaN");
74 auto y_not_nan = !isnan(y_val);
75 auto check_mu_finite
76 = check_cl(function, "Location parameter", mu_val, "finite");
77 auto mu_finite = isfinite(mu_val);
78 auto check_sigma_positive
79 = check_cl(function, "Scale parameter", sigma_val, "positive");
80 auto sigma_positive = 0 < sigma_val;
81 auto check_alpha_finite
82 = check_cl(function, "Shape parameter", alpha_val, "finite");
83 auto alpha_finite = isfinite(alpha_val);
84
85 auto inv_sigma = elt_divide(1., sigma_val);
86 auto y_minus_mu_over_sigma = elt_multiply((y_val - mu_val), inv_sigma);
87 auto log_erfc_alpha_z = log(
88 erfc(elt_multiply(alpha_val, y_minus_mu_over_sigma) * -INV_SQRT_TWO));
89
90 auto logp1 = log_erfc_alpha_z;
91 auto logp2 = static_select<include_summand<propto, T_scale_cl>::value>(
92 logp1 - log(sigma_val), logp1);
93 auto logp_expr = colwise_sum(
96 logp2
97 - elt_multiply(y_minus_mu_over_sigma, y_minus_mu_over_sigma)
98 * 0.5,
99 logp2));
100
101 auto scaled = elt_multiply(alpha_val, y_minus_mu_over_sigma) * INV_SQRT_TWO;
102 auto deriv_logerf = SQRT_TWO_OVER_SQRT_PI
103 * exp(-elt_multiply(scaled, scaled) - log_erfc_alpha_z);
104 auto y_loc_deriv = elt_multiply(
105 y_minus_mu_over_sigma - elt_multiply(deriv_logerf, alpha_val), inv_sigma);
106 auto sigma_deriv
107 = elt_multiply(elt_multiply(y_minus_mu_over_sigma
108 - elt_multiply(deriv_logerf, alpha_val),
109 y_minus_mu_over_sigma)
110 - 1,
111 inv_sigma);
112 auto alpha_deriv = elt_multiply(deriv_logerf, y_minus_mu_over_sigma);
113
114 matrix_cl<double> logp_cl;
115 matrix_cl<double> mu_deriv_cl;
116 matrix_cl<double> y_deriv_cl;
117 matrix_cl<double> sigma_deriv_cl;
118 matrix_cl<double> alpha_deriv_cl;
119
120 results(check_y_not_nan, check_mu_finite, check_sigma_positive,
121 check_alpha_finite, logp_cl, y_deriv_cl, mu_deriv_cl, sigma_deriv_cl,
122 alpha_deriv_cl)
123 = expressions(y_not_nan, mu_finite, sigma_positive, alpha_finite,
124 logp_expr,
125 calc_if<!is_constant<T_y_cl>::value>(-y_loc_deriv),
129
130 T_partials_return logp = sum(from_matrix_cl(logp_cl));
131
133 logp -= HALF_LOG_TWO_PI * N;
134 }
135
136 auto ops_partials
137 = make_partials_propagator(y_col, mu_col, sigma_col, alpha_col);
138
140 partials<0>(ops_partials) = std::move(y_deriv_cl);
141 }
143 partials<1>(ops_partials) = std::move(mu_deriv_cl);
144 }
146 partials<2>(ops_partials) = std::move(sigma_deriv_cl);
147 }
149 partials<3>(ops_partials) = std::move(alpha_deriv_cl);
150 }
151 return ops_partials.build(logp);
152}
153
154} // namespace math
155} // namespace stan
156#endif
157#endif
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 as_column_vector_or_scalar(T &&a)
as_column_vector_or_scalar of 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)
calc_if_< true, as_operation_cl_t< T > > calc_if(T &&a)
Definition calc_if.hpp:121
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_loc_cl, T_scale_cl, T_shape_cl > skew_normal_lpdf(const T_y_cl &y, const T_loc_cl &mu, const T_scale_cl &sigma, const T_shape_cl &alpha)
The log of the skew normal density for the specified scalar(s) given the specified mean(s),...
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.
require_any_not_t< is_stan_scalar< std::decay_t< Types > >... > require_any_not_stan_scalar_t
Require at least one of the types do not satisfy is_stan_scalar.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
size_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:19
static constexpr double SQRT_TWO_OVER_SQRT_PI
The square root of 2 divided by the square root of , .
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
static constexpr double INV_SQRT_TWO
The value of 1 over the square root of 2, .
T1 static_select(T1 &&a, T2 &&b)
Returns one of the arguments that can be of different type, depending on the compile time condition.
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:22
fvar< T > erfc(const fvar< T > &x)
Definition erfc.hpp:15
static constexpr double HALF_LOG_TWO_PI
The value of half the natural logarithm , .
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:13
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
bool isnan(const stan::math::var &a)
Checks if the given number is NaN.
Definition std_isnan.hpp:18
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...