1 #ifndef STAN_MATH_PRIM_PROB_FRECHET_RNG_HPP
2 #define STAN_MATH_PRIM_PROB_FRECHET_RNG_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/err.hpp>
6 #include <stan/math/prim/fun/max_size.hpp>
7 #include <stan/math/prim/fun/scalar_seq_view.hpp>
8 #include <boost/random/weibull_distribution.hpp>
9 #include <boost/random/variate_generator.hpp>
10 
11 namespace stan {
12 namespace math {
13 /** \ingroup prob_dists
14  * Return a pseudorandom Frechet variate for the given shape
15  * and scale parameters using the specified random number generator.
16  *
17  * alpha and sigma can each be a scalar or a one-dimensional container. Any
18  * non-scalar inputs must be the same size.
19  *
20  * @tparam T_shape type of shape parameter
21  * @tparam T_scale type of scale parameter
22  * @tparam RNG type of random number generator
23  * @param alpha (Sequence of) positive shape parameter(s)
24  * @param sigma (Sequence of) positive scale parameter(s)
25  * @param rng random number generator
26  * @return (Sequence of) Frechet random variate(s)
27  * @throw std::domain_error if alpha is nonpositive or sigma is nonpositive
28  * @throw std::invalid_argument if non-scalar arguments are of different
29  * sizes
30  */
31 template <typename T_shape, typename T_scale, class RNG>
frechet_rng(const T_shape & alpha,const T_scale & sigma,RNG & rng)32 inline typename VectorBuilder<true, double, T_shape, T_scale>::type frechet_rng(
33     const T_shape& alpha, const T_scale& sigma, RNG& rng) {
34   using boost::variate_generator;
35   using boost::random::weibull_distribution;
36   using T_alpha_ref = ref_type_t<T_shape>;
37   using T_sigma_ref = ref_type_t<T_scale>;
38   static const char* function = "frechet_rng";
39   check_consistent_sizes(function, "Shape parameter", alpha, "Scale Parameter",
40                          sigma);
41   T_alpha_ref alpha_ref = alpha;
42   T_sigma_ref sigma_ref = sigma;
43   check_positive_finite(function, "Shape parameter", alpha_ref);
44   check_positive_finite(function, "Scale parameter", sigma_ref);
45 
46   scalar_seq_view<T_alpha_ref> alpha_vec(alpha_ref);
47   scalar_seq_view<T_sigma_ref> sigma_vec(sigma_ref);
48   size_t N = max_size(alpha, sigma);
49   VectorBuilder<true, double, T_shape, T_scale> output(N);
50 
51   for (size_t n = 0; n < N; ++n) {
52     variate_generator<RNG&, weibull_distribution<> > weibull_rng(
53         rng, weibull_distribution<>(alpha_vec[n], 1.0 / sigma_vec[n]));
54     output[n] = 1 / weibull_rng();
55   }
56 
57   return output.data();
58 }
59 
60 }  // namespace math
61 }  // namespace stan
62 #endif
63