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

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