Stan Math Library  2.10.0
reverse mode automatic differentiation
corr_matrix_free.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_CORR_MATRIX_FREE_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_CORR_MATRIX_FREE_HPP
3 
8 #include <boost/throw_exception.hpp>
9 #include <cmath>
10 #include <sstream>
11 #include <stdexcept>
12 
13 namespace stan {
14 
15  namespace math {
16 
37  template <typename T>
38  Eigen::Matrix<T, Eigen::Dynamic, 1>
39  corr_matrix_free(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>&
40  y) {
41  using Eigen::Array;
42  using Eigen::Dynamic;
43  using Eigen::Matrix;
45  typedef typename index_type<Matrix<T, Dynamic, 1> >::type size_type;
46 
47  size_type k = y.rows();
48  if (y.cols() != k)
49  throw std::domain_error("y is not a square matrix");
50  if (k == 0)
51  throw std::domain_error("y has no elements");
52  size_type k_choose_2 = (k * (k-1)) / 2;
53  Array<T, Dynamic, 1> x(k_choose_2);
54  Array<T, Dynamic, 1> sds(k);
55  bool successful = factor_cov_matrix(y, x, sds);
56  if (!successful)
57  throw std::runtime_error("factor_cov_matrix failed on y");
58  for (size_type i = 0; i < k; ++i) {
59  // sds on log scale unconstrained
60  if (fabs(sds[i] - 0.0) >= CONSTRAINT_TOLERANCE) {
61  std::stringstream s;
62  s << "all standard deviations must be zero."
63  << " found log(sd[" << i << "])=" << sds[i] << std::endl;
64  BOOST_THROW_EXCEPTION(std::runtime_error(s.str()));
65  }
66  }
67  return x.matrix();
68  }
69  }
70 
71 }
72 
73 #endif
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:14
const double CONSTRAINT_TOLERANCE
The tolerance for checking arithmetic bounds In rank and in simplexes.
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.
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.
Eigen::Matrix< T, Eigen::Dynamic, 1 > corr_matrix_free(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &y)
Return the vector of unconstrained partial correlations that define the specified correlation matrix ...

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