Automatic Differentiation
 
Loading...
Searching...
No Matches
std_normal_lcdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_STD_NORMAL_LCDF_HPP
2#define STAN_MATH_OPENCL_PRIM_STD_NORMAL_LCDF_HPP
3#ifdef STAN_OPENCL
4
12
13namespace stan {
14namespace math {
15namespace internal {
17 double std_normal_lcdf_lcdf_n; if (std_normal_lcdf_scaled_y > 0.0) {
18 // CDF(x) = 1/2 + 1/2erf(x) = 1 - 1/2erfc(x)
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;
22 }
23 } else if (std_normal_lcdf_scaled_y > -20.0) {
24 // CDF(x) = 1/2 - 1/2erf(-x) = 1/2erfc(-x)
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)) {
27 // entering territory where erfc(-x)~0
28 // need to use direct numerical approximation of lcdf instead
29 // the following based on W. J. Cody, Math. Comp. 23(107):631-638 (1969)
30 // CDF(x) = 1/2erfc(-x)
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);
35 const double temp_p
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;
48 } else {
49 // std_normal_lcdf_scaled_y^10 term will overflow
50 std_normal_lcdf_lcdf_n = -INFINITY;
51 });
53 // compute partial derivatives
54 // based on analytic form given by:
55 // dln(CDF)/dx = exp(-x^2)/(sqrt(pi)*(1/2+erf(x)/2)
56 double std_normal_lcdf_dnlcdf = 0.0; double t = 0.0; double t2 = 0.0;
57 double t4 = 0.0;
58
59 // calculate using piecewise function
60 // (due to instability / inaccuracy in the various approximations)
61 if (std_normal_lcdf_deriv_scaled_y > 2.9) {
62 // approximation derived from Abramowitz and Stegun (1964) 7.1.26
63 t = 1.0 / (1.0 + 0.3275911 * std_normal_lcdf_deriv_scaled_y);
64 t2 = t * t;
65 t4 = pow(t, 4);
66 std_normal_lcdf_dnlcdf
67 = 0.5 * M_2_SQRTPI
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) {
71 // in the trouble area where all of the standard numerical
72 // approximations are unstable - bridge the gap using Taylor
73 // expansions of the analytic function
74 // use Taylor expansion centred around x=2.7
75 t = std_normal_lcdf_deriv_scaled_y - 2.7;
76 t2 = t * t;
77 t4 = pow(t, 4);
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) {
83 // use Taylor expansion centred around x=2.3
84 t = std_normal_lcdf_deriv_scaled_y - 2.3;
85 t2 = t * t;
86 t4 = pow(t, 4);
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) {
92 // use Taylor expansion centred around x=1.85
93 t = std_normal_lcdf_deriv_scaled_y - 1.85;
94 t2 = t * t;
95 t4 = pow(t, 4);
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) {
101 // use Taylor expansion centred around x=1.15
102 t = std_normal_lcdf_deriv_scaled_y - 1.15;
103 t2 = t * t;
104 t4 = pow(t, 4);
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) {
110 // use Taylor expansion centred around x=0.45
111 t = std_normal_lcdf_deriv_scaled_y - 0.45;
112 t2 = t * t;
113 t4 = pow(t, 4);
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))
119 < log(DBL_MAX)) {
120 // approximation derived from Abramowitz and Stegun (1964) 7.1.26
121 // use fact that erf(x)=-erf(-x)
122 // Abramowitz and Stegun define this for -inf<x<0 but seems to be
123 // accurate for -inf<x<0.1
124 t = 1.0 / (1.0 - 0.3275911 * std_normal_lcdf_deriv_scaled_y);
125 t2 = t * t;
126 t4 = pow(t, 4);
127 std_normal_lcdf_dnlcdf
128 = M_2_SQRTPI
129 / (0.254829592 * t - 0.284496736 * t2 + 1.421413741 * t2 * t
130 - 1.453152027 * t4 + 1.061405429 * t4 * t);
131 // check if we need to add a correction term
132 // (from cubic fit of residuals)
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
144 - 1.252783383752970;
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
151 + 0.425316974683324;
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;
166 }
167 } else { std_normal_lcdf_dnlcdf = INFINITY; });
168} // namespace internal
169
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)";
183 using std::isfinite;
184 using std::isnan;
185
186 const size_t N = math::size(y);
187 if (N == 0) {
188 return 1.0;
189 }
190
191 const auto& y_col = as_column_vector_or_scalar(y);
192 const auto& y_val = value_of(y_col);
193
194 auto check_y_not_nan
195 = check_cl(function, "Random variable", y_val, "not NaN");
196 auto y_not_nan_expr = !isnan(y_val);
197
198 auto scaled_y = y_val * INV_SQRT_TWO;
199 auto x2 = square(scaled_y);
200 auto lcdf_expr = colwise_sum(
201 opencl_code<internal::opencl_std_normal_lcdf_impl>(
202 std::make_tuple("std_normal_lcdf_scaled_y", "std_normal_lcdf_x2"),
203 scaled_y, 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"),
208 scaled_y, x2)
209 .template output<double>("std_normal_lcdf_dnlcdf");
210 auto y_deriv = dnlcdf * INV_SQRT_TWO;
211
212 matrix_cl<double> lcdf_cl;
213 matrix_cl<double> y_deriv_cl;
214
215 results(check_y_not_nan, lcdf_cl, y_deriv_cl) = expressions(
216 y_not_nan_expr, lcdf_expr, calc_if<!is_constant<T_y_cl>::value>(y_deriv));
217
218 double lcdf = from_matrix_cl(lcdf_cl).sum();
219
220 auto ops_partials = make_partials_propagator(y_col);
221
223 partials<0>(ops_partials) = std::move(y_deriv_cl);
224 }
225 return ops_partials.build(lcdf);
226}
227
228} // namespace math
229} // namespace stan
230#endif
231#endif
Represents an arithmetic matrix on the OpenCL device.
Definition matrix_cl.hpp:47
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.
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 > 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.
Definition copy.hpp:61
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>>.
Definition size.hpp:19
const char opencl_std_normal_lcdf_dnlcdf[]
bool isnan(double_d a)
Definition double_d.hpp:327
const char opencl_std_normal_lcdf_impl[]
auto pow(const T1 &x1, const T2 &x2)
Definition pow.hpp:32
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:15
static constexpr double INV_SQRT_TWO
The value of 1 over the square root of 2, .
fvar< T > erfc(const fvar< T > &x)
Definition erfc.hpp:15
fvar< T > log1p(const fvar< T > &x)
Definition log1p.hpp:12
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
fvar< T > fabs(const fvar< T > &x)
Definition fabs.hpp:15
fvar< T > square(const fvar< T > &x)
Definition square.hpp:12
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:13
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
#define STRINGIFY(...)
Definition stringify.hpp:9
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...