Automatic Differentiation
 
Loading...
Searching...
No Matches
std_normal_lcdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_STD_NORMAL_LCDF_HPP
2#define STAN_MATH_PRIM_PROB_STD_NORMAL_LCDF_HPP
3
18#include <cmath>
19#include <limits>
20
21namespace stan {
22namespace math {
23namespace internal {
24constexpr char std_normal_lcdf_func[] = "std_normal_lcdf";
25} // namespace internal
26
53template <
54 const char* func = internal::std_normal_lcdf_func, typename T_y,
56inline return_type_t<T_y> std_normal_lcdf(const T_y& y) {
57 using T_partials_return = partials_return_t<T_y>;
58 using std::exp;
59 using std::fabs;
60 using std::log;
61 using std::pow;
62 using T_y_ref = ref_type_t<T_y>;
63 static constexpr const char* function = func;
64 T_y_ref y_ref = y;
65 check_not_nan(function, "Random variable", y_ref);
66
67 if (size_zero(y)) {
68 return 0;
69 }
70
71 T_partials_return lcdf(0.0);
72 auto ops_partials = make_partials_propagator(y_ref);
73
74 scalar_seq_view<T_y_ref> y_vec(y_ref);
75 size_t N = stan::math::size(y);
76
77 for (size_t n = 0; n < N; n++) {
78 const T_partials_return y_dbl = y_vec.val(n);
79 const T_partials_return scaled_y = y_dbl * INV_SQRT_TWO;
80 const T_partials_return x2 = square(scaled_y);
81
82 // Rigorous numerical approximations are applied here to deal with values
83 // of |scaled_y|>>0. This is needed to deal with rare base-rate
84 // logistic regression problems where it is useful to use an alternative
85 // link function instead.
86 //
87 // use erfc() instead of erf() in order to retain precision
88 // since for x>0 erfc()->0
89 if (scaled_y > 0.0) {
90 // CDF(x) = 1/2 + 1/2erf(x) = 1 - 1/2erfc(x)
91 lcdf += log1p(-0.5 * erfc(scaled_y));
92 if (!is_not_nan(lcdf)) {
93 lcdf = 0;
94 }
95 } else if (scaled_y > -4.0) {
96 // CDF(x) = 1/2 - 1/2erf(-x) = 1/2erfc(-x); -4 is R pnorm's M_SQRT_32
97 // crossover expressed in scaled_y, sqrt(32)/sqrt(2)
98 lcdf += log(erfc(-scaled_y)) + LOG_HALF;
99 } else if (10.0 * log(fabs(scaled_y))
100 < log(std::numeric_limits<T_partials_return>::max())) {
101 // entering territory where erfc(-x)~0
102 // need to use direct numerical approximation of lcdf instead
103 // the following based on W. J. Cody, Math. Comp. 23(107):631-638 (1969)
104 // CDF(x) = 1/2erfc(-x)
105 const T_partials_return x4 = pow(scaled_y, 4);
106 const T_partials_return x6 = pow(scaled_y, 6);
107 const T_partials_return x8 = pow(scaled_y, 8);
108 const T_partials_return x10 = pow(scaled_y, 10);
109 const T_partials_return temp_p
110 = 0.000658749161529837803157 + 0.0160837851487422766278 / x2
111 + 0.125781726111229246204 / x4 + 0.360344899949804439429 / x6
112 + 0.305326634961232344035 / x8 + 0.0163153871373020978498 / x10;
113 const T_partials_return temp_q
114 = -0.00233520497626869185443 - 0.0605183413124413191178 / x2
115 - 0.527905102951428412248 / x4 - 1.87295284992346047209 / x6
116 - 2.56852019228982242072 / x8 - 1.0 / x10;
117 lcdf += LOG_HALF + log(INV_SQRT_PI + (temp_p / temp_q) / x2)
118 - log(-scaled_y) - x2;
119 } else {
120 // scaled_y^10 term will overflow
122 }
123
124 if constexpr (is_autodiff_v<T_y>) {
125 // compute partial derivatives
126 // based on analytic form given by:
127 // dln(CDF)/dx = exp(-x^2)/(sqrt(pi)*(1/2+erf(x)/2)
128 T_partials_return dnlcdf = 0.0;
129 T_partials_return t = 0.0;
130 T_partials_return t2 = 0.0;
131 T_partials_return t4 = 0.0;
132
133 // calculate using piecewise function
134 // (due to instability / inaccuracy in the various approximations)
135 if (scaled_y > 2.9) {
136 // approximation derived from Abramowitz and Stegun (1964) 7.1.26
137 t = 1.0 / (1.0 + 0.3275911 * scaled_y);
138 t2 = square(t);
139 t4 = pow(t, 4);
140 // A&S 7.1.26 puts exp(-x2) in the numerator; keep it there so it
141 // underflows to zero instead of overflowing inside a denominator
142 const T_partials_return exp_m_x2 = exp(-x2);
143 dnlcdf = INV_SQRT_PI * exp_m_x2
144 / (1.0
145 - exp_m_x2
146 * (0.254829592 - 0.284496736 * t + 1.421413741 * t2
147 - 1.453152027 * t2 * t + 1.061405429 * t4));
148 } else if (scaled_y > 2.5) {
149 // in the trouble area where all of the standard numerical
150 // approximations are unstable - bridge the gap using Taylor
151 // expansions of the analytic function
152 // use Taylor expansion centred around x=2.7
153 t = scaled_y - 2.7;
154 t2 = square(t);
155 t4 = pow(t, 4);
156 dnlcdf = 0.0003849882382 - 0.002079084702 * t + 0.005229340880 * t2
157 - 0.008029540137 * t2 * t + 0.008232190507 * t4
158 - 0.005692364250 * t4 * t + 0.002399496363 * pow(t, 6);
159 } else if (scaled_y > 2.1) {
160 // use Taylor expansion centred around x=2.3
161 t = scaled_y - 2.3;
162 t2 = square(t);
163 t4 = pow(t, 4);
164 dnlcdf = 0.002846135439 - 0.01310032351 * t + 0.02732189391 * t2
165 - 0.03326906904 * t2 * t + 0.02482478940 * t4
166 - 0.009883071924 * t4 * t - 0.0002771362254 * pow(t, 6);
167 } else if (scaled_y > 1.5) {
168 // use Taylor expansion centred around x=1.85
169 t = scaled_y - 1.85;
170 t2 = square(t);
171 t4 = pow(t, 4);
172 dnlcdf = 0.01849212058 - 0.06876280470 * t + 0.1099906382 * t2
173 - 0.09274533184 * t2 * t + 0.03543327418 * t4
174 + 0.005644855518 * t4 * t - 0.01111434424 * pow(t, 6);
175 } else if (scaled_y > 0.8) {
176 // use Taylor expansion centred around x=1.15
177 t = scaled_y - 1.15;
178 t2 = square(t);
179 t4 = pow(t, 4);
180 dnlcdf = 0.1585747034 - 0.3898677543 * t + 0.3515963775 * t2
181 - 0.09748053605 * t2 * t - 0.04347986191 * t4
182 + 0.02182506378 * t4 * t + 0.01074751427 * pow(t, 6);
183 } else if (scaled_y > 0.1) {
184 // use Taylor expansion centred around x=0.45
185 t = scaled_y - 0.45;
186 t2 = square(t);
187 t4 = pow(t, 4);
188 dnlcdf = 0.6245634904 - 0.9521866949 * t + 0.3986215682 * t2
189 + 0.04700850676 * t2 * t - 0.03478651979 * t4
190 - 0.01772675404 * t4 * t + 0.0006577254811 * pow(t, 6);
191 } else if (scaled_y < -29.0) {
192 // asymptotic Mills ratio, DLMF 7.12.1: dnlcdf grows linearly as
193 // -2*scaled_y, so no quadratic residual fit can track it
194 const T_partials_return inv_x2 = 1.0 / x2;
195 dnlcdf = -2.0 * scaled_y
196 / (1.0 + inv_x2 * (-0.5 + inv_x2 * (0.75 + inv_x2 * -1.875)));
197 } else if (10.0 * log(fabs(scaled_y))
198 < log(std::numeric_limits<T_partials_return>::max())) {
199 // approximation derived from Abramowitz and Stegun (1964) 7.1.26
200 // use fact that erf(x)=-erf(-x)
201 // Abramowitz and Stegun define this for -inf<x<0 but seems to be
202 // accurate for -inf<x<0.1
203 t = 1.0 / (1.0 - 0.3275911 * scaled_y);
204 t2 = square(t);
205 t4 = pow(t, 4);
206 dnlcdf = 2.0 * INV_SQRT_PI
207 / (0.254829592 * t - 0.284496736 * t2 + 1.421413741 * t2 * t
208 - 1.453152027 * t4 + 1.061405429 * t4 * t);
209 // check if we need to add a correction term
210 // (from cubic fit of residuals)
211 if (scaled_y < -17.0) {
212 dnlcdf += 0.0001263257217272 * x2 * scaled_y + 0.0123586859488623 * x2
213 - 0.0860505264736028 * scaled_y - 1.252783383752970;
214 } else if (scaled_y < -7.0) {
215 dnlcdf += 0.000471585349920831 * x2 * scaled_y
216 + 0.0296839305424034 * x2 + 0.207402143352332 * scaled_y
217 + 0.425316974683324;
218 } else if (scaled_y < -3.9) {
219 dnlcdf += -0.0006972280656443 * x2 * scaled_y
220 + 0.0068218494628567 * x2 + 0.0585761964460277 * scaled_y
221 + 0.1034397670201370;
222 } else if (scaled_y < -2.1) {
223 dnlcdf += -0.0018742199480885 * x2 * scaled_y
224 - 0.0097119598291202 * x2 - 0.0170137970924080 * scaled_y
225 - 0.0100428567412041;
226 }
227 } else {
229 }
230
231 if constexpr (is_autodiff_v<T_y>) {
232 partials<0>(ops_partials)[n] += dnlcdf * INV_SQRT_TWO;
233 }
234 }
235 }
236
237 return ops_partials.build(lcdf);
238}
239
240} // namespace math
241} // namespace stan
242#endif
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
require_all_not_t< is_nonscalar_prim_or_rev_kernel_expression< std::decay_t< Types > >... > require_all_not_nonscalar_prim_or_rev_kernel_expression_t
Require none of the types satisfy is_nonscalar_prim_or_rev_kernel_expression.
return_type_t< T_y_cl > std_normal_lcdf(const T_y_cl &y)
Returns the log standard normal complementary cumulative distribution function.
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
constexpr char std_normal_lcdf_func[]
static constexpr double negative_infinity()
Return negative infinity.
static constexpr double LOG_HALF
The natural logarithm of 0.5, .
Definition constants.hpp:92
static constexpr double positive_infinity()
Return positive infinity.
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
auto pow(const T1 &x1, const T2 &x2)
Definition pow.hpp:32
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
bool is_not_nan(const T_y &y)
Return true if y is not NaN.
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:16
fvar< T > log1p(const fvar< T > &x)
Definition log1p.hpp:12
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
static constexpr double INV_SQRT_PI
The value of 1 over the square root of , .
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
fvar< T > fabs(const fvar< T > &x)
Definition fabs.hpp:16
fvar< T > square(const fvar< T > &x)
Definition square.hpp:12
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:15
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:56
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 ...