Automatic Differentiation
 
Loading...
Searching...
No Matches
std_normal_cdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_STD_NORMAL_CDF_HPP
2#define STAN_MATH_PRIM_PROB_STD_NORMAL_CDF_HPP
3
15#include <cmath>
16
17namespace stan {
18namespace math {
19
30template <
31 typename T_y,
32 require_all_not_nonscalar_prim_or_rev_kernel_expression_t<T_y>* = nullptr>
33inline return_type_t<T_y> std_normal_cdf(const T_y& y) {
34 using T_partials_return = partials_return_t<T_y>;
35 using std::exp;
36 using T_y_ref = ref_type_t<T_y>;
37 static constexpr const char* function = "std_normal_cdf";
38 T_y_ref y_ref = y;
39 check_not_nan(function, "Random variable", y_ref);
40
41 if (size_zero(y)) {
42 return 1.0;
43 }
44
45 T_partials_return cdf(1.0);
46 auto ops_partials = make_partials_propagator(y_ref);
47
48 scalar_seq_view<T_y_ref> y_vec(y_ref);
49 size_t N = stan::math::size(y);
50
51 for (size_t n = 0; n < N; n++) {
52 const T_partials_return y_dbl = y_vec.val(n);
53 const T_partials_return scaled_y = y_dbl * INV_SQRT_TWO;
54 T_partials_return cdf_n;
55 if (y_dbl < -37.5) {
56 cdf_n = 0.0;
57 } else if (y_dbl < -5.0) {
58 cdf_n = 0.5 * erfc(-scaled_y);
59 } else if (y_dbl > 8.25) {
60 cdf_n = 1;
61 } else {
62 cdf_n = 0.5 * (1.0 + erf(scaled_y));
63 }
64
65 cdf *= cdf_n;
66
68 const T_partials_return rep_deriv
69 = (y_dbl < -37.5)
70 ? 0.0
71 : INV_SQRT_TWO_PI * exp(-scaled_y * scaled_y) / cdf_n;
73 partials<0>(ops_partials)[n] += rep_deriv;
74 }
75 }
76 }
77
79 for (size_t n = 0; n < N; ++n) {
80 partials<0>(ops_partials)[n] *= cdf;
81 }
82 }
83
84 return ops_partials.build(cdf);
85}
86
87} // namespace math
88} // namespace stan
89#endif
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
return_type_t< T_y_cl > std_normal_cdf(const T_y_cl &y)
Returns the standard normal 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
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
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 , .
fvar< T > erfc(const fvar< T > &x)
Definition erfc.hpp:16
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
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:55
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 ...
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...