| Stan Math Library 2.11.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
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";
29 typedef typename stan::partials_return_type<T_y, T_loc, T_scale>::type
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
38 using stan::is_constant_struct;
39 using stan::math::check_finite;
40 using stan::math::check_positive_finite;
41 using stan::math::check_greater;
42 using stan::math::check_nonnegative;
43 using stan::math::check_consistent_sizes;
44 using stan::math::value_of;
45
46 using stan::math::modified_bessel_first_kind;
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
63 if (!include_summand<propto, T_y, T_loc, T_scale>::value)
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
81 VectorBuilder<true, T_partials_return, T_scale> kappa_dbl(length(kappa));
82 VectorBuilder<include_summand<propto, T_scale>::value,
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]);
86 if (include_summand<propto, T_scale>::value)
87 log_bessel0[i]
88 = log(modified_bessel_first_kind(0, value_of(kappa_vec[i])));
89 }
90
91 OperandsAndPartials<T_y, T_loc, T_scale>
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.
113 if (include_summand<propto>::value)
114 logp -= LOG_TWO_PI;
115 if (include_summand<propto, T_scale>::value)
116 logp -= log_bessel0[n];
117 if (include_summand<propto, T_y, T_loc, T_scale>::value)
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>
134 inline typename return_type<T_y, T_loc, T_scale>::type
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
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:40
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:15
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:68
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:36
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:193
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:15
stan::math::sin
fvar< T > sin(const fvar< T > &x)
Definition: sin.hpp:14
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:27
stan::math::OperandsAndPartials
This class builds partial derivatives with respect to a set of operands.
Definition: OperandsAndPartials.hpp:38
VectorView.hpp
stan::math::OperandsAndPartials::d_x3
VectorView< T_return_type, false, true > d_x3
Definition: OperandsAndPartials.hpp:41
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:62
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:86
stan::math::check_nonnegative
bool check_nonnegative(const char *function, const char *name, const T_y &y)
Return true if y is non-negative.
Definition: check_nonnegative.hpp:66
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::check_greater
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.
Definition: check_greater.hpp:84
stan::math::OperandsAndPartials::d_x1
VectorView< T_return_type, false, true > d_x1
Definition: OperandsAndPartials.hpp:39

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