30 #ifndef STAN_MATH_PRIM_MAT_PROB_WIENER_LPDF_HPP 31 #define STAN_MATH_PRIM_MAT_PROB_WIENER_LPDF_HPP 44 #include <boost/math/distributions.hpp> 70 template <
bool propto,
71 typename T_y,
typename T_alpha,
typename T_tau,
72 typename T_beta,
typename T_delta>
74 wiener_lpdf(
const T_y& y,
const T_alpha& alpha,
const T_tau& tau,
75 const T_beta& beta,
const T_delta& delta) {
76 static const char*
function(
"wiener_lpdf(%1%)");
82 static const double WIENER_ERR = 0.000001;
83 static const double PI_TIMES_WIENER_ERR =
pi() * WIENER_ERR;
84 static const double LOG_PI_LOG_WIENER_ERR =
87 TWO_TIMES_SQRT_2_TIMES_SQRT_PI_TIMES_WIENER_ERR =
89 static const double LOG_TWO_OVER_TWO_PLUS_LOG_SQRT_PI =
91 static const double SQUARE_PI_OVER_TWO =
square(
pi()) * 0.5;
92 static const double TWO_TIMES_LOG_SQRT_PI = 2.0 *
LOG_SQRT_PI;
102 T_beta, T_delta>::type T_return_type;
103 T_return_type lp(0.0);
119 "Boundary separation", alpha,
120 "A-priori bias", beta,
121 "Nondecision time", tau,
"Drift rate", delta);
133 for (
size_t i = 0; i < N_y_tau; ++i) {
134 if (y_vec[i] <= tau_vec[i]) {
135 std::stringstream msg;
136 msg <<
", but must be greater than nondecision time = " << tau_vec[i];
137 std::string msg_str(msg.str());
138 domain_error(
function,
"Random variable", y_vec[i],
" = ",
146 for (
size_t i = 0; i < N; i++) {
149 T_return_type x = (y_vec[i] - tau_vec[i]) / alpha2;
150 T_return_type kl, ks, tmp = 0;
152 T_return_type sqrt_x =
sqrt(x);
153 T_return_type log_x =
log(x);
154 T_return_type one_over_pi_times_sqrt_x = 1.0 /
pi() * sqrt_x;
158 if (PI_TIMES_WIENER_ERR * x < 1) {
160 kl =
sqrt(-2.0 *
SQRT_PI * (LOG_PI_LOG_WIENER_ERR + log_x)) / sqrt_x;
162 kl = (kl > one_over_pi_times_sqrt_x) ? kl : one_over_pi_times_sqrt_x;
164 kl = one_over_pi_times_sqrt_x;
168 T_return_type tmp_expr0
169 = TWO_TIMES_SQRT_2_TIMES_SQRT_PI_TIMES_WIENER_ERR * sqrt_x;
172 ks = 2.0 + sqrt_x *
sqrt(-2 *
log(tmp_expr0));
174 T_return_type sqrt_x_plus_one = sqrt_x + 1.0;
175 ks = (ks > sqrt_x_plus_one) ? ks : sqrt_x_plus_one;
181 T_return_type tmp_expr1 = (K - 1.0) / 2.0;
182 T_return_type tmp_expr2 =
ceil(tmp_expr1);
183 for (k = -
floor(tmp_expr1); k <= tmp_expr2; k++)
184 tmp += (one_minus_beta + 2.0 * k) *
185 exp(-(
square(one_minus_beta + 2.0 * k)) * 0.5 / x);
186 tmp =
log(tmp) - LOG_TWO_OVER_TWO_PLUS_LOG_SQRT_PI - 1.5 * log_x;
189 for (k = 1; k <= K; ++k)
190 tmp += k *
exp(-(
square(k)) * (SQUARE_PI_OVER_TWO * x))
191 *
sin(k *
pi() * one_minus_beta);
192 tmp =
log(tmp) + TWO_TIMES_LOG_SQRT_PI;
196 lp += delta_vec[i] * alpha_vec[i] * one_minus_beta
197 -
square(delta_vec[i]) * x * alpha2 / 2.0
203 template <
typename T_y,
typename T_alpha,
typename T_tau,
204 typename T_beta,
typename T_delta>
208 const T_beta& beta,
const T_delta& delta) {
209 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)
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
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)
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)