All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
simplex_constrain.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_MAT_FUN_SIMPLEX_CONSTRAIN_HPP
#define STAN_MATH_PRIM_MAT_FUN_SIMPLEX_CONSTRAIN_HPP
#include <stan/math/prim/mat/fun/Eigen.hpp>
#include <stan/math/prim/mat/meta/index_type.hpp>
#include <stan/math/prim/scal/fun/inv_logit.hpp>
#include <stan/math/prim/scal/fun/log1m.hpp>
#include <stan/math/prim/scal/fun/log1p_exp.hpp>
#include <stan/math/prim/scal/fun/logit.hpp>
#include <cmath>
namespace stan {
namespace math {
template <typename T>
Eigen::Matrix<T, Eigen::Dynamic, 1>
simplex_constrain(const Eigen::Matrix<T, Eigen::Dynamic, 1>& y) {
// cut & paste simplex_constrain(Eigen::Matrix, T) w/o Jacobian
using Eigen::Matrix;
using Eigen::Dynamic;
using stan::math::index_type;
using stan::math::inv_logit;
using stan::math::logit;
using stan::math::log1m;
using std::log;
typedef typename index_type<Matrix<T, Dynamic, 1> >::type size_type;
int Km1 = y.size();
Matrix<T, Dynamic, 1> x(Km1 + 1);
T stick_len(1.0);
for (size_type k = 0; k < Km1; ++k) {
T z_k(inv_logit(y(k) - log(Km1 - k)));
x(k) = stick_len * z_k;
stick_len -= x(k);
}
x(Km1) = stick_len;
return x;
}
template <typename T>
Eigen::Matrix<T, Eigen::Dynamic, 1>
simplex_constrain(const Eigen::Matrix<T, Eigen::Dynamic, 1>& y,
T& lp) {
using Eigen::Dynamic;
using Eigen::Matrix;
using stan::math::index_type;
using stan::math::inv_logit;
using stan::math::logit;
using stan::math::log1m;
using stan::math::log1p_exp;
using std::log;
typedef typename index_type<Matrix<T, Dynamic, 1> >::type size_type;
int Km1 = y.size(); // K = Km1 + 1
Matrix<T, Dynamic, 1> x(Km1 + 1);
T stick_len(1.0);
for (size_type k = 0; k < Km1; ++k) {
double eq_share = -log(Km1 - k); // = logit(1.0/(Km1 + 1 - k));
T adj_y_k(y(k) + eq_share);
T z_k(inv_logit(adj_y_k));
x(k) = stick_len * z_k;
lp += log(stick_len);
lp -= log1p_exp(-adj_y_k);
lp -= log1p_exp(adj_y_k);
stick_len -= x(k); // equivalently *= (1 - z_k);
}
x(Km1) = stick_len; // no Jacobian contrib for last dim
return x;
}
}
}
#endif
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
stan::math::inv_logit
fvar< T > inv_logit(const fvar< T > &x)
Definition: inv_logit.hpp:15
stan::math::size_type
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
stan::math::index_type
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:19
index_type.hpp
log1m.hpp
Eigen.hpp
stan::math::logit
fvar< T > logit(const fvar< T > &x)
Definition: logit.hpp:17
log1p_exp.hpp
stan::math::log1p_exp
fvar< T > log1p_exp(const fvar< T > &x)
Definition: log1p_exp.hpp:13
stan::math::simplex_constrain
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.
Definition: simplex_constrain.hpp:30
logit.hpp
inv_logit.hpp
stan::math::log1m
fvar< T > log1m(const fvar< T > &x)
Definition: log1m.hpp:16

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