logistic_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_LOGISTIC_RNG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_LOGISTIC_RNG_HPP
3 
10 #include <boost/random/exponential_distribution.hpp>
11 #include <boost/random/variate_generator.hpp>
12 
13 namespace stan {
14 namespace math {
15 
16 /**
17  * Return a Logistic random variate for the given location and scale
18  * using the specified random number generator.
19  *
20  * mu and sigma can each be a scalar or a one-dimensional container. Any
21  * non-scalar inputs must be the same size.
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 sigma (Sequence of) scale parameter(s)
28  * @param rng random number generator
29  * @return (Sequence of) Logistic random variate(s)
30  * @throw std::domain_error if mu is infinite or sigma 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& sigma, RNG& rng) {
37  using boost::random::exponential_distribution;
38  using boost::variate_generator;
39  static const char* function = "logistic_rng";
40 
41  check_finite(function, "Location parameter", mu);
42  check_positive_finite(function, "Scale parameter", sigma);
43  check_consistent_sizes(function, "Location parameter", mu, "Scale Parameter",
44  sigma);
45 
46  scalar_seq_view<T_loc> mu_vec(mu);
47  scalar_seq_view<T_scale> sigma_vec(sigma);
48  size_t N = max_size(mu, sigma);
50 
51  variate_generator<RNG&, exponential_distribution<> > exp_rng(
52  rng, exponential_distribution<>(1));
53  for (size_t n = 0; n < N; ++n)
54  output[n] = mu_vec[n] - sigma_vec[n] * std::log(exp_rng() / exp_rng());
55 
56  return output.data();
57 }
58 
59 } // namespace math
60 } // namespace stan
61 #endif
ofstream output
void check_finite(const char *function, const char *name, const T_y &y)
void check_positive_finite(const char *function, const char *name, const T_y &y)
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)
VectorBuilder< true, double, T_loc, T_scale >::type logistic_rng(const T_loc &mu, const T_scale &sigma, RNG &rng)