1 #ifndef STAN_MATH_PRIM_PROB_SKEW_DOUBLE_EXPONENTIAL_LCDF_HPP
2 #define STAN_MATH_PRIM_PROB_SKEW_DOUBLE_EXPONENTIAL_LCDF_HPP
3
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/err.hpp>
6 #include <stan/math/prim/fun/constants.hpp>
7 #include <stan/math/prim/fun/exp.hpp>
8 #include <stan/math/prim/fun/inv.hpp>
9 #include <stan/math/prim/fun/log1m.hpp>
10 #include <stan/math/prim/fun/max_size.hpp>
11 #include <stan/math/prim/fun/size_zero.hpp>
12 #include <stan/math/prim/fun/value_of.hpp>
13 #include <stan/math/prim/functor/operands_and_partials.hpp>
14 #include <cmath>
15 #include <limits>
16
17 namespace stan {
18 namespace math {
19
20 /** \ingroup prob_dists
21 * Returns the skew double exponential log cumulative density
22 * function. Given containers of matching sizes, returns the log sum of
23 * probabilities.
24 *
25 * @tparam T_y type of real parameter.
26 * @tparam T_loc type of location parameter.
27 * @tparam T_scale type of scale parameter.
28 * @tparam T_skewness type of skewness parameter.
29 * @param y real parameter
30 * @param mu location parameter
31 * @param sigma scale parameter
32 * @param tau skewness parameter
33 * @return log probability or log sum of probabilities
34 * @throw std::domain_error if mu is infinite or sigma is nonpositive or tau is
35 * not bound between 0.0 and 1.0
36 * @throw std::invalid_argument if container sizes mismatch
37 */
38 template <typename T_y, typename T_loc, typename T_scale, typename T_skewness,
39 require_all_not_nonscalar_prim_or_rev_kernel_expression_t<
40 T_y, T_loc, T_scale, T_skewness>* = nullptr>
skew_double_exponential_lcdf(const T_y & y,const T_loc & mu,const T_scale & sigma,const T_skewness & tau)41 return_type_t<T_y, T_loc, T_scale, T_skewness> skew_double_exponential_lcdf(
42 const T_y& y, const T_loc& mu, const T_scale& sigma,
43 const T_skewness& tau) {
44 using std::exp;
45 using std::log;
46 using T_partials_return = partials_return_t<T_y, T_loc, T_scale, T_skewness>;
47 static const char* function = "skew_double_exponential_lcdf";
48 check_consistent_sizes(function, "Random variable", y, "Location parameter",
49 mu, "Shape parameter", sigma, "Skewness parameter",
50 tau);
51 auto&& y_ref = to_ref(y);
52 auto&& mu_ref = to_ref(mu);
53 auto&& sigma_ref = to_ref(sigma);
54 auto&& tau_ref = to_ref(tau);
55 using T_y_ref = std::decay_t<decltype(y_ref)>;
56 using T_mu_ref = std::decay_t<decltype(mu_ref)>;
57 using T_sigma_ref = std::decay_t<decltype(sigma_ref)>;
58 using T_tau_ref = std::decay_t<decltype(tau_ref)>;
59
60 auto&& y_val = as_value_array_or_scalar(y_ref);
61 auto&& mu_val = as_value_array_or_scalar(mu_ref);
62 auto&& sigma_val = as_value_array_or_scalar(sigma_ref);
63 auto&& tau_val = as_value_array_or_scalar(tau_ref);
64
65 check_not_nan(function, "Random variable", y_val);
66 check_finite(function, "Location parameter", mu_val);
67 check_positive_finite(function, "Scale parameter", sigma_val);
68 check_bounded(function, "Skewness parameter", tau_val, 0.0, 1.0);
69 if (size_zero(y, mu, sigma, tau)) {
70 return 0.0;
71 }
72
73 operands_and_partials<T_y_ref, T_mu_ref, T_sigma_ref, T_tau_ref> ops_partials(
74 y_ref, mu_ref, sigma_ref, tau_ref);
75
76 scalar_seq_view<std::decay_t<decltype(y_val)>> y_vec(y_val);
77 scalar_seq_view<std::decay_t<decltype(mu_val)>> mu_vec(mu_val);
78 scalar_seq_view<std::decay_t<decltype(sigma_val)>> sigma_vec(sigma_val);
79 scalar_seq_view<std::decay_t<decltype(tau_val)>> tau_vec(tau_val);
80
81 const int size_sigma = stan::math::size(sigma);
82 const auto N = max_size(y, mu, sigma, tau);
83 auto inv_sigma_val = to_ref(inv(sigma_val));
84 scalar_seq_view<decltype(inv_sigma_val)> inv_sigma(inv_sigma_val);
85
86 T_partials_return cdf_log(0.0);
87 for (int i = 0; i < N; ++i) {
88 const T_partials_return y_dbl = y_vec[i];
89 const T_partials_return mu_dbl = mu_vec[i];
90 const T_partials_return sigma_dbl = sigma_vec[i];
91 const T_partials_return tau_dbl = tau_vec[i];
92
93 const T_partials_return y_m_mu = y_dbl - mu_dbl;
94 const T_partials_return diff_sign = sign(y_m_mu);
95 const T_partials_return diff_sign_smaller_0 = step(-diff_sign);
96 const T_partials_return abs_diff_y_mu = fabs(y_m_mu);
97 const T_partials_return abs_diff_y_mu_over_sigma
98 = abs_diff_y_mu * inv_sigma[i];
99 const T_partials_return expo = (diff_sign_smaller_0 + diff_sign * tau_dbl)
100 * abs_diff_y_mu_over_sigma;
101 const T_partials_return inv_exp_2_expo_tau
102 = inv(exp(2.0 * expo) + tau_dbl - 1.0);
103
104 const T_partials_return rep_deriv
105 = y_dbl < mu_dbl ? 2.0 * inv_sigma[i] * (1.0 - tau_dbl)
106 : -2.0 * (tau_dbl - 1.0) * tau_dbl * inv_sigma[i]
107 * inv_exp_2_expo_tau;
108 const T_partials_return sig_deriv = y_dbl < mu_dbl
109 ? 2.0 * inv_sigma[i] * expo
110 : -rep_deriv * expo / tau_dbl;
111 const T_partials_return skew_deriv
112 = y_dbl < mu_dbl
113 ? 1.0 / tau_dbl + 2.0 * inv_sigma[i] * y_m_mu * diff_sign
114 : (sigma_dbl - 2.0 * (tau_dbl - 1.0) * y_m_mu) * inv_sigma[i]
115 * inv_exp_2_expo_tau;
116
117 if (y_dbl <= mu_dbl) {
118 cdf_log += log(tau_dbl) - 2.0 * expo;
119 } else {
120 cdf_log += log1m_exp(log1m(tau_dbl) - 2.0 * expo);
121 }
122
123 if (!is_constant_all<T_y>::value) {
124 ops_partials.edge1_.partials_[i] += rep_deriv;
125 }
126 if (!is_constant_all<T_loc>::value) {
127 ops_partials.edge2_.partials_[i] -= rep_deriv;
128 }
129 if (!is_constant_all<T_scale>::value) {
130 ops_partials.edge3_.partials_[i] += sig_deriv;
131 }
132 if (!is_constant_all<T_skewness>::value) {
133 ops_partials.edge4_.partials_[i] += skew_deriv;
134 }
135 }
136 return ops_partials.build(cdf_log);
137 }
138 } // namespace math
139 } // namespace stan
140 #endif
141