Automatic Differentiation
 
Loading...
Searching...
No Matches
pareto_cdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_PARETO_CDF_HPP
2#define STAN_MATH_PRIM_PROB_PARETO_CDF_HPP
3
15#include <cmath>
16
17namespace stan {
18namespace math {
19
20template <typename T_y, typename T_scale, typename T_shape,
22 T_y, T_scale, T_shape>* = nullptr>
24 const T_scale& y_min,
25 const T_shape& alpha) {
26 using T_partials_return = partials_return_t<T_y, T_scale, T_shape>;
27 using T_y_ref = ref_type_t<T_y>;
28 using T_y_min_ref = ref_type_t<T_scale>;
29 using T_alpha_ref = ref_type_t<T_shape>;
30 using std::exp;
31 using std::log;
32 static constexpr const char* function = "pareto_cdf";
33 check_consistent_sizes(function, "Random variable", y, "Scale parameter",
34 y_min, "Shape parameter", alpha);
35 if (size_zero(y, y_min, alpha)) {
36 return 1.0;
37 }
38
39 T_y_ref y_ref = y;
40 T_y_min_ref y_min_ref = y_min;
41 T_alpha_ref alpha_ref = alpha;
42
43 check_nonnegative(function, "Random variable", y_ref);
44 check_positive_finite(function, "Scale parameter", y_min_ref);
45 check_positive_finite(function, "Shape parameter", alpha_ref);
46
47 T_partials_return P(1.0);
48 auto ops_partials = make_partials_propagator(y_ref, y_min_ref, alpha_ref);
49
50 scalar_seq_view<T_y_ref> y_vec(y_ref);
51 scalar_seq_view<T_y_min_ref> y_min_vec(y_min_ref);
52 scalar_seq_view<T_alpha_ref> alpha_vec(alpha_ref);
53 size_t N = max_size(y, y_min, alpha);
54
55 // Explicit return for extreme values
56 // The gradients are technically ill-defined, but treated as zero
57 for (size_t i = 0; i < stan::math::size(y); i++) {
58 if (y_vec.val(i) < y_min_vec.val(i)) {
59 return ops_partials.build(0.0);
60 }
61 }
62
63 for (size_t n = 0; n < N; n++) {
64 // Explicit results for extreme values
65 // The gradients are technically ill-defined, but treated as zero
66 if (y_vec.val(n) == INFTY) {
67 continue;
68 }
69
70 const T_partials_return log_dbl = log(y_min_vec.val(n) / y_vec.val(n));
71 const T_partials_return y_min_inv_dbl = 1.0 / y_min_vec.val(n);
72 const T_partials_return alpha_dbl = alpha_vec.val(n);
73
74 const T_partials_return Pn = 1.0 - exp(alpha_dbl * log_dbl);
75
76 P *= Pn;
77
79 partials<0>(ops_partials)[n]
80 += alpha_dbl * y_min_inv_dbl * exp((alpha_dbl + 1) * log_dbl) / Pn;
81 }
83 partials<1>(ops_partials)[n]
84 += -alpha_dbl * y_min_inv_dbl * exp(alpha_dbl * log_dbl) / Pn;
85 }
87 partials<2>(ops_partials)[n] += -exp(alpha_dbl * log_dbl) * log_dbl / Pn;
88 }
89 }
90
92 for (size_t n = 0; n < stan::math::size(y); ++n) {
93 partials<0>(ops_partials)[n] *= P;
94 }
95 }
97 for (size_t n = 0; n < stan::math::size(y_min); ++n) {
98 partials<1>(ops_partials)[n] *= P;
99 }
100 }
102 for (size_t n = 0; n < stan::math::size(alpha); ++n) {
103 partials<2>(ops_partials)[n] *= P;
104 }
105 }
106 return ops_partials.build(P);
107}
108
109} // namespace math
110} // namespace stan
111#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_scale_cl, T_shape_cl > pareto_cdf(const T_y_cl &y, const T_scale_cl &y_min, const T_shape_cl &alpha)
Returns the Pareto cumulative density function.
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
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
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:15
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
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:13
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:55
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 ...
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...