Stan Math Library  2.20.0
reverse mode automatic differentiation
corr_matrix_constrain.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_CORR_MATRIX_CONSTRAIN_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_CORR_MATRIX_CONSTRAIN_HPP
3 
9 #include <stdexcept>
10 
11 namespace stan {
12 namespace math {
13 
38 template <typename T>
39 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> corr_matrix_constrain(
40  const Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
41  typename math::index_type<Eigen::Matrix<T, Eigen::Dynamic, 1> >::type k) {
42  using Eigen::Dynamic;
43  using Eigen::Matrix;
44  typedef typename index_type<Matrix<T, Dynamic, 1> >::type size_type;
45 
46  size_type k_choose_2 = (k * (k - 1)) / 2;
47  check_size_match("cov_matrix_constrain", "x.size()", x.size(), "k_choose_2",
48  k_choose_2);
49  Eigen::Array<T, Eigen::Dynamic, 1> cpcs(k_choose_2);
50  for (size_type i = 0; i < k_choose_2; ++i)
51  cpcs[i] = corr_constrain(x[i]);
52  return read_corr_matrix(cpcs, k);
53 }
54 
74 template <typename T>
75 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> corr_matrix_constrain(
76  const Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
77  typename math::index_type<Eigen::Matrix<T, Eigen::Dynamic, 1> >::type k,
78  T& lp) {
79  using Eigen::Array;
80  using Eigen::Dynamic;
81  using Eigen::Matrix;
82  typedef typename index_type<Matrix<T, Dynamic, 1> >::type size_type;
83 
84  size_type k_choose_2 = (k * (k - 1)) / 2;
85  check_size_match("cov_matrix_constrain", "x.size()", x.size(), "k_choose_2",
86  k_choose_2);
87  Array<T, Dynamic, 1> cpcs(k_choose_2);
88  for (size_type i = 0; i < k_choose_2; ++i)
89  cpcs[i] = corr_constrain(x[i], lp);
90  return read_corr_matrix(cpcs, k, lp);
91 }
92 
93 } // namespace math
94 } // namespace stan
95 #endif
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > corr_matrix_constrain(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &x, typename math::index_type< Eigen::Matrix< T, Eigen::Dynamic, 1 > >::type k)
Return the correlation matrix of the specified dimensionality derived from the specified vector of un...
T corr_constrain(const T &x)
Return the result of transforming the specified scalar to have a valid correlation value between -1 a...
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< double, Eigen::Dynamic, Eigen::Dynamic >::Index size_type
Type for sizes and indexes in an Eigen matrix with double e.
Definition: typedefs.hpp:11
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:18
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > read_corr_matrix(const Eigen::Array< T, Eigen::Dynamic, 1 > &CPCs, size_t K)
Return the correlation matrix of the specified dimensionality corresponding to the specified canonica...

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