| Stan Math Library 2.6.3 probability, sampling & optimization

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
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
10 namespace stan {
11
12 namespace math {
13
14 // COVARIANCE MATRIX
15
28 template <typename T>
29 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
30 cov_matrix_constrain(const Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
31 typename math::index_type
32 <Eigen::Matrix<T, Eigen::Dynamic, 1> >::type K) {
33 using std::exp;
34
35 using Eigen::Dynamic;
36 using Eigen::Matrix;
37 using stan::math::index_type;
38 using stan::math::multiply_lower_tri_self_transpose;
39 typedef typename index_type<Matrix<T, Dynamic, Dynamic> >::type size_type;
40
41 Matrix<T, Dynamic, Dynamic> L(K, K);
42 if (x.size() != (K * (K + 1)) / 2)
43 throw std::domain_error("x.size() != K + (K choose 2)");
44 int i = 0;
45 for (size_type m = 0; m < K; ++m) {
46 for (int n = 0; n < m; ++n)
47 L(m, n) = x(i++);
48 L(m, m) = exp(x(i++));
49 for (size_type n = m + 1; n < K; ++n)
50 L(m, n) = 0.0;
51 }
52 return multiply_lower_tri_self_transpose(L);
53 }
54
55
68 template <typename T>
69 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
70 cov_matrix_constrain(const Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
71 typename math::index_type<Eigen::Matrix<T,
72 Eigen::Dynamic,
73 Eigen::Dynamic> >::type K,
74 T& lp) {
75 using std::exp;
76
77 using Eigen::Dynamic;
78 using Eigen::Matrix;
79 using stan::math::index_type;
80 typedef typename index_type<Matrix<T, Dynamic, Dynamic> >::type size_type;
81
82 if (x.size() != (K * (K + 1)) / 2)
83 throw std::domain_error("x.size() != K + (K choose 2)");
84 Matrix<T, Dynamic, Dynamic> L(K, K);
85 int i = 0;
86 for (size_type m = 0; m < K; ++m) {
87 for (size_type n = 0; n < m; ++n)
88 L(m, n) = x(i++);
89 L(m, m) = exp(x(i++));
90 for (size_type n = m + 1; n < K; ++n)
91 L(m, n) = 0.0;
92 }
93 // Jacobian for complete transform, including exp() above
94 lp += (K * stan::math::LOG_2); // needless constant; want propto
95 for (int k = 0; k < K; ++k)
96 lp += (K - k + 1) * log(L(k, k)); // only +1 because index from 0
97 return L * L.transpose();
98 // return tri_multiply_transpose(L);
99 }
100
101 }
102
103 }
104
105 #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.