30 #ifndef STAN_MATH_PRIM_MAT_PROB_WIENER_LPDF_HPP 31 #define STAN_MATH_PRIM_MAT_PROB_WIENER_LPDF_HPP 43 #include <boost/math/distributions.hpp> 69 template <
bool propto,
70 typename T_y,
typename T_alpha,
typename T_tau,
71 typename T_beta,
typename T_delta>
73 wiener_lpdf(
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_lpdf(%1%)");
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 =
86 TWO_TIMES_SQRT_2_TIMES_SQRT_PI_TIMES_WIENER_ERR =
88 static const double LOG_TWO_OVER_TWO_PLUS_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;
101 T_beta, T_delta>::type T_return_type;
102 T_return_type lp(0.0);
118 "Boundary separation", alpha,
119 "A-priori bias", beta,
120 "Nondecision time", tau,
"Drift rate", delta);
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],
" = ",
145 for (
size_t i = 0; i < N; i++) {
148 T_return_type x = (y_vec[i] - tau_vec[i]) / alpha2;
149 T_return_type kl, ks, tmp = 0;
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;
157 if (PI_TIMES_WIENER_ERR * x < 1) {
159 kl =
sqrt(-2.0 *
SQRT_PI * (LOG_PI_LOG_WIENER_ERR + log_x)) / sqrt_x;
161 kl = (kl > one_over_pi_times_sqrt_x) ? kl : one_over_pi_times_sqrt_x;
163 kl = one_over_pi_times_sqrt_x;
167 T_return_type tmp_expr0
168 = TWO_TIMES_SQRT_2_TIMES_SQRT_PI_TIMES_WIENER_ERR * sqrt_x;
171 ks = 2.0 + sqrt_x *
sqrt(-2 *
log(tmp_expr0));
173 T_return_type sqrt_x_plus_one = sqrt_x + 1.0;
174 ks = (ks > sqrt_x_plus_one) ? ks : sqrt_x_plus_one;
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;
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;
195 lp += delta_vec[i] * alpha_vec[i] * one_minus_beta
196 -
square(delta_vec[i]) * x * alpha2 / 2.0
202 template <
typename T_y,
typename T_alpha,
typename T_tau,
203 typename T_beta,
typename T_delta>
207 const T_beta& beta,
const T_delta& delta) {
208 return wiener_lpdf<false>(y, alpha, tau, beta, delta);
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
fvar< T > sqrt(const fvar< T > &x)
void check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Check if the value is between the low and high values, inclusively.
fvar< T > log(const fvar< T > &x)
size_t length(const std::vector< T > &x)
Metaprogram to calculate the base scalar return type resulting from promoting all the scalar types of...
scalar_type_helper< is_vector< T >::value, T >::type type
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
fvar< T > square(const fvar< T > &x)
const double SQRT_2_TIMES_SQRT_PI
fvar< T > sin(const fvar< T > &x)
fvar< T > exp(const fvar< T > &x)
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
return_type< T_y, T_alpha, T_tau, T_beta, T_delta >::type wiener_lpdf(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...
size_t max_size(const T1 &x1, const T2 &x2)
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.
int max(const std::vector< int > &x)
Returns the maximum coefficient in the specified column vector.
fvar< T > floor(const fvar< T > &x)
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
double pi()
Return the value of pi.
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
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[].
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Check if the dimension of x1 is consistent with x2.
fvar< T > ceil(const fvar< T > &x)