gumbel_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_GUMBEL_RNG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_GUMBEL_RNG_HPP
3 
10 #include <boost/random/uniform_01.hpp>
11 #include <boost/random/variate_generator.hpp>
12 
13 namespace stan {
14 namespace math {
15 
16 /**
17  * Return a Gumbel random variate with the given location and scale
18  * using the specified random number generator.
19  *
20  * mu and beta can each be a scalar or a vector. Any non-scalar inputs
21  * must be the same length.
22  *
23  * @tparam T_loc Type of location parameter
24  * @tparam T_scale Type of scale parameter
25  * @tparam RNG type of random number generator
26  * @param mu (Sequence of) location parameter(s)
27  * @param beta (Sequence of) scale parameter(s)
28  * @param rng random number generator
29  * @return (Sequence of) Gumbel random variate(s)
30  * @throw std::domain_error if mu is infinite or beta is nonpositive.
31  * @throw std::invalid_argument if non-scalar arguments are of different
32  * sizes
33  */
34 template <typename T_loc, typename T_scale, class RNG>
36  const T_loc& mu, const T_scale& beta, RNG& rng) {
37  using boost::uniform_01;
38  using boost::variate_generator;
39  static const char* function = "gumbel_rng";
40 
41  check_finite(function, "Location parameter", mu);
42  check_positive_finite(function, "Scale parameter", beta);
43  check_consistent_sizes(function, "Location parameter", mu, "Scale Parameter",
44  beta);
45 
46  scalar_seq_view<T_loc> mu_vec(mu);
47  scalar_seq_view<T_scale> beta_vec(beta);
48  size_t N = max_size(mu, beta);
50 
51  variate_generator<RNG&, uniform_01<> > uniform01_rng(rng, uniform_01<>());
52  for (size_t n = 0; n < N; ++n)
53  output[n] = mu_vec[n] - beta_vec[n] * std::log(-std::log(uniform01_rng()));
54 
55  return output.data();
56 }
57 
58 } // namespace math
59 } // namespace stan
60 #endif
ofstream output
void check_finite(const char *function, const char *name, const T_y &y)
Double_t beta
VectorBuilder< true, double, T_loc, T_scale >::type gumbel_rng(const T_loc &mu, const T_scale &beta, RNG &rng)
Definition: gumbel_rng.hpp:35
void check_positive_finite(const char *function, const char *name, const T_y &y)
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)