Stan Math Library  2.14.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  namespace math {
22 
23  template<bool propto,
24  typename T_y, typename T_loc, typename T_scale>
26  von_mises_log(T_y const& y, T_loc const& mu, T_scale const& kappa) {
27  static char const* const function = "von_mises_log";
29  T_partials_return;
30 
31  if (!(stan::length(y)
32  && stan::length(mu)
33  && stan::length(kappa)))
34  return 0.0;
35 
37 
38  using std::log;
39 
40  T_partials_return logp = 0.0;
41 
42  check_finite(function, "Random variable", y);
43  check_finite(function, "Location paramter", mu);
44  check_positive_finite(function, "Scale parameter", kappa);
45  check_consistent_sizes(function,
46  "Random variable", y,
47  "Location parameter", mu,
48  "Scale parameter", kappa);
49 
51  return logp;
52 
53  const bool y_const = is_constant_struct<T_y>::value;
54  const bool mu_const = is_constant_struct<T_loc>::value;
55  const bool kappa_const = is_constant_struct<T_scale>::value;
56 
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 * pi();
60 
61  VectorView<const T_y> y_vec(y);
62  VectorView<const T_loc> mu_vec(mu);
63  VectorView<const T_scale> kappa_vec(kappa);
64 
67  T_partials_return, T_scale> log_bessel0(length(kappa));
68  for (size_t i = 0; i < length(kappa); i++) {
69  kappa_dbl[i] = value_of(kappa_vec[i]);
71  log_bessel0[i]
72  = log(modified_bessel_first_kind(0, value_of(kappa_vec[i])));
73  }
74 
76  operands_and_partials(y, mu, kappa);
77 
78  size_t N = max_size(y, mu, kappa);
79 
80  for (size_t n = 0; n < N; n++) {
81  const T_partials_return y_ = value_of(y_vec[n]);
82  const T_partials_return y_dbl = y_ - floor(y_ / TWO_PI) * TWO_PI;
83  const T_partials_return mu_dbl = value_of(mu_vec[n]);
84 
85  T_partials_return bessel0 = 0;
86  if (compute_bessel0)
87  bessel0 = modified_bessel_first_kind(0, kappa_dbl[n]);
88  T_partials_return bessel1 = 0;
89  if (compute_bessel1)
90  bessel1 = modified_bessel_first_kind(-1, kappa_dbl[n]);
91  const T_partials_return kappa_sin = kappa_dbl[n] * sin(mu_dbl - y_dbl);
92  const T_partials_return kappa_cos = kappa_dbl[n] * cos(mu_dbl - y_dbl);
93 
95  logp -= LOG_TWO_PI;
97  logp -= log_bessel0[n];
99  logp += kappa_cos;
100 
101  if (!y_const)
102  operands_and_partials.d_x1[n] += kappa_sin;
103  if (!mu_const)
104  operands_and_partials.d_x2[n] -= kappa_sin;
105  if (!kappa_const)
106  operands_and_partials.d_x3[n] += kappa_cos / kappa_dbl[n]
107  - bessel1 / bessel0;
108  }
109  return operands_and_partials.value(logp);
110  }
111 
112  template<typename T_y, typename T_loc, typename T_scale>
114  von_mises_log(T_y const& y, T_loc const& mu, T_scale const& kappa) {
115  return von_mises_log<false>(y, mu, kappa);
116  }
117 
118  }
119 }
120 #endif
fvar< T > cos(const fvar< T > &x)
Definition: cos.hpp:13
VectorView< T_return_type, false, true > d_x2
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
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:14
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:190
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)
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
fvar< T > sin(const fvar< T > &x)
Definition: sin.hpp:12
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
VectorBuilder allocates type T1 values to be used as intermediate values.
fvar< T > floor(const fvar< T > &x)
Definition: floor.hpp:11
double pi()
Return the value of pi.
Definition: constants.hpp:85
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
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Check if the dimension of x1 is consistent with x2.
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
VectorView< T_return_type, false, true > d_x1

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