Automatic Differentiation
 
Loading...
Searching...
No Matches
std_normal_lccdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_STD_NORMAL_LCCDF_HPP
2#define STAN_MATH_PRIM_PROB_STD_NORMAL_LCCDF_HPP
3
16#include <cmath>
17
18namespace stan {
19namespace math {
20
21template <
22 typename T_y,
23 require_all_not_nonscalar_prim_or_rev_kernel_expression_t<T_y>* = nullptr>
25 using T_partials_return = partials_return_t<T_y>;
26 using std::exp;
27 using std::log;
28 using T_y_ref = ref_type_t<T_y>;
29 static constexpr const char* function = "std_normal_lccdf";
30 T_y_ref y_ref = y;
31 check_not_nan(function, "Random variable", y_ref);
32
33 if (size_zero(y)) {
34 return 0;
35 }
36
37 T_partials_return lccdf(0.0);
38 auto ops_partials = make_partials_propagator(y_ref);
39
40 scalar_seq_view<T_y_ref> y_vec(y_ref);
41 size_t N = stan::math::size(y);
42
43 for (size_t n = 0; n < N; n++) {
44 const T_partials_return y_dbl = y_vec.val(n);
45 const T_partials_return scaled_y = y_dbl * INV_SQRT_TWO;
46
47 T_partials_return one_m_erf;
48 if (y_dbl < -37.5) {
49 one_m_erf = 2.0;
50 } else if (y_dbl < -5.0) {
51 one_m_erf = 2.0 - erfc(-scaled_y);
52 } else if (y_dbl > 8.25) {
53 one_m_erf = 0.0;
54 } else {
55 one_m_erf = 1.0 - erf(scaled_y);
56 }
57
58 lccdf += LOG_HALF + log(one_m_erf);
59
61 const T_partials_return rep_deriv
62 = y_dbl > 8.25
63 ? INFTY
64 : SQRT_TWO_OVER_SQRT_PI * exp(-scaled_y * scaled_y) / one_m_erf;
65 partials<0>(ops_partials)[n] -= rep_deriv;
66 }
67 }
68
69 return ops_partials.build(lccdf);
70}
71
72} // namespace math
73} // namespace stan
74#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_lccdf(const T_y_cl &y)
Returns the log standard normal complementary cumulative distribution function.
size_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:18
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
static constexpr double LOG_HALF
The natural logarithm of 0.5, .
Definition constants.hpp:92
static constexpr double SQRT_TWO_OVER_SQRT_PI
The square root of 2 divided by the square root of , .
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
fvar< T > log(const fvar< T > &x)
Definition log.hpp:15
fvar< T > erf(const fvar< T > &x)
Definition erf.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
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.
static constexpr double INFTY
Positive infinity.
Definition constants.hpp:46
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:13
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 ...
Definition fvar.hpp:9
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...