Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 26 additions & 89 deletions stan/math/prim/prob/skew_normal_lccdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,101 +2,38 @@
#define STAN_MATH_PRIM_PROB_SKEW_NORMAL_LCCDF_HPP

#include <stan/math/prim/meta.hpp>
#include <stan/math/prim/err.hpp>
#include <stan/math/prim/fun/as_column_vector_or_scalar.hpp>
#include <stan/math/prim/fun/as_array_or_scalar.hpp>
#include <stan/math/prim/fun/as_value_column_array_or_scalar.hpp>
#include <stan/math/prim/fun/constants.hpp>
#include <stan/math/prim/fun/erf.hpp>
#include <stan/math/prim/fun/erfc.hpp>
#include <stan/math/prim/fun/exp.hpp>
#include <stan/math/prim/fun/log.hpp>
#include <stan/math/prim/fun/max_size.hpp>
#include <stan/math/prim/fun/owens_t.hpp>
#include <stan/math/prim/fun/size_zero.hpp>
#include <stan/math/prim/fun/to_ref.hpp>
#include <stan/math/prim/fun/value_of.hpp>
#include <stan/math/prim/functor/partials_propagator.hpp>
#include <cmath>
#include <stan/math/prim/fun/minus.hpp>
#include <stan/math/prim/prob/skew_normal_lcdf.hpp>

namespace stan {
namespace math {

namespace internal {
constexpr char skew_normal_lccdf_func[] = "skew_normal_lccdf";
} // namespace internal

/** \ingroup prob_dists
* Returns the log of the skew normal complementary cumulative distribution
* function. If X ~ SkewNormal(mu, sigma, alpha), then
* -X ~ SkewNormal(-mu, sigma, -alpha), so the ccdf at y is the cdf of
* SkewNormal(-mu, sigma, -alpha) at -y. The upper tail is then computed
* without the subtraction 1 - cdf.
*
* @tparam T_y type of the random variable
* @tparam T_loc type of the location parameter
* @tparam T_scale type of the scale parameter
* @tparam T_shape type of the shape parameter
* @param y random variable
* @param mu location parameter
* @param sigma scale parameter
* @param alpha shape parameter
* @return The log of the skew normal ccdf evaluated at the specified
* arguments. If given containers, the log of the product of the ccdfs.
*/
template <typename T_y, typename T_loc, typename T_scale, typename T_shape>
inline return_type_t<T_y, T_loc, T_scale, T_shape> skew_normal_lccdf(
const T_y& y, const T_loc& mu, const T_scale& sigma, const T_shape& alpha) {
using T_partials_return = partials_return_t<T_y, T_loc, T_scale, T_shape>;
using T_y_ref = ref_type_if_not_constant_t<T_y>;
using T_mu_ref = ref_type_if_not_constant_t<T_loc>;
using T_sigma_ref = ref_type_if_not_constant_t<T_scale>;
using T_alpha_ref = ref_type_if_not_constant_t<T_shape>;
static constexpr const char* function = "skew_normal_lccdf";
check_consistent_sizes(function, "Random variable", y, "Location parameter",
mu, "Scale parameter", sigma, "Shape parameter",
alpha);
T_y_ref y_ref = y;
T_mu_ref mu_ref = mu;
T_sigma_ref sigma_ref = sigma;
T_alpha_ref alpha_ref = alpha;

decltype(auto) y_val = to_ref(as_value_column_array_or_scalar(y_ref));
decltype(auto) mu_val = to_ref(as_value_column_array_or_scalar(mu_ref));
decltype(auto) sigma_val = to_ref(as_value_column_array_or_scalar(sigma_ref));
decltype(auto) alpha_val = to_ref(as_value_column_array_or_scalar(alpha_ref));

check_not_nan(function, "Random variable", y_val);
check_finite(function, "Location parameter", mu_val);
check_positive(function, "Scale parameter", sigma_val);
check_finite(function, "Shape parameter", alpha_val);

if (size_zero(y, mu, sigma, alpha)) {
return 0;
}

auto ops_partials
= make_partials_propagator(y_ref, mu_ref, sigma_ref, alpha_ref);

const auto& diff = to_ref((y_val - mu_val) / sigma_val);
const auto& scaled_diff
= to_ref_if<is_any_autodiff_v<T_y, T_scale, T_loc>>(diff / SQRT_TWO);
const auto& erfc_m_scaled_diff = erfc(-scaled_diff);
const auto& owens_t_diff_alpha = owens_t(diff, alpha_val);
const auto& ccdf_log_ = to_ref_if<is_autodiff_v<T_shape>>(
1.0 - 0.5 * erfc_m_scaled_diff + 2 * owens_t_diff_alpha);

T_partials_return ccdf_log = sum(log(ccdf_log_));

if constexpr (is_any_autodiff_v<T_y, T_scale, T_loc, T_shape>) {
const auto& diff_square = to_ref_if<(
is_any_autodiff_v<T_y, T_scale, T_loc> && is_autodiff_v<T_shape>)>(
square(diff));
if constexpr (is_any_autodiff_v<T_y, T_scale, T_loc>) {
const auto& erf_alpha_scaled_diff = erf(alpha_val * scaled_diff);
const auto& exp_m_scaled_diff_square = exp(-0.5 * diff_square);
auto rep_deriv = to_ref_if<
is_autodiff_v<
T_y> + is_autodiff_v<T_scale> + is_autodiff_v<T_loc> >= 2>(
(erf_alpha_scaled_diff + 1) * INV_SQRT_TWO_PI
/ (ccdf_log_ * sigma_val) * exp_m_scaled_diff_square);
if constexpr (is_autodiff_v<T_y>) {
partials<0>(ops_partials) = -rep_deriv;
}
if constexpr (is_autodiff_v<T_scale>) {
partials<2>(ops_partials) = rep_deriv * diff;
}
if constexpr (is_autodiff_v<T_loc>) {
partials<1>(ops_partials) = std::move(rep_deriv);
}
}
if constexpr (is_autodiff_v<T_shape>) {
const auto& alpha_square = square(alpha_val);
edge<3>(ops_partials).partials_
= 2.0 * exp(-0.5 * diff_square * (1.0 + alpha_square))
/ ((1 + alpha_square) * TWO_PI * ccdf_log_);
}
}

return ops_partials.build(ccdf_log);
return internal::skew_normal_lcdf_impl<internal::skew_normal_lccdf_func>(
minus(y), minus(mu), sigma, minus(alpha));
}

} // namespace math
Expand Down
Loading
Loading