Automatic Differentiation
 
Loading...
Searching...
No Matches
skew_double_exponential_cdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_SKEW_DOUBLE_EXPONENTIAL_CDF_HPP
2#define STAN_MATH_PRIM_PROB_SKEW_DOUBLE_EXPONENTIAL_CDF_HPP
3
14#include <cmath>
15#include <limits>
16
17namespace stan {
18namespace math {
19
38template <typename T_y, typename T_loc, typename T_scale, typename T_skewness,
40 T_y, T_loc, T_scale, T_skewness>* = nullptr>
42 const T_y& y, const T_loc& mu, const T_scale& sigma,
43 const T_skewness& tau) {
45 static constexpr const char* function = "skew_double_exponential_lcdf";
46 check_consistent_sizes(function, "Random variable", y, "Location parameter",
47 mu, "Shape parameter", sigma, "Skewness parameter",
48 tau);
49 auto&& y_ref = to_ref(y);
50 auto&& mu_ref = to_ref(mu);
51 auto&& sigma_ref = to_ref(sigma);
52 auto&& tau_ref = to_ref(tau);
53
54 auto&& y_val = as_value_array_or_scalar(y_ref);
55 auto&& mu_val = as_value_array_or_scalar(mu_ref);
56 auto&& sigma_val = as_value_array_or_scalar(sigma_ref);
57 auto&& tau_val = as_value_array_or_scalar(tau_ref);
58
59 check_not_nan(function, "Random variable", y_val);
60 check_finite(function, "Location parameter", mu_val);
61 check_positive_finite(function, "Scale parameter", sigma_val);
62 check_bounded(function, "Skewness parameter", tau_val, 0.0, 1.0);
63 if (size_zero(y, mu, sigma, tau)) {
64 return 1.0;
65 }
66
67 T_partials_return cdf(1.0);
68 auto ops_partials
69 = make_partials_propagator(y_ref, mu_ref, sigma_ref, tau_ref);
70
71 scalar_seq_view<std::decay_t<decltype(y_val)>> y_vec(y_val);
72 scalar_seq_view<std::decay_t<decltype(mu_val)>> mu_vec(mu_val);
73 scalar_seq_view<std::decay_t<decltype(sigma_val)>> sigma_vec(sigma_val);
74 scalar_seq_view<std::decay_t<decltype(tau_val)>> tau_vec(tau_val);
75
76 const auto N = max_size(y, mu, sigma, tau);
77 auto inv_sigma_val = to_ref(inv(sigma_val));
78 scalar_seq_view<decltype(inv_sigma_val)> inv_sigma(inv_sigma_val);
79
80 for (int i = 0; i < N; ++i) {
81 const T_partials_return y_dbl = y_vec[i];
82 const T_partials_return mu_dbl = mu_vec[i];
83 const T_partials_return sigma_dbl = sigma_vec[i];
84 const T_partials_return tau_dbl = tau_vec[i];
85
86 const T_partials_return y_m_mu = y_dbl - mu_dbl;
87 const T_partials_return diff_sign = sign(y_m_mu);
88 const T_partials_return diff_sign_smaller_0 = step(-diff_sign);
89 const T_partials_return abs_diff_y_mu = fabs(y_m_mu);
90 const T_partials_return abs_diff_y_mu_over_sigma
91 = abs_diff_y_mu * inv_sigma[i];
92 const T_partials_return expo = (diff_sign_smaller_0 + diff_sign * tau_dbl)
93 * abs_diff_y_mu_over_sigma;
94 const T_partials_return inv_exp_2_expo_tau
95 = inv(exp(2.0 * expo) + tau_dbl - 1.0);
96
97 const T_partials_return rep_deriv
98 = y_dbl < mu_dbl ? 2.0 * inv_sigma[i] * (1.0 - tau_dbl)
99 : -2.0 * (tau_dbl - 1.0) * tau_dbl * inv_sigma[i]
100 * inv_exp_2_expo_tau;
101 const T_partials_return sig_deriv = y_dbl < mu_dbl
102 ? 2.0 * inv_sigma[i] * expo
103 : -rep_deriv * expo / tau_dbl;
104 const T_partials_return skew_deriv
105 = y_dbl < mu_dbl
106 ? 1.0 / tau_dbl + 2.0 * inv_sigma[i] * y_m_mu * diff_sign
107 : (sigma_dbl - 2.0 * (tau_dbl - 1.0) * y_m_mu) * inv_sigma[i]
108 * inv_exp_2_expo_tau;
109
110 T_partials_return cdfn(1.0);
111 if (y_dbl <= mu_dbl) {
112 cdfn *= tau_dbl * exp(-2.0 * expo);
113 } else {
114 cdfn *= 1.0 - (1.0 - tau_dbl) * exp(-2.0 * expo);
115 }
116 cdf *= cdfn;
117
119 partials<0>(ops_partials)[i] += rep_deriv;
120 }
122 partials<1>(ops_partials)[i] -= rep_deriv;
123 }
125 partials<2>(ops_partials)[i] += sig_deriv;
126 }
128 partials<3>(ops_partials)[i] += skew_deriv;
129 }
130 }
131
133 for (size_t n = 0; n < stan::math::size(y); ++n) {
134 partials<0>(ops_partials)[n] *= cdf;
135 }
136 }
138 for (size_t n = 0; n < stan::math::size(mu); ++n) {
139 partials<1>(ops_partials)[n] *= cdf;
140 }
141 }
143 for (size_t n = 0; n < stan::math::size(sigma); ++n) {
144 partials<2>(ops_partials)[n] *= cdf;
145 }
146 }
148 for (size_t n = 0; n < stan::math::size(tau); ++n) {
149 partials<3>(ops_partials)[n] *= cdf;
150 }
151 }
152
153 return ops_partials.build(cdf);
154}
155} // namespace math
156} // namespace stan
157#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, T_skewness_cl > skew_double_exponential_cdf(const T_y_cl &y, const T_loc_cl &mu, const T_scale_cl &sigma, const T_skewness_cl &tau)
Returns the skew double exponential cumulative density function.
size_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Definition size.hpp:18
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
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
void check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Check if the value is between the low and high values, inclusively.
auto sign(const T &x)
Returns signs of the arguments.
Definition sign.hpp:18
T step(const T &y)
The step, or Heaviside, function.
Definition step.hpp:31
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
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 as_value_array_or_scalar(T &&v)
Extract the value from an object.
fvar< T > inv(const fvar< T > &x)
Definition inv.hpp:12
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 > fabs(const fvar< T > &x)
Definition fabs.hpp:15
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:13
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...