pareto_type_2_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_PARETO_TYPE_2_RNG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_PARETO_TYPE_2_RNG_HPP
3 
11 #include <boost/random/uniform_real_distribution.hpp>
12 #include <boost/random/variate_generator.hpp>
13 
14 namespace stan {
15 namespace math {
16 
17 /**
18  * Return a Pareto type 2 random variate for the given location,
19  * scale, and shape using the specified random number generator.
20  *
21  * mu, lambda, and alpha can each be a scalar or a one-dimensional container.
22  * Any non-scalar inputs must be the same size.
23  *
24  * @tparam T_loc Type of location parameter
25  * @tparam T_scale Type of scale parameter
26  * @tparam T_shape Type of shape parameter
27  * @tparam RNG type of random number generator
28  * @param mu (Sequence of) location parameter(s)
29  * @param lambda (Sequence of) scale parameter(s)
30  * @param alpha (Sequence of) shape parameter(s)
31  * @param rng random number generator
32  * @return (Sequence of) Pareto type 2 random variate(s)
33  * @throw std::domain_error if mu is infinite or lambda or alpha are
34  * nonpositive,
35  * @throw std::invalid_argument if non-scalar arguments are of different
36  * sizes
37  */
38 template <typename T_loc, typename T_scale, typename T_shape, class RNG>
40 pareto_type_2_rng(const T_loc& mu, const T_scale& lambda, const T_shape& alpha,
41  RNG& rng) {
42  using boost::random::uniform_real_distribution;
43  using boost::variate_generator;
44  static const char* function = "pareto_type_2_rng";
45 
46  check_finite(function, "Location parameter", mu);
47  check_positive_finite(function, "Scale parameter", lambda);
48  check_positive_finite(function, "Shape parameter", alpha);
49  check_consistent_sizes(function, "Location parameter", mu, "Scale Parameter",
50  lambda, "Shape Parameter", alpha);
51 
52  scalar_seq_view<T_loc> mu_vec(mu);
53  scalar_seq_view<T_scale> lambda_vec(lambda);
54  scalar_seq_view<T_shape> alpha_vec(alpha);
55  size_t N = max_size(mu, lambda, alpha);
57 
58  variate_generator<RNG&, uniform_real_distribution<> > uniform_rng(
59  rng, uniform_real_distribution<>(0.0, 1.0));
60  for (size_t n = 0; n < N; ++n)
61  output[n] = (std::pow(1.0 - uniform_rng(), -1.0 / alpha_vec[n]) - 1.0)
62  * lambda_vec[n]
63  + mu_vec[n];
64 
65  return output.data();
66 }
67 
68 } // namespace math
69 } // namespace stan
70 #endif
ofstream output
VectorBuilder< true, double, T_loc, T_scale, T_shape >::type pareto_type_2_rng(const T_loc &mu, const T_scale &lambda, const T_shape &alpha, RNG &rng)
void check_finite(const char *function, const char *name, const T_y &y)
constexpr T pow(T x)
Definition: pow.h:75
VectorBuilder< true, double, T_alpha, T_beta >::type uniform_rng(const T_alpha &alpha, const T_beta &beta, RNG &rng)
Definition: uniform_rng.hpp:37
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)