Stan Math Library  2.10.0
reverse mode automatic differentiation
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 
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;
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  }
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;
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
const double LOG_2
The natural logarithm of 2, .
Definition: constants.hpp:33
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...
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
Eigen::Matrix< fvar< T >, R, R > multiply_lower_tri_self_transpose(const Eigen::Matrix< fvar< T >, R, C > &m)
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 > exp(const fvar< T > &x)
Definition: exp.hpp:10
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.

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