Automatic Differentiation
 
Loading...
Searching...
No Matches
stochastic_row_constrain.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_CONSTRAINT_STOCHASTIC_ROW_CONSTRAIN_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_STOCHASTIC_ROW_CONSTRAIN_HPP
3
11#include <cmath>
12
13namespace stan {
14namespace math {
15
25template <typename Mat, require_eigen_matrix_dynamic_t<Mat>* = nullptr,
26 require_not_st_var<Mat>* = nullptr>
28 auto&& y_ref = to_ref(y);
29 const Eigen::Index N = y_ref.rows();
30 int Km1 = y_ref.cols();
31 plain_type_t<Mat> x(N, Km1 + 1);
32 using eigen_arr = Eigen::Array<scalar_type_t<Mat>, -1, 1>;
33 eigen_arr stick_len = eigen_arr::Constant(N, 1.0);
34 for (Eigen::Index k = 0; k < Km1; ++k) {
35 auto z_k = inv_logit(y_ref.array().col(k) - log(Km1 - k));
36 x.array().col(k) = stick_len * z_k;
37 stick_len -= x.array().col(k);
38 }
39 x.array().col(Km1) = stick_len;
40 return x;
41}
42
53template <typename Mat, require_eigen_matrix_dynamic_t<Mat>* = nullptr,
54 require_not_st_var<Mat>* = nullptr>
57 auto&& y_ref = to_ref(y);
58 const Eigen::Index N = y_ref.rows();
59 Eigen::Index Km1 = y_ref.cols();
60 plain_type_t<Mat> x(N, Km1 + 1);
61 Eigen::Array<scalar_type_t<Mat>, -1, 1> stick_len
62 = Eigen::Array<scalar_type_t<Mat>, -1, 1>::Constant(N, 1.0);
63 for (Eigen::Index k = 0; k < Km1; ++k) {
64 const auto eq_share = -log(Km1 - k); // = logit(1.0/(Km1 + 1 - k));
65 auto adj_y_k = (y_ref.array().col(k) + eq_share).eval();
66 auto z_k = inv_logit(adj_y_k);
67 x.array().col(k) = stick_len * z_k;
68 lp += -sum(log1p_exp(adj_y_k)) - sum(log1p_exp(-adj_y_k))
69 + sum(log(stick_len));
70 stick_len -= x.array().col(k); // equivalently *= (1 - z_k);
71 }
72 x.col(Km1).array() = stick_len;
73 return x;
74}
75
92template <bool Jacobian, typename Mat, require_not_std_vector_t<Mat>* = nullptr>
95 if (Jacobian) {
96 return stochastic_row_constrain(y, lp);
97 } else {
99 }
100}
101
118template <bool Jacobian, typename T, require_std_vector_t<T>* = nullptr>
119inline auto stochastic_row_constrain(const T& y, return_type_t<T>& lp) {
121 y, [&lp](auto&& v) { return stochastic_row_constrain<Jacobian>(v, lp); });
122}
123
124} // namespace math
125} // namespace stan
126
127#endif
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
plain_type_t< Mat > stochastic_row_constrain(const Mat &y)
Return a row stochastic matrix.
T eval(T &&arg)
Inputs which have a plain_type equal to the own time are forwarded unmodified (for Eigen expressions ...
Definition eval.hpp:20
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
fvar< T > log1p_exp(const fvar< T > &x)
Definition log1p_exp.hpp:14
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
fvar< T > inv_logit(const fvar< T > &x)
Returns the inverse logit function applied to the argument.
Definition inv_logit.hpp:20
typename plain_type< T >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...