Automatic Differentiation
 
Loading...
Searching...
No Matches
exp_mod_normal_lcdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_DOUBLE_EXP_MOD_NORMAL_LCDF_HPP
2#define STAN_MATH_OPENCL_PRIM_DOUBLE_EXP_MOD_NORMAL_LCDF_HPP
3#ifdef STAN_OPENCL
4
13
14namespace stan {
15namespace math {
16
32template <typename T_y_cl, typename T_loc_cl, typename T_scale_cl,
33 typename T_inv_scale_cl,
35 T_y_cl, T_loc_cl, T_scale_cl, T_inv_scale_cl>* = nullptr,
36 require_any_not_stan_scalar_t<T_y_cl, T_loc_cl, T_scale_cl,
37 T_inv_scale_cl>* = nullptr>
38inline return_type_t<T_y_cl, T_loc_cl, T_scale_cl, T_inv_scale_cl>
39exp_mod_normal_lcdf(const T_y_cl& y, const T_loc_cl& mu,
40 const T_scale_cl& sigma, const T_inv_scale_cl& lambda) {
41 static constexpr const char* function = "exp_mod_normal_lcdf(OpenCL)";
42 using T_partials_return
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
54 const auto& y_col = as_column_vector_or_scalar(y);
55 const auto& mu_col = as_column_vector_or_scalar(mu);
56 const auto& sigma_col = as_column_vector_or_scalar(sigma);
57 const auto& lambda_col = as_column_vector_or_scalar(lambda);
58
59 const auto& y_val = value_of(y_col);
60 const auto& mu_val = value_of(mu_col);
61 const auto& sigma_val = value_of(sigma_col);
62 const auto& lambda_val = value_of(lambda_col);
63
64 auto check_y_not_nan
65 = check_cl(function, "Random variable", y_val, "not NaN");
66 auto y_not_nan_expr = !isnan(y_val);
67 auto check_mu_finite
68 = check_cl(function, "Location parameter", mu_val, "finite");
69 auto mu_finite_expr = isfinite(mu_val);
70 auto check_sigma_positive_finite
71 = check_cl(function, "Scale parameter", sigma_val, "positive finite");
72 auto sigma_positive_finite_expr = 0 < sigma_val && isfinite(sigma_val);
73 auto check_lambda_positive_finite
74 = check_cl(function, "Inv_cale parameter", lambda_val, "positive finite");
75 auto lambda_positive_finite_expr = 0 < lambda_val && isfinite(lambda_val);
76
77 auto any_y_neg_inf = colwise_max(cast<char>(y_val == NEGATIVE_INFTY));
78 auto any_y_pos_inf = colwise_max(cast<char>(y_val == INFTY));
79 auto sigma_inv = elt_divide(1.0, sigma_val);
80 auto diff = y_val - mu_val;
81 auto scaled_diff = elt_multiply(diff * INV_SQRT_TWO, sigma_inv);
82 auto v = elt_multiply(lambda_val, sigma_val);
83 auto scaled_diff_diff = scaled_diff - v * INV_SQRT_TWO;
84 auto cdf_term_1 = 0.5 + 0.5 * erf(scaled_diff);
85 auto cdf_term_2_phi = 0.5 * (1.0 + erf(scaled_diff_diff));
86 auto log_exp_term = 0.5 * square(v) - elt_multiply(lambda_val, diff);
87 auto exp_term = exp(log_exp_term);
88 auto cdf_term_2 = elt_multiply(exp_term, cdf_term_2_phi);
89 auto cdf_n = cdf_term_1 - cdf_term_2;
90 auto use_stable = cdf_n <= 0.0 || !isfinite(cdf_n);
91
92 auto exp_term_2 = exp(-square(scaled_diff_diff));
93 auto deriv_1
94 = elt_multiply(elt_multiply(lambda_val, exp_term), cdf_term_2_phi);
95 auto deriv_2 = INV_SQRT_TWO_PI
96 * elt_multiply(elt_multiply(exp_term, exp_term_2), sigma_inv);
97 auto deriv_3
98 = INV_SQRT_TWO_PI * elt_multiply(exp(-square(scaled_diff)), sigma_inv);
99 auto direct_cdf_log = log(cdf_n);
100 auto direct_y_deriv = elt_divide(deriv_1 - deriv_2 + deriv_3, cdf_n);
101 auto direct_mu_deriv = -direct_y_deriv;
102 auto direct_sigma_deriv = -elt_divide(
103 elt_multiply(deriv_1 - deriv_2, v)
104 + elt_multiply(deriv_3 - deriv_2, scaled_diff) * SQRT_TWO,
105 cdf_n);
106 auto direct_lambda_deriv = elt_divide(
107 elt_multiply(exp_term,
108 INV_SQRT_TWO_PI * elt_multiply(sigma_val, exp_term_2)
109 - elt_multiply(elt_multiply(v, sigma_val) - diff,
110 cdf_term_2_phi)),
111 cdf_n);
112
113 auto log_cdf_term_1 = std_normal_lcdf_scaled_impl(scaled_diff);
114 auto dlog_cdf_term_1 = std_normal_lcdf_dscaled_impl(scaled_diff);
115 auto log_cdf_term_2_phi = std_normal_lcdf_scaled_impl(scaled_diff_diff);
116 auto dlog_cdf_term_2_phi = std_normal_lcdf_dscaled_impl(scaled_diff_diff);
117 auto log_cdf_term_2 = log_exp_term + log_cdf_term_2_phi;
118 auto log_cdf_n = log_diff_exp(log_cdf_term_1, log_cdf_term_2);
119 auto cdf_term_1_weight = exp(log_cdf_term_1 - log_cdf_n);
120 auto cdf_term_2_weight = exp(log_cdf_term_2 - log_cdf_n);
121 auto scaled_diff_deriv
122 = elt_multiply(dlog_cdf_term_1, sigma_inv * INV_SQRT_TWO);
123 auto scaled_diff_diff_deriv
124 = elt_multiply(dlog_cdf_term_2_phi, sigma_inv * INV_SQRT_TWO);
125 auto stable_y_deriv
126 = elt_multiply(cdf_term_1_weight, scaled_diff_deriv)
127 - elt_multiply(cdf_term_2_weight, -lambda_val + scaled_diff_diff_deriv);
128 auto stable_mu_deriv = -stable_y_deriv;
129 auto stable_sigma_deriv
130 = elt_multiply(cdf_term_1_weight,
131 -elt_multiply(dlog_cdf_term_1,
132 elt_multiply(scaled_diff, sigma_inv)))
133 - elt_multiply(
134 cdf_term_2_weight,
135 elt_multiply(lambda_val, v)
136 - elt_multiply(
137 dlog_cdf_term_2_phi,
138 elt_multiply(scaled_diff + v * INV_SQRT_TWO, sigma_inv)));
139 auto stable_lambda_deriv = -elt_multiply(
140 cdf_term_2_weight,
141 elt_multiply(v, sigma_val) - diff
142 - elt_multiply(dlog_cdf_term_2_phi, sigma_val * INV_SQRT_TWO));
143 auto cdf_log_expr
144 = colwise_sum(select(use_stable, log_cdf_n, direct_cdf_log));
145 auto y_deriv = select(use_stable, stable_y_deriv, direct_y_deriv);
146 auto mu_deriv = select(use_stable, stable_mu_deriv, direct_mu_deriv);
147 auto sigma_deriv = select(use_stable, stable_sigma_deriv, direct_sigma_deriv);
148 auto lambda_deriv
149 = select(use_stable, stable_lambda_deriv, direct_lambda_deriv);
150
151 matrix_cl<char> any_y_neg_inf_cl;
152 matrix_cl<char> any_y_pos_inf_cl;
153 matrix_cl<double> cdf_log_cl;
154 matrix_cl<double> mu_deriv_cl;
155 matrix_cl<double> y_deriv_cl;
156 matrix_cl<double> sigma_deriv_cl;
157 matrix_cl<double> lambda_deriv_cl;
158
159 results(check_y_not_nan, check_mu_finite, check_sigma_positive_finite,
160 check_lambda_positive_finite, any_y_neg_inf_cl, any_y_pos_inf_cl,
161 cdf_log_cl, y_deriv_cl, mu_deriv_cl, sigma_deriv_cl, lambda_deriv_cl)
162 = expressions(y_not_nan_expr, mu_finite_expr, sigma_positive_finite_expr,
163 lambda_positive_finite_expr, any_y_neg_inf, any_y_pos_inf,
164 cdf_log_expr, calc_if<is_autodiff_v<T_y_cl>>(y_deriv),
165 calc_if<is_autodiff_v<T_loc_cl>>(mu_deriv),
166 calc_if<is_autodiff_v<T_scale_cl>>(sigma_deriv),
167 calc_if<is_autodiff_v<T_inv_scale_cl>>(lambda_deriv));
168
169 if (from_matrix_cl(any_y_pos_inf_cl).maxCoeff()) {
170 return 0.0;
171 }
172
173 if (from_matrix_cl(any_y_neg_inf_cl).maxCoeff()) {
174 return NEGATIVE_INFTY;
175 }
176
177 T_partials_return cdf_log = (from_matrix_cl(cdf_log_cl)).sum();
178
179 auto ops_partials
180 = make_partials_propagator(y_col, mu_col, sigma_col, lambda_col);
181
182 if constexpr (is_autodiff_v<T_y_cl>) {
183 partials<0>(ops_partials) = std::move(y_deriv_cl);
184 }
185 if constexpr (is_autodiff_v<T_loc_cl>) {
186 partials<1>(ops_partials) = std::move(mu_deriv_cl);
187 }
188 if constexpr (is_autodiff_v<T_scale_cl>) {
189 partials<2>(ops_partials) = std::move(sigma_deriv_cl);
190 }
191 if constexpr (is_autodiff_v<T_inv_scale_cl>) {
192 partials<3>(ops_partials) = std::move(lambda_deriv_cl);
193 }
194 return ops_partials.build(cdf_log);
195}
196
197} // namespace math
198} // namespace stan
199#endif
200#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.
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
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.
std_normal_lcdf_dscaled_impl_< as_operation_cl_t< T > > std_normal_lcdf_dscaled_impl(T &&a)
std_normal_lcdf_scaled_impl_< as_operation_cl_t< T > > std_normal_lcdf_scaled_impl(T &&a)
return_type_t< T_y_cl, T_loc_cl, T_scale_cl, T_inv_scale_cl > exp_mod_normal_lcdf(const T_y_cl &y, const T_loc_cl &mu, const T_scale_cl &sigma, const T_inv_scale_cl &lambda)
Returns the exp mod normal log 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.
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
fvar< T > erf(const fvar< T > &x)
Definition erf.hpp:16
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, .
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > log_diff_exp(const fvar< T > &x1, const fvar< T > &x2)
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
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
static constexpr double INFTY
Positive infinity.
Definition constants.hpp:46
fvar< T > square(const fvar< T > &x)
Definition square.hpp:12
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