Automatic Differentiation
 
Loading...
Searching...
No Matches
beta_rng.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_BETA_RNG_HPP
2#define STAN_MATH_PRIM_PROB_BETA_RNG_HPP
3
10#include <boost/random/gamma_distribution.hpp>
11#include <boost/random/uniform_real_distribution.hpp>
12#include <boost/random/variate_generator.hpp>
13#include <cmath>
14
15namespace stan {
16namespace math {
17
36template <typename T_shape1, typename T_shape2, class RNG>
38 const T_shape1 &alpha, const T_shape2 &beta, RNG &rng) {
39 using boost::variate_generator;
40 using boost::random::gamma_distribution;
41 using boost::random::uniform_real_distribution;
42 using T_alpha_ref = ref_type_t<T_shape1>;
43 using T_beta_ref = ref_type_t<T_shape2>;
44 static constexpr const char *function = "beta_rng";
45 check_consistent_sizes(function, "First shape parameter", alpha,
46 "Second shape Parameter", beta);
47 if (size_zero(alpha, beta)) {
48 return {};
49 }
50
51 T_alpha_ref alpha_ref = alpha;
52 T_beta_ref beta_ref = beta;
53 check_positive_finite(function, "First shape parameter", alpha_ref);
54 check_positive_finite(function, "Second shape parameter", beta_ref);
55
56 scalar_seq_view<T_alpha_ref> alpha_vec(alpha_ref);
57 scalar_seq_view<T_beta_ref> beta_vec(beta_ref);
58 size_t N = max_size(alpha, beta);
60
61 variate_generator<RNG &, uniform_real_distribution<>> uniform_rng(
62 rng, uniform_real_distribution<>(0.0, 1.0));
63 for (size_t n = 0; n < N; ++n) {
64 // If alpha and beta are large, trust the usual ratio of gammas
65 // method for generating beta random variables. If any parameter
66 // is small, work in log space and use Marsaglia and Tsang's trick
67 if (alpha_vec[n] > 1.0 && beta_vec[n] > 1.0) {
68 variate_generator<RNG &, gamma_distribution<>> rng_gamma_alpha(
69 rng, gamma_distribution<>(alpha_vec[n], 1.0));
70 variate_generator<RNG &, gamma_distribution<>> rng_gamma_beta(
71 rng, gamma_distribution<>(beta_vec[n], 1.0));
72 double a = rng_gamma_alpha();
73 double b = rng_gamma_beta();
74 output[n] = a / (a + b);
75 } else {
76 variate_generator<RNG &, gamma_distribution<>> rng_gamma_alpha(
77 rng, gamma_distribution<>(alpha_vec[n] + 1, 1.0));
78 variate_generator<RNG &, gamma_distribution<>> rng_gamma_beta(
79 rng, gamma_distribution<>(beta_vec[n] + 1, 1.0));
80 double log_a = std::log(uniform_rng()) / alpha_vec[n]
81 + std::log(rng_gamma_alpha());
82 double log_b
83 = std::log(uniform_rng()) / beta_vec[n] + std::log(rng_gamma_beta());
84 double log_sum = log_sum_exp(log_a, log_b);
85 output[n] = std::exp(log_a - log_sum);
86 }
87 }
88
89 return output.data();
90}
91
92} // namespace math
93} // namespace stan
94#endif
typename helper::type type
VectorBuilder allocates type T1 values to be used as intermediate values.
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
VectorBuilder< true, double, T_alpha, T_beta >::type uniform_rng(const T_alpha &alpha, const T_beta &beta, RNG &rng)
Return a uniform random variate for the given upper and lower bounds using the specified random numbe...
VectorBuilder< true, double, T_shape1, T_shape2 >::type beta_rng(const T_shape1 &alpha, const T_shape2 &beta, RNG &rng)
Return a Beta random variate with the supplied success and failure parameters using the given random ...
Definition beta_rng.hpp:37
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
fvar< T > beta(const fvar< T > &x1, const fvar< T > &x2)
Return fvar with the beta function applied to the specified arguments and its gradient.
Definition beta.hpp:51
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
fvar< T > log_sum_exp(const fvar< T > &x1, const fvar< T > &x2)
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:56
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...