1 #ifndef STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__VON_MISES_HPP
2 #define STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__VON_MISES_HPP
17 typename T_y,
typename T_loc,
typename T_scale>
18 typename return_type<T_y,T_loc,T_scale>::type
20 static char const*
const function =
"stan::prob::von_mises_log(%1%)";
44 "Location parameter",
"Scale parameter",
58 const bool compute_bessel1 = !kappa_const;
68 for (
size_t i = 0; i <
length(kappa); i++) {
69 kappa_dbl[i] =
value_of(kappa_vec[i]);
71 log_bessel0[i] =
log(boost::math::cyl_bessel_i(0,
value_of(kappa_vec[i])));
77 for (
size_t n = 0; n < N; n++) {
79 const double y_ =
value_of(y_vec[n]);
80 const double y_dbl = y_ -
std::floor(y_ / TWO_PI) * TWO_PI;
81 const double mu_dbl =
value_of(mu_vec[n]);
86 bessel0 = boost::math::cyl_bessel_i(0, kappa_dbl[n]);
89 bessel1 = boost::math::cyl_bessel_i(-1, kappa_dbl[n]);
90 const double kappa_sin = kappa_dbl[n] *
std::sin(mu_dbl - y_dbl);
91 const double kappa_cos = kappa_dbl[n] *
std::cos(mu_dbl - y_dbl);
97 logp -= log_bessel0[n];
103 oap.
d_x1[n] += kappa_sin;
105 oap.
d_x2[n] -= kappa_sin;
107 oap.
d_x3[n] += kappa_cos / kappa_dbl[n] - bessel1 / bessel0;
113 template<
typename T_y,
typename T_loc,
typename T_scale>
116 return von_mises_log<false>(y, mu, kappa);
133 using boost::variate_generator;
136 static const char*
function =
"stan::prob::von_mises_rng(%1%)";
142 double r = 1 +
pow((1 + 4 * kappa * kappa), 0.5);
143 double rho = 0.5 * (r -
pow(2 * r, 0.5)) / kappa;
144 double s = 0.5 * (1 + rho * rho) / rho;
150 W = (1 + s * Z) / (s + Z);
151 double Y = kappa * (s - W);
153 done = Y * (2 - Y) - U2 > 0;
156 done =
log(Y / U2) + 1 - Y >= 0;
160 double sign = ((U3 >= 0) - (U3 <= 0));
fvar< T > sin(const fvar< T > &x)
bool check_greater(const char *function, const T_y &y, const T_low &low, const char *name, T_result *result)
double von_mises_rng(const double mu, const double kappa, RNG &rng)
fvar< T > acos(const fvar< T > &x)
T_return_type to_var(double logp)
bool check_positive_finite(const char *function, const T_y &y, const char *name, T_result *result)
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
fvar< T > floor(const fvar< T > &x)
DoubleVectorView allocates double values to be used as intermediate values.
bool check_finite(const char *function, const T_y &y, const char *name, T_result *result)
Checks if the variable y is finite.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
A variable implementation that stores operands and derivatives with respect to the variable...
boost::math::tools::promote_args< typename scalar_type< T1 >::type, typename scalar_type< T2 >::type, typename scalar_type< T3 >::type, typename scalar_type< T4 >::type, typename scalar_type< T5 >::type, typename scalar_type< T6 >::type >::type type
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
double value_of(const T x)
Return the value of the specified scalar argument converted to a double value.
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
VectorView< double *, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
double uniform_rng(const double alpha, const double beta, RNG &rng)
bool check_nonnegative(const char *function, const T_y &y, const char *name, T_result *result)
bool check_consistent_sizes(const char *function, const T1 &x1, const T2 &x2, const char *name1, const char *name2, T_result *result)
size_t max_size(const T1 &x1, const T2 &x2)
fvar< T > cos(const fvar< T > &x)
return_type< T_y, T_loc, T_scale >::type von_mises_log(T_y const &y, T_loc const &mu, T_scale const &kappa)
fvar< T > fmod(const fvar< T > &x1, const fvar< T > &x2)
VectorView< double *, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1
VectorView< double *, is_vector< T3 >::value, is_constant_struct< T3 >::value > d_x3
double pi()
Return the value of pi.
fvar< T > log(const fvar< T > &x)
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...