1#ifndef STAN_MATH_PRIM_PROB_VON_MISES_RNG_HPP
2#define STAN_MATH_PRIM_PROB_VON_MISES_RNG_HPP
9#include <boost/random/uniform_real_distribution.hpp>
10#include <boost/random/variate_generator.hpp>
45template <
typename T_loc,
typename T_conc,
class RNG>
47 const T_loc& mu,
const T_conc& kappa, RNG& rng) {
48 using boost::variate_generator;
49 using boost::random::uniform_real_distribution;
52 static constexpr const char* function =
"von_mises_rng";
56 T_kappa_ref kappa_ref = kappa;
67 variate_generator<RNG&, uniform_real_distribution<> >
uniform_rng(
68 rng, uniform_real_distribution<>(0.0, 1.0));
70 for (
size_t n = 0; n < N; ++n) {
73 if (kappa_vec[n] < 1.4e-8) {
79 double r = 1 + std::pow((1 + 4 * kappa_vec[n] * kappa_vec[n]), 0.5);
80 double rho = 0.5 * (r - std::pow(2 * r, 0.5)) / kappa_vec[n];
81 double s = 0.5 * (1 + rho * rho) / rho;
87 W = (1 + s * Z) / (s + Z);
88 double Y = kappa_vec[n] * (s - W);
90 done = Y * (2 - Y) - U2 > 0;
93 done = std::log(Y / U2) + 1 - Y >= 0;
98 double sign = ((U3 >= 0) - (U3 <= 0));
101 output[n] =
sign * std::acos(W)
105 return output.
data();
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_loc, T_conc >::type von_mises_rng(const T_loc &mu, const T_conc &kappa, RNG &rng)
Return a von Mises random variate for the given location and concentration using the specified random...
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
auto sign(const T &x)
Returns signs of the arguments.
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
static constexpr double TWO_PI
Twice the value of , .
static constexpr double pi()
Return the value of pi.
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
typename ref_type_if< true, T >::type ref_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...