Stan Math Library  2.11.0
reverse mode automatic differentiation
check_corr_matrix.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_ERR_CHECK_CORR_MATRIX_HPP
2 #define STAN_MATH_PRIM_MAT_ERR_CHECK_CORR_MATRIX_HPP
3 
13 #include <sstream>
14 #include <string>
15 
16 namespace stan {
17 
18  namespace math {
43  template <typename T_y>
44  inline bool
46  const char* function,
47  const char* name,
48  const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y
49  ) {
50  using Eigen::Matrix;
52 
53  typedef typename index_type<Matrix<
54  T_y, Eigen::Dynamic, Eigen::Dynamic> >::type size_t;
55 
56  check_size_match(function,
57  "Rows of correlation matrix", y.rows(),
58  "columns of correlation matrix", y.cols());
59  check_positive_size(function, name, "rows", y.rows());
60  check_symmetric(function, "y", y);
61 
62  for (size_t k = 0; k < y.rows(); ++k) {
63  if (!(fabs(y(k, k) - 1.0) <= CONSTRAINT_TOLERANCE)) {
64  std::ostringstream msg;
65  msg << "is not a valid correlation matrix. "
66  << name << "(" << stan::error_index::value + k
67  << "," << stan::error_index::value + k
68  << ") is ";
69  std::string msg_str(msg.str());
70  domain_error(function, name, y(k, k),
71  msg_str.c_str(),
72  ", but should be near 1.0");
73  return false;
74  }
75  }
76  stan::math::check_pos_definite(function, "y", y);
77  return true;
78  }
79 
80  }
81 }
82 #endif
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:14
bool check_positive_size(const char *function, const char *name, const char *expr, const int size)
Return true if size is positive.
const double CONSTRAINT_TOLERANCE
The tolerance for checking arithmetic bounds In rank and in simplexes.
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:19
bool check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Return true if the provided sizes match.
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.
bool check_corr_matrix(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Return true if the specified matrix is a valid correlation matrix.
bool check_pos_definite(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Return true if the specified square, symmetric matrix is positive definite.
bool check_symmetric(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Return true if the specified matrix is symmetric.

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