Automatic Differentiation
 
Loading...
Searching...
No Matches
logistic_lccdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_LOGISTIC_LCCDF_HPP
2#define STAN_MATH_PRIM_PROB_LOGISTIC_LCCDF_HPP
3
17#include <cmath>
18
19namespace stan {
20namespace math {
21
22template <typename T_y, typename T_loc, typename T_scale,
24 T_y, T_loc, T_scale>* = nullptr>
26 const T_loc& mu,
27 const T_scale& sigma) {
28 using T_partials_return = partials_return_t<T_y, T_loc, T_scale>;
29 using std::exp;
30 using std::log;
31 using T_y_ref = ref_type_t<T_y>;
32 using T_mu_ref = ref_type_t<T_loc>;
33 using T_sigma_ref = ref_type_t<T_scale>;
34 static constexpr const char* function = "logistic_lccdf";
35 check_consistent_sizes(function, "Random variable", y, "Location parameter",
36 mu, "Scale parameter", sigma);
37 T_y_ref y_ref = y;
38 T_mu_ref mu_ref = mu;
39 T_sigma_ref sigma_ref = sigma;
40 check_not_nan(function, "Random variable", y_ref);
41 check_finite(function, "Location parameter", mu_ref);
42 check_positive_finite(function, "Scale parameter", sigma_ref);
43
44 if (size_zero(y, mu, sigma)) {
45 return 0;
46 }
47
48 T_partials_return P(0.0);
49 auto ops_partials = make_partials_propagator(y_ref, mu_ref, sigma_ref);
50
51 scalar_seq_view<T_y_ref> y_vec(y_ref);
52 scalar_seq_view<T_mu_ref> mu_vec(mu_ref);
53 scalar_seq_view<T_sigma_ref> sigma_vec(sigma_ref);
54 size_t N = max_size(y, mu, sigma);
55
56 // Explicit return for extreme values
57 // The gradients are technically ill-defined, but treated as zero
58 for (size_t i = 0; i < stan::math::size(y); i++) {
59 if (y_vec.val(i) == NEGATIVE_INFTY) {
60 return ops_partials.build(0.0);
61 }
62 }
63
64 for (size_t n = 0; n < N; n++) {
65 // Explicit results for extreme values
66 // The gradients are technically ill-defined, but treated as zero
67 if (y_vec.val(n) == INFTY) {
68 return ops_partials.build(negative_infinity());
69 }
70
71 const T_partials_return y_dbl = y_vec.val(n);
72 const T_partials_return mu_dbl = mu_vec.val(n);
73 const T_partials_return sigma_dbl = sigma_vec.val(n);
74 const T_partials_return sigma_inv_vec = 1.0 / sigma_vec.val(n);
75
76 // TODO(Andrew) Further simplify derivatives and log-scale below
77 const T_partials_return Pn
78 = 1.0 - inv_logit((y_dbl - mu_dbl) * sigma_inv_vec);
79 P += log(Pn);
80
81 if constexpr (is_autodiff_v<T_y>) {
82 partials<0>(ops_partials)[n]
83 -= exp(logistic_lpdf(y_dbl, mu_dbl, sigma_dbl)) / Pn;
84 }
85 if constexpr (is_autodiff_v<T_loc>) {
86 partials<1>(ops_partials)[n]
87 -= -exp(logistic_lpdf(y_dbl, mu_dbl, sigma_dbl)) / Pn;
88 }
89 if constexpr (is_autodiff_v<T_scale>) {
90 partials<2>(ops_partials)[n]
91 -= -(y_dbl - mu_dbl) * sigma_inv_vec
92 * exp(logistic_lpdf(y_dbl, mu_dbl, sigma_dbl)) / Pn;
93 }
94 }
95 return ops_partials.build(P);
96}
97
98} // namespace math
99} // namespace stan
100#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 > logistic_lpdf(const T_y_cl &y, const T_loc_cl &mu, const T_scale_cl &sigma)
The log of a logistic density for y with the specified location and scale parameters.
return_type_t< T_y_cl, T_loc_cl, T_scale_cl > logistic_lccdf(const T_y_cl &y, const T_loc_cl &mu, const T_scale_cl &sigma)
Returns the logistic cumulative distribution function for the given location, and scale.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
int64_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:19
static constexpr double negative_infinity()
Return negative infinity.
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
auto inv_logit(T &&x)
Returns the inverse logit function applied to the argument.
Definition inv_logit.hpp:20
static constexpr double NEGATIVE_INFTY
Negative infinity.
Definition constants.hpp:51
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
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.
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
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.
static constexpr double INFTY
Positive infinity.
Definition constants.hpp:46
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:15
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:56
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 ...