Automatic Differentiation
 
Loading...
Searching...
No Matches
frechet_cdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_FRECHET_CDF_HPP
2#define STAN_MATH_PRIM_PROB_FRECHET_CDF_HPP
3
17#include <boost/random/weibull_distribution.hpp>
18#include <boost/random/variate_generator.hpp>
20#include <cmath>
21
22namespace stan {
23namespace math {
24
25template <typename T_y, typename T_shape, typename T_scale,
27 T_y, T_shape, T_scale>* = nullptr>
29 const T_shape& alpha,
30 const T_scale& sigma) {
31 using T_partials_return = partials_return_t<T_y, T_shape, T_scale>;
32 using T_y_ref = ref_type_t<T_y>;
33 using T_alpha_ref = ref_type_t<T_shape>;
34 using T_sigma_ref = ref_type_t<T_scale>;
35 using std::pow;
36 static constexpr const char* function = "frechet_cdf";
37 T_y_ref y_ref = y;
38 T_alpha_ref alpha_ref = alpha;
39 T_sigma_ref sigma_ref = sigma;
40
41 check_positive(function, "Random variable", y_ref);
42 check_positive_finite(function, "Shape parameter", alpha_ref);
43 check_positive_finite(function, "Scale parameter", sigma_ref);
44
45 if (size_zero(y_ref, alpha_ref, sigma_ref)) {
46 return 1.0;
47 }
48
49 T_partials_return cdf(1.0);
50 auto ops_partials = make_partials_propagator(y_ref, alpha_ref, sigma_ref);
51
52 scalar_seq_view<T_y> y_vec(y_ref);
53 scalar_seq_view<T_scale> sigma_vec(sigma_ref);
54 scalar_seq_view<T_shape> alpha_vec(alpha_ref);
55 size_t N = max_size(y_ref, sigma_ref, alpha_ref);
56
57 for (size_t n = 0; n < N; n++) {
58 const T_partials_return y_dbl = y_vec.val(n);
59 const T_partials_return sigma_dbl = sigma_vec.val(n);
60 const T_partials_return alpha_dbl = alpha_vec.val(n);
61 const T_partials_return pow_n = pow(sigma_dbl / y_dbl, alpha_dbl);
62 const T_partials_return cdf_n = exp(-pow_n);
63
64 cdf *= cdf_n;
65
67 partials<0>(ops_partials)[n] += pow_n * alpha_dbl / y_dbl;
68 }
70 partials<1>(ops_partials)[n] += pow_n * log(y_dbl / sigma_dbl);
71 }
73 partials<2>(ops_partials)[n] -= pow_n * alpha_dbl / sigma_dbl;
74 }
75 }
76
78 for (size_t n = 0; n < stan::math::size(y); ++n) {
79 partials<0>(ops_partials)[n] *= cdf;
80 }
81 }
83 for (size_t n = 0; n < stan::math::size(alpha); ++n) {
84 partials<1>(ops_partials)[n] *= cdf;
85 }
86 }
88 for (size_t n = 0; n < stan::math::size(sigma); ++n) {
89 partials<2>(ops_partials)[n] *= cdf;
90 }
91 }
92 return ops_partials.build(cdf);
93}
94
95} // namespace math
96} // namespace stan
97#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_cdf(const T_y_cl &y, const T_shape_cl &alpha, const T_scale_cl &sigma)
Returns the frechet 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
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.
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: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...