Automatic Differentiation
 
Loading...
Searching...
No Matches
cauchy_cdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_CAUCHY_CDF_HPP
2#define STAN_MATH_PRIM_PROB_CAUCHY_CDF_HPP
3
13
14namespace stan {
15namespace math {
16
32template <typename T_y, typename T_loc, typename T_scale,
34 T_y, T_loc, T_scale>* = nullptr>
35return_type_t<T_y, T_loc, T_scale> cauchy_cdf(const T_y& y, const T_loc& mu,
36 const T_scale& sigma) {
37 using T_partials_return = partials_return_t<T_y, T_loc, T_scale>;
38 using std::atan;
39 using T_y_ref = ref_type_t<T_y>;
40 using T_mu_ref = ref_type_t<T_loc>;
41 using T_sigma_ref = ref_type_t<T_scale>;
42 static constexpr const char* function = "cauchy_cdf";
43 check_consistent_sizes(function, "Random variable", y, "Location parameter",
44 mu, "Scale Parameter", sigma);
45 T_y_ref y_ref = y;
46 T_mu_ref mu_ref = mu;
47 T_sigma_ref sigma_ref = sigma;
48 check_not_nan(function, "Random variable", y_ref);
49 check_finite(function, "Location parameter", mu_ref);
50 check_positive_finite(function, "Scale parameter", sigma_ref);
51
52 if (size_zero(y, mu, sigma)) {
53 return 1.0;
54 }
55
56 T_partials_return P(1.0);
57 auto ops_partials = make_partials_propagator(y_ref, mu_ref, sigma_ref);
58
59 scalar_seq_view<T_y_ref> y_vec(y_ref);
60 scalar_seq_view<T_mu_ref> mu_vec(mu_ref);
61 scalar_seq_view<T_sigma_ref> sigma_vec(sigma_ref);
62 size_t N = max_size(y, mu, sigma);
63
64 // Explicit return for extreme values
65 // The gradients are technically ill-defined, but treated as zero
66 for (size_t i = 0; i < stan::math::size(y); i++) {
67 if (y_vec.val(i) == NEGATIVE_INFTY) {
68 return ops_partials.build(0.0);
69 }
70 }
71
72 for (size_t n = 0; n < N; n++) {
73 // Explicit results for extreme values
74 // The gradients are technically ill-defined, but treated as zero
75 if (y_vec.val(n) == INFTY) {
76 continue;
77 }
78
79 const T_partials_return y_dbl = y_vec.val(n);
80 const T_partials_return mu_dbl = mu_vec.val(n);
81 const T_partials_return sigma_inv_dbl = 1.0 / sigma_vec.val(n);
82
83 const T_partials_return z = (y_dbl - mu_dbl) * sigma_inv_dbl;
84
85 const T_partials_return Pn = atan(z) / pi() + 0.5;
86
87 P *= Pn;
88
90 partials<0>(ops_partials)[n]
91 += sigma_inv_dbl / (pi() * (1.0 + z * z) * Pn);
92 }
94 partials<1>(ops_partials)[n]
95 += -sigma_inv_dbl / (pi() * (1.0 + z * z) * Pn);
96 }
98 partials<2>(ops_partials)[n]
99 += -z * sigma_inv_dbl / (pi() * (1.0 + z * z) * Pn);
100 }
101 }
102
104 for (size_t n = 0; n < stan::math::size(y); ++n) {
105 partials<0>(ops_partials)[n] *= P;
106 }
107 }
109 for (size_t n = 0; n < stan::math::size(mu); ++n) {
110 partials<1>(ops_partials)[n] *= P;
111 }
112 }
114 for (size_t n = 0; n < stan::math::size(sigma); ++n) {
115 partials<2>(ops_partials)[n] *= P;
116 }
117 }
118 return ops_partials.build(P);
119}
120
121} // namespace math
122} // namespace stan
123#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 > cauchy_cdf(const T_y_cl &y, const T_loc_cl &mu, const T_scale_cl &sigma)
Returns the cauchy 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
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
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.
fvar< T > atan(const fvar< T > &x)
Definition atan.hpp:15
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.
static constexpr double pi()
Return the value of pi.
Definition constants.hpp:36
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
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...