All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
cov_matrix_constrain.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_MAT_FUN_COV_MATRIX_CONSTRAIN_HPP
#define STAN_MATH_PRIM_MAT_FUN_COV_MATRIX_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/constants.hpp>
#include <stan/math/prim/mat/fun/multiply_lower_tri_self_transpose.hpp>
#include <cmath>
namespace stan {
namespace math {
// COVARIANCE MATRIX
template <typename T>
Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
cov_matrix_constrain(const Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
typename math::index_type
<Eigen::Matrix<T, Eigen::Dynamic, 1> >::type K) {
using std::exp;
using Eigen::Dynamic;
using Eigen::Matrix;
using stan::math::index_type;
using stan::math::multiply_lower_tri_self_transpose;
typedef typename index_type<Matrix<T, Dynamic, Dynamic> >::type size_type;
Matrix<T, Dynamic, Dynamic> L(K, K);
if (x.size() != (K * (K + 1)) / 2)
throw std::domain_error("x.size() != K + (K choose 2)");
int i = 0;
for (size_type m = 0; m < K; ++m) {
for (int n = 0; n < m; ++n)
L(m, n) = x(i++);
L(m, m) = exp(x(i++));
for (size_type n = m + 1; n < K; ++n)
L(m, n) = 0.0;
}
return multiply_lower_tri_self_transpose(L);
}
template <typename T>
Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
cov_matrix_constrain(const Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
typename math::index_type<Eigen::Matrix<T,
Eigen::Dynamic,
Eigen::Dynamic> >::type K,
T& lp) {
using std::exp;
using std::log;
using Eigen::Dynamic;
using Eigen::Matrix;
using stan::math::index_type;
typedef typename index_type<Matrix<T, Dynamic, Dynamic> >::type size_type;
if (x.size() != (K * (K + 1)) / 2)
throw std::domain_error("x.size() != K + (K choose 2)");
Matrix<T, Dynamic, Dynamic> L(K, K);
int i = 0;
for (size_type m = 0; m < K; ++m) {
for (size_type n = 0; n < m; ++n)
L(m, n) = x(i++);
L(m, m) = exp(x(i++));
for (size_type n = m + 1; n < K; ++n)
L(m, n) = 0.0;
}
// Jacobian for complete transform, including exp() above
lp += (K * stan::math::LOG_2); // needless constant; want propto
for (int k = 0; k < K; ++k)
lp += (K - k + 1) * log(L(k, k)); // only +1 because index from 0
return L * L.transpose();
// return tri_multiply_transpose(L);
}
}
}
#endif
stan::math::LOG_2
const double LOG_2
The natural logarithm of 2, .
Definition: constants.hpp:33
multiply_lower_tri_self_transpose.hpp
stan::math::cov_matrix_constrain
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > cov_matrix_constrain(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &x, typename math::index_type< Eigen::Matrix< T, Eigen::Dynamic, 1 > >::type K)
Return the symmetric, positive-definite matrix of dimensions K by K resulting from transforming the s...
Definition: cov_matrix_constrain.hpp:30
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
stan::math::multiply_lower_tri_self_transpose
Eigen::Matrix< fvar< T >, R, R > multiply_lower_tri_self_transpose(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: multiply_lower_tri_self_transpose.hpp:17
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
stan::math::exp
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
stan::math::domain_error
void domain_error(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw a domain error with a consistently formatted message.
Definition: domain_error.hpp:32
Eigen.hpp
constants.hpp

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