Automatic Differentiation
 
Loading...
Searching...
No Matches
beta_neg_binomial_rng.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_BETA_NEG_BINOMIAL_RNG_HPP
2#define STAN_MATH_PRIM_PROB_BETA_NEG_BINOMIAL_RNG_HPP
3
7
8namespace stan {
9namespace math {
10
32template <typename T_r, typename T_alpha, typename T_beta, typename RNG>
33inline auto beta_neg_binomial_rng(const T_r &r, const T_alpha &alpha,
34 const T_beta &beta, RNG &rng) {
35 using T_r_ref = ref_type_t<T_r>;
36 using T_alpha_ref = ref_type_t<T_alpha>;
37 using T_beta_ref = ref_type_t<T_beta>;
38 static constexpr const char *function = "beta_neg_binomial_rng";
39 check_consistent_sizes(function, "Number of successes parameter", r,
40 "Prior success parameter", alpha,
41 "Prior failure parameter", beta);
42
43 T_r_ref r_ref = r;
44 T_alpha_ref alpha_ref = alpha;
45 T_beta_ref beta_ref = beta;
46 check_positive_finite(function, "Number of successes parameter", r_ref);
47 check_positive_finite(function, "Prior success parameter", alpha_ref);
48 check_positive_finite(function, "Prior failure parameter", beta_ref);
49
50 using T_p = decltype(beta_rng(alpha_ref, beta_ref, rng));
51 T_p p = beta_rng(alpha_ref, beta_ref, rng);
52
53 scalar_seq_view<T_p> p_vec(p);
54 size_t size_p = stan::math::size(p);
55 VectorBuilder<true, double, T_p> odds_ratio_p(size_p);
56 for (size_t n = 0; n < size_p; ++n) {
57 odds_ratio_p[n]
58 = stan::math::exp(stan::math::log(p_vec.val(n)) - log1m(p_vec.val(n)));
59 }
60
61 return neg_binomial_rng(r_ref, odds_ratio_p.data(), rng);
62}
63
64} // namespace math
65} // namespace stan
66#endif
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, int, T_shape, T_inv >::type neg_binomial_rng(const T_shape &alpha, const T_inv &beta, RNG &rng)
Return a negative binomial random variate with the specified shape and inverse scale parameters using...
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:36
auto beta_neg_binomial_rng(const T_r &r, const T_alpha &alpha, const T_beta &beta, RNG &rng)
Return a beta-negative binomial random variate with the given number of successes,...
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Definition size.hpp:19
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > log1m(const fvar< T > &x)
Definition log1m.hpp:12
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 > exp(const fvar< T > &x)
Definition exp.hpp:15
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:55
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...