| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
4 #include <stan/math/prim/scal/meta/partials_return_type.hpp>
5 #include <stan/math/prim/scal/meta/OperandsAndPartials.hpp>
6 #include <stan/math/prim/scal/err/check_consistent_sizes.hpp>
7 #include <stan/math/prim/scal/err/check_finite.hpp>
8 #include <stan/math/prim/scal/err/check_greater.hpp>
9 #include <stan/math/prim/scal/err/check_nonnegative.hpp>
10 #include <stan/math/prim/scal/err/check_positive_finite.hpp>
11 #include <stan/math/prim/scal/meta/is_constant_struct.hpp>
12 #include <stan/math/prim/scal/fun/modified_bessel_first_kind.hpp>
13 #include <stan/math/prim/scal/fun/constants.hpp>
14 #include <stan/math/prim/scal/meta/include_summand.hpp>
15 #include <stan/math/prim/scal/meta/VectorView.hpp>
16 #include <stan/math/prim/scal/meta/VectorBuilder.hpp>
17 #include <stan/math/prim/scal/fun/value_of.hpp>
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>
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 = "von_mises_log";
28 typedef typename stan::partials_return_type<T_y, T_loc, T_scale>::type
29 T_partials_return;
30
31 if (!(stan::length(y)
32 && stan::length(mu)
33 && stan::length(kappa)))
34 return 0.0;
35
36 using stan::is_constant_struct;
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
50 if (!include_summand<propto, T_y, T_loc, T_scale>::value)
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
65 VectorBuilder<true, T_partials_return, T_scale> kappa_dbl(length(kappa));
66 VectorBuilder<include_summand<propto, T_scale>::value,
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]);
70 if (include_summand<propto, T_scale>::value)
71 log_bessel0[i]
72 = log(modified_bessel_first_kind(0, value_of(kappa_vec[i])));
73 }
74
75 OperandsAndPartials<T_y, T_loc, T_scale>
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
94 if (include_summand<propto>::value)
95 logp -= LOG_TWO_PI;
96 if (include_summand<propto, T_scale>::value)
97 logp -= log_bessel0[n];
98 if (include_summand<propto, T_y, T_loc, T_scale>::value)
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>
113 inline typename return_type<T_y, T_loc, T_scale>::type
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
stan::math::cos
fvar< T > cos(const fvar< T > &x)
Definition: cos.hpp:13
stan::math::OperandsAndPartials::d_x2
VectorView< T_return_type, false, true > d_x2
Definition: OperandsAndPartials.hpp:39
check_positive_finite.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::value_of
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:16
y_
vari ** y_
Definition: unit_vector_constrain.hpp:19
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
stan::math::OperandsAndPartials::value
T_return_type value(double value)
Returns a T_return_type with the value specified with the partial derivatves.
Definition: OperandsAndPartials.hpp:67
stan::length
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
modified_bessel_first_kind.hpp
is_constant_struct.hpp
stan::math::include_summand
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: include_summand.hpp:35
stan::return_type::type
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
include_summand.hpp
stan::math::LOG_TWO_PI
const double LOG_TWO_PI
Definition: constants.hpp:190
check_greater.hpp
check_nonnegative.hpp
stan::is_constant_struct
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
Definition: is_constant_struct.hpp:13
stan::math::modified_bessel_first_kind
fvar< T > modified_bessel_first_kind(int v, const fvar< T > &z)
Definition: modified_bessel_first_kind.hpp:13
stan::math::sin
fvar< T > sin(const fvar< T > &x)
Definition: sin.hpp:12
stan::math::von_mises_log
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_log.hpp:26
stan::math::OperandsAndPartials
This class builds partial derivatives with respect to a set of operands.
Definition: OperandsAndPartials.hpp:37
VectorView.hpp
stan::math::OperandsAndPartials::d_x3
VectorView< T_return_type, false, true > d_x3
Definition: OperandsAndPartials.hpp:40
stan::max_size
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
VectorBuilder.hpp
partials_return_type.hpp
OperandsAndPartials.hpp
stan::math::check_finite
bool check_finite(const char *function, const char *name, const T_y &y)
Return true if y is finite.
Definition: check_finite.hpp:60
stan::VectorBuilder
VectorBuilder allocates type T1 values to be used as intermediate values.
Definition: VectorBuilder.hpp:28
constants.hpp
stan::math::floor
fvar< T > floor(const fvar< T > &x)
Definition: floor.hpp:11
stan::math::check_consistent_sizes
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.
Definition: check_consistent_sizes.hpp:31
check_finite.hpp
stan::math::pi
double pi()
Return the value of pi.
Definition: constants.hpp:85
value_of.hpp
stan::VectorView
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
stan::partials_return_type::type
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
Definition: partials_return_type.hpp:26
check_consistent_sizes.hpp
stan::math::check_positive_finite
bool check_positive_finite(const char *function, const char *name, const T_y &y)
Return true if y is positive and finite.
Definition: check_positive_finite.hpp:28
stan::math::OperandsAndPartials::d_x1
VectorView< T_return_type, false, true > d_x1
Definition: OperandsAndPartials.hpp:38

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