Automatic Differentiation
 
Loading...
Searching...
No Matches
skew_double_exponential_cdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_DOUBLE_SKEW_DOUBLE_EXPONENTIAL_CDF_HPP
2#define STAN_MATH_OPENCL_PRIM_DOUBLE_SKEW_DOUBLE_EXPONENTIAL_CDF_HPP
3#ifdef STAN_OPENCL
4
12
13namespace stan {
14namespace math {
15
30template <typename T_y_cl, typename T_loc_cl, typename T_scale_cl,
31 typename T_skewness_cl,
33 T_y_cl, T_loc_cl, T_scale_cl, T_skewness_cl>* = nullptr,
34 require_any_not_stan_scalar_t<T_y_cl, T_loc_cl, T_scale_cl,
35 T_skewness_cl>* = nullptr>
36return_type_t<T_y_cl, T_loc_cl, T_scale_cl, T_skewness_cl>
37skew_double_exponential_cdf(const T_y_cl& y, const T_loc_cl& mu,
38 const T_scale_cl& sigma, const T_skewness_cl& tau) {
39 static constexpr const char* function = "skew_double_exponential_cdf(OpenCL)";
40 using T_partials_return
42 using std::isfinite;
43 using std::isnan;
44
45 check_consistent_sizes(function, "Random variable", y, "Location parameter",
46 mu, "Shape parameter", sigma, "Skewness parameter",
47 tau);
48 const size_t N = max_size(y, mu, sigma, tau);
49 if (N == 0) {
50 return 1.0;
51 }
52
53 const auto& y_col = as_column_vector_or_scalar(y);
54 const auto& mu_col = as_column_vector_or_scalar(mu);
55 const auto& sigma_col = as_column_vector_or_scalar(sigma);
56 const auto& tau_col = as_column_vector_or_scalar(tau);
57
58 const auto& y_val = value_of(y_col);
59 const auto& mu_val = value_of(mu_col);
60 const auto& sigma_val = value_of(sigma_col);
61 const auto& tau_val = value_of(tau_col);
62
63 auto check_y_not_nan
64 = check_cl(function, "Random variable", y_val, "not NaN");
65 auto y_not_nan_expr = !isnan(y_val);
66 auto check_mu_finite
67 = check_cl(function, "Location parameter", mu_val, "finite");
68 auto mu_finite_expr = isfinite(mu_val);
69 auto check_sigma_positive_finite
70 = check_cl(function, "Scale parameter", sigma_val, "positive finite");
71 auto sigma_positive_finite_expr = 0.0 < sigma_val && isfinite(sigma_val);
72 auto check_tau_bounded = check_cl(function, "Skewness parameter", tau_val,
73 "in the interval [0, 1]");
74 auto tau_bounded_expr = 0.0 < tau_val && tau_val <= 1.0;
75
76 auto inv_sigma = elt_divide(1.0, sigma_val);
77 auto y_m_mu = y_val - mu_val;
78 auto diff_sign = sign(y_m_mu);
79 auto diff_sign_smaller_0 = diff_sign < 0;
80 auto abs_diff_y_mu = fabs(y_m_mu);
81 auto abs_diff_y_mu_over_sigma = elt_multiply(abs_diff_y_mu, inv_sigma);
82 auto expo
83 = elt_multiply(diff_sign_smaller_0 + elt_multiply(diff_sign, tau_val),
84 abs_diff_y_mu_over_sigma);
85 auto tau_minus_1 = tau_val - 1.0;
86 auto inv_exp_2_expo_tau = elt_divide(1.0, exp(2.0 * expo) + tau_minus_1);
87
88 auto cdf_expr = colwise_prod(
89 select(y_val <= mu_val, elt_multiply(tau_val, exp(-2.0 * expo)),
90 1.0 + elt_multiply(tau_minus_1, exp(-2.0 * expo))));
91
92 auto cond = y_val < mu_val;
93 auto y_deriv1
94 = -2.0
95 * select(cond, elt_multiply(inv_sigma, tau_minus_1),
96 elt_multiply(elt_multiply(tau_minus_1, tau_val),
97 elt_multiply(inv_sigma, inv_exp_2_expo_tau)));
98 auto sigma_deriv1
99 = select(cond, 2.0 * elt_multiply(inv_sigma, expo),
100 elt_divide(elt_multiply(-y_deriv1, expo), tau_val));
101 auto tau_deriv1 = select(
102 cond,
103 elt_divide(1.0, tau_val)
104 + 2.0 * elt_multiply(elt_multiply(inv_sigma, y_m_mu), diff_sign),
105 elt_multiply(sigma_val - 2.0 * elt_multiply(tau_minus_1, y_m_mu),
106 elt_multiply(inv_sigma, inv_exp_2_expo_tau)));
107
108 matrix_cl<double> cdf_cl;
109 matrix_cl<double> y_deriv_cl;
110 matrix_cl<double> mu_deriv_cl;
111 matrix_cl<double> sigma_deriv_cl;
112 matrix_cl<double> tau_deriv_cl;
113
114 results(check_y_not_nan, check_mu_finite, check_sigma_positive_finite,
115 check_tau_bounded, cdf_cl, mu_deriv_cl, sigma_deriv_cl, tau_deriv_cl)
116 = expressions(
117 y_not_nan_expr, mu_finite_expr, sigma_positive_finite_expr,
118 tau_bounded_expr, cdf_expr,
122
123 T_partials_return cdf = (from_matrix_cl(cdf_cl)).prod();
124
125 auto ops_partials
126 = make_partials_propagator(y_col, mu_col, sigma_col, tau_col);
128 auto y_deriv = mu_deriv_cl * cdf;
129 auto mu_deriv = -y_deriv;
130 auto sigma_deriv = sigma_deriv_cl * cdf;
131 auto tau_deriv = tau_deriv_cl * cdf;
132
133 results(y_deriv_cl, mu_deriv_cl, sigma_deriv_cl, tau_deriv_cl)
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(tau_deriv_cl);
150 }
151 }
152 return ops_partials.build(cdf);
153}
154
155} // namespace math
156} // namespace stan
157#endif
158#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)
select_< as_operation_cl_t< T_condition >, as_operation_cl_t< T_then >, as_operation_cl_t< T_else > > select(T_condition &&condition, T_then &&then, T_else &&els)
Selection operation on kernel generator expressions.
Definition select.hpp:148
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.
auto colwise_prod(T &&a)
Column wise product - reduction 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
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_cdf(const T_y_cl &y, const T_loc_cl &mu, const T_scale_cl &sigma, const T_skewness_cl &tau)
Returns the skew double exponential cumulative density function.
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.
value_type_t< T > prod(const T &m)
Calculates product of given kernel generator expression elements.
Definition prod.hpp:21
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
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
fvar< T > fabs(const fvar< T > &x)
Definition fabs.hpp:16
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:15
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 ...
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...