Automatic Differentiation
 
Loading...
Searching...
No Matches
lub_constrain.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_REV_CONSTRAINT_LUB_CONSTRAIN_HPP
2#define STAN_MATH_OPENCL_REV_CONSTRAINT_LUB_CONSTRAIN_HPP
3#ifdef STAN_OPENCL
4
15
16namespace stan {
17namespace math {
18
37template <
38 typename T_x, typename T_lb, typename T_ub,
39 require_all_prim_or_rev_kernel_expression_t<T_x, T_lb, T_ub>* = nullptr,
40 require_any_var_t<T_x, T_lb, T_ub>* = nullptr,
41 require_any_not_stan_scalar_t<T_x, T_lb, T_ub>* = nullptr>
42inline var_value<matrix_cl<double>> lub_constrain(T_x&& x, T_lb&& lb,
43 T_ub&& ub) {
44 arena_t<T_x> x_arena = std::forward<T_x>(x);
45 arena_t<T_lb> lb_arena = std::forward<T_lb>(lb);
46 arena_t<T_ub> ub_arena = std::forward<T_ub>(ub);
47
48 return make_callback_var(
49 lub_constrain(value_of(x_arena), value_of(lb_arena), value_of(ub_arena)),
50 [x_arena, lb_arena,
51 ub_arena](vari_value<matrix_cl<double>>& res) mutable {
52 auto lb_inf = value_of(lb_arena) == NEGATIVE_INFTY;
53 auto ub_inf = value_of(ub_arena) == INFTY;
54 auto inv_logit_x = inv_logit(value_of(x_arena));
55 auto one_m_inv_logit_x = 1.0 - inv_logit_x;
56 auto exp_x = exp(value_of(x_arena));
57 auto res_adj_exp_x = elt_multiply(res.adj(), exp_x);
58 adjoint_results(x_arena, lb_arena, ub_arena) += expressions(
59 select(lb_inf, select(ub_inf, res.adj(), -res_adj_exp_x),
60 select(ub_inf, res_adj_exp_x,
62 elt_multiply(res.adj(), (value_of(ub_arena)
63 - value_of(lb_arena))),
64 elt_multiply(inv_logit_x, one_m_inv_logit_x)))),
65 select(lb_inf, 0,
66 select(ub_inf, res.adj(),
67 elt_multiply(res.adj(), one_m_inv_logit_x))),
68 select(ub_inf, 0,
69 select(lb_inf, res.adj(),
70 elt_multiply(res.adj(), inv_logit_x))));
71 });
72}
73
93template <
94 typename T_x, typename T_lb, typename T_ub,
98inline var_value<matrix_cl<double>> lub_constrain(T_x&& x, T_lb&& lb, T_ub&& ub,
99 var& lp) {
100 arena_t<T_x> x_arena = std::forward<T_x>(x);
101 arena_t<T_lb> lb_arena = std::forward<T_lb>(lb);
102 arena_t<T_ub> ub_arena = std::forward<T_ub>(ub);
103
104 double lp_inc = 0;
105 matrix_cl<double> res = lub_constrain(value_of(x_arena), value_of(lb_arena),
106 value_of(ub_arena), lp_inc);
107 lp += lp_inc;
108
109 return make_callback_var(
110 std::move(res), [x_arena, lb_arena, ub_arena,
111 lp](vari_value<matrix_cl<double>>& res) mutable {
112 auto lb_inf = value_of(lb_arena) == NEGATIVE_INFTY;
113 auto ub_inf = value_of(ub_arena) == INFTY;
114 auto inv_logit_x = inv_logit(value_of(x_arena));
115 auto one_m_inv_logit_x = 1.0 - inv_logit_x;
116 auto diff = value_of(ub_arena) - value_of(lb_arena);
117 auto one_over_diff = elt_divide(1.0, diff);
118 auto exp_x = exp(value_of(x_arena));
119 auto res_adj_exp_x = elt_multiply(res.adj(), exp_x);
120 adjoint_results(x_arena, lb_arena, ub_arena) += expressions(
121 select(lb_inf, select(ub_inf, res.adj(), lp.adj() - res_adj_exp_x),
122 select(ub_inf, res_adj_exp_x + lp.adj(),
124 elt_multiply(res.adj(), diff),
125 elt_multiply(inv_logit_x, one_m_inv_logit_x))
126 + lp.adj() * (1.0 - 2.0 * inv_logit_x))),
127 select(lb_inf, 0.0,
128 select(ub_inf, res.adj(),
129 elt_multiply(res.adj(), one_m_inv_logit_x)
130 - one_over_diff * lp.adj())),
131 select(ub_inf, 0.0,
132 select(lb_inf, res.adj(),
133 elt_multiply(res.adj(), inv_logit_x)
134 + one_over_diff * lp.adj())));
135 });
136}
137
138} // namespace math
139} // namespace stan
140
141#endif
142#endif
Represents an arithmetic matrix on the OpenCL device.
Definition matrix_cl.hpp:47
elt_multiply_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > elt_multiply(T_a &&a, T_b &&b)
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.
Definition select.hpp:148
elt_divide_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > elt_divide(T_a &&a, T_b &&b)
expressions_cl< T_expressions... > expressions(T_expressions &&... expressions)
Deduces types for constructing expressions_cl object.
require_all_t< is_prim_or_rev_kernel_expression< std::decay_t< Types > >... > require_all_prim_or_rev_kernel_expression_t
Require type satisfies is_prim_or_rev_kernel_expression.
require_any_not_t< is_stan_scalar< std::decay_t< Types > >... > require_any_not_stan_scalar_t
Require at least one of the types do not satisfy is_stan_scalar.
require_any_t< is_var< std::decay_t< Types > >... > require_any_var_t
Require any of the types satisfy is_var.
Definition is_var.hpp:39
adjoint_results_cl< T_results... > adjoint_results(T_results &&... results)
Deduces types for constructing adjoint_results_cl object.
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...
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
static constexpr double NEGATIVE_INFTY
Negative infinity.
Definition constants.hpp:51
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...
fvar< T > inv_logit(const fvar< T > &x)
Returns the inverse logit function applied to the argument.
Definition inv_logit.hpp:20
static constexpr double INFTY
Positive infinity.
Definition constants.hpp:46
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:13
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.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...