Automatic Differentiation
 
Loading...
Searching...
No Matches
exp_mod_normal_cdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_DOUBLE_EXP_MOD_NORMAL_CDF_HPP
2#define STAN_MATH_OPENCL_PRIM_DOUBLE_EXP_MOD_NORMAL_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_inv_scale_cl,
33 T_y_cl, T_loc_cl, T_scale_cl, T_inv_scale_cl>* = nullptr,
34 require_any_not_stan_scalar_t<T_y_cl, T_loc_cl, T_scale_cl,
35 T_inv_scale_cl>* = nullptr>
37 const T_y_cl& y, const T_loc_cl& mu, const T_scale_cl& sigma,
38 const T_inv_scale_cl& lambda) {
39 static constexpr const char* function = "exp_mod_normal_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, "Scale parameter", sigma);
47 const size_t N = max_size(y, mu, sigma);
48 if (N == 0) {
49 return 1.0;
50 }
51
52 const auto& y_col = as_column_vector_or_scalar(y);
53 const auto& mu_col = as_column_vector_or_scalar(mu);
54 const auto& sigma_col = as_column_vector_or_scalar(sigma);
55 const auto& lambda_col = as_column_vector_or_scalar(lambda);
56
57 const auto& y_val = value_of(y_col);
58 const auto& mu_val = value_of(mu_col);
59 const auto& sigma_val = value_of(sigma_col);
60 const auto& lambda_val = value_of(lambda_col);
61
62 auto check_y_not_nan
63 = check_cl(function, "Random variable", y_val, "not NaN");
64 auto y_not_nan_expr = !isnan(y_val);
65 auto check_mu_finite
66 = check_cl(function, "Location parameter", mu_val, "finite");
67 auto mu_finite_expr = isfinite(mu_val);
68 auto check_sigma_positive_finite
69 = check_cl(function, "Scale parameter", sigma_val, "positive finite");
70 auto sigma_positive_finite_expr = 0 < sigma_val && isfinite(sigma_val);
71 auto check_lambda_positive_finite
72 = check_cl(function, "Inv_cale parameter", lambda_val, "positive finite");
73 auto lambda_positive_finite_expr = 0 < lambda_val && isfinite(lambda_val);
74
75 auto any_y_neg_inf = colwise_max(cast<char>(y_val == NEGATIVE_INFTY));
76 auto inv_sigma = elt_divide(1.0, sigma_val);
77 auto diff = y_val - mu_val;
78 auto v = elt_multiply(lambda_val, sigma_val);
79 auto scaled_diff = elt_multiply(diff, inv_sigma * INV_SQRT_TWO);
80 auto scaled_diff_diff = scaled_diff - v * INV_SQRT_TWO;
81 auto erf_calc = 0.5 * (1.0 + erf(scaled_diff_diff));
82 auto exp_term = exp(0.5 * square(v) - elt_multiply(lambda_val, diff));
83 auto cdf_n = 0.5 + 0.5 * erf(scaled_diff) - elt_multiply(exp_term, erf_calc);
84 auto cdf_expr = colwise_prod(cdf_n);
85
86 auto exp_term_2 = exp(-square(scaled_diff_diff));
87 auto deriv_1 = elt_multiply(elt_multiply(lambda_val, exp_term), erf_calc);
88 auto deriv_2 = INV_SQRT_TWO_PI
89 * elt_multiply(elt_multiply(exp_term, exp_term_2), inv_sigma);
90 auto deriv_3
91 = INV_SQRT_TWO_PI * elt_multiply(exp(-square(scaled_diff)), inv_sigma);
92 auto mu_deriv1 = elt_divide(deriv_2 - deriv_1 - deriv_3, cdf_n);
93 auto sigma_deriv1 = elt_divide(
94 -elt_multiply(deriv_1 - deriv_2, v)
95 + elt_multiply(deriv_3 - deriv_2, scaled_diff) * SQRT_TWO,
96 cdf_n);
97 auto lambda_deriv1 = elt_divide(
99 exp_term,
100 INV_SQRT_TWO_PI * elt_multiply(sigma_val, exp_term_2)
101 - elt_multiply(elt_multiply(v, sigma_val) - diff, erf_calc)),
102 cdf_n);
103
104 matrix_cl<char> any_y_neg_inf_cl;
105 matrix_cl<double> cdf_cl;
106 matrix_cl<double> y_deriv_cl;
107 matrix_cl<double> mu_deriv_cl;
108 matrix_cl<double> sigma_deriv_cl;
109 matrix_cl<double> lambda_deriv_cl;
110
111 results(check_y_not_nan, check_mu_finite, check_sigma_positive_finite,
112 check_lambda_positive_finite, any_y_neg_inf_cl, cdf_cl, y_deriv_cl,
113 mu_deriv_cl, sigma_deriv_cl, lambda_deriv_cl)
114 = expressions(
115 y_not_nan_expr, mu_finite_expr, sigma_positive_finite_expr,
116 lambda_positive_finite_expr, any_y_neg_inf, cdf_expr,
117 calc_if<!is_constant_all<T_y_cl, T_loc_cl, T_scale_cl,
118 T_inv_scale_cl>::value>(cdf_n),
122
123 if (from_matrix_cl(any_y_neg_inf_cl).maxCoeff()) {
124 return 0.0;
125 }
126
127 T_partials_return cdf = (from_matrix_cl(cdf_cl)).prod();
128
129 auto ops_partials
130 = make_partials_propagator(y_col, mu_col, sigma_col, lambda_col);
132 auto mu_deriv = elt_multiply(
134 cdf);
135 auto y_deriv = -mu_deriv;
136 auto sigma_deriv = elt_multiply(
137 static_select<is_constant<T_scale_cl>::value>(0, sigma_deriv_cl), cdf);
138 auto lambda_deriv = elt_multiply(
140 cdf);
141
142 results(y_deriv_cl, mu_deriv_cl, sigma_deriv_cl, lambda_deriv_cl)
143 = expressions(
148
150 partials<0>(ops_partials) = std::move(y_deriv_cl);
151 }
153 partials<1>(ops_partials) = std::move(mu_deriv_cl);
154 }
156 partials<2>(ops_partials) = std::move(sigma_deriv_cl);
157 }
159 partials<3>(ops_partials) = std::move(lambda_deriv_cl);
160 }
161 }
162 return ops_partials.build(cdf);
163}
164
165} // namespace math
166} // namespace stan
167#endif
168#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.
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)
auto colwise_max(T &&a)
Column wise max - reduction of a kernel generator expression.
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_inv_scale_cl > exp_mod_normal_cdf(const T_y_cl &y, const T_loc_cl &mu, const T_scale_cl &sigma, const T_inv_scale_cl &lambda)
Returns the 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.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
value_type_t< T > prod(const T &m)
Calculates product of given kernel generator expression elements.
Definition prod.hpp:21
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
fvar< T > erf(const fvar< T > &x)
Definition erf.hpp:15
static constexpr double INV_SQRT_TWO
The value of 1 over the square root of 2, .
static constexpr double INV_SQRT_TWO_PI
The value of 1 over the square root of , .
static constexpr double NEGATIVE_INFTY
Negative infinity.
Definition constants.hpp:51
static constexpr double SQRT_TWO
The value of 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.
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 > square(const fvar< T > &x)
Definition square.hpp:12
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 ...
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...