Automatic Differentiation
 
Loading...
Searching...
No Matches
von_mises_rng.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_VON_MISES_RNG_HPP
2#define STAN_MATH_PRIM_PROB_VON_MISES_RNG_HPP
3
10#include <boost/random/uniform_real_distribution.hpp>
11#include <boost/random/variate_generator.hpp>
12#include <cmath>
13
14namespace stan {
15namespace math {
16
46template <typename T_loc, typename T_conc, class RNG>
48 const T_loc& mu, const T_conc& kappa, RNG& rng) {
49 using boost::variate_generator;
50 using boost::random::uniform_real_distribution;
51 using T_mu_ref = ref_type_t<T_loc>;
52 using T_kappa_ref = ref_type_t<T_conc>;
53 static constexpr const char* function = "von_mises_rng";
54 check_consistent_sizes(function, "Location parameter", mu, "Scale parameter",
55 kappa);
56 if (size_zero(mu, kappa)) {
57 return {};
58 }
59 T_mu_ref mu_ref = mu;
60 T_kappa_ref kappa_ref = kappa;
61
62 check_finite(function, "Location parameter", mu_ref);
63 check_nonnegative(function, "Scale parameter", kappa_ref);
64 check_finite(function, "Scale parameter", kappa_ref);
65
66 scalar_seq_view<T_mu_ref> mu_vec(mu_ref);
67 scalar_seq_view<T_kappa_ref> kappa_vec(kappa_ref);
68 size_t N = max_size(mu, kappa_ref);
70
71 variate_generator<RNG&, uniform_real_distribution<> > uniform_rng(
72 rng, uniform_real_distribution<>(0.0, 1.0));
73
74 for (size_t n = 0; n < N; ++n) {
75 // for kappa sufficiently close to zero, it reduces to a
76 // circular uniform distribution centered at mu
77 if (kappa_vec[n] < 1.4e-8) {
78 output[n] = (uniform_rng() - 0.5) * TWO_PI
79 + std::fmod(std::fmod(mu_vec[n], TWO_PI) + TWO_PI, TWO_PI);
80 continue;
81 }
82
83 double r = 1 + std::pow((1 + 4 * kappa_vec[n] * kappa_vec[n]), 0.5);
84 double rho = 0.5 * (r - std::pow(2 * r, 0.5)) / kappa_vec[n];
85 double s = 0.5 * (1 + rho * rho) / rho;
86
87 bool done = false;
88 double W;
89 while (!done) {
90 double Z = std::cos(pi() * uniform_rng());
91 W = (1 + s * Z) / (s + Z);
92 double Y = kappa_vec[n] * (s - W);
93 double U2 = uniform_rng();
94 done = Y * (2 - Y) - U2 > 0;
95
96 if (!done) {
97 done = std::log(Y / U2) + 1 - Y >= 0;
98 }
99 }
100
101 double U3 = uniform_rng() - 0.5;
102 double sign = ((U3 >= 0) - (U3 <= 0));
103
104 // it's really an fmod() with a positivity constraint
105 output[n] = sign * std::acos(W)
106 + std::fmod(std::fmod(mu_vec[n], TWO_PI) + TWO_PI, TWO_PI);
107 }
108
109 return output.data();
110}
111
112} // namespace math
113} // namespace stan
114#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_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.
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
auto sign(const T &x)
Returns signs of the arguments.
Definition sign.hpp:18
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 , .
Definition constants.hpp:62
static constexpr double pi()
Return the value of pi.
Definition constants.hpp:36
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
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 ...