Stan Math Library  2.8.0
reverse mode automatic differentiation
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
von_mises_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_VON_MISES_LOG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_VON_MISES_LOG_HPP
3 
17 #include <cmath>
18 
19 namespace stan {
20 
21  namespace math {
22 
23  template<bool propto,
24  typename T_y, typename T_loc, typename T_scale>
25  typename return_type<T_y, T_loc, T_scale>::type
26  von_mises_log(T_y const& y, T_loc const& mu, T_scale const& kappa) {
27  static char const* const function = "stan::math::von_mises_log";
29  T_partials_return;
30 
31  // check if any vectors are zero length
32  if (!(stan::length(y)
33  && stan::length(mu)
34  && stan::length(kappa)))
35  return 0.0;
36 
44 
46  using std::log;
47 
48  // Result accumulator.
49  T_partials_return logp = 0.0;
50 
51  // Validate arguments.
52  check_finite(function, "Random variable", y);
53  check_finite(function, "Location paramter", mu);
54  check_positive_finite(function, "Scale parameter", kappa);
55  check_consistent_sizes(function,
56  "Random variable", y,
57  "Location parameter", mu,
58  "Scale parameter", kappa);
59 
60 
61  // check if no variables are involved and prop-to
63  return logp;
64 
65  // Determine constants.
66  const bool y_const = is_constant_struct<T_y>::value;
67  const bool mu_const = is_constant_struct<T_loc>::value;
68  const bool kappa_const = is_constant_struct<T_scale>::value;
69 
70  // Determine which expensive computations to perform.
71  const bool compute_bessel0 = include_summand<propto, T_scale>::value;
72  const bool compute_bessel1 = !kappa_const;
73  const double TWO_PI = 2.0 * stan::math::pi();
74 
75  // Wrap scalars into vector views.
76  VectorView<const T_y> y_vec(y);
77  VectorView<const T_loc> mu_vec(mu);
78  VectorView<const T_scale> kappa_vec(kappa);
79 
82  T_partials_return, T_scale> log_bessel0(length(kappa));
83  for (size_t i = 0; i < length(kappa); i++) {
84  kappa_dbl[i] = value_of(kappa_vec[i]);
86  log_bessel0[i]
87  = log(modified_bessel_first_kind(0, value_of(kappa_vec[i])));
88  }
89 
91 
92  size_t N = max_size(y, mu, kappa);
93 
94  for (size_t n = 0; n < N; n++) {
95  // Extract argument values.
96  const T_partials_return y_ = value_of(y_vec[n]);
97  const T_partials_return y_dbl = y_ - floor(y_ / TWO_PI) * TWO_PI;
98  const T_partials_return mu_dbl = value_of(mu_vec[n]);
99 
100  // Reusable values.
101  T_partials_return bessel0 = 0;
102  if (compute_bessel0)
103  bessel0 = modified_bessel_first_kind(0, kappa_dbl[n]);
104  T_partials_return bessel1 = 0;
105  if (compute_bessel1)
106  bessel1 = modified_bessel_first_kind(-1, kappa_dbl[n]);
107  const T_partials_return kappa_sin = kappa_dbl[n] * sin(mu_dbl - y_dbl);
108  const T_partials_return kappa_cos = kappa_dbl[n] * cos(mu_dbl - y_dbl);
109 
110  // Log probability.
112  logp -= LOG_TWO_PI;
114  logp -= log_bessel0[n];
116  logp += kappa_cos;
117 
118  // Gradient.
119  if (!y_const)
120  oap.d_x1[n] += kappa_sin;
121  if (!mu_const)
122  oap.d_x2[n] -= kappa_sin;
123  if (!kappa_const)
124  oap.d_x3[n] += kappa_cos / kappa_dbl[n] - bessel1 / bessel0;
125  }
126 
127  return oap.to_var(logp, y, mu, kappa);
128  }
129 
130  template<typename T_y, typename T_loc, typename T_scale>
132  von_mises_log(T_y const& y, T_loc const& mu, T_scale const& kappa) {
133  return von_mises_log<false>(y, mu, kappa);
134  }
135  }
136 }
137 #endif
fvar< T > cos(const fvar< T > &x)
Definition: cos.hpp:13
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:16
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
T_return_type to_var(T_partials_return logp, const T1 &x1=0, const T2 &x2=0, const T3 &x3=0, const T4 &x4=0, const T5 &x5=0, const T6 &x6=0)
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
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: return_type.hpp:27
const double LOG_TWO_PI
Definition: constants.hpp:193
VectorView< T_partials_return, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
fvar< T > modified_bessel_first_kind(int v, const fvar< T > &z)
fvar< T > sin(const fvar< T > &x)
Definition: sin.hpp:14
return_type< T_y, T_loc, T_scale >::type von_mises_log(T_y const &y, T_loc const &mu, T_scale const &kappa)
VectorView< T_partials_return, is_vector< T3 >::value, is_constant_struct< T3 >::value > d_x3
A variable implementation that stores operands and derivatives with respect to the variable...
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
bool check_finite(const char *function, const char *name, const T_y &y)
Return true if y is finite.
fvar< T > floor(const fvar< T > &x)
Definition: floor.hpp:11
bool check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Return true if the dimension of x1 is consistent with x2.
VectorView< T_partials_return, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
double pi()
Return the value of pi.
Definition: constants.hpp:86
bool check_nonnegative(const char *function, const char *name, const T_y &y)
Return true if y is non-negative.
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
Definition: VectorView.hpp:41
boost::math::tools::promote_args< typename partials_type< typename scalar_type< T1 >::type >::type, typename partials_type< typename scalar_type< T2 >::type >::type, typename partials_type< typename scalar_type< T3 >::type >::type, typename partials_type< typename scalar_type< T4 >::type >::type, typename partials_type< typename scalar_type< T5 >::type >::type, typename partials_type< typename scalar_type< T6 >::type >::type >::type type
bool check_positive_finite(const char *function, const char *name, const T_y &y)
Return true if y is positive and finite.
bool check_greater(const char *function, const char *name, const T_y &y, const T_low &low)
Return true if y is strictly greater than low.

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