1#ifndef STAN_MATH_PRIM_PROB_SKEW_NORMAL_CDF_HPP
2#define STAN_MATH_PRIM_PROB_SKEW_NORMAL_CDF_HPP
27template <
typename T_y,
typename T_loc,
typename T_scale,
typename T_shape>
29 const T_y& y,
const T_loc& mu,
const T_scale& sigma,
const T_shape& alpha) {
35 static constexpr const char* function =
"skew_normal_cdf";
37 mu,
"Scale parameter", sigma,
"Shape paramter", alpha);
40 T_sigma_ref sigma_ref = sigma;
41 T_alpha_ref alpha_ref = alpha;
60 auto diff =
to_ref((y_val - mu_val) / sigma_val);
62 = to_ref_if<is_any_autodiff_v<T_y, T_loc, T_scale>>(diff /
SQRT_TWO);
63 auto erfc_m_scaled_diff =
erfc(-scaled_diff);
64 auto owens_t_diff_alpha =
owens_t(diff, alpha_val);
65 auto cdf_ =
to_ref(0.5 * erfc_m_scaled_diff - 2 * owens_t_diff_alpha);
67 T_partials_return cdf =
prod(cdf_);
69 if constexpr (is_any_autodiff_v<T_y, T_loc, T_scale, T_shape>) {
70 auto cdf_quot = cdf / cdf_;
71 auto diff_square =
square(diff);
72 if constexpr (is_any_autodiff_v<T_y, T_loc, T_scale>) {
73 auto erfc_m_alpha_scaled_diff =
erfc(-alpha_val * scaled_diff);
74 auto exp_m_scaled_diff_square =
exp(-0.5 * diff_square);
76 / sigma_val * std::move(exp_m_scaled_diff_square);
77 if constexpr (is_autodiff_v<T_y>) {
78 partials<0>(ops_partials) = rep_deriv;
80 if constexpr (is_autodiff_v<T_loc>) {
81 partials<1>(ops_partials) = -rep_deriv;
83 if constexpr (is_autodiff_v<T_scale>) {
84 partials<2>(ops_partials) = -std::move(rep_deriv) * diff;
87 if constexpr (is_autodiff_v<T_shape>) {
88 auto alpha_square =
square(alpha_val);
89 auto exp_tmp =
exp(-0.5 * std::move(diff_square) * (1.0 + alpha_square));
90 edge<3>(ops_partials).partials_
91 = -2.0 * std::move(exp_tmp) / ((1 + std::move(alpha_square)) *
TWO_PI)
92 * std::move(cdf_quot);
95 return ops_partials.build(cdf);
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
return_type_t< T_y, T_loc, T_scale, T_shape > skew_normal_cdf(const T_y &y, const T_loc &mu, const T_scale &sigma, const T_shape &alpha)
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
value_type_t< T > prod(const T &m)
Calculates product of given kernel generator expression elements.
static constexpr double INV_SQRT_TWO_PI
The value of 1 over the square root of , .
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...
fvar< T > owens_t(const fvar< T > &x1, const fvar< T > &x2)
Return Owen's T function applied to the specified arguments.
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > erfc(const fvar< T > &x)
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.
static constexpr double TWO_PI
Twice the value of , .
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
fvar< T > square(const fvar< T > &x)
fvar< T > exp(const fvar< T > &x)
typename ref_type_if< is_autodiff_v< T >, T >::type ref_type_if_not_constant_t
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 ...