Stan Math Library  2.20.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 
9 #include <cmath>
10 
11 namespace stan {
12 
13 namespace math {
14 
27 template <typename T>
28 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> cov_matrix_constrain(
29  const Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
30  typename math::index_type<Eigen::Matrix<T, Eigen::Dynamic, 1> >::type K) {
31  using Eigen::Dynamic;
32  using Eigen::Matrix;
33  using std::exp;
34  typedef typename index_type<Matrix<T, Dynamic, Dynamic> >::type index_t;
35 
36  Matrix<T, Dynamic, Dynamic> L(K, K);
37  check_size_match("cov_matrix_constrain", "x.size()", x.size(),
38  "K + (K choose 2)", (K * (K + 1)) / 2);
39  int i = 0;
40  for (index_t m = 0; m < K; ++m) {
41  for (int n = 0; n < m; ++n)
42  L(m, n) = x(i++);
43  L(m, m) = exp(x(i++));
44  for (index_t n = m + 1; n < K; ++n)
45  L(m, n) = 0.0;
46  }
48 }
49 
62 template <typename T>
63 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> cov_matrix_constrain(
64  const Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
65  typename math::index_type<
66  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> >::type K,
67  T& lp) {
68  using Eigen::Dynamic;
69  using Eigen::Matrix;
70  using std::exp;
71  using std::log;
72  typedef typename index_type<Matrix<T, Dynamic, Dynamic> >::type index_t;
73  check_size_match("cov_matrix_constrain", "x.size()", x.size(),
74  "K + (K choose 2)", (K * (K + 1)) / 2);
75  Matrix<T, Dynamic, Dynamic> L(K, K);
76  int i = 0;
77  for (index_t m = 0; m < K; ++m) {
78  for (index_t n = 0; n < m; ++n)
79  L(m, n) = x(i++);
80  L(m, m) = exp(x(i++));
81  for (index_t n = m + 1; n < K; ++n)
82  L(m, n) = 0.0;
83  }
84  // Jacobian for complete transform, including exp() above
85  lp += (K * LOG_2); // needless constant; want propto
86  for (index_t k = 0; k < K; ++k)
87  lp += (K - k + 1) * log(L(k, k)); // only +1 because index from 0
89 }
90 
91 } // namespace math
92 } // namespace stan
93 #endif
const double LOG_2
The natural logarithm of 2, .
Definition: constants.hpp:37
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:12
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
Eigen::Matrix< fvar< T >, R, R > multiply_lower_tri_self_transpose(const Eigen::Matrix< fvar< T >, R, C > &m)
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:18
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:11

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