1#ifndef STAN_MATH_PRIM_PROB_WIENER4_LCCDF_DEFECTIVE_HPP
2#define STAN_MATH_PRIM_PROB_WIENER4_LCCDF_DEFECTIVE_HPP
22template <
typename T_a,
typename T_w,
typename T_v>
26 const auto neg_v = -v;
27 const auto one_m_w = 1.0 - w;
31 const auto exponent = 2.0 * v * a * w;
54template <
typename T_a,
typename T_w,
typename T_v>
56 const T_w& w)
noexcept {
58 const auto exponent_m1 =
log1m(1.1 * 1.0e-8);
59 const auto neg_v = -v;
60 const auto one_m_w = 1 - w;
61 int sign_v = neg_v < 0 ? 1 : -1;
62 const auto two_a_neg_v = 2.0 * a * neg_v;
63 const auto exponent_with_1mw = sign_v * two_a_neg_v * w;
64 const auto exponent = sign_v * two_a_neg_v;
65 const auto exponent_with_w = two_a_neg_v * one_m_w;
67 if (
unlikely((exponent_with_1mw >= exponent_m1)
68 || ((exponent_with_w >= exponent_m1) && (sign_v == 1))
69 || (exponent >= exponent_m1) || neg_v == 0)) {
70 return ret_t(-one_m_w);
74 const auto log_w =
log(one_m_w);
82 if (log_w > diff_term) {
104template <
typename T_y,
typename T_a,
typename T_w,
typename T_v,
106inline auto wiener4_ccdf(
const T_y& y,
const T_a& a,
const T_v& v,
const T_w& w,
107 T_err log_err =
log(1
e-12)) noexcept {
110 = internal::wiener4_distribution<GradientCalc::ON>(y, a, v, w, log_err);
111 return prob_hit_upper - cdf;
126template <
typename T_y,
typename T_a,
typename T_w,
typename T_v,
127 typename T_cdf,
typename T_err>
129 const T_w& w, T_cdf&& cdf,
130 T_err log_err =
log(1
e-12)) noexcept {
140 return prob_grad_a *
exp(log_prob_hit_upper) - cdf_grad_a;
155template <
typename T_y,
typename T_a,
typename T_w,
typename T_v,
156 typename T_cdf,
typename T_err>
158 const T_w& w, T_cdf&& cdf,
159 T_err log_err =
log(1
e-12)) noexcept {
169 return prob_grad_v *
exp(log_prob_hit_upper) - cdf_grad_v;
184template <
typename T_y,
typename T_a,
typename T_w,
typename T_v,
185 typename T_cdf,
typename T_err>
187 const T_w& w, T_cdf&& cdf,
188 T_err log_err =
log(1
e-12)) noexcept {
192 const auto exponent = -
sign(v) * 2.0 * v * a * w;
197 prob_grad_w *=
exp(exponent);
201 return prob_grad_w *
exp(log_prob_hit_upper) - cdf_grad_w;
225template <
bool propto =
false,
typename T_y,
typename T_a,
typename T_t0,
226 typename T_w,
typename T_v>
228 const T_w& w,
const T_v& v,
229 const double& precision_derivatives = 1
e-4) {
241 T_t0_ref t0_ref = t0;
251 static constexpr const char* function_name =
"wiener4_lccdf";
261 "Boundary separation", a,
"Drift rate", v,
262 "A-priori bias", w,
"Nondecision time", t0);
266 check_less(function_name,
"A-priori bias", w_val, 1);
269 check_finite(function_name,
"Nondecision time", t0_val);
271 const size_t N =
max_size(y, a, t0, w, v);
278 const size_t N_y_t0 =
max_size(y, t0);
280 for (
size_t i = 0; i < N_y_t0; ++i) {
281 if (y_vec[i] <= t0_vec[i]) {
282 std::stringstream msg;
283 msg <<
", but must be greater than nondecision time = " << t0_vec[i];
284 std::string msg_str(msg.str());
291 const auto log_error_cdf =
log(1
e-6);
292 const auto log_error_derivative =
log(precision_derivatives);
293 const T_partials_return log_error_absolute =
log(1
e-12);
294 T_partials_return lccdf = 0.0;
298 const double LOG_FOUR = std::log(4.0);
301 for (
size_t i = 0; i < N; i++) {
302 const auto y_value = y_vec.val(i);
303 const auto a_value = a_vec.val(i);
304 const auto t0_value = t0_vec.val(i);
305 const auto w_value = w_vec.val(i);
306 const auto v_value = v_vec.val(i);
308 const T_partials_return cdf
312 return internal::wiener4_distribution<GradientCalc::ON>(args...);
314 log_error_cdf -
LOG_TWO, y_value - t0_value, a_value, v_value,
315 w_value, log_error_absolute);
317 const auto prob_hit_upper
319 const auto ccdf = prob_hit_upper - cdf;
320 const auto log_ccdf_single_value =
log(ccdf);
322 lccdf += log_ccdf_single_value;
324 const auto new_est_err
325 = log_ccdf_single_value + log_error_derivative - LOG_FOUR;
328 const auto deriv_y = internal::estimate_with_err_check<5, 0>(
330 return internal::wiener5_density<GradientCalc::ON>(args...);
332 new_est_err, y_value - t0_value, a_value, v_value, w_value, 0.0,
335 partials<0>(ops_partials)[i] = -deriv_y / ccdf;
338 partials<2>(ops_partials)[i] = deriv_y / ccdf;
342 partials<1>(ops_partials)[i]
343 = internal::estimate_with_err_check<5, 0>(
347 new_est_err, y_value - t0_value, a_value, v_value, w_value, cdf,
352 partials<3>(ops_partials)[i]
353 = internal::estimate_with_err_check<5, 0>(
357 new_est_err, y_value - t0_value, a_value, v_value, w_value, cdf,
362 partials<4>(ops_partials)[i]
364 w_value, cdf, log_error_absolute)
368 return ops_partials.build(lccdf);
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 wiener_prob_derivative_term(const T_a &a, const T_v &v, const T_w &w) noexcept
Calculate parts of the partial derivatives for wiener_prob_grad_a and wiener_prob_grad_v (on log-scal...
auto wiener4_ccdf_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)) noexcept
Calculate derivative of the wiener4 ccdf 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 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 log_wiener_prob_hit_upper(const T_a &a, const T_v &v, const T_w &w)
Log of probability of reaching the upper bound in diffusion process.
auto wiener4_ccdf(const T_y &y, const T_a &a, const T_v &v, const T_w &w, T_err log_err=log(1e-12)) noexcept
Calculate wiener4 ccdf (natural-scale)
auto wiener4_ccdf_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)) noexcept
Calculate derivative of the wiener4 ccdf w.r.t.
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 wiener4_ccdf_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)) noexcept
Calculate derivative of the wiener4 ccdf w.r.t.
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.
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 sign(const T &x)
Returns signs of the arguments.
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.
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...
bool is_scal_finite(const T_y &y)
Return true if y is finite.
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
fvar< T > log_diff_exp(const fvar< T > &x1, const fvar< T > &x2)
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)
auto wiener_lccdf_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-CCDF for the 4-parameter Wiener distribution.
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 > 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...