Automatic Differentiation
 
Loading...
Searching...
No Matches
frechet_lcdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_FRECHET_LCDF_HPP
2#define STAN_MATH_PRIM_PROB_FRECHET_LCDF_HPP
3
11#include <cmath>
12
13namespace stan {
14namespace math {
15
16template <typename T_y, typename T_shape, typename T_scale,
18 T_y, T_shape, T_scale>* = nullptr>
20 const T_shape& alpha,
21 const T_scale& sigma) {
22 using T_partials_return = partials_return_t<T_y, T_shape, T_scale>;
23 using T_y_ref = ref_type_t<T_y>;
24 using T_alpha_ref = ref_type_t<T_shape>;
25 using T_sigma_ref = ref_type_t<T_scale>;
26 static constexpr const char* function = "frechet_lcdf";
27 T_y_ref y_ref = y;
28 T_alpha_ref alpha_ref = alpha;
29 T_sigma_ref sigma_ref = sigma;
30 using std::exp;
31 using std::pow;
32
33 check_positive(function, "Random variable", y_ref);
34 check_positive_finite(function, "Shape parameter", alpha_ref);
35 check_positive_finite(function, "Scale parameter", sigma_ref);
36
37 if (size_zero(y_ref, alpha_ref, sigma_ref)) {
38 return 0.0;
39 }
40
41 T_partials_return cdf_log(0.0);
42 auto ops_partials = make_partials_propagator(y_ref, alpha_ref, sigma_ref);
43
44 scalar_seq_view<T_y> y_vec(y_ref);
45 scalar_seq_view<T_scale> sigma_vec(sigma_ref);
46 scalar_seq_view<T_shape> alpha_vec(alpha_ref);
47 size_t N = max_size(y_ref, sigma_ref, alpha_ref);
48 for (size_t n = 0; n < N; n++) {
49 const T_partials_return y_dbl = y_vec.val(n);
50 const T_partials_return sigma_dbl = sigma_vec.val(n);
51 const T_partials_return alpha_dbl = alpha_vec.val(n);
52 const T_partials_return pow_n = pow(sigma_dbl / y_dbl, alpha_dbl);
53
54 cdf_log -= pow_n;
55
56 if constexpr (is_autodiff_v<T_y>) {
57 partials<0>(ops_partials)[n] += pow_n * alpha_dbl / y_dbl;
58 }
59 if constexpr (is_autodiff_v<T_shape>) {
60 partials<1>(ops_partials)[n] += pow_n * log(y_dbl / sigma_dbl);
61 }
62 if constexpr (is_autodiff_v<T_scale>) {
63 partials<2>(ops_partials)[n] -= pow_n * alpha_dbl / sigma_dbl;
64 }
65 }
66 return ops_partials.build(cdf_log);
67}
68
69} // namespace math
70} // namespace stan
71#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_shape_cl, T_scale_cl > frechet_lcdf(const T_y_cl &y, const T_shape_cl &alpha, const T_scale_cl &sigma)
Returns the frechet log 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.
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
auto pow(const T1 &x1, const T2 &x2)
Definition pow.hpp:32
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
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.
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 ...