1 #ifndef STAN_MATH_REV_MAT_FUN_SIMPLEX_CONSTRAIN_HPP 2 #define STAN_MATH_REV_MAT_FUN_SIMPLEX_CONSTRAIN_HPP 35 template <std::
size_t size>
36 Eigen::VectorXd
operator()(
const std::array<bool, size>& needs_adj,
37 const Eigen::VectorXd& y) {
42 Eigen::Matrix<double, Eigen::Dynamic, 1> x(N_ + 1);
43 double stick_len(1.0);
44 for (
int k = 0; k < N_; ++k) {
45 double log_N_minus_k =
std::log(N_ - k);
47 diag_[k] = stick_len * z_[k] *
inv_logit(log_N_minus_k - y(k));
48 x(k) = stick_len * z_[k];
65 template <std::
size_t size>
67 const Eigen::VectorXd& adj)
const {
68 Eigen::VectorXd adj_times_jac(N_);
72 adj_times_jac(N_ - 1) = diag_[N_ - 1] * (adj(N_ - 1) - acc);
73 for (
int n = N_ - 1; --n >= 0;) {
74 acc = adj(n + 1) * z_[n + 1] + (1 - z_[n + 1]) * acc;
75 adj_times_jac(n) = diag_[n] * (adj(n) - acc);
79 return std::make_tuple(adj_times_jac);
96 const Eigen::Matrix<var, Eigen::Dynamic, 1>& y) {
97 return adj_jac_apply<internal::simplex_constrain_op>(y);
Eigen::VectorXd operator()(const std::array< bool, size > &needs_adj, const Eigen::VectorXd &y)
Return the simplex corresponding to the specified free vector.
fvar< T > log(const fvar< T > &x)
fvar< T > inv_logit(const fvar< T > &x)
Returns the inverse logit function applied to the argument.
static STAN_THREADS_DEF AutodiffStackStorage * instance_
auto multiply_adjoint_jacobian(const std::array< bool, size > &needs_adj, const Eigen::VectorXd &adj) const
T * alloc_array(size_t n)
Allocate an array on the arena of the specified size to hold values of the specified template paramet...
Eigen::Matrix< T, Eigen::Dynamic, 1 > simplex_constrain(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &y)
Return the simplex corresponding to the specified free vector.