Automatic Differentiation
 
Loading...
Searching...
No Matches
yule_simon_rng.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_YULE_SIMON_RNG_HPP
2#define STAN_MATH_PRIM_PROB_YULE_SIMON_RNG_HPP
3
4#include <utility>
11
12namespace stan {
13namespace math {
14
29template <typename T_alpha, typename RNG>
30inline auto yule_simon_rng(T_alpha&& alpha, RNG& rng) {
31 static constexpr const char* function = "yule_simon_rng";
32 decltype(auto) alpha_ref = to_ref(std::forward<T_alpha>(alpha));
33 check_positive_finite(function, "Shape parameter", alpha_ref);
34
35 auto w = exponential_rng(std::forward<decltype(alpha_ref)>(alpha_ref), rng);
36 auto w_arr = as_array_or_scalar(w);
37 const auto p = stan::math::exp(-w_arr);
38 const auto odds_ratio_p
40
41 if constexpr (is_stan_scalar_v<T_alpha>) {
42 return neg_binomial_rng(1.0, odds_ratio_p, rng) + 1;
43 } else {
44 return to_array_1d(
45 as_array_or_scalar(neg_binomial_rng(1.0, std::move(odds_ratio_p), rng))
46 + 1);
47 }
48}
49
50} // namespace math
51} // namespace stan
52#endif
auto to_array_1d(T_x &&x)
Returns input matrix reshaped into a vector.
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...
auto yule_simon_rng(T_alpha &&alpha, RNG &rng)
Return a yule-simon random variate with the given shape parameter, using the given random number gene...
VectorBuilder< true, double, T_inv >::type exponential_rng(const T_inv &beta, RNG &rng)
Return a exponential random variate with inverse scale beta using the specified random number generat...
T as_array_or_scalar(T &&v)
Returns specified input value.
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
fvar< T > log1m(const fvar< T > &x)
Definition log1m.hpp:12
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
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...