1#ifndef STAN_MATH_OPENCL_PRIM_STD_NORMAL_LCDF_HPP
2#define STAN_MATH_OPENCL_PRIM_STD_NORMAL_LCDF_HPP
17 double std_normal_lcdf_lcdf_n;
if (std_normal_lcdf_scaled_y > 0.0) {
19 std_normal_lcdf_lcdf_n =
log1p(-0.5 *
erfc(std_normal_lcdf_scaled_y));
20 if (
isnan(std_normal_lcdf_lcdf_n)) {
21 std_normal_lcdf_lcdf_n = 0;
23 }
else if (std_normal_lcdf_scaled_y > -20.0) {
25 std_normal_lcdf_lcdf_n =
log(
erfc(-std_normal_lcdf_scaled_y)) - M_LN2;
26 }
else if (10.0 *
log(fabs(std_normal_lcdf_scaled_y)) <
log(DBL_MAX)) {
31 const double x4 =
pow(std_normal_lcdf_scaled_y, 4);
32 const double x6 =
pow(std_normal_lcdf_scaled_y, 6);
33 const double x8 =
pow(std_normal_lcdf_scaled_y, 8);
34 const double x10 =
pow(std_normal_lcdf_scaled_y, 10);
36 = 0.000658749161529837803157
37 + 0.0160837851487422766278 / std_normal_lcdf_x2
38 + 0.125781726111229246204 / x4 + 0.360344899949804439429 / x6
39 + 0.305326634961232344035 / x8 + 0.0163153871373020978498 / x10;
40 const double temp_q = -0.00233520497626869185443
41 - 0.0605183413124413191178 / std_normal_lcdf_x2
42 - 0.527905102951428412248 / x4
43 - 1.87295284992346047209 / x6
44 - 2.56852019228982242072 / x8 - 1.0 / x10;
45 std_normal_lcdf_lcdf_n
46 +=
log(0.5 * M_2_SQRTPI + (temp_p / temp_q) / std_normal_lcdf_x2)
47 - M_LN2 -
log(-std_normal_lcdf_scaled_y) - std_normal_lcdf_x2;
50 std_normal_lcdf_lcdf_n = -INFINITY;
56 double std_normal_lcdf_dnlcdf = 0.0;
double t = 0.0;
double t2 = 0.0;
61 if (std_normal_lcdf_deriv_scaled_y > 2.9) {
63 t = 1.0 / (1.0 + 0.3275911 * std_normal_lcdf_deriv_scaled_y);
66 std_normal_lcdf_dnlcdf
68 / (
exp(std_normal_lcdf_deriv_x2) - 0.254829592 + 0.284496736 * t
69 - 1.421413741 * t2 + 1.453152027 * t2 * t - 1.061405429 * t4);
70 }
else if (std_normal_lcdf_deriv_scaled_y > 2.5) {
75 t = std_normal_lcdf_deriv_scaled_y - 2.7;
78 std_normal_lcdf_dnlcdf = 0.0003849882382 - 0.002079084702 * t
79 + 0.005229340880 * t2 - 0.008029540137 * t2 * t
80 + 0.008232190507 * t4 - 0.005692364250 * t4 * t
81 + 0.002399496363 *
pow(t, 6);
82 }
else if (std_normal_lcdf_deriv_scaled_y > 2.1) {
84 t = std_normal_lcdf_deriv_scaled_y - 2.3;
87 std_normal_lcdf_dnlcdf = 0.002846135439 - 0.01310032351 * t
88 + 0.02732189391 * t2 - 0.03326906904 * t2 * t
89 + 0.02482478940 * t4 - 0.009883071924 * t4 * t
90 - 0.0002771362254 *
pow(t, 6);
91 }
else if (std_normal_lcdf_deriv_scaled_y > 1.5) {
93 t = std_normal_lcdf_deriv_scaled_y - 1.85;
96 std_normal_lcdf_dnlcdf = 0.01849212058 - 0.06876280470 * t
97 + 0.1099906382 * t2 - 0.09274533184 * t2 * t
98 + 0.03543327418 * t4 + 0.005644855518 * t4 * t
99 - 0.01111434424 *
pow(t, 6);
100 }
else if (std_normal_lcdf_deriv_scaled_y > 0.8) {
102 t = std_normal_lcdf_deriv_scaled_y - 1.15;
105 std_normal_lcdf_dnlcdf = 0.1585747034 - 0.3898677543 * t
106 + 0.3515963775 * t2 - 0.09748053605 * t2 * t
107 - 0.04347986191 * t4 + 0.02182506378 * t4 * t
108 + 0.01074751427 *
pow(t, 6);
109 }
else if (std_normal_lcdf_deriv_scaled_y > 0.1) {
111 t = std_normal_lcdf_deriv_scaled_y - 0.45;
114 std_normal_lcdf_dnlcdf = 0.6245634904 - 0.9521866949 * t
115 + 0.3986215682 * t2 + 0.04700850676 * t2 * t
116 - 0.03478651979 * t4 - 0.01772675404 * t4 * t
117 + 0.0006577254811 *
pow(t, 6);
118 }
else if (10.0 *
log(
fabs(std_normal_lcdf_deriv_scaled_y))
124 t = 1.0 / (1.0 - 0.3275911 * std_normal_lcdf_deriv_scaled_y);
127 std_normal_lcdf_dnlcdf
129 / (0.254829592 * t - 0.284496736 * t2 + 1.421413741 * t2 * t
130 - 1.453152027 * t4 + 1.061405429 * t4 * t);
133 if (std_normal_lcdf_deriv_scaled_y < -29.0) {
134 std_normal_lcdf_dnlcdf
135 += 0.0015065154280332 * std_normal_lcdf_deriv_x2
136 - 0.3993154819705530 * std_normal_lcdf_deriv_scaled_y
137 - 4.2919418242931700;
138 }
else if (std_normal_lcdf_deriv_scaled_y < -17.0) {
139 std_normal_lcdf_dnlcdf
140 += 0.0001263257217272 * std_normal_lcdf_deriv_x2
141 * std_normal_lcdf_deriv_scaled_y
142 + 0.0123586859488623 * std_normal_lcdf_deriv_x2
143 - 0.0860505264736028 * std_normal_lcdf_deriv_scaled_y
145 }
else if (std_normal_lcdf_deriv_scaled_y < -7.0) {
146 std_normal_lcdf_dnlcdf
147 += 0.000471585349920831 * std_normal_lcdf_deriv_x2
148 * std_normal_lcdf_deriv_scaled_y
149 + 0.0296839305424034 * std_normal_lcdf_deriv_x2
150 + 0.207402143352332 * std_normal_lcdf_deriv_scaled_y
152 }
else if (std_normal_lcdf_deriv_scaled_y < -3.9) {
153 std_normal_lcdf_dnlcdf
154 += -0.0006972280656443 * std_normal_lcdf_deriv_x2
155 * std_normal_lcdf_deriv_scaled_y
156 + 0.0068218494628567 * std_normal_lcdf_deriv_x2
157 + 0.0585761964460277 * std_normal_lcdf_deriv_scaled_y
158 + 0.1034397670201370;
159 }
else if (std_normal_lcdf_deriv_scaled_y < -2.1) {
160 std_normal_lcdf_dnlcdf
161 += -0.0018742199480885 * std_normal_lcdf_deriv_x2
162 * std_normal_lcdf_deriv_scaled_y
163 - 0.0097119598291202 * std_normal_lcdf_deriv_x2
164 - 0.0170137970924080 * std_normal_lcdf_deriv_scaled_y
165 - 0.0100428567412041;
167 }
else { std_normal_lcdf_dnlcdf = INFINITY; });
178template <
typename T_y_cl,
179 require_all_prim_or_rev_kernel_expression_t<T_y_cl>* =
nullptr,
180 require_any_not_stan_scalar_t<T_y_cl>* =
nullptr>
182 static constexpr const char* function =
"std_normal_lcdf(OpenCL)";
192 const auto& y_val =
value_of(y_col);
195 =
check_cl(function,
"Random variable", y_val,
"not NaN");
196 auto y_not_nan_expr = !isnan(y_val);
199 auto x2 =
square(scaled_y);
201 opencl_code<internal::opencl_std_normal_lcdf_impl>(
202 std::make_tuple(
"std_normal_lcdf_scaled_y",
"std_normal_lcdf_x2"),
204 .
template output<double>(
"std_normal_lcdf_lcdf_n"));
205 auto dnlcdf = opencl_code<internal::opencl_std_normal_lcdf_dnlcdf>(
206 std::make_tuple(
"std_normal_lcdf_deriv_scaled_y",
207 "std_normal_lcdf_deriv_x2"),
209 .template output<double>(
"std_normal_lcdf_dnlcdf");
223 partials<0>(ops_partials) = std::move(y_deriv_cl);
225 return ops_partials.build(lcdf);
Represents an arithmetic matrix on the OpenCL device.
auto check_cl(const char *function, const char *var_name, T &&y, const char *must_be)
Constructs a check on opencl matrix or expression.
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.
calc_if_< true, as_operation_cl_t< T > > calc_if(T &&a)
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 > std_normal_lcdf(const T_y_cl &y)
Returns the log standard normal complementary cumulative distribution function.
auto from_matrix_cl(const T &src)
Copies the source matrix that is stored on the OpenCL device to the destination Eigen matrix.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
const char opencl_std_normal_lcdf_dnlcdf[]
const char opencl_std_normal_lcdf_impl[]
auto pow(const T1 &x1, const T2 &x2)
T value_of(const fvar< T > &v)
Return the value of the specified variable.
fvar< T > log(const fvar< T > &x)
static constexpr double INV_SQRT_TWO
The value of 1 over the square root of 2, .
fvar< T > erfc(const fvar< T > &x)
fvar< T > log1p(const fvar< T > &x)
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
fvar< T > fabs(const fvar< T > &x)
fvar< T > square(const fvar< T > &x)
fvar< T > exp(const fvar< T > &x)
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.
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...