Stan Math Library  2.6.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
cov_matrix_free_lkj.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_COV_MATRIX_FREE_LKJ_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_COV_MATRIX_FREE_LKJ_HPP
3 
6 #include <stdexcept>
7 
8 namespace stan {
9 
10  namespace math {
11 
30  template <typename T>
31  Eigen::Matrix<T, Eigen::Dynamic, 1>
33  const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& y) {
34  using Eigen::Array;
35  using Eigen::Dynamic;
36  using Eigen::Matrix;
38  typedef typename index_type<Matrix<T, Dynamic, Dynamic> >::type size_type;
39 
40  size_type k = y.rows();
41  if (y.cols() != k)
42  throw std::domain_error("y is not a square matrix");
43  if (k == 0)
44  throw std::domain_error("y has no elements");
45  size_type k_choose_2 = (k * (k-1)) / 2;
46  Array<T, Dynamic, 1> cpcs(k_choose_2);
47  Array<T, Dynamic, 1> sds(k);
48  bool successful = factor_cov_matrix(y, cpcs, sds);
49  if (!successful)
50  throw std::runtime_error("factor_cov_matrix failed on y");
51  Matrix<T, Dynamic, 1> x(k_choose_2 + k);
52  size_type pos = 0;
53  for (size_type i = 0; i < k_choose_2; ++i)
54  x[pos++] = cpcs[i];
55  for (size_type i = 0; i < k; ++i)
56  x[pos++] = sds[i];
57  return x;
58  }
59 
60  }
61 
62 }
63 
64 #endif
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
bool factor_cov_matrix(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &Sigma, Eigen::Array< T, Eigen::Dynamic, 1 > &CPCs, Eigen::Array< T, Eigen::Dynamic, 1 > &sds)
This function is intended to make starting values, given a covariance matrix Sigma.
Eigen::Matrix< T, Eigen::Dynamic, 1 > cov_matrix_free_lkj(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &y)
Return the vector of unconstrained partial correlations and deviations that transform to the specifie...
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–2015, Stan Development Team.