1#ifndef STAN_MATH_PRIM_PROB_WIENER_FULL_LCCDF_DEFECTIVE_HPP
2#define STAN_MATH_PRIM_PROB_WIENER_FULL_LCCDF_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,
36 auto low =
fmax(0.0, w - sw / 2.0);
37 auto high =
fmin(1.0, w + sw / 2.0);
39 const auto lower_value =
wiener4_ccdf(y, a, v, low, log_error);
40 const auto upper_value =
wiener4_ccdf(y, a, v, high, log_error);
41 return 0.5 * (lower_value + upper_value) / sw;
84template <
bool propto =
false,
typename T_y,
typename T_a,
typename T_t0,
85 typename T_w,
typename T_v,
typename T_sv,
typename T_sw,
88 const T_w& w,
const T_v& v,
const T_sv& sv,
89 const T_sw& sw,
const T_st0& st0,
90 const double& precision_derivatives = 1
e-8) {
101 using T_partials_return
108 T_t0_ref t0_ref = t0;
109 T_sv_ref sv_ref = sv;
110 T_sw_ref sw_ref = sw;
111 T_st0_ref st0_ref = st0;
127 static constexpr const char* function_name =
"wiener_lccdf_defective";
129 "Boundary separation", a,
"Drift rate", v,
130 "A-priori bias", w,
"Nondecision time", t0,
131 "Inter-trial variability in drift rate", sv,
132 "Inter-trial variability in A-priori bias", sw,
133 "Inter-trial variability in Nondecision time", st0);
137 check_less(function_name,
"A-priori bias", w_val, 1);
140 check_finite(function_name,
"Nondecision time", t0_val);
143 check_finite(function_name,
"Inter-trial variability in drift rate", sv_val);
144 check_bounded(function_name,
"Inter-trial variability in A-priori bias",
147 "Inter-trial variability in Nondecision time", st0_val);
148 check_finite(function_name,
"Inter-trial variability in Nondecision time",
151 const size_t N =
max_size(y, a, v, w, t0, sv, sw, st0);
163 const size_t N_y_t0 =
max_size(y, t0, st0);
165 for (
size_t i = 0; i < N_y_t0; ++i) {
166 if (y_vec[i] <= t0_vec[i]) {
167 std::stringstream msg;
168 msg <<
", but must be greater than nondecision time = " << t0_vec[i];
169 std::string msg_str(msg.str());
175 for (
size_t i = 0; i < N_beta_sw; ++i) {
176 if (
unlikely(w_vec[i] - .5 * sw_vec[i] <= 0)) {
177 std::stringstream msg;
178 msg <<
", but must be smaller than 2*(A-priori bias) = "
180 std::string msg_str(msg.str());
182 "Inter-trial variability in A-priori bias", sw_vec[i],
183 " = ", msg_str.c_str());
185 if (
unlikely(w_vec[i] + .5 * sw_vec[i] >= 1)) {
186 std::stringstream msg;
187 msg <<
", but must be smaller than 2*(1-A-priori bias) = "
188 << 2.0 * (1.0 - w_vec[i]);
189 std::string msg_str(msg.str());
191 "Inter-trial variability in A-priori bias", sw_vec[i],
192 " = ", msg_str.c_str());
197 const T_partials_return log_error_cdf =
log(1
e-6);
198 const auto error_bound = precision_derivatives;
200 const auto log_error_derivatives =
log(error_bound);
201 const T_partials_return absolute_error_hcubature = 0.0;
202 const T_partials_return relative_error_hcubature
204 const T_partials_return log_error_absolute =
log(1
e-12);
205 const int maximal_evaluations_hcubature = 6000;
206 T_partials_return lccdf = 0.0;
208 v_ref, sv_ref, sw_ref, st0_ref);
212 for (
size_t i = 0; i < N; i++) {
213 if (sv_vec[i] == 0 && sw_vec[i] == 0 && st0_vec[i] == 0) {
214 result += wiener_lccdf_defective<propto>(y_vec[i], a_vec[i], t0_vec[i],
216 precision_derivatives);
219 const T_partials_return y_value = y_vec.val(i);
220 const T_partials_return a_value = a_vec.val(i);
221 const T_partials_return v_value = v_vec.val(i);
222 const T_partials_return w_value = w_vec.val(i);
223 const T_partials_return t0_value = t0_vec.val(i);
224 const T_partials_return sv_value = sv_vec.val(i);
225 const T_partials_return sw_value = sw_vec.val(i);
226 const T_partials_return st0_value = st0_vec.val(i);
227 const int dim = (sv_value != 0) + (sw_value != 0) + (st0_value != 0);
229 "(Inter-trial variability in drift rate) + "
230 "(Inter-trial variability in A-priori bias) + "
231 "(Inter-trial variability in nondecision time)",
234 Eigen::Matrix<T_partials_return, -1, 1> xmin = Eigen::VectorXd::Zero(dim);
235 Eigen::Matrix<T_partials_return, -1, 1> xmax = Eigen::VectorXd::Ones(dim);
236 for (
int i = 0; i < dim; i++) {
244 if (st0_value != 0) {
245 xmax[dim - 1] =
fmin(1.0, (y_value - t0_value) / st0_value);
248 T_partials_return hcubature_err
249 = log_error_absolute - log_error_cdf +
LOG_TWO + 1;
251 const auto params = std::make_tuple(y_value, a_value, v_value, w_value,
252 t0_value, sv_value, sw_value, st0_value,
255 const T_partials_return ccdf
257 GradientCalc::OFF, GradientCalc::OFF,
258 GradientCalc::OFF, GradientCalc::OFF>(
260 hcubature_err, params, dim, xmin, xmax,
261 maximal_evaluations_hcubature, absolute_error_hcubature,
262 relative_error_hcubature / 2);
265 hcubature_err = log_error_absolute - log_error_derivatives +
log(
fabs(ccdf))
273 const T_partials_return deriv_t_7
275 GradientCalc::OFF, GradientCalc::OFF, GradientCalc::OFF,
276 GradientCalc::OFF, GradientCalc::ON>(
277 [&](
auto&&... args) {
278 return internal::wiener5_density<GradientCalc::ON>(args...);
280 hcubature_err, params, dim, xmin, xmax,
281 maximal_evaluations_hcubature, absolute_error_hcubature,
282 relative_error_hcubature / 2)
285 partials<0>(ops_partials)[i] = deriv_t_7;
288 partials<2>(ops_partials)[i] = -deriv_t_7;
291 T_partials_return deriv;
293 partials<1>(ops_partials)[i]
295 [&](
auto&&... args) {
298 hcubature_err, params, dim, xmin, xmax,
299 maximal_evaluations_hcubature, absolute_error_hcubature,
300 relative_error_hcubature / 2)
304 partials<3>(ops_partials)[i]
307 [&](
auto&&... args) {
310 hcubature_err, params, dim, xmin, xmax,
311 maximal_evaluations_hcubature, absolute_error_hcubature,
312 relative_error_hcubature / 2)
316 partials<4>(ops_partials)[i]
318 [&](
auto&&... args) {
321 hcubature_err, params, dim, xmin, xmax,
322 maximal_evaluations_hcubature, absolute_error_hcubature,
323 relative_error_hcubature / 2)
328 partials<5>(ops_partials)[i] = 0.0;
330 partials<5>(ops_partials)[i]
332 GradientCalc::OFF, GradientCalc::OFF, GradientCalc::ON>(
333 [&](
auto&&... args) {
336 hcubature_err, params, dim, xmin, xmax,
337 maximal_evaluations_hcubature, absolute_error_hcubature,
338 relative_error_hcubature / 2)
344 partials<6>(ops_partials)[i] = 0.0;
346 if (st0_value == 0 && sv_value == 0) {
352 hcubature_err, y_value - t0_value, a_value, v_value, w_value,
353 sw_value, log_error_absolute -
LOG_TWO);
354 deriv = deriv / ccdf - 1 / sw_value;
357 GradientCalc::OFF, GradientCalc::OFF, GradientCalc::OFF,
359 [&](
auto&&... args) {
362 hcubature_err, params, dim, xmin, xmax,
363 maximal_evaluations_hcubature, absolute_error_hcubature,
364 relative_error_hcubature / 2)
367 partials<6>(ops_partials)[i] = deriv;
371 if (st0_value == 0) {
372 partials<7>(ops_partials)[i] = 0.0;
373 }
else if (y_value - (t0_value + st0_value) <= 0) {
374 partials<7>(ops_partials)[i] = -1 / st0_value;
376 const auto t0_st0 = t0_value + st0_value;
377 if (sw_value == 0 && sv_value == 0) {
378 deriv = internal::estimate_with_err_check<4, 0>(
380 log_error_derivatives +
log(st0_value), y_value - t0_st0, a_value,
381 v_value, w_value, log_error_absolute -
LOG_TWO);
382 deriv = deriv / st0_value / ccdf - 1 / st0_value;
384 const int dim_st = (sv_value != 0) + (sw_value != 0);
385 const auto new_error = log_error_absolute -
LOG_TWO;
386 const auto& params_st
387 = std::make_tuple(y_value, a_value, v_value, w_value, t0_st0,
388 sv_value, sw_value, 0.0, new_error);
390 GradientCalc::OFF, GradientCalc::OFF, GradientCalc::OFF,
391 GradientCalc::OFF, GradientCalc::OFF, GradientCalc::OFF>(
393 hcubature_err, params_st, dim_st, xmin, xmax,
394 maximal_evaluations_hcubature, absolute_error_hcubature,
395 relative_error_hcubature / 2);
396 deriv = deriv / st0_value / ccdf - 1 / st0_value;
398 partials<7>(ops_partials)[i] = deriv;
402 return result + 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_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 wiener7_ccdf_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 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_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 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 ...
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.
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.
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.
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.
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.
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)
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...