Stan Math Library  2.6.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
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 
7 #include <boost/throw_exception.hpp>
8 #include <cmath>
9 #include <sstream>
10 #include <stdexcept>
11 
12 namespace stan {
13 
14  namespace math {
15 
36  template <typename T>
37  Eigen::Matrix<T, Eigen::Dynamic, 1>
38  corr_matrix_free(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>&
39  y) {
40  using Eigen::Array;
41  using Eigen::Dynamic;
42  using Eigen::Matrix;
44  typedef typename index_type<Matrix<T, Dynamic, 1> >::type size_type;
45 
46  size_type k = y.rows();
47  if (y.cols() != k)
48  throw std::domain_error("y is not a square matrix");
49  if (k == 0)
50  throw std::domain_error("y has no elements");
51  size_type k_choose_2 = (k * (k-1)) / 2;
52  Array<T, Dynamic, 1> x(k_choose_2);
53  Array<T, Dynamic, 1> sds(k);
54  bool successful = factor_cov_matrix(y, x, sds);
55  if (!successful)
56  throw std::runtime_error("factor_cov_matrix failed on y");
57  for (size_type i = 0; i < k; ++i) {
58  // sds on log scale unconstrained
59  if (fabs(sds[i] - 0.0) >= CONSTRAINT_TOLERANCE) {
60  std::stringstream s;
61  s << "all standard deviations must be zero."
62  << " found log(sd[" << i << "])=" << sds[i] << std::endl;
63  BOOST_THROW_EXCEPTION(std::runtime_error(s.str()));
64  }
65  }
66  return x.matrix();
67  }
68  }
69 
70 }
71 
72 #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–2015, Stan Development Team.