All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
von_mises_rng.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_SCAL_PROB_VON_MISES_RNG_HPP
#define STAN_MATH_PRIM_SCAL_PROB_VON_MISES_RNG_HPP
#include <stan/math/prim/scal/meta/OperandsAndPartials.hpp>
#include <stan/math/prim/scal/err/check_consistent_sizes.hpp>
#include <stan/math/prim/scal/err/check_finite.hpp>
#include <stan/math/prim/scal/err/check_greater.hpp>
#include <stan/math/prim/scal/err/check_nonnegative.hpp>
#include <stan/math/prim/scal/err/check_positive_finite.hpp>
#include <stan/math/prim/scal/meta/is_constant_struct.hpp>
#include <stan/math/prim/scal/fun/modified_bessel_first_kind.hpp>
#include <stan/math/prim/scal/fun/constants.hpp>
#include <stan/math/prim/scal/prob/uniform_rng.hpp>
#include <stan/math/prim/scal/meta/include_summand.hpp>
#include <cmath>
namespace stan {
namespace math {
// The algorithm used in von_mises_rng is a modified version of the
// algorithm in:
//
// Efficient Simulation of the von Mises Distribution
// D. J. Best and N. I. Fisher
// Journal of the Royal Statistical Society. Series C (Applied Statistics),
// Vol. 28, No. 2 (1979), pp. 152-157
//
// See licenses/stan-license.txt for Stan license.
template <class RNG>
inline double
von_mises_rng(const double mu,
const double kappa,
RNG& rng) {
using boost::variate_generator;
using stan::math::uniform_rng;
using std::fmod;
using std::log;
using std::pow;
static const char* function("stan::math::von_mises_rng");
stan::math::check_finite(function, "mean", mu);
stan::math::check_positive_finite(function, "inverse of variance", kappa);
double r = 1 + pow((1 + 4 * kappa * kappa), 0.5);
double rho = 0.5 * (r - pow(2 * r, 0.5)) / kappa;
double s = 0.5 * (1 + rho * rho) / rho;
bool done = 0;
double W;
while (!done) {
double Z = std::cos(stan::math::pi() * uniform_rng(0.0, 1.0, rng));
W = (1 + s * Z) / (s + Z);
double Y = kappa * (s - W);
double U2 = uniform_rng(0.0, 1.0, rng);
done = Y * (2 - Y) - U2 > 0;
if (!done)
done = log(Y / U2) + 1 - Y >= 0;
}
double U3 = uniform_rng(0.0, 1.0, rng) - 0.5;
double sign = ((U3 >= 0) - (U3 <= 0));
// it's really an fmod() with a positivity constraint
return sign * std::acos(W)
+ fmod(fmod(mu, 2*stan::math::pi())+2*stan::math::pi(),
2*stan::math::pi());
}
}
}
#endif
stan::math::cos
fvar< T > cos(const fvar< T > &x)
Definition: cos.hpp:13
check_positive_finite.hpp
stan::math::sign
int sign(const T &z)
Definition: sign.hpp:9
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
modified_bessel_first_kind.hpp
stan::math::fmod
fvar< T > fmod(const fvar< T > &x1, const fvar< T > &x2)
Definition: fmod.hpp:16
include_summand.hpp
check_greater.hpp
uniform_rng.hpp
check_nonnegative.hpp
stan::math::von_mises_rng
double von_mises_rng(const double mu, const double kappa, RNG &rng)
Definition: von_mises_rng.hpp:33
OperandsAndPartials.hpp
stan::math::uniform_rng
double uniform_rng(const double alpha, const double beta, RNG &rng)
Definition: uniform_rng.hpp:22
stan::math::acos
fvar< T > acos(const fvar< T > &x)
Definition: acos.hpp:14
stan::math::check_finite
bool check_finite(const char *function, const char *name, const T_y &y)
Return true if y is finite.
Definition: check_finite.hpp:62
constants.hpp
check_finite.hpp
is_constant_struct.hpp
stan::math::pi
double pi()
Return the value of pi.
Definition: constants.hpp:86
stan::math::pow
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition: pow.hpp:18
check_consistent_sizes.hpp
stan::math::check_positive_finite
bool check_positive_finite(const char *function, const char *name, const T_y &y)
Return true if y is positive and finite.
Definition: check_positive_finite.hpp:28

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