Automatic Differentiation
 
Loading...
Searching...
No Matches
skew_normal_lcdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_SKEW_NORMAL_LCDF_HPP
2#define STAN_MATH_PRIM_PROB_SKEW_NORMAL_LCDF_HPP
3
22#include <cmath>
23
24namespace stan {
25namespace math {
26
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) {
31 using T_y_ref = ref_type_if_not_constant_t<T_y>;
32 using T_mu_ref = ref_type_if_not_constant_t<T_loc>;
33 using T_sigma_ref = ref_type_if_not_constant_t<T_scale>;
34 using T_alpha_ref = ref_type_if_not_constant_t<T_shape>;
35 static constexpr const char* function = "skew_normal_lcdf";
36 check_consistent_sizes(function, "Random variable", y, "Location parameter",
37 mu, "Scale parameter", sigma, "Shape parameter",
38 alpha);
39 T_y_ref y_ref = y;
40 T_mu_ref mu_ref = mu;
41 T_sigma_ref sigma_ref = sigma;
42 T_alpha_ref alpha_ref = alpha;
43
44 decltype(auto) y_val = to_ref(as_value_column_array_or_scalar(y_ref));
45 decltype(auto) mu_val = to_ref(as_value_column_array_or_scalar(mu_ref));
46 decltype(auto) sigma_val = to_ref(as_value_column_array_or_scalar(sigma_ref));
47 decltype(auto) alpha_val = to_ref(as_value_column_array_or_scalar(alpha_ref));
48
49 check_not_nan(function, "Random variable", y_val);
50 check_finite(function, "Location parameter", mu_val);
51 check_positive(function, "Scale parameter", sigma_val);
52 check_finite(function, "Shape parameter", alpha_val);
53
54 if (size_zero(y, mu, sigma, alpha)) {
55 return 0;
56 }
57
58 auto ops_partials
59 = make_partials_propagator(y_ref, mu_ref, sigma_ref, alpha_ref);
60
61 const auto& diff = to_ref((y_val - mu_val) / sigma_val);
62 const auto& scaled_diff
63 = to_ref_if<is_any_autodiff_v<T_y, T_loc, T_scale>>(diff / SQRT_TWO);
64 const auto& erfc_m_scaled_diff = erfc(-scaled_diff);
65 const auto& owens_t_diff_alpha = owens_t(diff, alpha_val);
66 const auto& cdf_log_
67 = to_ref_if<is_any_autodiff_v<T_y, T_loc, T_scale, T_shape>>(
68 0.5 * erfc_m_scaled_diff - 2 * owens_t_diff_alpha);
69
70 T_partials_return cdf_log = sum(log(cdf_log_));
71
72 if constexpr (is_any_autodiff_v<T_y, T_loc, T_scale, T_shape>) {
73 const auto& diff_square
75 T_y, T_loc, T_scale> && is_autodiff_v<T_shape> > (square(diff));
76 if constexpr (is_any_autodiff_v<T_y, T_loc, T_scale>) {
77 const auto& erf_alpha_scaled_diff = erf(alpha_val * scaled_diff);
78 const auto& exp_m_scaled_diff_square = exp(-0.5 * diff_square);
79 auto rep_deriv = to_ref_if<
81 T_y> + is_autodiff_v<T_loc> + is_autodiff_v<T_scale> >= 2>(
82 (erf_alpha_scaled_diff + 1) * INV_SQRT_TWO_PI
83 * exp_m_scaled_diff_square / (sigma_val * cdf_log_));
84 if constexpr (is_autodiff_v<T_loc>) {
85 partials<1>(ops_partials) = -rep_deriv;
86 }
87 if constexpr (is_autodiff_v<T_scale>) {
88 partials<2>(ops_partials) = -rep_deriv * diff;
89 }
90 if constexpr (is_autodiff_v<T_y>) {
91 partials<0>(ops_partials) = std::move(rep_deriv);
92 }
93 }
94 if constexpr (is_autodiff_v<T_shape>) {
95 const auto& alpha_square = square(alpha_val);
96 edge<3>(ops_partials).partials_
97 = -exp(-0.5 * diff_square * (1.0 + alpha_square))
98 / ((1 + alpha_square) * pi()) / cdf_log_;
99 }
100 }
101 return ops_partials.build(cdf_log);
102}
103
104} // namespace math
105} // namespace stan
106#endif
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 > log(const fvar< T > &x)
Definition log.hpp:18
fvar< T > erf(const fvar< T > &x)
Definition erf.hpp:16
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.
Definition owens_t.hpp:26
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:16
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.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
static constexpr double pi()
Return the value of pi.
Definition constants.hpp:36
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
return_type_t< T_y, T_loc, T_scale, T_shape > skew_normal_lcdf(const T_y &y, const T_loc &mu, const T_scale &sigma, const T_shape &alpha)
fvar< T > square(const fvar< T > &x)
Definition square.hpp:12
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:15
constexpr bool is_any_autodiff_v
typename ref_type_if< is_autodiff_v< T >, T >::type ref_type_if_not_constant_t
Definition ref_type.hpp:63
constexpr bool is_autodiff_v
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 ...