Stan Math Library: stan/math/prim/mat/fun/cov_matrix_constrain.hpp Source File

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

cov_matrix_constrain.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_COV_MATRIX_CONSTRAIN_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_COV_MATRIX_CONSTRAIN_HPP
3
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/mat/meta/index_type.hpp>
6 #include <stan/math/prim/scal/fun/constants.hpp>
7 #include <stan/math/prim/mat/fun/multiply_lower_tri_self_transpose.hpp>
8 #include <cmath>
9 #include <stdexcept>
10
11 namespace stan {
12
13 namespace math {
14
15 // COVARIANCE MATRIX
16
29 template <typename T>
30 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
31 cov_matrix_constrain(const Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
32 typename math::index_type
33 <Eigen::Matrix<T, Eigen::Dynamic, 1> >::type K) {
34 using std::exp;
35
36 using Eigen::Dynamic;
37 using Eigen::Matrix;
38 using stan::math::index_type;
39 using stan::math::multiply_lower_tri_self_transpose;
40 typedef typename index_type<Matrix<T, Dynamic, Dynamic> >::type size_type;
41
42 Matrix<T, Dynamic, Dynamic> L(K, K);
43 if (x.size() != (K * (K + 1)) / 2)
44 throw std::domain_error("x.size() != K + (K choose 2)");
45 int i = 0;
46 for (size_type m = 0; m < K; ++m) {
47 for (int n = 0; n < m; ++n)
48 L(m, n) = x(i++);
49 L(m, m) = exp(x(i++));
50 for (size_type n = m + 1; n < K; ++n)
51 L(m, n) = 0.0;
52 }
53 return multiply_lower_tri_self_transpose(L);
54 }
55
56
69 template <typename T>
70 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
71 cov_matrix_constrain(const Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
72 typename math::index_type<Eigen::Matrix<T,
73 Eigen::Dynamic,
74 Eigen::Dynamic> >::type K,
75 T& lp) {
76 using std::exp;
77 using std::log;
78
79 using Eigen::Dynamic;
80 using Eigen::Matrix;
81 using stan::math::index_type;
82 typedef typename index_type<Matrix<T, Dynamic, Dynamic> >::type size_type;
83
84 if (x.size() != (K * (K + 1)) / 2)
85 throw std::domain_error("x.size() != K + (K choose 2)");
86 Matrix<T, Dynamic, Dynamic> L(K, K);
87 int i = 0;
88 for (size_type m = 0; m < K; ++m) {
89 for (size_type n = 0; n < m; ++n)
90 L(m, n) = x(i++);
91 L(m, m) = exp(x(i++));
92 for (size_type n = m + 1; n < K; ++n)
93 L(m, n) = 0.0;
94 }
95 // Jacobian for complete transform, including exp() above
96 lp += (K * stan::math::LOG_2); // needless constant; want propto
97 for (int k = 0; k < K; ++k)
98 lp += (K - k + 1) * log(L(k, k)); // only +1 because index from 0
99 return L * L.transpose();
100 // return tri_multiply_transpose(L);
101 }
102
103 }
104
105 }
106
107 #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:31
stan
Definition: log_sum_exp.hpp:8
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–2016, Stan Development Team.