1#ifndef STAN_MATH_PRIM_PROB_WIENER_FULL_LCDF_DEFECTIVE_HPP
2#define STAN_MATH_PRIM_PROB_WIENER_FULL_LCDF_DEFECTIVE_HPP
31template <
typename T_y,
typename T_a,
typename T_v,
typename T_w,
typename T_sw,
34 const T_w& w,
const T_sw& sw, T_err log_error) {
35 auto low =
fmax(0.0, w - sw / 2.0);
36 auto high =
fmin(1.0, w + sw / 2.0);
37 const auto lower_value
38 = wiener4_distribution<GradientCalc::ON>(y, a, v, low, log_error);
39 const auto upper_value
40 = wiener4_distribution<GradientCalc::ON>(y, a, v, high, log_error);
41 return 0.5 * (lower_value + upper_value) / sw;
72template <
GradientCalc Dist,
typename F,
typename T_y,
typename T_a,
73 typename T_v,
typename T_w,
typename T_sv,
typename T_sw,
74 typename T_err, std::enable_if_t<!Dist>* =
nullptr>
76 T_v&& v, T_w&& w, T_sv&& sv, T_sw&& sw,
78 return functor(y_diff, a, v, w, log_error);
109template <
GradientCalc Dist,
typename F,
typename T_y,
typename T_a,
110 typename T_v,
typename T_w,
typename T_sv,
typename T_sw,
111 typename T_err, std::enable_if_t<Dist>* =
nullptr>
113 T_v&& v, T_w&& w, T_sv&& sv, T_sw&& sw,
115 return functor(y_diff, a, v, w, sv, log_error);
138 typename Wiener7FunctorT,
typename T_err,
typename... TArgs>
140 T_err&& hcubature_err, TArgs&&... args) {
141 const auto functor = [&wiener7_functor](
auto&&... integration_args) {
143 [&wiener7_functor](
auto&& x,
auto&& y,
auto&& a,
auto&& v,
auto&& w,
144 auto&& t0,
auto&& sv,
auto&& sw,
auto&& st0,
148 decltype(w),
decltype(t0),
decltype(sv),
149 decltype(sw),
decltype(st0),
decltype(lerr)>;
151 const auto temp = (sv == 0) ? 0 :
square(x_vec[0]);
152 const auto factor = (sv == 0) ? 0 : x_vec[0] / (1 - temp);
153 const auto new_v = (sv == 0) ? v : v + sv * factor;
155 = (sw == 0) ? w : w + sw * (x_vec[sv == 0 ? 0 : 1] - 0.5);
157 = (sv == 0 && sw == 0) ? 0 : (sv != 0 && sw != 0) ? 2 : 1;
158 const auto new_t0 = (st0 == 0) ? t0 : t0 + st0 * x_vec[idx];
159 if (y - new_t0 <= 0) {
162 const auto dist = GradT ? 0
163 : wiener4_distribution<true>(
164 y - new_t0, a, new_v, new_w, lerr);
165 const auto temp2 = (sv == 0) ? 0
169 const auto factor_sv = GradSV ? factor : 1;
171 = GradSW ? ((sv == 0) ? (x_vec[0] - 0.5) : (x_vec[1] - 0.5)) : 1;
176 ? conditionally_grad_sw_cdf<Conditionally_cdf>(
177 wiener7_functor, y - new_t0, a, v, new_w, sv, sw,
179 : factor_sv * factor_sw
180 * conditionally_grad_sw_cdf<Conditionally_cdf>(
181 wiener7_functor, y - new_t0, a, new_v,
182 new_w, dist, sw, lerr);
183 return ret_t(integrand *
exp(temp2));
185 integration_args...);
188 return estimate_with_err_check<0, 8, GradW7, GradientCalc::ON>(
189 functor, hcubature_err, args...);
260template <
bool propto =
false,
typename T_y,
typename T_a,
typename T_t0,
261 typename T_w,
typename T_v,
typename T_sv,
typename T_sw,
264 const T_w& w,
const T_v& v,
const T_sv& sv,
265 const T_sw& sw,
const T_st0& st0,
266 const double& precision_derivatives = 1
e-8) {
277 using T_partials_return
284 T_t0_ref t0_ref = t0;
285 T_sv_ref sv_ref = sv;
286 T_sw_ref sw_ref = sw;
287 T_st0_ref st0_ref = st0;
303 static constexpr const char* function_name =
"wiener_lcdf_defective";
305 "Boundary separation", a,
"Drift rate", v,
306 "A-priori bias", w,
"Nondecision time", t0,
307 "Inter-trial variability in drift rate", sv,
308 "Inter-trial variability in A-priori bias", sw,
309 "Inter-trial variability in Nondecision time", st0);
313 check_less(function_name,
"A-priori bias", w_val, 1);
316 check_finite(function_name,
"Nondecision time", t0_val);
319 check_finite(function_name,
"Inter-trial variability in drift rate", sv_val);
320 check_bounded(function_name,
"Inter-trial variability in A-priori bias",
323 "Inter-trial variability in Nondecision time", st0_val);
324 check_finite(function_name,
"Inter-trial variability in Nondecision time",
327 const size_t N =
max_size(y, a, v, w, t0, sv, sw, st0);
339 const size_t N_y_t0 =
max_size(y, t0, st0);
341 for (
size_t i = 0; i < N_y_t0; ++i) {
342 if (y_vec[i] <= t0_vec[i]) {
343 std::stringstream msg;
344 msg <<
", but must be greater than nondecision time = " << t0_vec[i];
345 std::string msg_str(msg.str());
351 for (
size_t i = 0; i < N_beta_sw; ++i) {
352 if (
unlikely(w_vec[i] - .5 * sw_vec[i] <= 0)) {
353 std::stringstream msg;
354 msg <<
", but must be smaller than 2*(A-priori bias) = "
356 std::string msg_str(msg.str());
358 "Inter-trial variability in A-priori bias", sw_vec[i],
359 " = ", msg_str.c_str());
361 if (
unlikely(w_vec[i] + .5 * sw_vec[i] >= 1)) {
362 std::stringstream msg;
363 msg <<
", but must be smaller than 2*(1-A-priori bias) = "
364 << 2 * (1 - w_vec[i]);
365 std::string msg_str(msg.str());
367 "Inter-trial variability in A-priori bias", sw_vec[i],
368 " = ", msg_str.c_str());
373 const T_partials_return log_error_cdf =
log(1
e-6);
374 const auto error_bound = precision_derivatives;
376 const auto lerror_bound =
log(error_bound);
377 const T_partials_return absolute_error_hcubature = 0.0;
378 const T_partials_return relative_error_hcubature
380 const T_partials_return log_error_absolute =
log(1
e-12);
381 const int maximal_evaluations_hcubature = 6000;
382 T_partials_return lcdf = 0.0;
384 v_ref, sv_ref, sw_ref, st0_ref);
388 for (
size_t i = 0; i < N; i++) {
389 if (sv_vec[i] == 0 && sw_vec[i] == 0 && st0_vec[i] == 0) {
390 result += wiener_lcdf_defective<propto>(y_vec[i], a_vec[i], t0_vec[i],
392 precision_derivatives);
395 const T_partials_return y_value = y_vec.val(i);
396 const T_partials_return a_value = a_vec.val(i);
397 const T_partials_return v_value = v_vec.val(i);
398 const T_partials_return w_value = w_vec.val(i);
399 const T_partials_return t0_value = t0_vec.val(i);
400 const T_partials_return sv_value = sv_vec.val(i);
401 const T_partials_return sw_value = sw_vec.val(i);
402 const T_partials_return st0_value = st0_vec.val(i);
403 const int dim = (sv_value != 0) + (sw_value != 0) + (st0_value != 0);
405 "(Inter-trial variability in drift rate) + "
406 "(Inter-trial variability in A-priori bias) + "
407 "(Inter-trial variability in nondecision time)",
410 Eigen::Matrix<T_partials_return, -1, 1> xmin = Eigen::VectorXd::Zero(dim);
411 Eigen::Matrix<T_partials_return, -1, 1> xmax = Eigen::VectorXd::Ones(dim);
412 for (
int i = 0; i < dim; i++) {
420 if (st0_value != 0) {
421 xmax[dim - 1] =
fmin(1.0, (y_value - t0_value) / st0_value);
424 T_partials_return hcubature_err
425 = log_error_absolute - log_error_cdf +
LOG_TWO + 1;
427 const auto params = std::make_tuple(y_value, a_value, v_value, w_value,
428 t0_value, sv_value, sw_value, st0_value,
431 const T_partials_return cdf
433 GradientCalc::OFF, GradientCalc::OFF,
434 GradientCalc::OFF, GradientCalc::OFF>(
435 [&](
auto&&... args) {
436 return internal::wiener4_distribution<true>(args...);
438 hcubature_err, params, dim, xmin, xmax,
439 maximal_evaluations_hcubature, absolute_error_hcubature,
440 relative_error_hcubature / 2);
444 = log_error_absolute - lerror_bound +
log(
fabs(cdf)) +
LOG_TWO + 1;
450 T_partials_return deriv_t_7
452 GradientCalc::OFF, GradientCalc::OFF, GradientCalc::OFF,
453 GradientCalc::OFF, GradientCalc::ON>(
454 [&](
auto&&... args) {
455 return internal::wiener5_density<GradientCalc::ON>(args...);
457 hcubature_err, params, dim, xmin, xmax,
458 maximal_evaluations_hcubature, absolute_error_hcubature,
459 relative_error_hcubature / 2)
462 partials<0>(ops_partials)[i] = deriv_t_7;
465 partials<2>(ops_partials)[i] = -deriv_t_7;
468 T_partials_return deriv;
470 partials<1>(ops_partials)[i]
472 [&](
auto&&... args) {
475 hcubature_err, params, dim, xmin, xmax,
476 maximal_evaluations_hcubature, absolute_error_hcubature,
477 relative_error_hcubature / 2)
481 partials<3>(ops_partials)[i]
484 [&](
auto&&... args) {
487 hcubature_err, params, dim, xmin, xmax,
488 maximal_evaluations_hcubature, absolute_error_hcubature,
489 relative_error_hcubature / 2)
493 partials<4>(ops_partials)[i]
495 [&](
auto&&... args) {
498 hcubature_err, params, dim, xmin, xmax,
499 maximal_evaluations_hcubature, absolute_error_hcubature,
500 relative_error_hcubature / 2)
505 partials<5>(ops_partials)[i] = 0;
507 partials<5>(ops_partials)[i]
509 GradientCalc::OFF, GradientCalc::OFF, GradientCalc::ON>(
510 [&](
auto&&... args) {
513 hcubature_err, params, dim, xmin, xmax,
514 maximal_evaluations_hcubature, absolute_error_hcubature,
515 relative_error_hcubature / 2)
521 partials<6>(ops_partials)[i] = 0;
523 if (st0_value == 0 && sv_value == 0) {
529 hcubature_err, y_value - t0_value, a_value, v_value, w_value,
530 sw_value, log_error_absolute -
LOG_TWO);
531 deriv = deriv / cdf - 1 / sw_value;
534 GradientCalc::OFF, GradientCalc::OFF, GradientCalc::OFF,
536 [&](
auto&&... args) {
539 hcubature_err, params, dim, xmin, xmax,
540 maximal_evaluations_hcubature, absolute_error_hcubature,
541 relative_error_hcubature / 2)
544 partials<6>(ops_partials)[i] = deriv;
548 if (st0_value == 0) {
549 partials<7>(ops_partials)[i] = 0;
550 }
else if (y_value - (t0_value + st0_value) <= 0) {
551 partials<7>(ops_partials)[i] = -1 / st0_value;
553 const T_partials_return t0_st0 = t0_value + st0_value;
554 if (sw_value == 0 && sv_value == 0) {
558 return internal::wiener4_distribution<GradientCalc::ON>(
561 lerror_bound +
log(st0_value), y_value - t0_st0, a_value, v_value,
562 w_value, log_error_absolute -
LOG_TWO);
563 deriv = deriv / st0_value / cdf - 1 / st0_value;
565 const int dim_st = (sv_value != 0) + (sw_value != 0);
566 const T_partials_return new_error = log_error_absolute -
LOG_TWO;
567 const auto& params_st
568 = std::make_tuple(y_value, a_value, v_value, w_value, t0_st0,
569 sv_value, sw_value, 0, new_error);
571 GradientCalc::OFF, GradientCalc::OFF, GradientCalc::OFF,
572 GradientCalc::OFF, GradientCalc::OFF, GradientCalc::OFF>(
573 [&](
auto&&... args) {
574 return internal::wiener4_distribution<GradientCalc::ON>(
577 hcubature_err, params_st, dim_st, xmin, xmax,
578 maximal_evaluations_hcubature, absolute_error_hcubature,
579 relative_error_hcubature / 2);
580 deriv = deriv / st0_value / cdf - 1 / st0_value;
582 partials<7>(ops_partials)[i] = deriv;
586 return result + ops_partials.build(lcdf);
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
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_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 wiener7_cdf_grad_sw(const T_y &y, const T_a &a, const T_v &v, const T_w &w, const T_sw &sw, T_err log_error)
Calculate the derivative of the wiener7 density w.r.t.
auto conditionally_grad_sw_cdf(F &&functor, T_y &&y_diff, T_a &&a, T_v &&v, T_w &&w, T_sv &&sv, T_sw &&sw, T_err log_error)
Helper function for agnostically calling wiener4 functions (to be integrated over) or directly callin...
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 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 wiener7_integrate_cdf(const Wiener7FunctorT &wiener7_functor, T_err &&hcubature_err, TArgs &&... args)
Implementation function for preparing arguments and functor to be passed to the hcubature() function ...
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
fvar< T > fmin(const fvar< T > &x1, const fvar< T > &x2)
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.
static constexpr double e()
Return the base of the natural logarithm.
auto hcubature(const F &integrand, const ParsTuple &pars, const int dim, const Eigen::Matrix< T_a, Eigen::Dynamic, 1 > &a, const Eigen::Matrix< T_b, Eigen::Dynamic, 1 > &b, const int max_eval, const TAbsErr reqAbsError, const TRelErr reqRelError)
Compute the [dim]-dimensional integral of the function from to within specified relative and absol...
fvar< T > log(const fvar< T > &x)
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.
static constexpr double LOG_SQRT_PI
The natural logarithm of the square root of , .
fvar< T > log1p(const fvar< T > &x)
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.
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
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.
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.
fvar< T > fabs(const fvar< T > &x)
fvar< T > square(const fvar< T > &x)
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...