double_exponential_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_DOUBLE_EXPONENTIAL_RNG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_DOUBLE_EXPONENTIAL_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 double exponential random variate with the given location
19  * and scale using the specified random number generator.
20  *
21  * mu and sigma can each be a scalar or a one-dimensional container. Any
22  * 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 RNG class of random number generator
27  * @param mu (Sequence of) location parameter(s)
28  * @param sigma (Sequence of) scale parameter(s)
29  * @param rng random number generator
30  * @return (Sequence of) double exponential random variate(s)
31  * @throw std::domain_error if mu is infinite or sigma is nonpositive
32  * @throw std::invalid_argument if non-scalar arguments are of different
33  * sizes
34  */
35 template <typename T_loc, typename T_scale, class RNG>
37 double_exponential_rng(const T_loc& mu, const T_scale& sigma, RNG& rng) {
38  using boost::random::uniform_real_distribution;
39  using boost::variate_generator;
40  static const char* function = "double_exponential_rng";
41 
42  check_finite(function, "Location parameter", mu);
43  check_positive_finite(function, "Scale parameter", sigma);
44  check_consistent_sizes(function, "Location parameter", mu, "Scale Parameter",
45  sigma);
46 
47  scalar_seq_view<T_loc> mu_vec(mu);
48  scalar_seq_view<T_scale> sigma_vec(sigma);
49  size_t N = max_size(mu, sigma);
51 
52  variate_generator<RNG&, uniform_real_distribution<> > z_rng(
53  rng, uniform_real_distribution<>(-1.0, 1.0));
54  for (size_t n = 0; n < N; ++n) {
55  double z = z_rng();
56  output[n] = mu_vec[n]
57  - ((z > 0) ? 1.0 : -1.0) * sigma_vec[n] * std::log(std::abs(z));
58  }
59 
60  return output.data();
61 }
62 
63 } // namespace math
64 } // namespace stan
65 #endif
ofstream output
void check_finite(const char *function, const char *name, const T_y &y)
float abs(float number)
Definition: d0nt_math.hpp:39
void check_positive_finite(const char *function, const char *name, const T_y &y)
VectorBuilder< true, double, T_loc, T_scale >::type double_exponential_rng(const T_loc &mu, const T_scale &sigma, RNG &rng)
z
Definition: test.py:28
double sigma(TH1F *hist, double percentile)
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)