1#ifndef STAN_MATH_REV_CONSTRAINT_LUB_CONSTRAIN_HPP
2#define STAN_MATH_REV_CONSTRAINT_LUB_CONSTRAIN_HPP
33template <
typename T,
typename L,
typename U,
34 require_all_stan_scalar_t<T, L, U>* =
nullptr,
35 require_var_t<return_type_t<T, L, U>>* =
nullptr>
36inline auto lub_constrain(
const T& x,
const L& lb,
const U& ub) {
41 const bool is_ub_inf = ub_val ==
INFTY;
42 if (
unlikely(is_ub_inf && is_lb_inf)) {
49 check_less(
"lub_constrain",
"lb", lb_val, ub_val);
50 auto diff = ub_val - lb_val;
53 [x, ub, lb, diff, inv_logit_x](
auto& vi)
mutable {
54 if constexpr (is_autodiff_v<T>) {
55 x.adj() += vi.adj() *
diff * inv_logit_x
56 * (1.0 - inv_logit_x);
58 if constexpr (is_autodiff_v<L>) {
59 lb.adj() += vi.adj() * (1.0 - inv_logit_x);
61 if constexpr (is_autodiff_v<U>) {
62 ub.adj() += vi.adj() * inv_logit_x;
101template <
typename T,
typename L,
typename U,
102 require_all_stan_scalar_t<T, L, U>* =
nullptr,
103 require_var_t<return_type_t<T, L, U>>* =
nullptr>
104inline auto lub_constrain(
const T& x,
const L& lb,
const U& ub,
105 return_type_t<T, L, U>& lp) {
110 const bool is_ub_inf = ub_val ==
INFTY;
111 if (
unlikely(is_ub_inf && is_lb_inf)) {
118 check_less(
"lub_constrain",
"lb", lb_val, ub_val);
120 auto diff = ub_val - lb_val;
122 lp += (
log(diff) + (neg_abs_x - (2.0 *
log1p_exp(neg_abs_x))));
124 diff * inv_logit_x + lb_val,
125 [x, ub, lb, diff, lp, inv_logit_x](
auto& vi)
mutable {
126 if constexpr (is_autodiff_v<T>) {
127 x.adj() += vi.adj() *
diff * inv_logit_x * (1.0 - inv_logit_x)
128 + lp.adj() * (1.0 - 2.0 * inv_logit_x);
130 if constexpr (is_autodiff_v<L> && is_autodiff_v<U>) {
131 const auto one_over_diff = 1.0 /
diff;
133 += vi.adj() * (1.0 - inv_logit_x) + -one_over_diff * lp.adj();
134 ub.adj() += vi.adj() * inv_logit_x + one_over_diff * lp.adj();
135 }
else if constexpr (is_autodiff_v<L>) {
137 += vi.adj() * (1.0 - inv_logit_x) + (-1.0 / diff) * lp.adj();
138 }
else if constexpr (is_autodiff_v<U>) {
139 ub.adj() += vi.adj() * inv_logit_x + (1.0 /
diff) * lp.adj();
148template <
typename T,
typename L,
typename U, require_matrix_t<T>* =
nullptr,
149 require_all_stan_scalar_t<L, U>* =
nullptr,
150 require_var_t<return_type_t<T, L, U>>* =
nullptr>
151inline auto lub_constrain(
const T& x,
const L& lb,
const U& ub) {
153 using ret_type = return_var_matrix_t<T, T, L, U>;
157 const bool is_ub_inf = ub_val ==
INFTY;
158 if (
unlikely(is_ub_inf && is_lb_inf)) {
165 arena_t<T> arena_x = x;
166 check_less(
"lub_constrain",
"lb", lb_val, ub_val);
167 const auto diff = ub_val - lb_val;
169 arena_t<ret_type> ret =
diff * inv_logit_x + lb_val;
171 if constexpr (is_autodiff_v<T>) {
172 arena_x.adj().array()
173 += ret.adj().array() *
diff * inv_logit_x * (1.0 - inv_logit_x);
175 if constexpr (is_autodiff_v<L>) {
176 lb.adj() += (ret.adj().array() * (1.0 - inv_logit_x)).sum();
178 if constexpr (is_autodiff_v<U>) {
179 ub.adj() += (ret.adj().array() * inv_logit_x).
sum();
182 return ret_type(ret);
189template <
typename T,
typename L,
typename U, require_matrix_t<T>* =
nullptr,
190 require_all_stan_scalar_t<L, U>* =
nullptr,
191 require_var_t<return_type_t<T, L, U>>* =
nullptr>
199 const bool is_ub_inf = ub_val ==
INFTY;
200 if (
unlikely(is_ub_inf && is_lb_inf)) {
207 check_less(
"lub_constrain",
"lb", lb_val, ub_val);
210 auto diff = ub_val - lb_val;
215 [arena_x, ub, lb, ret, lp, diff, inv_logit_x]()
mutable {
216 if constexpr (is_autodiff_v<T>) {
217 arena_x.adj().array()
218 += ret.adj().array() * diff * inv_logit_x * (1.0 - inv_logit_x)
219 + lp.adj() * (1.0 - 2.0 * inv_logit_x);
221 if constexpr (is_autodiff_v<L> && is_autodiff_v<U>) {
222 const auto lp_calc = lp.adj() * ret.size();
223 const auto one_over_diff = 1.0 / diff;
224 lb.adj() += (ret.adj().array() * (1.0 - inv_logit_x)).sum()
225 + -one_over_diff * lp_calc;
226 ub.adj() += (ret.adj().array() * inv_logit_x).
sum()
227 + one_over_diff * lp_calc;
228 }
else if constexpr (is_autodiff_v<L>) {
229 lb.adj() += (ret.adj().array() * (1.0 - inv_logit_x)).sum()
230 + -(1.0 / diff) * lp.adj() * ret.size();
231 }
else if constexpr (is_autodiff_v<U>) {
232 ub.adj() += (ret.adj().array() * inv_logit_x).
sum()
233 + (1.0 / diff) * lp.adj() * ret.size();
236 return ret_type(ret);
244template <
typename T,
typename L,
typename U,
248inline auto lub_constrain(
const T& x,
const L& lb,
const U& ub) {
252 const bool is_ub_inf = ub_val ==
INFTY;
256 arena_t<T> arena_x = x;
257 arena_t<L> arena_lb = lb;
258 const auto lb_val =
value_of(arena_lb).array().eval();
259 check_less(
"lub_constrain",
"lb", lb_val, ub_val);
261 auto diff =
to_arena(ub_val - lb_val);
263 arena_t<ret_type> ret = (is_lb_inf).
select(
264 ub_val -
value_of(arena_x).array().exp(), diff * inv_logit_x + lb_val);
266 is_lb_inf]()
mutable {
267 if constexpr (is_autodiff_v<T>) {
268 arena_x.adj().array() += (is_lb_inf).
select(
269 ret.adj().array() * -
value_of(arena_x).array().exp(),
270 ret.adj().array() * diff * inv_logit_x * (1.0 - inv_logit_x));
272 if constexpr (is_autodiff_v<U>) {
275 .
select(ret.adj().array(), ret.adj().array() * inv_logit_x)
278 if constexpr (is_autodiff_v<L>) {
279 arena_lb.adj().array()
280 += (is_lb_inf).
select(0, ret.adj().array() * (1.0 - inv_logit_x));
283 return ret_type(ret);
291template <
typename T,
typename L,
typename U,
292 require_all_matrix_t<T, L>* =
nullptr,
293 require_stan_scalar_t<U>* =
nullptr,
294 require_var_t<return_type_t<T, L, U>>* =
nullptr>
300 const bool is_ub_inf = ub_val ==
INFTY;
307 const auto lb_val =
value_of(arena_lb).array().eval();
308 check_less(
"lub_constrain",
"lb", lb_val, ub_val);
310 auto diff =
to_arena(ub_val - lb_val);
311 auto neg_abs_x =
to_arena(-arena_x_val.abs());
314 diff * inv_logit_x + lb_val);
320 inv_logit_x, is_lb_inf]()
mutable {
321 const auto lp_adj = lp.adj();
322 if constexpr (is_autodiff_v<T>) {
323 const auto x_sign = arena_x_val.sign().eval();
324 arena_x.adj().array() += (is_lb_inf).
select(
325 ret.adj().array() * -arena_x_val.exp() + lp_adj,
326 ret.adj().array() * diff * inv_logit_x * (1.0 - inv_logit_x)
327 + lp.adj() * (1.0 - 2.0 * inv_logit_x));
329 if constexpr (is_autodiff_v<L>) {
330 arena_lb.adj().array()
331 += (is_lb_inf).
select(0, ret.adj().array() * (1.0 - inv_logit_x)
332 + -(1.0 / diff) * lp_adj);
334 if constexpr (is_autodiff_v<U>) {
337 .
select(ret.adj().array(), ret.adj().array() * inv_logit_x
338 + (1.0 / diff) * lp_adj)
342 return ret_type(ret);
350template <
typename T,
typename L,
typename U,
354inline auto lub_constrain(
const T& x,
const L& lb,
const U& ub) {
362 arena_t<T> arena_x = x;
364 arena_t<U> arena_ub = ub;
365 const auto ub_val =
value_of(arena_ub).array().eval();
366 check_less(
"lub_constrain",
"lb", lb_val, ub_val);
368 auto diff =
to_arena(ub_val - lb_val);
370 arena_t<ret_type> ret = (is_ub_inf).
select(
371 arena_x_val.array().exp() + lb_val, diff * inv_logit_x + lb_val);
373 inv_logit_x, diff]()
mutable {
374 if constexpr (is_autodiff_v<T>) {
375 arena_x.adj().array() += (is_ub_inf).
select(
376 ret.adj().array() * arena_x_val.array().exp(),
377 ret.adj().array() * diff * inv_logit_x * (1.0 - inv_logit_x));
379 if constexpr (is_autodiff_v<L>) {
380 lb.adj() += (is_ub_inf)
381 .
select(ret.adj().array(),
382 ret.adj().array() * (1.0 - inv_logit_x))
385 if constexpr (is_autodiff_v<U>) {
386 arena_ub.adj().array()
387 += (is_ub_inf).
select(0, ret.adj().array() * inv_logit_x);
390 return ret_type(ret);
398template <
typename T,
typename L,
typename U,
399 require_all_matrix_t<T, U>* =
nullptr,
400 require_stan_scalar_t<L>* =
nullptr,
401 require_var_t<return_type_t<T, L, U>>* =
nullptr>
402inline auto lub_constrain(
const T& x,
const L& lb,
const U& ub,
403 std::decay_t<return_type_t<T, L, U>>& lp) {
405 using ret_type = return_var_matrix_t<T, T, L, U>;
411 arena_t<T> arena_x = x;
413 arena_t<U> arena_ub = ub;
415 check_less(
"lub_constrain",
"lb", lb_val, ub_val);
418 auto neg_abs_x =
to_arena(-(arena_x_val.array()).abs());
420 .
select(arena_x_val.array(),
424 arena_t<ret_type> ret = (is_ub_inf).
select(
425 arena_x_val.array().exp() + lb_val,
diff * inv_logit_x + lb_val);
427 lb, ret, lp, is_ub_inf]()
mutable {
428 const auto lp_adj = lp.adj();
429 if constexpr (is_autodiff_v<T>) {
430 arena_x.adj().array() += (is_ub_inf).
select(
431 ret.adj().array() * arena_x_val.array().exp() + lp_adj,
432 ret.adj().array() *
diff * inv_logit_x * (1.0 - inv_logit_x)
433 + lp.adj() * (1.0 - 2.0 * inv_logit_x));
435 if constexpr (is_autodiff_v<L>) {
436 lb.adj() += (is_ub_inf)
437 .
select(ret.adj().array(),
438 ret.adj().array() * (1.0 - inv_logit_x)
439 + -(1.0 / diff) * lp_adj)
442 if constexpr (is_autodiff_v<U>) {
443 arena_ub.adj().array() += (is_ub_inf).
select(
444 0, ret.adj().array() * inv_logit_x + (1.0 /
diff) * lp_adj);
447 return ret_type(ret);
454template <
typename T,
typename L,
typename U,
455 require_all_matrix_t<T, L, U>* =
nullptr,
456 require_var_t<return_type_t<T, L, U>>* =
nullptr>
457inline auto lub_constrain(
const T& x,
const L& lb,
const U& ub) {
459 using ret_type = return_var_matrix_t<T, T, L, U>;
460 arena_t<T> arena_x = x;
461 auto arena_x_val =
value_of(arena_x);
462 arena_t<L> arena_lb = lb;
463 arena_t<U> arena_ub = ub;
464 auto lb_val =
value_of(arena_lb).array();
465 auto ub_val =
value_of(arena_ub).array();
466 check_less(
"lub_constrain",
"lb", lb_val, ub_val);
470 auto is_lb_ub_inf =
to_arena(is_lb_inf && is_ub_inf);
473 arena_t<ret_type> ret
475 .
select(arena_x_val.array(),
477 ub_val - arena_x.val().array().exp(),
478 (is_ub_inf).
select(arena_x_val.array().exp() + lb_val,
479 diff * inv_logit_x + lb_val)));
481 diff, ret, is_ub_inf, is_lb_inf,
482 is_lb_ub_inf]()
mutable {
484 const bool is_none_inf = !(is_lb_inf.any() || is_ub_inf.any());
486 if constexpr (is_autodiff_v<T>) {
487 arena_x.adj().array()
488 += ret.adj().array() *
diff * inv_logit_x * (1.0 - inv_logit_x);
490 if constexpr (is_autodiff_v<L>) {
491 arena_lb.adj().array() += ret.adj().array() * (1.0 - inv_logit_x);
493 if constexpr (is_autodiff_v<U>) {
494 arena_ub.adj().array() += ret.adj().array() * inv_logit_x;
497 if constexpr (is_autodiff_v<T>) {
498 arena_x.adj().array()
503 ret.adj().array() * -arena_x_val.array().exp(),
505 ret.adj().array() * arena_x_val.array().exp(),
506 ret.adj().array() *
diff * inv_logit_x
507 * (1.0 - inv_logit_x))));
509 if constexpr (is_autodiff_v<U>) {
510 arena_ub.adj().array() += (is_ub_inf).
select(
511 0, (is_lb_inf).select(ret.adj().array(),
512 ret.adj().array() * inv_logit_x));
514 if constexpr (is_autodiff_v<L>) {
515 arena_lb.adj().array() += (is_lb_inf).
select(
516 0, (is_ub_inf).select(ret.adj().array(),
517 ret.adj().array() * (1.0 - inv_logit_x)));
521 return ret_type(ret);
527template <
typename T,
typename L,
typename U,
528 require_all_matrix_t<T, L, U>* =
nullptr,
529 require_var_t<return_type_t<T, L, U>>* =
nullptr>
530inline auto lub_constrain(
const T& x,
const L& lb,
const U& ub,
531 return_type_t<T, L, U>& lp) {
533 using ret_type = return_var_matrix_t<T, T, L, U>;
534 arena_t<T> arena_x = x;
535 auto arena_x_val =
value_of(arena_x);
536 arena_t<L> arena_lb = lb;
537 arena_t<U> arena_ub = ub;
538 auto lb_val =
value_of(arena_lb).array();
539 auto ub_val =
value_of(arena_ub).array();
540 check_less(
"lub_constrain",
"lb", lb_val, ub_val);
544 auto is_lb_ub_inf =
to_arena(is_lb_inf && is_ub_inf);
547 arena_t<ret_type> ret
549 .
select(arena_x_val.array(),
551 ub_val - arena_x.val().array().exp(),
552 (is_ub_inf).
select(arena_x_val.array().exp() + lb_val,
553 diff * inv_logit_x + lb_val)));
554 auto neg_abs_x =
to_arena(-(arena_x_val.array()).abs());
558 (is_lb_inf || is_ub_inf)
561 log(diff) + (neg_abs_x - (2.0 *
log1p_exp(neg_abs_x)))))
564 diff, ret, is_ub_inf, is_lb_inf, is_lb_ub_inf,
566 const auto lp_adj = lp.adj();
568 const bool is_none_inf = !(is_lb_inf.any() || is_ub_inf.any());
570 if constexpr (is_autodiff_v<T>) {
571 arena_x.adj().array()
572 += ret.adj().array() *
diff * inv_logit_x * (1.0 - inv_logit_x)
573 + lp.adj() * (1.0 - 2.0 * inv_logit_x);
575 if constexpr (is_autodiff_v<L>) {
576 arena_lb.adj().array()
577 += ret.adj().array() * (1.0 - inv_logit_x) + -(1.0 / diff) * lp_adj;
579 if constexpr (is_autodiff_v<U>) {
580 arena_ub.adj().array()
581 += ret.adj().array() * inv_logit_x + (1.0 /
diff) * lp_adj;
584 if constexpr (is_autodiff_v<T>) {
585 arena_x.adj().array()
590 ret.adj().array() * -arena_x_val.array().exp()
593 ret.adj().array() * arena_x_val.array().exp()
595 ret.adj().array() *
diff * inv_logit_x
596 * (1.0 - inv_logit_x)
597 + lp.adj() * (1.0 - 2.0 * inv_logit_x))));
599 if constexpr (is_autodiff_v<L>) {
600 arena_lb.adj().array() += (is_lb_inf).
select(
601 0, (is_ub_inf).select(ret.adj().array(),
602 ret.adj().array() * (1.0 - inv_logit_x)
603 + -(1.0 / diff) * lp_adj));
605 if constexpr (is_autodiff_v<U>) {
606 arena_ub.adj().array() += (is_ub_inf).
select(
607 0, (is_lb_inf).select(
609 ret.adj().array() * inv_logit_x + (1.0 /
diff) * lp_adj));
613 return ret_type(ret);
require_all_t< is_matrix< std::decay_t< Types > >... > require_all_matrix_t
Require all of the types satisfy is_matrix.
select_< as_operation_cl_t< T_condition >, as_operation_cl_t< T_then >, as_operation_cl_t< T_else > > select(T_condition &&condition, T_then &&then, T_else &&els)
Selection operation on kernel generator expressions.
require_t< is_stan_scalar< std::decay_t< T > > > require_stan_scalar_t
Require type satisfies is_stan_scalar.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
require_t< is_var< std::decay_t< T > > > require_var_t
Require type satisfies is_var.
auto diff(F &&f, Theta &&theta, const Eigen::Index hessian_block_size, Stream *msgs, Args &&... args)
Computes theta gradient and negative block diagonal Hessian of f wrt theta and args....
fvar< T > abs(const fvar< T > &x)
var_value< plain_type_t< T > > make_callback_var(T &&value, F &&functor)
Creates a new var initialized with a callback_vari with a given value and reverse-pass callback funct...
void reverse_pass_callback(F &&functor)
Puts a callback on the autodiff stack to be called in reverse pass.
T eval(T &&arg)
Inputs which have a plain_type equal to the own time are forwarded unmodified (for Eigen expressions ...
T value_of(const fvar< T > &v)
Return the value of the specified variable.
fvar< T > log(const fvar< T > &x)
auto inv_logit(T &&x)
Returns the inverse logit function applied to the argument.
static constexpr double NEGATIVE_INFTY
Negative infinity.
fvar< T > log1p_exp(const fvar< T > &x)
arena_t< T > to_arena(const T &a)
Converts given argument into a type that either has any dynamic allocation on AD stack or schedules i...
auto lb_constrain(T &&x, L &&lb)
Return the lower-bounded value for the specified unconstrained input and specified lower bound.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
auto ub_constrain(T &&x, U &&ub)
Return the upper-bounded value for the specified unconstrained matrix and upper bound.
matrix_cl< double > lub_constrain(const T &x, const L &lb, const U &ub)
Return the lower and upper-bounded matrix derived by transforming the specified free matrix given the...
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.
auto identity_constrain(T &&x, Types &&...)
Returns the result of applying the identity constraint transform to the input.
static constexpr double INFTY
Positive infinity.
typename internal::arena_type_impl< std::decay_t< T > >::type arena_t
Determines a type that can be used in place of T that does any dynamic allocations on the AD stack.
std::conditional_t< is_any_var_matrix< ReturnType, Types... >::value, stan::math::var_value< stan::math::promote_scalar_t< double, plain_type_t< ReturnType > > >, stan::math::promote_scalar_t< stan::math::var_value< double >, plain_type_t< ReturnType > > > return_var_matrix_t
Given an Eigen type and several inputs, determine if a matrix should be var<Matrix> or Matrix<var>.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...