Stan Math Library  2.11.0
reverse mode automatic differentiation
beta_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BETA_RNG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_BETA_RNG_HPP
3 
4 #include <boost/math/special_functions/gamma.hpp>
5 #include <boost/random/gamma_distribution.hpp>
6 #include <boost/random/variate_generator.hpp>
22 
23 namespace stan {
24 
25  namespace math {
26 
27  template <class RNG>
28  inline double
29  beta_rng(const double alpha,
30  const double beta,
31  RNG& rng) {
32  using boost::variate_generator;
33  using boost::random::gamma_distribution;
34  // Error checks
35  static const char* function("stan::math::beta_rng");
36 
38 
39  check_positive_finite(function, "First shape parameter", alpha);
40  check_positive_finite(function, "Second shape parameter", beta);
41 
42  variate_generator<RNG&, gamma_distribution<> >
43  rng_gamma_alpha(rng, gamma_distribution<>(alpha, 1.0));
44  variate_generator<RNG&, gamma_distribution<> >
45  rng_gamma_beta(rng, gamma_distribution<>(beta, 1.0));
46  double a = rng_gamma_alpha();
47  double b = rng_gamma_beta();
48  return a / (a + b);
49  }
50 
51  }
52 }
53 #endif
double beta_rng(const double alpha, const double beta, RNG &rng)
Definition: beta_rng.hpp:29
bool check_positive_finite(const char *function, const char *name, const T_y &y)
Return true if y is positive and finite.

     [ Stan Home Page ] © 2011–2016, Stan Development Team.