| Stan Math Library 2.6.3 probability, sampling & optimization

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
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/OperandsAndPartials.hpp>
5 #include <stan/math/prim/scal/err/check_consistent_sizes.hpp>
6 #include <stan/math/prim/scal/err/check_finite.hpp>
7 #include <stan/math/prim/scal/err/check_greater.hpp>
8 #include <stan/math/prim/scal/err/check_nonnegative.hpp>
9 #include <stan/math/prim/scal/err/check_positive_finite.hpp>
10 #include <stan/math/prim/scal/meta/is_constant_struct.hpp>
11 #include <stan/math/prim/scal/fun/modified_bessel_first_kind.hpp>
12 #include <stan/math/prim/scal/fun/constants.hpp>
13 #include <stan/math/prim/scal/meta/include_summand.hpp>
14 #include <stan/math/prim/scal/meta/VectorView.hpp>
15 #include <stan/math/prim/scal/meta/VectorBuilder.hpp>
16 #include <stan/math/prim/scal/fun/value_of.hpp>
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";
28 typedef typename stan::partials_return_type<T_y, T_loc, T_scale>::type
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
37 using stan::is_constant_struct;
38 using stan::math::check_finite;
39 using stan::math::check_positive_finite;
40 using stan::math::check_greater;
41 using stan::math::check_nonnegative;
42 using stan::math::check_consistent_sizes;
43 using stan::math::value_of;
44
45 using stan::math::modified_bessel_first_kind;
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
62 if (!include_summand<propto, T_y, T_loc, T_scale>::value)
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
80 VectorBuilder<true, T_partials_return, T_scale> kappa_dbl(length(kappa));
81 VectorBuilder<include_summand<propto, T_scale>::value,
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]);
85 if (include_summand<propto, T_scale>::value)
86 log_bessel0[i]
87 = log(modified_bessel_first_kind(0, value_of(kappa_vec[i])));
88 }
89
90 OperandsAndPartials<T_y, T_loc, T_scale> oap(y, mu, kappa);
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.
111 if (include_summand<propto>::value)
112 logp -= LOG_TWO_PI;
113 if (include_summand<propto, T_scale>::value)
114 logp -= log_bessel0[n];
115 if (include_summand<propto, T_y, T_loc, T_scale>::value)
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>
131 inline typename return_type<T_y, T_loc, T_scale>::type
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
stan::math::cos
fvar< T > cos(const fvar< T > &x)
Definition: cos.hpp:13
check_positive_finite.hpp
stan::math::value_of
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:16
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
stan::length
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
modified_bessel_first_kind.hpp
stan::math::OperandsAndPartials::to_var
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)
Definition: OperandsAndPartials.hpp:322
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
VectorView.hpp
include_summand.hpp
stan::math::LOG_TWO_PI
const double LOG_TWO_PI
Definition: constants.hpp:193
check_greater.hpp
stan::math::OperandsAndPartials::d_x1
VectorView< T_partials_return, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1
Definition: OperandsAndPartials.hpp:253
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:16
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:26
stan::math::OperandsAndPartials::d_x3
VectorView< T_partials_return, is_vector< T3 >::value, is_constant_struct< T3 >::value > d_x3
Definition: OperandsAndPartials.hpp:259
stan::math::OperandsAndPartials
A variable implementation that stores operands and derivatives with respect to the variable...
Definition: OperandsAndPartials.hpp:238
OperandsAndPartials.hpp
stan::max_size
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
VectorBuilder.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
Definition: VectorBuilder.hpp:61
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::OperandsAndPartials::d_x2
VectorView< T_partials_return, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
Definition: OperandsAndPartials.hpp:256
is_constant_struct.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 metaprogram that takes its argument and allows it to be used like a vector...
Definition: VectorView.hpp:41
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 Home Page ] © 2011–2015, Stan Development Team.