Automatic Differentiation
 
Loading...
Searching...
No Matches
exp_mod_normal_cdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_EXP_MOD_NORMAL_CDF_HPP
2#define STAN_MATH_PRIM_PROB_EXP_MOD_NORMAL_CDF_HPP
3
21#include <cmath>
22
23namespace stan {
24namespace math {
25
26template <typename T_y, typename T_loc, typename T_scale, typename T_inv_scale,
28 T_y, T_loc, T_scale, T_inv_scale>* = nullptr>
30 const T_y& y, const T_loc& mu, const T_scale& sigma,
31 const T_inv_scale& lambda) {
33 using T_y_ref = ref_type_if_not_constant_t<T_y>;
34 using T_mu_ref = ref_type_if_not_constant_t<T_loc>;
35 using T_sigma_ref = ref_type_if_not_constant_t<T_scale>;
36 using T_lambda_ref = ref_type_if_not_constant_t<T_inv_scale>;
37 static constexpr const char* function = "exp_mod_normal_cdf";
38 check_consistent_sizes(function, "Random variable", y, "Location parameter",
39 mu, "Scale parameter", sigma, "Inv_scale paramter",
40 lambda);
41 T_y_ref y_ref = y;
42 T_mu_ref mu_ref = mu;
43 T_sigma_ref sigma_ref = sigma;
44 T_lambda_ref lambda_ref = lambda;
45
46 decltype(auto) y_val = to_ref(as_value_column_array_or_scalar(y_ref));
47 decltype(auto) mu_val = to_ref(as_value_column_array_or_scalar(mu_ref));
48 decltype(auto) sigma_val = to_ref(as_value_column_array_or_scalar(sigma_ref));
49 decltype(auto) lambda_val
51
52 check_not_nan(function, "Random variable", y_val);
53 check_finite(function, "Location parameter", mu_val);
54 check_positive_finite(function, "Scale parameter", sigma_val);
55 check_positive_finite(function, "Inv_scale parameter", lambda_val);
56
57 if (size_zero(y, mu, sigma, lambda)) {
58 return 1.0;
59 }
60
61 auto ops_partials
62 = make_partials_propagator(y_ref, mu_ref, sigma_ref, lambda_ref);
63
64 if constexpr (is_vector<T_y>::value) {
65 if ((y_val == NEGATIVE_INFTY).any()) {
66 return ops_partials.build(0.0);
67 }
68 } else {
69 if (y_val == NEGATIVE_INFTY) {
70 return ops_partials.build(0.0);
71 }
72 }
73
74 const auto& inv_sigma
75 = to_ref_if<is_any_autodiff_v<T_y, T_loc, T_scale>>(inv(sigma_val));
76 const auto& diff = to_ref(y_val - mu_val);
77 const auto& v = to_ref(lambda_val * sigma_val);
78 const auto& scaled_diff = to_ref(diff * INV_SQRT_TWO * inv_sigma);
79 const auto& scaled_diff_diff
80 = to_ref_if<is_any_autodiff_v<T_y, T_loc, T_scale, T_inv_scale>>(
81 scaled_diff - v * INV_SQRT_TWO);
82 const auto& erf_calc = to_ref(0.5 * (1 + erf(scaled_diff_diff)));
83
84 const auto& exp_term
85 = to_ref_if<is_any_autodiff_v<T_y, T_loc, T_scale, T_inv_scale>>(
86 exp(0.5 * square(v) - lambda_val * diff));
87 const auto& cdf_n
88 = to_ref(0.5 + 0.5 * erf(scaled_diff) - exp_term * erf_calc);
89
90 T_partials_return cdf(1.0);
91 if constexpr (is_vector<decltype(cdf_n)>::value) {
92 cdf = cdf_n.prod();
93 } else {
94 cdf = cdf_n;
95 }
96
97 if constexpr (is_any_autodiff_v<T_y, T_loc, T_scale, T_inv_scale>) {
98 const auto& exp_term_2 = to_ref_if<(
99 is_any_autodiff_v<T_y, T_loc, T_scale> && is_autodiff_v<T_inv_scale>)>(
100 exp(-square(scaled_diff_diff)));
101 if constexpr (is_any_autodiff_v<T_y, T_loc, T_scale>) {
102 constexpr bool need_deriv_refs
103 = is_any_autodiff_v<T_y, T_loc> && is_autodiff_v<T_scale>;
104 const auto& deriv_1
105 = to_ref_if<need_deriv_refs>(lambda_val * exp_term * erf_calc);
106 const auto& deriv_2 = to_ref_if<need_deriv_refs>(
107 INV_SQRT_TWO_PI * exp_term * exp_term_2 * inv_sigma);
108 const auto& sq_scaled_diff = square(scaled_diff);
109 const auto& exp_m_sq_scaled_diff = exp(-sq_scaled_diff);
110 const auto& deriv_3 = to_ref_if<need_deriv_refs>(
111 INV_SQRT_TWO_PI * exp_m_sq_scaled_diff * inv_sigma);
112 if constexpr (is_any_autodiff_v<T_y, T_loc>) {
113 const auto& deriv
114 = to_ref_if<(is_autodiff_v<T_loc> && is_autodiff_v<T_y>)>(
115 cdf * (deriv_1 - deriv_2 + deriv_3) / cdf_n);
116 if constexpr (is_autodiff_v<T_y>) {
117 partials<0>(ops_partials) = deriv;
118 }
119 if constexpr (is_autodiff_v<T_loc>) {
120 partials<1>(ops_partials) = -deriv;
121 }
122 }
123 if constexpr (is_autodiff_v<T_scale>) {
124 edge<2>(ops_partials).partials_
125 = -cdf
126 * ((deriv_1 - deriv_2) * v
127 + (deriv_3 - deriv_2) * scaled_diff * SQRT_TWO)
128 / cdf_n;
129 }
130 }
131 if constexpr (is_autodiff_v<T_inv_scale>) {
132 edge<3>(ops_partials).partials_
133 = cdf * exp_term
134 * (INV_SQRT_TWO_PI * sigma_val * exp_term_2
135 - (v * sigma_val - diff) * erf_calc)
136 / cdf_n;
137 }
138 }
139 return ops_partials.build(cdf);
140}
141
142} // namespace math
143} // namespace stan
144#endif
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, T_inv_scale_cl > exp_mod_normal_cdf(const T_y_cl &y, const T_loc_cl &mu, const T_scale_cl &sigma, const T_inv_scale_cl &lambda)
Returns the double exponential cumulative density function.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
T to_ref_if(T &&a)
No-op that should be optimized away.
Definition to_ref.hpp:45
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 , .
static constexpr double NEGATIVE_INFTY
Negative infinity.
Definition constants.hpp:51
static constexpr double SQRT_TWO
The value of the square root of 2, .
auto as_value_column_array_or_scalar(T &&a)
Extract the value from an object and for eigen vectors and std::vectors convert to an eigen column ar...
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
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.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
fvar< T > inv(const fvar< T > &x)
Definition inv.hpp:13
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
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< is_autodiff_v< T >, T >::type ref_type_if_not_constant_t
Definition ref_type.hpp:63
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 ...
If the input type T is either an eigen matrix with 1 column or 1 row at compile time or a standard ve...