| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

wiener_log.hpp
Go to the documentation of this file.
1 // Original code from which Stan's code is derived:
2 // Copyright (c) 2013, Joachim Vandekerckhove.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted
7 // provided that the following conditions are met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // * this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright notice,
12 // * this list of conditions and the following disclaimer in the
13 // * documentation and/or other materials provided with the distribution.
14 // * Neither the name of the University of California, Irvine nor the names
15 // * of its contributors may be used to endorse or promote products derived
16 // * from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 // THE POSSIBILITY OF SUCH DAMAGE.
29
30 #ifndef STAN_MATH_PRIM_MAT_PROB_WIENER_LOG_HPP
31 #define STAN_MATH_PRIM_MAT_PROB_WIENER_LOG_HPP
32
33 #include <stan/math/prim/scal/fun/constants.hpp>
34 #include <stan/math/prim/scal/fun/square.hpp>
35 #include <stan/math/prim/scal/fun/value_of.hpp>
36 #include <stan/math/prim/scal/err/check_consistent_sizes.hpp>
37 #include <stan/math/prim/scal/err/check_bounded.hpp>
38 #include <stan/math/prim/scal/err/check_finite.hpp>
39 #include <stan/math/prim/scal/err/check_not_nan.hpp>
40 #include <stan/math/prim/scal/err/check_positive.hpp>
41 #include <stan/math/prim/scal/meta/return_type.hpp>
42 #include <stan/math/prim/scal/meta/include_summand.hpp>
43 #include <boost/math/distributions.hpp>
44 #include <algorithm>
45 #include <cmath>
46 #include <string>
47
48 namespace stan {
49 namespace math {
50
69 template <bool propto,
70 typename T_y, typename T_alpha, typename T_tau,
71 typename T_beta, typename T_delta>
72 typename return_type<T_y, T_alpha, T_tau, T_beta, T_delta>::type
73 wiener_log(const T_y& y, const T_alpha& alpha, const T_tau& tau,
74 const T_beta& beta, const T_delta& delta) {
75 static const char* function("wiener_log(%1%)");
76
77 using std::log;
78 using std::exp;
79 using std::pow;
80
81 static const double WIENER_ERR = 0.000001;
82 static const double PI_TIMES_WIENER_ERR = pi() * WIENER_ERR;
83 static const double LOG_PI_LOG_WIENER_ERR =
84 LOG_PI + log(WIENER_ERR);
85 static const double
86 TWO_TIMES_SQRT_2_TIMES_SQRT_PI_TIMES_WIENER_ERR =
87 2.0 * SQRT_2_TIMES_SQRT_PI * WIENER_ERR;
88 static const double LOG_TWO_OVER_TWO_PLUS_LOG_SQRT_PI =
89 LOG_TWO / 2 + LOG_SQRT_PI;
90 static const double SQUARE_PI_OVER_TWO = square(pi()) * 0.5;
91 static const double TWO_TIMES_LOG_SQRT_PI = 2.0 * LOG_SQRT_PI;
92
93 if (!(stan::length(y)
94 && stan::length(alpha)
95 && stan::length(beta)
96 && stan::length(tau)
97 && stan::length(delta)))
98 return 0.0;
99
100 typedef typename return_type<T_y, T_alpha, T_tau,
101 T_beta, T_delta>::type T_return_type;
102 T_return_type lp(0.0);
103
104 check_not_nan(function, "Random variable", y);
105 check_not_nan(function, "Boundary separation", alpha);
106 check_not_nan(function, "A-priori bias", beta);
107 check_not_nan(function, "Nondecision time", tau);
108 check_not_nan(function, "Drift rate", delta);
109 check_finite(function, "Boundary separation", alpha);
110 check_finite(function, "A-priori bias", beta);
111 check_finite(function, "Nondecision time", tau);
112 check_finite(function, "Drift rate", delta);
113 check_positive(function, "Random variable", y);
114 check_positive(function, "Boundary separation", alpha);
115 check_positive(function, "Nondecision time", tau);
116 check_bounded(function, "A-priori bias", beta , 0, 1);
117 check_consistent_sizes(function, "Random variable", y,
118 "Boundary separation", alpha,
119 "A-priori bias", beta,
120 "Nondecision time", tau, "Drift rate", delta);
121
122 size_t N = std::max(max_size(y, alpha, beta), max_size(tau, delta));
123 if (!N) return 0.0;
124
125 VectorView<const T_y> y_vec(y);
126 VectorView<const T_alpha> alpha_vec(alpha);
127 VectorView<const T_beta> beta_vec(beta);
128 VectorView<const T_tau> tau_vec(tau);
129 VectorView<const T_delta> delta_vec(delta);
130
131 size_t N_y_tau = max_size(y, tau);
132 for (size_t i = 0; i < N_y_tau; ++i) {
133 if (y_vec[i] <= tau_vec[i]) {
134 std::stringstream msg;
135 msg << ", but must be greater than nondecision time = " << tau_vec[i];
136 std::string msg_str(msg.str());
137 domain_error(function, "Random variable", y_vec[i], " = ",
138 msg_str.c_str());
139 }
140 }
141
142 if (!include_summand<propto, T_y, T_alpha, T_tau, T_beta, T_delta>::value)
143 return 0;
144
145 for (size_t i = 0; i < N; i++) {
146 typename scalar_type<T_beta>::type one_minus_beta = 1.0 - beta_vec[i];
147 typename scalar_type<T_alpha>::type alpha2 = square(alpha_vec[i]);
148 T_return_type x = (y_vec[i] - tau_vec[i]) / alpha2;
149 T_return_type kl, ks, tmp = 0;
150 T_return_type k, K;
151 T_return_type sqrt_x = sqrt(x);
152 T_return_type log_x = log(x);
153 T_return_type one_over_pi_times_sqrt_x = 1.0 / pi() * sqrt_x;
154
155 // calculate number of terms needed for large t:
156 // if error threshold is set low enough
157 if (PI_TIMES_WIENER_ERR * x < 1) {
158 // compute bound
159 kl = sqrt(-2.0 * SQRT_PI * (LOG_PI_LOG_WIENER_ERR + log_x)) / sqrt_x;
160 // ensure boundary conditions met
161 kl = (kl > one_over_pi_times_sqrt_x) ? kl : one_over_pi_times_sqrt_x;
162 } else {
163 kl = one_over_pi_times_sqrt_x; // set to boundary condition
164 }
165 // calculate number of terms needed for small t:
166 // if error threshold is set low enough
167 T_return_type tmp_expr0
168 = TWO_TIMES_SQRT_2_TIMES_SQRT_PI_TIMES_WIENER_ERR * sqrt_x;
169 if (tmp_expr0 < 1) {
170 // compute bound
171 ks = 2.0 + sqrt_x * sqrt(-2 * log(tmp_expr0));
172 // ensure boundary conditions are met
173 T_return_type sqrt_x_plus_one = sqrt_x + 1.0;
174 ks = (ks > sqrt_x_plus_one) ? ks : sqrt_x_plus_one;
175 } else { // if error threshold was set too high
176 ks = 2.0; // minimal kappa for that case
177 }
178 if (ks < kl) { // small t
179 K = ceil(ks); // round to smallest integer meeting error
180 T_return_type tmp_expr1 = (K - 1.0) / 2.0;
181 T_return_type tmp_expr2 = ceil(tmp_expr1);
182 for (k = -floor(tmp_expr1); k <= tmp_expr2; k++)
183 tmp += (one_minus_beta + 2.0 * k) *
184 exp(-(square(one_minus_beta + 2.0 * k)) * 0.5 / x);
185 tmp = log(tmp) - LOG_TWO_OVER_TWO_PLUS_LOG_SQRT_PI - 1.5 * log_x;
186 } else { // if large t is better...
187 K = ceil(kl); // round to smallest integer meeting error
188 for (k = 1; k <= K; ++k)
189 tmp += k * exp(-(square(k)) * (SQUARE_PI_OVER_TWO * x))
190 * sin(k * pi() * one_minus_beta);
191 tmp = log(tmp) + TWO_TIMES_LOG_SQRT_PI;
192 }
193
194 // convert to f(t|v,a,w) and return result
195 lp += delta_vec[i] * alpha_vec[i] * one_minus_beta
196 - square(delta_vec[i]) * x * alpha2 / 2.0
197 - log(alpha2) + tmp;
198 }
199 return lp;
200 }
201
202 template <typename T_y, typename T_alpha, typename T_tau,
203 typename T_beta, typename T_delta>
204 inline
205 typename return_type<T_y, T_alpha, T_tau, T_beta, T_delta>::type
206 wiener_log(const T_y& y, const T_alpha& alpha, const T_tau& tau,
207 const T_beta& beta, const T_delta& delta) {
208 return wiener_log<false>(y, alpha, tau, beta, delta);
209 }
210
211 }
212 }
213 #endif
check_not_nan.hpp
stan::math::wiener_log
return_type< T_y, T_alpha, T_tau, T_beta, T_delta >::type wiener_log(const T_y &y, const T_alpha &alpha, const T_tau &tau, const T_beta &beta, const T_delta &delta)
The log of the first passage time density function for a (Wiener) drift diffusion model for the given...
Definition: wiener_log.hpp:73
stan::math::sqrt
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:14
stan::math::check_not_nan
bool check_not_nan(const char *function, const char *name, const T_y &y)
Return true if y is not NaN.
Definition: check_not_nan.hpp:60
check_positive.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::LOG_PI
const double LOG_PI
Definition: constants.hpp:167
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
square.hpp
stan::math::LOG_SQRT_PI
const double LOG_SQRT_PI
Definition: constants.hpp:170
stan::length
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
stan::math::check_bounded
bool check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Return true if the value is between the low and high values, inclusively.
Definition: check_bounded.hpp:95
stan::return_type
Metaprogram to calculate the base scalar return type resulting from promoting all the scalar types of...
Definition: return_type.hpp:19
stan::scalar_type::type
scalar_type_helper< is_vector< T >::value, T >::type type
Definition: scalar_type.hpp:34
return_type.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
stan::math::square
fvar< T > square(const fvar< T > &x)
Definition: square.hpp:14
stan::math::LOG_TWO
const double LOG_TWO
Definition: constants.hpp:174
include_summand.hpp
stan::math::SQRT_2_TIMES_SQRT_PI
const double SQRT_2_TIMES_SQRT_PI
Definition: constants.hpp:156
stan::math::sin
fvar< T > sin(const fvar< T > &x)
Definition: sin.hpp:12
stan::math::exp
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
stan::math::check_positive
bool check_positive(const char *function, const char *name, const T_y &y)
Return true if y is positive.
Definition: check_positive.hpp:66
stan::max_size
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
stan::math::domain_error
void domain_error(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw a domain error with a consistently formatted message.
Definition: domain_error.hpp:30
stan::math::max
int max(const std::vector< int > &x)
Returns the maximum coefficient in the specified column vector.
Definition: max.hpp:22
check_bounded.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
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
stan::math::pow
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition: pow.hpp:17
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
check_consistent_sizes.hpp
stan::math::SQRT_PI
const double SQRT_PI
Definition: constants.hpp:154
stan::math::ceil
fvar< T > ceil(const fvar< T > &x)
Definition: ceil.hpp:11

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