Stan Math Library  2.20.0
reverse mode automatic differentiation
simplex_free.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_SIMPLEX_FREE_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_SIMPLEX_FREE_HPP
3 
8 #include <cmath>
9 
10 namespace stan {
11 namespace math {
12 
27 template <typename T>
28 Eigen::Matrix<T, Eigen::Dynamic, 1> simplex_free(
29  const Eigen::Matrix<T, Eigen::Dynamic, 1>& x) {
30  using Eigen::Dynamic;
31  using Eigen::Matrix;
32  using std::log;
33 
34  typedef typename index_type<Matrix<T, Dynamic, 1> >::type size_type;
35 
36  check_simplex("stan::math::simplex_free", "Simplex variable", x);
37  int Km1 = x.size() - 1;
38  Eigen::Matrix<T, Eigen::Dynamic, 1> y(Km1);
39  T stick_len(x(Km1));
40  for (size_type k = Km1; --k >= 0;) {
41  stick_len += x(k);
42  T z_k(x(k) / stick_len);
43  y(k) = logit(z_k) + log(Km1 - k);
44  // note: log(Km1 - k) = logit(1.0 / (Km1 + 1 - k));
45  }
46  return y;
47 }
48 
49 } // namespace math
50 } // namespace stan
51 #endif
void check_simplex(const char *function, const char *name, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta)
Check if the specified vector is simplex.
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:12
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Index size_type
Type for sizes and indexes in an Eigen matrix with double e.
Definition: typedefs.hpp:11
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:18
fvar< T > logit(const fvar< T > &x)
Definition: logit.hpp:14
Eigen::Matrix< T, Eigen::Dynamic, 1 > simplex_free(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &x)
Return an unconstrained vector that when transformed produces the specified simplex.

     [ Stan Home Page ] © 2011–2018, Stan Development Team.