Stan Math Library  2.8.0
reverse mode automatic differentiation
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
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 
12  namespace math {
13 
28  template <typename T>
29  Eigen::Matrix<T, Eigen::Dynamic, 1>
30  simplex_free(const Eigen::Matrix<T, Eigen::Dynamic, 1>& x) {
31  using Eigen::Dynamic;
32  using Eigen::Matrix;
34  using stan::math::logit;
35  using std::log;
36 
37  typedef typename index_type<Matrix<T, Dynamic, 1> >::type size_type;
38 
39  stan::math::check_simplex("stan::math::simplex_free",
40  "Simplex variable", x);
41  int Km1 = x.size() - 1;
42  Eigen::Matrix<T, Eigen::Dynamic, 1> y(Km1);
43  T stick_len(x(Km1));
44  for (size_type k = Km1; --k >= 0; ) {
45  stick_len += x(k);
46  T z_k(x(k) / stick_len);
47  y(k) = logit(z_k) + log(Km1 - k);
48  // note: log(Km1 - k) = logit(1.0 / (Km1 + 1 - k));
49  }
50  return y;
51  }
52 
53  }
54 
55 }
56 
57 #endif
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
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:13
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:19
fvar< T > logit(const fvar< T > &x)
Definition: logit.hpp:17
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.
bool check_simplex(const char *function, const char *name, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta)
Return true if the specified vector is simplex.

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