Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
von_mises.hpp
Go to the documentation of this file.
1 #ifndef STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__VON_MISES_HPP
2 #define STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__VON_MISES_HPP
3 
6 #include <stan/math.hpp>
7 #include <stan/meta/traits.hpp>
9 #include <stan/prob/traits.hpp>
11 
12 namespace stan {
13 
14  namespace prob {
15 
16  template<bool propto,
17  typename T_y, typename T_loc, typename T_scale>
18  typename return_type<T_y,T_loc,T_scale>::type
19  von_mises_log(T_y const& y, T_loc const& mu, T_scale const& kappa) {
20  static char const* const function = "stan::prob::von_mises_log(%1%)";
21 
22  // check if any vectors are zero length
23  if (!(stan::length(y)
24  && stan::length(mu)
25  && stan::length(kappa)))
26  return 0.0;
27 
35 
36  // Result accumulator.
37  double logp = 0.0;
38 
39  // Validate arguments.
40  check_finite(function, y, "Random variable", &logp);
41  check_finite(function, mu, "Location paramter", &logp);
42  check_positive_finite(function, kappa, "Scale parameter", &logp);
43  check_consistent_sizes(function, y, mu, kappa, "Random variable",
44  "Location parameter", "Scale parameter",
45  &logp);
46 
47  // check if no variables are involved and prop-to
49  return logp;
50 
51  // Determine constants.
52  const bool y_const = is_constant_struct<T_y>::value;
53  const bool mu_const = is_constant_struct<T_loc>::value;
54  const bool kappa_const = is_constant_struct<T_scale>::value;
55 
56  // Determine which expensive computations to perform.
57  const bool compute_bessel0 = include_summand<propto,T_scale>::value;
58  const bool compute_bessel1 = !kappa_const;
59  const double TWO_PI = 2.0 * stan::math::pi();
60 
61  // Wrap scalars into vector views.
62  VectorView<const T_y> y_vec(y);
63  VectorView<const T_loc> mu_vec(mu);
64  VectorView<const T_scale> kappa_vec(kappa);
65 
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])));
72  }
74 
75  size_t N = max_size(y, mu, kappa);
76 
77  for (size_t n = 0; n < N; n++) {
78  // Extract argument values.
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]);
82 
83  // Reusable values.
84  double bessel0 = 0;
85  if (compute_bessel0)
86  bessel0 = boost::math::cyl_bessel_i(0, kappa_dbl[n]);
87  double bessel1 = 0;
88  if (compute_bessel1)
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);
92 
93  // Log probability.
95  logp -= LOG_TWO_PI;
97  logp -= log_bessel0[n];
99  logp += kappa_cos;
100 
101  // Gradient.
102  if (!y_const)
103  oap.d_x1[n] += kappa_sin;
104  if (!mu_const)
105  oap.d_x2[n] -= kappa_sin;
106  if (!kappa_const)
107  oap.d_x3[n] += kappa_cos / kappa_dbl[n] - bessel1 / bessel0;
108  }
109 
110  return oap.to_var(logp);
111  }
112 
113  template<typename T_y, typename T_loc, typename T_scale>
115  von_mises_log(T_y const& y, T_loc const& mu, T_scale const& kappa) {
116  return von_mises_log<false>(y, mu, kappa);
117  }
118 
119  // The algorithm used in von_mises_rng is a modified version of the
120  // algorithm in:
121  //
122  // Efficient Simulation of the von Mises Distribution
123  // D. J. Best and N. I. Fisher
124  // Journal of the Royal Statistical Society. Series C (Applied Statistics), Vol. 28, No. 2 (1979), pp. 152-157
125  //
126  // See licenses/stan-license.txt for Stan license.
127 
128  template <class RNG>
129  inline double
130  von_mises_rng(const double mu,
131  const double kappa,
132  RNG& rng) {
133  using boost::variate_generator;
135 
136  static const char* function = "stan::prob::von_mises_rng(%1%)";
137 
138  stan::math::check_finite(function,mu,"mean",(double*)0);
139  stan::math::check_positive_finite(function,kappa,"inverse of variance",
140  (double*)0);
141 
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;
145 
146  bool done = 0;
147  double W;
148  while (!done) {
149  double Z = std::cos(stan::math::pi() * uniform_rng(0.0, 1.0, rng));
150  W = (1 + s * Z) / (s + Z);
151  double Y = kappa * (s - W);
152  double U2 = uniform_rng(0.0, 1.0, rng);
153  done = Y * (2 - Y) - U2 > 0;
154 
155  if(!done)
156  done = log(Y / U2) + 1 - Y >= 0;
157  }
158 
159  double U3 = uniform_rng(0.0, 1.0, rng) - 0.5;
160  double sign = ((U3 >= 0) - (U3 <= 0));
161 
162  // it's really an fmod() with a positivity constraint
163  return sign * std::acos(W) + fmod(fmod(mu,2*stan::math::pi())+2*stan::math::pi(),2*stan::math::pi());
164  }
165 
166  }
167 }
168 #endif
fvar< T > sin(const fvar< T > &x)
Definition: sin.hpp:14
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)
Definition: von_mises.hpp:130
fvar< T > acos(const fvar< T > &x)
Definition: acos.hpp:16
T_return_type to_var(double logp)
bool check_positive_finite(const char *function, const T_y &y, const char *name, T_result *result)
int sign(const T &z)
Definition: sign.hpp:9
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition: pow.hpp:17
fvar< T > floor(const fvar< T > &x)
Definition: floor.hpp:15
size_t length(const T &)
Definition: traits.hpp:159
DoubleVectorView allocates double values to be used as intermediate values.
Definition: traits.hpp:358
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.
Definition: value_of.hpp:16
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
Definition: traits.hpp:406
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
Definition: traits.hpp:57
double value_of(const T x)
Return the value of the specified scalar argument converted to a double value.
Definition: value_of.hpp:24
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: traits.hpp:35
VectorView< double *, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
double uniform_rng(const double alpha, const double beta, RNG &rng)
Definition: uniform.hpp:339
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)
Definition: traits.hpp:191
fvar< T > cos(const fvar< T > &x)
Definition: cos.hpp:15
return_type< T_y, T_loc, T_scale >::type von_mises_log(T_y const &y, T_loc const &mu, T_scale const &kappa)
Definition: von_mises.hpp:19
fvar< T > fmod(const fvar< T > &x1, const fvar< T > &x2)
Definition: fmod.hpp:16
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.
Definition: constants.hpp:77
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
Definition: traits.hpp:275

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