Automatic Differentiation
 
Loading...
Searching...
No Matches
skew_double_exponential_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_SKEW_DOUBLE_EXPONENTIAL_LPDF_HPP
2#define STAN_MATH_OPENCL_PRIM_SKEW_DOUBLE_EXPONENTIAL_LPDF_HPP
3#ifdef STAN_OPENCL
4
12
13namespace stan {
14namespace math {
15
33template <bool propto, typename T_y_cl, typename T_loc_cl, typename T_scale_cl,
34 typename T_skewness_cl,
36 T_y_cl, T_loc_cl, T_scale_cl, T_skewness_cl>* = nullptr,
37 require_any_not_stan_scalar_t<T_y_cl, T_loc_cl, T_scale_cl,
38 T_skewness_cl>* = nullptr>
39return_type_t<T_y_cl, T_loc_cl, T_scale_cl, T_skewness_cl>
40skew_double_exponential_lpdf(const T_y_cl& y, const T_loc_cl& mu,
41 const T_scale_cl& sigma,
42 const T_skewness_cl& tau) {
43 static constexpr const char* function
44 = "skew_double_exponential_lpdf(OpenCL)";
45 using T_partials_return
47 using std::isfinite;
48 using std::isnan;
49
50 check_consistent_sizes(function, "Random variable", y, "Location parameter",
51 mu, "Scale parameter", sigma, "Inv_scale paramter",
52 tau);
53 const size_t N = max_size(y, mu, sigma, tau);
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_skewness_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& tau_col = as_column_vector_or_scalar(tau);
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& tau_val = value_of(tau_col);
71
72 auto check_y_not_nan
73 = check_cl(function, "Random variable", y_val, "not_nan");
74 auto y_not_nan_expr = !isnan(y_val);
75 auto check_mu_finite
76 = check_cl(function, "Location parameter", mu_val, "finite");
77 auto mu_finite_expr = isfinite(mu_val);
78 auto check_sigma_positive_finite
79 = check_cl(function, "Scale parameter", sigma_val, "positive finite");
80 auto sigma_positive_finite_expr = isfinite(sigma_val) && sigma_val > 0;
81 auto check_tau_bounded = check_cl(function, "Skewness parameter", tau_val,
82 "in the interval [0, 1]");
83 auto tau_bounded_expr = 0.0 < tau_val && tau_val < 1.0;
84
85 auto inv_sigma = elt_divide(1.0, sigma_val);
86 auto y_m_mu = y_val - mu_val;
87 auto diff_sign = sign(y_m_mu);
88 auto diff_sign_smaller_0 = diff_sign < 0.0;
89 auto abs_diff_y_mu = fabs(y_m_mu);
90 auto abs_diff_y_mu_over_sigma = elt_multiply(abs_diff_y_mu, inv_sigma);
91 auto tmp = diff_sign_smaller_0 + elt_multiply(diff_sign, tau_val);
92 auto expo = elt_multiply(tmp, abs_diff_y_mu_over_sigma);
93
94 auto logp1 = -2.0 * expo;
95 auto logp2 = static_select<include_summand<propto, T_scale_cl>::value>(
96 logp1 - log(sigma_val), logp1);
97 auto logp3 = static_select<include_summand<propto, T_skewness_cl>::value>(
98 logp2 + log(tau_val) + log1m(tau_val), logp2);
99 auto logp_expr = colwise_sum(logp3);
100
101 auto mu_deriv = 2.0 * elt_multiply(tmp, elt_multiply(diff_sign, inv_sigma));
102 auto y_deriv = -mu_deriv;
103 auto sigma_deriv = -inv_sigma + 2.0 * elt_multiply(expo, inv_sigma);
104 auto tau_deriv = elt_divide(1.0, tau_val) - elt_divide(1.0, 1.0 - tau_val)
105 - elt_multiply(diff_sign * 2.0, abs_diff_y_mu_over_sigma);
106
107 matrix_cl<double> logp_cl;
108 matrix_cl<double> y_deriv_cl;
109 matrix_cl<double> mu_deriv_cl;
110 matrix_cl<double> sigma_deriv_cl;
111 matrix_cl<double> tau_deriv_cl;
112
113 results(check_y_not_nan, check_mu_finite, check_sigma_positive_finite,
114 check_tau_bounded, logp_cl, y_deriv_cl, mu_deriv_cl, sigma_deriv_cl,
115 tau_deriv_cl)
116 = expressions(y_not_nan_expr, mu_finite_expr, sigma_positive_finite_expr,
117 tau_bounded_expr, logp_expr,
122
123 T_partials_return logp = sum(from_matrix_cl(logp_cl));
124
126 logp += N * LOG_TWO;
127 }
128
129 auto ops_partials
130 = make_partials_propagator(y_col, mu_col, sigma_col, tau_col);
132 partials<0>(ops_partials) = std::move(y_deriv_cl);
133 }
135 partials<1>(ops_partials) = std::move(mu_deriv_cl);
136 }
138 partials<2>(ops_partials) = std::move(sigma_deriv_cl);
139 }
141 partials<3>(ops_partials) = std::move(tau_deriv_cl);
142 }
143 return ops_partials.build(logp);
144}
145
146} // namespace math
147} // namespace stan
148
149#endif
150#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_skewness_cl > skew_double_exponential_lpdf(const T_y_cl &y, const T_loc_cl &mu, const T_scale_cl &sigma, const T_skewness_cl &tau)
Returns the log PMF of the skew double exponential distribution.
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.
auto sign(const T &x)
Returns signs of the arguments.
Definition sign.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:18
static constexpr double LOG_TWO
The natural logarithm of 2, .
Definition constants.hpp:80
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
fvar< T > log1m(const fvar< T > &x)
Definition log1m.hpp:12
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
fvar< T > fabs(const fvar< T > &x)
Definition fabs.hpp:16
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 ...
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...