Stan Math Library  2.11.0
reverse mode automatic differentiation
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 
18 #include <cmath>
19 
20 namespace stan {
21 
22  namespace math {
23 
24  template<bool propto,
25  typename T_y, typename T_loc, typename T_scale>
26  typename return_type<T_y, T_loc, T_scale>::type
27  von_mises_log(T_y const& y, T_loc const& mu, T_scale const& kappa) {
28  static char const* const function = "stan::math::von_mises_log";
30  T_partials_return;
31 
32  // check if any vectors are zero length
33  if (!(stan::length(y)
34  && stan::length(mu)
35  && stan::length(kappa)))
36  return 0.0;
37 
45 
47  using std::log;
48 
49  // Result accumulator.
50  T_partials_return logp = 0.0;
51 
52  // Validate arguments.
53  check_finite(function, "Random variable", y);
54  check_finite(function, "Location paramter", mu);
55  check_positive_finite(function, "Scale parameter", kappa);
56  check_consistent_sizes(function,
57  "Random variable", y,
58  "Location parameter", mu,
59  "Scale parameter", kappa);
60 
61 
62  // check if no variables are involved and prop-to
64  return logp;
65 
66  // Determine constants.
67  const bool y_const = is_constant_struct<T_y>::value;
68  const bool mu_const = is_constant_struct<T_loc>::value;
69  const bool kappa_const = is_constant_struct<T_scale>::value;
70 
71  // Determine which expensive computations to perform.
72  const bool compute_bessel0 = include_summand<propto, T_scale>::value;
73  const bool compute_bessel1 = !kappa_const;
74  const double TWO_PI = 2.0 * stan::math::pi();
75 
76  // Wrap scalars into vector views.
77  VectorView<const T_y> y_vec(y);
78  VectorView<const T_loc> mu_vec(mu);
79  VectorView<const T_scale> kappa_vec(kappa);
80 
83  T_partials_return, T_scale> log_bessel0(length(kappa));
84  for (size_t i = 0; i < length(kappa); i++) {
85  kappa_dbl[i] = value_of(kappa_vec[i]);
87  log_bessel0[i]
88  = log(modified_bessel_first_kind(0, value_of(kappa_vec[i])));
89  }
90 
92  operands_and_partials(y, mu, kappa);
93 
94  size_t N = max_size(y, mu, kappa);
95 
96  for (size_t n = 0; n < N; n++) {
97  // Extract argument values.
98  const T_partials_return y_ = value_of(y_vec[n]);
99  const T_partials_return y_dbl = y_ - floor(y_ / TWO_PI) * TWO_PI;
100  const T_partials_return mu_dbl = value_of(mu_vec[n]);
101 
102  // Reusable values.
103  T_partials_return bessel0 = 0;
104  if (compute_bessel0)
105  bessel0 = modified_bessel_first_kind(0, kappa_dbl[n]);
106  T_partials_return bessel1 = 0;
107  if (compute_bessel1)
108  bessel1 = modified_bessel_first_kind(-1, kappa_dbl[n]);
109  const T_partials_return kappa_sin = kappa_dbl[n] * sin(mu_dbl - y_dbl);
110  const T_partials_return kappa_cos = kappa_dbl[n] * cos(mu_dbl - y_dbl);
111 
112  // Log probability.
114  logp -= LOG_TWO_PI;
116  logp -= log_bessel0[n];
118  logp += kappa_cos;
119 
120  // Gradient.
121  if (!y_const)
122  operands_and_partials.d_x1[n] += kappa_sin;
123  if (!mu_const)
124  operands_and_partials.d_x2[n] -= kappa_sin;
125  if (!kappa_const)
126  operands_and_partials.d_x3[n] += kappa_cos / kappa_dbl[n]
127  - bessel1 / bessel0;
128  }
129 
130  return operands_and_partials.value(logp);
131  }
132 
133  template<typename T_y, typename T_loc, typename T_scale>
135  von_mises_log(T_y const& y, T_loc const& mu, T_scale const& kappa) {
136  return von_mises_log<false>(y, mu, kappa);
137  }
138  }
139 }
140 #endif
fvar< T > cos(const fvar< T > &x)
Definition: cos.hpp:13
VectorView< T_return_type, false, true > d_x2
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:16
vari ** y_
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
T_return_type value(double value)
Returns a T_return_type with the value specified with the partial derivatves.
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
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
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)
This class builds partial derivatives with respect to a set of operands.
VectorView< T_return_type, false, true > d_x3
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.
VectorBuilder allocates type T1 values to be used as intermediate values.
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.
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 expression that is constructed with a container or scalar, which it then allows to be used as an array using operator[].
Definition: VectorView.hpp:48
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.
VectorView< T_return_type, false, true > d_x1

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