Automatic Differentiation
 
Loading...
Searching...
No Matches
normal_lccdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_NORMAL_LCCDF_HPP
2#define STAN_MATH_PRIM_PROB_NORMAL_LCCDF_HPP
3
16#include <cmath>
17
18namespace stan {
19namespace math {
20
21template <typename T_y, typename T_loc, typename T_scale,
23 T_y, T_loc, T_scale>* = nullptr>
25 const T_loc& mu,
26 const T_scale& sigma) {
27 using T_partials_return = partials_return_t<T_y, T_loc, T_scale>;
28 using std::exp;
29 using std::log;
30 using T_y_ref = ref_type_t<T_y>;
31 using T_mu_ref = ref_type_t<T_loc>;
32 using T_sigma_ref = ref_type_t<T_scale>;
33 static constexpr const char* function = "normal_lccdf";
34 check_consistent_sizes(function, "Random variable", y, "Location parameter",
35 mu, "Scale parameter", sigma);
36 T_y_ref y_ref = y;
37 T_mu_ref mu_ref = mu;
38 T_sigma_ref sigma_ref = sigma;
39 check_not_nan(function, "Random variable", y_ref);
40 check_finite(function, "Location parameter", mu_ref);
41 check_positive(function, "Scale parameter", sigma_ref);
42
43 if (size_zero(y, mu, sigma)) {
44 return 0;
45 }
46
47 T_partials_return ccdf_log(0.0);
48 auto ops_partials = make_partials_propagator(y_ref, mu_ref, sigma_ref);
49
50 scalar_seq_view<T_y_ref> y_vec(y_ref);
51 scalar_seq_view<T_mu_ref> mu_vec(mu_ref);
52 scalar_seq_view<T_sigma_ref> sigma_vec(sigma_ref);
53 size_t N = max_size(y, mu, sigma);
54
55 for (size_t n = 0; n < N; n++) {
56 const T_partials_return y_dbl = y_vec.val(n);
57 const T_partials_return mu_dbl = mu_vec.val(n);
58 const T_partials_return sigma_dbl = sigma_vec.val(n);
59
60 const T_partials_return scaled_diff
61 = (y_dbl - mu_dbl) / (sigma_dbl * SQRT_TWO);
62
63 T_partials_return one_m_erf;
64 if (scaled_diff < -37.5 * INV_SQRT_TWO) {
65 one_m_erf = 2.0;
66 } else if (scaled_diff < -5.0 * INV_SQRT_TWO) {
67 one_m_erf = 2.0 - erfc(-scaled_diff);
68 } else if (scaled_diff > 8.25 * INV_SQRT_TWO) {
69 one_m_erf = 0.0;
70 } else {
71 one_m_erf = 1.0 - erf(scaled_diff);
72 }
73
74 ccdf_log += LOG_HALF + log(one_m_erf);
75
77 const T_partials_return rep_deriv_div_sigma
78 = scaled_diff > 8.25 * INV_SQRT_TWO
79 ? INFTY
80 : SQRT_TWO_OVER_SQRT_PI * exp(-scaled_diff * scaled_diff)
81 / one_m_erf / sigma_dbl;
83 partials<0>(ops_partials)[n] -= rep_deriv_div_sigma;
84 }
86 partials<1>(ops_partials)[n] += rep_deriv_div_sigma;
87 }
89 partials<2>(ops_partials)[n]
90 += rep_deriv_div_sigma * scaled_diff * SQRT_TWO;
91 }
92 }
93 }
94 return ops_partials.build(ccdf_log);
95}
96
97} // namespace math
98} // namespace stan
99#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, T_loc_cl, T_scale_cl > normal_lccdf(const T_y_cl &y, const T_loc_cl &mu, const T_scale_cl &sigma)
Returns the normal log complementary cumulative distribution function for the given location,...
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
size_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:19
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, .
static constexpr double SQRT_TWO
The value of the square root of 2, .
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > erfc(const fvar< T > &x)
Definition erfc.hpp:15
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
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...