1#ifndef STAN_MATH_PRIM_PROB_WIENER4_LCDF_DEFECTIVE_HPP
2#define STAN_MATH_PRIM_PROB_WIENER4_LCDF_DEFECTIVE_HPP
16template <
typename T_x>
18 if (x < std::numeric_limits<T_x>::max()) {
21 return std::numeric_limits<T_x>::max();
33template <
typename T_a,
typename T_w,
typename T_v>
38 return ret_t(
log1m(w));
40 auto two_va = 2.0 * v * a;
41 auto minus_two_va_one_minus_w = -two_va * (1.0 - w);
43 if (minus_two_va_one_minus_w < 0) {
44 const auto exp_arg =
exp(minus_two_va_one_minus_w);
45 auto two_vaw = two_va * w;
46 if (two_vaw > minus_two_va_one_minus_w) {
48 }
else if (two_vaw < minus_two_va_one_minus_w) {
66template <
typename T_a,
typename T_w,
typename T_v>
72 auto nearly_one = ret_t(1.0 - 1.1 * 1.0e-5);
75 const auto two_av = 2.0 * a * v;
76 const auto two_va_one_minus_w = (two_av * (1.0 - w));
77 const auto two_avw = two_av * w;
78 const auto exp_two_va_one_minus_w =
exp(two_va_one_minus_w);
79 const auto exp_two_avw =
exp(two_avw);
80 const auto exp_two_av =
exp(two_av);
81 if (((exp_two_va_one_minus_w >= nearly_one) || (exp_two_avw >= nearly_one))
82 || (exp_two_av >= nearly_one)) {
85 log_prob =
LOG_TWO + two_va_one_minus_w -
log1m(exp_two_va_one_minus_w);
86 auto log_quotient =
log1m(exp_two_avw) -
log1m(exp_two_av);
87 if (
log(w) > log_quotient) {
88 return exp(log_prob) * (w -
exp(log_quotient));
90 return -
exp(log_prob) * (
exp(log_quotient) - w);
93 const auto minus_two_av = -2.0 * a * v;
94 const auto minus_two_va_one_minus_w = minus_two_av * (1.0 - w);
95 const auto exp_minus_two_va_one_minus_w =
exp(minus_two_va_one_minus_w);
96 const auto exp_minus_two_av =
exp(minus_two_av);
97 if ((exp_minus_two_va_one_minus_w >= nearly_one)
98 || (exp_minus_two_av >= nearly_one)) {
101 log_prob =
LOG_TWO -
log1m(exp_minus_two_va_one_minus_w);
103 if (minus_two_va_one_minus_w > minus_two_av) {
104 log_quotient =
log_diff_exp(minus_two_va_one_minus_w, minus_two_av)
105 -
log1m(exp_minus_two_av);
106 }
else if (minus_two_va_one_minus_w < minus_two_av) {
107 log_quotient =
log_diff_exp(minus_two_av, minus_two_va_one_minus_w)
108 -
log1m(exp_minus_two_av);
112 if (
log(w) > log_quotient) {
126template <
typename T_x>
145template <
bool NaturalScale =
false,
typename T_y,
typename T_a,
typename T_w,
146 typename T_v,
typename T_err>
148 const T_w& w, T_err log_err =
log(1
e-12)) {
150 const auto neg_v = -v;
151 const auto one_m_w = 1.0 - w;
153 const auto one_m_w_a_neg_v = one_m_w * a * neg_v;
155 const auto K1 = 0.5 * (
fabs(neg_v) / a * y - one_m_w);
157 0.0,
fmin(1.0,
exp(one_m_w_a_neg_v +
square(neg_v) * y / 2.0 + log_err)
162 const auto K_small_value =
ceil(
fmax(K1, K1 + K2));
164 const auto api = a /
pi();
165 const auto v_square =
square(neg_v);
166 const auto sqrtL1 =
sqrt(1.0 / y) * api;
168 1.0, -2.0 / y *
square(api)
169 * (log_err +
log(
pi() * y / 2.0 * (v_square +
square(
pi() / a)))
170 + one_m_w_a_neg_v + v_square * y / 2.0)));
171 const auto K_large_value =
ceil(
fmax(sqrtL1, sqrtL2));
175 if (3 * K_small_value < K_large_value) {
176 const auto sqrt_y =
sqrt(y);
177 const auto neg_vy = neg_v * y;
180 for (
auto k = K_small_value; k >= 0; --k) {
181 auto rj = a * (2.0 * k + one_m_w);
183 auto pos1 = dj +
logMill((rj - neg_vy) / sqrt_y);
184 auto pos2 = dj +
logMill((rj + neg_vy) / sqrt_y);
186 rj = a * (2.0 * k + 2.0 - one_m_w);
188 auto neg1 = dj +
logMill((rj - neg_vy) / sqrt_y);
189 auto neg2 = dj +
logMill((rj + neg_vy) / sqrt_y);
192 auto ans = ret_t(0.0);
195 ret_t log_distribution = ans - one_m_w_a_neg_v -
square(neg_v) * y / 2;
196 return NaturalScale ?
exp(log_distribution) : log_distribution;
198 const auto log_a =
log(a);
199 const auto log_v =
log(
fabs(neg_v));
202 for (
auto k = K_large_value; k > 0; --k) {
204 auto k_pi = k *
pi();
205 auto sin_k_pi_w =
sin(k_pi * one_m_w);
206 if (sin_k_pi_w > 0) {
210 - 0.5 *
square(k_pi / a) * y +
log(sin_k_pi_w));
211 }
else if (sin_k_pi_w < 0) {
215 - 0.5 *
square(k_pi / a) * y +
log(-sin_k_pi_w));
222 auto summand_2 = lg + (ans - one_m_w_a_neg_v - 0.5 *
square(neg_v) * y);
224 if (summand_1 > summand_2) {
226 }
else if (summand_1 < summand_2) {
229 return NaturalScale ?
exp(log_distribution) : log_distribution;
244template <
typename T_y,
typename T_a,
typename T_v,
typename T_w,
245 typename T_cdf,
typename T_err>
247 const T_w& w, T_cdf&& cdf,
248 T_err log_err =
log(1
e-12)) {
250 const auto neg_v = -v;
251 const auto one_m_w = 1 - w;
253 const auto one_m_w_neg_v = one_m_w * neg_v;
254 const auto one_m_w_a_neg_v = one_m_w_neg_v * a;
256 const auto log_y =
log(y);
257 const auto log_a =
log(a);
261 const auto factor = one_m_w_a_neg_v +
square(neg_v) * y / 2.0 + log_err;
263 const auto K = a /
pi() /
sqrt(y);
264 const auto K_large_value
267 const auto sqrt_y =
sqrt(y);
268 const auto wdash =
fmin(one_m_w, w);
272 = (sqrt_y *
sqrt(-(ueps -
sqrt(-2.0 * ueps - 2.0))) - a * wdash) / a;
273 const auto K_large = sqrt_y / a - wdash;
274 const auto K_small_value =
ceil(
fmax(
fmax(K_small, K_large), ret_t(1.0)));
278 if (K_large_value > 4 * K_small_value) {
279 const auto neg_vy = neg_v * y;
280 auto ans = ret_t(0.0);
281 auto F_k = ret_t(0.0);
282 for (
auto k = K_small_value; k >= 0; --k) {
283 auto r_k_1 = a * (2.0 * k + one_m_w);
284 auto x_1 = r_k_1 - neg_vy;
285 auto x_over_sqrt_y_1 = x_1 / sqrt_y;
288 auto exp_d_k_1 =
exp(d_k_1);
289 auto ans_1 = -temp_1 * neg_vy - sqrt_y * exp_d_k_1;
291 auto x_2 = r_k_1 + neg_vy;
292 auto x_over_sqrt_y_2 = x_2 / sqrt_y;
294 auto ans_2 = temp_2 * neg_vy - sqrt_y * exp_d_k_1;
295 auto r_k_2 = a * (2.0 * k + 1.0 + w);
298 auto x_3 = r_k_2 - neg_vy;
299 auto x_over_sqrt_y_3 = x_3 / sqrt_y;
301 auto exp_d_k_2 =
exp(d_k_2);
302 auto ans_3 = -temp_3 * neg_vy - sqrt_y * exp_d_k_2;
304 auto x_4 = r_k_2 + neg_vy;
305 auto x_over_sqrt_y_4 = x_4 / sqrt_y;
307 auto ans_4 = temp_4 * neg_vy - sqrt_y * exp_d_k_2;
309 ans += (ans_1 + ans_2 + ans_3 - ans_4) * (2.0 * k + one_m_w)
313 const auto summands_small_y = ans / (y * F_k);
314 return -one_m_w_neg_v * cdf + summands_small_y;
317 for (
auto k = K_large_value; k > 0; --k) {
318 const auto kpi = k *
pi();
319 const auto kpia2 =
square(kpi / a);
320 const auto denom =
square(neg_v) + kpia2;
321 auto last = (
square(kpi) /
pow(a, 3) * (y + 2.0 / denom)) * k / denom
322 *
exp(-0.5 * kpia2 * y);
323 ans -= last *
sin(kpi * one_m_w);
328 auto dav_neg_v = dav * neg_v;
329 auto prob_deriv =
fabs(neg_v) == 0
332 ans = (-2.0 / a - one_m_w_neg_v) * (cdf - prob)
334 *
exp(-one_m_w_a_neg_v - 0.5 *
square(neg_v) * y);
335 return prob_deriv + ans;
350template <
typename T_y,
typename T_a,
typename T_v,
typename T_w,
351 typename T_cdf,
typename T_err>
353 const T_w& w, T_cdf&& cdf,
354 T_err log_err =
log(1
e-12)) {
356 const auto neg_v = -v;
357 const auto one_m_w = 1.0 - w;
359 const auto one_m_w_a = one_m_w * a;
360 const auto one_m_w_a_neg_v = one_m_w_a * neg_v;
362 const auto log_y =
log(y);
363 const auto factor = one_m_w_a_neg_v +
square(neg_v) * y / 2.0 + log_err;
365 const auto log_a =
log(a);
366 auto K_large_value = ret_t(1.0);
369 const auto log_v =
log(
fabs(neg_v));
370 auto alphK_large =
fmin(
exp(factor + 0.5 * (7 *
LOG_PI + log_y)
371 - 2.5 *
LOG_TWO - 3 * log_a - log_v),
377 : temp *
inv_Phi(alphK_large)),
381 const auto sqrt_y =
sqrt(y);
382 const auto wdash =
fmin(one_m_w, w);
383 auto K_large =
fabs(neg_v) / a * y - wdash;
384 const auto alphK_small = factor + 0.5 * (
LOG_TWO - log_y +
LOG_PI);
386 = (alphK_small < 0) ? sqrt_y *
sqrt(-2.0 * alphK_small) / a - wdash : 0;
387 const auto K_small_value =
ceil(
fmax(
fmax(K_small, K_large), ret_t(1.0)));
388 if (K_large_value > 4 * K_small_value) {
389 const auto sqrt_y =
sqrt(y);
390 const auto neg_vy = neg_v * y;
391 auto ans = ret_t(0.0);
392 auto F_k = ret_t(0.0);
393 for (
auto k = K_small_value; k >= 0; --k) {
394 auto r_k_1 = a * (2.0 * k + one_m_w);
396 auto x_1 = r_k_1 - neg_vy;
397 auto x_over_sqrt_y_1 = x_1 / sqrt_y;
400 auto x_2 = r_k_1 + neg_vy;
401 auto x_over_sqrt_y_2 = x_2 / sqrt_y;
403 auto r_k_2 = a * (2.0 * k + 1.0 + w);
406 auto x_3 = r_k_2 - neg_vy;
407 auto x_over_sqrt_y_3 = x_3 / sqrt_y;
410 auto x_4 = r_k_2 + neg_vy;
411 auto x_over_sqrt_y_4 = x_4 / sqrt_y;
413 ans += -ans_1 * x_1 + ans_2 * x_2 + ans_3 * x_3 - ans_4 * x_4;
416 const auto summands_small_y = ans / F_k;
417 return (one_m_w_a + neg_vy) * cdf - summands_small_y;
420 for (
auto k = K_large_value; k > 0; --k) {
421 const auto kpi = k *
pi();
422 const auto kpia2 =
square(kpi / a);
423 const auto ekpia2y =
exp(-0.5 * kpia2 * y);
424 const auto denom =
square(neg_v) + kpia2;
425 const auto denomk = k / denom;
426 auto last = denomk * ekpia2y / denom;
427 ans -= last *
sin(kpi * one_m_w);
432 auto dav_a = dav * a;
434 ans = (-one_m_w_a + v * y) * (cdf - prob)
436 *
exp(-one_m_w_a_neg_v - 0.5 *
square(neg_v) * y);
437 return -(prob_deriv + ans);
452template <
typename T_y,
typename T_a,
typename T_v,
typename T_w,
453 typename T_cdf,
typename T_err>
455 const T_w& w, T_cdf&& cdf,
456 T_err log_err =
log(1
e-12)) {
458 const auto neg_v = -v;
459 const auto one_m_w = 1 - w;
461 const auto one_m_w_a_neg_v = one_m_w * a * neg_v;
463 const auto factor = one_m_w_a_neg_v +
square(neg_v) * y / 2.0 + log_err;
465 const auto log_y =
log(y);
466 const auto log_a =
log(a);
470 alphK_large =
fmax(0.0, alphK_large);
471 const auto K_large_value
475 : temp *
inv_Phi(alphK_large)),
478 const auto sqrt_y =
sqrt(y);
479 const auto wdash =
fmin(one_m_w, w);
480 const auto K_large =
fabs(neg_v) / a * y - wdash;
482 const auto alphK_small = factor -
LOG_TWO - lv;
488 const auto K_small_value =
ceil(
fmax(
fmax(K_small, K_large), ret_t(1.0)));
490 if (K_large_value > 4 * K_small_value) {
491 const auto sqrt_y =
sqrt(y);
492 const auto neg_vy = neg_v * y;
493 auto ans = ret_t(0.0);
494 auto F_k = ret_t(0.0);
495 for (
auto k = K_small_value; k >= 0; --k) {
496 auto r_k_1 = a * (2.0 * k + one_m_w);
498 auto x_1 = r_k_1 - neg_vy;
499 auto x_over_sqrt_y_1 = x_1 / sqrt_y;
501 auto exp_d_k_1 =
exp(d_k_1);
502 auto ans_1 = -temp_1 * neg_vy - sqrt_y * exp_d_k_1;
504 auto x_2 = r_k_1 + neg_vy;
505 auto x_over_sqrt_y_2 = x_2 / sqrt_y;
507 auto ans_2 = temp_2 * neg_vy - sqrt_y * exp_d_k_1;
508 auto r_k_2 = a * (2.0 * k + 1.0 + w);
511 auto x_3 = r_k_2 - neg_vy;
512 auto x_over_sqrt_y_3 = x_3 / sqrt_y;
514 auto exp_d_k_2 =
exp(d_k_2);
515 auto ans_3 = -temp_3 * neg_vy - sqrt_y * exp_d_k_2;
517 auto x_4 = r_k_2 + neg_vy;
518 auto x_over_sqrt_y_4 = x_4 / sqrt_y;
520 auto ans_4 = temp_4 * neg_vy - sqrt_y * exp_d_k_2;
522 ans += (ans_1 + ans_2 + ans_3 - ans_4) * a;
525 const auto summands_small_y = ans / (y * F_k);
526 return neg_v * a * cdf - summands_small_y;
529 for (
auto k = K_large_value; k > 0; --k) {
530 const auto kpi = k *
pi();
531 const auto kpia2 =
square(kpi / a);
532 const auto ekpia2y =
exp(-0.5 * kpia2 * y);
533 const auto denom =
square(neg_v) + kpia2;
534 const auto denomk = k / denom;
536 last *= denomk * ekpia2y;
537 ans -= last *
cos(kpi * one_m_w);
539 const auto evaw =
exp(-one_m_w_a_neg_v - 0.5 *
square(neg_v) * y);
544 auto dav = ret_t(-1 / w);
546 auto nearly_one = ret_t(1.0 - 1.0e-6);
547 const auto sign_v = (neg_v < 0) ? 1 : -1;
548 const auto sign_two_va_one_minus_w = sign_v * (2.0 * neg_v * a * w);
549 const auto exp_arg =
exp(sign_two_va_one_minus_w);
550 if (exp_arg >= nearly_one) {
555 prob += sign_two_va_one_minus_w;
561 const auto pia2 = 2.0 *
pi() /
square(a);
562 auto prob_deriv = dav * prob;
563 ans = v * a * (cdf - prob) + ans * pia2 * evaw;
564 return -(prob_deriv + ans);
587template <
bool propto =
false,
typename T_y,
typename T_a,
typename T_t0,
588 typename T_w,
typename T_v>
590 const T_w& w,
const T_v& v,
591 const double& precision_derivatives = 1
e-4) {
603 T_t0_ref t0_ref = t0;
617 static constexpr const char* function_name =
"wiener4_lcdf";
623 "Boundary separation", a,
"Drift rate", v,
624 "A-priori bias", w,
"Nondecision time", t0);
628 check_less(function_name,
"A-priori bias", w_val, 1);
631 check_finite(function_name,
"Nondecision time", t0_val);
633 const size_t N =
max_size(y, a, t0, w, v);
640 const size_t N_y_t0 =
max_size(y, t0);
642 for (
size_t i = 0; i < N_y_t0; ++i) {
643 if (y_vec[i] <= t0_vec[i]) {
644 std::stringstream msg;
645 msg <<
", but must be greater than nondecision time = " << t0_vec[i];
646 std::string msg_str(msg.str());
653 const auto log_error_cdf =
log(1
e-6);
654 const auto log_error_derivative =
log(precision_derivatives);
655 const T_partials_return log_error_absolute =
log(1
e-12);
656 T_partials_return lcdf = 0.0;
660 const double LOG_FOUR = std::log(4.0);
663 for (
size_t i = 0; i < N; i++) {
664 const auto y_value = y_vec.val(i);
665 const auto a_value = a_vec.val(i);
666 const auto t0_value = t0_vec.val(i);
667 const auto w_value = w_vec.val(i);
668 const auto v_value = v_vec.val(i);
670 const T_partials_return log_cdf
674 return internal::wiener4_distribution<GradientCalc::OFF>(args...);
676 log_error_cdf -
LOG_TWO, y_value - t0_value, a_value, v_value,
677 w_value, log_error_absolute);
679 const T_partials_return cdf =
exp(log_cdf);
683 const auto new_est_err = log_cdf + log_error_derivative - LOG_FOUR;
690 return internal::wiener5_density<GradientCalc::ON>(args...);
692 new_est_err, y_value - t0_value, a_value, v_value, w_value, 0,
696 partials<0>(ops_partials)[i] = deriv_y / cdf;
699 partials<2>(ops_partials)[i] = -deriv_y / cdf;
703 partials<1>(ops_partials)[i]
709 new_est_err, y_value - t0_value, a_value, v_value, w_value, cdf,
714 partials<3>(ops_partials)[i]
720 new_est_err, y_value - t0_value, a_value, v_value, w_value, cdf,
725 partials<4>(ops_partials)[i]
727 w_value, cdf, log_error_absolute)
731 return ops_partials.build(lcdf);
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
return_type_t< T_y_cl > std_normal_lcdf(const T_y_cl &y)
Returns the log standard normal complementary cumulative distribution function.
return_type_t< T_y_cl > std_normal_lpdf(const T_y_cl &y)
The log of the normal density for the specified scalar(s) given a location of 0 and a scale of 1.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
auto wiener4_cdf_grad_w(const T_y &y, const T_a &a, const T_v &v, const T_w &w, T_cdf &&cdf, T_err log_err=log(1e-12))
Calculate derivative of the wiener4 distribution w.r.t.
auto wiener4_distribution(const T_y &y, const T_a &a, const T_v &v, const T_w &w, T_err log_err=log(1e-12))
Calculate the wiener4 distribution.
auto wiener4_cdf_grad_v(const T_y &y, const T_a &a, const T_v &v, const T_w &w, T_cdf &&cdf, T_err log_err=log(1e-12))
Calculate derivative of the wiener4 distribution w.r.t.
auto estimate_with_err_check(F &&functor, T_err &&log_err, ArgsTupleT &&... args_tuple)
Utility function for estimating a function with a given set of arguments, checking the result against...
auto logMill(T_x &&x)
Log of Mill's ratio for the normal distribution.
auto make_finite(const T_x &x)
Make the expression finite.
auto wiener4_cdf_grad_a(const T_y &y, const T_a &a, const T_v &v, const T_w &w, T_cdf &&cdf, T_err log_err=log(1e-12))
Calculate derivative of the wiener4 distribution w.r.t.
auto log_probability_distribution(const T_a &a, const T_v &v, const T_w &w)
Calculate the probability term 'P' on log scale for distribution.
auto log_probability_GradAV(const T_a &a, const T_v &v, const T_w &w)
Calculate the probability term 'P' on log scale for grad_a and grad_v.
fvar< T > sin(const fvar< T > &x)
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
fvar< T > fmin(const fvar< T > &x1, const fvar< T > &x2)
static constexpr double e()
Return the base of the natural logarithm.
fvar< T > log1m_exp(const fvar< T > &x)
Return the natural logarithm of one minus the exponentiation of the specified argument.
auto pow(const T1 &x1, const T2 &x2)
fvar< T > arg(const std::complex< fvar< T > > &z)
Return the phase angle of the complex argument.
fvar< T > log(const fvar< T > &x)
static constexpr double NEGATIVE_INFTY
Negative infinity.
void throw_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.
auto wiener_lcdf_defective(const T_y &y, const T_a &a, const T_t0 &t0, const T_w &w, const T_v &v, const double &precision_derivatives=1e-4)
Log-CDF function for the 4-parameter Wiener distribution.
static constexpr double LOG_TWO
The natural logarithm of 2, .
auto as_value_column_array_or_scalar(T &&a)
Extract the value from an object and for eigen vectors and std::vectors convert to an eigen column ar...
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > sqrt(const fvar< T > &x)
static constexpr double LOG_PI
The natural logarithm of , .
fvar< T > log1p(const fvar< T > &x)
fvar< T > inv_Phi(const fvar< T > &p)
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
fvar< T > fmax(const fvar< T > &x1, const fvar< T > &x2)
Return the greater of the two specified arguments.
fvar< T > log_diff_exp(const fvar< T > &x1, const fvar< T > &x2)
fvar< T > cos(const fvar< T > &x)
static constexpr double pi()
Return the value of pi.
int is_inf(const fvar< T > &x)
Returns 1 if the input's value is infinite and 0 otherwise.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
void check_less(const char *function, const char *name, const T_y &y, const T_high &high, Idxs... idxs)
Throw an exception if y is not strictly less than high.
fvar< T > ceil(const fvar< T > &x)
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
fvar< T > log1m(const fvar< T > &x)
void check_greater(const char *function, const char *name, const T_y &y, const T_low &low, Idxs... idxs)
Throw an exception if y is not strictly greater than low.
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
static constexpr double INFTY
Positive infinity.
fvar< T > fabs(const fvar< T > &x)
fvar< T > square(const fvar< T > &x)
fvar< T > log_sum_exp(const fvar< T > &x1, const fvar< T > &x2)
fvar< T > exp(const fvar< T > &x)
typename ref_type_if< true, T >::type ref_type_t
typename partials_return_type< Args... >::type partials_return_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...