Stan Math Library  2.11.0
reverse mode automatic differentiation
von_mises_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_VON_MISES_RNG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_VON_MISES_RNG_HPP
3 
13 #include <cmath>
14 
15 namespace stan {
16 
17  namespace math {
18 
19  // The algorithm used in von_mises_rng is a modified version of the
20  // algorithm in:
21  //
22  // Efficient Simulation of the von Mises Distribution
23  // D. J. Best and N. I. Fisher
24  // Journal of the Royal Statistical Society. Series C (Applied Statistics),
25  // Vol. 28, No. 2 (1979), pp. 152-157
26  //
27  // See licenses/stan-license.txt for Stan license.
28 
29  template <class RNG>
30  inline double
31  von_mises_rng(const double mu,
32  const double kappa,
33  RNG& rng) {
34  using boost::variate_generator;
36  using std::fmod;
37  using std::log;
38  using std::pow;
39 
40  static const char* function("stan::math::von_mises_rng");
41 
42  stan::math::check_finite(function, "mean", mu);
43  stan::math::check_positive_finite(function, "inverse of variance", kappa);
44 
45  double r = 1 + pow((1 + 4 * kappa * kappa), 0.5);
46  double rho = 0.5 * (r - pow(2 * r, 0.5)) / kappa;
47  double s = 0.5 * (1 + rho * rho) / rho;
48 
49  bool done = 0;
50  double W;
51  while (!done) {
52  double Z = std::cos(stan::math::pi() * uniform_rng(0.0, 1.0, rng));
53  W = (1 + s * Z) / (s + Z);
54  double Y = kappa * (s - W);
55  double U2 = uniform_rng(0.0, 1.0, rng);
56  done = Y * (2 - Y) - U2 > 0;
57 
58  if (!done)
59  done = log(Y / U2) + 1 - Y >= 0;
60  }
61 
62  double U3 = uniform_rng(0.0, 1.0, rng) - 0.5;
63  double sign = ((U3 >= 0) - (U3 <= 0));
64 
65  // it's really an fmod() with a positivity constraint
66  return sign * std::acos(W)
67  + fmod(fmod(mu, 2*stan::math::pi())+2*stan::math::pi(),
68  2*stan::math::pi());
69  }
70 
71  }
72 }
73 #endif
fvar< T > cos(const fvar< T > &x)
Definition: cos.hpp:13
int sign(const T &z)
Definition: sign.hpp:9
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
fvar< T > fmod(const fvar< T > &x1, const fvar< T > &x2)
Definition: fmod.hpp:16
double von_mises_rng(const double mu, const double kappa, RNG &rng)
double uniform_rng(const double alpha, const double beta, RNG &rng)
Definition: uniform_rng.hpp:21
fvar< T > acos(const fvar< T > &x)
Definition: acos.hpp:14
bool check_finite(const char *function, const char *name, const T_y &y)
Return true if y is finite.
double pi()
Return the value of pi.
Definition: constants.hpp:86
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition: pow.hpp:18
bool check_positive_finite(const char *function, const char *name, const T_y &y)
Return true if y is positive and finite.

     [ Stan Home Page ] © 2011–2016, Stan Development Team.